Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Packaging: Cherry-pick changes to runners.sh from st2-packages #6302

Merged
merged 11 commits into from
Feb 22, 2025
Prev Previous commit
Next Next commit
beautify runners.sh
  • Loading branch information
dennybaa authored and cognifloyd committed Feb 17, 2025
commit 61b8506b4ac9fa1609c7c0fcb78754825be3fbec
95 changes: 50 additions & 45 deletions st2actions/bin/runners.sh
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
#!/bin/sh

INITCOMM=$(cat /proc/1/comm)
SYSTEMDCTL=/bin/systemctl
UPSTARTCTL=/sbin/initctl
SPAWNSVC=st2actionrunner
WORKERSVC=st2actionrunner-worker
# Set default number of workers
# Default number of workers
WORKERS="${WORKERS:-10}"

## Use running init system detection criterias
#
if [ -d /run/systemd/system ]; then
# systemd is running
sv=systemd
svbin=$SYSTEMDCTL
elif [ "$INITCOMM" = init ] && ($UPSTARTCTL version 2>&1) &&
[ -f /etc/init/$WORKERSVC.conf ]; then
# init is running and upstart has been detected
sv=upstart
svbin=$UPSTARTCTL
else
# In all other cases which may apply to older debians, redhats and
# centos, amazon etc.
sv=sysv
svbin=/etc/init.d/$WORKERSVC
if [ ! -x $svbin ]; then
>&2 echo "Init file not found: $svbin"
>&2 echo "Unknown platform, we support ONLY debian, systemd and sysv!"
exit 99
# Choose init system to perform actions with a service.
choose_sysinit() {
local service="$1" svinit="sysv"
if [ -d /run/systemd/system ]; then
svinit=systemd
elif [ "$(cat /proc/1/comm)" = init ] && [ -f /etc/init/${service}.conf ] &&
(/sbin/initctl version 2>&1 | grep -q upstart); then
svinit=upstart
else
if [ ! -x /etc/init.d/${service} ]; then
>&2 echo "Supported init systems: systemd, upstart and sysv"
>&2 echo "/etc/init.d/${service} not found or disabled"
exit 99
fi
fi
fi
echo $svinit
}

## Spwan workers
#
action="$1"; shift;
rs=0
i=1
while [ $i -le $WORKERS ]; do
if [ $sv = systemd ]; then
$svbin $action $SPAWNSVC@$i
elif [ $sv = upstart ]; then
$svbin $action $WORKERSVC WORKERID=$i
elif [ $sv = sysv ]; then
WORKERID=$i $svbin $action
fi
cmdrs=$?
[ $cmdrs -gt 0 ] && rs=$cmdrs
i=`expr $i + 1`
done
# Perform service action over the given number of workers.
spawn_workers() {
local action=$1
local init= seq=$(eval printf '%g\\n' {1..$WORKERS})

# Choose init system and exit if it's not supported.
init=$(choose_sysinit st2actionrunner)
[ $? -gt 0 ] && exit $?

case $init in
systemd)
echo "$seq" | xargs -I{} /bin/systemctl $action \
st2actionrunner@{}
;;
upstart)
echo "$seq" | xargs -I{} /sbin/initctl $action \
st2actionrunner-worker WORKERID={}
;;
sysv)
echo "$seq" | xargs -I{} /bin/sh -c \
"WORKERID={} /etc/init.d/st2actionrunner-worker $action"
;;
esac
# return 1 in case if xargs failed any invoked commands.
[ $? -ge 123 ] && return 1 || return $?
}

# Perform service action on all actionrunners
if [ -z "$1" ]; then
echo >&2 "Usage: $0 action"
exit 99
fi

exit $rs
spawn_workers $1