diff --git a/app/__pycache__/main.cpython-311.pyc b/app/__pycache__/main.cpython-311.pyc index 8f4f4af2..40186295 100644 Binary files a/app/__pycache__/main.cpython-311.pyc and b/app/__pycache__/main.cpython-311.pyc differ diff --git a/app/__pycache__/models.cpython-311.pyc b/app/__pycache__/models.cpython-311.pyc index 6f14027f..1ac01e0d 100644 Binary files a/app/__pycache__/models.cpython-311.pyc and b/app/__pycache__/models.cpython-311.pyc differ diff --git a/app/__pycache__/prompt_generators.cpython-311.pyc b/app/__pycache__/prompt_generators.cpython-311.pyc index c0606ae4..9ffae021 100644 Binary files a/app/__pycache__/prompt_generators.cpython-311.pyc and b/app/__pycache__/prompt_generators.cpython-311.pyc differ diff --git a/app/__pycache__/services.cpython-311.pyc b/app/__pycache__/services.cpython-311.pyc index 3447cd80..2ff8af20 100644 Binary files a/app/__pycache__/services.cpython-311.pyc and b/app/__pycache__/services.cpython-311.pyc differ diff --git a/app/directory_generators/directory_generator.py b/app/directory_generators/directory_generator.py deleted file mode 100644 index 92e7a28a..00000000 --- a/app/directory_generators/directory_generator.py +++ /dev/null @@ -1,98 +0,0 @@ -import os - -project_name = "MyTerraform" -base_dir = "app/media/" -terraform_dir = os.path.join(base_dir, project_name, "terraform") -modules_dir = os.path.join(terraform_dir, "modules") -ci_dir = os.path.join(base_dir, project_name, ".github", "workflows") - -os.makedirs(terraform_dir, exist_ok=True) -os.makedirs(modules_dir, exist_ok=True) -os.makedirs(ci_dir, exist_ok=True) - -# Create main.tf -with open(os.path.join(terraform_dir, "main.tf"), 'w') as f: - f.write(f'''provider "aws" {{ - region = "us-west-2" -}} - -module "ec2_instance" {{ - source = "./modules/ec2" - - instance_type = "t2.micro" - ami = "ami-0c55b159cbfafe1f0" # Update with a valid AMI ID -}} -''') - -# Create variables.tf -with open(os.path.join(terraform_dir, "variables.tf"), 'w') as f: - f.write('''variable "instance_type" { - description = "Type of EC2 instance" - type = string - default = "t2.micro" -} - -variable "ami" { - description = "AMI ID for the EC2 instance" - type = string -} -''') - -# Create outputs.tf -with open(os.path.join(terraform_dir, "outputs.tf"), 'w') as f: - f.write('''output "instance_id" { - value = module.ec2_instance.instance_id -} -''') - -# Create EC2 module directory -ec2_module_dir = os.path.join(modules_dir, "ec2") -os.makedirs(ec2_module_dir, exist_ok=True) - -# Create ec2/main.tf for the module -with open(os.path.join(ec2_module_dir, "main.tf"), 'w') as f: - f.write('''resource "aws_instance" "this" { - ami = var.ami - instance_type = var.instance_type - - tags = { - Name = "TerraformEC2Instance" - } -} - -output "instance_id" { - value = aws_instance.this.id -} -''') - -# Create CI pipeline file -with open(os.path.join(ci_dir, "terraform.yml"), 'w') as f: - f.write('''name: Terraform - -on: - push: - 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 -''') \ No newline at end of file diff --git a/app/directory_generators/helm_generator.py b/app/directory_generators/helm_generator.py new file mode 100644 index 00000000..a9e125cd --- /dev/null +++ b/app/directory_generators/helm_generator.py @@ -0,0 +1,82 @@ +import os + +# Define the project structure +project_name = "app/media/MyHelm" +directories = ["charts", "crds", "templates"] +files = ["Chart.yaml", "values.yaml"] + +# Define default content for Chart.yaml and values.yaml +chart_yaml_content = """apiVersion: v2 +name: myhelm +description: A Helm chart for Kubernetes +version: 0.1.0 +""" +values_yaml_content = """# Default values for myhelm. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: myimage + pullPolicy: IfNotPresent + tag: "" +service: + name: myservice + type: ClusterIP + port: 80 +""" + +# Create the project structure +os.makedirs(project_name, exist_ok=True) + +for directory in directories: + os.makedirs(os.path.join(project_name, directory), exist_ok=True) + +for file in files: + file_path = os.path.join(project_name, file) + with open(file_path, 'w') as f: + if file == "Chart.yaml": + f.write(chart_yaml_content) + elif file == "values.yaml": + f.write(values_yaml_content) + +# Create a basic GitHub Actions workflow file +github_actions_dir = os.path.join(project_name, ".github/workflows") +os.makedirs(github_actions_dir, exist_ok=True) +with open(os.path.join(github_actions_dir, "ci.yml"), 'w') as f: + f.write("""name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + file: Dockerfile + push: true + tags: myimage:latest +""") \ No newline at end of file diff --git a/app/directory_generators/terraform_generator.py b/app/directory_generators/terraform_generator.py new file mode 100644 index 00000000..ae23fedb --- /dev/null +++ b/app/directory_generators/terraform_generator.py @@ -0,0 +1,88 @@ +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" + type = string + default = "us-east-1" +}} + +variable "instance_type" {{ + description = "EC2 Instance type" + type = string + default = "t2.micro" +}} + +variable "ami" {{ + description = "AMI ID" + 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) \ No newline at end of file diff --git a/app/main.py b/app/main.py index 0f9cface..56372f14 100644 --- a/app/main.py +++ b/app/main.py @@ -7,14 +7,15 @@ from .models import (IaCBasicInput, IaCBugfixInput, Output, - IaCInstallationInput,IaCTemplateGeneration) + IaCInstallationInput,IaCTemplateGeneration,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) + IaC_template_generator,helm_template_generator) + import os app = FastAPI() @@ -49,8 +50,17 @@ async def IaC_template_generation(request:IaCTemplateGeneration) -> Output: generated_prompt = IaC_template_generator(request) output = gpt_service(generated_prompt) - edit_directory_generator(output) - execute_pythonfile() + 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') diff --git a/app/media/MyHelm/.github/workflows/ci.yml b/app/media/MyHelm/.github/workflows/ci.yml new file mode 100644 index 00000000..6f41b6c7 --- /dev/null +++ b/app/media/MyHelm/.github/workflows/ci.yml @@ -0,0 +1,36 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v1 + + - name: Cache Docker layers + uses: actions/cache@v2 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + + - name: Build and push Docker image + uses: docker/build-push-action@v2 + with: + context: . + file: Dockerfile + push: true + tags: myimage:latest diff --git a/app/media/MyHelm/Chart.yaml b/app/media/MyHelm/Chart.yaml new file mode 100644 index 00000000..e4501d66 --- /dev/null +++ b/app/media/MyHelm/Chart.yaml @@ -0,0 +1,4 @@ +apiVersion: v2 +name: myhelm +description: A Helm chart for Kubernetes +version: 0.1.0 diff --git a/app/media/MyHelm/values.yaml b/app/media/MyHelm/values.yaml new file mode 100644 index 00000000..0739cc7c --- /dev/null +++ b/app/media/MyHelm/values.yaml @@ -0,0 +1,12 @@ +# Default values for myhelm. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. +replicaCount: 1 +image: + repository: myimage + pullPolicy: IfNotPresent + tag: "" +service: + name: myservice + type: ClusterIP + port: 80 diff --git a/app/media/MyTerraform/.github/workflows/terraform.yml b/app/media/MyTerraform/.github/workflows/terraform-ci.yml similarity index 62% rename from app/media/MyTerraform/.github/workflows/terraform.yml rename to app/media/MyTerraform/.github/workflows/terraform-ci.yml index 6e433644..8c6e977e 100644 --- a/app/media/MyTerraform/.github/workflows/terraform.yml +++ b/app/media/MyTerraform/.github/workflows/terraform-ci.yml @@ -1,16 +1,19 @@ -name: Terraform +name: Terraform CI on: push: branches: - main + pull_request: + branches: + - main jobs: terraform: runs-on: ubuntu-latest steps: - - name: Checkout Code + - name: Checkout code uses: actions/checkout@v2 - name: Set up Terraform @@ -26,3 +29,7 @@ jobs: - 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 }} diff --git a/app/media/MyTerraform/main.tf b/app/media/MyTerraform/main.tf new file mode 100644 index 00000000..71842c53 --- /dev/null +++ b/app/media/MyTerraform/main.tf @@ -0,0 +1,12 @@ +provider "aws" { + region = "us-east-1" +} + +resource "aws_instance" "web" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + + tags = { + Name = "MyEC2Instance" + } +} diff --git a/app/media/MyTerraform/terraform/main.tf b/app/media/MyTerraform/terraform/main.tf deleted file mode 100644 index b80468fe..00000000 --- a/app/media/MyTerraform/terraform/main.tf +++ /dev/null @@ -1,10 +0,0 @@ -provider "aws" { - region = "us-west-2" -} - -module "ec2_instance" { - source = "./modules/ec2" - - instance_type = "t2.micro" - ami = "ami-0c55b159cbfafe1f0" # Update with a valid AMI ID -} diff --git a/app/media/MyTerraform/terraform/modules/ec2/main.tf b/app/media/MyTerraform/terraform/modules/ec2/main.tf deleted file mode 100644 index 32f2548f..00000000 --- a/app/media/MyTerraform/terraform/modules/ec2/main.tf +++ /dev/null @@ -1,12 +0,0 @@ -resource "aws_instance" "this" { - ami = var.ami - instance_type = var.instance_type - - tags = { - Name = "TerraformEC2Instance" - } -} - -output "instance_id" { - value = aws_instance.this.id -} diff --git a/app/media/MyTerraform/terraform/outputs.tf b/app/media/MyTerraform/terraform/outputs.tf deleted file mode 100644 index 32d38a1d..00000000 --- a/app/media/MyTerraform/terraform/outputs.tf +++ /dev/null @@ -1,3 +0,0 @@ -output "instance_id" { - value = module.ec2_instance.instance_id -} diff --git a/app/media/MyTerraform/terraform/variables.tf b/app/media/MyTerraform/terraform/variables.tf deleted file mode 100644 index 74042d15..00000000 --- a/app/media/MyTerraform/terraform/variables.tf +++ /dev/null @@ -1,10 +0,0 @@ -variable "instance_type" { - description = "Type of EC2 instance" - type = string - default = "t2.micro" -} - -variable "ami" { - description = "AMI ID for the EC2 instance" - type = string -} diff --git a/app/media/MyTerraform/variables.tf b/app/media/MyTerraform/variables.tf new file mode 100644 index 00000000..e3b293c4 --- /dev/null +++ b/app/media/MyTerraform/variables.tf @@ -0,0 +1,17 @@ +variable "region" {{ + description = "AWS region" + type = string + default = "us-east-1" +}} + +variable "instance_type" {{ + description = "EC2 Instance type" + type = string + default = "t2.micro" +}} + +variable "ami" {{ + description = "AMI ID" + type = string + default = "ami-0c55b159cbfafe1f0" +}} diff --git a/app/models.py b/app/models.py index 63d95a7c..b3561532 100644 --- a/app/models.py +++ b/app/models.py @@ -25,7 +25,10 @@ class IaCTemplateGeneration(BaseModel): CI_integration:bool = True base_config:str = 'ec2' service:str = 'terraform' - + +class HelmTemplateGeneration(BaseModel): + CI_integration:bool = True + class Output(BaseModel): diff --git a/app/prompt_generators.py b/app/prompt_generators.py index 2ff89dfd..b0355450 100644 --- a/app/prompt_generators.py +++ b/app/prompt_generators.py @@ -1,6 +1,6 @@ from .models import (IaCBasicInput, IaCBugfixInput, - IaCInstallationInput, IaCTemplateGeneration) + IaCInstallationInput, IaCTemplateGeneration,HelmTemplateGeneration) def IaC_basics_generator(input : IaCBasicInput) -> str: @@ -53,5 +53,22 @@ def IaC_template_generator(input : IaCTemplateGeneration) -> str: the final terraform template must work very well without any error! + """ + return prompt + +def helm_template_generator(input : HelmTemplateGeneration) -> str: + + prompt = f""" + generate a correct python code to generate a helm project structure (project name: app/media/MyHelm) + based on the latest version of helm chart. + just generate a code to generate a folder as project template. don't consider base_dir + + CI integrated (using github actions) = {input.CI_integration}. + consider these directories : [charts/, crds/, templates/] + consider these files : Chart.yaml & values.yaml + + please set a something default in chart.yaml and values.yaml + + just Generate a python code without any additional notes or ```python3 entry """ return prompt \ No newline at end of file diff --git a/app/services.py b/app/services.py index 868cc423..d7d43569 100644 --- a/app/services.py +++ b/app/services.py @@ -34,17 +34,17 @@ def write_installation(request,output): save_to_mongo(data, index=['os','service'], collection = 'installation') -def edit_directory_generator(python_code): +def edit_directory_generator(gen_file,python_code): - with open('app/directory_generators/directory_generator.py', 'w') as file: + with open(f"app/directory_generators/{gen_file}.py", 'w') as file: file.write(python_code) -def execute_pythonfile(): - folder = 'app/media/MyTerraform' +def execute_pythonfile(folder,gen_file): + folder = f"app/media/{folder}" if os.path.isdir(folder): try: @@ -53,5 +53,5 @@ def execute_pythonfile(): except Exception as e: raise HTTPException(status_code=400, detail='please try again') - os.system('python3 app/directory_generators/directory_generator.py') - os.system('python3 app/directory_generators/directory_generator.py') + os.system(f"python3 app/directory_generators/{gen_file}.py") + os.system(f"python3 app/directory_generators/{gen_file}.py") diff --git a/app/test_services.py b/app/test_services.py deleted file mode 100644 index a05cbb3d..00000000 --- a/app/test_services.py +++ /dev/null @@ -1,6 +0,0 @@ -from .utils import save_QA_to_mongo - -def test_mongo_save(): - save_QA_to_mongo('q','a') - -