Some more systemd work, a simple DSL
Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
parent
51d452934a
commit
a633061d30
123
ssm
123
ssm
|
@ -49,12 +49,8 @@ respawn() {
|
||||||
declare jobs job_pid
|
declare jobs job_pid
|
||||||
|
|
||||||
respawn::cleanup() {
|
respawn::cleanup() {
|
||||||
jobs=( $(jobs -p) )
|
kill -n "$service_stop_signal" "$job_pid"
|
||||||
|
wait "$job_pid"
|
||||||
if [[ "$jobs" ]]; then
|
|
||||||
kill -n 15 "${jobs[@]}"
|
|
||||||
wait "${jobs[@]}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
rm -f "$svc_pidfile" "$service_ready_flag"
|
rm -f "$svc_pidfile" "$service_ready_flag"
|
||||||
|
|
||||||
|
@ -227,6 +223,43 @@ read_systemd_service() {
|
||||||
done < "$1"
|
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
|
# Overloadable functions
|
||||||
## Start the service, write down the svc pid
|
## Start the service, write down the svc pid
|
||||||
start() {
|
start() {
|
||||||
|
@ -269,11 +302,7 @@ start() {
|
||||||
reload() {
|
reload() {
|
||||||
(( service_running )) || return 3
|
(( service_running )) || return 3
|
||||||
|
|
||||||
if (( service_managed )); then
|
kill -n "$service_reload_signal" "$service_pid"
|
||||||
kill -n 1 "$service_pid"
|
|
||||||
else
|
|
||||||
"${service_command_reload[@]}"
|
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
|
|
||||||
## Stop the service
|
## Stop the service
|
||||||
|
@ -289,11 +318,7 @@ stop() {
|
||||||
else
|
else
|
||||||
(( service_running )) || return 3
|
(( service_running )) || return 3
|
||||||
|
|
||||||
if (( service_managed )); then
|
nullexec kill -n "$service_stop_signal" "$service_pid" || return 1
|
||||||
nullexec kill "$service_pid" || return 1
|
|
||||||
else
|
|
||||||
"${service_command_stop[@]}" || return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
pid_wait "$service_pid" || return 5
|
pid_wait "$service_pid" || return 5
|
||||||
> "$service_stopped_flag"
|
> "$service_stopped_flag"
|
||||||
|
@ -372,26 +397,14 @@ main() {
|
||||||
|
|
||||||
# Let's set some defaults
|
# Let's set some defaults
|
||||||
service_managed=1
|
service_managed=1
|
||||||
|
|
||||||
usrdir='/usr/share/ssm'
|
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
|
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"
|
rundir="$XDG_RUNTIME_DIR/ssm"
|
||||||
logdir="$HOME/log/ssm"
|
logdir="$HOME/log/ssm"
|
||||||
else
|
else
|
||||||
|
@ -400,26 +413,36 @@ main() {
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Warn the user of deprecated stuff.
|
# Warn the user of deprecated stuff.
|
||||||
if [[ -d "/etc/ssm/init.d" ]]; then
|
for p in "/etc/ssm/init.d" "$XDG_CONFIG_HOME/ssm/init.d"; do
|
||||||
printf 'WARNING: `/etc/ssm/init.d` was renamed to `/etc/ssm/services`! Please move your scripts accordingly!\n' >&2
|
if [[ -d "$p" ]]; then
|
||||||
service_path+=( "/etc/ssm/init.d" )
|
printf 'WARNING: `%s` was renamed to `%s`! Please move your scripts accordingly!\n' "$p" "${p%init.d}services" >&2
|
||||||
fi
|
service_path+=( "$p" )
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
# Common service path
|
# 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
|
# 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
|
for (( idx=${#cfg_path[@]}-1; idx>=0; idx-- )); do
|
||||||
cfg_dir="${cfg_path[idx]}"
|
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
|
for f in "$cfg_dir/functions"/*; do
|
||||||
source "$f" || die 9 "Failed to source functions from $f"
|
source "$f" || die 9 "Failed to source functions from $f"
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
||||||
# Now create the needed runtime stuff
|
# Now create the needed runtime stuff
|
||||||
for d in "$rundir" "$logdir"; do
|
for d in "$rundir" "$logdir"; do
|
||||||
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
|
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
|
||||||
|
@ -439,15 +462,17 @@ main() {
|
||||||
|
|
||||||
[[ $service_config ]] || {
|
[[ $service_config ]] || {
|
||||||
# Search for a systemd service too
|
# Search for a systemd service too
|
||||||
for i in {/etc/systemd,/run/systemd,/lib/systemd,/usr/lib/systemd}/system/$1.service; do
|
(( systemd )) && {
|
||||||
[[ -f "$i" ]] && {
|
for i in "${systemd_service_path[@]/%//$1.service}"; do
|
||||||
service_name=$1
|
[[ -f "$i" ]] && {
|
||||||
service_systemd=1
|
service_name=$1
|
||||||
service_config=$i
|
service_systemd=1
|
||||||
read_systemd_service "$i"
|
service_config=$i
|
||||||
break
|
read_systemd_service "$i"
|
||||||
}
|
break
|
||||||
done
|
}
|
||||||
|
done
|
||||||
|
}
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user