2022-05-22 06:34:18 +02:00
|
|
|
import re
|
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-27 08:17:04 +02:00
|
|
|
from compose_viz.models.compose import Compose, Service
|
|
|
|
from compose_viz.models.extends import Extends
|
|
|
|
from compose_viz.models.port import Port, Protocol
|
|
|
|
from compose_viz.models.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-24 10:02:47 +02:00
|
|
|
if service.get("build"):
|
|
|
|
if type(service["build"]) is str:
|
2022-05-25 11:47:23 +02:00
|
|
|
service_image = f"build from '{service['build']}'"
|
2022-05-24 10:02:47 +02:00
|
|
|
elif type(service["build"]) is dict:
|
2022-05-25 11:47:23 +02:00
|
|
|
if service["build"].get("context") and service["build"].get("dockerfile"):
|
|
|
|
service_image = (
|
|
|
|
f"build from '{service['build']['context']}' using '{service['build']['dockerfile']}'"
|
|
|
|
)
|
|
|
|
elif service["build"].get("context"):
|
|
|
|
service_image = f"build from '{service['build']['context']}'"
|
|
|
|
if service.get("image"):
|
|
|
|
if service_image:
|
|
|
|
service_image += ", image: " + service["image"]
|
|
|
|
else:
|
|
|
|
service_image = service["image"]
|
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"]
|
2022-05-21 19:57:21 +02:00
|
|
|
elif type(service["networks"]) is dict:
|
2022-05-16 19:27:59 +02:00
|
|
|
service_networks = list(service["networks"].keys())
|
2022-05-21 08:32:07 +02:00
|
|
|
|
|
|
|
service_extends: Optional[Extends] = None
|
|
|
|
if service.get("extends"):
|
2022-05-24 10:02:47 +02:00
|
|
|
assert type(service["extends"]) is dict, "Invalid extends format, aborting."
|
|
|
|
assert service["extends"]["service"], "Missing extends service, aborting."
|
|
|
|
extend_service_name = str(service["extends"]["service"])
|
|
|
|
|
|
|
|
extend_from_file: Optional[str] = None
|
|
|
|
if service["extends"].get("file"):
|
|
|
|
assert service["extends"]["file"], "Missing extends file, aborting."
|
|
|
|
extend_from_file = str(service["extends"]["file"])
|
|
|
|
|
|
|
|
service_extends = Extends(service_name=extend_service_name, from_file=extend_from_file)
|
2022-05-18 17:28:18 +02:00
|
|
|
|
2022-05-21 19:57:21 +02:00
|
|
|
service_ports: List[Port] = []
|
2022-05-21 10:08:35 +02:00
|
|
|
if service.get("ports"):
|
2022-05-22 06:34:18 +02:00
|
|
|
assert type(service["ports"]) is list
|
|
|
|
for port_data in service["ports"]:
|
|
|
|
if type(port_data) is dict:
|
|
|
|
# define a nested function to limit variable scope
|
|
|
|
def long_syntax():
|
2022-05-22 20:41:35 +02:00
|
|
|
assert type(port_data) is dict
|
2022-05-22 06:34:18 +02:00
|
|
|
assert port_data["target"]
|
|
|
|
|
2022-05-22 20:41:35 +02:00
|
|
|
container_port: str = str(port_data["target"])
|
2022-05-22 06:34:18 +02:00
|
|
|
host_port: str = ""
|
2022-05-24 10:02:47 +02:00
|
|
|
protocol: Protocol = Protocol.any
|
2022-05-22 06:34:18 +02:00
|
|
|
|
2022-05-22 20:41:35 +02:00
|
|
|
if port_data.get("published"):
|
|
|
|
host_port = str(port_data["published"])
|
2022-05-22 06:34:18 +02:00
|
|
|
else:
|
|
|
|
host_port = container_port
|
|
|
|
|
|
|
|
if port_data.get("host_ip"):
|
|
|
|
host_ip = str(port_data["host_ip"])
|
|
|
|
host_port = f"{host_ip}:{host_port}"
|
2022-05-24 10:02:47 +02:00
|
|
|
else:
|
|
|
|
host_port = f"0.0.0.0:{host_port}"
|
2022-05-22 06:34:18 +02:00
|
|
|
|
|
|
|
if port_data.get("protocol"):
|
2022-05-22 20:41:35 +02:00
|
|
|
protocol = Protocol[str(port_data["protocol"])]
|
2022-05-22 06:34:18 +02:00
|
|
|
|
|
|
|
assert host_port, "Error parsing port, aborting."
|
|
|
|
|
|
|
|
service_ports.append(
|
|
|
|
Port(
|
|
|
|
host_port=host_port,
|
|
|
|
container_port=container_port,
|
|
|
|
protocol=protocol,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
long_syntax()
|
|
|
|
elif type(port_data) is str:
|
|
|
|
# ports that needs to parse using regex:
|
|
|
|
# - "3000"
|
|
|
|
# - "3000-3005"
|
|
|
|
# - "8000:8000"
|
|
|
|
# - "9090-9091:8080-8081"
|
|
|
|
# - "49100:22"
|
|
|
|
# - "127.0.0.1:8001:8001"
|
|
|
|
# - "127.0.0.1:5000-5010:5000-5010"
|
|
|
|
# - "6060:6060/udp"
|
|
|
|
|
|
|
|
def short_syntax():
|
2022-05-22 20:41:35 +02:00
|
|
|
assert type(port_data) is str
|
2022-05-22 06:34:18 +02:00
|
|
|
regex = r"(?P<host_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:)?((?P<host_port>\d+(\-\d+)?):)?((?P<container_port>\d+(\-\d+)?))?(/(?P<protocol>\w+))?" # noqa: E501
|
|
|
|
match = re.match(regex, port_data)
|
|
|
|
if match:
|
|
|
|
host_ip: Optional[str] = match.group("host_ip")
|
|
|
|
host_port: Optional[str] = match.group("host_port")
|
|
|
|
container_port: Optional[str] = match.group("container_port")
|
|
|
|
protocol: Optional[str] = match.group("protocol")
|
|
|
|
|
|
|
|
assert container_port, "Invalid port format, aborting."
|
|
|
|
|
|
|
|
if container_port and not host_port:
|
|
|
|
host_port = container_port
|
|
|
|
|
|
|
|
if host_ip:
|
|
|
|
host_port = f"{host_ip}{host_port}"
|
2022-05-24 10:02:47 +02:00
|
|
|
else:
|
|
|
|
host_port = f"0.0.0.0:{host_port}"
|
2022-05-22 06:34:18 +02:00
|
|
|
|
|
|
|
assert host_port, "Error while parsing port, aborting."
|
|
|
|
|
|
|
|
if protocol:
|
|
|
|
service_ports.append(
|
|
|
|
Port(
|
|
|
|
host_port=host_port,
|
|
|
|
container_port=container_port,
|
|
|
|
protocol=Protocol[protocol],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
service_ports.append(
|
|
|
|
Port(
|
|
|
|
host_port=host_port,
|
|
|
|
container_port=container_port,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
short_syntax()
|
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"):
|
2022-05-23 17:24:20 +02:00
|
|
|
if type(service["depends_on"]) is list:
|
|
|
|
for depends_on in service["depends_on"]:
|
|
|
|
service_depends_on.append(str(depends_on))
|
|
|
|
elif type(service["depends_on"]) is dict:
|
|
|
|
service_depends_on = list(service["depends_on"].keys())
|
2022-05-14 15:30:18 +02:00
|
|
|
|
2022-05-21 15:57:18 +02:00
|
|
|
service_volumes: List[Volume] = []
|
|
|
|
if service.get("volumes"):
|
2022-05-23 17:24:20 +02:00
|
|
|
assert type(service["volumes"]) is list
|
2022-05-21 15:57:18 +02:00
|
|
|
for volume_data in service["volumes"]:
|
|
|
|
if type(volume_data) is dict:
|
2022-05-22 06:34:18 +02:00
|
|
|
assert volume_data["source"] and volume_data["target"], "Invalid volume input, aborting."
|
|
|
|
|
2022-05-22 20:41:35 +02:00
|
|
|
volume_source: str = str(volume_data["source"])
|
|
|
|
volume_target: str = str(volume_data["target"])
|
2022-05-22 06:34:18 +02:00
|
|
|
volume_type: VolumeType = VolumeType.volume
|
|
|
|
|
2022-05-21 19:57:21 +02:00
|
|
|
if volume_data.get("type"):
|
2022-05-22 20:41:35 +02:00
|
|
|
volume_type = VolumeType[str(volume_data["type"])]
|
2022-05-22 06:34:18 +02:00
|
|
|
|
|
|
|
service_volumes.append(Volume(source=volume_source, target=volume_target, type=volume_type))
|
2022-05-21 15:57:18 +02:00
|
|
|
elif type(volume_data) is str:
|
2022-05-22 06:34:18 +02:00
|
|
|
assert ":" in volume_data, "Invalid volume input, aborting."
|
|
|
|
|
|
|
|
spilt_data = volume_data.split(":")
|
|
|
|
if len(spilt_data) == 2:
|
|
|
|
service_volumes.append(Volume(source=spilt_data[0], target=spilt_data[1]))
|
|
|
|
elif len(spilt_data) == 3:
|
|
|
|
service_volumes.append(
|
2022-05-24 11:06:31 +02:00
|
|
|
Volume(source=spilt_data[0], target=spilt_data[1], access_mode=spilt_data[2])
|
2022-05-22 06:34:18 +02:00
|
|
|
)
|
2022-05-21 15:57:18 +02:00
|
|
|
|
2022-05-21 16:00:38 +02:00
|
|
|
service_links: List[str] = []
|
|
|
|
if service.get("links"):
|
|
|
|
service_links = service["links"]
|
2022-05-21 15:57:18 +02:00
|
|
|
|
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 19:50:07 +02:00
|
|
|
ports=service_ports,
|
2022-05-21 10:08:35 +02:00
|
|
|
depends_on=service_depends_on,
|
2022-05-21 15:57:18 +02:00
|
|
|
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
|