Skip to content

Commit

Permalink
implement is-system-running by setting the substate of a sysinit.targ…
Browse files Browse the repository at this point in the history
…et conf
  • Loading branch information
gdraheim committed May 26, 2019
1 parent 0415b2f commit f4e2cf5
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
35 changes: 32 additions & 3 deletions files/docker/systemctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ def __init__(self):
self._file_for_unit_sysd = None # name.service => /etc/systemd/system/name.service
self._preset_file_list = None # /etc/systemd/system-preset/* => file content
self._default_target = _default_target
self._sysinit_target = None
self.exit_when_no_more_procs = EXIT_WHEN_NO_MORE_PROCS or False
self.exit_when_no_more_services = EXIT_WHEN_NO_MORE_SERVICES or False
self._user_mode = _user_mode
Expand Down Expand Up @@ -1014,13 +1015,13 @@ def load_unit_conf(self, module): # -> conf | None(not-found)
except Exception as e:
logg.warning("%s not loaded: %s", module, e)
return None
def default_unit_conf(self, module): # -> conf
def default_unit_conf(self, module, description = None): # -> conf
""" a unit conf that can be printed to the user where
attributes are empty and loaded() is False """
data = UnitConfParser()
data.set("Unit","Id", module)
data.set("Unit", "Names", module)
data.set("Unit", "Description", "NOT-FOUND "+module)
data.set("Unit", "Description", description or ("NOT-FOUND "+module))
# assert(not data.loaded())
return SystemctlConf(data, module)
def get_unit_conf(self, module): # -> conf (conf | default-conf)
Expand Down Expand Up @@ -3832,13 +3833,15 @@ def system_default(self, arg = True):
a system_halt is performed with the enabled services to be stopped."""
logg.info("system default requested - %s", arg)
init = self._now or self._init
self.sysinit_status(SubState = "initializing")
self.start_system_default(init = init)
def start_system_default(self, init = False):
""" detect the default.target services and start them.
When --init is given then the init-loop is run and
the services are stopped again by 'systemctl halt'."""
default_target = self._default_target
default_services = self.system_default_services("S", default_target)
self.sysinit_status(SubState = "starting")
self.start_units(default_services)
logg.info(" -- system is up")
if init:
Expand All @@ -3852,6 +3855,7 @@ def stop_system_default(self):
at the end of a 'systemctl --init default' loop."""
default_target = self._default_target
default_services = self.system_default_services("K", default_target)
self.sysinit_status(SubState = "stopping")
self.stop_units(default_services)
logg.info(" -- system is down")
def system_halt(self, arg = True):
Expand Down Expand Up @@ -3994,6 +3998,7 @@ def init_loop_until_stop(self, units):
signal.signal(signal.SIGINT, lambda signum, frame: ignore_signals_and_raise_keyboard_interrupt("SIGINT"))
signal.signal(signal.SIGTERM, lambda signum, frame: ignore_signals_and_raise_keyboard_interrupt("SIGTERM"))
self.start_log_files(units)
self.sysinit_status(ActiveState = "active", SubState = "running")
result = None
while True:
try:
Expand Down Expand Up @@ -4026,6 +4031,7 @@ def init_loop_until_stop(self, units):
signal.signal(signal.SIGINT, signal.SIG_DFL)
logg.info("interrupted - exit init-loop")
result = e.message or "STOPPED"
self.sysinit_status(ActiveState = None, SubState = "degraded")
self.read_log_files(units)
self.read_log_files(units)
self.stop_log_files(units)
Expand Down Expand Up @@ -4062,6 +4068,29 @@ def system_reap_zombies(self):
if pid > 1:
running += 1
return running # except PID 0 and PID 1
def sysinit_status(self, **status):
conf = self.sysinit_target()
self.write_status_from(conf, **status)
def sysinit_target(self):
if not self._sysinit_target:
self._sysinit_target = self.default_unit_conf("sysinit.target", "System Initialization")
return self._sysinit_target
def is_system_running(self):
conf = self.sysinit_target()
status_file = self.status_file_from(conf)
if not os.path.isfile(status_file):
return "offline"
status = self.read_status_from(conf)
return status.get("SubState", "unknown")
def system_is_system_running(self):
state = self.is_system_running()
if self._quiet:
return state in [ "running" ]
else:
if state in [ "running" ]:
return True, state
else:
return False, state
def pidlist_of(self, pid):
try: pid = int(pid)
except: return []
Expand Down Expand Up @@ -4401,7 +4430,7 @@ def logg_debug(*msg): pass
args = [ "version" ]
if not args:
if _init:
args = [ "init" ] # alias "--init default"
args = [ "default" ]
else:
args = [ "list-units" ]
logg.debug("======= systemctl.py " + " ".join(args))
Expand Down
35 changes: 32 additions & 3 deletions files/docker/systemctl3.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ def __init__(self):
self._file_for_unit_sysd = None # name.service => /etc/systemd/system/name.service
self._preset_file_list = None # /etc/systemd/system-preset/* => file content
self._default_target = _default_target
self._sysinit_target = None
self.exit_when_no_more_procs = EXIT_WHEN_NO_MORE_PROCS or False
self.exit_when_no_more_services = EXIT_WHEN_NO_MORE_SERVICES or False
self._user_mode = _user_mode
Expand Down Expand Up @@ -1014,13 +1015,13 @@ def load_unit_conf(self, module): # -> conf | None(not-found)
except Exception as e:
logg.warning("%s not loaded: %s", module, e)
return None
def default_unit_conf(self, module): # -> conf
def default_unit_conf(self, module, description = None): # -> conf
""" a unit conf that can be printed to the user where
attributes are empty and loaded() is False """
data = UnitConfParser()
data.set("Unit","Id", module)
data.set("Unit", "Names", module)
data.set("Unit", "Description", "NOT-FOUND "+module)
data.set("Unit", "Description", description or ("NOT-FOUND "+module))
# assert(not data.loaded())
return SystemctlConf(data, module)
def get_unit_conf(self, module): # -> conf (conf | default-conf)
Expand Down Expand Up @@ -3832,13 +3833,15 @@ def system_default(self, arg = True):
a system_halt is performed with the enabled services to be stopped."""
logg.info("system default requested - %s", arg)
init = self._now or self._init
self.sysinit_status(SubState = "initializing")
self.start_system_default(init = init)
def start_system_default(self, init = False):
""" detect the default.target services and start them.
When --init is given then the init-loop is run and
the services are stopped again by 'systemctl halt'."""
default_target = self._default_target
default_services = self.system_default_services("S", default_target)
self.sysinit_status(SubState = "starting")
self.start_units(default_services)
logg.info(" -- system is up")
if init:
Expand All @@ -3852,6 +3855,7 @@ def stop_system_default(self):
at the end of a 'systemctl --init default' loop."""
default_target = self._default_target
default_services = self.system_default_services("K", default_target)
self.sysinit_status(SubState = "stopping")
self.stop_units(default_services)
logg.info(" -- system is down")
def system_halt(self, arg = True):
Expand Down Expand Up @@ -3994,6 +3998,7 @@ def init_loop_until_stop(self, units):
signal.signal(signal.SIGINT, lambda signum, frame: ignore_signals_and_raise_keyboard_interrupt("SIGINT"))
signal.signal(signal.SIGTERM, lambda signum, frame: ignore_signals_and_raise_keyboard_interrupt("SIGTERM"))
self.start_log_files(units)
self.sysinit_status(ActiveState = "active", SubState = "running")
result = None
while True:
try:
Expand Down Expand Up @@ -4026,6 +4031,7 @@ def init_loop_until_stop(self, units):
signal.signal(signal.SIGINT, signal.SIG_DFL)
logg.info("interrupted - exit init-loop")
result = e.message or "STOPPED"
self.sysinit_status(ActiveState = None, SubState = "degraded")
self.read_log_files(units)
self.read_log_files(units)
self.stop_log_files(units)
Expand Down Expand Up @@ -4062,6 +4068,29 @@ def system_reap_zombies(self):
if pid > 1:
running += 1
return running # except PID 0 and PID 1
def sysinit_status(self, **status):
conf = self.sysinit_target()
self.write_status_from(conf, **status)
def sysinit_target(self):
if not self._sysinit_target:
self._sysinit_target = self.default_unit_conf("sysinit.target", "System Initialization")
return self._sysinit_target
def is_system_running(self):
conf = self.sysinit_target()
status_file = self.status_file_from(conf)
if not os.path.isfile(status_file):
return "offline"
status = self.read_status_from(conf)
return status.get("SubState", "unknown")
def system_is_system_running(self):
state = self.is_system_running()
if self._quiet:
return state in [ "running" ]
else:
if state in [ "running" ]:
return True, state
else:
return False, state
def pidlist_of(self, pid):
try: pid = int(pid)
except: return []
Expand Down Expand Up @@ -4401,7 +4430,7 @@ def logg_debug(*msg): pass
args = [ "version" ]
if not args:
if _init:
args = [ "init" ] # alias "--init default"
args = [ "default" ]
else:
args = [ "list-units" ]
logg.debug("======= systemctl.py " + " ".join(args))
Expand Down
2 changes: 2 additions & 0 deletions testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -15506,6 +15506,8 @@ def usermode_keeps_running(self, system, testname, testdir):
#
cmd = "docker exec {testname} {systemctl} enable zzz.service -vv"
sh____(cmd.format(**locals()))
cmd = "docker exec {testname} {systemctl} is-system-running -vv"
sx____(cmd.format(**locals()))
cmd = "docker exec {testname} {systemctl} is-active zzz.service -vv"
out, err, end = output3(cmd.format(**locals()))
logg.info(" %s =>%s \n%s\n%s", cmd, end, err, out)
Expand Down

0 comments on commit f4e2cf5

Please sign in to comment.