ansible/roles/s6-rc/templates/s6-rc.j2
Crest 803ebdbded Improve s6-rc rc.d script
* Deduplicate path literals
	* Pass live directory to s6-rc invocations

Changes #31
2022-07-04 02:42:22 +02:00

142 lines
2.9 KiB
Django/Jinja

#!/bin/sh
# {{ ansible_managed }}
# PROVIDE: s6-rc
# REQUIRE: NETWORKING daemon
# KEYWORD: shutdown
etc_dir="{{ s6_etc_dir }}"
scan_dir="{{ s6_scan_dir }}"
live_dir="{{ s6_live_dir }}"
EX_UNAVAILABLE=69
EX_CONFIG=78
. /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 "$scan_dir" 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 "$live_dir" ]; then
s6-rc-init -l "$live_dir" "$scan_dir"
fi
}
s6_rc_up()
{
s6-rc -l "$live_dir" -v 2 -u -t "$up_timeout" change enabled
}
s6_rc_down()
{
s6-rc -l "$live_dir" -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_dir"
echo "Compiling the s6-rc service definitions into a services database: $etc_dir/service -> $etc_dir/.compiled.$uuid."
if ! s6-rc-compile -v 2 ".compiled.$uuid" service; then
echo "Failed to compile the service definitions into a services database." >&2
return $EX_CONFIG
fi
echo "Updating the running s6-rc service manager to the latest compiled services database: $etc_dir/.compiled.$uuid."
if s6-rc-update -l "$live_dir" -v 2 -t $update_timeout "$etc_dir/.compiled.$uuid"; then
echo "Marking the running services database as selected default configuration: .compiled.$uuid -> compiled."
ln -shf ".compiled.$uuid" compiled
echo "Deleting stale services databases."
if ! find -s . -mindepth 1 -maxdepth 1 -type d -name '.compiled.*' -not -name ".compiled.$uuid" -print0 | xargs -0 rm -r; then
echo "Failed to delete stale services databases." >&2
return $EX_CONFIG
fi
else
echo "Failed to update the running s6-rc manager to the latest service database." >&2
return $EX_CONFIG
fi
}
s6_rc_status()
{
local result=0
# Check if s6-svscan is responsive by asking it to invoke its reaper (almost a NOP)
if s6-svscanctl -z "$scan_dir" 2>/dev/null; then
echo "The s6-svscan supervisor is responsive."
else
echo "The s6-svscan supervisor is unavailable."
result=1
fi
if [ -e "$live_dir" ]; then
echo "The s6-rc service manager has been initialized."
echo
echo "These services are currently active:"
s6-rc -l "$live_dir" -a list
else
echo "The s6-rc service manager is uninitalized."
result=1
fi
return $result
}
load_rc_config $name
run_rc_command "$1"