Don't reset the counter every loop
[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     install -m 755 -o root -g root /etc/schroot/local-chroot.d/* /etc/schroot/chroot.d/
31     rsync -a --delete build@jack:chroots/ /chroot/rebuildd/chroots/
32     apt-get update > /dev/null 2>&1
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     echo 1 > /proc/sys/abi/cp15_barrier || true
77     
78     sudo -u sbuild sbuild \
79          --host=$ARCH --build=$ARCH -A -d sid \
80          --apt-update --apt-distupgrade -n \
81          *.dsc
82
83     mv -f * /chroot/rebuildd/out-$PKG
84
85     return 0
86 }    
87
88 NO_JOBS_SLOW_SLEEP=20
89 SLEEP_NO_JOB_DEFAULT=10
90 SLEEP_NO_JOB_SLOW=600
91 SLEEP_TIME=$SLEEP_NO_JOB_DEFAULT
92
93 START_TIME=$(date +'%s')
94
95 # How many times in a row have we asked for a job and not got
96 # anything? If this gets high, back off
97 NO_JOBS_COUNT=0
98
99 while true; do
100     if [ -f /chroot/rebuildd/EXIT ]; then
101         echo "Found EXIT file, stopping here"
102         exit 0
103     fi
104
105     # Should we update ourselves?
106     attempt_update
107
108     SCRIPT_TIME=$(stat -c %Y $0)
109     if [ $SCRIPT_TIME -gt $START_TIME ]; then
110         echo "$@ updated under me, need to restart"
111         exec $ME $@
112     fi
113
114     # Ask jack for a package to build
115     BNUM_PKG_VER_ARCH=$(ssh build@jack ./bin/get_package $HOSTNAME)
116     BNUM=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f1)
117     PKG_VER_ARCH=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f2)
118
119     # If we didn't get a job, sleep for a while
120     if [ "$PKG_VER_ARCH" = "NOPACKAGE" ]; then
121         DATE=$(date)
122         NO_JOBS_COUNT=$(($NO_JOBS_COUNT + 1))
123         echo "$DATE: Nothing to do ($NO_JOBS_COUNT times), wait..."
124         if [ $NO_JOBS_COUNT -eq $NO_JOBS_SLOW_SLEEP ]; then
125             echo "$DATE: No work for $NO_JOBS_SLOW_SLEEP, slowing down"
126             SLEEP_TIME=$SLEEP_NO_JOB_SLOW # Back off
127         fi
128         sleep $SLEEP_TIME
129         continue
130     else
131         NO_JOBS_COUNT=0
132         SLEEP_TIME=$SLEEP_NO_JOB_DEFAULT
133     fi
134     
135     PKG=$(echo $PKG_VER_ARCH | cut -d _ -f1)
136     VER=$(echo $PKG_VER_ARCH | cut -d _ -f2)
137     ARCH=$(echo $PKG_VER_ARCH | cut -d _ -f3)    
138
139     DATE=$(date)
140     echo "$DATE: starting build $BNUM: ${PKG} version ${VER} for ${ARCH}"
141     rm -rf build-$PKG out-$PKG
142     mkdir build-$PKG out-$PKG
143     chown -R sbuild: build-$PKG out-$PKG
144     cd build-$PKG
145     (grab_and_build $BNUM $PKG $VER $ARCH >${LOGDIR}/${PKG_VER_ARCH}.log 2>&1)
146
147     RESULT=$(tail -10 ${LOGDIR}/${PKG_VER_ARCH}.log | awk '/^Status:/ {print $2}')
148     DATE=$(date)
149     echo "   $DATE: returned $RESULT"
150     cd ..
151     rm -rf build-$PKG
152     case $RESULT in
153         successful)
154             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/PASS/${PKG_VER_ARCH}.log
155             ;;
156         *)
157             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/FAIL/${PKG_VER_ARCH}.log
158             ;;
159     esac
160     rm ${LOGDIR}/${PKG_VER_ARCH}.log
161     rsync -a /chroot/rebuildd/out-$PKG/ build@jack:out/
162     rm -rf /chroot/rebuildd/out-$PKG
163     ssh build@jack ./bin/report_build_result $HOSTNAME $BNUM $PKG_VER_ARCH $RESULT
164     BUILDS_DONE=$(($BUILDS_DONE + 1))
165     if [ $MAX_BUILDS -gt 0 ] && [ $BUILDS_DONE -ge $MAX_BUILDS ]; then
166         echo "Reached build limit of $MAX_BUILDS, stopping"
167         exit 0
168     fi
169 done