Skip to content

Commit

Permalink
feature: Merge pull request #15 from abolfazl8131/feature/heml_prompt
Browse files Browse the repository at this point in the history
add a prompt to generate a helm project
  • Loading branch information
abolfazl8131 authored Oct 28, 2024
2 parents dd4ac9f + a0db3d3 commit 6005ce6
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 153 deletions.
Binary file modified app/__pycache__/main.cpython-311.pyc
Binary file not shown.
Binary file modified 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 modified app/__pycache__/services.cpython-311.pyc
Binary file not shown.
98 changes: 0 additions & 98 deletions app/directory_generators/directory_generator.py

This file was deleted.

82 changes: 82 additions & 0 deletions app/directory_generators/helm_generator.py
Original file line number Diff line number Diff line change
@@ -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
""")
88 changes: 88 additions & 0 deletions app/directory_generators/terraform_generator.py
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 14 additions & 4 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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')


Expand Down
36 changes: 36 additions & 0 deletions app/media/MyHelm/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/media/MyHelm/Chart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v2
name: myhelm
description: A Helm chart for Kubernetes
version: 0.1.0
12 changes: 12 additions & 0 deletions app/media/MyHelm/values.yaml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 }}
Loading

0 comments on commit 6005ce6

Please sign in to comment.