Changelog update.
[debian_buildbot.git] / debian / buildbot-slave.init
bloba4b98cc83269c7f7d743842f2366f291b4f9d1f0
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: buildbot-slave
4 # Required-Start: $remote_fs buildbot-master
5 # Required-Stop: $remote_fs buildbot-master
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Short-Description: Buildbot slave init script
9 # Description: This file allows running buildbot slave instances at
10 # startup
11 ### END INIT INFO
13 # Author: Andriy Senkovych <andriysenkovych@gmail.com>
15 # Do NOT "set -e"
17 # PATH should only include /usr/* if it runs after the mountnfs.sh script
18 PATH=/sbin:/usr/local/bin:/usr/bin:/bin
19 NAME=twistd
20 DAEMON=/usr/bin/twistd
21 DESC="buildbot-slave"
22 CONFIG_DIR=/etc/buildbot/slaves-enabled
23 PID_DIR=/var/run/buildbot-slave
24 LOG_DIR=/var/log/buildbot-slave
26 SCRIPTNAME=/etc/init.d/buildbot-slave
28 # Exit if the package is not installed
29 [ -x "$DAEMON" ] || exit 0
31 # Read configuration variable file if it is present
32 [ -r /etc/default/buildbot-slave ] && . /etc/default/buildbot-slave
34 # Exit if disabled
35 [ "$ENABLED" = "true" ] || exit 0
37 # Exit if no configuration files available
38 [ -d "$CONFIG_DIR" ] || exit 0
40 # Load the VERBOSE setting and other rcS variables
41 . /lib/init/vars.sh
43 # Define LSB log_* functions.
44 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
45 # and status_of_proc is working.
46 . /lib/lsb/init-functions
49 # Function that checks the configuration file
51 check_config()
53 if [ -z "$BB_NAME" ] ; then
54 log_failure_msg "BB_NAME not set for $CONFIG_FILE"
55 return 2
58 if [ -z "$BB_BASEDIR" ] ; then
59 log_failure_msg "BB_BASEDIR not set for $CONFIG_FILE"
60 return 2
63 if [ -z "$BB_USER" ] ; then
64 log_failure_msg "BB_USER not set for $CONFIG_FILE"
65 return 2
66 elif ! getent passwd $BB_USER >/dev/null ; then
67 log_failure_msg "Unknown user $BB_USER in $CONFIG_FILE"
68 return 2
71 return 0
75 # Function that actually starts a buildbot instance.
77 start_buildbot()
79 BOT_NAME=$1
80 BOT_DIR=$2
81 USER=$3
82 OPTIONS=$4
83 PREFIXCMD=$5
85 PIDSDIR="/var/run/buildbot/$USER"
86 PIDFILE="$PID_DIR/$USER/$BOT_NAME.pid"
87 LOGFILE="$LOG_DIR/$USER/$BOT_NAME.log"
88 # twistd is insensitive to logfile argument due to buildbot.tac overrides
89 #DAEMON_ARGS="--no_save --pidfile $PIDFILE --logfile $LOGFILE -y $BOT_DIR/buildbot.tac"
90 DAEMON_ARGS="--no_save --pidfile $PIDFILE -y $BOT_DIR/buildbot.tac $OPTIONS"
92 # Create user PID directory"
93 if ! [ -d "$PID_DIR/$USER" ] ; then
94 mkdir -m 755 "$PID_DIR/$USER"
95 chown $USER: "$PID_DIR/$USER"
98 # logfile argument does not affect twistd because of overrides in
99 # buildbot.tac.
100 ## Create user log directory. Give read permission to adm group
101 #if ! [ -d "$LOG_DIR/$USER" ] ; then
102 # mkdir -m 750 "$LOG_DIR/$USER"
103 # chown $USER:adm "$LOG_DIR/$USER"
106 # Return
107 # 0 if daemon has been started
108 # 1 if daemon was already running
109 # 2 if daemon could not be started
110 start-stop-daemon --start --quiet --pidfile $PIDFILE --user $USER \
111 --startas $DAEMON --test > /dev/null \
112 || return 1
113 start-stop-daemon --start --quiet --pidfile $PIDFILE --chuid $USER \
114 --chdir $BOT_DIR --startas $DAEMON -- $DAEMON_ARGS \
115 || return 2
119 # Start wrapper function that does fancy things
121 wrap_start_buildbot()
123 local CONFIG_FILE=$CONFIG_DIR/$1
124 . $CONFIG_FILE
125 check_config || return 2
127 log_daemon_msg "Starting $BB_NAME"
128 start_buildbot "$BB_NAME" "$BB_BASEDIR" "$BB_USER" \
129 "$BB_OPTIONS" "$BB_PREFIXCMD"
130 RETVAL=$?
132 case $RETVAL in
133 0) log_success_msg "done" ;;
134 1) log_warning_msg "already running" ;;
135 *) log_failure_msg ;;
136 esac
138 return $RETVAL
142 # Function that starts the daemon/service
144 do_start()
146 # Start all enabled buildslave instances.
148 ls $CONFIG_DIR| while read CONFIG_FILE ; do
149 wrap_start_buildbot $CONFIG_FILE
150 RETVAL=$?
151 if [ $RETVAL -gt 0 ] && [ "$SKIP_FAILED" != "true" ] ; then
152 return $RETVAL
154 done
158 # Function that actually stops a buildbot instance.
160 stop_buildbot()
162 BOT_NAME=$1
163 USER=$2
165 PIDSDIR="/var/run/buildbot/$USER"
166 PIDFILE="$PID_DIR/$USER/$BOT_NAME.pid"
168 # Return
169 # 0 if daemon has been stopped
170 # 1 if daemon was already stopped
171 # 2 if daemon could not be stopped
172 # other if a failure occurred
173 start-stop-daemon --stop --quiet --retry=INT/30/KILL/5 \
174 --pidfile $PIDFILE --name $NAME --user $USER
175 RETVAL="$?"
177 # Many daemons don't delete their pidfiles when they exit.
178 rm -f $PIDFILE
179 return "$RETVAL"
184 # Stop wrapper function that does fancy things
186 wrap_stop_buildbot()
188 local CONFIG_FILE=$CONFIG_DIR/$1
189 . $CONFIG_FILE
190 check_config || return 2
192 log_daemon_msg "Stopping $BB_NAME"
193 stop_buildbot "$BB_NAME" "$BB_USER"
194 RETVAL=$?
196 case $RETVAL in
197 0) log_success_msg "done" ;;
198 1) log_warning_msg "already stopped" ;;
199 *) log_failure_msg ;;
200 esac
202 return $RETVAL
206 # Function that stops the daemon/service
208 do_stop()
210 # Stop all enabled buildslave instances.
212 ls $CONFIG_DIR| while read CONFIG_FILE ; do
213 wrap_stop_buildbot $CONFIG_FILE
214 done
218 # Function that actually stops a buildbot instance.
220 reload_buildbot()
222 BOT_NAME=$1
223 USER=$2
225 PIDSDIR="/var/run/buildbot/$USER"
226 PIDFILE="$PID_DIR/$USER/$BOT_NAME.pid"
228 # If the daemon can reload its configuration without
229 # restarting (for example, when it is sent a SIGHUP),
230 # then implement that here.
232 start-stop-daemon --stop --signal 1 --quiet \
233 --pidfile $PIDFILE --name $NAME --user $USER
234 return 0
238 # Reload wrapper function that does fancy things
240 wrap_reload_buildbot()
242 local CONFIG_FILE=$CONFIG_DIR/$1
243 . $CONFIG_FILE
244 check_config || return 2
246 log_daemon_msg "$BB_NAME"
247 reload_buildbot "$BB_NAME" "$BB_USER"
248 RETVAL=$?
250 case $RETVAL in
251 0) log_success_msg "done" ;;
252 1) log_warning_msg "already stopped" ;;
253 *) log_failure_msg ;;
254 esac
256 return $RETVAL
260 # Function that sends a SIGHUP to the daemon/service
262 do_reload() {
263 # Stop all enabled buildslave instances.
265 ls $CONFIG_DIR| while read CONFIG_FILE ; do
266 wrap_reload_buildbot $CONFIG_FILE
267 done
270 do_restart()
272 # Restart all enabled buildslave instances.
274 ls $CONFIG_DIR| while read CONFIG_FILE ; do
275 wrap_stop_buildbot $CONFIG_FILE
276 RETVAL=$?
277 case "$RETVAL" in
278 0|1)
279 wrap_start_buildbot $CONFIG_FILE
280 case "$?" in
281 0) log_end_msg 0 ;;
282 1) log_end_msg 1 ;; # Old process is still running
284 log_end_msg 1 # Failed to start
285 if [ "$SKIP_FAILED" != "true" ] ; then
286 return $RETVAL
289 esac
292 # Failed to stop
293 log_end_msg 1
295 esac
297 if [ $RETVAL -gt 0 ] && [ "$SKIP_FAILED" != "true" ] ; then
298 return $RETVAL
300 done
303 case "$1" in
304 start)
305 log_daemon_msg "Starting $DESC" "$NAME"
306 echo
307 do_start
309 stop)
310 log_daemon_msg "Stopping $DESC" "$NAME"
311 echo
312 do_stop
314 status)
315 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
317 reload|force-reload)
318 log_daemon_msg "Reloading $DESC" "$NAME"
319 echo
320 do_reload
322 restart)
323 log_daemon_msg "Restarting $DESC" "$NAME"
324 echo
325 do_restart
328 #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
329 echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
330 exit 3
332 esac