Improve s6-rc rc.d script

* Deduplicate path literals
	* Pass live directory to s6-rc invocations

Changes #31
This commit is contained in:
Crest 2022-07-04 02:42:22 +02:00
parent 050fb34846
commit 803ebdbded
1 changed files with 35 additions and 17 deletions

View File

@ -5,6 +5,13 @@
# 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"
@ -27,7 +34,7 @@ s6_wait()
{
local i=0
while ! s6-svscanctl -z /run/service 2>/dev/null; do
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
@ -48,19 +55,19 @@ s6_wait()
s6_rc_init()
{
if [ ! -e /run/s6-rc ]; then
s6-rc-init /run/service
if [ ! -e "$live_dir" ]; then
s6-rc-init -l "$live_dir" "$scan_dir"
fi
}
s6_rc_up()
{
s6-rc -v 2 -u -t $up_timeout change enabled
s6-rc -l "$live_dir" -v 2 -u -t "$up_timeout" change enabled
}
s6_rc_down()
{
s6-rc -v 2 -d -a -t $down_timeout change
s6-rc -l "$live_dir" -v 2 -d -a -t "$down_timeout" change
}
s6_rc_start()
@ -82,16 +89,26 @@ 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
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
if s6-rc-update -v 2 -t $update_timeout "/etc/s6-rc/.compiled.$uuid"; then
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 "Updated s6-rc service database."
echo "Deleting old service databases."
find -s . -mindepth 1 -maxdepth 1 -type d -name '.compiled.*' -not -name ".compiled.$uuid" -print0 | xargs -0 rm -r
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
}
@ -99,19 +116,20 @@ s6_rc_status()
{
local result=0
if s6-svscanctl -z /run/service 2>/dev/null; then
echo "The s6-svscan supervisor is responsible."
# 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 /run/s6-rc ]; then
echo "The s6-rc service manager is initialized."
if [ -e "$live_dir" ]; then
echo "The s6-rc service manager has been initialized."
echo
echo "These services are currently active:"
s6-rc -a list
s6-rc -l "$live_dir" -a list
else
echo "The s6-rc service manager is uninitalized."
result=1