Skip to content

Commit

Permalink
[BFN] Fixed SONiC fwutil exec time (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: Taras Keryk <tarasx.keryk@intel.com>
  • Loading branch information
Taras Keryk authored and Andriy Kokhan committed Sep 11, 2022
1 parent 3b2e947 commit 4341a4a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import json
from collections import OrderedDict
from sonic_py_common import device_info
from platform_utils import limit_execution_time

except ImportError as e:
raise ImportError(str(e) + "- required module not found")
Expand All @@ -24,6 +25,7 @@ def get_bios_version():
except subprocess.CalledProcessError as e:
raise RuntimeError("Failed to get BIOS version")

@limit_execution_time(1)
def get_bmc_version():
"""
Retrieves the firmware version of the BMC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
raise ImportError(str(e) + "- required module not found")

def file_create(path, mode=None):
"""
Ensure that file is created with the appropriate permissions
Args:
path: full path of a file
mode: file permission in octal representation
"""
def run_cmd(cmd):
if os.geteuid() != 0:
cmd.insert(0, 'sudo')
Expand All @@ -20,7 +26,7 @@ def run_cmd(cmd):
run_cmd(['mkdir', '-p', file_path])
if not os.path.isfile(path):
run_cmd(['touch', path])
if (mode is not None):
if (mode is not None):
run_cmd(['chmod', mode, path])

def cancel_on_sigterm(func):
Expand All @@ -42,4 +48,33 @@ def handler(sig, frame):
finally:
signal.signal(signal.SIGTERM, sigterm_handler)
return result
return wrapper
return wrapper

def limit_execution_time(execution_time_secs: int):
"""
Wrapper for a function whose execution time must be limited
Args:
execution_time_secs: maximum execution time in seconds,
after which the function execution will be stopped
"""
def wrapper(func):
@wraps(func)
def execution_func(*args, **kwargs):
def handler(sig, frame):
if sigalrm_handler:
sigalrm_handler(sig, frame)
raise Exception("Canceling {}() execution...".format(func.__name__))

sigalrm_handler = signal.getsignal(signal.SIGALRM)
signal.signal(signal.SIGALRM, handler)
signal.alarm(execution_time_secs)
result = None
try:
result = func(*args, **kwargs)
finally:
signal.alarm(0)

return result
return execution_func
return wrapper

0 comments on commit 4341a4a

Please sign in to comment.