From 2aff1db42bbea2f9cfc41dffbb11c3a2da95f532 Mon Sep 17 00:00:00 2001 From: fbt Date: Fri, 9 Mar 2018 05:57:26 +0300 Subject: [PATCH] Keep the state yo Signed-off-by: fbt --- ssm | 76 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 57 insertions(+), 19 deletions(-) diff --git a/ssm b/ssm index 6263418..ab8d668 100755 --- a/ssm +++ b/ssm @@ -601,6 +601,7 @@ var service_pid \ service_cgroup_procs \ service_cgroup_path \ service_signals_passthru \ + service_config_current \ cgroup_home \ failed_deps \ svc_pidfile \ @@ -610,12 +611,15 @@ var service_pid \ cfg_dir \ rundir \ logdir \ + action \ _self ## Internal defaults var flag_list_services = 0 var flag_edit_service = 0 var flag_reset_exit = 0 +var flag_reread_service = 0 +var flag_forget_service = 0 ## check for some environment stuff var EDITOR := 'vim' @@ -713,9 +717,15 @@ while (( $# )); do (--reset-exit) # Reset last exit status flag_reset_exit = 1;; - (-e|--edit-service) # Edit the service file. + (-e|--edit) # Edit the service file. 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;; (-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;; (*) break;; @@ -725,7 +735,7 @@ while (( $# )); do done # 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" done @@ -735,11 +745,9 @@ service_path += "$XDG_CONFIG_HOME/ssm/services" '/etc/ssm/services' "$rundir/ser # Special actions if flag_list_services; then var known_services - for i in "$rundir"/*.{pid,exit,stopped,enabled}; do - i_fname="${i##*/}" - i_sname="${i_fname%.*}" - - known_services u "$i_sname" || known_services += "$i_sname" + for i in "$rundir"/current/*; do + i_name="${i##*/}" + known_services u "$i_name" || known_services += "$i_name" done for s in "${known_services[@]}"; do @@ -753,12 +761,21 @@ fi # This script requires at least one argument (( $# >= 1 )) || { usage; exit 2; } -# If $1 is a full path, source it. -# If not, search for it in the service path. +# Unless otherwise specified +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 service_config = "$1" + service_name = "${service_config##*/}" else - for i in "${service_path[@]/%//$1}"; do + for i in "$service_config_current" "${service_path[@]/%//$service_name}"; do [[ -f "$i" ]] && { service_config = "$i" break @@ -766,16 +783,25 @@ else done 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 service_config || die 19 "Service not found: $1" # Edit the service config 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. service_ready_flag := "$rundir/$service_name.ready" service_stopped_flag := "$rundir/$service_name.stopped" @@ -796,6 +822,12 @@ flag_reset_exit && { reset-exit; die $?; } # Get the 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 service_args && service_command += "${service_args[@]}" service_type == 'oneshot' && service_oneshot = 1 @@ -893,8 +925,14 @@ if cgroups; then fi fi +# Were we given something to do? +(( $# )) || { usage; die 2; } + +# Action! +action = "$1" + # 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. service_workdir && { @@ -902,12 +940,12 @@ service_workdir && { } # 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_service_action "$2"; res=$? +run_service_action "$action"; res=$? -case "$2" in +case "$action" in stop) result "$res" \ 0 "Stopped $service_name" \ @@ -948,4 +986,4 @@ esac (( res )) && exit "$res" # 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!"