Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic example in Readme.rst not working for me (same with Diagnostic Walkthrough instructions) #3971

Open
mulkieran opened this issue Dec 8, 2023 · 2 comments

Comments

@mulkieran
Copy link

Was hoping to use the "Diagnostic Walkthrough" instructions[1] to sample making a local insights rule, but I came a cropper.

I simply opened the Python interpreter and started entering the code. I ran the report and I got the following result

>>> results[report]
{'rule_fqdn': '__main__.report', 'reason': 'MISSING_REQUIREMENTS', 'details': "All: ['insights.parsers.installed_rpms.InstalledRpms'] Any: ", 'type': 'skip'}

As far as I can interpret this, the failure is due to some MISSING REQUIREMENTS. But, since I imported all the things that I was supposed to import, and certainly I imported InstalledRpms, it is hard for me to discover what I did wrong. I can run rpm -qa without difficulty from the command-line, and get a bunch of results.

I see, at the bottom of the Diagnostic Walkthrough, that I am told that I should have jupyter-notebook installed. I installed that, but I still get the same problem. I am not actually running jupyter-notebook, but still using the Python interpreter.

Then I fell back on trying the example in the README[2]:

>>> from insights import run
>>> from insights.parsers import installed_rpms as rpm
>>> lower = rpm.Rpm("bash-4.4.11-1.fc26")
>>> upper = rpm.Rpm("bash-4.4.22-1.fc26")
>>> results = run(rpm.Installed)
>>> rpms = results[rpm.Installed]
>>> rpms.newest("bash")
0:bash-4.4.12-7.fc26
>>> lower <= rpms.newest("bash") < upper
True

but that didn't work out so well either:

>>> rpms = results[rpm.Installed]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/insights/core/dr.py", line 949, in __getitem__
    raise KeyError("Unknown component: %s" % get_name(component))
KeyError: 'Unknown component: insights.parsers.installed_rpms.InstalledRpms'

Turns out there is one item in results, just not accessible by the specified key:

>>> [x for x in results]
[<class 'insights.core.context.HostContext'>]
>>> [(x, y) for x, y in results.items()]
[(<class 'insights.core.context.HostContext'>, <HostContext('/', 30)>)]
>>> (key, value) =  [(x, y) for x, y in results.items()][0]
>>> key
<class 'insights.core.context.HostContext'>
>>> value
<HostContext('/', 30)>
>>> type(key)
<class 'insights.core.context.ExecutionContextMeta'>
>>> type(value)
<class 'insights.core.context.HostContext'>
>>> value.newest
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'HostContext' object has no attribute 'newest'

At this point, I'm hoping for some help to figure out how to make the README example work for me.

I'm running this code on a Fedora 39 machine, and never tried on a Fedora 38 machine, so I can't compare.

Happy to provide additional information.

[1] https://github.com/RedHatInsights/insights-core/blob/master/docs/notebooks/Diagnostic%20Walkthrough.ipynb
[2] https://github.com/RedHatInsights/insights-core#readme

@mulkieran
Copy link
Author

with logging added:

>>> import logging
>>> logging.basicConfig(level=logging.DEBUG)
>>> from insights.parsers import installed_rpms as rpm
>>> results = run(rpm.Installed)
INFO:insights.core.dr:Trying insights.specs.Specs.installed_rpms
DEBUG:insights.core.dr:insights.specs.Specs.installed_rpms missing requirements All: [] Any: []
INFO:insights.core.dr:Trying insights.parsers.installed_rpms.InstalledRpms
DEBUG:insights.core.dr:insights.parsers.installed_rpms.InstalledRpms missing requirements All: ['insights.specs.Specs.installed_rpms'] Any: 

but, in same Python session:

>>> from insights.specs import Specs
>>> type(Specs.installed_rpms)
<class 'insights.core.spec_factory.RegistryPoint'>

@bfahr
Copy link
Contributor

bfahr commented Dec 11, 2023

@mulkieran the example is a bit old. Running the code I see that the default insights plugins are not installed when you invoke run(report) and that is why you get a rule failure:

In [4]: results = run(report)

In [5]: results[report]
Out[5]: 
{'rule_fqdn': '__main__.report',
 'reason': 'MISSING_REQUIREMENTS',
 'details': "All: ['insights.parsers.installed_rpms.InstalledRpms'] Any: ",
 'type': 'skip'}

In [8]: results.__dict__
Out[8]: 
{'instances': {insights.core.context.HostContext: <HostContext('/', 30)>,
  <function __main__.report(rpms)>: {'rule_fqdn': '__main__.report',
   'reason': 'MISSING_REQUIREMENTS',
   'details': "All: ['insights.parsers.installed_rpms.InstalledRpms'] Any: ",
   'type': 'skip'}},
 'missing_requirements': {insights.specs.Specs.installed_rpms: ([], [[]]),
  insights.parsers.installed_rpms.InstalledRpms: ([insights.specs.Specs.installed_rpms],
   [])},

You can explicitly load the deault plugins which will ensure that when you inoke run with your rule the necessary installed_rpms spec is loaded.

In [12]: from insights import load_default_plugins

In [13]: load_default_plugins()

In [14]: results = run(report)

In [15]: results[report]
Out[15]: {'version': 'bash-5.2.21-1.fc38', 'type': 'pass', 'pass_key': 'BASH_INSTALLED'}

In [16]: results.__dict__
Out[16]: 
{'instances': {insights.core.context.HostContext: <HostContext('/', 30)>,
  <insights.core.spec_factory.simple_command at 0x7f3fdc0e2f10>: CommandOutputProvider("'/bin/rpm -qa --qf \'\\{"name":"%{NAME}","epoch":"%{EPOCH}","version":"%{VERSION}","release":"%{RELEASE}","arch":"%{ARCH}","installtime":"%{INSTALLTIME:date}","buildtime":"%{BUILDTIME}","vendor":"%{VENDOR}","buildhost":"%{BUILDHOST}","sigpgp":"%{SIGPGP:pgpsig}"\\}\n\''"),
  insights.specs.Specs.installed_rpms: CommandOutputProvider("'/bin/rpm -qa --qf \'\\{"name":"%{NAME}","epoch":"%{EPOCH}","version":"%{VERSION}","release":"%{RELEASE}","arch":"%{ARCH}","installtime":"%{INSTALLTIME:date}","buildtime":"%{BUILDTIME}","vendor":"%{VENDOR}","buildhost":"%{BUILDHOST}","sigpgp":"%{SIGPGP:pgpsig}"\\}\n\''"),
  insights.parsers.installed_rpms.InstalledRpms: <insights.parsers.installed_rpms.InstalledRpms at 0x7f3fdc80ff50>,
  <function __main__.report(rpms)>: {'version': 'bash-5.2.21-1.fc38',
   'type': 'pass',
   'pass_key': 'BASH_INSTALLED'}},
 'missing_requirements': {<insights.core.spec_factory.first_file at 0x7f3fd7efcf90>: ([insights.core.context.SosArchiveContext],
   []),
  <insights.core.spec_factory.glob_file at 0x7f3fd7e44ed0>: ([insights.core.context.HostArchiveContext],
   []),
  <insights.core.spec_factory.head at 0x7f3fd7e60750>: ([<insights.core.spec_factory.glob_file at 0x7f3fd7e44ed0>],
   [])},

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants