Skip to content

Commit 67e63bb

Browse files
committed
fix(compose): totally restructre docker compose generator
1 parent 5c60edc commit 67e63bb

File tree

6 files changed

+102
-177
lines changed

6 files changed

+102
-177
lines changed
Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +0,0 @@
1-
import os
2-
3-
project_name = "app/media/MyCompose"
4-
compose_file_path = os.path.join(project_name, "docker-compose.yaml")
5-
6-
# Create project directories
7-
os.makedirs(project_name, exist_ok=True)
8-
9-
# Create docker-compose.yaml
10-
with open(compose_file_path, "w") as compose_file:
11-
compose_file.write("version: '3'\n")
12-
compose_file.write("services:\n")
13-
compose_file.write(" web_server:\n")
14-
compose_file.write(" image: nginx:latest\n")
15-
compose_file.write(" volumes:\n")
16-
compose_file.write(" - ./nginx/nginx.conf:/etc/nginx/nginx.conf\n")
17-
compose_file.write(" depends_on:\n")
18-
compose_file.write(" - string\n")
19-
compose_file.write(" ports:\n")
20-
compose_file.write(" - '80:80'\n")
21-
compose_file.write(" environment:\n")
22-
compose_file.write(" - foo=bar\n")
23-
compose_file.write(" networks:\n")
24-
compose_file.write(" - app_network\n")
25-
compose_file.write(" monitoring_server:\n")
26-
compose_file.write(" image: grafana:latest\n")
27-
compose_file.write(" volumes:\n")
28-
compose_file.write(" - ./nginx/nginx.conf:/etc/nginx/nginx.conf\n")
29-
compose_file.write(" depends_on:\n")
30-
compose_file.write(" - string\n")
31-
compose_file.write(" ports:\n")
32-
compose_file.write(" - '82:80'\n")
33-
compose_file.write(" environment:\n")
34-
compose_file.write(" - foo=bar\n")
35-
compose_file.write(" networks:\n")
36-
compose_file.write(" - app_network\n")
37-
compose_file.write("networks:\n")
38-
compose_file.write(" app_network:\n")
39-
compose_file.write(" driver: bridge\n")
Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,78 @@
1-
version: '3'
1+
networks:
2+
additionalProp1:
3+
driver: string
4+
additionalProp2:
5+
driver: string
6+
additionalProp3:
7+
driver: string
28
services:
3-
web_server:
4-
image: nginx:latest
5-
volumes:
6-
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
9+
additionalProp1:
10+
args:
11+
additionalProp1: string
12+
additionalProp2: string
13+
additionalProp3: string
14+
build:
15+
context: string
16+
dockerfile: string
17+
command: string
18+
container_name: string
719
depends_on:
8-
- string
9-
ports:
10-
- '80:80'
20+
- string
1121
environment:
12-
- foo=bar
22+
additionalProp1: string
23+
additionalProp2: string
24+
additionalProp3: string
25+
image: string
1326
networks:
14-
- app_network
15-
16-
monitoring_server:
17-
image: grafana:latest
27+
- string
28+
ports:
29+
- string
1830
volumes:
19-
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
31+
- string
32+
additionalProp2:
33+
args:
34+
additionalProp1: string
35+
additionalProp2: string
36+
additionalProp3: string
37+
build:
38+
context: string
39+
dockerfile: string
40+
command: string
41+
container_name: string
2042
depends_on:
21-
- string
43+
- string
44+
environment:
45+
additionalProp1: string
46+
additionalProp2: string
47+
additionalProp3: string
48+
image: string
49+
networks:
50+
- string
2251
ports:
23-
- '82:80'
52+
- string
53+
volumes:
54+
- string
55+
additionalProp3:
56+
args:
57+
additionalProp1: string
58+
additionalProp2: string
59+
additionalProp3: string
60+
build:
61+
context: string
62+
dockerfile: string
63+
command: string
64+
container_name: string
65+
depends_on:
66+
- string
2467
environment:
25-
- foo=bar
68+
additionalProp1: string
69+
additionalProp2: string
70+
additionalProp3: string
71+
image: string
2672
networks:
27-
- app_network
28-
29-
networks:
30-
app_network:
31-
driver: bridge
73+
- string
74+
ports:
75+
- string
76+
volumes:
77+
- string
78+
version: string

app/models/ansible_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def validator_os(cls, value):
3232
return value
3333

3434

35-
35+
3636
class AnsibleInstallKuber(AnsibleBase):
3737
os: str = 'ubuntu'
3838
k8s_worker_nodes: List[str]

app/models/compose_models.py

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,27 @@
1-
from typing import List, Optional
2-
from pydantic import BaseModel, validator, ValidationError, computed_field
3-
4-
class Port(BaseModel):
5-
machine_port:int = 80
6-
container_port:int = 80
7-
8-
class Network(BaseModel):
9-
name:str = 'app_network'
10-
11-
class EnvironmentVariable(BaseModel):
12-
name:str = 'foo'
13-
value:str = "bar"
14-
15-
@computed_field
16-
@property
17-
def env_full(self) -> int:
18-
return f"{self.name}:{self.value}"
19-
20-
class Volume(BaseModel):
21-
local_dir: str = './nginx/nginx.conf'
22-
container_dir:str = '/etc/nginx/nginx.conf'
23-
24-
@computed_field
25-
@property
26-
def volume(self) -> int:
27-
return f"{self.local_dir}:{self.container_dir}"
1+
from typing import Dict, List, Optional
2+
from pydantic import BaseModel
283

294
class Build(BaseModel):
30-
context:str
31-
dockerfile:str
5+
context: str
6+
dockerfile: str
7+
328
class Service(BaseModel):
33-
image:str = 'nginx'
34-
name:str = 'web_server'
35-
container_name:str = 'web_server'
36-
build: Build | None = None
37-
version:str = 'latest'
38-
volumes:List[Volume] | None = None
39-
depends_on:List[str] | None = None
40-
ports:List[Port]
41-
networks:List[Network] | None = None
42-
environments:List[EnvironmentVariable] | None = None
43-
44-
@computed_field
45-
@property
46-
def image_full(self) -> int:
47-
return f"{self.image}:{self.version}"
48-
49-
@computed_field
50-
@property
51-
def volumes_full(self) -> int:
52-
return [i.volume for i in self.volumes]
53-
54-
55-
9+
build: Optional[Build] = None
10+
image: Optional[str] = None
11+
container_name: Optional[str] = None
12+
command: Optional[str] = None
13+
volumes: Optional[List[str]] = None
14+
environment: Optional[Dict[str, str]] = None
15+
ports: Optional[List[str]] = None
16+
networks: Optional[List[str]] = None
17+
args: Optional[Dict[str, str]] = None
18+
depends_on: Optional[List[str]] = None
19+
20+
class Network(BaseModel):
21+
driver: str
22+
5623
class DockerCompose(BaseModel):
57-
services: List[Service]
58-
network:Network
59-
24+
version: str
25+
services: Dict[str, Service]
26+
networks: Optional[Dict[str, Network]] = None
27+

app/routes/docker.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ async def docker_compose_template(request:DockerCompose) -> Output:
1010

1111
if os.environ.get("TEST"):
1212
return Output(output='output')
13-
generated_prompt = docker_compose_generator(request)
13+
docker_compose_generator(request)
1414

15-
output = gpt_service(generated_prompt)
16-
edit_directory_generator("compose_generator",output)
17-
execute_pythonfile("MyCompose","compose_generator")
1815
return Output(output='output')
1916

Lines changed: 8 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,11 @@
1-
def docker_compose_generator(input):
2-
compose_network = input.network.name
3-
compose_services = input.services
4-
services = [i.name for i in compose_services]
5-
images = [{i.name:i.image_full} for i in compose_services]
6-
volumes = [{i.name:i.volumes_full} for i in compose_services]
7-
depends_on = [{i.name:i.depends_on} for i in compose_services]
8-
ports = [{i.name:i.ports} for i in compose_services]
9-
env = [{i.name:i.environments} for i in compose_services]
10-
networks = [{i.name:i.networks} for i in compose_services]
11-
12-
13-
prompt = f"""
14-
15-
generate a python code (with out any ```python entry or additionals) with generates a docker-compose.yaml file in the directory 'app/media/MyCompose'
16-
17-
the docker-compose.yaml, must following there instructions:
18-
the version must be = 3
19-
set services following this list: {services}
20-
set images to serivce following this dict : {images}
21-
set volumes to service following this dict : {volumes}
22-
set depends_on to service following this dict : {depends_on}
23-
set ports to service following this dict : {ports}
24-
set environment to service following this dict : {env}
25-
set netwotks to service following this dict : {networks}
26-
27-
28-
finally, at the end of docker-compose file, add following block:
29-
```
30-
networks:
31-
{compose_network}:
32-
driver: bridge
33-
34-
```
35-
36-
37-
finally just give me a python code without any note that can generate a project folder with the
38-
given schema without ```python entry. and we dont need any base directory in the python code.
39-
the final ansible template must work very well without any error!
40-
41-
42-
the python code you give me, must have structure like that:
43-
44-
import os
45-
project_name = "app/media/MyCompose"
46-
foo_dir = os.path.join(project_name, "bar")
47-
x_dir = os.path.join(modules_dir, "y")
1+
import yaml
2+
from app.models.compose_models import DockerCompose
483

49-
# Create project directories
50-
os.makedirs(compose_dir, exist_ok=True)
51-
52-
# Create main.tf
53-
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
54-
# any thing you need
55-
4+
def docker_compose_generator(input):
565

6+
compose_total = input.model_dump(mode="json")
577

58-
"""
59-
return prompt
8+
file=open("app/media/MyCompose/docker-compose.yaml","w")
9+
yaml.dump(compose_total,file)
10+
file.close()
11+

0 commit comments

Comments
 (0)