source: scripts/autobuild.bash @ 2501

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