From b02b00a356cf740772e88de835f15e15394a5ed4 Mon Sep 17 00:00:00 2001 From: Rafael Leira Date: Thu, 27 Apr 2023 17:51:48 +0200 Subject: [PATCH] Partial fix for Init a failed controller #13 --- pystorcli2/controller/__init__.py | 2 +- pystorcli2/storcli.py | 25 ++++++++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/pystorcli2/controller/__init__.py b/pystorcli2/controller/__init__.py index f184bc9..6d61ac4 100644 --- a/pystorcli2/controller/__init__.py +++ b/pystorcli2/controller/__init__.py @@ -443,7 +443,7 @@ def __init__(self, binary='storcli64'): @ property def _ctl_ids(self) -> List[str]: - out = self._storcli.run(['show']) + out = self._storcli.run(['show'], allow_error_codes=[59]) response = common.response_data(out) if "Number of Controllers" in response and response["Number of Controllers"] == 0: diff --git a/pystorcli2/storcli.py b/pystorcli2/storcli.py index 7bca6cd..7027c28 100644 --- a/pystorcli2/storcli.py +++ b/pystorcli2/storcli.py @@ -14,7 +14,7 @@ import threading import subprocess import pystorcli2 -from typing import Optional, Dict, Any +from typing import Optional, Dict, Any, List from . import common from . import exc @@ -128,25 +128,38 @@ def cache(self, value): self.__response_cache = value @staticmethod - def check_response_status(cmd, out): + def check_response_status(cmd: List[str], out: Dict[str, Dict[int, Dict[str, Any]]], allow_error_codes: List[int]) -> bool: """Check ouput command line status from storcli. Args: cmd (list of str): full command line out (dict): output from command line + raise_on_error (bool): raise exception on error (default: True) + + Returns: + bool: True if no error found in output. False if error found but allowed. Raise exception otherwise. Raises: - StorCliCmdError + StorCliCmdError: if error found in output and not allowed """ cmd_status = common.response_cmd(out) if cmd_status['Status'] == 'Failure': if 'Detailed Status' in cmd_status: + # Check if the error code is allowed + for error in cmd_status['Detailed Status']: + if 'ErrCd' in error: + if error['ErrCd'] in allow_error_codes: + return False + + # Otherwise, raise an exception raise exc.StorCliCmdError( cmd, "{0}".format(cmd_status['Detailed Status'])) else: raise exc.StorCliCmdError(cmd, "{0}".format(cmd_status)) - def run(self, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs): + return True + + def run(self, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, allow_error_codes: List[int] = [], **kwargs): """Execute storcli command line with arguments. Run command line and check output for errors. @@ -155,6 +168,7 @@ def run(self, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs): args (list of str): cmd line arguments (without binary) stdout (fd): controll subprocess stdout fd stderr (fd): controll subporcess stderr fd + allow_error_codes (list of int): list of error codes to allow **kwargs: arguments to subprocess run Returns: @@ -181,7 +195,8 @@ def run(self, args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs): args=cmd, stdout=stdout, stderr=stderr, universal_newlines=True, **kwargs) try: ret_json = json.loads(ret.stdout) - self.check_response_status(cmd, ret_json) + self.check_response_status( + cmd, ret_json, allow_error_codes) ret.check_returncode() if self.cache_enable: self.__response_cache[cmd_cache_key] = ret_json