~upstart-devel/upstart/upstart-jobs

« back to all changes in this revision

Viewing changes to vivid/etc/init.d/buildslave

  • Committer: Dimitri John Ledkov
  • Date: 2014-11-19 12:58:41 UTC
  • Revision ID: dimitri.j.ledkov@intel.com-20141119125841-98dr37roy8dvcv3b
auto update

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
 
 
3
### Maintain compatibility with chkconfig
 
4
# chkconfig: 2345 83 17
 
5
# description: buildslave
 
6
 
 
7
### BEGIN INIT INFO
 
8
# Provides:          buildslave
 
9
# Required-Start:    $remote_fs
 
10
# Required-Stop:     $remote_fs
 
11
# Default-Start:     2 3 4 5
 
12
# Default-Stop:      0 1 6
 
13
# Short-Description: Buildbot slave init script
 
14
# Description:       This file allows running buildbot slave instances at
 
15
#                    startup
 
16
### END INIT INFO
 
17
 
 
18
PATH=/sbin:/bin:/usr/sbin:/usr/bin
 
19
SLAVE_RUNNER=/usr/bin/buildslave
 
20
 
 
21
 
 
22
# Source buildslave configuration
 
23
[[ -r /etc/default/buildslave ]] && . /etc/default/buildslave
 
24
 
 
25
 
 
26
# Get some LSB-like functions
 
27
if [ -r /lib/lsb/init-functions ]; then
 
28
    . /lib/lsb/init-functions
 
29
else
 
30
    function log_success_msg() {
 
31
        echo "$@"
 
32
    }
 
33
    function log_failure_msg() {
 
34
        echo "$@"
 
35
    }
 
36
    function log_warning_msg() {
 
37
        echo "$@"
 
38
    }
 
39
fi
 
40
 
 
41
 
 
42
# Some systems don't have seq (e.g. Solaris)
 
43
if type seq >/dev/null 2>&1; then
 
44
    :
 
45
else
 
46
    function seq() {
 
47
        for ((i=1; i<=$1; i+=1)); do
 
48
            echo $i
 
49
        done
 
50
    }
 
51
fi
 
52
 
 
53
 
 
54
if [[ ! -x ${SLAVE_RUNNER} ]]; then
 
55
    log_failure_msg "does not exist or not an executable file: ${SLAVE_RUNNER}"
 
56
    exit 1
 
57
fi
 
58
 
 
59
function is_enabled() {
 
60
    ANSWER=`echo $1|tr "[:upper:]" "[:lower:]"`
 
61
    [[ "$ANSWER" == "yes" ]] || [[ "$ANSWER" == "true" ]] || [[ "$ANSWER" ==  "1" ]]
 
62
    return $?
 
63
}
 
64
 
 
65
function is_disabled() {
 
66
    ANSWER=`echo $1|tr "[:upper:]" "[:lower:]"`
 
67
    [[ "$ANSWER" == "no" ]] || [[ "$ANSWER" == "false" ]] || [[ "$ANSWER" ==  "0" ]]
 
68
    return $?
 
69
}
 
70
 
 
71
 
 
72
function slave_config_valid() {
 
73
    # Function validates buildslave instance startup variables based on array
 
74
    # index
 
75
    local errors=0
 
76
    local index=$1
 
77
 
 
78
    if ! is_enabled "${SLAVE_ENABLED[$index]}" && ! is_disabled "${SLAVE_ENABLED[$index]}" ; then
 
79
        log_warning_msg "buildslave #${index}: invalid enabled status"
 
80
        errors=$(($errors+1))
 
81
    fi
 
82
 
 
83
    if [[ -z ${SLAVE_NAME[$index]} ]]; then
 
84
        log_failure_msg "buildslave #${index}: no name"
 
85
        errors=$(($errors+1))
 
86
    fi
 
87
 
 
88
    if [[ -z ${SLAVE_USER[$index]} ]]; then
 
89
        log_failure_msg "buildslave #${index}: no run user specified"
 
90
        errors=$( ($errors+1) )
 
91
    elif ! getent passwd ${SLAVE_USER[$index]} >/dev/null; then
 
92
        log_failure_msg "buildslave #${index}: unknown user ${SLAVE_USER[$index]}"
 
93
        errors=$(($errors+1))
 
94
    fi
 
95
 
 
96
    if [[ ! -d "${SLAVE_BASEDIR[$index]}" ]]; then
 
97
        log_failure_msg "buildslave ${index}: basedir does not exist ${SLAVE_BASEDIR[$index]}"
 
98
        errors=$(($errors+1))
 
99
    fi
 
100
 
 
101
    return $errors
 
102
}
 
