Skip to content

Commit

Permalink
Partial fix for Init a failed controller #13
Browse files Browse the repository at this point in the history
  • Loading branch information
ralequi committed Apr 27, 2023
1 parent b5c841e commit b02b00a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pystorcli2/controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 20 additions & 5 deletions pystorcli2/storcli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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
Expand Down

0 comments on commit b02b00a

Please sign in to comment.