ansible/roles/s6-rc/templates/s6-rc.j2
2020-09-14 00:38:41 +02:00

124 lines
2 KiB
Django/Jinja

#!/bin/sh
# {{ ansible_managed }}
# PROVIDE: s6-rc
# REQUIRE: NETWORKING daemon
# KEYWORD: shutdown
. /etc/rc.subr
export PATH="$PATH:/usr/local/bin:/usr/local/sbin"
name=s6-rc
rcvar=s6_rc_enable
extra_commands="reload"
start_cmd="s6_rc_start &"
stop_cmd="s6_rc_stop"
reload_cmd="s6_rc_reload"
status_cmd="s6_rc_status"
s6_timeout=300 # seconds
up_timeout=300000 # milliseconds
down_timeout=300000 # milliseconds
update_timeout=300000 # milliseconds
s6_wait()
{
local i=0
while ! s6-svscanctl -z /run/service 2>/dev/null; do
if [ $i -ge $s6_timeout ]; then
echo "Timeout waiting for s6-svscan." >&2
return 1
fi
if [ $i -eq 0 ]; then
echo -n "Waiting for s6-svscan." >&2
else
echo -n . >&2
fi
sleep 1
i=$((i + 1))
done
if [ $i -gt 0 ]; then
echo >&2
fi
return 0
}
s6_rc_init()
{
if [ ! -e /run/s6-rc ]; then
s6-rc-init /run/service
fi
}
s6_rc_up()
{
s6-rc -v 2 -u -t $up_timeout change enabled
}
s6_rc_down()
{
s6-rc -v 2 -d -a -t $down_timeout change
}
s6_rc_start()
{
if ! s6_wait; then
return 1
fi
s6_rc_init
s6_rc_up
}
s6_rc_stop()
{
s6_rc_down
}
s6_rc_reload()
{
local uuid="$(uuidgen)"
cd /etc/s6-rc
echo "Compiling new s6-rc service database."
s6-rc-compile -v 2 ".compiled.$uuid" service
if s6-rc-update -v 2 -t $update_timeout "/etc/s6-rc/.compiled.$uuid"; then
ln -shf ".compiled.$uuid" compiled
echo "Updated s6-rc service database."
fi
echo "Deleting old service databases."
find -s . -mindepth 1 -maxdepth 1 -type d -name '.compiled.*' -not -name ".compiled.$uuid" -print0 | xargs -0 rm -r
}
s6_rc_status()
{
local result=0
if s6-svscanctl -z /run/service 2>/dev/null; then
echo "The s6-svscan supervisor is responsible."
else
echo "The s6-svscan supervisor is unavailable."
result=1
fi
if [ -e /run/s6-rc ]; then
echo "The s6-rc service manager is initialized."
echo
echo "These services are currently active:"
s6-rc -a list
else
echo "The s6-rc service manager is uninitalized."
result=1
fi
return $result
}
load_rc_config $name
run_rc_command "$1"