Skip to content

Commit

Permalink
osfv_cli: allow for external models configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Pijanowski <maciej.pijanowski@3mdeb.com>
  • Loading branch information
macpijan committed Sep 20, 2023
1 parent 102ed89 commit 6b51d5d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
8 changes: 6 additions & 2 deletions osfv_cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
CONFIG_FILE = $(HOME)/.osfv/snipeit.yml
SNIPEIT_CONFIG_FILE = $(HOME)/.osfv/snipeit.yml
CLI_CONFIG_FILE = $(HOME)/.osfv/cli.yml
VERSION=$(shell grep '^version =' pyproject.toml | awk -F'\"' '{print $$2}')
PACKAGE_NAME=$(shell grep '^name =' pyproject.toml | awk -F'\"' '{print $$2}')
REPO_ROOT = $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

build:
poetry build

install: build
pip install dist/$(PACKAGE_NAME)-$(VERSION)-py3-none-any.whl
test -f $(CONFIG_FILE) || install -D -m 644 config.yml $(CONFIG_FILE)
test -f $(SNIPEIT_CONFIG_FILE) || install -D -m 644 snipeit.yml $(SNIPEIT_CONFIG_FILE)
test -f $(CLI_CONFIG_FILE) || (install -D -m 644 cli.yml $(CLI_CONFIG_FILE) && sed -e 's|MODELS_DIR|$(REPO_ROOT)/models|' -i $(CLI_CONFIG_FILE))


uninstall:
pip uninstall -y $(PACKAGE_NAME)
Expand Down
2 changes: 2 additions & 0 deletions osfv_cli/cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
models_dir: MODELS_DIR
2 changes: 1 addition & 1 deletion osfv_cli/osfv_cli/osfv_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ def main():
asset_id = snipeit_api.get_asset_id_by_rte_ip(args.rte_ip)
dut_model_name = snipeit_api.get_asset_model_name(asset_id)
print(f"DUT model retrieved from snipeit: {dut_model_name}")
rte = RTE(args.rte_ip, dut_model_name)
rte = RTE(args.rte_ip, dut_model_name, snipeit_api)

if args.rte_cmd == "rel":
# Handle RTE relay related commands
Expand Down
28 changes: 22 additions & 6 deletions osfv_cli/osfv_cli/rte.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import paramiko
import yaml

from . import snipeit_api
from .rtectrl_api import rtectrl
from .sonoff_api import SonoffDevice

Expand All @@ -28,19 +27,36 @@ class RTE(rtectrl):
PROGRAMMER = "linux_spi:dev=/dev/spidev1.0,spispeed=16000"
FLASHROM_CMD = "flashrom -p {programmer} {args}"

def __init__(self, rte_ip, dut_model):
CLI_CONFIG_FILE_PATH = os.path.expanduser("~/.osfv/cli.yml")

def __init__(self, rte_ip, dut_model, snipeit_api):
self.rte_ip = rte_ip
self.dut_model = dut_model
self.dut_data = self.load_model_data()
self.sonoff, self.sonoff_ip = self.init_sonoff()
self.snipeit_api = snipeit_api

def load_model_data(self):
# Specify the file path
file_path = f"models/{self.dut_model}.yml"
try:
with open(self.CLI_CONFIG_FILE_PATH, "r") as file:
config = yaml.safe_load(file)
except FileNotFoundError:
raise FileNotFoundError(f"Configuration file not found")
except yaml.YAMLError as e:
raise ValueError(f"Error parsing YAML: {e}")

if config is None:
raise ValueError(f"Empty configuration file")

print(config)
models_dir = config.get("models_dir")
file_path = os.path.join(models_dir, f"{self.dut_model}.yml")

# Check if the file exists
if not os.path.isfile(file_path):
raise UnsupportedDUTModel("The given model is not yet supported")
raise UnsupportedDUTModel(
f"The {self.dut_model} model is not yet supported"
)

# Load the YAML file
with open(file_path, "r") as file:
Expand All @@ -54,7 +70,7 @@ def init_sonoff(self):
sonoff = None

if self.dut_data["pwr_ctrl"]["sonoff"] is True:
sonoff_ip = snipeit_api.get_sonoff_ip_by_rte_ip(self.rte_ip)
sonoff_ip = self.snipeit_api.get_sonoff_ip_by_rte_ip(self.rte_ip)
if not sonoff_ip:
raise SonoffNotFound(
f"Sonoff IP not found in SnipeIT for RTE: {self.rte_ip}"
Expand Down
1 change: 1 addition & 0 deletions osfv_cli/config.yml → osfv_cli/snipeit.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
api_url: 'http://snipeit/api/v1'
api_token: 'YOUR_PERSONAL_API_TOKEN'
user_id: YOUR_USER_ID

0 comments on commit 6b51d5d

Please sign in to comment.