mirror of
https://github.com/kernelkit/infix.git
synced 2026-08-03 06:13:02 +02:00
Add 'show hardware' only show USB port status for now
This commit is contained in:
@@ -27,6 +27,12 @@ class PadSoftware:
|
||||
state = 10
|
||||
version = 23
|
||||
|
||||
|
||||
class PadUsbPort:
|
||||
title = 30
|
||||
name = 20
|
||||
state = 10
|
||||
|
||||
class Decore():
|
||||
@staticmethod
|
||||
def decorate(sgr, txt, restore="0"):
|
||||
@@ -131,6 +137,18 @@ class Software:
|
||||
print(f"SHA-256 : {self.hash}")
|
||||
print(f"Installed : {self.date}")
|
||||
|
||||
class USBport:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
self.name = data.get('name', '')
|
||||
self.state = get_json_data('', self.data, 'state', 'admin-state')
|
||||
|
||||
def print(self):
|
||||
#print(self.name)
|
||||
row = f"{self.name:<{PadUsbPort.name}}"
|
||||
row += f"{self.state:<{PadUsbPort.state}}"
|
||||
print(row)
|
||||
|
||||
class Iface:
|
||||
def __init__(self, data):
|
||||
self.data = data
|
||||
@@ -468,6 +486,24 @@ def infix_system(json, name):
|
||||
if slot.is_rootfs():
|
||||
slot.print()
|
||||
|
||||
def ietf_hardware(json):
|
||||
if not json.get("ietf-hardware:hardware"):
|
||||
print(f"Error, top level \"ietf-hardware:component\" missing")
|
||||
sys.exit(1)
|
||||
|
||||
hdr = (f"{'USB PORTS':<{PadUsbPort.title}}")
|
||||
print(Decore.invert(hdr))
|
||||
hdr = (f"{'NAME':<{PadUsbPort.name}}"
|
||||
f"{'STATE':<{PadUsbPort.state}}")
|
||||
print(Decore.invert(hdr))
|
||||
|
||||
components = get_json_data({}, json, "ietf-hardware:hardware", "component")
|
||||
|
||||
for component in components:
|
||||
if component.get("class") == "infix-hardware:usb":
|
||||
port = USBport(component)
|
||||
port.print()
|
||||
|
||||
def main():
|
||||
try:
|
||||
json_data = json.load(sys.stdin)
|
||||
@@ -484,6 +520,8 @@ def main():
|
||||
sys.exit(ietf_routing(json_data, args.name))
|
||||
if args.module == "infix-system":
|
||||
sys.exit(infix_system(json_data, args.name))
|
||||
if args.module == "ietf-hardware":
|
||||
sys.exit(ietf_hardware(json_data))
|
||||
else:
|
||||
print(f"Error, unknown module {args.module}")
|
||||
sys.exit(1)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import subprocess
|
||||
import json
|
||||
import sys # (built-in module)
|
||||
import os
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
|
||||
@@ -155,13 +156,43 @@ def get_vpd_data(vpd):
|
||||
component["infix-hardware:vpd-data"]["infix-hardware:vendor-extension"] = vendor_extensions
|
||||
return component
|
||||
|
||||
def get_usb_ports(usb_ports):
|
||||
ports=[]
|
||||
names=[]
|
||||
for usb_port in usb_ports:
|
||||
port={}
|
||||
if usb_port.get("path"):
|
||||
if usb_port["name"] in names:
|
||||
continue
|
||||
|
||||
path = usb_port["path"]
|
||||
if os.path.exists(path):
|
||||
names.append(usb_port["name"])
|
||||
with open(path, "r") as f:
|
||||
data=int(f.readline().strip())
|
||||
enabled="unlocked" if data == 1 else "locked"
|
||||
port["state"] = {}
|
||||
port["state"]["admin-state"] = enabled
|
||||
port["name"] = usb_port["name"]
|
||||
port["class"] = "infix-hardware:usb"
|
||||
port["state"]["oper-state"] = "enabled"
|
||||
ports.append(port)
|
||||
|
||||
return ports
|
||||
|
||||
|
||||
def add_hardware(hw_out, test):
|
||||
cmd = ['cat', "/run/system.json"]
|
||||
if test:
|
||||
cmd = ['cat', f"{test}/system.json"]
|
||||
else:
|
||||
cmd = ['cat', "/run/system.json"]
|
||||
data = run_json_cmd(cmd)
|
||||
components=[]
|
||||
for _,vpd in data.get("vpd").items():
|
||||
component=get_vpd_data(vpd)
|
||||
components.append(component)
|
||||
if(data.get("usb-ports", None)):
|
||||
components.extend(get_usb_ports(data["usb-ports"]))
|
||||
insert(hw_out, "component", components)
|
||||
|
||||
def get_routes(routes, proto, data):
|
||||
|
||||
@@ -326,6 +326,12 @@
|
||||
/libexec/infix/cli-pretty "ietf-routing" -n "$KLISH_PARAM_name"
|
||||
</ACTION>
|
||||
</COMMAND>
|
||||
<COMMAND name="hardware" help="Show hardware information">
|
||||
<ACTION sym="script">
|
||||
sysrepocfg -f json -X -d operational -x "/ietf-hardware:hardware" | \
|
||||
/libexec/infix/cli-pretty "ietf-hardware"
|
||||
</ACTION>
|
||||
</COMMAND>
|
||||
<!-- https://www.cisco.com/c/en/us/td/docs/wireless/access_point/mob_exp/83/cmd-ref/me_cr_book/me_ports_and_interfaces_cli.html -->
|
||||
<COMMAND name="interfaces" help="Show interface info">
|
||||
<SWITCH name="optional" min="0" max="1">
|
||||
|
||||
Reference in New Issue
Block a user