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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
venv
data
.pytest*
.env
Binary file modified app/__pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/prompt_generators.cpython-311.pyc
Binary file not shown.
138 changes: 83 additions & 55 deletions app/directory_generators/terraform_generator.py
Original file line number Diff line number Diff line change
@@ -1,143 +1,171 @@
import os
project_name = "app/media/MyTerraform"
modules_dir = os.path.join(project_name, "modules")
docker_dir = os.path.join(modules_dir, "docker")
docker_container_dir = os.path.join(modules_dir, "docker_container")

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

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

module "docker" {
source = "./modules/docker"
image = var.image
name = var.container_name
ports = var.ports
module "docker_container" {
source = "./modules/docker_container"

image = var.image
name = var.name
ports = var.ports
env = var.env
}

''')

# Create variables.tf at root
with open(os.path.join(project_name, "variables.tf"), "w") as variables_file:
variables_file.write('''variable "docker_host" {
description = "The Docker host Uri."
# 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
}

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

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

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

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

# Create terraform.tfvars at root
# 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"
container_name = "my_nginx"
tfvars_file.write('''
docker_host = "tcp://localhost:2375"
image = "nginx:latest"
name = "my-nginx-container"
ports = ["80:80"]
env = { "MY_ENV_VAR" = "value" }
''')

# Create versions.tf at root
# Create versions.tf
with open(os.path.join(project_name, "versions.tf"), "w") as versions_file:
versions_file.write('''terraform {
versions_file.write('''
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.0"
source = "kreuzwerker/docker"
version = ">= 2.8.0"
}
}
}

''')

# Create outputs.tf at root
# Create outputs.tf
with open(os.path.join(project_name, "outputs.tf"), "w") as outputs_file:
outputs_file.write('''output "container_id" {
outputs_file.write('''
output "container_id" {
description = "The ID of the Docker container."
value = module.docker.container_id
value = module.docker_container.container_id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = module.docker.container_ip
value = module.docker_container.container_ip
}
''')

# Create main.tf in modules/docker
with open(os.path.join(docker_dir, "main.tf"), "w") as docker_main_file:
docker_main_file.write('''resource "docker_container" "this" {
name = var.name
# 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 = var.ports[0]
external = var.ports[1]
internal = 80
external = var.ports[0]
}
env = var.env
}
''')

# Create variables.tf in modules/docker
with open(os.path.join(docker_dir, "variables.tf"), "w") as docker_variables_file:
docker_variables_file.write('''variable "image" {
description = "The Docker image to use."
# 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 "name" {
description = "The name of the Docker container."
description = "Container name."
type = string
}

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

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

# Create terraform.tfvars in modules/docker
with open(os.path.join(docker_dir, "terraform.tfvars"), "w") as docker_tfvars_file:
docker_tfvars_file.write('''image = "nginx:latest"
name = "my_nginx"
ports = ["80", "80"]
# Create docker_container/terraform.tfvars
with open(os.path.join(docker_container_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" }
''')

# Create versions.tf in modules/docker
with open(os.path.join(docker_dir, "versions.tf"), "w") as docker_versions_file:
docker_versions_file.write('''terraform {
# Create docker_container/versions.tf
with open(os.path.join(docker_container_dir, "versions.tf"), "w") as module_versions_file:
module_versions_file.write('''
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.0"
source = "kreuzwerker/docker"
version = ">= 2.8.0"
}
}
}
''')

# Create outputs.tf in modules/docker
with open(os.path.join(docker_dir, "outputs.tf"), "w") as docker_outputs_file:
docker_outputs_file.write('''output "container_id" {
# 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.this.id
value = docker_container.app.id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = docker_container.this.ip_address
value = docker_container.app.ip_address
}
''')
12 changes: 7 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
from app.models import (IaCBasicInput,
IaCBugfixInput,
Output,
IaCInstallationInput,IaCTemplateGeneration,HelmTemplateGeneration)
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,
IaC_template_generator,helm_template_generator)
helm_template_generator)

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

import os
app = FastAPI()
Expand Down Expand Up @@ -42,10 +44,10 @@ async def IaC_install_generation(request:IaCInstallationInput) -> Output:
output = gpt_service(generated_prompt)
return Output(output=output)

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

generated_prompt = IaC_template_generator(request)
generated_prompt = IaC_template_generator_docker(request)
output = gpt_service(generated_prompt)
edit_directory_generator("terraform_generator",output)
execute_pythonfile("MyTerraform","terraform_generator")
Expand Down
14 changes: 9 additions & 5 deletions app/media/MyTerraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@

provider "docker" {
host = var.docker_host
}

module "docker" {
source = "./modules/docker"
image = var.image
name = var.container_name
ports = var.ports
module "docker_container" {
source = "./modules/docker_container"

image = var.image
name = var.name
ports = var.ports
env = var.env
}

8 changes: 0 additions & 8 deletions app/media/MyTerraform/modules/docker/main.tf

This file was deleted.

3 changes: 0 additions & 3 deletions app/media/MyTerraform/modules/docker/terraform.tfvars

This file was deleted.

14 changes: 0 additions & 14 deletions app/media/MyTerraform/modules/docker/variables.tf

This file was deleted.

10 changes: 10 additions & 0 deletions app/media/MyTerraform/modules/docker_container/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

resource "docker_container" "app" {
image = var.image
name = var.name
ports {
internal = 80
external = var.ports[0]
}
env = var.env
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

output "container_id" {
description = "The ID of the Docker container."
value = docker_container.this.id
value = docker_container.app.id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = docker_container.this.ip_address
value = docker_container.app.ip_address
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

image = "nginx:latest"
name = "my-nginx-container"
ports = ["80:80"]
env = { "MY_ENV_VAR" = "value" }
20 changes: 20 additions & 0 deletions app/media/MyTerraform/modules/docker_container/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

variable "image" {
description = "Docker image."
type = string
}

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

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

variable "env" {
description = "Map of environment variables."
type = map(string)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.0"
source = "kreuzwerker/docker"
version = ">= 2.8.0"
}
}
}
5 changes: 3 additions & 2 deletions app/media/MyTerraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

output "container_id" {
description = "The ID of the Docker container."
value = module.docker.container_id
value = module.docker_container.container_id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = module.docker.container_ip
value = module.docker_container.container_ip
}
Loading