1-
2-
31def IaC_template_generator_docker (input ) -> str :
42
3+ docker = ['docker_container' , 'docker_image' ]
4+ docker_image_count = 1 if input .docker_image else 0
5+ docker_container_count = 1 if input .docker_container else 0
6+
57 prompt = f"""
68 Generate a Python code to generate a Terraform project (project name is app/media/MyTerraform)
7- that dynamically provisions resources for { input . base_config } ensuring a modular, flexible structure
8- to enable users to configure all essential settings at the root level. Only provide Python code,
9- no explanations or markdown formatting. The project should be organized as follows:
9+ that dynamically provisions { docker } resources ensuring a modular, flexible structure to enable users
10+ to configure all essential settings at the root level. Only provide Python code, no explanations or
11+ markdown formatting. The project should be organized as follows:
1012 1. Root Directory Structure:
1113 - main.tf:
12- - Contains a provider block, configured with flexible variables where required to allow
13- users to specify provider settings without hardcoding.
14- - Defines a module block that references { input .base_config } from a subdirectory within
15- modules. This module block should expose all variables that { input .base_config } requires,
16- allowing configuration at the root level rather than directly within the module.
17- - Every variable defined in { input .base_config } should be passed through the module block,
18- ensuring that users can adjust all critical parameters of { input .base_config } by
19- modifying root main.tf.
14+ - Define the provider block as follows:
15+ ```
16+ provider "docker" {{
17+ host = "unix:///var/run/docker.sock"
18+ }}
19+ ```
20+ - Defines a module block that references "docker" from a subdirectory within modules.
21+ This module block should expose all variables that { docker } resources require, allowing
22+ configuration at the root level rather than directly within the module.
23+ - Every variable defined in { docker } resources should be passed through the module block,
24+ ensuring that users can adjust all critical parameters of { docker } resources by modifying
25+ root main.tf. Avoid using any other parameters. just use the parameters of { docker } resources with the same keys
2026 - variables.tf:
21- - Declares all variables that users might need to configure for { input .base_config } .
22- These should include any inputs required by the provider or the { input .base_config }
23- resource, as well as optional parameters that users may want to customize.
24- - Variable descriptions should clearly indicate their purpose, and default values should
25- be avoided unless there are reasonable, common defaults, to maintain flexibility and
26- encourage explicit configuration.
27- - All types of variables can be used such as (number, string, bool, list(string),
28- map(string), list(map(string)), map(map(string)), object(), any)
27+ - Sets these variables names for docker_image resource:
28+ image_name(string), image_force_remove(bool), image_build(object), image_count(number)
29+ - Sets these variables names for docker_container resource:
30+ container_image(string), container_name(string), container_hostname(string),
31+ container_restart(string), container_count(number)
2932 - terraform.tfvars:
30- - Provides default values for variables declared in the root `variables.tf`, making it easy
31- for users to define common configurations.
32- - This file should be structured to include any typical default settings without hardcoding
33- sensitive values.
33+ - Structure as follows:
34+ image_name = "my-image"
35+ image_force_remove = true
36+ image_build = {{
37+ context = "./"
38+ tag = ["my-image:latest"]
39+ }}
40+ image_count = { docker_image_count }
41+
42+ container_image = "my-image"
43+ container_name = "my-container"
44+ container_hostname = "my-host"
45+ container_restart = "always"
46+ container_count = { docker_container_count }
3447 - versions.tf:
35- - Contains the `terraform` and `provider` blocks, specifying required versions.
36-
37- - If { input .base_config } is a Docker resource, set kreuzwerker/docker as the provider with appropriate version constraints.
38- - If { input .base_config } is an AWS resource, set hashicorp/aws as the provider with suitable version constraints.
39-
40- - Structure the `terraform` block as:
48+ - Structure as follows:
4149 terraform {{
4250 required_version = ">= 1.0"
4351
4452 required_providers {{
45- <provider_name> = {{
46- source = "<source> "
47- version = ">= <version> "
53+ docker = {{
54+ source = "kreuzwerker/docker "
55+ version = ">= 2.8.0 "
4856 }}
4957 }}
5058 }}
51- - outputs.tf:
52- - Exposes relevant outputs from { input .base_config } , retrieving them from the module output
53- and making them accessible at the root level. Each output should have a clear description,
54- making it easy for users to understand the value and purpose of the output data.
55- 2. Module Directory Structure (modules/{ input .base_config } ):
59+ 2. Module Directory Structure (modules/docker):
5660 - main.tf:
57- - Defines the { input .base_config } resource, fully utilizing required and also some optional
58- parameters. Avoid any hardcoded values within the module to support full configurability
59- from the root level.
60- - Required parameters should cover the essentials for creating { input .base_config } , while
61- optional parameters should provide a range of additional settings that enable more
62- granular configuration if desired.
61+ - Set the following parameters for docker_image resource and avoid using any other parameters:
62+ - 1. count (type: number) Specifies the number of images
63+ - 2. name (type: string): Specifies the image name.
64+ - 3. force_remove (type: boolean): Determines whether to forcibly remove intermediate containers.
65+ - 4. build (block type): Includes the following required field:
66+ - context (type: string, required): Specifies the build context for the image.
67+ - tag(type: List of Strings, required): Specifices the image tag in the 'name:tag'
68+ format, (e.g., ["NAME:VERSION"])
69+ - Set the following parameters for docker_container resource and avoid using any other parameters:
70+ - 1. count (type: number): Specifies the number of containers
71+ - 2. image (type: string): Specifies the container image.
72+ - 3. name (type: string): Sets the container name.
73+ - 4. hostname (type: string): Configures the container hostname.
74+ - 5. restart (type: string): Defines the container's restart policy (e.g., always, on-failure, no).
6375 - variables.tf:
64- - Lists all variables necessary for configuring { input . base_config } , with descriptions and
65- types specified to improve usability. No default values should be set here unless
66- necessary for required fields.
67- - Variable names should be clear and consistent with naming conventions in the root
68- variables file, ensuring consistency in usage.
76+ - Sets these variables names for docker_image resource:
77+ image_name(string), image_force_remove(bool), image_build(object), image_count(number)
78+ - Sets these variables names for docker_container resource:
79+ container_image(string), container_name(string), container_hostname(string),
80+ container_restart(string), container_count(number)
6981 - terraform.tfvars:
70- - Includes default values for module-level variables to ensure that common parameters have
71- defaults. This `terraform.tfvars` file within the module should be structured to provide
72- typical configuration values, making it easier to set up and reducing the need for hardcoded values.
82+ - Structure as follows:
83+ image_name = "my-image"
84+ image_force_remove = true
85+ image_build = {{
86+ context = "./"
87+ tag = ["my-image:latest"]
88+ }}
89+ image_count = { docker_image_count }
90+
91+ container_image = "my-image"
92+ container_name = "my-container"
93+ container_hostname = "my-host"
94+ container_restart = "always"
95+ container_count = { docker_container_count }
7396 - versions.tf:
74- - Contains the `terraform` and `provider` blocks, specifying required versions.
75- - If { input .base_config } is a Docker resource, set kreuzwerker/docker as the provider with appropriate version constraints.
76- - If { input .base_config } is an AWS resource, set hashicorp/aws as the provider with suitable version constraints.
77-
78- - Structure the `terraform` block as:
97+ - Structure as follows:
7998 terraform {{
8099 required_version = ">= 1.0"
81100
82101 required_providers {{
83- <provider_name> = {{
84- source = "<source> "
85- version = ">= <version> "
86- }}
102+ docker = {{
103+ source = "kreuzwerker/docker "
104+ version = ">= 2.8.0 "
105+ }}
87106 }}
88107 }}
89- - outputs.tf:
90- - Specifies outputs for the { input .base_config } resource, selecting relevant data that users
91- might need to access from the root level. Each output should have an informative
92- description, so users can understand its purpose and utility.
93- Ensure this project structure supports { input .base_config } ’s configurability, extensibility, and
108+ Ensure this project structure supports { docker } ’s configurability, extensibility, and
94109 reusability across diverse Terraform providers, empowering users to manage their resources through a
95110 single, customizable root configuration while keeping module internals robustly modular.
96111
@@ -100,19 +115,17 @@ def IaC_template_generator_docker(input) -> str:
100115
101116 Python code you give me, must have structure like that:
102117
103- import os
104- project_name = "app/media/MyTerraform"
105- moduels_dir = os.path.join(project_name, "modules")
106-
107- # Create project directories
108-
109- os.makedirs(moduels_dir, exist_ok=True)
110-
111- # Create main.tf (for example)
112- with open(os.path.join(project_name, "main.tf"), "w") as main_file:
113- # any thing you need
118+ import os
119+ project_name = "app/media/MyTerraform"
120+ modules_dir = os.path.join(project_name, "modules")
121+ docker_container_dir = os.path.join(modules_dir, "docker_container")
114122
123+ # Create project directories
124+ os.makedirs(docker_container_dir, exist_ok=True)
115125
126+ # Create main.tf
127+ with open(os.path.join(project_name, "main.tf"), "w") as main_file:
128+ # any thing you need
116129
117130 """
118- return prompt
131+ return prompt
0 commit comments