Keep the state yo

Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2018-03-09 05:57:26 +03:00
parent 0dd00077a5
commit 2aff1db42b

76
ssm
View File

@ -601,6 +601,7 @@ var service_pid \
service_cgroup_procs \ service_cgroup_procs \
service_cgroup_path \ service_cgroup_path \
service_signals_passthru \ service_signals_passthru \
service_config_current \
cgroup_home \ cgroup_home \
failed_deps \ failed_deps \
svc_pidfile \ svc_pidfile \
@ -610,12 +611,15 @@ var service_pid \
cfg_dir \ cfg_dir \
rundir \ rundir \
logdir \ logdir \
action \
_self _self
## Internal defaults ## Internal defaults
var flag_list_services = 0 var flag_list_services = 0
var flag_edit_service = 0 var flag_edit_service = 0
var flag_reset_exit = 0 var flag_reset_exit = 0
var flag_reread_service = 0
var flag_forget_service = 0
## check for some environment stuff ## check for some environment stuff
var EDITOR := 'vim' var EDITOR := 'vim'
@ -713,9 +717,15 @@ while (( $# )); do
(--reset-exit) # Reset last exit status (--reset-exit) # Reset last exit status
flag_reset_exit = 1;; flag_reset_exit = 1;;
(-e|--edit-service) # Edit the service file. (-e|--edit) # Edit the service file.
flag_edit_service = 1;; flag_edit_service = 1;;
(-r|--reread) # Reload the service file.
flag_reread_service = 1;;
(-u|--forget) # Unload a service.
flag_forget_service = 1;;
(--) shift; break;; (--) shift; break;;
(-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;; (-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;;
(*) break;; (*) break;;
@ -725,7 +735,7 @@ while (( $# )); do
done done
# Now create the needed runtime stuff # Now create the needed runtime stuff
for d in "$rundir" "$logdir"; do for d in "$rundir" "$rundir/current" "$logdir"; do
mkdir -p "$d" || die 3 "Failed to create runtime dir: $d" mkdir -p "$d" || die 3 "Failed to create runtime dir: $d"
done done
@ -735,11 +745,9 @@ service_path += "$XDG_CONFIG_HOME/ssm/services" '/etc/ssm/services' "$rundir/ser
# Special actions # Special actions
if flag_list_services; then if flag_list_services; then
var known_services var known_services
for i in "$rundir"/*.{pid,exit,stopped,enabled}; do for i in "$rundir"/current/*; do
i_fname="${i##*/}" i_name="${i##*/}"
i_sname="${i_fname%.*}" known_services u "$i_name" || known_services += "$i_name"
known_services u "$i_sname" || known_services += "$i_sname"
done done
for s in "${known_services[@]}"; do for s in "${known_services[@]}"; do
@ -753,12 +761,21 @@ fi
# This script requires at least one argument # This script requires at least one argument
(( $# >= 1 )) || { usage; exit 2; } (( $# >= 1 )) || { usage; exit 2; }
# If $1 is a full path, source it. # Unless otherwise specified
# If not, search for it in the service path. service_name = "$1"; shift
# Find the current loaded service and remove it if necessary
service_config_current = "$rundir/current/$service_name"
if flag_reread_service; then
rm -vf "$service_config_current" >&2 || die "$?" "Abort!"
fi
# Find the service
if [[ $1 == /* ]]; then if [[ $1 == /* ]]; then
service_config = "$1" service_config = "$1"
service_name = "${service_config##*/}"
else else
for i in "${service_path[@]/%//$1}"; do for i in "$service_config_current" "${service_path[@]/%//$service_name}"; do
[[ -f "$i" ]] && { [[ -f "$i" ]] && {
service_config = "$i" service_config = "$i"
break break
@ -766,16 +783,25 @@ else
done done
fi fi
# We really don't want this overriden
readonly service_name
# Unload the service and leave if asked.
if flag_forget_service; then
if service_config_current is file; then
rm -f "$service_config_current" || die "$?" "Abort!"
die 0 "Forgot service: $service_name"
else
die 90 "Service not currently known: $service_name"
fi
fi
# Die if there is no service config file # Die if there is no service config file
service_config || die 19 "Service not found: $1" service_config || die 19 "Service not found: $1"
# Edit the service config # Edit the service config
flag_edit_service && { edit; die $?; } flag_edit_service && { edit; die $?; }
# Service name is the basename
service_name = "${1##*/}"
readonly service_name
# These depend on the service_name and make little sense to reconfigure. # These depend on the service_name and make little sense to reconfigure.
service_ready_flag := "$rundir/$service_name.ready" service_ready_flag := "$rundir/$service_name.ready"
service_stopped_flag := "$rundir/$service_name.stopped" service_stopped_flag := "$rundir/$service_name.stopped"
@ -796,6 +822,12 @@ flag_reset_exit && { reset-exit; die $?; }
# Get the service config # Get the service config
source -- "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config" source -- "$service_config" "${@:3}" || die 7 "Failed to read the service config: $service_config"
# “Load” the service into memory
service_config == "$service_config_current" || {
cat "$service_config" > "$rundir/current/$service_name"
printf 'Loaded %s from: %s\n' "$service_name" "$service_config" >&2
}
# Legacy # Legacy
service_args && service_command += "${service_args[@]}" service_args && service_command += "${service_args[@]}"
service_type == 'oneshot' && service_oneshot = 1 service_type == 'oneshot' && service_oneshot = 1
@ -893,8 +925,14 @@ if cgroups; then
fi fi
fi fi
# Were we given something to do?
(( $# )) || { usage; die 2; }
# Action!
action = "$1"
# Do we have such a function? # Do we have such a function?
if_service_action "$2" || die 17 "Function $2 is not defined for $service_name." if_service_action "$action" || die 17 "Function $action is not defined for $service_name."
# cd into the workdir, if defined. # cd into the workdir, if defined.
service_workdir && { service_workdir && {
@ -902,12 +940,12 @@ service_workdir && {
} }
# Run pre_$action function # Run pre_$action function
run_service_action "pre_$2" || die 13 "pre_$2 failed!" run_service_action "pre_$action" || die 13 "pre_$action failed!"
# Run the main action # Run the main action
run_service_action "$2"; res=$? run_service_action "$action"; res=$?
case "$2" in case "$action" in
stop) stop)
result "$res" \ result "$res" \
0 "Stopped $service_name" \ 0 "Stopped $service_name" \
@ -948,4 +986,4 @@ esac
(( res )) && exit "$res" (( res )) && exit "$res"
# Run post_$action function # Run post_$action function
run_service_action "post_$2" || die 15 "post_$2 has failed!" run_service_action "post_$action" || die 15 "post_$action has failed!"