|
| 1 | +from abc import ABCMeta |
| 2 | +from datetime import datetime |
| 3 | + |
| 4 | +from podman import PodmanClient |
| 5 | +from proto.utils import cached_property |
| 6 | + |
| 7 | +from wrapanapi.entities.base import Entity |
| 8 | +from wrapanapi.systems.base import System |
| 9 | + |
| 10 | + |
| 11 | +class PodmanContainer(Entity, metaclass=ABCMeta): |
| 12 | + def __init__(self, system, key=None, raw=None, **kwargs): |
| 13 | + """ |
| 14 | + Constructor for an PodmanContainer tied to a specific Container. |
| 15 | +
|
| 16 | + Args: |
| 17 | + system: a Podman system object |
| 18 | + key: An unique identifier for the container, could be name or id |
| 19 | + raw: Raw container object if already obtained, or None |
| 20 | + """ |
| 21 | + self.key = key |
| 22 | + if not self.key: |
| 23 | + raise ValueError("missing required kwargs identifier: 'key'") |
| 24 | + self._name = kwargs.get("name") |
| 25 | + self._id = kwargs.get("id") |
| 26 | + self._image = kwargs.get("image") |
| 27 | + |
| 28 | + super().__init__(system, raw, **kwargs) |
| 29 | + self._api = self.system.containers_collection |
| 30 | + |
| 31 | + @property |
| 32 | + def _identifying_attrs(self): |
| 33 | + return {"name": self.name, "id": self.id} |
| 34 | + |
| 35 | + @property |
| 36 | + def name(self): |
| 37 | + if not self._name: |
| 38 | + self._name = self.raw.name |
| 39 | + return self._name |
| 40 | + |
| 41 | + @property |
| 42 | + def id(self): |
| 43 | + if not self._id: |
| 44 | + self._id = self.raw.id |
| 45 | + return self._id |
| 46 | + |
| 47 | + @property |
| 48 | + def image(self): |
| 49 | + return self._image |
| 50 | + |
| 51 | + @property |
| 52 | + def uuid(self): |
| 53 | + return self.id |
| 54 | + |
| 55 | + @property |
| 56 | + def creation_time(self): |
| 57 | + return datetime.fromisoformat(self.raw.attrs["Created"]) |
| 58 | + |
| 59 | + def delete(self, force=False): |
| 60 | + return self.raw.remove(force=force) |
| 61 | + |
| 62 | + def stop(self): |
| 63 | + return self.raw.stop() |
| 64 | + |
| 65 | + def cleanup(self, force=False): |
| 66 | + return self.delete(force=force) |
| 67 | + |
| 68 | + def refresh(self): |
| 69 | + container = self._api.get(self.key) |
| 70 | + self.raw = container |
| 71 | + return self.raw |
| 72 | + |
| 73 | + |
| 74 | +class Podman(System): |
| 75 | + def __init__( |
| 76 | + self, hostname, username, protocol="http+ssh", port=22, verify_ssl=False, **kwargs |
| 77 | + ): |
| 78 | + super().__init__(hostname, username, protocol, port, verify_ssl, **kwargs) |
| 79 | + self.username = username |
| 80 | + self.hostname = hostname |
| 81 | + self.protocol = protocol |
| 82 | + self.port = port |
| 83 | + self.verify_ssl = verify_ssl |
| 84 | + |
| 85 | + self._connect() |
| 86 | + |
| 87 | + def _identifying_attrs(self): |
| 88 | + """ |
| 89 | + Return a dict with key, value pairs for each kwarg that is used to |
| 90 | + uniquely identify this system. |
| 91 | + """ |
| 92 | + return {"hostname": self.hostname, "port": self.port} |
| 93 | + |
| 94 | + def _connect(self): |
| 95 | + self.url = "{proto}://{username}@{host}:{port}/run/podman/podman.sock".format( |
| 96 | + proto=self.protocol, username=self.username, host=self.hostname, port=self.port |
| 97 | + ) |
| 98 | + |
| 99 | + self.podmanclient = PodmanClient(base_url=self.url) |
| 100 | + |
| 101 | + def info(self): |
| 102 | + url = "{proto}://{username}@{host}:{port}".format( |
| 103 | + proto=self.protocol, username=self.username, host=self.hostname, port=self.port |
| 104 | + ) |
| 105 | + return f"podman {url}" |
| 106 | + |
| 107 | + @property |
| 108 | + def containers_collection(self): |
| 109 | + return self.podmanclient.containers |
| 110 | + |
| 111 | + @cached_property |
| 112 | + def containers(self): |
| 113 | + """Returns list of containers""" |
| 114 | + conInstance = [] |
| 115 | + for container in self.containers_collection.list(): |
| 116 | + conInstance.append( |
| 117 | + PodmanContainer( |
| 118 | + system=self, |
| 119 | + key=container.id, |
| 120 | + raw=container, |
| 121 | + name=container.name, |
| 122 | + image=container.image, |
| 123 | + ) |
| 124 | + ) |
| 125 | + return conInstance |
| 126 | + |
| 127 | + def get_container(self, key): |
| 128 | + container = self.containers_collection.get(key) |
| 129 | + return PodmanContainer( |
| 130 | + system=self, key=container.id, raw=container, name=container.name, image=container.image |
| 131 | + ) |
0 commit comments