Remove the pbuilder code
[buildd-scripts.git] / bin / build-client
1 #!/bin/bash
2
3 INPUT=/home/steve/build-list
4 LOGDIR=/chroot/rebuildd/logs
5 PBUILD_CACHE=/var/cache/pbuilder
6 PBUILD_OUT=/chroot/rebuildd/out
7 HOSTNAME=$(hostname)
8 BUILDS_DONE=0
9 MAX_BUILDS=0
10 JACK_SCRIPT=/home/build/bin/build-client
11 if [ "$1"x != ""x ]; then
12     MAX_BUILDS=$1
13 fi
14
15 cd /chroot/rebuildd
16
17 # Make sure we're building in our temp space, not in /var/lib
18 mkdir -p /chroot/rebuildd/schroot/unpack /chroot/rebuildd/sbuild/build
19 mount | grep -q /var/lib/schroot/unpack
20 if [ $? -ne 0 ]; then
21     mount --bind /chroot/rebuildd/schroot/unpack /var/lib/schroot/unpack
22 fi
23 mount | grep -q /var/lib/sbuild/build
24 if [ $? -ne 0 ]; then
25     mount --bind /chroot/rebuildd/sbuild/build /var/lib/sbuild/build
26 fi
27
28 attempt_update () {
29     rsync -a build@jack:build-config/ /etc/
30     chown -R root: /etc/schroot/chroot.d/
31     rsync -a --delete build@jack:chroots/ /chroot/rebuildd/chroots/
32     apt-get update
33     
34     MY_MD5=$(md5sum $0 | awk '{print $1}')
35     JACK_MD5=$(ssh build@jack md5sum $JACK_SCRIPT | awk '{print $1}')
36     if [ "$MY_MD5" = "$JACK_MD5" ]; then
37         return 0
38     fi
39
40     if [ x"$JACK_MD5" == x"" ]; then
41         return 0
42     fi    
43
44     # Different. Assumg that the one on jack is correct!
45     DATE=$(date)
46     echo "$DATE: Grabbing a new build-client script from jack"
47     echo "Old MD5 $MY_MD5, new MD5 $JACK_MD5"
48     
49     # Remove myself, so I can copy a new file in place
50     rm -f $0
51     rsync build@jack:$JACK_SCRIPT $0
52
53     # Now re-exec the new script
54     exec $0 $@
55 }
56
57 grab_and_build () {
58     BNUM=$1
59     PKG=$2
60     VER=$3
61     ARCH=$4
62     local error
63
64     DATE=$(date)
65     echo "$DATE: starting ${BNUM}: build of ${PKG} version ${VER} arch ${ARCH} on $HOSTNAME"
66     echo "Running apt-get -q --download-only --only-source source ${PKG}=${VER}"
67     apt-get -q --download-only --only-source source ${PKG}=${VER}
68     error=$?
69     ls -l
70     if [ $error -ne 0 ]; then
71         echo "Status: given-back"
72         return 1
73     fi
74
75     echo 1 > /proc/sys/abi/swp || true
76     
77     sudo -u sbuild sbuild \
78          --host=$ARCH --build=$ARCH -A -d sid \
79          --apt-update --apt-distupgrade -n \
80          *.dsc
81
82     mv -f * /chroot/rebuildd/out-$PKG
83
84     return 0
85 }    
86
87 while true; do
88     if [ -f /chroot/rebuildd/EXIT ]; then
89         echo "Found EXIT file, stopping here"
90         exit 0
91     fi
92
93     # Should we update ourselves?
94     attempt_update
95
96     # Ask jack for a package to build
97     BNUM_PKG_VER_ARCH=$(ssh build@jack ./bin/get_package $HOSTNAME)
98     BNUM=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f1)
99     PKG_VER_ARCH=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f2)
100     if [ "$PKG_VER_ARCH" = "NOPACKAGE" ]; then
101         echo "Nothing to do, wait..."
102         sleep 10
103         continue
104     fi
105     
106     PKG=$(echo $PKG_VER_ARCH | cut -d _ -f1)
107     VER=$(echo $PKG_VER_ARCH | cut -d _ -f2)
108     ARCH=$(echo $PKG_VER_ARCH | cut -d _ -f3)    
109
110     DATE=$(date)
111     echo "$DATE: starting build $BNUM: ${PKG} version ${VER} for ${ARCH}"
112     rm -rf build-$PKG out-$PKG
113     mkdir build-$PKG out-$PKG
114     chown -R sbuild: build-$PKG out-$PKG
115     cd build-$PKG
116     (grab_and_build $BNUM $PKG $VER $ARCH >${LOGDIR}/${PKG_VER_ARCH}.log 2>&1)
117
118     RESULT=$(tail -10 ${LOGDIR}/${PKG_VER_ARCH}.log | awk '/^Status:/ {print $2}')
119     DATE=$(date)
120     echo "   $DATE: returned $RESULT"
121     cd ..
122     rm -rf build-$PKG
123     case $RESULT in
124         successful)
125             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/PASS/${PKG_VER_ARCH}.log
126             ;;
127         *)
128             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/FAIL/${PKG_VER_ARCH}.log
129             ;;
130     esac
131     rm ${LOGDIR}/${PKG_VER_ARCH}.log
132     rsync -a /chroot/rebuildd/out-$PKG/ build@jack:out/
133     rm -rf /chroot/rebuildd/out-$PKG
134     ssh build@jack ./bin/report_build_result $HOSTNAME $BNUM $PKG_VER_ARCH $RESULT
135     BUILDS_DONE=$(($BUILDS_DONE + 1))
136     if [ $MAX_BUILDS -gt 0 ] && [ $BUILDS_DONE -ge $MAX_BUILDS ]; then
137         echo "Reached build limit of $MAX_BUILDS, stopping"
138         exit 0
139     fi
140 done