Skip to content

Commit

Permalink
fix(compose): compelete compose generation allgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
abolfazl8131 committed Dec 5, 2024
1 parent c61adb3 commit 7db40d2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 74 deletions.
94 changes: 33 additions & 61 deletions app/media/MyCompose/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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
28 changes: 17 additions & 11 deletions app/models/compose_models.py
Original file line number Diff line number Diff line change
@@ -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]]

13 changes: 11 additions & 2 deletions app/template_generators/docker/compose.py
Original file line number Diff line number Diff line change
@@ -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()

29 changes: 29 additions & 0 deletions m.py
Original file line number Diff line number Diff line change
@@ -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


}

}
}

0 comments on commit 7db40d2

Please sign in to comment.