forked from devopshobbies/devops-gpt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compose): compelete compose generation allgorithm
- Loading branch information
1 parent
c61adb3
commit 7db40d2
Showing
4 changed files
with
90 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
||
} | ||
|
||
} | ||
} |