Skip to content

Commit 95b68ef

Browse files
authored
Merge pull request #61 from abolfazl8131/master
refactor project
2 parents 5f0d301 + 12e95f6 commit 95b68ef

31 files changed

+397
-263
lines changed
221 Bytes
Binary file not shown.
-4.55 KB
Binary file not shown.

app/app_instance.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
Lines changed: 127 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,94 @@
11
import os
2+
23
project_name = "app/media/MyTerraform"
34
modules_dir = os.path.join(project_name, "modules")
4-
docker_container_dir = os.path.join(modules_dir, "docker_container")
5+
docker_dir = os.path.join(modules_dir, "docker")
56

67
# Create project directories
7-
os.makedirs(docker_container_dir, exist_ok=True)
8+
os.makedirs(docker_dir, exist_ok=True)
89

910
# Create main.tf
1011
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
1112
main_file.write('''
1213
provider "docker" {
13-
host = var.docker_host
14+
host = "unix:///var/run/docker.sock"
1415
}
1516
16-
module "docker_container" {
17-
source = "./modules/docker_container"
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
1824
19-
image = var.image
20-
name = var.name
21-
ports = var.ports
22-
env = var.env
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
2330
}
24-
2531
''')
2632

2733
# Create variables.tf
28-
with open(os.path.join(project_name, "variables.tf"), "w") as vars_file:
29-
vars_file.write('''
30-
variable "docker_host" {
31-
description = "Docker host URL."
32-
type = string
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
3353
}
3454
35-
variable "image" {
36-
description = "Docker image to use."
37-
type = string
55+
variable "container_image" {
56+
type = string
3857
}
3958
40-
variable "name" {
41-
description = "Name of the container."
42-
type = string
59+
variable "container_name" {
60+
type = string
4361
}
4462
45-
variable "ports" {
46-
description = "List of ports to expose."
47-
type = list(string)
63+
variable "container_hostname" {
64+
type = string
4865
}
4966
50-
variable "env" {
51-
description = "Environment variables for the container."
52-
type = map(string)
67+
variable "container_restart" {
68+
type = string
69+
}
70+
71+
variable "container_count" {
72+
type = number
5373
}
5474
''')
5575

5676
# Create terraform.tfvars
5777
with open(os.path.join(project_name, "terraform.tfvars"), "w") as tfvars_file:
5878
tfvars_file.write('''
59-
docker_host = "tcp://localhost:2375"
60-
image = "nginx:latest"
61-
name = "my-nginx-container"
62-
ports = ["80:80"]
63-
env = { "MY_ENV_VAR" = "value" }
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
6492
''')
6593

