ports/main/nnd-s6-services/manage.sh

57 lines
1.4 KiB
Bash
Executable File

#!/bin/sh
export PATH="/usr/bin:/bin"
error() {
: ${ERR:=1}
echo "$@" >&2
return "$ERR"
}
alt_ab() {
if [ "$1" == "a" ]; then echo "b"; else echo "a"; fi
}
: ${S6_PATH:='/etc/s6'}
: ${S6_RC_PATH:="$S6_PATH/rc"}
: ${S6_SV_PATH:="$S6_PATH/sv"}
# A/B mode, always keep last copy
DB_FRESH_ACT=""
DB_FRESH_NAC=""
cstate() {
DB_FRESH_ACT="$(readlink "$S6_SV_PATH/current" | cut -d. -f2)" # if it fails (missing), it will be empty and 'a' will be used as inactive by default
DB_FRESH_NAC="$(alt_ab "$DB_FRESH_ACT")"
}
generate() {
mkdir -p "$S6_SV_PATH" || ERR="$?" error "Failed to create sv directory"
# A/B current
if [ -d "$S6_SV_PATH/current.$DB_FRESH_NAC" ]; then
rm -rf "$S6_SV_PATH/current.$DB_FRESH_NAC" || ERR="$?" error "Failed to remove inactive database path"
fi
s6-rc-compile "$S6_SV_PATH/current.$DB_FRESH_NAC" "$S6_RC_PATH"/* || ERR="$?" error "Failed to compile current s6 database"
}
update() {
s6-rc-update "$S6_SV_PATH/current.$DB_FRESH_ACT" || ERR="$?" error "Failed to update live state of the database"
}
swap() {
if [ -d "$S6_SV_PATH/current.$DB_FRESH_NAC" ]; then
ln -sfn "current.$DB_FRESH_NAC" "$S6_SV_PATH/current" || ERR="$?" error "Failed to update A/B current symlink"
else
error "There's no database to switch to"
fi
}
for act in $@; do
cstate
case "$act" in
generate|update|swap) eval "$act";;
*) error "Invalid action $act";;
esac
done