aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorB Stack <bgstack15@gmail.com>2020-01-12 18:42:10 -0500
committerB Stack <bgstack15@gmail.com>2020-01-12 18:42:10 -0500
commit595982218707cbbe30e7920d3a6af8f9398e32b0 (patch)
tree0b7071723719121a6cae63e403b05b63e4773101
parentWIP: initial commit, 5% done (diff)
downloadsystemdtl-595982218707cbbe30e7920d3a6af8f9398e32b0.tar.gz
systemdtl-595982218707cbbe30e7920d3a6af8f9398e32b0.tar.bz2
systemdtl-595982218707cbbe30e7920d3a6af8f9398e32b0.zip
WIP: add most parsing
Still need to add the commands to execute the action list.
-rw-r--r--README.md9
-rwxr-xr-xsrc/bin/systemctl.ver165
-rw-r--r--src/etc/systemdtl.conf2
-rwxr-xr-xsrc/sbin/systemctl (renamed from src/bin/systemctl)162
4 files changed, 146 insertions, 92 deletions
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..70b9e6b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,9 @@
+# Readme for systemdtl
+Systemdtl is a package that provides a systemctl interface to fool simple things that make hard-coded requests to a systemctl executable.
+This script attempts to convert every request to a suitable real command.
+
+# Project license
+CC-BY-SA 4.0
+
+# Dependencies
+bgscripts-core for framework.sh
diff --git a/src/bin/systemctl.ver1 b/src/bin/systemctl.ver1
deleted file mode 100755
index 74f29c4..0000000
--- a/src/bin/systemctl.ver1
+++ /dev/null
@@ -1,65 +0,0 @@
-#!/bin/sh
-# goal: handle various systemctl commands, particularly for freeipa
-# restart X(.service)?
-# start
-# stop
-# enable
-# status
-# disable
-# mask
-# unmask
-# is-enabled
-# list-unit-files --full
-# is-active
-# reload-or-try-restart X
-# condrestart X
-# daemon-reload --system
-# --now
-
-# log all parameters to learn usage
-logfile=/var/log/systemctl.log
-printf "%s\n" "${*}" >> "${logfile}"
-
-evalparam() {
- # call: evalparam "${_x}" "word/single/double" "${_thisparam}" "${_nextparam}"
- printf "%s\n" "evalparam $*"
- __paramposition="${1}"
- __paramflaglevel="${2}"
- __param="${3}"
- __nextparam="${4}"
- case "${__param}" in
- "now")
- esac
-}
-
-# iterate through all parameters
-_param_count="${#}"
-_x=0
-while test ${_x} -lt ${_param_count} ;
-do
- _x=$(( _x + 1 ))
- eval _thisparam="\${${_x}}"
- test ${_x} -lt ${_param_count} && eval _nextparam="\${$(( _x + 1 ))}" || unset _nextparam
- #printf "%s\n" "param ${_x} is \"${_thisparam}\" and next is \"${_nextparam:-UNDEFINED}\""
- case "${_thisparam}" in
- --*)
- #printf "%s\n" "This is a double-dash! Send the whole thing."
- evalparam "${_x}" "double" "${_thisparam##--}" "${_nextparam:-UNDEFINED}"
- ;;
- -*)
- #printf "%s\n" "This is a single dash! Send each char."
- _i=2
- while test ${_i} -le ${#_thisparam} ;
- do
- _char="$( printf '%s' "${_thisparam}" | cut -c ${_i})"
- evalparam "${_x}" "single" "${_char}" "${_nextparam:-UNDEFINED}" "$(( _i -1 ))"
- _i=$(( _i + 1 ))
- done
- unset _char
- ;;
- *)
- #printf "%s\n" "No dash. Evaluate this as a word."
- evalparam "${_x}" "word" "${_thisparam}" "${_nextparam:-UNDEFINED}"
- ;;
- esac
-done
diff --git a/src/etc/systemdtl.conf b/src/etc/systemdtl.conf
new file mode 100644
index 0000000..17b917f
--- /dev/null
+++ b/src/etc/systemdtl.conf
@@ -0,0 +1,2 @@
+# Part of systemdtl package
+logfile="/var/log/systemctl.log"
diff --git a/src/bin/systemctl b/src/sbin/systemctl
index 2f3201e..3f13c9d 100755
--- a/src/bin/systemctl
+++ b/src/sbin/systemctl
@@ -1,6 +1,6 @@
#!/bin/sh
# Filename: systemctl
-# Location:
+# Location: /usr/sbin/
# Author: bgstack15@gmail.com
# Startdate: 2020-01-10 13:02:14
# Title:
@@ -8,14 +8,34 @@
# Package: systemdtl
# History:
# Usage:
+# Should be mostly like systemctl from systemd.
# Reference: ftemplate.sh 2019-05-02a ; framework.sh 2018-05-02a
+# man 1 systemctl
# Improve:
+# x restart X(.service)?
+# x start
+# x stop
+# x enable
+# x status
+# x disable
+# x mask
+# x unmask
+# x is-enabled
+# x is-active
+# x list-unit-files --full
+# x reload-or-try-restart X
+# x condrestart X
+# x daemon-reload --system
+# x --now
fiversion="2019-05-02a"
systemctlversion="2020-01-10a"
usage() {
${PAGER:-/usr/bin/less -F} >&2 <<ENDUSAGE
usage: systemctl [-duV] [-c conffile]
+Provides a systemctl-like interface to sysvinit. Simulates (poorly)
+various actions on services: start, stop, restart, enable, disable,
+status, mask, unmask, is-enabled, is-active, condrestart
version ${systemctlversion}
-d debug Show debugging info, including parsed variables.
-u usage Show this usage block.
@@ -23,7 +43,7 @@ version ${systemctlversion}
-c conf Read in this config file.
Return values:
0 Normal
- 1 Help or version info displayed
+ 1 Queried service is disabled/inactive
2 Count or type of flaglessvals is incorrect
3 Incorrect OS type
4 Unable to find dependency
@@ -67,8 +87,8 @@ parseFlag() {
case ${flag} in
# INSERT FLAGS HERE
"d" | "debug" | "DEBUG" | "dd" ) setdebug ; ferror "debug level ${debug}" ; __debug_set_by_param=1 ;;
- "u" | "usage" | "help" | "h" ) usage ; exit 1 ;;
- "V" | "fcheck" | "version" ) ferror "${scriptfile} version ${systemctlversion}" ; exit 1 ;;
+ "u" | "usage" | "help" | "h" ) usage ; exit 0 ;;
+ "V" | "fcheck" | "version" ) ferror "${scriptfile} version ${systemctlversion}" ; exit 0 ;;
#"i" | "infile" | "inputfile" ) getval ; infile1=${tempval} ;;
"c" | "conf" | "conffile" | "config" ) getval ; conffile="${tempval}" ;;
"now" ) export SYSTEMCTL_NOW=1 ;;
@@ -109,10 +129,10 @@ test -z "${frameworkscript}" && echo "$0: framework ${f_needed} not found. Try s
. ${frameworkscript} || echo "$0: framework did not run properly. Continuing..." 1>&2
infile1=
outfile1=
-logfile=/var/log/systemctl.log
+define_if_new logfile "/var/log/systemctl.log"
define_if_new interestedparties "bgstack15@gmail.com"
# SIMPLECONF
-#define_if_new default_conffile "/etc/systemctl/systemctl.conf"
+define_if_new default_conffile "/etc/systemdtl.conf"
#define_if_new defuser_conffile ~/.config/systemctl/systemctl.conf
#define_if_new SYSTEMCTL_TMPDIR "$( mktemp -d )"
#tmpfile1="$( TMPDIR="${SYSTEMCTL_TMPDIR}" mktemp )"
@@ -162,21 +182,21 @@ test -z "${__debug_set_by_param}" && fisnum "${SYSTEMCTL_DEBUG}" && debug="${SYS
# exit 2
#fi
-## LOAD CONFIG FROM SIMPLECONF
-## This section follows a simple hierarchy of precedence, with first being used:
-## 1. parameters and flags
-## 2. environment
-## 3. config file
-## 4. default user config: ~/.config/script/script.conf
-## 5. default config: /etc/script/script.conf
-#if test -f "${conffile}" ;
-#then
-# get_conf "${conffile}"
-#else
-# if test "${conffile}" = "${default_conffile}" || test "${conffile}" = "${defuser_conffile}" ; then : ; else test -n "${conffile}" && ferror "${scriptfile}: Ignoring conf file which is not found: ${conffile}." ; fi
-#fi
-#test -f "${defuser_conffile}" && get_conf "${defuser_conffile}"
-#test -f "${default_conffile}" && get_conf "${default_conffile}"
+# LOAD CONFIG FROM SIMPLECONF
+# This section follows a simple hierarchy of precedence, with first being used:
+# 1. parameters and flags
+# 2. environment
+# 3. config file
+# 4. default user config: ~/.config/script/script.conf
+# 5. default config: /etc/script/script.conf
+if test -f "${conffile}" ;
+then
+ get_conf "${conffile}"
+else
+ if test "${conffile}" = "${default_conffile}" || test "${conffile}" = "${defuser_conffile}" ; then : ; else test -n "${conffile}" && ferror "${scriptfile}: Ignoring conf file which is not found: ${conffile}." ; fi
+fi
+test -f "${defuser_conffile}" && get_conf "${defuser_conffile}"
+test -f "${default_conffile}" && get_conf "${default_conffile}"
# CONFIGURE VARIABLES AFTER PARAMETERS
@@ -223,7 +243,7 @@ test -z "${__debug_set_by_param}" && fisnum "${SYSTEMCTL_DEBUG}" && debug="${SYS
# SET TRAPS
#trap "CTRLC" 2
#trap "CTRLZ" 18
-trap '__ec=$? ; clean_systemctl ; trap "" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; exit ${__ec} ;' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
+#trap '__ec=$? ; clean_systemctl ; trap "" 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ; exit ${__ec} ;' 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# DEBUG SIMPLECONF
debuglev 5 && {
@@ -234,7 +254,7 @@ debuglev 5 && {
# MAIN LOOP
#{
- #printf "%s\n" "${*}" >> "${logfile}"
+ printf "%s\n" "${*}" >> "${logfile}"
#echo "thiscount=${thiscount}"
#x=1
#while test ${x:-${thiscount}} -le $(( thiscount - 1 )) && test ${thiscount} -gt 1 ;
@@ -247,7 +267,12 @@ debuglev 5 && {
# actions
actionlist=""
case "${action}" in
- start|stop|status)
+
+ restart|start|stop|status|reload|condrestart|try-restart|reload-or-try-restart)
+ # re-map a few actions
+ case "${action}" in
+ "reload-or-try-restart") action=restart ;;
+ esac
x=1
while test ${x:-${thiscount}} -le $(( thiscount - 1 )) && test ${thiscount} -gt 1 ;
do
@@ -258,12 +283,95 @@ debuglev 5 && {
x=$(( x + 1 ))
done
;;
- enable|disable)
- # action should be update-rc.d ${thisopt} ${action}
- echo "WORK IN PROGRESS"
+
+ enable|disable|mask|unmask)
+ case "${action}" in
+ mask) action=disable ;;
+ unmask) action=enable ;;
+ esac
+ x=1
+ while test ${x:-${thiscount}} -le $(( thiscount - 1 )) && test ${thiscount} -gt 1 ;
+ do
+ eval thisopt="\${opt${x}}"
+ thisopt="$( echo "${thisopt}" | sed -r -e 's/\.service$//;' )"
+ actionstatement="$( printf "%s" "update-rc.d ${thisopt} ${action};" )"
+ actionlist="${actionlist:+${actionlist} }${actionstatement}"
+ test "${SYSTEMCTL_NOW}" = "1" && {
+ case "${action}" in
+ enable)
+ nowaction=start
+ ;;
+ disable)
+ nowaction=stop
+ ;;
+ esac
+ actionstatement="$( printf "%s" "service ${thisopt} ${nowaction:-stop};" )"
+ actionlist="${actionlist:+${actionlist} }${actionstatement}"
+ }
+ x=$(( x + 1 ))
+ done
+ ;;
+
+ daemon-reload)
+ debuglev 1 && echo "${action} is a NOP."
;;
+
+ list-unit-files)
+ # Future improvement: can consume --full, but I do not care enough to deal with it now.
+ ls -Al /etc/init.d
+ ;;
+
+ is-enabled)
+ currentrunlevel="$( who -r | grep -oE 'run-level\s+[[:digit:]]+' | awk '{print $NF}' )"
+ responsenumber=1
+
+ # loop through each service on the command line
+ x=1
+ while test ${x:-${thiscount}} -le $(( thiscount - 1 )) && test ${thiscount} -gt 1 ;
+ do
+ eval thisopt="\${opt${x}}"
+ thisopt="$( echo "${thisopt}" | sed -r -e 's/\.service$//;' )"
+ #actionstatement="$( printf "%s" "service ${thisopt} ${action};" )"
+ scriptfile="$( find "/etc/rc${currentrunlevel}.d" -mindepth 1 -maxdepth 1 -name "S??${thisopt}" 2>/dev/null )"
+ responsetext="disabled"
+ # if file exists, let us return 0.
+ if test -n "${scriptfile}" ;
+ then
+ debuglev 2 && echo "${scriptfile}"
+ responsenumber=0 # any "enabled" response makes systemctl return 0
+ responsetext="enabled"
+ fi
+ echo "${responsetext:-UNKNOWN}"
+ x=$(( x + 1 ))
+ done
+ exit "${responsenumber}"
+ ;;
+
+ is-active)
+ responsenumber=3
+ x=1
+ while test ${x:-${thiscount}} -le $(( thiscount - 1 )) && test ${thiscount} -gt 1 ;
+ do
+ eval thisopt="\${opt${x}}"
+ thisopt="$( echo "${thisopt}" | sed -r -e 's/\.service$//;' )"
+ #actionstatement="$( printf "%s" "service ${thisopt} ${action};" )"
+ servicestatus="$( service "${thisopt}" status 1>/dev/null 2>&1 ; echo "${?}" )"
+ responsetext="stopped"
+ # if file exists, let us return 0.
+ if test ${servicestatus:-1} -eq 0 ;
+ then
+ responsenumber=0
+ responsetext="active"
+ fi
+ echo "${responsetext:-unknown}"
+ x=$(( x + 1 ))
+ done
+ exit "${responsenumber}"
+ ;;
+
*)
ferror "Fatal! 2. Unable to understand action ${action}. Aborted."
+ exit 2
;;
esac
bgstack15