feat: implement Port

This commit is contained in:
Xyphuz 2022-05-21 23:19:01 +08:00
parent 5d090fca45
commit 2b9ddaa865
5 changed files with 171 additions and 47 deletions

View file

@ -64,8 +64,8 @@ class Graph:
self.add_vertex(volume.source, "volume") self.add_vertex(volume.source, "volume")
self.add_edge(service.name, volume.source, "links") self.add_edge(service.name, volume.source, "links")
for port in service.ports: for port in service.ports:
self.add_vertex(port, "port") self.add_vertex(port.host_port, "port")
self.add_edge(service.name, port, "ports") self.add_edge(service.name, port.host_port, "ports")
for depends_on in service.depends_on: for depends_on in service.depends_on:
self.dot.edge(depends_on, service.name, "depends_on") self.dot.edge(depends_on, service.name, "depends_on")

View file

@ -4,6 +4,7 @@ from ruamel.yaml import YAML
from compose_viz.compose import Compose, Service from compose_viz.compose import Compose, Service
from compose_viz.extends import Extends from compose_viz.extends import Extends
from compose_viz.port import Port
from compose_viz.volume import Volume, VolumeType from compose_viz.volume import Volume, VolumeType
@ -86,7 +87,13 @@ class Parser:
image=service_image, image=service_image,
networks=service_networks, networks=service_networks,
extends=service_extends, extends=service_extends,
ports=service_ports, ports=[
Port(
# TODO: not implemented yet
host_port=service_ports[0],
container_port=service_ports[0],
)
],
depends_on=service_depends_on, depends_on=service_depends_on,
volumes=service_volumes, volumes=service_volumes,
links=service_links, links=service_links,

25
compose_viz/port.py Normal file
View file

@ -0,0 +1,25 @@
from enum import Enum
class Protocol(str, Enum):
tcp = "tcp"
udp = "udp"
class Port:
def __init__(self, host_port: str, container_port: str, protocol: Protocol = Protocol.tcp):
self._host_port = host_port
self._container_port = container_port
self._protocol = protocol
@property
def host_port(self):
return self._host_port
@property
def container_port(self):
return self._container_port
@property
def protocol(self):
return self._protocol

View file

@ -1,6 +1,7 @@
from typing import List, Optional from typing import List, Optional
from compose_viz.extends import Extends from compose_viz.extends import Extends
from compose_viz.port import Port
from compose_viz.volume import Volume from compose_viz.volume import Volume
@ -9,7 +10,7 @@ class Service:
self, self,
name: str, name: str,
image: Optional[str] = None, image: Optional[str] = None,
ports: List[str] = [], ports: List[Port] = [],
networks: List[str] = [], networks: List[str] = [],
volumes: List[Volume] = [], volumes: List[Volume] = [],
depends_on: List[str] = [], depends_on: List[str] = [],

View file

