From 91a80bdf1dd14a44d35eb54c1c8cf13d4cf8d5e4 Mon Sep 17 00:00:00 2001 From: abolfazl1381 Date: Tue, 3 Dec 2024 17:33:38 +0330 Subject: [PATCH] feat(compose): compelete compose prompt --- app/directory_generators/compose_generator.py | 39 +++++++++++++++++++ app/media/MyCompose/docker-compose.yaml | 27 ++++++++++++- app/template_generators/docker/compose.py | 21 +++++++++- 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/app/directory_generators/compose_generator.py b/app/directory_generators/compose_generator.py index e69de29b..0627c345 100644 --- a/app/directory_generators/compose_generator.py +++ b/app/directory_generators/compose_generator.py @@ -0,0 +1,39 @@ +import os + +project_name = "app/media/MyCompose" +compose_file_path = os.path.join(project_name, "docker-compose.yaml") + +# Create project directories +os.makedirs(project_name, exist_ok=True) + +# Create docker-compose.yaml +with open(compose_file_path, "w") as compose_file: + compose_file.write("version: '3'\n") + compose_file.write("services:\n") + compose_file.write(" web_server:\n") + compose_file.write(" image: nginx:latest\n") + compose_file.write(" volumes:\n") + compose_file.write(" - ./nginx/nginx.conf:/etc/nginx/nginx.conf\n") + compose_file.write(" depends_on:\n") + compose_file.write(" - string\n") + compose_file.write(" ports:\n") + compose_file.write(" - '80:80'\n") + compose_file.write(" environment:\n") + compose_file.write(" - foo=bar\n") + compose_file.write(" networks:\n") + compose_file.write(" - app_network\n") + compose_file.write(" monitoring_server:\n") + compose_file.write(" image: grafana:latest\n") + compose_file.write(" volumes:\n") + compose_file.write(" - ./nginx/nginx.conf:/etc/nginx/nginx.conf\n") + compose_file.write(" depends_on:\n") + compose_file.write(" - string\n") + compose_file.write(" ports:\n") + compose_file.write(" - '82:80'\n") + compose_file.write(" environment:\n") + compose_file.write(" - foo=bar\n") + compose_file.write(" networks:\n") + compose_file.write(" - app_network\n") + compose_file.write("networks:\n") + compose_file.write(" app_network:\n") + compose_file.write(" driver: bridge\n") \ No newline at end of file diff --git a/app/media/MyCompose/docker-compose.yaml b/app/media/MyCompose/docker-compose.yaml index e48d178b..5a225be0 100644 --- a/app/media/MyCompose/docker-compose.yaml +++ b/app/media/MyCompose/docker-compose.yaml @@ -1,4 +1,29 @@ +version: '3' services: web_server: image: nginx:latest - build: + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + depends_on: + - string + ports: + - '80:80' + environment: + - foo=bar + networks: + - app_network + monitoring_server: + image: grafana:latest + volumes: + - ./nginx/nginx.conf:/etc/nginx/nginx.conf + depends_on: + - string + ports: + - '82:80' + environment: + - foo=bar + networks: + - app_network +networks: + app_network: + driver: bridge diff --git a/app/template_generators/docker/compose.py b/app/template_generators/docker/compose.py index 79b739ff..f2589073 100644 --- a/app/template_generators/docker/compose.py +++ b/app/template_generators/docker/compose.py @@ -1,4 +1,5 @@ def docker_compose_generator(input): + compose_network = input.network.name compose_services = input.services services = [i.container_name for i in compose_services] images = [{i.container_name:i.image_full} for i in compose_services] @@ -13,13 +14,31 @@ def docker_compose_generator(input): generate a python code (with out any ```python entry or additionals) with generates a docker-compose.yaml file in the directory 'app/media/MyCompose' - + the docker-compose.yaml, must following there instructions: + the version must be = 3 + set services following this list: {services} + set images to serivce following this dict : {images} + set volumes to service following this dict : {volumes} + set depends_on to service following this dict : {depends_on} + set ports to service following this dict : {ports} + set environment to service following this dict : {env} + set netwotks to service following this dict : {networks} + + + finally, at the end of docker-compose file, add following block: + ``` + networks: + {compose_network}: + driver: bridge + + ``` finally just give me a python code without any note that can generate a project folder with the given schema without ```python entry. and we dont need any base directory in the python code. the final ansible template must work very well without any error! + the python code you give me, must have structure like that: import os