|
1 | 1 | #!/bin/sh |
2 | 2 |
|
3 | | -LSB_RELEASE=$(which lsb_release) |
4 | | -SYSTEMDCTL=/bin/systemctl |
5 | | -UPSTARTCTL=/sbin/initctl |
6 | | -SPAWNSVC=st2actionrunner |
7 | | -WORKERSVC=st2actionrunner-worker |
| 3 | +# Default number of workers |
| 4 | +WORKERS="${WORKERS:-10}" |
8 | 5 |
|
9 | | -# Set default number of workers |
10 | | -if [ -z "$WORKERS" ]; then |
11 | | - WORKERS=$(/usr/bin/nproc 2>/dev/null) |
12 | | - WORKERS="${WORKERS:-4}" |
13 | | -fi |
| 6 | +# Choose init system to perform actions with a service. |
| 7 | +choose_sysinit() { |
| 8 | + local service="$1" svinit="unknown" |
| 9 | + if [ -d /run/systemd/system ]; then |
| 10 | + svinit=systemd |
| 11 | + else |
| 12 | + >&2 echo "Supported init systems: ONLY systemd" |
| 13 | + exit 99 |
| 14 | + fi |
| 15 | + echo $svinit |
| 16 | +} |
| 17 | + |
| 18 | +# Perform service action over the given number of workers. |
| 19 | +spawn_workers() { |
| 20 | + local action=$1 init= seq= |
| 21 | + seq=$(bash -c "printf '%g\\n' {1..$WORKERS}") |
| 22 | + |
| 23 | + # Choose init system and exit if it's not supported. |
| 24 | + init=$(choose_sysinit st2actionrunner) |
| 25 | + [ $? -gt 0 ] && exit $? |
14 | 26 |
|
15 | | -# 1. Choose init type |
16 | | -if [ -z "$sv" -a -x $SYSTEMDCTL ]; then |
17 | | - sv=systemd |
18 | | - svbin=$SYSTEMDCTL |
19 | | -elif [ -z "$sv" ] && ( /sbin/start 2>&1 | grep -q "missing job name" ); then |
20 | | - sv=upstart |
21 | | - svbin=$UPSTARTCTL |
22 | | -else |
23 | | - >&2 echo "Unknown platform, we support ONLY upstart and systemd!" |
| 27 | + case $init in |
| 28 | + systemd) |
| 29 | + echo "$seq" | xargs -I{} /bin/systemctl $action \ |
| 30 | + st2actionrunner@{} |
| 31 | + ;; |
| 32 | + esac |
| 33 | + # return 1 in case if xargs failed any invoked commands. |
| 34 | + retval=$?; [ $retval -ge 123 ] && return 1 || return $retval |
| 35 | +} |
| 36 | + |
| 37 | +# Perform service action on all actionrunners |
| 38 | +if [ -z "$1" ]; then |
| 39 | + echo >&2 "Usage: $0 action" |
24 | 40 | exit 99 |
25 | 41 | fi |
26 | 42 |
|
27 | | -# 2. Spwan workers |
28 | | -action="$1"; shift; |
29 | | -rs=0 |
30 | | -i=1 |
31 | | -while [ $i -le $WORKERS ]; do |
32 | | - if [ $sv = systemd ]; then |
33 | | - $svbin $action $SPAWNSVC@$i |
34 | | - elif [ $sv = upstart ]; then |
35 | | - $svbin $action $WORKERSVC WORKERID=$i |
36 | | - fi |
37 | | - cmdrs=$? |
38 | | - [ $cmdrs -gt 0 ] && rs=$cmdrs |
39 | | - i=`expr $i + 1` |
40 | | -done |
41 | | - |
42 | | -exit $rs |
| 43 | +spawn_workers $1 |
0 commit comments