1- import os
2-
3- project_name = "app/media/MyTerraform"
4- modules_dir = os .path .join (project_name , "modules" )
5- docker_dir = os .path .join (modules_dir , "docker" )
6-
7- # Create project directories
8- os .makedirs (docker_dir , exist_ok = True )
9-
10- # Create main.tf
11- with open (os .path .join (project_name , "main.tf" ), "w" ) as main_file :
12- main_file .write ('''
13- provider "docker" {
14- host = "unix:///var/run/docker.sock"
15- }
16-
17- module "docker" {
18- source = "./modules/docker"
19-
20- image_name = var.image_name
21- image_force_remove = var.image_force_remove
22- image_build = var.image_build
23- image_count = var.image_count
24-
25- container_image = var.container_image
26- container_name = var.container_name
27- container_hostname = var.container_hostname
28- container_restart = var.container_restart
29- container_count = var.container_count
30- }
31- ''' )
32-
33- # Create variables.tf
34- with open (os .path .join (project_name , "variables.tf" ), "w" ) as variables_file :
35- variables_file .write ('''
36- variable "image_name" {
37- type = string
38- }
39-
40- variable "image_force_remove" {
41- type = bool
42- }
43-
44- variable "image_build" {
45- type = object({
46- context = string
47- tag = list(string)
48- })
49- }
50-
51- variable "image_count" {
52- type = number
53- }
54-
55- variable "container_image" {
56- type = string
57- }
58-
59- variable "container_name" {
60- type = string
61- }
62-
63- variable "container_hostname" {
64- type = string
65- }
66-
67- variable "container_restart" {
68- type = string
69- }
70-
71- variable "container_count" {
72- type = number
73- }
74- ''' )
75-
76- # Create terraform.tfvars
77- with open (os .path .join (project_name , "terraform.tfvars" ), "w" ) as tfvars_file :
78- tfvars_file .write ('''
79- image_name = "my-image"
80- image_force_remove = true
81- image_build = {
82- context = "./"
83- tag = ["my-image:latest"]
84- }
85- image_count = 1
86-
87- container_image = "my-image"
88- container_name = "my-container"
89- container_hostname = "my-host"
90- container_restart = "always"
91- container_count = 1
92- ''' )
93-
94- # Create versions.tf
95- with open (os .path .join (project_name , "versions.tf" ), "w" ) as versions_file :
96- versions_file .write ('''
97- terraform {
98- required_version = ">= 1.0"
99-
100- required_providers {
101- docker = {
102- source = "kreuzwerker/docker"
103- version = ">= 2.8.0"
104- }
105- }
106- }
107- ''' )
108-
109- # Create module main.tf
110- with open (os .path .join (docker_dir , "main.tf" ), "w" ) as module_main_file :
111- module_main_file .write ('''
112- resource "docker_image" "app_image" {
113- count = var.image_count
114- name = var.image_name
115- force_remove = var.image_force_remove
116-
117- build {
118- context = var.image_build.context
119- tag = var.image_build.tag
120- }
121- }
122-
123- resource "docker_container" "app_container" {
124- count = var.container_count
125- image = var.container_image
126- name = var.container_name
127- hostname = var.container_hostname
128- restart = var.container_restart
129- }
130- ''' )
131-
132- # Create module variables.tf
133- with open (os .path .join (docker_dir , "variables.tf" ), "w" ) as module_variables_file :
134- module_variables_file .write ('''
135- variable "image_name" {
136- type = string
137- }
138-
139- variable "image_force_remove" {
140- type = bool
141- }
142-
143- variable "image_build" {
144- type = object({
145- context = string
146- tag = list(string)
147- })
148- }
149-
150- variable "image_count" {
151- type = number
152- }
153-
154- variable "container_image" {
155- type = string
156- }
157-
158- variable "container_name" {
159- type = string
160- }
161-
162- variable "container_hostname" {
163- type = string
164- }
165-
166- variable "container_restart" {
167- type = string
168- }
169-
170- variable "container_count" {
171- type = number
172- }
173- ''' )
174-
175- # Create module terraform.tfvars
176- with open (os .path .join (docker_dir , "terraform.tfvars" ), "w" ) as module_tfvars_file :
177- module_tfvars_file .write ('''
178- image_name = "my-image"
179- image_force_remove = true
180- image_build = {
181- context = "./"
182- tag = ["my-image:latest"]
183- }
184- image_count = 1
185-
186- container_image = "my-image"
187- container_name = "my-container"
188- container_hostname = "my-host"
189- container_restart = "always"
190- container_count = 1
191- ''' )
192-
193- # Create module versions.tf
194- with open (os .path .join (docker_dir , "versions.tf" ), "w" ) as module_versions_file :
195- module_versions_file .write ('''
196- terraform {
197- required_version = ">= 1.0"
198-
199- required_providers {
200- docker = {
201- source = "kreuzwerker/docker"
202- version = ">= 2.8.0"
203- }
204- }
205- }
206- ''' )
1+ Hello ! It looks like your message might have been empty . How can I assist you today ?
0 commit comments