Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/__pycache__/app_instance.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/main.cpython-311.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions app/app_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from fastapi import FastAPI

app = FastAPI()
219 changes: 127 additions & 92 deletions app/directory_generators/terraform_generator.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,94 @@
import os

project_name = "app/media/MyTerraform"
modules_dir = os.path.join(project_name, "modules")
docker_container_dir = os.path.join(modules_dir, "docker_container")
docker_dir = os.path.join(modules_dir, "docker")

# Create project directories
os.makedirs(docker_container_dir, exist_ok=True)
os.makedirs(docker_dir, exist_ok=True)

# Create main.tf
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
main_file.write('''
provider "docker" {
host = var.docker_host
host = "unix:///var/run/docker.sock"
}

module "docker_container" {
source = "./modules/docker_container"
module "docker" {
source = "./modules/docker"

image_name = var.image_name
image_force_remove = var.image_force_remove
image_build = var.image_build
image_count = var.image_count

image = var.image
name = var.name
ports = var.ports
env = var.env
container_image = var.container_image
container_name = var.container_name
container_hostname = var.container_hostname
container_restart = var.container_restart
container_count = var.container_count
}

''')

# Create variables.tf
with open(os.path.join(project_name, "variables.tf"), "w") as vars_file:
vars_file.write('''
variable "docker_host" {
description = "Docker host URL."
type = string
with open(os.path.join(project_name, "variables.tf"), "w") as variables_file:
variables_file.write('''
variable "image_name" {
type = string
}

variable "image_force_remove" {
type = bool
}

variable "image_build" {
type = object({
context = string
tag = list(string)
})
}

variable "image_count" {
type = number
}

variable "image" {
description = "Docker image to use."
type = string
variable "container_image" {
type = string
}

variable "name" {
description = "Name of the container."
type = string
variable "container_name" {
type = string
}

variable "ports" {
description = "List of ports to expose."
type = list(string)
variable "container_hostname" {
type = string
}

variable "env" {
description = "Environment variables for the container."
type = map(string)
variable "container_restart" {
type = string
}

variable "container_count" {
type = number
}
''')

# Create terraform.tfvars
with open(os.path.join(project_name, "terraform.tfvars"), "w") as tfvars_file:
tfvars_file.write('''
docker_host = "tcp://localhost:2375"
image = "nginx:latest"
name = "my-nginx-container"
ports = ["80:80"]
env = { "MY_ENV_VAR" = "value" }
image_name = "my-image"
image_force_remove = true
image_build = {
context = "./"
tag = ["my-image:latest"]
}
image_count = 1

container_image = "my-image"
container_name = "my-container"
container_hostname = "my-host"
container_restart = "always"
container_count = 1
''')

# Create versions.tf
Expand All @@ -76,73 +104,94 @@
}
}
}

''')

# Create outputs.tf
with open(os.path.join(project_name, "outputs.tf"), "w") as outputs_file:
outputs_file.write('''
output "container_id" {
description = "The ID of the Docker container."
value = module.docker_container.container_id
# Create module main.tf
with open(os.path.join(docker_dir, "main.tf"), "w") as module_main_file:
module_main_file.write('''
resource "docker_image" "app_image" {
count = var.image_count
name = var.image_name
force_remove = var.image_force_remove

build {
context = var.image_build.context
tag = var.image_build.tag
}
}

output "container_ip" {
description = "The IP address of the Docker container."
value = module.docker_container.container_ip
resource "docker_container" "app_container" {
count = var.container_count
image = var.container_image
name = var.container_name
hostname = var.container_hostname
restart = var.container_restart
}
''')

# Create module files
# Create docker_container/main.tf
with open(os.path.join(docker_container_dir, "main.tf"), "w") as module_main_file:
module_main_file.write('''
resource "docker_container" "app" {
image = var.image
name = var.name
ports {
internal = 80
external = var.ports[0]
}
env = var.env
# Create module variables.tf
with open(os.path.join(docker_dir, "variables.tf"), "w") as module_variables_file:
module_variables_file.write('''
variable "image_name" {
type = string
}

variable "image_force_remove" {
type = bool
}

variable "image_build" {
type = object({
context = string
tag = list(string)
})
}

variable "image_count" {
type = number
}
''')

# Create docker_container/variables.tf
with open(os.path.join(docker_container_dir, "variables.tf"), "w") as module_vars_file:
module_vars_file.write('''
variable "image" {
description = "Docker image."
type = string
variable "container_image" {
type = string
}

variable "name" {
description = "Container name."
type = string
variable "container_name" {
type = string
}

variable "ports" {
description = "List of exposed ports."
type = list(string)
variable "container_hostname" {
type = string
}

variable "env" {
description = "Map of environment variables."
type = map(string)
variable "container_restart" {
type = string
}

variable "container_count" {
type = number
}
''')

# Create docker_container/terraform.tfvars
with open(os.path.join(docker_container_dir, "terraform.tfvars"), "w") as module_tfvars_file:
# Create module terraform.tfvars
with open(os.path.join(docker_dir, "terraform.tfvars"), "w") as module_tfvars_file:
module_tfvars_file.write('''
image = "nginx:latest"
name = "my-nginx-container"
ports = ["80:80"]
env = { "MY_ENV_VAR" = "value" }
image_name = "my-image"
image_force_remove = true
image_build = {
context = "./"
tag = ["my-image:latest"]
}
image_count = 1

container_image = "my-image"
container_name = "my-container"
container_hostname = "my-host"
container_restart = "always"
container_count = 1
''')

# Create docker_container/versions.tf
with open(os.path.join(docker_container_dir, "versions.tf"), "w") as module_versions_file:
# Create module versions.tf
with open(os.path.join(docker_dir, "versions.tf"), "w") as module_versions_file:
module_versions_file.write('''
terraform {
required_version = ">= 1.0"
Expand All @@ -154,18 +203,4 @@
}
}
}
''')

# Create docker_container/outputs.tf
with open(os.path.join(docker_container_dir, "outputs.tf"), "w") as module_outputs_file:
module_outputs_file.write('''
output "container_id" {
description = "The ID of the Docker container."
value = docker_container.app.id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = docker_container.app.ip_address
}
''')
89 changes: 3 additions & 86 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,3 @@

from .gpt_services import gpt_service
from .services import (write_basic,
write_bugfix,
write_installation,
edit_directory_generator,execute_pythonfile)
from app.models import (IaCBasicInput,
IaCBugfixInput,
Output,
IaCInstallationInput,IaCTemplateGenerationDocker,HelmTemplateGeneration)

from fastapi import FastAPI, HTTPException,Response
from fastapi.responses import FileResponse
from .prompt_generators import (IaC_basics_generator,
IaC_bugfix_generator,
IaC_installation_generator,
helm_template_generator)

from app.template_generators.terraform.docker import (IaC_template_generator_docker)

import os
app = FastAPI()


@app.post("/IaC-basic/")
async def IaC_basic_generation(request:IaCBasicInput) -> Output:

generated_prompt = IaC_basics_generator(request)
output = gpt_service(generated_prompt)
return Output(output=output)

@app.post("/IaC-bugfix/")
async def IaC_bugfix_generation(request:IaCBugfixInput) -> Output:

generated_prompt = IaC_bugfix_generator(request)
output = gpt_service(generated_prompt)
return Output(output=output)


@app.post("/IaC-install/")
async def IaC_install_generation(request:IaCInstallationInput) -> Output:

generated_prompt = IaC_installation_generator(request)
output = gpt_service(generated_prompt)
return Output(output=output)

@app.post("/IaC-template/docker")
async def IaC_template_generation_docker(request:IaCTemplateGenerationDocker) -> Output:

generated_prompt = IaC_template_generator_docker(request)
output = gpt_service(generated_prompt)
edit_directory_generator("terraform_generator",output)
execute_pythonfile("MyTerraform","terraform_generator")
return Output(output='output')

@app.post("/Helm-template/")
async def Helm_template_generation(request:HelmTemplateGeneration) -> Output:

generated_prompt = helm_template_generator(request)
output = gpt_service(generated_prompt)
edit_directory_generator("helm_generator",output)
execute_pythonfile("MyHelm","helm_generator")
return Output(output='output')


@app.get("/download/{filename}")
def download_file(filename: str):
folder = "app/media/MyTerraform" # specify your folder path here
file_path = os.path.join(folder, filename)

# Ensure the file exists
if not os.path.isfile(file_path):
raise HTTPException(status_code=404, detail="File not found.")

# Return the file response for download
return FileResponse(file_path, media_type='application/octet-stream', filename=filename)

@app.get("/list-directory")
def list_directory(folder: str):
# Ensure the folder exists
if not os.path.isdir(folder):
raise HTTPException(status_code=404, detail=f"{folder} does not exist.")

# List the contents of the directory
contents = os.listdir(folder)
return {"folder": folder, "contents": contents}
from app.routes.utils import *
from app.routes.terraform import *
from app.routes.helm import *
Loading
Loading