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 modified app/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file removed app/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file modified app/__pycache__/prompt_generators.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
217 changes: 136 additions & 81 deletions app/directory_generators/terraform_generator.py
Original file line number Diff line number Diff line change
@@ -1,88 +1,143 @@
import os

project_name = "app/media/MyTerraform"
base_directory = project_name.replace("/", os.sep)
modules_directory = os.path.join(base_directory, "modules")
ci_directory = os.path.join(base_directory, ".github", "workflows")

os.makedirs(modules_directory, exist_ok=True)
os.makedirs(ci_directory, exist_ok=True)

terraform_main = f"""provider "aws" {{
region = "us-east-1"
}}

resource "aws_instance" "web" {{
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = {{
Name = "MyEC2Instance"
}}
}}
"""

terraform_variables = """variable "region" {{
description = "AWS region"
modules_dir = os.path.join(project_name, "modules")
docker_dir = os.path.join(modules_dir, "docker")

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

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

module "docker" {
source = "./modules/docker"
image = var.image
name = var.container_name
ports = var.ports
}
''')

# 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."
type = string
}

variable "image" {
description = "The Docker image to use."
type = string
default = "us-east-1"
}}
}

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

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

# Create terraform.tfvars at root
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"
ports = ["80:80"]
''')

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

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

# Create outputs.tf at root
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_id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = module.docker.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
image = var.image
ports {
internal = var.ports[0]
external = var.ports[1]
}
}
''')

# 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."
type = string
default = "t2.micro"
}}
}

variable "ami" {{
description = "AMI ID"
variable "name" {
description = "The name of the Docker container."
type = string
default = "ami-0c55b159cbfafe1f0"
}}
"""

github_actions = """name: Terraform CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
terraform:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 1.0.0

- name: Terraform Init
run: terraform init

- name: Terraform Plan
run: terraform plan

- name: Terraform Apply
run: terraform apply -auto-approve
env:
TF_VAR_region: ${{ secrets.AWS_REGION }}
TF_VAR_instance_type: ${{ secrets.AWS_INSTANCE_TYPE }}
TF_VAR_ami: ${{ secrets.AWS_AMI }}
"""

with open(os.path.join(base_directory, "main.tf"), "w") as f:
f.write(terraform_main)

with open(os.path.join(base_directory, "variables.tf"), "w") as f:
f.write(terraform_variables)

with open(os.path.join(ci_directory, "terraform-ci.yml"), "w") as f:
f.write(github_actions)
}

variable "ports" {
description = "List of ports for the container."
type = list(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 versions.tf in modules/docker
with open(os.path.join(docker_dir, "versions.tf"), "w") as docker_versions_file:
docker_versions_file.write('''terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.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" {
description = "The ID of the Docker container."
value = docker_container.this.id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = docker_container.this.ip_address
}
''')
35 changes: 0 additions & 35 deletions app/media/MyTerraform/.github/workflows/terraform-ci.yml

This file was deleted.

16 changes: 7 additions & 9 deletions app/media/MyTerraform/main.tf
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
provider "aws" {
region = "us-east-1"
provider "docker" {
host = var.docker_host
}

resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"

tags = {
Name = "MyEC2Instance"
}
module "docker" {
source = "./modules/docker"
image = var.image
name = var.container_name
ports = var.ports
}
8 changes: 8 additions & 0 deletions app/media/MyTerraform/modules/docker/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "docker_container" "this" {
name = var.name
image = var.image
ports {
internal = var.ports[0]
external = var.ports[1]
}
}
9 changes: 9 additions & 0 deletions app/media/MyTerraform/modules/docker/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "container_id" {
description = "The ID of the Docker container."
value = docker_container.this.id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = docker_container.this.ip_address
}
3 changes: 3 additions & 0 deletions app/media/MyTerraform/modules/docker/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
image = "nginx:latest"
name = "my_nginx"
ports = ["80", "80"]
14 changes: 14 additions & 0 deletions app/media/MyTerraform/modules/docker/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "image" {
description = "The Docker image to use."
type = string
}

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

variable "ports" {
description = "List of ports for the container."
type = list(string)
}
10 changes: 10 additions & 0 deletions app/media/MyTerraform/modules/docker/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.0"
}
}
}
9 changes: 9 additions & 0 deletions app/media/MyTerraform/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
output "container_id" {
description = "The ID of the Docker container."
value = module.docker.container_id
}

output "container_ip" {
description = "The IP address of the Docker container."
value = module.docker.container_ip
}
4 changes: 4 additions & 0 deletions app/media/MyTerraform/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
docker_host = "tcp://localhost:2375"
image = "nginx:latest"
container_name = "my_nginx"
ports = ["80:80"]
26 changes: 14 additions & 12 deletions app/media/MyTerraform/variables.tf
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
variable "region" {{
description = "AWS region"
variable "docker_host" {
description = "The Docker host Uri."
type = string
default = "us-east-1"
}}
}

variable "instance_type" {{
description = "EC2 Instance type"
variable "image" {
description = "The Docker image to use."
type = string
default = "t2.micro"
}}
}

variable "ami" {{
description = "AMI ID"
variable "container_name" {
description = "The name of the Docker container."
type = string
default = "ami-0c55b159cbfafe1f0"
}}
}

variable "ports" {
description = "List of ports to expose."
type = list(string)
}
10 changes: 10 additions & 0 deletions app/media/MyTerraform/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = ">= 1.0"

required_providers {
docker = {
source = "hashicorp/docker"
version = ">= 2.0"
}
}
}
Binary file modified app/models/__pycache__/terraform_models.cpython-311.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion app/models/terraform_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class IaCTemplateGeneration(BaseModel):

@validator("base_config")
def validate_base_config(cls, value):
allowed_configs = ['ec2', 's3', 'rds']
allowed_configs = ['ec2', 's3', 'rds','docker']
if value not in allowed_configs:
raise ValueError(f"Base config must be one of {allowed_configs}.")
return value
Expand Down
Loading