2022-05-16 18:57:14 +02:00
|
|
|
import graphviz
|
|
|
|
|
|
|
|
from compose_viz.compose import Compose
|
|
|
|
|
|
|
|
|
|
|
|
def apply_vertex_style(type) -> dict:
|
|
|
|
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": {
|
|
|
|
"shape": "folder",
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return style[type]
|
|
|
|
|
|
|
|
|
|
|
|
def apply_edge_style(type) -> dict:
|
|
|
|
style = {
|
2022-05-18 17:28:18 +02:00
|
|
|
"ports": {
|
|
|
|
"style": "solid",
|
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",
|
|
|
|
},
|
|
|
|
"depends_on": {
|
|
|
|
"style": "dotted",
|
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
|
|
|
|
|
|
|
|
def add_vertex(self, name: str, type: str) -> None:
|
|
|
|
self.dot.node(name, **apply_vertex_style(type))
|
|
|
|
|
|
|
|
def add_edge(self, head: str, tail: str, type: str) -> None:
|
|
|
|
self.dot.edge(head, tail, **apply_edge_style(type))
|
|
|
|
|
|
|
|
def render(self, format: str, cleanup: bool = True) -> None:
|
|
|
|
for service in self.compose.services:
|
2022-05-18 17:28:18 +02:00
|
|
|
self.add_vertex(service.name, "service")
|
2022-05-16 18:57:14 +02:00
|
|
|
for network in service.networks:
|
2022-05-18 17:28:18 +02:00
|
|
|
self.add_vertex("net#" + network, "network")
|
|
|
|
self.add_edge(service.name, "net#" + 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")
|
|
|
|
self.add_edge(service.name, volume.source, "links")
|
2022-05-16 18:57:14 +02:00
|
|
|
for port in service.ports:
|
2022-05-18 17:28:18 +02:00
|
|
|
self.add_vertex(port, "port")
|
|
|
|
self.add_edge(service.name, port, "ports")
|
2022-05-16 18:57:14 +02:00
|
|
|
for depends_on in service.depends_on:
|
2022-05-18 17:28:18 +02:00
|
|
|
self.dot.edge(depends_on, service.name, "depends_on")
|
2022-05-16 18:57:14 +02:00
|
|
|
|
|
|
|
self.dot.render(outfile=self.filename, format=format, cleanup=cleanup)
|