Docker outline
This commit is contained in:
parent
b18488a6d3
commit
7e189271e3
4 changed files with 69 additions and 0 deletions
0
app/model/__init__.py
Normal file
0
app/model/__init__.py
Normal file
40
app/model/containers.py
Normal file
40
app/model/containers.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from dataclasses import dataclass, field
|
||||
from datetime import datetime
|
||||
|
||||
from docker.models.containers import Container
|
||||
|
||||
|
||||
@dataclass
|
||||
class SimpleContainer:
|
||||
name: str
|
||||
image: str
|
||||
status: str
|
||||
health: str
|
||||
created: datetime
|
||||
|
||||
@staticmethod
|
||||
def from_container(container: Container):
|
||||
created = datetime.strptime(container.attrs['Created'].split('.')[0], '%Y-%m-%dT%H:%M:%S')
|
||||
return SimpleContainer(
|
||||
name=container.name,
|
||||
image=container.image.tags[0],
|
||||
status=container.status,
|
||||
health=container.health,
|
||||
created=created
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Compose:
|
||||
directory: str
|
||||
containers: list[SimpleContainer] = field(default_factory=list)
|
||||
|
||||
@property
|
||||
def last_modified(self):
|
||||
return max(self.containers, key=lambda c: c.created).created
|
||||
|
||||
|
||||
@dataclass
|
||||
class Tree:
|
||||
composes: dict[str, Compose] = field(default_factory=dict)
|
||||
containers: list[SimpleContainer] = field(default_factory=list)
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import docker
|
||||
from docker.models.containers import Container
|
||||
|
||||
from app.model.containers import Tree, Compose, SimpleContainer
|
||||
|
||||
|
||||
class DockerService:
|
||||
def __init__(self):
|
||||
self._client = docker.from_env()
|
||||
self._tree = self._init_tree()
|
||||
|
||||
def _init_tree(self) -> Tree:
|
||||
tree = Tree()
|
||||
container: Container
|
||||
for container in self._client.containers.list():
|
||||
labels = container.labels
|
||||
working_dir = labels.get("com.docker.compose.project.working_dir")
|
||||
if working_dir:
|
||||
if tree.composes.get(working_dir) is None:
|
||||
tree.composes[working_dir] = Compose(working_dir)
|
||||
tree.composes[working_dir].containers.append(SimpleContainer.from_container(container))
|
||||
else:
|
||||
tree.containers.append(SimpleContainer.from_container(container))
|
||||
return tree
|
||||
|
||||
@property
|
||||
def tree(self) -> Tree:
|
||||
return self._tree
|
||||
Loading…
Add table
Add a link
Reference in a new issue