summaryrefslogtreecommitdiff
path: root/ncserver/module/openrc.py
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-09-29 18:34:04 -0500
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-09-29 18:34:04 -0500
commit8b92336a355a70cca6a665fead1149a17ccdd5ec (patch)
treec591cab077a46773dd9857cc6093b863b625498c /ncserver/module/openrc.py
parentf5c4f5a8ecd16e7aaabe70934f6a7998e9dcd48e (diff)
downloadnetconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.gz
netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.bz2
netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.tar.xz
netconfapk-8b92336a355a70cca6a665fead1149a17ccdd5ec.zip
openrc: Split state to container; add start-time code
Diffstat (limited to 'ncserver/module/openrc.py')
-rw-r--r--ncserver/module/openrc.py23
1 files changed, 16 insertions, 7 deletions
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):