Fix up perms for chroot.d better
[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
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 while true; do
89     if [ -f /chroot/rebuildd/EXIT ]; then
90         echo "Found EXIT file, stopping here"
91         exit 0
92     fi
93
94     # Should we update ourselves?
95     attempt_update
96
97     # Ask jack for a package to build
98     BNUM_PKG_VER_ARCH=$(ssh build@jack ./bin/get_package $HOSTNAME)
99     BNUM=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f1)
100     PKG_VER_ARCH=$(echo $BNUM_PKG_VER_ARCH | cut -d " " -f2)
101     if [ "$PKG_VER_ARCH" = "NOPACKAGE" ]; then
102         echo "Nothing to do, wait..."
103         sleep 10
104         continue
105     fi
106     
107     PKG=$(echo $PKG_VER_ARCH | cut -d _ -f1)
108     VER=$(echo $PKG_VER_ARCH | cut -d _ -f2)
109     ARCH=$(echo $PKG_VER_ARCH | cut -d _ -f3)    
110
111     DATE=$(date)
112     echo "$DATE: starting build $BNUM: ${PKG} version ${VER} for ${ARCH}"
113     rm -rf build-$PKG out-$PKG
114     mkdir build-$PKG out-$PKG
115     chown -R sbuild: build-$PKG out-$PKG
116     cd build-$PKG
117     (grab_and_build $BNUM $PKG $VER $ARCH >${LOGDIR}/${PKG_VER_ARCH}.log 2>&1)
118
119     RESULT=$(tail -10 ${LOGDIR}/${PKG_VER_ARCH}.log | awk '/^Status:/ {print $2}')
120     DATE=$(date)
121     echo "   $DATE: returned $RESULT"
122     cd ..
123     rm -rf build-$PKG
124     case $RESULT in
125         successful)
126             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/PASS/${PKG_VER_ARCH}.log
127             ;;
128         *)
129             rsync ${LOGDIR}/${PKG_VER_ARCH}.log build@jack:logs/${ARCH}/FAIL/${PKG_VER_ARCH}.log
130             ;;
131     esac
132     rm ${LOGDIR}/${PKG_VER_ARCH}.log
133     rsync -a /chroot/rebuildd/out-$PKG/ build@jack:out/
134     rm -rf /chroot/rebuildd/out-$PKG
135     ssh build@jack ./bin/report_build_result $HOSTNAME $BNUM $PKG_VER_ARCH $RESULT
136     BUILDS_DONE=$(($BUILDS_DONE + 1))
137     if [ $MAX_BUILDS -gt 0 ] && [ $BUILDS_DONE -ge $MAX_BUILDS ]; then
138         echo "Reached build limit of $MAX_BUILDS, stopping"
139         exit 0
140     fi
141 done