Stop saving FAIL_SOURCE logs separately
[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     rsync -a --delete build@jack:chroots/ /chroot/rebuildd/chroots/
31     apt-get update
32     
33     MY_MD5=$(md5sum $0 | awk '{print $1}')
34     JACK_MD5=$(ssh build@jack md5sum $JACK_SCRIPT | awk '{print $1}')
35     if [ "$MY_MD5" = "$JACK_MD5" ]; then
36         return 0
37     fi
38
39     if [ x"$JACK_MD5" == x"" ]; then
40         return 0
41     fi    
42
43     # Different. Assumg that the one on jack is correct!
44     DATE=$(date)
45     echo "$DATE: Grabbing a new build-client script from jack"
46     echo "Old MD5 $MY_MD5, new MD5 $JACK_MD5"
47     
48     # Remove myself, so I can copy a new file in place
49     rm -f $0
50     rsync build@jack:$JACK_SCRIPT $0
51
52     # Now re-exec the new script
53     exec $0 $@
54 }
55
56 grab_and_build () {
57     BNUM=$1
58     PKG=$2
59     VER=$3
60     ARCH=$4
61     local error
62
63     DATE=$(date)
64     echo "$DATE: starting ${BNUM}: build of ${PKG} version ${VER} arch ${ARCH} on $HOSTNAME"
65     echo "Running apt-get -q --download-only --only-source source ${PKG}=${VER}"
66     apt-get -q --download-only --only-source source ${PKG}=${VER}
67     error=$?
68     ls -l
69     if [ $error -ne 0 ]; then
70         echo "Status: given-back"
71         return 1
72     fi
73
74     echo 1 > /proc/sys/abi/swp || true
75     
76 #    PBUILD_TAR=${PBUILD_CACHE}/unstable-${ARCH}.tgz
77 #    pbuilder build \
78 #            --buildresult /chroot/rebuildd/out-$PKG \
79 #            --bindmounts "/sys /proc /dev /dev/pts" \
80 #            --basetgz ${PBUILD_TAR} \
81 #            --timeout 4h \
82 #            *.dsc
83
84     sudo -u sbuild sbuild \
85          --host=$ARCH --build=$ARCH -A -d sid \
86          --apt-update --apt-distupgrade -n \
87          *.dsc
88
89     mv -f * /chroot/rebuildd/out-$PKG
90
91     return 0
92 }    
93
94 while true; do
95     if [ -f /chroot/rebuildd/EXIT ]; then
96         echo "Found EXIT file, stopping here"
97         exit 0
98     fi
99
100     # Should we update ourselves?
101     attempt_update
102
103     # Ask jack for a package to build
104     BNUM_PKG_VER_ARCH=$(ssh build@jack ./bin/get_package $HOSTNAME)
105     BNUM=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f1)
106     PKG_VER_ARCH=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f2)
107     if [ "$PKG_VER_ARCH" = "NOPACKAGE" ]; then
108         echo "Nothing to do, wait..."
109         sleep 10
110         continue
111     fi
112     
113     PKG=$(echo $PKG_VER_ARCH | cut -d _ -f1)
114     VER=$(echo $PKG_VER_ARCH | cut -d _ -f2)
115     ARCH=$(echo $PKG_VER_ARCH | cut -d _ -f3)    
116
117     DATE=$(date)
118     echo "$DATE: starting build $BNUM: ${PKG} version ${VER} for ${ARCH}"
119     rm -rf build-$PKG out-$PKG
120     mkdir build-$PKG out-$PKG
121     cd build-$PKG
122     (grab_and_build $BNUM $PKG $VER $ARCH >${LOGDIR}/${PKG_VER_ARCH}.log 2>&1)
123
124     RESULT=$(tail -10 ${LOGDIR}/${PKG_VER_ARCH}.log | awk '/^Status:/ {print $2}')
125     DATE=$(date)
126     echo "   $DATE: returned $RESULT"
127     cd ..
128     rm -rf build-$PKG
129     case $RESULT in
130         successful)
131             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/PASS/${PKG_VER_ARCH}.log
132             ;;
133         *)
134             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/FAIL/${PKG_VER_ARCH}.log
135             ;;
136     esac
137     rm ${LOGDIR}/${PKG_VER_ARCH}.log
138     rsync -a /chroot/rebuildd/out-$PKG/ build@jack:out/
139     rm -rf /chroot/rebuildd/out-$PKG
140     ssh build@jack ./bin/report_build_result $HOSTNAME $BNUM $PKG_VER_ARCH $RESULT
141     BUILDS_DONE=$(($BUILDS_DONE + 1))
142     if [ $MAX_BUILDS -gt 0 ] && [ $BUILDS_DONE -ge $MAX_BUILDS ]; then
143         echo "Reached build limit of $MAX_BUILDS, stopping"
144         exit 0
145     fi
146 done