2022-03-21 12:25:55 +00:00
|
|
|
#!/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'}
|
2022-10-21 18:41:16 +00:00
|
|
|
: ${S6_DIST_PATH:="$S6_PATH/dist"}
|
2022-03-21 12:25:55 +00:00
|
|
|
: ${S6_RC_PATH:="$S6_PATH/rc"}
|
2022-10-21 18:41:16 +00:00
|
|
|
: ${S6_ENV_PATH:="$S6_PATH/env"}
|
2022-03-21 12:25:55 +00:00
|
|
|
: ${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
|
2022-10-21 18:41:16 +00:00
|
|
|
s6-rc-compile "$S6_SV_PATH/current.$DB_FRESH_NAC" "$S6_RC_PATH" || ERR="$?" error "Failed to compile current s6 database"
|
2022-03-21 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
update() {
|
2022-03-21 12:30:01 +00:00
|
|
|
s6-rc-update "$S6_SV_PATH/current.$DB_FRESH_ACT" || ERR="$?" error "Failed to update live state of the database"
|
2022-03-21 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
swap() {
|
2022-07-30 14:00:54 +00:00
|
|
|
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
|
2022-03-21 12:25:55 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 18:41:16 +00:00
|
|
|
dist() {
|
|
|
|
SDIR="$S6_DIST_PATH/rc" DPATH="$S6_RC_PATH" distdefs
|
|
|
|
SDIR="$S6_DIST_PATH/env" DPATH="$S6_ENV_PATH" distdefs
|
|
|
|
}
|
|
|
|
|
|
|
|
distdefs() {
|
|
|
|
[ -z "$SDIR" ] && error "SDIR not defined"
|
|
|
|
[ -z "$DPATH" ] && error "DPATH not defined"
|
|
|
|
for cdir in "$SDIR"/*; do
|
|
|
|
local srv="${cdir##*/}"
|
|
|
|
local dsv="$DPATH/$srv"
|
2022-11-01 13:24:27 +00:00
|
|
|
if [ ! -e "$dsv" ]; then
|
2022-10-21 18:41:16 +00:00
|
|
|
ln -sv "$cdir" "$dsv" || ERR="$?" error "Failed to create reference"
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2022-03-21 12:25:55 +00:00
|
|
|
for act in $@; do
|
|
|
|
cstate
|
2022-07-30 14:00:54 +00:00
|
|
|
case "$act" in
|
2022-10-21 18:41:16 +00:00
|
|
|
generate|update|swap|dist) eval "$act" || return;;
|
2022-07-30 14:00:54 +00:00
|
|
|
*) error "Invalid action $act";;
|
|
|
|
esac
|
2022-03-21 12:25:55 +00:00
|
|
|
done
|