compose-viz/compose_viz/parser.py

113 lines
4.3 KiB
Python
Raw Normal View History

2022-05-21 16:32:59 +02:00
from typing import Dict, List, Optional
2022-05-18 17:28:18 +02:00
2022-05-16 18:53:29 +02:00
from ruamel.yaml import YAML
2022-05-07 18:42:14 +02:00
2022-05-18 17:28:18 +02:00
from compose_viz.compose import Compose, Service
2022-05-21 08:32:07 +02:00
from compose_viz.extends import Extends
2022-05-21 17:19:01 +02:00
from compose_viz.port import Port
from compose_viz.volume import Volume, VolumeType
2022-05-21 11:47:35 +02:00
2022-05-21 16:32:59 +02:00
2022-05-07 18:42:14 +02:00
class Parser:
def __init__(self):
pass
def parse(self, file_path: str) -> Compose:
2022-05-14 15:30:18 +02:00
# load the yaml file
with open(file_path, "r") as f:
try:
2022-05-18 17:28:18 +02:00
yaml = YAML(typ="safe", pure=True)
2022-05-16 18:53:29 +02:00
yaml_data = yaml.load(f)
2022-05-18 17:28:18 +02:00
except Exception as e:
2022-05-18 18:34:53 +02:00
raise RuntimeError(f"Error parsing file '{file_path}': {e}")
2022-05-14 15:30:18 +02:00
# validate the yaml file
if not yaml_data:
2022-05-18 18:34:53 +02:00
raise RuntimeError("Empty yaml file, aborting.")
2022-05-14 15:30:18 +02:00
if not yaml_data.get("services"):
2022-05-18 18:34:53 +02:00
raise RuntimeError("No services found, aborting.")
2022-05-18 17:28:18 +02:00
2022-05-14 15:30:18 +02:00
# parse services data into Service objects
2022-05-21 10:08:35 +02:00
services = self.parse_service_data(yaml_data["services"])
# create Compose object
compose = Compose(services)
2022-05-18 17:28:18 +02:00
2022-05-21 10:08:35 +02:00
return compose
2022-05-18 17:28:18 +02:00
2022-05-21 16:32:59 +02:00
def parse_service_data(self, services_yaml_data: Dict[str, dict]) -> List[Service]:
services: List[Service] = []
2022-05-21 10:08:35 +02:00
for service, service_name in zip(services_yaml_data.values(), services_yaml_data.keys()):
2022-05-21 16:32:59 +02:00
2022-05-18 17:28:18 +02:00
service_image: Optional[str] = None
2022-05-14 15:30:18 +02:00
if service.get("image"):
service_image = service["image"]
2022-05-21 09:52:11 +02:00
elif service.get("build"):
service_image = "build from " + service["build"]
2022-05-18 17:28:18 +02:00
service_networks: List[str] = []
2022-05-14 15:30:18 +02:00
if service.get("networks"):
2022-05-18 17:28:18 +02:00
if type(service["networks"]) is list:
2022-05-16 19:27:59 +02:00
service_networks = service["networks"]
else:
service_networks = list(service["networks"].keys())
2022-05-21 08:32:07 +02:00
service_extends: Optional[Extends] = None
if service.get("extends"):
service_extends = Extends(service_name=service["extends"]["service"])
2022-05-18 17:28:18 +02:00
2022-05-21 10:08:35 +02:00
service_ports: List[str] = []
if service.get("ports"):
service_ports = service["ports"]
2022-05-18 17:28:18 +02:00
2022-05-21 10:08:35 +02:00
service_depends_on: List[str] = []
if service.get("depends_on"):
service_depends_on = service["depends_on"]
2022-05-14 15:30:18 +02:00
service_volumes: List[Volume] = []
if service.get("volumes"):
for volume_data in service["volumes"]:
if type(volume_data) is dict:
volume_source = volume_data["source"]
volume_target = volume_data["target"]
volume_type = VolumeType[volume_data["type"]]
service_volumes.append(Volume(source=volume_source, target=volume_target, type=volume_type))
elif type(volume_data) is str:
2022-05-21 16:32:59 +02:00
spilt_data = volume_data.split(":", 1)
volume_source = spilt_data[0]
volume_target = spilt_data[1]
service_volumes.append(Volume(source=volume_source, target=volume_target))
2022-05-21 16:00:38 +02:00
service_links: List[str] = []
if service.get("links"):
service_links = service["links"]
2022-05-21 10:08:35 +02:00
services.append(
Service(
name=service_name,
image=service_image,
networks=service_networks,
extends=service_extends,
2022-05-21 17:19:01 +02:00
ports=[
Port(
# TODO: not implemented yet
host_port=service_ports[0],
container_port=service_ports[0],
)
],
2022-05-21 10:08:35 +02:00
depends_on=service_depends_on,
volumes=service_volumes,
2022-05-21 16:32:59 +02:00
links=service_links,
2022-05-21 10:08:35 +02:00
)
)
# Service print debug
2022-05-21 16:32:59 +02:00
# print("--------------------")
# print("Service name: {}".format(service_name))
# print("image: {}".format(service_image))
# print("networks: {}".format(service_networks))
# print("image: {}".format(service_image))
# print("extends: {}".format(service_extends))
# print("ports: {}".format(service_ports))
# print("depends: {}".format(service_depends_on))
2022-05-21 10:08:35 +02:00
return services