Skip to content

Commit

Permalink
Fixing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Sep 19, 2024
1 parent 7d28edd commit 8e41ed7
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies = [
"openshift==0.3.4",
"ovirt-engine-sdk-python~=4.3",
"packaging",
"podman==5.0.0",
"py3winrm==0.0.1",
"python-cinderclient",
"python-glanceclient",
Expand Down
2 changes: 2 additions & 0 deletions wrapanapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Imports for convenience
from .entities.vm import VmState
from .systems.container.podman import Podman
from .systems.container.rhopenshift import Openshift
from .systems.ec2 import EC2System
from .systems.google import GoogleCloudSystem
Expand Down Expand Up @@ -30,5 +31,6 @@
"VmwareCloudSystem",
"VMWareSystem",
"Openshift",
"Podman",
"VmState",
]
3 changes: 2 additions & 1 deletion wrapanapi/systems/container/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .podman import Podman
from .rhopenshift import Openshift

__all__ = ["Openshift"]
__all__ = ["Openshift", "Podman"]
121 changes: 121 additions & 0 deletions wrapanapi/systems/container/podman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from abc import ABCMeta
from datetime import datetime

from podman import PodmanClient
from proto.utils import cached_property

from wrapanapi.entities.base import Entity
from wrapanapi.systems.base import System


class PodmanContainer(Entity, metaclass=ABCMeta):
def __init__(self, system, raw=None, **kwargs):
"""
Constructor for an PodmanContainer tied to a specific Container.
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")
self._image = kwargs.get("image")
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 image(self):
return self._image

@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, force=False):
return self.delete(force=force)

def refresh(self):
container = self._api.get(self.name)
self.raw = container
return self.raw


class Podman(System):
def __init__(
self, hostname, username, protocol="http+ssh", port=22, verify_ssl=False, **kwargs
):
super().__init__(hostname, username, protocol, port, verify_ssl, **kwargs)
self.username = username
self.hostname = hostname
self.protocol = protocol
self.port = port
self.verify_ssl = verify_ssl

self._connect()

def _identifying_attrs(self):
"""
Return a dict with key, value pairs for each kwarg that is used to
uniquely identify this system.
"""
return {"hostname": self.hostname, "port": self.port}

def _connect(self):
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=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}"

@property
def containers_collection(self):
return self.podmanclient.containers

@cached_property
def containers(self):
"""Returns list of containers"""
conInstance = []
for container in self.containers_collection.list():
conInstance.append(
PodmanContainer(
system=self, raw=container, name=container.name, id=container.id, image=container.image
)
)
return conInstance

def get_container(self, key):
container = self.containers_collection.get(key)
return PodmanContainer(
system=self, raw=container, name=container.name, id=container.id, image=container.image
)

0 comments on commit 8e41ed7

Please sign in to comment.