From 7db40d2da6f34222d6c454ec32636ed8cc6317a0 Mon Sep 17 00:00:00 2001 From: abolfazl1381 Date: Thu, 5 Dec 2024 11:04:47 +0330 Subject: [PATCH] fix(compose): compelete compose generation allgorithm --- app/media/MyCompose/docker-compose.yaml | 94 ++++++++--------------- app/models/compose_models.py | 28 ++++--- app/template_generators/docker/compose.py | 13 +++- m.py | 29 +++++++ 4 files changed, 90 insertions(+), 74 deletions(-) create mode 100644 m.py diff --git a/app/media/MyCompose/docker-compose.yaml b/app/media/MyCompose/docker-compose.yaml index 607ae626..19cb7613 100644 --- a/app/media/MyCompose/docker-compose.yaml +++ b/app/media/MyCompose/docker-compose.yaml @@ -1,78 +1,50 @@ -networks: - additionalProp1: - driver: string - additionalProp2: - driver: string - additionalProp3: - driver: string +version: '3' services: - additionalProp1: - args: - additionalProp1: string - additionalProp2: string - additionalProp3: string - build: - context: string - dockerfile: string - command: string - container_name: string - depends_on: - - string + web1: + image: nginx:latest + container_name: web_server environment: - additionalProp1: string - additionalProp2: string - additionalProp3: string - image: string - networks: - - string + foo: bar ports: - - string - volumes: - - string - additionalProp2: - args: - additionalProp1: string - additionalProp2: string - additionalProp3: string + - 80:80 + networks: + - app_network + web2: build: - context: string - dockerfile: string + context: . + dockerfile: DockerFile + image: nginx:latest + container_name: web_server command: string - container_name: string - depends_on: + volumes: - string environment: - additionalProp1: string - additionalProp2: string - additionalProp3: shhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh - image: string - networks: - - string + foo: bar ports: - - string - volumes: - - string - additionalProp3: + - 80:80 + networks: + - app_network args: additionalProp1: string additionalProp2: string additionalProp3: string + depends_on: + - string + web3: build: - context: string - dockerfile: string + context: . + dockerfile: DockerFile + image: nginx:latest + container_name: web_server command: string - container_name: string - depends_on: + volumes: - string environment: - additionalProp1: string - additionalProp2: string - additionalProp3: string - image: string - networks: - - string + foo: bar ports: - - string - volumes: - - string -version: string + - 80:80 + networks: + - app_network +networks: + app: + driver: bridge diff --git a/app/models/compose_models.py b/app/models/compose_models.py index 137dc1f1..62e409f7 100644 --- a/app/models/compose_models.py +++ b/app/models/compose_models.py @@ -1,27 +1,33 @@ from typing import Dict, List, Optional -from pydantic import BaseModel +from pydantic import BaseModel, model_validator class Build(BaseModel): - context: str - dockerfile: str + context: str = "." + dockerfile: str = "DockerFile" class Service(BaseModel): build: Optional[Build] = None - image: Optional[str] = None - container_name: Optional[str] = None + image: Optional[str] = "nginx:latest" + container_name: Optional[str] = "web_server" command: Optional[str] = None volumes: Optional[List[str]] = None - environment: Optional[Dict[str, str]] = None - ports: Optional[List[str]] = None - networks: Optional[List[str]] = None + environment: Optional[Dict[str, str]] = {"foo":"bar"} + ports: Optional[List[str]] = ["80:80"] + networks: Optional[List[str]] = ["app_network"] args: Optional[Dict[str, str]] = None depends_on: Optional[List[str]] = None + @model_validator(mode="after") + def validator(self): + if self.build == None and self.image == None: + raise ValueError(f"one of the build or image sections must be present!") + return self + class Network(BaseModel): - driver: str + driver: str = "bridge" class DockerCompose(BaseModel): - version: str + version: str = "3" services: Dict[str, Service] - networks: Optional[Dict[str, Network]] = None + networks: Optional[Dict[str, Network]] diff --git a/app/template_generators/docker/compose.py b/app/template_generators/docker/compose.py index 2ef066a4..7a11eb31 100644 --- a/app/template_generators/docker/compose.py +++ b/app/template_generators/docker/compose.py @@ -1,19 +1,28 @@ import yaml from app.models.compose_models import DockerCompose import os +def remove_none_values(d): + if isinstance(d, dict): + return {k: remove_none_values(v) for k, v in d.items() if v is not None} + elif isinstance(d, list): + return [remove_none_values(i) for i in d if i is not None] + return d + def docker_compose_generator(input): dir = 'app/media/MyCompose' + compose_total = input.model_dump(mode="json") + compose_total = remove_none_values(compose_total) if not os.path.exists(dir): os.makedirs(dir) os.path.join(dir, 'docker-compose.yaml') file=open("app/media/MyCompose/docker-compose.yaml","w") - yaml.dump(compose_total,file) + yaml.dump(compose_total,file,default_flow_style=False) file.close() file=open("app/media/MyCompose/docker-compose.yaml","w") - yaml.dump(compose_total,file) + yaml.dump(compose_total,file,default_flow_style=False,sort_keys=False) file.close() \ No newline at end of file diff --git a/m.py b/m.py new file mode 100644 index 00000000..976d59bd --- /dev/null +++ b/m.py @@ -0,0 +1,29 @@ +{ + "version": "3", + "services": { + "webserver": { + "build": { + "context": ".", + "dockerfile": "DockerFile" + }, + "image": null, + "container_name": "web_server", + "command": null, + "volumes": null, + "environment": { + "foo": "bar" + }, + "ports": [ + "80:80" + ], + "networks": [ + "app_network" + ], + "args": null, + "depends_on": null + + + } + +} +} \ No newline at end of file