Add cleanup of autogenerated filesystems after build as option to obsworker
[opensuse:build-service.git] / dist / obsworker
1 #! /bin/sh
2 # Copyright (c) 2007, Novell Inc.
3 #
4 # Author: adrian@suse.de
5 #
6 # /etc/init.d/obsworker
7 #   and its symbolic  link
8 # /usr/sbin/rcobsworker
9 #
10 ### BEGIN INIT INFO
11 # Provides:          obsworker
12 # Required-Start:    $time $network $syslog
13 # Required-Stop:     $time $network $syslog
14 # Should-Start:      $remote_fs obssrcserver obsrepserver xend
15 # Should-Stop:       $none
16 # Default-Start:     3 5
17 # Default-Stop:      0 1 2 4 6
18 # Description:       openSUSE Build Service worker
19 ### END INIT INFO
20
21 . /etc/rc.status
22
23 . /etc/sysconfig/obs-worker
24
25 # Determine the base and follow a runlevel link name.
26 base=${0##*/}
27 link=${base#*[SK][0-9][0-9]}
28
29 # Preconfigured by obsstoragesetup runlevel setup script
30 if [ -e /OBS.worker/config ]; then
31     . /OBS.worker/config
32 fi
33
34 if [ -f /etc/buildhost.config ];then
35     . /etc/buildhost.config
36 fi
37
38 if [ -z "$OBS_RUN_DIR" ]; then
39     OBS_RUN_DIR="/var/run/obs"
40 fi
41 if [ -z "$OBS_LOG_DIR" ]; then
42     OBS_LOG_DIR="/var/log/obs"
43 fi
44 if [ -z "$OBS_REPO_SERVERS" ]; then
45     OBS_REPO_SERVERS="localhost:5252"
46 fi
47
48 if [ -n "$OBS_WORKER_TEST_MODE" ]; then
49     OBS_TEST="--test"
50 fi
51 if [ -n "$OBS_WORKER_JOBS" ]; then
52     OBS_JOBS="--jobs $OBS_WORKER_JOBS"
53 fi
54 if [ -n "$OBS_WORKER_NICE_LEVEL" ]; then
55     OBS_NICE=$OBS_WORKER_NICE_LEVEL
56 else
57     OBS_NICE=18
58 fi
59
60 if [ -n "$OBS_REPO_SERVERS" ]; then
61     for i in $OBS_REPO_SERVERS; do
62         OBS_REPO_PARM="$OBS_REPO_PARM --reposerver http://$i"
63         WORKER_CODE="http://$i"
64         done
65 else
66     OBS_REPO_PARM="$OBS_REPO_PARM --reposerver http://localhost:5252"
67     WORKER_CODE="http://localhost:5252"
68 fi
69
70 obsrundir="$OBS_RUN_DIR"
71 workerdir="$obsrundir"/worker
72 workerbootdir="$workerdir"/boot
73 screenrc="$workerdir"/boot/screenrc
74 OBS_WORKER_OPT=""
75
76 if [ -n "$OBS_CACHE_DIR" ]; then
77     OBS_WORKER_OPT="--cachedir $OBS_CACHE_DIR"
78 else
79     # For automatic appliance setup
80     if [ -e /OBS.worker/config -a -d /OBS.worker/cache ]; then
81         OBS_CACHE_DIR="/OBS.worker/cache"
82         OBS_WORKER_OPT="--cachedir /OBS.worker/cache"
83         size=`df -m /OBS.worker/cache | tail -n 1 | sed -n 's,[^ ]*[ ]*\([^ ]*\).*,\1,p'`
84         size=$(( $size / 2 ))
85         OBS_WORKER_OPT="$OBS_WORKER_OPT --cachesize $size"
86     fi
87 fi
88
89 if [ -n "$OBS_CACHE_SIZE" ]; then
90     if [ -z "$OBS_CACHE_DIR" ]; then
91         OBS_CACHE_DIR="/tmp/workercache"
92         OBS_WORKER_OPT="--cachedir $OBS_CACHE_DIR"
93     fi
94     mkdir $OBS_CACHE_DIR
95     OBS_WORKER_OPT="$OBS_WORKER_OPT --cachesize $OBS_CACHE_SIZE"
96 fi
97
98 if [ -n "$OBS_VM_KERNEL" -a "$OBS_VM_KERNEL" != "none" ] ; then
99     if [ -n "$OBS_VM_INITRD" -a "$OBS_VM_INITRD" != "none" ] ; then
100         OBS_WORKER_OPT="$OBS_WORKER_OPT --vm-kernel $OBS_VM_KERNEL  --vm-initrd $OBS_VM_INITRD"
101     fi
102 fi
103
104
105 [ -z "$OBS_INSTANCE_MEMORY" ] && OBS_INSTANCE_MEMORY=256
106
107 vmopt=
108
109 if [ -n "$OBS_VM_TYPE" -a "$OBS_VM_TYPE" != "auto" ] ; then
110     if [ "$OBS_VM_TYPE" != "none" ] ; then
111         vmopt="--$OBS_VM_TYPE"
112     fi
113 elif [ -e /dev/kvm ] ; then
114     vmopt=--kvm
115 elif [ -e /sys/hypervisor/type ] && grep -q xen /sys/hypervisor/type; then
116     vmopt=--xen
117 fi
118
119 if [ -n "$vmopt" ]; then
120     # we start up in VM mode, check for the worker disk options
121     if [ -n "$OBS_VM_DISK_AUTOSETUP_ROOT_FILESIZE" -a -n "$OBS_VM_DISK_AUTOSETUP_SWAP_FILESIZE" ]; then
122         VMDISK_ROOT_FILESIZE="--vmdisk-rootsize ${OBS_VM_DISK_AUTOSETUP_ROOT_FILESIZE}"
123         VMDISK_SWAP_FILESIZE="--vmdisk-swapsize ${OBS_VM_DISK_AUTOSETUP_SWAP_FILESIZE}"
124         if [ -n "$OBS_VM_DISK_AUTOSETUP_FILESYSTEM" ]; then
125             VMDISK_FILESYSTEM="--vmdisk-filesystem ${OBS_VM_DISK_AUTOSETUP_FILESYSTEM}"
126         fi
127     else
128         echo "Warning: VM build enabled but either"
129         echo "         OBS_VM_DISK_AUTOSETUP_ROOT_FILESIZE or "
130         echo "         OBS_VM_DISK_AUTOSETUP_SWAP_FILESIZE are not set."
131         echo ""
132         echo "Worker startup failed"
133         exit 1
134     fi
135 fi
136
137 if [ "--kvm" == "$vmopt" -a -n "$OBS_VM_DISK_CLEAN_POST" ]; then
138     VMDISK_CLEAN_POST="--vmdisk-clean-post"
139 fi
140
141 rc_reset
142 case "$1" in
143     start)
144         # reset screenrc
145         mkdir -p "$obsrundir"
146         chown obsrun:obsrun "$obsrundir"
147         rm -rf "$workerdir"
148         mkdir -p "$workerbootdir"
149 #        echo "zombie on"       > $screenrc
150         echo "caption always" >> $screenrc
151
152         if [ "$OBS_WORKER_INSTANCES" -gt 0 ]; then
153             NUM="$OBS_WORKER_INSTANCES"
154         else
155             # start one build backend per CPU
156             NUM=`grep -i ^processor /proc/cpuinfo  | wc -l`
157         fi
158         I=0
159         ROOT="/tmp"
160         [ -d /BUILD ] && ROOT="/BUILD"
161         [ -d /abuild ] && ROOT="/abuild"
162         [ -n "$OBS_WORKER_DIRECTORY" ] && ROOT="$OBS_WORKER_DIRECTORY"
163         echo "Run $NUM obsworker using $ROOT"
164
165         # find SLP announced OBS servers
166         if [ "$OBS_USE_SLP" == "yes" ]; then
167             for i in `slptool findsrvs service:obs.repo_server | sed -n 's/service:obs.repo_server:\([^,]*\),.*/\1/p'`; do
168                 OBS_REPO_PARM="$OBS_REPO_PARM --reposerver $i"
169                 # any of them should be okay
170                 WORKER_CODE="$i"
171             done
172         fi
173
174         # fetch worker sources from server
175         [ -d "$workerbootdir" ] || mkdir -p "$workerbootdir"
176         pushd "$workerbootdir" > /dev/null
177         curl -s "$WORKER_CODE"/getworkercode | cpio --quiet --extract 
178         ln -s . XML 
179         chmod 755 bs_worker
180         popd > /dev/null
181         OBS_WORKER_OPT1="$OBS_WORKER_OPT"
182         while test "$NUM" -gt "$I"; do
183             if [ "$OBS_WORKER_PORTBASE" -gt 0 ]; then
184                 port="--port $((OBS_WORKER_PORTBASE + I))"
185             else
186                 port=""
187             fi
188             I=$(( $I + 1 ))
189             title="$HOSTNAME/$I"
190             R=$ROOT/root_$I
191             # prepare obsworker startup in screen...
192             if [ -n "$vmopt" ]; then
193                 [ -d $R ] || mkdir -p $R
194                 DEVICE="$ROOT/root_$I/root"
195                 SWAP="$ROOT/root_$I/swap"
196                 if [ -n "$OBS_VM_DISK_AUTOSETUP_ROOT_FILESIZE" -a -n "$OBS_VM_DISK_AUTOSETUP_SWAP_FILESIZE" ]; then
197                     OBS_WORKER_OPT="$OBS_WORKER_OPT1 $VMDISK_AUTOSETUP $VMDISK_ROOT_FILESIZE $VMDISK_SWAP_FILESIZE $VMDISK_FILESYSTEM $VMDISK_CLEAN_POST"
198                 elif [ ! -e "$DEVICE" -o ! -e "$SWAP" ]; then
199                         echo "ERROR: worker is configured to use a VM, but one or all of the following devices/files do not exist: $DEVICE, $SWAP"
200                         exit 1
201                 fi
202                 DEVICE="--device $DEVICE"
203                 SWAP="--swap $SWAP"
204                 echo $OBS_INSTANCE_MEMORY > "$ROOT/root_$I/memory"
205             else
206                 [ -d $R ] || mkdir -p $R
207             fi
208                 echo "screen -t $title nice -n $OBS_NICE ./bs_worker $vmopt $port --root $R" \
209                 "--statedir $workerdir/$I --id $HOSTNAME/$I $OBS_REPO_PARM" \
210                 "$OBS_JOBS $OBS_TEST $OBS_WORKER_OPT $DEVICE $SWAP" \
211                 >> $screenrc
212                 [ -d $workerdir/$I ] || mkdir -p $workerdir/$I
213         done
214         pushd "$workerbootdir" > /dev/null
215         screen -m -d -c $screenrc
216         popd > /dev/null
217     ;;
218     stop)
219         echo -n "Shutting down obsworker"
220         killall bs_worker 
221         rc_status -v
222     ;;
223     restart)
224         ## If first returns OK call the second, if first or
225         ## second command fails, set echo return value.
226         $0 stop
227         $0 start
228         rc_status
229     ;;
230     try-restart)
231         $0 status
232         if test $? = 0; then
233             $0 restart
234         else
235             rc_reset        # Not running is not a failure.
236         fi
237         # Remember status and be quiet
238         rc_status
239     ;;
240     reload)
241     ;;
242     status)
243         echo -n "Checking for obsworker: "
244         checkproc bs_worker
245         rc_status -v
246     ;;
247     *)
248         echo "Usage: $0 {start|stop|status|try-restart|restart|reload}"
249         exit 1
250     ;;
251 esac
252 rc_exit