Skip to content

Commit

Permalink
[BFN] Fix exception when fwutil run without sudo
Browse files Browse the repository at this point in the history
  • Loading branch information
bratashX committed Mar 31, 2022
1 parent 1b71f45 commit 96d826b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#!/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
from sonic_platform.psu import psu_list_get
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
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
try:
import os
import subprocess
from sonic_platform_base.component_base import ComponentBase
from platform_thrift_client import thrift_try
Expand All @@ -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():
"""
Expand Down
Original file line number Diff line number Diff line change
@@ -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__))
Expand All @@ -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:
Expand Down Expand Up @@ -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..")

Expand Down Expand Up @@ -152,3 +139,4 @@ def modelstr(self):

def revision_str(self):
return self.__tlv_get(self._TLV_CODE_LABEL_REVISION)

Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit 96d826b

Please sign in to comment.