Skip to content

Commit

Permalink
PodmanContainer parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Sep 17, 2024
1 parent e11b769 commit 7141a46
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
2 changes: 2 additions & 0 deletions wrapanapi/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -30,5 +31,6 @@
"VmwareCloudSystem",
"VMWareSystem",
"Openshift",
"Podman",
"VmState",
]
84 changes: 74 additions & 10 deletions wrapanapi/systems/container/podman.py
Original file line number Diff line number Diff line change
@@ -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):
return self.raw.delete()

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):
Expand All @@ -25,27 +84,32 @@ 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(
proto=self.protocol, username=self.username, host=self.hostname, port=self.port
)
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)

0 comments on commit 7141a46

Please sign in to comment.