-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add parser container_inspect (#3562)
* Add parser container_inspect Signed-off-by: jiazhang <[email protected]> * Update containers_inspect Signed-off-by: jiazhang <[email protected]> * Add parser Signed-off-by: jiazhang <[email protected]> * Update docs Signed-off-by: jiazhang <[email protected]> * Update format and description Signed-off-by: jiazhang <[email protected]> * Add back docker_container_inspect Signed-off-by: jiazhang <[email protected]> * Add deprecated warning Signed-off-by: jiazhang <[email protected]> * Fix string format Signed-off-by: jiazhang <[email protected]> * Update test data format Signed-off-by: jiazhang <[email protected]> * Update test data Signed-off-by: jiazhang <[email protected]> * Update test data Signed-off-by: jiazhang <[email protected]> * Update return value format Signed-off-by: jiazhang <[email protected]> * Update parser test data Signed-off-by: jiazhang <[email protected]> * Update datasource details Signed-off-by: jiazhang <[email protected]> * Update filter Signed-off-by: jiazhang <[email protected]> * Update docstring Signed-off-by: jiazhang <[email protected]> * Update doc Signed-off-by: jiazhang <[email protected]> * Update no_filters Signed-off-by: jiazhang <[email protected]> * Update default format Signed-off-by: jiazhang <[email protected]> * Update deprecated info Signed-off-by: jiazhang <[email protected]> * Update set format Signed-off-by: jiazhang <[email protected]> * Add a comment for condition checking if total_results is not null Signed-off-by: jiazhang <[email protected]> Signed-off-by: jiazhang <[email protected]>
- Loading branch information
1 parent
1878a29
commit 969c407
Showing
10 changed files
with
1,221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.. automodule:: insights.parsers.containers_inspect | ||
:members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
""" | ||
ContainersInspect - commands ``docker|podman inspect`` | ||
====================================================== | ||
This parser reads the output of commands: "/usr/bin/docker|podman inspect <containers ID>" | ||
which are used to show the metadata information of containers. | ||
""" | ||
from insights import parser, JSONParser | ||
from insights.specs import Specs | ||
|
||
|
||
@parser(Specs.containers_inspect) | ||
class ContainersInspect(JSONParser): | ||
""" | ||
Class for parsing the output of the containers inspect commands | ||
``/usr/bin/docker|podman inspect <containers ID>`` | ||
Typical Output of this command after datasource containers_inspect is:: | ||
[ | ||
{ | ||
"Id": "aeaea3ead527", | ||
"Image": "538460c14d75dee1504e56ad8ddb7fe039093b1530ef8f90442a454b9aa3dc8b", | ||
"engine": "podman", | ||
"HostConfig|Privileged": false, | ||
"Config|Cmd": ["sleep", "1000000"] | ||
} | ||
] | ||
Attributes: | ||
data (list): A list containing the parsed information | ||
Examples: | ||
>>> from insights.core.filters import add_filter | ||
>>> from insights.specs import Specs | ||
>>> add_filter(Specs.container_inspect_keys, ['HostConfig|Privileged']) | ||
>>> str(inspect_containers.data[0]["Id"]) | ||
'aeaea3ead527' | ||
>>> str(inspect_containers.data[0]["engine"]) | ||
'podman' | ||
>>> inspect_containers.data[0]["HostConfig|Privileged"] | ||
False | ||
""" | ||
|
||
def parse_content(self, content): | ||
super(ContainersInspect, self).parse_content(content[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
insights/specs/datasources/container/containers_inspect.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
Custom datasources for containers inspect information | ||
""" | ||
import json | ||
import os | ||
from insights.core.context import HostContext | ||
from insights.core.dr import SkipComponent | ||
from insights.core.filters import get_filters | ||
from insights.core.plugins import datasource, ContentException | ||
from insights.core.spec_factory import DatasourceProvider, foreach_execute | ||
from insights.specs import Specs | ||
from insights.specs.datasources.container import running_rhel_containers | ||
|
||
|
||
@datasource(running_rhel_containers, HostContext) | ||
def running_rhel_containers_id(broker): | ||
""" | ||
Returns a list of container_id of the running containers. | ||
""" | ||
containers_info = [] | ||
for container in broker[running_rhel_containers]: | ||
containers_info.append((container[1], container[2])) | ||
return containers_info | ||
|
||
|
||
class LocalSpecs(Specs): | ||
""" Local specs used only by docker|podman_inspect datasources """ | ||
|
||
containers_inspect_data_raw = foreach_execute(running_rhel_containers_id, "/usr/bin/%s inspect %s") | ||
""" Returns the output of command ``/usr/bin/docker|podman inspect <container ID>`` """ | ||
|
||
|
||
@datasource(LocalSpecs.containers_inspect_data_raw, HostContext) | ||
def containers_inspect_data_datasource(broker): | ||
""" | ||
This datasource provides the filtered information collected | ||
from ``/usr/bin/docker|podman inspect <container ID>``. | ||
.. note:: | ||
The path of the target key is like raw_data[key1][key2][target_key], | ||
then the filter pattern is like "key1|key2|target_key". | ||
If the value type of raw_data[key1][key2] is list, although the target_key is in the list, | ||
the filter pattern must be "key1|key2", this datasource returns the whole value of the list. | ||
The value of "Id" and "Image" are checked from raw data directly, no need filter these items. | ||
Typical content of ``/usr/bin/docker|podman inspect <container ID>`` file is:: | ||
[ | ||
{ | ||
"Id": "aeaea3ead52724bb525bb2b5c619d67836250756920f0cb9884431ba53b476d8", | ||
"Created": "2022-10-21T23:47:24.506159696-04:00", | ||
"Path": "sleep" | ||
... | ||
} | ||
... | ||
] | ||
Returns: | ||
list: item is JSON string containing filtered information. | ||
Raises: | ||
SkipComponent: When the filter/path does not exist or any exception occurs. | ||
""" | ||
try: | ||
no_filters = ['Id', 'Image'] | ||
filters = sorted(set(get_filters(Specs.container_inspect_keys)) - set(no_filters)) | ||
if filters: | ||
total_results = [] | ||
for item in broker[LocalSpecs.containers_inspect_data_raw]: | ||
raw_data = json.loads(''.join(item.content))[0] | ||
engine, _, container_id = item.cmd.split(None) | ||
filter_result = dict(Id=container_id, engine=os.path.basename(engine)) | ||
if 'Image' in raw_data: | ||
filter_result['Image'] = raw_data['Image'].split("sha256:")[-1] | ||
for item in filters: | ||
val = raw_data | ||
for key in item.split("|"): | ||
if key in val: | ||
val = val[key] | ||
else: | ||
break | ||
# If the filtered key does not exist, skip it | ||
if val == raw_data: | ||
continue | ||
filter_result[item] = val | ||
total_results.append(filter_result) | ||
# Generally the False branch of this condition will never reach, since the required component | ||
# LocalSpecs.containers_inspect_data_raw can guarantee total_results is not null. However, it's worth | ||
# leaving this condition as an explicit assertion to avoid unexpected situation. | ||
if total_results: | ||
return DatasourceProvider(content=json.dumps(total_results), relative_path='insights_containers/containers_inspect') | ||
except Exception as e: | ||
raise ContentException("Unexpected content exception:{e}".format(e=str(e))) | ||
raise SkipComponent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.