From 96d826b5821c1d24e02579d71aa71e30f57090b2 Mon Sep 17 00:00:00 2001 From: Petro Bratash Date: Thu, 31 Mar 2022 15:56:32 -0700 Subject: [PATCH] [BFN] Fix exception when fwutil run without sudo --- .../sonic_platform/chassis.py | 10 +++++++++ .../sonic_platform/component.py | 8 +++++-- .../sonic_platform/eeprom.py | 20 ++++------------- .../sonic_platform/platform_utils.py | 22 +++++++++++++++++++ 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py index 64536abbb532..6d0e8b8c120c 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py @@ -1,8 +1,12 @@ #!/usr/bin/env python try: + import os import time import syslog + import logging + import logging.config + import yaml from sonic_platform_base.chassis_base import ChassisBase from sonic_platform.sfp import Sfp @@ -10,6 +14,7 @@ from sonic_platform.fan_drawer import fan_drawer_list_get from sonic_platform.thermal import thermal_list_get from eeprom import Eeprom + from platform_utils import file_create from sonic_platform.platform_thrift_client import pltfm_mgr_ready from sonic_platform.platform_thrift_client import thrift_try @@ -47,6 +52,11 @@ def __init__(self): self.qsfp_interval = self.QSFP_CHECK_INTERVAL self.__initialize_components() + with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f: + config_dict = yaml.load(f, yaml.SafeLoader) + file_create(config_dict['handlers']['file']['filename'], '646') + logging.config.dictConfig(config_dict) + @property def _eeprom(self): if self.__eeprom is None: diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py index 5e72bb63e343..47a0993bf3e5 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py @@ -1,4 +1,5 @@ try: + import os import subprocess from sonic_platform_base.component_base import ComponentBase from platform_thrift_client import thrift_try @@ -16,9 +17,12 @@ def get_bios_version(): A string containing the firmware version of the BIOS """ try: - return subprocess.check_output(['dmidecode', '-s', 'bios-version']).strip().decode() + cmd = ['dmidecode', '-s', 'bios-version'] + if os.geteuid() != 0: + cmd.insert(0, 'sudo') + return subprocess.check_output(cmd).strip().decode() except subprocess.CalledProcessError as e: - raise RuntimeError("Failed to getget BIOS version") + raise RuntimeError("Failed to get BIOS version") def get_bmc_version(): """ diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/eeprom.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/eeprom.py index ec7112b3c093..2335c02863d9 100644 --- a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/eeprom.py +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/eeprom.py @@ -1,11 +1,7 @@ try: import os import sys - import errno import datetime - import logging - import logging.config - import yaml import re sys.path.append(os.path.dirname(__file__)) @@ -17,6 +13,7 @@ from sonic_platform_base.sonic_eeprom import eeprom_base from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo + from platform_utils import file_create from platform_thrift_client import thrift_try except ImportError as e: @@ -45,18 +42,8 @@ class Eeprom(eeprom_tlvinfo.TlvInfoDecoder): def __init__(self): - with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f: - config_dict = yaml.load(f, yaml.SafeLoader) - logging.config.dictConfig(config_dict) - - if not os.path.exists(os.path.dirname(_EEPROM_SYMLINK)): - try: - os.makedirs(os.path.dirname(_EEPROM_SYMLINK)) - except OSError as e: - if e.errno != errno.EEXIST: - raise - - open(_EEPROM_SYMLINK, 'a').close() + file_create(_EEPROM_SYMLINK, '646') + file_create(_EEPROM_STATUS, '646') with open(_EEPROM_STATUS, 'w') as f: f.write("initializing..") @@ -152,3 +139,4 @@ def modelstr(self): def revision_str(self): return self.__tlv_get(self._TLV_CODE_LABEL_REVISION) + diff --git a/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py new file mode 100644 index 000000000000..81e78ee01041 --- /dev/null +++ b/platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_utils.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +try: + import os + import subprocess + +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + +def file_create(path, mode=None): + def run_cmd(cmd): + if os.geteuid() != 0: + cmd.insert(0, 'sudo') + subprocess.check_output(cmd) + + file_path = os.path.dirname(path) + if not os.path.exists(file_path): + run_cmd(['mkdir', '-p', file_path]) + if not os.path.isfile(path): + run_cmd(['touch', path]) + if (mode is not None): + run_cmd(['chmod', mode, path])