Naudit / pystorcli2

Python library provides wrapper around storcli tool to manage and control LSI MegaRAID controllers.
BSD 3-Clause "New" or "Revised" License
2 stars 7 forks source link

Storcli runner: some commands do not have json format #8

Closed ulmitov closed 1 year ago

ulmitov commented 1 year ago

Hi, there is an exception when running some commands, for example:

In [4]: ctrl._run(['show', 'events', 'file=/root/raid_events.log'])
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File ~/Desktop/venvs/dev/lib/python3.9/site-packages/pystorcli2/storcli.py:189, in StorCLI.run(self, args, stdout, stderr, **kwargs)
    188 try:
--> 189     ret_json = json.loads(ret.stdout)
    190     self.check_response_status(cmd, ret_json)

File /usr/lib/python3.9/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File /usr/lib/python3.9/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File /usr/lib/python3.9/json/decoder.py:355, in JSONDecoder.raw_decode(self, s, idx)
    354 except StopIteration as err:
--> 355     raise JSONDecodeError("Expecting value", s, err.value) from None
    356 return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[4], line 1
----> 1 a._run(['show', 'events', f'file=/root/raid_events.log'])

File ~/Desktop/venvs/dev/lib/python3.9/site-packages/pystorcli2/controller.py:228, in Controller._run(self, args, **kwargs)
    226 args = args[:]
    227 args.insert(0, self._name)
--> 228 return self._storcli.run(args, **kwargs)

File ~/Desktop/venvs/dev/lib/python3.9/site-packages/pystorcli2/storcli.py:197, in StorCLI.run(self, args, stdout, stderr, **kwargs)
    194         return ret_json
    195     except json.JSONDecodeError:
    196         # :/
--> 197         err = re.search('(^.*)Storage.*Command.*$',
    198                         ret.stdout, re.MULTILINE | re.DOTALL).group(1)
    199         raise exc.StorCliCmdError(cmd, err)
    200 except subprocess.TimeoutExpired as err:

AttributeError: 'NoneType' object has no attribute 'group'

This command does not have a json format:

root@ubuntu20-cuda:~# storcli64 /c0 show events file=/root/raid_events.log J
CLI Version = 007.2309.0000.0000 Sep 16, 2022
Operating system = Linux 5.4.0-99-generic
Controller = 0
Status = Success
Description = None

Events = GETEVENTS

Controller Properties :
=====================

------------------------------------
Ctrl Status  Method          Value  
------------------------------------
   0 Success handleSuboption Events 
------------------------------------
ralequi commented 1 year ago

Don't sure what to say.

It's not a "supported method" so it may be "ok" to fail, but may be other calls that may fail too in the future. Let's see what we can do about this....

Thanks for your report!

ulmitov commented 1 year ago

Ok, btw the develop branch has a conflict in package names: import from pystorcli2 is working, but not from pystorcli

In [1]: from pystorcli.common import response_data
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from pystorcli.common import response_data

ModuleNotFoundError: No module named 'pystorcli.common'

In [2]: import pystorcli

In [3]: import pystorcli.common
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 1
----> 1 import pystorcli.common

ModuleNotFoundError: No module named 'pystorcli.common'

In [4]: import pystorcli2.common

In [5]:                               
ralequi commented 1 year ago

Ok, btw the develop branch has a conflict in package names: import from pystorcli2 is working, but not from pystorcli

In [1]: from pystorcli.common import response_data
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 from pystorcli.common import response_data

ModuleNotFoundError: No module named 'pystorcli.common'

In [2]: import pystorcli

In [3]: import pystorcli.common
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[3], line 1
----> 1 import pystorcli.common

ModuleNotFoundError: No module named 'pystorcli.common'

In [4]: import pystorcli2.common

In [5]:                               

Try the latest develop branch

ralequi commented 1 year ago

Hi @ulmitov try the latest commit on develop. The custom non-json commands should have now a better handling

ulmitov commented 1 year ago

Both fixed, thanks!

>>> import pystorcli
>>> pystorcli.Controllers()
<pystorcli.controller.Controllers object at 0x7feec62f4f40>
>>> c=pystorcli.Controllers()
>>> c
<pystorcli.controller.Controllers object at 0x7feec635a880>
>>> c.ids
[0]
>>> c=c.get_ctl(0)
>>> c.name
'/c0'
>>> c._run(['show', 'events', 'file=/root/raid_events111.log'])
{'CLI Version': '007.2309.0000.0000 Sep 16, 2022', 'Operating system': 'Linux 5.4.0-99-generic', 'Controller': '0', 'Status': 'Success', 'Description': 'None', 'Events': 'GETEVENTS', '': '===================='}
ulmitov commented 1 year ago

But this output was missed, not critical though

------------------------------------
Ctrl Status  Method          Value  
------------------------------------
   0 Success handleSuboption Events 
------------------------------------
ralequi commented 1 year ago

But this output was missed, not critical though

------------------------------------
Ctrl Status  Method          Value  
------------------------------------
   0 Success handleSuboption Events 
------------------------------------

I tried but was harder than I think without much "hardcoding"