Some more systemd work, a simple DSL

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2018-01-07 12:02:12 +03:00
parent 51d452934a
commit a633061d30

123
ssm
View File

@ -49,12 +49,8 @@ respawn() {
declare jobs job_pid
respawn::cleanup() {
jobs=( $(jobs -p) )
if [[ "$jobs" ]]; then
kill -n 15 "${jobs[@]}"
wait "${jobs[@]}"
fi
kill -n "$service_stop_signal" "$job_pid"
wait "$job_pid"
rm -f "$svc_pidfile" "$service_ready_flag"
@ -227,6 +223,43 @@ read_systemd_service() {
done < "$1"
}
# Some DSL for the config
setter() {
for i in "$@"; do
declare varname=$i
eval "
${varname}() {
declare mode=set
declare -n _var=${varname}
while (( \$# )); do
case \$1 in
(=) mode=set;;
(+=) mode=append;;
(_=) mode=prepend;;
(--) shift; break;;
(*) break;;
esac
shift
done
case \$mode in
(append) _var+=( \"\$@\" );;
(prepend) _var=( \"\$@\" \"\${_var[@]}\" );;
(set) _var=( \"\$@\" );;
esac
}; readonly -f ${varname}
"
done
}; readonly setter
setter \
service_path \
service_workdir \
service_stop_timeout service_ready_timeout \
service_stop_signal service_reload_signal service_signals \
systemd systemd_service_path
# Overloadable functions
## Start the service, write down the svc pid
start() {
@ -269,11 +302,7 @@ start() {
reload() {
(( service_running )) || return 3
if (( service_managed )); then
kill -n 1 "$service_pid"
else
"${service_command_reload[@]}"
fi
kill -n "$service_reload_signal" "$service_pid"
}
## Stop the service
@ -289,11 +318,7 @@ stop() {
else
(( service_running )) || return 3
if (( service_managed )); then
nullexec kill "$service_pid" || return 1
else
"${service_command_stop[@]}" || return 1
fi
nullexec kill -n "$service_stop_signal" "$service_pid" || return 1
pid_wait "$service_pid" || return 5
> "$service_stopped_flag"
@ -372,26 +397,14 @@ main() {
# Let's set some defaults
service_managed=1
usrdir='/usr/share/ssm'
systemd_service_path=( /etc/systemd/system /run/systemd/system /lib/systemd/system )
# XDG stuff
default XDG_CONFIG_HOME "$HOME/.config"
default XDG_RUNTIME_DIR "/run/user/$UID"
if (( $UID )); then
# XDG stuff
default XDG_CONFIG_HOME "$HOME/.config"
default XDG_RUNTIME_DIR "/run/user/$UID"
service_path=( "$XDG_CONFIG_HOME/ssm/services" )
cfg_path=( "$XDG_CONFIG_HOME/ssm" )
# Warn the user of deprecated stuff.
if [[ -d "$XDG_CONFIG_HOME/ssm/init.d" ]]; then
printf 'WARNING: `%s` was renamed to `%s`! Please move your scripts accordingly!\n' \
"$XDG_CONFIG_HOME/ssm/init.d" \
"$XDG_CONFIG_HOME/ssm/services" >&2
service_path+=( "$XDG_CONFIG_HOME/ssm/init.d" )
fi
rundir="$XDG_RUNTIME_DIR/ssm"
logdir="$HOME/log/ssm"
else
@ -400,26 +413,36 @@ main() {
fi
# Warn the user of deprecated stuff.
if [[ -d "/etc/ssm/init.d" ]]; then
printf 'WARNING: `/etc/ssm/init.d` was renamed to `/etc/ssm/services`! Please move your scripts accordingly!\n' >&2
service_path+=( "/etc/ssm/init.d" )
fi
for p in "/etc/ssm/init.d" "$XDG_CONFIG_HOME/ssm/init.d"; do
if [[ -d "$p" ]]; then
printf 'WARNING: `%s` was renamed to `%s`! Please move your scripts accordingly!\n' "$p" "${p%init.d}services" >&2
service_path+=( "$p" )
fi
done
# Common service path
service_path+=( '/etc/ssm/services' "$rundir/services" "$usrdir/services" )
service_path+=( "$XDG_CONFIG_HOME/ssm/services" '/etc/ssm/services' "$rundir/services" "$usrdir/services" )
# Common config path
cfg_path+=( '/etc/ssm' )
cfg_path+=( "$XDG_CONFIG_HOME/ssm" '/etc/ssm' )
# Load custom functions, reversing the PATH order
# Load custom config and functions, reversing the PATH order
for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do
cfg_dir="${cfg_path[idx]}"
(( ssm_config )) || {
[[ -f "$cfg_dir/ssm.conf" ]] && {
source "$cfg_dir/ssm.conf" || die 37 "Failed to load config: $cfg_dir/ssm.conf"
ssm_config=1
}
}
for f in "$cfg_dir/functions"/*; do
source "$f" || die 9 "Failed to source functions from $f"
done
done
# Now create the needed runtime stuff
for d in "$rundir" "$logdir"; do
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
@ -439,15 +462,17 @@ main() {
[[ $service_config ]] || {
# Search for a systemd service too
for i in {/etc/systemd,/run/systemd,/lib/systemd,/usr/lib/systemd}/system/$1.service; do
[[ -f "$i" ]] && {
service_name=$1
service_systemd=1
service_config=$i
read_systemd_service "$i"
break
}
done
(( systemd )) && {
for i in "${systemd_service_path[@]/%//$1.service}"; do
[[ -f "$i" ]] && {
service_name=$1
service_systemd=1
service_config=$i
read_systemd_service "$i"
break
}
done
}
}
fi