diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2020-09-29 18:34:04 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2020-09-29 18:34:04 -0500 |
commit | 8b92336a355a70cca6a665fead1149a17ccdd5ec (patch) | |
tree | c591cab077a46773dd9857cc6093b863b625498c | |
parent | f5c4f5a8ecd16e7aaabe70934f6a7998e9dcd48e (diff) | |
download | netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.gz netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.bz2 netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.xz netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.zip |
openrc: Split state to container; add start-time code
-rw-r--r-- | doc/service.rst | 10 | ||||
-rw-r--r-- | ncserver/module/openrc.py | 23 | ||||
-rw-r--r-- | yang-modules/adelie-services.yang | 50 |
3 files changed, 59 insertions, 24 deletions
diff --git a/doc/service.rst b/doc/service.rst index 162b168..b64870e 100644 --- a/doc/service.rst +++ b/doc/service.rst @@ -231,13 +231,17 @@ The data model for system services has the following structure: :: +--rw services - +--rw service* [name] - +--rw name string + | +--rw service* [name] + | +--rw name string + | +--rw enabled boolean + +--ro service-status + +--ro service* [name] + +--ro name string +--ro description? string - +--rw enabled boolean +--ro status service-status +--ro start-time? yang:date-and-time + Module ------ diff --git a/ncserver/module/openrc.py b/ncserver/module/openrc.py index 60f6c7f..5503002 100644 --- a/ncserver/module/openrc.py +++ b/ncserver/module/openrc.py @@ -20,7 +20,7 @@ from lxml import etree from netconf import error, util from ncserver.base.service import Service, ServiceStatus -from ncserver.base.util import _ +from ncserver.base.util import _, yang_dt_for_timestamp QName = etree.QName # pylint: disable=I1101 @@ -89,11 +89,11 @@ def get_var(shell_like, var_name: str, default="") -> str: :param str default: The default value to return if the variable is not set. """ - script = ". /etc/init.d/functions.sh; ( . {shlike} && printf %s \"${var}\" )" + script = ". /etc/init.d/functions.sh;(. {shlike} && printf %s \"${var}\")" script = script.format(shlike=shell_like, var=var_name) - proc = subprocess.run(['/bin/sh', '-e', '-c', script], - stdout=subprocess.PIPE, check=True, env={'SHDIR':'/etc/init.d'}, - stderr=subprocess.PIPE + proc = subprocess.run( + ['/bin/sh', '-e', '-c', script], check=True, + stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) value = proc.stdout.decode('utf-8') if value == "": @@ -143,10 +143,13 @@ class OpenRCService(Service): self._name = name self._description = get_var(svcpath, "description", name) self._enabled = is_enabled(name) + # Update status and start-time. + self.status() def status(self): """Retrieve the service's status.""" stat = ServiceStatus.Stopped + path = None status_dirs = { ServiceStatus.Starting: "/run/openrc/starting", @@ -158,6 +161,7 @@ class OpenRCService(Service): for service in pathlib.Path(directory).iterdir(): if pathlib.Path(service).resolve().name == self.name: stat = value + path = service break if stat == ServiceStatus.Running: @@ -169,6 +173,8 @@ class OpenRCService(Service): dpid = get_var(daemon, 'pidfile') if dpid and not check_alive(dpid): return ServiceStatus.Crashed + # Update start-time + self._start_time = pathlib.Path(path).lstat().st_mtime return stat @@ -243,16 +249,19 @@ def running(node): def operational(node): """Retrieve the service state for this device.""" - svcs = util.subelm(node, 'svcs:services') + svcs = util.subelm(node, 'svcs:service-status') for service in service_list(): svcnode = util.subelm(svcs, 'svcs:service') svcnode.append(util.leaf_elm('svcs:name', service.name)) svcnode.append(util.leaf_elm('svcs:description', service.description)) - svcnode.append(util.leaf_elm('svcs:enabled', service.enabled)) svcnode.append(util.leaf_elm( 'svcs:status', service.status().name.lower() )) + if service.status() == ServiceStatus.Running: + svcnode.append(util.leaf_elm( + 'svcs:start-time', yang_dt_for_timestamp(service.start_time) + )) def edit(rpc, node, def_op): diff --git a/yang-modules/adelie-services.yang b/yang-modules/adelie-services.yang index 39a2fae..f006845 100644 --- a/yang-modules/adelie-services.yang +++ b/yang-modules/adelie-services.yang @@ -36,6 +36,11 @@ module adelie-services { This version of this YANG module is part of the NETCONF APK 1.0 distribution; see the distributon for full legal notices."; + revision 2020-09-29 { + description + "Separate state into its own container."; + } + revision 2020-09-22 { description "Initial revision."; @@ -89,17 +94,6 @@ module adelie-services { Changing the name of the service may have surprising consequences. It is not recommended."; } - - leaf description { - type string; - config false; - description - "A textual description of the service. - - Depending on the service manager in use on the device, - this node may be empty or contain only the name of the - service."; - } leaf enabled { type boolean; @@ -110,10 +104,39 @@ module adelie-services { When this leaf is true, the service will be started automatically by the device during system bootup."; } - + } + } + + container service-status { + config false; + description + "Contains information about the state of services on the + device."; + + list service { + key "name"; + + description + "The list of services on the device."; + + leaf name { + type string; + description + "The name of the service."; + } + + leaf description { + type string; + description + "A textual description of the service. + + Depending on the service manager in use on the device, + this node may be empty or contain only the name of the + service."; + } + leaf status { type service-status; - config false; mandatory true; description "The current status of the service."; @@ -121,7 +144,6 @@ module adelie-services { leaf start-time { type yang:date-and-time; - config false; when "./status = 'running'"; description "The time that this service entered the running state."; |