103
 
 
104
function check_config() {
 
105
    itemcount="${#SLAVE_ENABLED[@]}
 
106
               ${#SLAVE_NAME[@]}
 
107
               ${#SLAVE_USER[@]}
 
108
               ${#SLAVE_BASEDIR[@]}
 
109
               ${#SLAVE_OPTIONS[@]}
 
110
               ${#SLAVE_PREFIXCMD[@]}"
 
111
 
 
112
    if [[ $(echo "$itemcount" | tr -d ' ' | sort -u | wc -l) -ne 1 ]]; then
 
113
        log_failure_msg "SLAVE_* arrays must have an equal number of elements!"
 
114
        return 1
 
115
    fi
 
116
 
 
117
    errors=0
 
118
    for i in $( seq ${#SLAVE_ENABLED[@]} ); do
 
119
        if is_disabled "${SLAVE_ENABLED[$i]}" ; then
 
120
            log_warning_msg "buildslave #${i}: disabled"
 
121
            continue
 
122
        fi
 
123
        slave_config_valid $i
 
124
        errors=$(($errors+$?))
 
125
    done
 
126
 
 
127
    [[ $errors == 0 ]]; return $?
 
128
}
 
129
 
 
130
check_config || exit $?
 
131
 
 
132
function iscallable () { type $1 2>/dev/null | grep -q 'shell function'; }
 
133
 
 
134
function slave_op () {
 
135
    op=$1 ; mi=$2
 
136
 
 
137
    if [ `uname` = SunOS ]; then
 
138
        suopt=""
 
139
    else
 
140
        suopt="-s /bin/sh"
 
141
    fi
 
142
    ${SLAVE_PREFIXCMD[$mi]} \
 
143
    su $suopt - ${SLAVE_USER[$mi]} \
 
144
    -c "$SLAVE_RUNNER $op ${SLAVE_OPTIONS[$mi]} ${SLAVE_BASEDIR[$mi]} > /dev/null"
 
145
    return $?
 
146
}
 
147
 
 
148
function do_op () {
 
149
    errors=0
 
150
    for i in $( seq ${#SLAVE_ENABLED[@]} ); do
 
151
        if [ -n "$4" ] && [ "$4" != "${SLAVE_NAME[$i]}" ] ; then
 
152
            continue
 
153
        elif is_disabled "${SLAVE_ENABLED[$i]}" && [ -z "$4" ] ; then
 
154
            continue
 
155
        fi
 
156
 
 
157
        # Some rhels don't come with all the lsb goodies
 
158
        if iscallable log_daemon_msg; then
 
159
                log_daemon_msg "$3 \"${SLAVE_NAME[$i]}\""
 
160
            if eval $1 $2 $i; then
 
161
                log_end_msg 0
 
162
            else
 
163
                log_end_msg 1
 
164
                errors=$(($errors+1))
 
165
            fi
 
166
        else
 
167
            if eval $1 $2 $i; then
 
168
                log_success_msg "$3 \"${SLAVE_NAME[$i]}\""
 
169
            else
 
170
                log_failure_msg "$3 \"${SLAVE_NAME[$i]}\""
 
171
                errors=$(($errors+1))
 
172
            fi
 
173
        fi
 
174
    done
 
175
    return $errors
 
176
}
 
177
 
 
178
case "$1" in
 
179
    start)
 
180
        do_op "slave_op" "start" "Starting buildslave" "$2"
 
181
        exit $?
 
182
        ;;
 
183
    stop)
 
184
        do_op "slave_op" "stop" "Stopping buildslave" "$2"
 
185
        exit $?
 
186
        ;;
 
187
    reload)
 
188
        do_op "slave_op" "reload" "Reloading buildslave" "$2"
 
189
        exit $?
 
190
        ;;
 
191
    restart|force-reload)
 
192
        do_op "slave_op" "restart" "Restarting buildslave" "$2"
 
193
        exit $?
 
194
        ;;
 
195
    *)
 
196
        echo "Usage: $0 {start|stop|restart|reload|force-reload}"
 
197
        exit 1
 
198
        ;;
 
199
esac
 
200
 
 
201
exit 0