2022-05-25 11:16:10 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2022-05-16 18:57:14 +02:00
|
|
|
import graphviz
|
|
|
|
|
2022-05-27 08:17:04 +02:00
|
|
|
from compose_viz.models.compose import Compose
|
2022-05-16 18:57:14 +02:00
|
|
|
|
|
|
|
|
2022-06-08 17:29:01 +02:00
|
|
|
def apply_vertex_style(type: str) -> dict:
|
2022-05-16 18:57:14 +02:00
|
|
|
style = {
|
2022-05-18 17:28:18 +02:00
|
|
|
"service": {
|
|
|
|
"shape": "component",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-18 17:28:18 +02:00
|
|
|
"volume": {
|
2022-06-05 08:17:38 +02:00
|
|
|
"shape": "cylinder",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-18 17:28:18 +02:00
|
|
|
"network": {
|
|
|
|
"shape": "pentagon",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-18 17:28:18 +02:00
|
|
|
"port": {
|
|
|
|
"shape": "circle",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-06-05 08:17:38 +02:00
|
|
|
"env_file": {
|
|
|
|
"shape": "tab",
|
|
|
|
},
|
|
|
|
"porfile": {
|
|
|
|
"shape": "invhouse",
|
|
|
|
},
|
|
|
|
"cgroup": {
|
|
|
|
"shape": "diamond",
|
|
|
|
},
|
2022-06-08 17:25:52 +02:00
|
|
|
"device": {
|
|
|
|
"shape": "box3d",
|
|
|
|
},
|
2022-05-16 18:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return style[type]
|
|
|
|
|
|
|
|
|
2022-06-08 17:29:01 +02:00
|
|
|
def apply_edge_style(type: str) -> dict:
|
2022-05-16 18:57:14 +02:00
|
|
|
style = {
|
2022-06-08 17:25:52 +02:00
|
|
|
"exposes": {
|
2022-05-18 17:28:18 +02:00
|
|
|
"style": "solid",
|
2022-05-25 10:18:53 +02:00
|
|
|
"dir": "both",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-18 17:28:18 +02:00
|
|
|
"links": {
|
|
|
|
"style": "solid",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-18 17:28:18 +02:00
|
|
|
"volumes": {
|
|
|
|
"style": "dashed",
|
2022-05-25 07:16:56 +02:00
|
|
|
"dir": "both",
|
2022-05-18 17:28:18 +02:00
|
|
|
},
|
|
|
|
"depends_on": {
|
|
|
|
"style": "dotted",
|
2022-05-16 18:57:14 +02:00
|
|
|
},
|
2022-05-25 11:16:10 +02:00
|
|
|
"extends": {
|
2022-05-25 07:16:56 +02:00
|
|
|
"dir": "both",
|
|
|
|
"arrowhead": "inv",
|
|
|
|
"arrowtail": "dot",
|
2022-05-25 11:16:10 +02:00
|
|
|
},
|
2022-06-05 08:17:38 +02:00
|
|
|
"env_file": {
|
|
|
|
"style": "solid",
|
|
|
|
},
|
2022-05-16 18:57:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return style[type]
|
|
|
|
|
|
|
|
|
|
|
|
class Graph:
|
|
|
|
def __init__(self, compose: Compose, filename: str) -> None:
|
|
|
|
self.dot = graphviz.Digraph()
|
2022-05-18 17:28:18 +02:00
|
|
|
self.dot.attr("graph", background="#ffffff", pad="0.5", ratio="fill")
|
2022-05-16 18:57:14 +02:00
|
|
|
self.compose = compose
|
|
|
|
self.filename = filename
|
|
|
|
|
2022-05-25 07:16:56 +02:00
|
|
|
def validate_name(self, name: str) -> str:
|
|
|
|
# graphviz does not allow ':' in node name
|
|
|
|
transTable = name.maketrans({":": ""})
|
|
|
|
return name.translate(transTable)
|
2022-05-16 18:57:14 +02:00
|
|
|
|
2022-05-25 11:16:10 +02:00
|
|
|
def add_vertex(self, name: str, type: str, lable: Optional[str] = None) -> None:
|
2022-05-25 07:16:56 +02:00
|
|
|
self.dot.node(self.validate_name(name), lable, **apply_vertex_style(type))
|
|
|
|
|
2022-05-25 11:16:10 +02:00
|
|
|
def add_edge(self, head: str, tail: str, type: str, lable: Optional[str] = None) -> None:
|
2022-05-25 07:16:56 +02:00
|
|
|
self.dot.edge(self.validate_name(head), self.validate_name(tail), lable, **apply_edge_style(type))
|
2022-05-16 18:57:14 +02:00
|
|
|
|
|
|
|
def render(self, format: str, cleanup: bool = True) -> None:
|
|
|
|
for service in self.compose.services:
|
2022-05-25 07:16:56 +02:00
|
|
|
if service.image is not None:
|
2022-06-05 08:17:38 +02:00
|
|
|
self.add_vertex(
|
|
|
|
service.name,
|
|
|
|
"service",
|
|
|
|
lable=f"{service.container_name if service.container_name else service.name}\n({service.image})",
|
|
|
|
)
|
2022-05-25 07:16:56 +02:00
|
|
|
if service.extends is not None:
|
2022-05-25 10:18:53 +02:00
|
|
|
self.add_vertex(service.name, "service", lable=f"{service.name}\n")
|
2022-05-25 07:16:56 +02:00
|
|
|
self.add_edge(service.extends.service_name, service.name, "extends")
|
2022-06-05 08:17:38 +02:00
|
|
|
if service.cgroup_parent is not None:
|
|
|
|
self.add_vertex(service.cgroup_parent, "cgroup")
|
|
|
|
self.add_edge(service.cgroup_parent, service.name, "links")
|
|
|
|
|
2022-05-16 18:57:14 +02:00
|
|
|
for network in service.networks:
|
2022-05-25 07:16:56 +02:00
|
|
|
self.add_vertex(network, "network", lable=f"net:{network}")
|
|
|
|
self.add_edge(service.name, network, "links")
|
2022-05-16 18:57:14 +02:00
|
|
|
for volume in service.volumes:
|
2022-05-21 11:41:26 +02:00
|
|
|
self.add_vertex(volume.source, "volume")
|
2022-06-08 17:27:12 +02:00
|
|
|
self.add_edge(
|
|
|
|
service.name,
|
|
|
|
volume.source,
|
|
|
|
"volumes" if "rw" in volume.access_mode else "links",
|
|
|
|
lable=volume.target,
|
|
|
|
)
|
2022-06-05 08:17:38 +02:00
|
|
|
for expose in service.expose:
|
|
|
|
self.add_vertex(expose, "port")
|
2022-06-08 17:25:52 +02:00
|
|
|
self.add_edge(expose, service.name, "exposes")
|
2022-05-16 18:57:14 +02:00
|
|
|
for port in service.ports:
|
2022-05-25 07:16:56 +02:00
|
|
|
self.add_vertex(port.host_port, "port", lable=port.host_port)
|
2022-06-05 08:17:38 +02:00
|
|
|
self.add_edge(port.host_port, service.name, "links", lable=port.container_port)
|
|
|
|
for env_file in service.env_file:
|
|
|
|
self.add_vertex(env_file, "env_file")
|
|
|
|
self.add_edge(env_file, service.name, "env_file")
|
2022-05-25 07:16:56 +02:00
|
|
|
for link in service.links:
|
2022-05-26 03:54:28 +02:00
|
|
|
if ":" in link:
|
|
|
|
service_name, alias = link.split(":", 1)
|
|
|
|
self.add_edge(service_name, service.name, "links", alias)
|
|
|
|
else:
|
|
|
|
self.add_edge(link, service.name, "links")
|
2022-05-16 18:57:14 +02:00
|
|
|
for depends_on in service.depends_on:
|
2022-05-25 07:16:56 +02:00
|
|
|
self.add_edge(service.name, depends_on, "depends_on")
|
2022-06-05 08:17:38 +02:00
|
|
|
for porfile in service.profiles:
|
|
|
|
self.add_vertex(porfile, "porfile")
|
|
|
|
self.add_edge(porfile, service.name, "links")
|
2022-06-08 17:25:52 +02:00
|
|
|
for device in service.devices:
|
|
|
|
self.add_vertex(device.host_path, "device")
|
|
|
|
self.add_edge(
|
|
|
|
device.host_path, service.name, "exposes", f"{device.container_path}\n({device.cgroup_permissions})"
|
|
|
|
)
|
2022-05-16 18:57:14 +02:00
|
|
|
|
2022-05-26 05:06:06 +02:00
|
|
|
self.dot.render(outfile=f"{self.filename}.{format}", format=format, cleanup=cleanup)
|