feat: implement Volume

This commit is contained in:
Xyphuz 2022-05-21 17:41:26 +08:00
parent 9f9733bec9
commit d5a9bf3f72
4 changed files with 288 additions and 37 deletions

View file

@ -61,8 +61,8 @@ class Graph:
self.add_vertex("net#" + network, "network") self.add_vertex("net#" + network, "network")
self.add_edge(service.name, "net#" + network, "links") self.add_edge(service.name, "net#" + network, "links")
for volume in service.volumes: for volume in service.volumes:
self.add_vertex(volume, "volume") self.add_vertex(volume.source, "volume")
self.add_edge(service.name, volume, "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, "port")
self.add_edge(service.name, port, "ports") self.add_edge(service.name, port, "ports")

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.volume import Volume
class Service: class Service:
@ -10,7 +11,7 @@ class Service:
image: Optional[str] = None, image: Optional[str] = None,
ports: List[str] = [], ports: List[str] = [],
networks: List[str] = [], networks: List[str] = [],
volumes: List[str] = [], volumes: List[Volume] = [],
depends_on: List[str] = [], depends_on: List[str] = [],
links: List[str] = [], links: List[str] = [],
extends: Optional[Extends] = None, extends: Optional[Extends] = None,

26
compose_viz/volume.py Normal file
View file

@ -0,0 +1,26 @@
from enum import Enum
class VolumeType(str, Enum):
volume = "volume"
bind = "bind"
tmpfs = "tmpfs"
class Volume:
def __init__(self, source: str, target: str, type: VolumeType = VolumeType.volume):
self._source = source
self._target = target
self._type = type
@property
def source(self):
return self._source
@property
def target(self):
return self._target
@property
def type(self):
return self._type

View file

@ -4,6 +4,7 @@ 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.service import Service from compose_viz.service import Service
from compose_viz.volume import Volume, VolumeType
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -345,7 +346,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
), ),
] ]
), ),
@ -368,7 +376,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
), ),
] ]
), ),
@ -380,12 +395,12 @@ from compose_viz.service import Service
Service( Service(
name="common", name="common",
image="busybox", image="busybox",
volumes=["common-volume"], volumes=[Volume(source="common-volume", target="/var/lib/backup/data:rw")],
), ),
Service( Service(
name="cli", name="cli",
extends=Extends(service_name="common"), extends=Extends(service_name="common"),
volumes=["cli-volume"], volumes=[Volume(source="cli-volume", target="/var/lib/backup/data:ro")],
), ),
] ]
), ),
@ -407,7 +422,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
), ),
] ]
@ -420,7 +442,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
ports=["8000:5000"], ports=["8000:5000"],
), ),
] ]
@ -444,7 +473,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
ports=["8000:5000"], ports=["8000:5000"],
), ),
] ]
@ -464,7 +500,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
ports=["8000:5000"], ports=["8000:5000"],
), ),
@ -488,7 +531,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
ports=["8000:5000"], ports=["8000:5000"],
), ),
@ -507,7 +557,14 @@ from compose_viz.service import Service
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
depends_on=["backend"], depends_on=["backend"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
), ),
Service( Service(
name="backend", name="backend",
@ -534,7 +591,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
), ),
] ]
@ -554,7 +618,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
), ),
@ -578,7 +649,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
), ),
@ -596,7 +674,14 @@ from compose_viz.service import Service
Service( Service(
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["backend"], depends_on=["backend"],
), ),
Service( Service(
@ -625,7 +710,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=["8000:5010"],
), ),
@ -646,7 +738,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=["8000:5010"],
@ -671,7 +770,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="monitoring"), extends=Extends(service_name="monitoring"),
ports=["8000:5010"], ports=["8000:5010"],
@ -1106,7 +1212,14 @@ from compose_viz.service import Service
Service( Service(
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1138,7 +1251,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1162,7 +1282,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
links=["db:database"], links=["db:database"],
), ),
@ -1190,7 +1317,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
links=["db:database"], links=["db:database"],
), ),
@ -1216,7 +1350,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
), ),
@ -1245,7 +1386,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
), ),
@ -1270,7 +1418,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
@ -1299,7 +1454,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
@ -1323,7 +1485,14 @@ from compose_viz.service import Service
name="monitoring", name="monitoring",
image="awesome/monitoring", image="awesome/monitoring",
depends_on=["backend"], depends_on=["backend"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
links=["db:database"], links=["db:database"],
), ),
Service( Service(
@ -1355,7 +1524,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
links=["db:database"], links=["db:database"],
), ),
@ -1380,7 +1556,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
links=["db:database"], links=["db:database"],
@ -1409,7 +1592,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
links=["db:database"], links=["db:database"],
@ -1436,7 +1626,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
@ -1466,7 +1663,14 @@ from compose_viz.service import Service
name="backend", name="backend",
image="awesome/backend", image="awesome/backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
ports=["8000:5010"], ports=["8000:5010"],
links=["db:database"], links=["db:database"],
@ -1492,7 +1696,14 @@ from compose_viz.service import Service
), ),
Service( Service(
name="backend", name="backend",
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=["8000:5010"],
@ -1522,7 +1733,14 @@ from compose_viz.service import Service
Service( Service(
name="backend", name="backend",
networks=["back-tier", "admin"], networks=["back-tier", "admin"],
volumes=["db-data"], volumes=[
Volume(source="db-data", target="/data"),
Volume(
source="/var/run/postgres/postgres.sock",
target="/var/run/postgres/postgres.sock",
type=VolumeType.bind,
),
],
depends_on=["monitoring"], depends_on=["monitoring"],
extends=Extends(service_name="frontend"), extends=Extends(service_name="frontend"),
ports=["8000:5010"], ports=["8000:5010"],
@ -1548,7 +1766,13 @@ def test_parse_file(test_input: str, expected: Compose) -> None:
assert actual_service.image == expected_service.image assert actual_service.image == expected_service.image
assert actual_service.ports == expected_service.ports assert actual_service.ports == expected_service.ports
assert actual_service.networks == expected_service.networks assert actual_service.networks == expected_service.networks
assert actual_service.volumes == expected_service.volumes
assert len(actual_service.volumes) == len(expected_service.volumes)
for actual_volume, expected_volume in zip(actual_service.volumes, expected_service.volumes):
assert actual_volume.source == expected_volume.source
assert actual_volume.target == expected_volume.target
assert actual_volume.type == expected_volume.type
assert actual_service.depends_on == expected_service.depends_on assert actual_service.depends_on == expected_service.depends_on
assert actual_service.links == expected_service.links assert actual_service.links == expected_service.links