Fix returns and don't skip if failing to execute something

This commit is contained in:
Alex D. 2022-12-05 13:54:30 +00:00
parent 50e3a97088
commit 9c5bcc29e5
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
2 changed files with 73 additions and 23 deletions

View File

@ -3,7 +3,7 @@
. ../../APKBUILD.template
pkgname=nnd-s6-services
pkgver=1.22
pkgver=1.23
pkgrel=0
pkgdesc="Base services for s6"
depends="s6-rc s6-portable-utils s6-linux-utils"

View File

@ -1,11 +1,10 @@
#!/bin/sh
set +e
export PATH="/usr/bin:/bin"
error() {
: ${ERR:=1}
echo "$@" >&2
return "$ERR"
}
alt_ab() {
@ -28,42 +27,73 @@ cstate() {
}
generate() {
mkdir -p "$S6_SV_PATH" || ERR="$?" error "Failed to create sv directory"
if ! mkdir -p "$S6_SV_PATH"; then
error "Failed to create sv directory"
return "$?"
fi
# 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"
if ! rm -rf "$S6_SV_PATH/current.$DB_FRESH_NAC"; then
error "Failed to remove inactive database path"
return "$?"
fi
fi
if ! s6-rc-compile "$S6_SV_PATH/current.$DB_FRESH_NAC" "$S6_RC_PATH"; then
error "Failed to compile current s6 database"
return "$?"
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"
if ! s6-rc-update "$S6_SV_PATH/current.$DB_FRESH_ACT"; then
error "Failed to update live state of the database"
return "$?"
fi
}
swap() {
local new="current.$DB_FRESH_NAC"
[ -d "$S6_SV_PATH/$new" ] || error "There's no database to switch to"
if ! [ -d "$S6_SV_PATH/$new" ]; then
error "There's no database to switch to"
return 1
fi
s6-rc-db -c "$S6_SV_PATH/$new" check || ERR="$?" error "Database $DB_FRESH_NAC is invalid"
ln -sfn "$new" "$S6_SV_PATH/current" || ERR="$?" error "Failed to update A/B current symlink"
if ! s6-rc-db -c "$S6_SV_PATH/$new" check; then
error "Database $DB_FRESH_NAC is invalid"
return "$?"
fi
if ! ln -sfn "$new" "$S6_SV_PATH/current"; then
error "Failed to update A/B current symlink"
return "$?"
fi
}
dist() {
SDIR="$S6_DIST_PATH/rc" DPATH="$S6_RC_PATH" distdefs
SDIR="$S6_DIST_PATH/env" DPATH="$S6_ENV_PATH" distdefs
SDIR="$S6_DIST_PATH/rc" DPATH="$S6_RC_PATH" distdefs || return "$?"
SDIR="$S6_DIST_PATH/env" DPATH="$S6_ENV_PATH" distdefs || return "$?"
# Remove any dangling invalid symlinks
find -L /etc/s6/rc/ -type l -exec rm -v -- {} +
}
distdefs() {
[ -z "$SDIR" ] && error "SDIR not defined"
[ -z "$DPATH" ] && error "DPATH not defined"
if [ -z "$SDIR" ]; then
error "SDIR not defined"
return 1
fi
if [ -z "$DPATH" ]; then
error "DPATH not defined"
return 1
fi
for cdir in "$SDIR"/*; do
local srv="${cdir##*/}"
local dsv="$DPATH/$srv"
if [ ! -e "$dsv" ]; then
ln -sv "$cdir" "$dsv" || ERR="$?" error "Failed to create reference"
if ! ln -sv "$cdir" "$dsv"; then
error "Failed to create reference"
return "$?"
fi
fi
done
}
@ -72,16 +102,36 @@ custom() {
local SVC="$1"
local target="$S6_RC_PATH/$SVC"
[ -e "$target" ] || error "Service $SVC doesn't exist"
[ -h "$target" ] || error "Service $SVC is already a custom instance"
rm -rf "$target"
cp -r "$S6_DIST_PATH/rc/$SVC" "$target"
if ! [ -e "$target" ]; then
error "Service $SVC doesn't exist"
return "$?"
fi
if ! [ -h "$target" ]; then
error "Service $SVC is already a custom instance"
return "$?"
fi
customfunc "$S6_DIST_PATH/rc/$SVC" "$target" || return "$?"
local target="$S6_ENV_PATH/$SVC"
if [ -e "$target" ]; then
rm -rf "$target"
cp -r "$S6_DIST_PATH/env/$SVC" "$target"
customfunc "$S6_DIST_PATH/env/$SVC" "$target" || return "$?"
fi
}
customfunc() {
local src="$1"
local target="$2"
if ! rm -rf "$target"; then
error "Failed to remove distributed directory $target"
return "$?"
fi
if ! cp -r "$S6_DIST_PATH/rc/$SVC" "$target"; then
error "Failed to initialise custom directory $target"
return "$?"
fi
}
@ -89,6 +139,6 @@ cmd="$1"
shift
cstate
case "$cmd" in
generate|update|swap|dist|custom) eval "$cmd" $@;;
*) error "Invalid command $cmd";;
generate|update|swap|dist|custom) eval "$cmd" $@ || return "$?";;
*) error "Invalid command $cmd"; return 1;;
esac