Skip to content

Commit

Permalink
feat: revert and refine the padman list specs and parsers (#3466)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiangce Liu <[email protected]>
  • Loading branch information
xiangce authored Jul 14, 2022
1 parent 998129a commit a32d131
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 80 deletions.
89 changes: 14 additions & 75 deletions insights/parsers/podman_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,22 @@
====================================================
Parse the output of command "podman_list_images" and "podman_list_containers",
which have very similar formats.
The header line is parsed and used as the names for the remaining columns.
All fields in both header and data are assumed to be separated by at least
three spaces. This allows single spaces in values and headers, so headers
such as 'IMAGE ID' are captured as is.
If the header line and at least one data line are not found, no data is
stored.
Each row is stored as a dictionary, keyed on the header fields. The data is
available in two formats:
* The old format is a list of row dictionaries.
* The new format stores each dictionary in a dictionary keyed on the value of
a given field, given by the subclass.
which have very similar formats with the "docker_list_images" and
"docker_list_containers",
For more details, please refer to the
`:class:insights.parsers.docker_list.DockerList`.
"""
from insights import CommandParser, parser
from insights.parsers import SkipException, parse_fixed_table
from insights import parser
from insights.specs import Specs


class PodmanList(CommandParser):
"""
A general class for parsing tabular podman list information. Parsing
rules are:
* The first line is the header line.
* The other lines are data lines.
* All fields line up vertically.
* Fields are separated from each other by at least three spaces.
* Some fields can contain nothing, and this is shown as spaces, so we
need to catch that and turn it into None.
Why not just use hard-coded fields and columns? So that we can adapt to
different output lists.
Raises:
NotImplementedError: If `key_field` or `attr_name` is not defined
SkipException: If no data to parse
"""
key_field = ''
heading_ignore = []
attr_name = ''
substitutions = []

def parse_content(self, content):
if not (self.key_field and self.attr_name):
raise NotImplementedError("'key_field' or 'attr_name' is not defined")

self.rows = parse_fixed_table(content,
heading_ignore=self.heading_ignore,
header_substitute=self.substitutions)

if not self.rows:
raise SkipException('No data.')

data = {}
for row in self.rows:
k = row.get(self.key_field)
for sub in self.substitutions:
row[sub[0]] = row.pop(sub[1])
if k is not None and k != '<none>':
data[k] = row
setattr(self, self.attr_name, data)
from insights.parsers.docker_list import DockerListContainers, DockerListImages


@parser(Specs.podman_list_images)
class PodmanListImages(PodmanList):
class PodmanListImages(DockerListImages):
"""
Handle the list of podman images using the PodmanList parser class.
Handle the list of podman images using the
`class:insights.parsers.docker_list.DockerListImages` parser class.
Sample output of command ``podman images --all --no-trunc --digests``::
Expand All @@ -95,16 +39,14 @@ class PodmanListImages(PodmanList):
>>> images.images['rhel6_vsftpd']['CREATED']
'37 minutes ago'
"""
key_field = 'REPOSITORY'
heading_ignore = [key_field]
attr_name = 'images'
substitutions = [("IMAGE ID", "IMAGE_ID")]
pass


@parser(Specs.podman_list_containers)
class PodmanListContainers(PodmanList):
class PodmanListContainers(DockerListContainers):
"""
Handle the list of podman containers using the PodmanList parser class.
Handle the list of podman containers using the
`class:insights.parsers.docker_list.DockerListContainers` parser class.
Sample output of command ``podman ps --all --no-trunc --size``::
Expand All @@ -125,7 +67,4 @@ class PodmanListContainers(PodmanList):
>>> containers.containers['tender_rosalind']['STATUS']
'Exited (137) 18 hours ago'
"""
key_field = 'NAMES'
heading_ignore = ['CONTAINER']
attr_name = 'containers'
substitutions = [("CONTAINER ID", "CONTAINER_ID")]
pass
2 changes: 2 additions & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ class DefaultSpecs(Specs):
pluginconf_d = glob_file("/etc/yum/pluginconf.d/*.conf")
pmlog_summary = command_with_args("/usr/bin/pmlogsummary %s", pmlog_summary_args)
pmrep_metrics = simple_command("/usr/bin/pmrep -t 1s -T 1s network.interface.out.packets network.interface.collisions swap.pagesout mssql.memory_manager.stolen_server_memory mssql.memory_manager.total_server_memory -o csv")
podman_list_containers = simple_command("/usr/bin/podman ps --all --no-trunc")
podman_list_images = simple_command("/usr/bin/podman images --all --no-trunc --digests")
postconf_builtin = simple_command("/usr/sbin/postconf -C builtin")
postconf = simple_command("/usr/sbin/postconf")
postgresql_conf = first_file([
Expand Down
2 changes: 2 additions & 0 deletions insights/specs/insights_archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ class InsightsArchiveSpecs(Specs):
pcs_quorum_status = simple_file("insights_commands/pcs_quorum_status")
pcs_status = simple_file("insights_commands/pcs_status")
pmrep_metrics = first_file(["insights_commands/pmrep_-t_1s_-T_1s_network.interface.out.packets_network.interface.collisions_swap.pagesout_mssql.memory_manager.stolen_server_memory_mssql.memory_manager.total_server_memory_-o_csv", "insights_commands/pmrep_-t_1s_-T_1s_network.interface.out.packets_network.interface.collisions_swap.pagesout_-o_csv"])
podman_list_containers = simple_file("insights_commands/podman_ps_--all_--no-trunc")
podman_list_images = simple_file("insights_commands/podman_images_--all_--no-trunc_--digests")
postconf_builtin = simple_file("insights_commands/postconf_-C_builtin")
postconf = simple_file("insights_commands/postconf")
ps_alxwww = simple_file("insights_commands/ps_alxwww")
Expand Down
5 changes: 0 additions & 5 deletions insights/tests/parsers/test_podman_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,6 @@ def test_podman_list_images_no_data():
assert 'No data.' in str(ex)


def test_podman_undefined_key_field():
with pytest.raises(NotImplementedError):
assert podman_list.PodmanList(context_wrap(PODMAN_LIST_CONTAINERS))


def test_podman_documentation():
failed_count, tests = doctest.testmod(
podman_list,
Expand Down

0 comments on commit a32d131

Please sign in to comment.