Merge pull request #10 from compose-viz/dev-parser
feat: implement full parser
This commit is contained in:
commit
10f2f0d326
2 changed files with 63 additions and 22 deletions
|
@ -1,9 +1,10 @@
|
||||||
from typing import List, Optional
|
from typing import Dict, List, Optional
|
||||||
|
|
||||||
from ruamel.yaml import YAML
|
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.volume import Volume, VolumeType
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
|
@ -18,25 +19,29 @@ class Parser:
|
||||||
yaml_data = yaml.load(f)
|
yaml_data = yaml.load(f)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise RuntimeError(f"Error parsing file '{file_path}': {e}")
|
raise RuntimeError(f"Error parsing file '{file_path}': {e}")
|
||||||
|
|
||||||
# validate the yaml file
|
# validate the yaml file
|
||||||
if not yaml_data:
|
if not yaml_data:
|
||||||
raise RuntimeError("Empty yaml file, aborting.")
|
raise RuntimeError("Empty yaml file, aborting.")
|
||||||
|
|
||||||
if not yaml_data.get("services"):
|
if not yaml_data.get("services"):
|
||||||
raise RuntimeError("No services found, aborting.")
|
raise RuntimeError("No services found, aborting.")
|
||||||
|
|
||||||
# parse services data into Service objects
|
# parse services data into Service objects
|
||||||
services_data = yaml_data["services"]
|
services = self.parse_service_data(yaml_data["services"])
|
||||||
services = []
|
|
||||||
|
|
||||||
for service, service_name in zip(services_data.values(), services_data.keys()):
|
# create Compose object
|
||||||
print("name: {}".format(service_name))
|
compose = Compose(services)
|
||||||
|
|
||||||
|
return compose
|
||||||
|
|
||||||
|
def parse_service_data(self, services_yaml_data: Dict[str, dict]) -> List[Service]:
|
||||||
|
services: List[Service] = []
|
||||||
|
for service, service_name in zip(services_yaml_data.values(), services_yaml_data.keys()):
|
||||||
|
|
||||||
service_image: Optional[str] = None
|
service_image: Optional[str] = None
|
||||||
if service.get("image"):
|
if service.get("image"):
|
||||||
service_image = service["image"]
|
service_image = service["image"]
|
||||||
print("image: {}".format(service_image))
|
elif service.get("build"):
|
||||||
|
service_image = "build from " + service["build"]
|
||||||
|
|
||||||
service_networks: List[str] = []
|
service_networks: List[str] = []
|
||||||
if service.get("networks"):
|
if service.get("networks"):
|
||||||
|
@ -44,23 +49,57 @@ class Parser:
|
||||||
service_networks = service["networks"]
|
service_networks = service["networks"]
|
||||||
else:
|
else:
|
||||||
service_networks = list(service["networks"].keys())
|
service_networks = list(service["networks"].keys())
|
||||||
print("networks: {}".format(service_networks))
|
|
||||||
|
|
||||||
service_image: Optional[str] = None
|
|
||||||
if service.get("image"):
|
|
||||||
service_image = service["image"]
|
|
||||||
print("image: {}".format(service_image))
|
|
||||||
|
|
||||||
service_extends: Optional[Extends] = None
|
service_extends: Optional[Extends] = None
|
||||||
if service.get("extends"):
|
if service.get("extends"):
|
||||||
service_extends = Extends(service_name=service["extends"]["service"])
|
service_extends = Extends(service_name=service["extends"]["service"])
|
||||||
print("extends: {}".format(service_extends))
|
|
||||||
|
service_ports: List[str] = []
|
||||||
|
if service.get("ports"):
|
||||||
|
service_ports = service["ports"]
|
||||||
|
|
||||||
|
service_depends_on: List[str] = []
|
||||||
|
if service.get("depends_on"):
|
||||||
|
service_depends_on = service["depends_on"]
|
||||||
|
|
||||||
|
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:
|
||||||
|
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))
|
||||||
|
|
||||||
|
service_links: List[str] = []
|
||||||
|
if service.get("links"):
|
||||||
|
service_links = service["links"]
|
||||||
|
|
||||||
services.append(
|
services.append(
|
||||||
Service(name=service_name, image=service_image, networks=service_networks, extends=service_extends)
|
Service(
|
||||||
|
name=service_name,
|
||||||
|
image=service_image,
|
||||||
|
networks=service_networks,
|
||||||
|
extends=service_extends,
|
||||||
|
ports=service_ports,
|
||||||
|
depends_on=service_depends_on,
|
||||||
|
volumes=service_volumes,
|
||||||
|
links=service_links,
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
# Service print debug
|
||||||
|
# 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))
|
||||||
|
|
||||||
# create Compose object
|
return services
|
||||||
compose = Compose(services)
|
|
||||||
|
|
||||||
return compose
|
|
||||||
|
|
|
@ -309,7 +309,7 @@ from compose_viz.volume import Volume, VolumeType
|
||||||
),
|
),
|
||||||
Service(
|
Service(
|
||||||
name="backend",
|
name="backend",
|
||||||
image="awesome/backend",
|
extends=Extends(service_name="frontend"),
|
||||||
ports=["8000:5001"],
|
ports=["8000:5001"],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -670,10 +670,12 @@ from compose_viz.volume import Volume, VolumeType
|
||||||
Service(
|
Service(
|
||||||
name="frontend",
|
name="frontend",
|
||||||
image="awesome/webapp",
|
image="awesome/webapp",
|
||||||
|
ports=["8000:5000"],
|
||||||
),
|
),
|
||||||
Service(
|
Service(
|
||||||
name="monitoring",
|
name="monitoring",
|
||||||
image="awesome/monitoring",
|
image="awesome/monitoring",
|
||||||
|
ports=["8000:5010"],
|
||||||
volumes=[
|
volumes=[
|
||||||
Volume(source="db-data", target="/data"),
|
Volume(source="db-data", target="/data"),
|
||||||
Volume(
|
Volume(
|
||||||
|
@ -779,7 +781,7 @@ from compose_viz.volume import Volume, VolumeType
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
depends_on=["monitoring"],
|
depends_on=["monitoring"],
|
||||||
extends=Extends(service_name="monitoring"),
|
extends=Extends(service_name="frontend"),
|
||||||
ports=["8000:5010"],
|
ports=["8000:5010"],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in a new issue