Skip to content

Commit 96d826b

Browse files
committed
[BFN] Fix exception when fwutil run without sudo
1 parent 1b71f45 commit 96d826b

4 files changed

Lines changed: 42 additions & 18 deletions

File tree

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/chassis.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
#!/usr/bin/env python
22

33
try:
4+
import os
45
import time
56
import syslog
7+
import logging
8+
import logging.config
9+
import yaml
610

711
from sonic_platform_base.chassis_base import ChassisBase
812
from sonic_platform.sfp import Sfp
913
from sonic_platform.psu import psu_list_get
1014
from sonic_platform.fan_drawer import fan_drawer_list_get
1115
from sonic_platform.thermal import thermal_list_get
1216
from eeprom import Eeprom
17+
from platform_utils import file_create
1318

1419
from sonic_platform.platform_thrift_client import pltfm_mgr_ready
1520
from sonic_platform.platform_thrift_client import thrift_try
@@ -47,6 +52,11 @@ def __init__(self):
4752
self.qsfp_interval = self.QSFP_CHECK_INTERVAL
4853
self.__initialize_components()
4954

55+
with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f:
56+
config_dict = yaml.load(f, yaml.SafeLoader)
57+
file_create(config_dict['handlers']['file']['filename'], '646')
58+
logging.config.dictConfig(config_dict)
59+
5060
@property
5161
def _eeprom(self):
5262
if self.__eeprom is None:

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/component.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
try:
2+
import os
23
import subprocess
34
from sonic_platform_base.component_base import ComponentBase
45
from platform_thrift_client import thrift_try
@@ -16,9 +17,12 @@ def get_bios_version():
1617
A string containing the firmware version of the BIOS
1718
"""
1819
try:
19-
return subprocess.check_output(['dmidecode', '-s', 'bios-version']).strip().decode()
20+
cmd = ['dmidecode', '-s', 'bios-version']
21+
if os.geteuid() != 0:
22+
cmd.insert(0, 'sudo')
23+
return subprocess.check_output(cmd).strip().decode()
2024
except subprocess.CalledProcessError as e:
21-
raise RuntimeError("Failed to getget BIOS version")
25+
raise RuntimeError("Failed to get BIOS version")
2226

2327
def get_bmc_version():
2428
"""

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/eeprom.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
try:
22
import os
33
import sys
4-
import errno
54
import datetime
6-
import logging
7-
import logging.config
8-
import yaml
95
import re
106

117
sys.path.append(os.path.dirname(__file__))
@@ -17,6 +13,7 @@
1713

1814
from sonic_platform_base.sonic_eeprom import eeprom_base
1915
from sonic_platform_base.sonic_eeprom import eeprom_tlvinfo
16+
from platform_utils import file_create
2017

2118
from platform_thrift_client import thrift_try
2219
except ImportError as e:
@@ -45,18 +42,8 @@
4542

4643
class Eeprom(eeprom_tlvinfo.TlvInfoDecoder):
4744
def __init__(self):
48-
with open(os.path.dirname(__file__) + "/logging.conf", 'r') as f:
49-
config_dict = yaml.load(f, yaml.SafeLoader)
50-
logging.config.dictConfig(config_dict)
51-
52-
if not os.path.exists(os.path.dirname(_EEPROM_SYMLINK)):
53-
try:
54-
os.makedirs(os.path.dirname(_EEPROM_SYMLINK))
55-
except OSError as e:
56-
if e.errno != errno.EEXIST:
57-
raise
58-
59-
open(_EEPROM_SYMLINK, 'a').close()
45+
file_create(_EEPROM_SYMLINK, '646')
46+
file_create(_EEPROM_STATUS, '646')
6047
with open(_EEPROM_STATUS, 'w') as f:
6148
f.write("initializing..")
6249

@@ -152,3 +139,4 @@ def modelstr(self):
152139

153140
def revision_str(self):
154141
return self.__tlv_get(self._TLV_CODE_LABEL_REVISION)
142+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
3+
try:
4+
import os
5+
import subprocess
6+
7+
except ImportError as e:
8+
raise ImportError(str(e) + "- required module not found")
9+
10+
def file_create(path, mode=None):
11+
def run_cmd(cmd):
12+
if os.geteuid() != 0:
13+
cmd.insert(0, 'sudo')
14+
subprocess.check_output(cmd)
15+
16+
file_path = os.path.dirname(path)
17+
if not os.path.exists(file_path):
18+
run_cmd(['mkdir', '-p', file_path])
19+
if not os.path.isfile(path):
20+
run_cmd(['touch', path])
21+
if (mode is not None):
22+
run_cmd(['chmod', mode, path])

0 commit comments

Comments
 (0)