statd: Move operational from confd to statd (yanger)

This commit is contained in:
Mattias Walström
2025-04-23 13:45:48 +02:00
parent a4def1379c
commit c6b44db18a
9 changed files with 97 additions and 169 deletions
+84 -14
View File
@@ -1,7 +1,7 @@
import subprocess
import ipaddress
from .common import insert
from .common import insert,YangDate
from .host import HOST
def uboot_get_boot_order():
@@ -157,6 +157,24 @@ def add_software_slots(out, data):
slots.append(new)
out["slot"] = slots
def add_platform(out):
platform = {}
pmap = {
"NAME": "os-name",
"VERSION_ID": "os-version",
"BUILD_ID": "os-release",
"ARCHITECTURE": "machine"
}
os_release = HOST.read("/etc/os-release")
for line in os_release.splitlines():
key, value = line.split('=')
name = pmap.get(key)
if name:
platform[name] = value.strip("\"")
insert(out, "platform", platform)
def add_software(out):
software = {}
try:
@@ -171,31 +189,83 @@ def add_software(out):
except subprocess.CalledProcessError:
pass # Maybe an upgrade i progress, then rauc does not respond
installer_status = HOST.run_json(["rauc-installation-status"])
installer = {}
if installer_status.get("operation"):
installer["operation"] = installer_status["operation"]
if "progress" in installer_status:
progress = {}
except FileNotFoundError:
pass
if installer_status["progress"].get("percentage"):
progress["percentage"] = int(installer_status["progress"]["percentage"])
if installer_status["progress"].get("message"):
progress["message"] = installer_status["progress"]["message"]
installer["progress"] = progress
software["installer"] = installer
installer = {}
try:
installer_status = HOST.run_json(["rauc-installation-status"])
if installer_status.get("operation"):
installer["operation"] = installer_status["operation"]
if "progress" in installer_status:
progress = {}
if installer_status["progress"].get("percentage"):
progress["percentage"] = int(installer_status["progress"]["percentage"])
if installer_status["progress"].get("message"):
progress["message"] = installer_status["progress"]["message"]
installer["progress"] = progress
software["installer"] = installer
except FileNotFoundError:
pass
insert(out, "infix-system:software", software)
def add_hostname(out):
hostname = HOST.run(tuple(["hostname"]))
out["hostname"] = hostname.strip()
def add_users(out):
shadow_output = HOST.run_multiline(["getent", "shadow"], [])
users = []
for line in shadow_output:
parts = line.split(':')
if len(parts) < 2:
continue
username = parts[0]
password_hash = parts[1]
# Skip any records that do not pass YANG validation
if (not password_hash or
password_hash.startswith('0') or
password_hash.startswith('*') or
password_hash.startswith('!')):
continue
user = {}
user["name"] = username
user["password"] = password_hash
users.append(user)
insert(out, "authentication", "user", users)
def add_clock(out):
clock = {}
clock_now=YangDate()
uptime=HOST.read("/proc/uptime")
uptime = float(uptime.split()[0])
clock["boot-datetime"] = str(clock_now.from_seconds(uptime))
clock["current-datetime"] = str(clock_now)
insert(out, "clock", clock)
def operational():
out = {
"ietf-system:system": {
},
"ietf-system:system-state": {
}
}
out_state = out["ietf-system:system-state"]
out_system = out["ietf-system:system"]
add_hostname(out_system)
add_users(out_system)
add_software(out_state)
add_ntp(out_state)
add_dns(out_state)
add_clock(out_state)
add_platform(out_state)
return out