Check for cockups in the schroot setup
[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 #    PBUILD_TAR=${PBUILD_CACHE}/unstable-${ARCH}.tgz
78 #    pbuilder build \
79 #            --buildresult /chroot/rebuildd/out-$PKG \
80 #            --bindmounts "/sys /proc /dev /dev/pts" \
81 #            --basetgz ${PBUILD_TAR} \
82 #            --timeout 4h \
83 #            *.dsc
84
85     sudo -u sbuild sbuild \
86          --host=$ARCH --build=$ARCH -A -d sid \
87          --apt-update --apt-distupgrade -n \
88          *.dsc
89
90     mv -f * /chroot/rebuildd/out-$PKG
91
92     return 0
93 }    
94
95 while true; do
96     if [ -f /chroot/rebuildd/EXIT ]; then
97         echo "Found EXIT file, stopping here"
98         exit 0
99     fi
100
101     # Should we update ourselves?
102     attempt_update
103
104     # Ask jack for a package to build
105     BNUM_PKG_VER_ARCH=$(ssh build@jack ./bin/get_package $HOSTNAME)
106     BNUM=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f1)
107     PKG_VER_ARCH=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f2)
108     if [ "$PKG_VER_ARCH" = "NOPACKAGE" ]; then
109         echo "Nothing to do, wait..."
110         sleep 10
111         continue
112     fi
113     
114     PKG=$(echo $PKG_VER_ARCH | cut -d _ -f1)
115     VER=$(echo $PKG_VER_ARCH | cut -d _ -f2)
116     ARCH=$(echo $PKG_VER_ARCH | cut -d _ -f3)    
117
118     DATE=$(date)
119     echo "$DATE: starting build $BNUM: ${PKG} version ${VER} for ${ARCH}"
120     rm -rf build-$PKG out-$PKG
121     mkdir build-$PKG out-$PKG
122     cd build-$PKG
123     (grab_and_build $BNUM $PKG $VER $ARCH >${LOGDIR}/${PKG_VER_ARCH}.log 2>&1)
124
125     RESULT=$(tail -10 ${LOGDIR}/${PKG_VER_ARCH}.log | awk '/^Status:/ {print $2}')
126     DATE=$(date)
127     echo "   $DATE: returned $RESULT"
128     cd ..
129     rm -rf build-$PKG
130     case $RESULT in
131         successful)
132             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/PASS/${PKG_VER_ARCH}.log
133             ;;
134         *)
135             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/FAIL/${PKG_VER_ARCH}.log
136             ;;
137     esac
138     rm ${LOGDIR}/${PKG_VER_ARCH}.log
139     rsync -a /chroot/rebuildd/out-$PKG/ build@jack:out/
140     rm -rf /chroot/rebuildd/out-$PKG
141     ssh build@jack ./bin/report_build_result $HOSTNAME $BNUM $PKG_VER_ARCH $RESULT
142     BUILDS_DONE=$(($BUILDS_DONE + 1))
143     if [ $MAX_BUILDS -gt 0 ] && [ $BUILDS_DONE -ge $MAX_BUILDS ]; then
144         echo "Reached build limit of $MAX_BUILDS, stopping"
145         exit 0
146     fi
147 done