Signed-off-by: fbt <fbt@fleshless.org>
This commit is contained in:
Jack L. Frost 2018-03-09 06:57:26 +03:00
parent 2aff1db42b
commit 924324aa10
1 changed files with 47 additions and 37 deletions

84
ssm
View File

@ -494,22 +494,15 @@ stop() {
info() { info() {
declare -a _info_items declare -a _info_items
declare _status_code _status declare _status_code _status _show_pid
var _info_items _status_code _status var _info_items _status_code _status _show_pid
_info_items = 'Name' "$service_name" info::item() { printf "%16s: %s\n" "$1" "$2"; }
if service_oneshot; then
_info_items += 'Oneshot' 'yes'
fi
status; _status_code = "$?" status; _status_code = "$?"
case $_status_code in case $_status_code in
(0) (0) _status = 'running';;
_status = 'running' (1) _status = 'down';;
_info_items += 'PID' "$service_pid"
_info_items += 'PIDFile' "$service_pidfile"
;;
(2) _status = 'success';; (2) _status = 'success';;
(7) _status = 'stopped';; (7) _status = 'stopped';;
(9) _status = "failed: $service_exit_last";; (9) _status = "failed: $service_exit_last";;
@ -517,26 +510,35 @@ info() {
(*) _status = 'unknown';; (*) _status = 'unknown';;
esac esac
_info_items += 'Status' "$_status" info::item Name "$service_name"
_info_items += \ if service_oneshot; then
"Exec" "${service_command[*]}" \ info::item Oneshot 'yes'
"Respawn" "$service_respawn" \ else
"Config path" "$service_config" \ info::item Restart "$service_respawn"
# Logs
_info_items += 'Output log' "$service_logfile_out"
service_logfile_out == "$service_logfile_err" || \
_info_items += "Error log" "$service_logfile_err"
# Show the cgroup
if cgroups; then
_info_items += "Cgroup" "$cgroup_home/$service_cgroup_name" \
"Cgroup procs" "${#service_cgroup_procs[@]}"
fi fi
printf "%16s: %s\n" "${_info_items[@]}" info::item Status "$_status"
info::item Exec "${service_command[*]}"
info::item Config "$service_config"
info::item Workdir "$service_workdir"
info::item 'Output log' "$service_logfile_out"
service_logfile_out == "$service_logfile_err" || \
info::item 'Error log' "$service_logfile_err"
if service_running; then
info::item PID "$service_pid"
info::item PIDFile "$service_pidfile"
cgroups && {
info::item Cgroup "$cgroup_home/$service_cgroup_name"
info::item 'Cgroup procs' "${#service_cgroup_procs[@]}"
}
fi
return 0
} }
## Restart just calls the script twice by default ## Restart just calls the script twice by default
@ -620,6 +622,7 @@ var flag_edit_service = 0
var flag_reset_exit = 0 var flag_reset_exit = 0
var flag_reread_service = 0 var flag_reread_service = 0
var flag_forget_service = 0 var flag_forget_service = 0
var flag_load_service = 0
## check for some environment stuff ## check for some environment stuff
var EDITOR := 'vim' var EDITOR := 'vim'
@ -723,9 +726,15 @@ while (( $# )); do
(-r|--reread) # Reload the service file. (-r|--reread) # Reload the service file.
flag_reread_service = 1;; flag_reread_service = 1;;
(-u|--forget) # Unload a service. (-f|--forget) # Unload a service.
flag_forget_service = 1;; flag_forget_service = 1;;
(-l|--load) # Load a service.
flag_load_service = 1;;
(-i|--info)
action = 'info';;
(--) shift; break;; (--) shift; break;;
(-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;; (-*) printf 'Unknown key: %s\n' "$1" >&2; exit 1;;
(*) break;; (*) break;;
@ -771,9 +780,9 @@ if flag_reread_service; then
fi fi
# Find the service # Find the service
if [[ $1 == /* ]]; then if [[ $service_name == /* ]]; then
service_config = "$1" service_config = "$service_name"
service_name = "${service_config##*/}" service_name = "${service_name##*/}"
else else
for i in "$service_config_current" "${service_path[@]/%//$service_name}"; do for i in "$service_config_current" "${service_path[@]/%//$service_name}"; do
[[ -f "$i" ]] && { [[ -f "$i" ]] && {
@ -797,7 +806,7 @@ if flag_forget_service; then
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: $service_name"
# Edit the service config # Edit the service config
flag_edit_service && { edit; die $?; } flag_edit_service && { edit; die $?; }
@ -828,6 +837,9 @@ service_config == "$service_config_current" || {
printf 'Loaded %s from: %s\n' "$service_name" "$service_config" >&2 printf 'Loaded %s from: %s\n' "$service_name" "$service_config" >&2
} }
# Die if we only needed to load a service
flag_load_service && die 0
# 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
@ -925,11 +937,9 @@ if cgroups; then
fi fi
fi fi
# Were we given something to do?
(( $# )) || { usage; die 2; }
# Action! # Action!
action = "$1" [[ "$1" ]] && action = "$1"
action || { usage; die 2; }
# Do we have such a function? # Do we have such a function?
if_service_action "$action" || die 17 "Function $action is not defined for $service_name." if_service_action "$action" || die 17 "Function $action is not defined for $service_name."