2022-05-14 15:30:18 +02:00
|
|
|
from re import S
|
2022-05-07 18:42:14 +02:00
|
|
|
from compose_viz.compose import Compose
|
2022-05-14 15:30:18 +02:00
|
|
|
from compose_viz.compose import Service
|
2022-05-16 18:53:29 +02:00
|
|
|
from ruamel.yaml import YAML
|
2022-05-07 18:42:14 +02:00
|
|
|
|
2022-05-14 20:27:33 +02:00
|
|
|
|
2022-05-07 18:42:14 +02:00
|
|
|
class Parser:
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def parse(self, file_path: str) -> Compose:
|
|
|
|
# validate input file using `docker-compose config -q sys.argv[1]` first
|
2022-05-14 15:30:18 +02:00
|
|
|
# load the yaml file
|
|
|
|
with open(file_path, "r") as f:
|
|
|
|
try:
|
2022-05-16 18:53:29 +02:00
|
|
|
yaml = YAML()
|
|
|
|
yaml_data = yaml.load(f)
|
|
|
|
except YAML.YAMLError as exc:
|
|
|
|
raise YAML.YAMLError
|
2022-05-14 15:30:18 +02:00
|
|
|
# validate the yaml file
|
|
|
|
if not yaml_data:
|
|
|
|
print("Error: empty yaml file")
|
|
|
|
raise ValueError
|
|
|
|
if not yaml_data.get("services"):
|
|
|
|
print("Error: no services found")
|
|
|
|
raise ValueError
|
|
|
|
# parse services data into Service objects
|
|
|
|
services_data = yaml_data["services"]
|
|
|
|
services = []
|
|
|
|
for service, service_name in zip(services_data.values(), services_data.keys()):
|
|
|
|
#print("name: {}".format(service_name))
|
|
|
|
if service.get("image"):
|
|
|
|
service_image = service["image"]
|
|
|
|
#print("image: {}".format(service_image))
|
|
|
|
if service.get("networks"):
|
|
|
|
service_networks = service["networks"]
|
|
|
|
#print("networks: {}".format(service_networks))
|
|
|
|
services.append(Service(
|
|
|
|
name=service_name,
|
|
|
|
image=service_image,
|
|
|
|
networks=service_networks,
|
|
|
|
))
|
|
|
|
# create Compose object
|
|
|
|
compose = Compose(services)
|
|
|
|
|
|
|
|
return compose
|