@ -3,6 +3,7 @@ import pytest
from compose_viz.compose import Compose from compose_viz.compose import Compose
from compose_viz.extends import Extends from compose_viz.extends import Extends
from compose_viz.parser import Parser from compose_viz.parser import Parser
from compose_viz.port import Port
from compose_viz.service import Service from compose_viz.service import Service
from compose_viz.volume import Volume, VolumeType from compose_viz.volume import Volume, VolumeType
@ -80,7 +81,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="web", name="web",
image="build from .", image="build from .",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
Service( Service(
name="redis", name="redis",
@ -96,19 +99,25 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
networks=["front-tier", "back-tier"], networks=["front-tier", "back-tier"],
), ),
Service( Service(
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
networks=["admin"], networks=["admin"],
), ),
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
), ),
] ]
@ -121,7 +130,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
Service( Service(
name="monitoring", name="monitoring",
@ -130,7 +141,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="backend", name="backend",
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
), ),
] ]
), ),
@ -142,7 +155,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
networks=["front-tier", "back-tier"], networks=["front-tier", "back-tier"],
), ),
Service( Service(
@ -152,7 +167,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
Service( Service(
name="backend", name="backend",
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
), ),
@ -252,18 +269,24 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
Service( Service(
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
depends_on=["backend"], depends_on=["backend"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
), ),
] ]
), ),
@ -282,7 +305,9 @@ from compose_viz.volume import Volume, VolumeType
image="awesome/monitoring", image="awesome/monitoring",
networks=["admin"], networks=["admin"],
depends_on=["backend"], depends_on=["backend"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
Service( Service(
name="backend", name="backend",
@ -299,18 +324,24 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
Service( Service(
name="monitoring", name="monitoring",
depends_on=["backend"], depends_on=["backend"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
Service( Service(
name="backend", name="backend",
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
), ),
] ]
), ),
@ -329,7 +360,9 @@ from compose_viz.volume import Volume, VolumeType
networks=["admin"], networks=["admin"],
depends_on=["backend"], depends_on=["backend"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
Service( Service(
name="backend", name="backend",
@ -450,7 +483,9 @@ from compose_viz.volume import Volume, VolumeType
type=VolumeType.bind, type=VolumeType.bind,
), ),
], ],
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
] ]
), ),
@ -481,7 +516,9 @@ from compose_viz.volume import Volume, VolumeType
type=VolumeType.bind, type=VolumeType.bind,
), ),
], ],
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
] ]
), ),
@ -509,7 +546,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
] ]
), ),
@ -540,7 +579,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
] ]
), ),
@ -670,12 +711,16 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="frontend", name="frontend",
image="awesome/webapp", image="awesome/webapp",
ports=["8000:5000"], ports=[
Port(host_port="8000", container_port="5000"),
],
), ),
Service( Service(
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
volumes=[ volumes=[
Volume(source="db-data", target="/data"), Volume(source="db-data", target="/data"),
Volume( Volume(
@ -689,7 +734,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
ports=["8000:5001"], ports=[
Port(host_port="8000", container_port="5001"),
],
), ),
] ]
), ),
@ -721,7 +768,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
] ]
), ),
@ -750,7 +799,9 @@ from compose_viz.volume import Volume, VolumeType
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
] ]
), ),
@ -782,7 +833,9 @@ from compose_viz.volume import Volume, VolumeType
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
), ),
] ]
), ),
@ -896,7 +949,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -924,7 +979,9 @@ from compose_viz.volume import Volume, VolumeType
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -949,7 +1006,9 @@ from compose_viz.volume import Volume, VolumeType
Service( Service(
name="backend", name="backend",
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -977,7 +1036,9 @@ from compose_viz.volume import Volume, VolumeType
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1109,7 +1170,9 @@ from compose_viz.volume import Volume, VolumeType
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1138,7 +1201,9 @@ from compose_viz.volume import Volume, VolumeType
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1164,7 +1229,9 @@ from compose_viz.volume import Volume, VolumeType
name="backend", name="backend",
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1193,7 +1260,9 @@ from compose_viz.volume import Volume, VolumeType
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1360,7 +1429,9 @@ from compose_viz.volume import Volume, VolumeType
type=VolumeType.bind, type=VolumeType.bind,
), ),
], ],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1396,7 +1467,9 @@ from compose_viz.volume import Volume, VolumeType
type=VolumeType.bind, type=VolumeType.bind,
), ),
], ],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1429,7 +1502,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1465,7 +1540,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1637,7 +1714,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1674,7 +1753,9 @@ from compose_viz.volume import Volume, VolumeType
), ),
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1708,7 +1789,9 @@ from compose_viz.volume import Volume, VolumeType
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1745,7 +1828,9 @@ from compose_viz.volume import Volume, VolumeType
], ],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=[
Port(host_port="8000", container_port="5010"),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1766,7 +1851,13 @@ def test_parse_file(test_input: str, expected: Compose) -> None:
for actual_service, expected_service in zip(actual.services, expected.services): for actual_service, expected_service in zip(actual.services, expected.services):
assert actual_service.name == expected_service.name assert actual_service.name == expected_service.name
assert actual_service.image == expected_service.image assert actual_service.image == expected_service.image
assert actual_service.ports == expected_service.ports
assert len(actual_service.ports) == len(expected_service.ports)
for actual_port, expected_port in zip(actual_service.ports, expected_service.ports):
assert actual_port.host_port == expected_port.host_port
assert actual_port.container_port == expected_port.container_port
assert actual_port.protocol == expected_port.protocol
assert actual_service.networks == expected_service.networks assert actual_service.networks == expected_service.networks
assert len(actual_service.volumes) == len(expected_service.volumes) assert len(actual_service.volumes) == len(expected_service.volumes)