~canonical-launchpad-branches/launchpad-buildd/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/sh
#
# Copyright 2009,2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
#
# launchpad-buildd
#               This file is used to start and stop launchpad buildds

### BEGIN INIT INFO
# Provides:          launchpad_buildd
# Required-Start:    $local_fs $network $syslog $time $remote_fs
# Required-Stop:     $local_fs $network $syslog $time $remote_fs
# Should-Start:      cloud-init
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# X-Interactive:     false
# Short-Description: Start/stop launchpad buildds
### END INIT INFO

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="launchpad build slaves"

TACFILE="/usr/lib/launchpad-buildd/buildd-slave.tac"

PIDROOT="/var/run/launchpad-buildd"
LOGROOT="/var/log/launchpad-buildd"
CONFROOT="/etc/launchpad-buildd"

# Gracefully exit if the package has been removed.
test -e $TACFILE || exit 0


d_check_enabled() {
RUN_NETWORK_REQUESTS_AS_ROOT=no   # Good idea generally

[ -f /etc/default/launchpad-buildd ] && . /etc/default/launchpad-buildd

hostname="`hostname -f`"
case "$hostname" in
*.ppa|*.buildd)
	cat <<END

launchpad-buildd: starting automatically because $hostname seems to be a buildd machine.

CAUTION: this service accepts network commands and runs them as root.
END
	return
	;;
*)
	echo "launchpad-buildd: not starting automatically on $hostname"
	;;
esac

if [ "$RUN_NETWORK_REQUESTS_AS_ROOT" != yes ]
then
	cat <<END

launchpad-buildd is disabled.
When enabled, launchpad-buildd accepts network commands and runs them as root.
If you are sure this server will only be reachable by trusted machines, edit
/etc/default/launchpad-buildd.

END
	exit 0
fi
}


#
#       Function that starts a buildd slave
#
d_start() {
    CONF=$1
    PIDFILE="$PIDROOT"/"$CONF".pid
    LOGFILE="$LOGROOT"/"$CONF".log

    # Useful for certain kinds of image builds.
    modprobe nbd || true

    su - buildd -c "BUILDD_SLAVE_CONFIG=$CONFROOT/$CONF twistd --no_save --pidfile $PIDFILE --python $TACFILE --logfile $LOGFILE --umask 022"
}

#
#       Function that stops a buildd slave
#
d_stop() {
    CONF=$1
    PIDFILE="$PIDROOT"/"$CONF".pid
    test -r $PIDFILE && kill -TERM $(cat $PIDFILE) || true
}

#
#       Function that reloads a buildd slave
#
d_reload() {
    CONF=$1
    PIDFILE="$PIDROOT"/"$CONF".pid
    test -r $PIDFILE && kill -HUP $(cat $PIDFILE) || true
}

CONFS=$(cd $CONFROOT; ls|grep -v "^-"|grep -v "~$")

case "$1" in
  start)
	d_check_enabled

        echo -n "Starting $DESC:"
        install -m 755 -o buildd -g buildd -d $PIDROOT

	# Create any missing directories and chown them appropriately
	install -d -o buildd -g buildd /home/buildd/filecache-default

	for conf in $CONFS; do
	    echo -n " $conf"
	    d_start $conf
	done
        echo "."
        ;;
  stop)
        echo -n "Stopping $DESC:"
	for conf in $CONFS; do
	    echo -n " $conf"
	    d_stop $conf
	done
        echo "."
        ;;
  restart|force-reload)
        #
        #       If the "reload" option is implemented, move the "force-reload"
        #       option to the "reload" entry above. If not, "force-reload" is
        #       just the same as "restart".
        #
	$0 stop
	sleep 1
	$0 start
        ;;
  reload)
        for conf in $CONFS; do
            d_reload $conf
        done
        ;;
  *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload|reload}" >&2
        exit 1
        ;;
esac

exit 0