From 6dad995df7214b81d86a754f1e855f4d9485cfe8 Mon Sep 17 00:00:00 2001 From: jyejare Date: Tue, 17 Sep 2024 22:51:19 +0530 Subject: [PATCH] PodmanContainer parent class --- wrapanapi/__init__.py | 2 + wrapanapi/systems/container/podman.py | 84 +++++++++++++++++++++++---- 2 files changed, 76 insertions(+), 10 deletions(-) diff --git a/wrapanapi/__init__.py b/wrapanapi/__init__.py index 4fadfb82..cdae110b 100644 --- a/wrapanapi/__init__.py +++ b/wrapanapi/__init__.py @@ -1,6 +1,7 @@ # Imports for convenience from .entities.vm import VmState from .systems.container.rhopenshift import Openshift +from .systems.container.podman import Podman from .systems.ec2 import EC2System from .systems.google import GoogleCloudSystem from .systems.hawkular import HawkularSystem @@ -30,5 +31,6 @@ "VmwareCloudSystem", "VMWareSystem", "Openshift", + "Podman", "VmState", ] diff --git a/wrapanapi/systems/container/podman.py b/wrapanapi/systems/container/podman.py index 5f5b469c..50aa5713 100644 --- a/wrapanapi/systems/container/podman.py +++ b/wrapanapi/systems/container/podman.py @@ -1,6 +1,65 @@ +from abc import ABCMeta + from podman import PodmanClient +from proto.utils import cached_property +from datetime import datetime from wrapanapi.systems.base import System +from wrapanapi.entities.base import Entity +from abc import ABCMeta + + +class PodmanContainer(Entity, metaclass=ABCMeta): + + def __init__(self, system, raw=None, **kwargs): + """ + Constructor for an PodmanContainer tied to a specific host. + + Args: + system: a Podman system object + raw: Raw container object if already obtained, or None + """ + self._name = kwargs.get("name") + self._id = kwargs.get("id") + if not self._name: + raise ValueError("missing required kwargs identifier: 'name'") + + super().__init__(system, raw, **kwargs) + self._api = self.system.containers_collection + + @property + def _identifying_attrs(self): + return {"name": self._name, "id": self._id} + + @property + def name(self): + return self._name + + @property + def id(self): + return self._id + + @property + def uuid(self): + return self.id + + @property + def creation_time(self): + return datetime.fromisoformat(self.raw.attrs['Created']) + + def delete(self, force=False): + return self.raw.remove(force=force) + + def stop(self): + return self.raw.stop() + + def cleanup(self): + return self.delete() + + def refresh(self): + container = self._api.get(self.name) + self.raw = container + return self.raw class Podman(System): @@ -25,11 +84,11 @@ def _identifying_attrs(self): return {"hostname": self.hostname, "port": self.port} def _connect(self): - url = "{proto}://{username}@{host}:{port}/run/podman/podman.sock".format( + self.url = "{proto}://{username}@{host}:{port}/run/podman/podman.sock".format( proto=self.protocol, username=self.username, host=self.hostname, port=self.port ) - self.podmanclient = PodmanClient(base_url=url) + self.podmanclient = PodmanClient(base_url=self.url) def info(self): url = "{proto}://{username}@{host}:{port}".format( @@ -37,15 +96,20 @@ def info(self): ) return f"podman {url}" - def list_containers(self): - """Returns list of containers""" - containers = self.podmanclient.containers - return containers.list() + @property + def containers_collection(self): + return self.podmanclient.containers - def list_containers_by_names_and_ids(self): + @cached_property + def containers(self): """Returns list of containers""" - containers = self.list_containers() - return [(container.name, container.id) for container in containers] + conInstance = [] + for cont in self.containers_collection.list(): + conInstance.append( + PodmanContainer(system=self, name=cont.name, id=cont.id, raw=cont) + ) + return conInstance def get_container(self, key): - return self.podmanclient.containers.get(key) + container = self.containers_collection.get(key) + return PodmanContainer(system=self, name=container.name, id=container.id, raw=container)