source: scripts/autobuild.bash @ 3001

Revision 3001, 14.8 KB checked in by alanbach-guest, 6 years ago (diff)
  • Fixed malfored apt line error
  • Property svn:executable set to *
Line 
1#!/bin/bash
2# Helper bash script to build debian biarch i386 and amd64 packages from SVN
3# Requires: subversion, devcripts, svn-buildpackage, pbuilder, awk, reprepro, sed, gnupg, sendmail.pl
4# Coyright 2007 Alan Baghumian / GNU/GPL
5#
6# Created: 2007-10-20 15:10 +330 GMT
7# Updated: 2008-02-01 19:30 +330 GMT
8#
9# TODO:
10# 1) bz2 packages?
11
12SRC_PKG_NAME="$1"
13DIST_NAME="$2"
14UNDERLINE=$'\137' # 137 is octal ASCII code for '_'
15
16# Define some colors
17GREEN="[1;32m"
18YELLOW="[1;33m"
19BLUE="[1;34m"
20MAGENTA="[1;35m"
21CYAN="[1;36m"
22WHITE="[1;37m"
23RED="[1;31m"
24NORMAL="[0;39m"
25
26# Load configuration file
27if [ -f ./autobuild.conf ]; then
28    source ./autobuild.conf
29else
30    echo "${GREEN}Couldn't load configuration file, exiting...${NORMAL}"
31    exit     
32fi
33
34# Define core functions
35
36# Reverts /etc/pbuilderrc base.tgz changes
37revert_pbrc()
38{
39if [ ! -z $PXBUILDARCH ]; then
40   sed -i s/base-$PXBUILDARCH.tgz/base.tgz/g /etc/pbuilderrc
41else
42   # Static revert
43   sed -i s/base-i386.tgz/base.tgz/g /etc/pbuilderrc
44   sed -i s/base-amd64.tgz/base.tgz/g /etc/pbuilderrc
45fi
46}
47
48# Fixes /etc/pbuilderrc base.tgz
49fix_pbrc()
50{
51sed -i s/base.tgz/base-$PXBUILDARCH.tgz/g /etc/pbuilderrc
52}
53
54# Builds or updates needed chroots
55handle_chroot()
56{
57if [ ! -f /var/cache/pbuilder/base-$PXBUILDARCH.tgz ]; then
58    echo "${GREEN}Creating ${YELLOW}$PXBUILDARCH${NORMAL} ${GREEN}chroot...${NORMAL}"
59    pbuilder --create --distribution $DIST_NAME --override-config --debootstrap debootstrap --debootstrapopts --arch --debootstrapopts $PXBUILDARCH --logfile $LOG_FILE --mirror $APT_MIRROR --othermirror "$APT_MIRROR_OTHER" &> /dev/null || exit
60else
61    echo "${GREEN}Updating ${YELLOW}$PXBUILDARCH${NORMAL} ${GREEN}chroot...${NORMAL}"
62    pbuilder --update --distribution $DIST_NAME --override-config --debootstrap debootstrap --debootstrapopts --arch --debootstrapopts $PXBUILDARCH --logfile $LOG_FILE --mirror $APT_MIRROR --othermirror "$APT_MIRROR_OTHER" &> /dev/null || exit
63fi
64}
65
66# This takes three arguments: the name of the file to sign, the key
67# or maintainer name to use and password. Based On Debian's debsign
68signfile () {
69    signcommand="gpg"
70    signinterface="gpg"
71    if [ $signinterface = gpg ]
72    then
73        gpgversion=`gpg --version | head -n 1 | cut -d' ' -f3`
74        gpgmajorversion=`echo $gpgversion | cut -d. -f1`
75        gpgminorversion=`echo $gpgversion | cut -d. -f2`
76        if [ $gpgmajorversion -gt 1 -o $gpgminorversion -ge 4 ]
77        then
78                (cat "$1" ; echo "") | \
79                    $signcommand --no-tty --batch --passphrase "$3" --local-user "$2" --clearsign \
80                    --list-options no-show-policy-urls \
81                    --armor --textmode --output - - > "$1.asc" || exit
82        else
83                (cat "$1" ; echo "") | \
84                    $signcommand --no-tty --batch --passphrase "$3" --local-user "$2" --clearsign \
85                        --no-show-policy-url \
86                        --armor --textmode --output - - > "$1.asc" || exit
87        fi
88    else
89        $signcommand -u "$2" +clearsig=on -fast < "$1" > "$1.asc"
90    fi
91
92    echo
93    PRECIOUS_FILES=$(($PRECIOUS_FILES + 1))
94    mv -f -- "$1.asc" "$1"
95}
96
97# Checking required inputs
98if [ -z "$1" ]; then
99    echo "${GREEN}Couldn't detect source package name, exiting...${NORMAL}"
100    exit
101else
102    echo "${GREEN}Building ${YELLOW}$SRC_PKG_NAME${GREEN}...${NORMAL}"
103fi
104
105if [ -z "$2" ]; then
106    echo "${GREEN}Couldn't detect distro name, exiting...${NORMAL}"
107    exit
108fi
109
110# Initial Revert
111revert_pbrc
112
113cd $LOCAL_REPO_PATH
114POOL=`find ./ -type d | grep /$SRC_PKG_NAME$ | awk 'BEGIN{FS="/"}{print $3}'`
115
116if [ -z "$POOL" ]; then
117      echo "${GREEN}Couldn't detect pool folder name, trying to calculate...${NORMAL}"
118
119      case "$SRC_PKG_NAME" in
120            lib*)
121                POOL=`echo $SRC_PKG_NAME | cut -c1-4`
122            ;;
123            *)
124                POOL=`echo $SRC_PKG_NAME | cut -c1`
125            ;;
126      esac
127      echo "${GREEN}Detected pool as: ${YELLOW}$POOL${NORMAL}"
128fi
129
130cd $SVN_REPO_PATH
131COMPONENT=`find ./ -maxdepth 2 -type d | grep /$SRC_PKG_NAME$ | awk 'BEGIN{FS="/"}{print $2}'`
132
133if [ ! -z "$COMPONENT" ]; then
134    echo "${GREEN}Detected component: ${YELLOW}$COMPONENT${NORMAL}"
135else
136    echo "${GREEN}Couldn't detect component, updating SVN...${NORMAL}"
137    # Local working copy is out dated?
138    svn up --username $SVN_USER --password $SVN_PASS &> /dev/null
139    COMPONENT=`find ./ -maxdepth 2 -type d | grep /$SRC_PKG_NAME$ | awk 'BEGIN{FS="/"}{print $2}'`
140fi
141
142# Check if package exists on the SVN repo
143if [ -d $SVN_REPO_PATH/$COMPONENT/$SRC_PKG_NAME ]; then
144    cd $SVN_REPO_PATH/$COMPONENT/$SRC_PKG_NAME
145else
146    echo "${GREEN}SVN directory does not exists, exiting...${NORMAL}"
147    exit
148fi
149
150# Update SVN to the latest version
151rm `svn st | awk '{print $2}'` &> /dev/null
152svn up --username $SVN_USER --password $SVN_PASS &> /dev/null
153
154echo "${GREEN}Parsing changelog to get the latest prepared version...${NORMAL} "
155PKG_VERSION=`dpkg-parsechangelog -ltrunk/debian/changelog  | grep Version | awk 'BEGIN{FS=": "}{print $2}' | awk 'BEGIN{FS="-"}{print $1}' | awk 'BEGIN{FS=" "}{print $1}'`
156PKG_REVISION=`dpkg-parsechangelog -ltrunk/debian/changelog  | grep Version | awk 'BEGIN{FS=": "}{print $2}' | awk 'BEGIN{FS="-"}{print $2}'`
157
158if [ ! -z $PKG_REVISION ]; then
159    echo "${YELLOW}$PKG_VERSION-$PKG_REVISION${NORMAL}"
160    TARBALL_EXT="orig.tar.gz"
161    NATIVE_PKG=0
162else
163    echo "${YELLOW}$PKG_VERSION${NORMAL}"
164    TARBALL_EXT="tar.gz"
165    NATIVE_PKG=1
166fi
167
168echo "${GREEN}Getting the latest upstream version...${NORMAL}"
169UPSTREAM_VERSION=`uscan --report --dehs --check-dirname-level 0 trunk/ | grep upstream-version | awk 'BEGIN{FS=">"}{print $2}' | awk 'BEGIN{FS="<"}{print $1}'`
170UPSTREAM_TARBALL_URL=`uscan --report --dehs --check-dirname-level 0 trunk/ | grep upstream-url | awk 'BEGIN{FS=">"}{print $2}' | awk 'BEGIN{FS="<"}{print $1}'`
171
172if [ ! -z `echo "$PKG_VERSION" | awk 'BEGIN{FS=":"}{print $2}'` ]; then
173   PKG_VERSION=`echo "$PKG_VERSION" | awk 'BEGIN{FS=":"}{print $2}'`
174fi
175
176if [ ! "$UPSTREAM_VERSION" = "$PKG_VERSION" ]; then
177   UPSTREAM_VERSION="$PKG_VERSION"
178   echo "${YELLOW}Prepared $UPSTREAM_VERSION${NORMAL}"
179else
180   echo "${YELLOW}$UPSTREAM_VERSION${NORMAL}"
181fi
182
183# Log filename
184if [ ! -z $PKG_REVISION ]; then
185     LOG_FILE="$LOCAL_REPO_ROOT../buildlogs/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION-$PKG_REVISION.blog"
186else
187     LOG_FILE="$LOCAL_REPO_ROOT../buildlogs/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.blog"
188fi
189
190LOG_PATH="$LOCAL_REPO_ROOT../buildlogs"
191
192# Clean up old logs
193rm $LOG_PATH/$SRC_PKG_NAME* &> /dev/null
194
195# clean up untarred packages
196rm -r $SRC_PKG_NAME-* &> /dev/null
197rm -r build-area/* &> /dev/null
198rm *.tar.gz &> /dev/null
199
200# Make tarballs
201mkdir -p tarballs &> /dev/null
202mkdir -p build-area &> /dev/null
203
204# Package is not native, get tarballs
205if [ $NATIVE_PKG -eq 0 ]; then
206     if [ -f "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" ]; then
207         echo "${GREEN}Copying orig tarball to the tarballs directory...${NORMAL}"
208         cp "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" tarballs/ -v >> $LOG_FILE
209     else
210         # try to get upstream tarball using uscan
211         uscan --rename --no-dehs --check-dirname-level 0 trunk/ >> $LOG_FILE
212
213         # if fails, try classic manual way
214         if [ ! -f "$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" ]; then
215            if [ ! -z $UPSTREAM_TARBALL_URL ]; then
216                 wget -c $UPSTREAM_TARBALL_URL &> /dev/null
217                 FRESH_TARBALL=`ls | grep .tar.gz$`
218            else
219                 echo "${GREEN}Upstream tarball URL is empty, exiting...${NORMAL}"
220                 echo "Upstream tarball URL is empty, exiting..." >> $LOG_FILE
221            fi
222
223            if [ ! -z "$FRESH_TARBALL" ]; then
224               mv "$FRESH_TARBALL" "$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" -v >> $LOG_FILE
225            else
226               echo "${GREEN}Can't find fresh tarball file, exiting...${NORMAL}"
227               echo "Can't find fresh tarball file, exiting..." >> $LOG_FILE
228               exit
229            fi
230         fi
231
232             echo "${GREEN}Copying orig tarball to the local apt repo...${NORMAL}"
233
234             # Not exists? Create it!
235             if [ ! -d "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME" ]; then
236                  mkdir -p "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME"
237             fi
238
239             cp *."$TARBALL_EXT" "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME/" -v >> $LOG_FILE
240
241             # Copy tarball
242             cp *.$TARBALL_EXT tarballs/ -v >> $LOG_FILE
243
244         # Clean up untarred packages
245         rm -r $SRC_PKG_NAME-* &> /dev/null
246     fi
247
248     # Check if both tarballs exist
249     if [ ! -f "tarballs/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" ]; then
250          echo "${RED}Source tarball not found${NORMAL}" && exit
251     fi
252
253     if [ ! -f "$LOCAL_REPO_PATH/$COMPONENT/$POOL/$SRC_PKG_NAME/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.$TARBALL_EXT" ]; then
254          echo "${RED}Source tarball not found on repo${NORMAL}" && exit
255     fi
256fi
257
258# Goto trunk to start build
259cd trunk/
260
261# Fixing /etc/pbuilderrc base.tgz
262PXBUILDARCH="amd64"
263fix_pbrc
264
265# Build amd64 chroot if needed
266handle_chroot
267
268# Build amd64 packages
269echo "${GREEN}Building ${YELLOW}amd64${NORMAL} ${GREEN}specific packages...${NORMAL}"
270svn-buildpackage --svn-ignore --svn-noninteractive --svn-builder="pdebuild --use-pdebuild-internal -- --logfile $LOG_FILE" || exit
271
272# Revert /etc/pbuilderrc base.tgz changes
273revert_pbrc
274
275# Check if the result is platform independent and skip i386 build if needed
276ARCH_ALL_COUNT="$(ls ../build-area/ | grep -c _all.deb)"
277ARCH_I386_COUNT="$(ls ../build-area/ | grep -c _i386.deb)"
278ARCH_AMD64_COUNT="$(ls ../build-area/ | grep -c _amd64.deb)"
279
280# SVN Tagging: amd64 buid was OK, so tag it
281cd ..
282if [ $NATIVE_PKG -eq 0 ]; then
283     if [ ! -d tags ]; then
284         mkdir tags &> /dev/null
285         svn add tags &> /dev/null
286     fi
287
288     if [ ! -d tags/$UPSTREAM_VERSION-$PKG_REVISION ]; then
289         echo "${GREEN}Tagging ${YELLOW}$SRC_PKG_NAME $UPSTREAM_VERSION-$PKG_REVISION${NORMAL} ${GREEN}...${NORMAL}"
290         echo "${GREEN}Tagging $SRC_PKG_NAME $UPSTREAM_VERSION-$PKG_REVISION" >> $LOG_FILE
291         svn copy trunk/ tags/$UPSTREAM_VERSION-$PKG_REVISION &> /dev/null
292         svn commit -m "* BUILDD: Tagging $SRC_PKG_NAME $UPSTREAM_VERSION-$PKG_REVISION" --username $SVN_USER --password $SVN_PASS &> /dev/null
293     fi
294else
295     if [ ! -d tags ]; then
296         mkdir tags &> /dev/null
297         svn add tags &> /dev/null
298     fi
299
300     if [ ! -d tags/$UPSTREAM_VERSION ]; then
301         echo "${GREEN}Tagging ${YELLOW}$SRC_PKG_NAME $UPSTREAM_VERSION${NORMAL} ${GREEN}...${NORMAL}"
302         echo "${GREEN}Tagging $SRC_PKG_NAME $UPSTREAM_VERSION" >> $LOG_FILE
303         svn copy trunk/ tags/$UPSTREAM_VERSION &> /dev/null
304         svn commit -m "* BUILDD: Tagging $SRC_PKG_NAME $UPSTREAM_VERSION" --username $SVN_USER --password $SVN_PASS &> /dev/null
305     fi
306fi
307
308if [ $ARCH_ALL_COUNT -gt 0 -a $ARCH_I386_COUNT -lt 1 -a $ARCH_AMD64_COUNT -lt 1 ]; then
309    echo "${GREEN}Build is platform independent, skipping ${YELLOW}i386${NORMAL} ${GREEN}build...${NORMAL}"
310    ARCH_INDEP=1
311    cd build-area/
312else
313    ARCH_INDEP=0
314
315    cd trunk/
316
317    # Fixing /etc/pbuilderrc base.tgz
318    PXBUILDARCH="i386"
319    fix_pbrc
320
321    # Build i386 chroot if needed
322    handle_chroot
323
324    # Restore the latest SVN version
325    rm `svn st | awk '{print $2}'` &> /dev/null
326    svn up --username $SVN_USER --password $SVN_PASS &> /dev/null
327
328    # Build i386 packages
329    echo "${GREEN}Building ${YELLOW}i386${NORMAL} ${GREEN}specific packages...${NORMAL}"
330    svn-buildpackage --svn-ignore --svn-noninteractive --svn-builder="pdebuild --use-pdebuild-internal --debbuildopts -B -- --logfile $LOG_FILE" || exit
331
332    # Revert /etc/pbuilderrc base.tgz changes
333    revert_pbrc
334
335    cd ../build-area/
336fi
337
338if [ $ARCH_INDEP -eq 0 ]; then
339    echo "${GREEN}Signing and adding ${YELLOW}multi_arch${NORMAL} ${GREEN}packages to the APT repository...${NORMAL}"
340
341    # Merging changes file for multiarch upload
342    AMD64_CHANGES=`ls | grep _amd64.changes`
343    I386_CHANGES=`ls | grep _i386.changes`
344    mergechanges -f $AMD64_CHANGES $I386_CHANGES >> $LOG_FILE
345
346    CHANGES_FILE=`ls | grep _multi.changes$`
347    DSC_FILE=`ls | grep .dsc$`
348
349    if [ -z "$CHANGES_FILE" ]; then
350        echo "${GREEN}No ${YELLOW}changes${NORMAL} ${GREEN}file?${NORMAL}"
351        echo "No changes file" >> $LOG_FILE
352        echo "Build failed" >> $LOG_FILE
353        revert_pbrc
354        exit
355    fi
356
357    DSC_OLDMD5SUM=`md5sum $DSC_FILE | awk '{print $1}'`
358    DSC_OLDSIZE=`ls -l $DSC_FILE | awk '{print $5}'`
359
360    # Sign dsc file
361    signfile "$DSC_FILE" "$GPG_KEY" "$GPG_PASS" &> /dev/null
362   
363    DSC_MD5SUM=`md5sum $DSC_FILE | awk '{print $1}'`
364    DSC_SIZE=`ls -l $DSC_FILE | awk '{print $5}'`
365
366    sed -i s/" $DSC_OLDMD5SUM "/" $DSC_MD5SUM "/g $CHANGES_FILE || exit
367    sed -i s/" $DSC_OLDSIZE "/" $DSC_SIZE "/g $CHANGES_FILE || exit
368
369    # Sign changes file
370    signfile "$CHANGES_FILE" "$GPG_KEY" "$GPG_PASS" &> /dev/null
371
372    # Add package to the repo
373    cd $LOCAL_REPO_ROOT
374    reprepro --ignore=wrongdistribution -Vb . include $DIST_NAME $SVN_REPO_PATH/$COMPONENT/$SRC_PKG_NAME/build-area/$CHANGES_FILE &> /dev/null
375else
376    echo "${GREEN}Signing and adding ${YELLOW}plat_indep${NORMAL} ${GREEN}packages to the APT repository...${NORMAL}"
377
378    CHANGES_FILE=`ls | grep _amd64.changes$`
379 
380    if [ -z "$CHANGES_FILE" ]; then
381        echo "${GREEN}No ${YELLOW}changes${NORMAL} ${GREEN}file?${NORMAL}"
382        echo "No changes file" >> $LOG_FILE
383        echo "Build failed" >> $LOG_FILE
384        revert_pbrc
385        exit
386    fi
387 
388    DSC_FILE=`ls | grep .dsc$`
389
390    DSC_OLDMD5SUM=`md5sum $DSC_FILE | awk '{print $1}'`
391    DSC_OLDSIZE=`ls -l $DSC_FILE | awk '{print $5}'`
392
393    # Sign dsc file
394    signfile "$DSC_FILE" "$GPG_KEY" "$GPG_PASS" &> /dev/null
395 
396    DSC_MD5SUM=`md5sum $DSC_FILE | awk '{print $1}'`
397    DSC_SIZE=`ls -l $DSC_FILE | awk '{print $5}' `
398 
399    sed -i s/" $DSC_OLDMD5SUM "/" $DSC_MD5SUM "/g $CHANGES_FILE || exit
400    sed -i s/" $DSC_OLDSIZE "/" $DSC_SIZE "/g $CHANGES_FILE || exit
401
402    # Sign changes file
403    signfile "$CHANGES_FILE" "$GPG_KEY" "$GPG_PASS" &> /dev/null
404 
405    # Add package to the repo
406    cd $LOCAL_REPO_ROOT
407    reprepro --ignore=wrongdistribution -Vb . include $DIST_NAME $SVN_REPO_PATH/$COMPONENT/$SRC_PKG_NAME/build-area/$CHANGES_FILE &> /dev/null
408fi
409
410# Sign release files
411sh ../../sign &> /dev/null && cd
412
413# Sending mail
414if [ $NATIVE_PKG -eq 0 ]; then
415     $MAILER "$MAIL_SENDER" "$MAILING_LIST" "$SRC_PKG_NAME-$PKG_VERSION-$PKG_REVISION Build Confirmation" "$SRC_PKG_NAME-$PKG_VERSION-$PKG_REVISION has been built successfully. For more information see log file at http://buildd.parsix.org/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION-$PKG_REVISION.blog"
416else
417     $MAILER "$MAIL_SENDER" "$MAILING_LIST" "$SRC_PKG_NAME-$PKG_VERSION Build Confirmation" "$SRC_PKG_NAME-$PKG_VERSION has been built successfully. For more information see log file at http://buildd.parsix.org/$SRC_PKG_NAME$UNDERLINE$UPSTREAM_VERSION.blog"
418fi
419
420echo "${GREEN}Finished.${NORMAL}"
421echo "Build OK" >> $LOG_FILE
422
Note: See TracBrowser for help on using the repository browser.