6694
# Create versions.tf
@@ -76,73 +104,94 @@
76104
}
77105
}
78106
}
79-
80107
''')
81108

82-
# Create outputs.tf
83-
with open(os.path.join(project_name, "outputs.tf"), "w") as outputs_file:
84-
outputs_file.write('''
85-
output "container_id" {
86-
description = "The ID of the Docker container."
87-
value = module.docker_container.container_id
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+
}
88121
}
89122
90-
output "container_ip" {
91-
description = "The IP address of the Docker container."
92-
value = module.docker_container.container_ip
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
93129
}
94130
''')
95131

96-
# Create module files
97-
# Create docker_container/main.tf
98-
with open(os.path.join(docker_container_dir, "main.tf"), "w") as module_main_file:
99-
module_main_file.write('''
100-
resource "docker_container" "app" {
101-
image = var.image
102-
name = var.name
103-
ports {
104-
internal = 80
105-
external = var.ports[0]
106-
}
107-
env = var.env
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
108152
}
109-
''')
110153
111-
# Create docker_container/variables.tf
112-
with open(os.path.join(docker_container_dir, "variables.tf"), "w") as module_vars_file:
113-
module_vars_file.write('''
114-
variable "image" {
115-
description = "Docker image."
116-
type = string
154+
variable "container_image" {
155+
type = string
117156
}
118157
119-
variable "name" {
120-
description = "Container name."
121-
type = string
158+
variable "container_name" {
159+
type = string
122160
}
123161
124-
variable "ports" {
125-
description = "List of exposed ports."
126-
type = list(string)
162+
variable "container_hostname" {
163+
type = string
127164
}
128165
129-
variable "env" {
130-
description = "Map of environment variables."
131-
type = map(string)
166+
variable "container_restart" {
167+
type = string
168+
}
169+
170+
variable "container_count" {
171+
type = number
132172
}
133173
''')
134174

135-
# Create docker_container/terraform.tfvars
136-
with open(os.path.join(docker_container_dir, "terraform.tfvars"), "w") as module_tfvars_file:
175+
# Create module terraform.tfvars
176+
with open(os.path.join(docker_dir, "terraform.tfvars"), "w") as module_tfvars_file:
137177
module_tfvars_file.write('''
138-
image = "nginx:latest"
139-
name = "my-nginx-container"
140-
ports = ["80:80"]
141-
env = { "MY_ENV_VAR" = "value" }
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
142191
''')
143192

144-
# Create docker_container/versions.tf
145-
with open(os.path.join(docker_container_dir, "versions.tf"), "w") as module_versions_file:
193+
# Create module versions.tf
194+
with open(os.path.join(docker_dir, "versions.tf"), "w") as module_versions_file:
146195
module_versions_file.write('''
147196
terraform {
148197
required_version = ">= 1.0"
@@ -154,18 +203,4 @@
154203
}
155204
}
156205
}
157-
''')
158-
159-
# Create docker_container/outputs.tf
160-
with open(os.path.join(docker_container_dir, "outputs.tf"), "w") as module_outputs_file:
161-
module_outputs_file.write('''
162-
output "container_id" {
163-
description = "The ID of the Docker container."
164-
value = docker_container.app.id
165-
}
166-
167-
output "container_ip" {
168-
description = "The IP address of the Docker container."
169-
value = docker_container.app.ip_address
170-
}
171206
''')

app/main.py

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,3 @@
1-
2-
from .gpt_services import gpt_service
3-
from .services import (write_basic,
4-
write_bugfix,
5-
write_installation,
6-
edit_directory_generator,execute_pythonfile)
7-
from app.models import (IaCBasicInput,
8-
IaCBugfixInput,
9-
Output,
10-
IaCInstallationInput,IaCTemplateGenerationDocker,HelmTemplateGeneration)
11-
12-
from fastapi import FastAPI, HTTPException,Response
13-
from fastapi.responses import FileResponse
14-
from .prompt_generators import (IaC_basics_generator,
15-
IaC_bugfix_generator,
16-
IaC_installation_generator,
17-
helm_template_generator)
18-
19-
from app.template_generators.terraform.docker import (IaC_template_generator_docker)
20-
21-
import os
22-
app = FastAPI()
23-
24-
25-
@app.post("/IaC-basic/")
26-
async def IaC_basic_generation(request:IaCBasicInput) -> Output:
27-
28-
generated_prompt = IaC_basics_generator(request)
29-
output = gpt_service(generated_prompt)
30-
return Output(output=output)
31-
32-
@app.post("/IaC-bugfix/")
33-
async def IaC_bugfix_generation(request:IaCBugfixInput) -> Output:
34-
35-
generated_prompt = IaC_bugfix_generator(request)
36-
output = gpt_service(generated_prompt)
37-
return Output(output=output)
38-
39-
40-
@app.post("/IaC-install/")
41-
async def IaC_install_generation(request:IaCInstallationInput) -> Output:
42-
43-
generated_prompt = IaC_installation_generator(request)
44-
output = gpt_service(generated_prompt)
45-
return Output(output=output)
46-
47-
@app.post("/IaC-template/docker")
48-
async def IaC_template_generation_docker(request:IaCTemplateGenerationDocker) -> Output:
49-
50-
generated_prompt = IaC_template_generator_docker(request)
51-
output = gpt_service(generated_prompt)
52-
edit_directory_generator("terraform_generator",output)
53-
execute_pythonfile("MyTerraform","terraform_generator")
54-
return Output(output='output')
55-
56-
@app.post("/Helm-template/")
57-
async def Helm_template_generation(request:HelmTemplateGeneration) -> Output:
58-
59-
generated_prompt = helm_template_generator(request)
60-
output = gpt_service(generated_prompt)
61-
edit_directory_generator("helm_generator",output)
62-
execute_pythonfile("MyHelm","helm_generator")
63-
return Output(output='output')
64-
65-
66-
@app.get("/download/{filename}")
67-
def download_file(filename: str):
68-
folder = "app/media/MyTerraform" # specify your folder path here
69-
file_path = os.path.join(folder, filename)
70-
71-
# Ensure the file exists
72-
if not os.path.isfile(file_path):
73-
raise HTTPException(status_code=404, detail="File not found.")
74-
75-
# Return the file response for download
76-
return FileResponse(file_path, media_type='application/octet-stream', filename=filename)
77-
78-
@app.get("/list-directory")
79-
def list_directory(folder: str):
80-
# Ensure the folder exists
81-
if not os.path.isdir(folder):
82-
raise HTTPException(status_code=404, detail=f"{folder} does not exist.")
83-
84-
# List the contents of the directory
85-
contents = os.listdir(folder)
86-
return {"folder": folder, "contents": contents}
1+
from app.routes.utils import *
2+
from app.routes.terraform import *
3+
from app.routes.helm import *

0 commit comments

Comments
 (0)