From 98c656ea569ead301a2270e735e8355e9288194f Mon Sep 17 00:00:00 2001 From: bauen1 Date: Thu, 24 Oct 2024 14:41:50 +0200 Subject: [PATCH 1/2] facts: add podman system info and ps facts --- pyinfra/facts/podman.py | 47 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 pyinfra/facts/podman.py diff --git a/pyinfra/facts/podman.py b/pyinfra/facts/podman.py new file mode 100644 index 000000000..982f2b117 --- /dev/null +++ b/pyinfra/facts/podman.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +import json +from typing import Any, Dict, Iterable, List, TypeVar + +from pyinfra.api import FactBase + +T = TypeVar("T") + + +class PodmanFactBase(FactBase[T]): + """ + Base for facts using `podman` to retrieve + """ + + abstract = True + + def requires_command(self, *args, **kwargs) -> str: + return "podman" + + +class PodmanSystemInfo(PodmanFactBase[Dict[str, Any]]): + """ + Output of 'podman system info' + """ + + def command(self) -> str: + return "podman system info --format=json" + + def process(self, output: Iterable[str]) -> Dict[str, Any]: + output = json.loads(("").join(output)) + assert isinstance(output, dict) + return output + + +class PodmanPs(PodmanFactBase[List[Dict[str, Any]]]): + """ + Output of 'podman ps' + """ + + def command(self) -> str: + return "podman ps --format=json --all" + + def process(self, output: Iterable[str]) -> List[Dict[str, Any]]: + output = json.loads(("").join(output)) + assert isinstance(output, list) + return output # type: ignore From 08ef1b342b24d5ba05ae18c9003949b4db09414a Mon Sep 17 00:00:00 2001 From: bauen1 Date: Thu, 31 Oct 2024 15:25:07 +0100 Subject: [PATCH 2/2] tests: add tests for PodmanSystemInfo, PodmanPs --- tests/facts/podman.PodmanPs/empty.json | 8 + tests/facts/podman.PodmanPs/onecontainer.json | 74 +++++ .../facts/podman.PodmanSystemInfo/info1.json | 296 ++++++++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 tests/facts/podman.PodmanPs/empty.json create mode 100644 tests/facts/podman.PodmanPs/onecontainer.json create mode 100644 tests/facts/podman.PodmanSystemInfo/info1.json diff --git a/tests/facts/podman.PodmanPs/empty.json b/tests/facts/podman.PodmanPs/empty.json new file mode 100644 index 000000000..90068187b --- /dev/null +++ b/tests/facts/podman.PodmanPs/empty.json @@ -0,0 +1,8 @@ +{ + "command": "podman ps --format=json --all", + "requires_command": "podman", + "output": [ + "[]" + ], + "fact": [] +} diff --git a/tests/facts/podman.PodmanPs/onecontainer.json b/tests/facts/podman.PodmanPs/onecontainer.json new file mode 100644 index 000000000..5c5dcdae5 --- /dev/null +++ b/tests/facts/podman.PodmanPs/onecontainer.json @@ -0,0 +1,74 @@ +{ + "command": "podman ps --format=json --all", + "requires_command": "podman", + "output": [ + "[", + " {", + " \"AutoRemove\": true,", + " \"Command\": [", + " \"bash\"", + " ],", + " \"CreatedAt\": \"3 seconds ago\",", + " \"Exited\": false,", + " \"ExitedAt\": -62135596800,", + " \"ExitCode\": 0,", + " \"Id\": \"a0319f34c9948123e217e0d145f35ad4903517f96d7c096145169dc70ed6badf\",", + " \"Image\": \"docker.io/library/debian:unstable\",", + " \"ImageID\": \"e1234e51d050c2c0e7d2517cc5d6e6db6b91de08124e56246bdd8acd01f4af92\",", + " \"IsInfra\": false,", + " \"Labels\": null,", + " \"Mounts\": [],", + " \"Names\": [", + " \"blissful_cerf\"", + " ],", + " \"Namespaces\": {", + " ", + " },", + " \"Networks\": [],", + " \"Pid\": 74195,", + " \"Pod\": \"\",", + " \"PodName\": \"\",", + " \"Ports\": null,", + " \"Size\": null,", + " \"StartedAt\": 1730384087,", + " \"State\": \"running\",", + " \"Status\": \"Up 3 seconds ago\",", + " \"Created\": 1730384086", + " }", + "]" + ], + "fact": [ + { + "AutoRemove": true, + "Command": [ + "bash" + ], + "CreatedAt": "3 seconds ago", + "Exited": false, + "ExitedAt": -62135596800, + "ExitCode": 0, + "Id": "a0319f34c9948123e217e0d145f35ad4903517f96d7c096145169dc70ed6badf", + "Image": "docker.io/library/debian:unstable", + "ImageID": "e1234e51d050c2c0e7d2517cc5d6e6db6b91de08124e56246bdd8acd01f4af92", + "IsInfra": false, + "Labels": null, + "Mounts": [], + "Names": [ + "blissful_cerf" + ], + "Namespaces": { + + }, + "Networks": [], + "Pid": 74195, + "Pod": "", + "PodName": "", + "Ports": null, + "Size": null, + "StartedAt": 1730384087, + "State": "running", + "Status": "Up 3 seconds ago", + "Created": 1730384086 + } + ] +} diff --git a/tests/facts/podman.PodmanSystemInfo/info1.json b/tests/facts/podman.PodmanSystemInfo/info1.json new file mode 100644 index 000000000..b17ee35ea --- /dev/null +++ b/tests/facts/podman.PodmanSystemInfo/info1.json @@ -0,0 +1,296 @@ +{ + "command": "podman system info --format=json", + "requires_command": "podman", + "output": [ + "{", + " \"host\": {", + " \"arch\": \"amd64\",", + " \"buildahVersion\": \"1.28.2\",", + " \"cgroupManager\": \"systemd\",", + " \"cgroupVersion\": \"v2\",", + " \"cgroupControllers\": [", + " \"cpu\",", + " \"memory\",", + " \"pids\"", + " ],", + " \"conmon\": {", + " \"package\": \"conmon_2.1.6+ds1-1_amd64\",", + " \"path\": \"/usr/bin/conmon\",", + " \"version\": \"conmon version 2.1.6, commit: unknown\"", + " },", + " \"cpus\": 6,", + " \"cpuUtilization\": {", + " \"userPercent\": 4.87,", + " \"systemPercent\": 5.66,", + " \"idlePercent\": 89.47", + " },", + " \"distribution\": {", + " \"distribution\": \"debian\",", + " \"version\": \"12\",", + " \"codename\": \"bookworm\"", + " },", + " \"eventLogger\": \"journald\",", + " \"hostname\": \"mcc011\",", + " \"idMappings\": {", + " \"gidmap\": [", + " {", + " \"container_id\": 0,", + " \"host_id\": 1000,", + " \"size\": 1", + " },", + " {", + " \"container_id\": 1,", + " \"host_id\": 100000,", + " \"size\": 65536", + " }", + " ],", + " \"uidmap\": [", + " {", + " \"container_id\": 0,", + " \"host_id\": 1000,", + " \"size\": 1", + " },", + " {", + " \"container_id\": 1,", + " \"host_id\": 100000,", + " \"size\": 65536", + " }", + " ]", + " },", + " \"kernel\": \"6.1.0-26-amd64\",", + " \"logDriver\": \"journald\",", + " \"memFree\": 11634356224,", + " \"memTotal\": 16606085120,", + " \"networkBackend\": \"cni\",", + " \"ociRuntime\": {", + " \"name\": \"crun\",", + " \"package\": \"crun_1.8.1-1+deb12u1_amd64\",", + " \"path\": \"/usr/bin/crun\",", + " \"version\": \"crun version 1.8.1\\ncommit: f8a096be060b22ccd3d5f3ebe44108517fbf6c30\\nrundir: /run/user/1000/crun\\nspec: 1.0.0\\n+SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +YAJL\"", + " },", + " \"os\": \"linux\",", + " \"remoteSocket\": {", + " \"path\": \"/run/user/1000/podman/podman.sock\"", + " },", + " \"serviceIsRemote\": false,", + " \"security\": {", + " \"apparmorEnabled\": false,", + " \"capabilities\": \"CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT\",", + " \"rootless\": true,", + " \"seccompEnabled\": true,", + " \"seccompProfilePath\": \"/usr/share/containers/seccomp.json\",", + " \"selinuxEnabled\": false", + " },", + " \"slirp4netns\": {", + " \"executable\": \"/usr/bin/slirp4netns\",", + " \"package\": \"slirp4netns_1.2.0-1_amd64\",", + " \"version\": \"slirp4netns version 1.2.0\\ncommit: 656041d45cfca7a4176f6b7eed9e4fe6c11e8383\\nlibslirp: 4.7.0\\nSLIRP_CONFIG_VERSION_MAX: 4\\nlibseccomp: 2.5.4\"", + " },", + " \"swapFree\": 7998533632,", + " \"swapTotal\": 7998533632,", + " \"uptime\": \"502h 36m 25.00s (Approximately 20.92 days)\",", + " \"linkmode\": \"dynamic\"", + " },", + " \"store\": {", + " \"configFile\": \"/home/sysadmin/.config/containers/storage.conf\",", + " \"containerStore\": {", + " \"number\": 0,", + " \"paused\": 0,", + " \"running\": 0,", + " \"stopped\": 0", + " },", + " \"graphDriverName\": \"vfs\",", + " \"graphOptions\": {", + " ", + " },", + " \"graphRoot\": \"/home/sysadmin/.local/share/containers/storage\",", + " \"graphRootAllocated\": 116060811264,", + " \"graphRootUsed\": 11818217472,", + " \"graphStatus\": {", + " ", + " },", + " \"imageCopyTmpDir\": \"/var/tmp\",", + " \"imageStore\": {", + " \"number\": 0", + " },", + " \"runRoot\": \"/run/user/1000/containers\",", + " \"volumePath\": \"/home/sysadmin/.local/share/containers/storage/volumes\"", + " },", + " \"registries\": {", + " ", + " },", + " \"plugins\": {", + " \"volume\": [", + " \"local\"", + " ],", + " \"network\": [", + " \"bridge\",", + " \"macvlan\",", + " \"ipvlan\"", + " ],", + " \"log\": [", + " \"k8s-file\",", + " \"none\",", + " \"passthrough\",", + " \"journald\"", + " ],", + " \"authorization\": null", + " },", + " \"version\": {", + " \"APIVersion\": \"4.3.1\",", + " \"Version\": \"4.3.1\",", + " \"GoVersion\": \"go1.19.8\",", + " \"GitCommit\": \"\",", + " \"BuiltTime\": \"Thu Jan 1 01:00:00 1970\",", + " \"Built\": 0,", + " \"OsArch\": \"linux/amd64\",", + " \"Os\": \"linux\"", + " }", + "}" + ], + "fact": { + "host": { + "arch": "amd64", + "buildahVersion": "1.28.2", + "cgroupManager": "systemd", + "cgroupVersion": "v2", + "cgroupControllers": [ + "cpu", + "memory", + "pids" + ], + "conmon": { + "package": "conmon_2.1.6+ds1-1_amd64", + "path": "/usr/bin/conmon", + "version": "conmon version 2.1.6, commit: unknown" + }, + "cpus": 6, + "cpuUtilization": { + "userPercent": 4.87, + "systemPercent": 5.66, + "idlePercent": 89.47 + }, + "distribution": { + "distribution": "debian", + "version": "12", + "codename": "bookworm" + }, + "eventLogger": "journald", + "hostname": "mcc011", + "idMappings": { + "gidmap": [ + { + "container_id": 0, + "host_id": 1000, + "size": 1 + }, + { + "container_id": 1, + "host_id": 100000, + "size": 65536 + } + ], + "uidmap": [ + { + "container_id": 0, + "host_id": 1000, + "size": 1 + }, + { + "container_id": 1, + "host_id": 100000, + "size": 65536 + } + ] + }, + "kernel": "6.1.0-26-amd64", + "logDriver": "journald", + "memFree": 11634356224, + "memTotal": 16606085120, + "networkBackend": "cni", + "ociRuntime": { + "name": "crun", + "package": "crun_1.8.1-1+deb12u1_amd64", + "path": "/usr/bin/crun", + "version": "crun version 1.8.1\ncommit: f8a096be060b22ccd3d5f3ebe44108517fbf6c30\nrundir: /run/user/1000/crun\nspec: 1.0.0\n+SYSTEMD +SELINUX +APPARMOR +CAP +SECCOMP +EBPF +YAJL" + }, + "os": "linux", + "remoteSocket": { + "path": "/run/user/1000/podman/podman.sock" + }, + "serviceIsRemote": false, + "security": { + "apparmorEnabled": false, + "capabilities": "CAP_CHOWN,CAP_DAC_OVERRIDE,CAP_FOWNER,CAP_FSETID,CAP_KILL,CAP_NET_BIND_SERVICE,CAP_SETFCAP,CAP_SETGID,CAP_SETPCAP,CAP_SETUID,CAP_SYS_CHROOT", + "rootless": true, + "seccompEnabled": true, + "seccompProfilePath": "/usr/share/containers/seccomp.json", + "selinuxEnabled": false + }, + "slirp4netns": { + "executable": "/usr/bin/slirp4netns", + "package": "slirp4netns_1.2.0-1_amd64", + "version": "slirp4netns version 1.2.0\ncommit: 656041d45cfca7a4176f6b7eed9e4fe6c11e8383\nlibslirp: 4.7.0\nSLIRP_CONFIG_VERSION_MAX: 4\nlibseccomp: 2.5.4" + }, + "swapFree": 7998533632, + "swapTotal": 7998533632, + "uptime": "502h 36m 25.00s (Approximately 20.92 days)", + "linkmode": "dynamic" + }, + "store": { + "configFile": "/home/sysadmin/.config/containers/storage.conf", + "containerStore": { + "number": 0, + "paused": 0, + "running": 0, + "stopped": 0 + }, + "graphDriverName": "vfs", + "graphOptions": { + + }, + "graphRoot": "/home/sysadmin/.local/share/containers/storage", + "graphRootAllocated": 116060811264, + "graphRootUsed": 11818217472, + "graphStatus": { + + }, + "imageCopyTmpDir": "/var/tmp", + "imageStore": { + "number": 0 + }, + "runRoot": "/run/user/1000/containers", + "volumePath": "/home/sysadmin/.local/share/containers/storage/volumes" + }, + "registries": { + + }, + "plugins": { + "volume": [ + "local" + ], + "network": [ + "bridge", + "macvlan", + "ipvlan" + ], + "log": [ + "k8s-file", + "none", + "passthrough", + "journald" + ], + "authorization": null + }, + "version": { + "APIVersion": "4.3.1", + "Version": "4.3.1", + "GoVersion": "go1.19.8", + "GitCommit": "", + "BuiltTime": "Thu Jan 1 01:00:00 1970", + "Built": 0, + "OsArch": "linux/amd64", + "Os": "linux" + } + } +}