Skip to content

Commit

Permalink
Merge pull request #86 from abolfazl8131/master
Browse files Browse the repository at this point in the history
fix(argocd): remove sink options
  • Loading branch information
abolfazl8131 authored Nov 22, 2024
2 parents 5966658 + eb430a7 commit e12629c
Show file tree
Hide file tree
Showing 16 changed files with 653 additions and 7 deletions.
317 changes: 316 additions & 1 deletion app/directory_generators/terraform_generator.py
Original file line number Diff line number Diff line change
@@ -1 +1,316 @@
Hello! It looks like your message might have been empty. How can I assist you today?
import os
project_name = "app/media/MyTerraform"
modules_dir = os.path.join(project_name, "modules")
ec2_dir = os.path.join(modules_dir, "ec2")

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

# Create main.tf
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
main_file.write('''
provider "aws" {
region = "us-east-1"
}
module "ec2" {
source = "./modules/ec2"
key_pair_create = var.key_pair_create
key_pair_name = var.key_pair_name
security_group_create = var.security_group_create
security_group_name = var.security_group_name
security_group_ingress_rules = var.security_group_ingress_rules
security_group_egress_rule = var.security_group_egress_rule
instance_create = var.instance_create
instance_type = var.instance_type
ami_from_instance_create = var.ami_from_instance_create
ami_name = var.ami_name
}
''')

# Create variables.tf
with open(os.path.join(project_name, "variables.tf"), "w") as variables_file:
variables_file.write('''
variable "key_pair_create" {
type = bool
}
variable "key_pair_name" {
type = string
}
variable "security_group_create" {
type = bool
}
variable "security_group_name" {
type = string
}
variable "security_group_ingress_rules" {
type = map(object({
description = string
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
}))
}
variable "security_group_egress_rule" {
type = object({
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
})
}
variable "instance_create" {
type = bool
}
variable "instance_type" {
type = string
}
variable "ami_from_instance_create" {
type = bool
}
variable "ami_name" {
type = string
}
''')

# Create terraform.tfvars
with open(os.path.join(project_name, "terraform.tfvars"), "w") as tfvars_file:
tfvars_file.write('''
key_pair_create = true
key_pair_name = "ec2"
security_group_create = true
security_group_name = "my_rules"
security_group_ingress_rules = {
ssh_rule = {
description = "SSH Ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
http_rule = {
description = "HTTP Ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
security_group_egress_rule = {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
instance_create = false
instance_type = "t2.micro"
ami_from_instance_create = true
ami_name = "my-own-ami"
''')

# Create versions.tf
with open(os.path.join(project_name, "versions.tf"), "w") as versions_file:
versions_file.write('''
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.20"
}
}
}
''')

# Create ec2 module files
with open(os.path.join(ec2_dir, "terraform.pub"), "w") as pub_file:
pass

with open(os.path.join(ec2_dir, "main.tf"), "w") as ec2_main_file:
ec2_main_file.write('''
data "aws_ami" "linux" {
most_recent = true
owners = ["amazon"]
filter {
name = "name"
values = ["al2023-ami-2023*kernel-6.1-x86_64"]
}
filter {
name = "root-device-type"
values = ["ebs"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
}
resource "aws_key_pair" "key_pair" {
count = var.key_pair_create ? 1 : 0
key_name = var.key_pair_name
public_key = file("${path.module}/terraform.pub")
}
resource "aws_security_group" "security_group" {
count = var.security_group_create ? 1 : 0
name = var.security_group_name
dynamic "ingress" {
for_each = var.security_group_ingress_rules
content {
description = ingress.value["description"]
from_port = ingress.value["from_port"]
to_port = ingress.value["to_port"]
protocol = ingress.value["protocol"]
cidr_blocks = ingress.value["cidr_blocks"]
}
}
egress {
from_port = var.security_group_egress_rule["from_port"]
to_port = var.security_group_egress_rule["to_port"]
protocol = var.security_group_egress_rule["protocol"]
cidr_blocks = var.security_group_egress_rule["cidr_blocks"]
}
}
resource "aws_instance" "instance" {
count = var.instance_create ? 1 : 0
ami = data.aws_ami.linux.id
instance_type = var.instance_type
key_name = var.key_pair_create ? aws_key_pair.key_pair[0].key_name : null
vpc_security_group_ids = var.security_group_create ? [aws_security_group.security_group[0].id] : null
}
resource "aws_ami_from_instance" "ami" {
count = var.instance_create && var.ami_from_instance_create ? 1 : 0
name = var.ami_name
source_instance_id = aws_instance.instance[0].id
}
''')

with open(os.path.join(ec2_dir, "variables.tf"), "w") as ec2_variables_file:
ec2_variables_file.write('''
variable "key_pair_create" {
type = bool
}
variable "key_pair_name" {
type = string
}
variable "security_group_create" {
type = bool
}
variable "security_group_name" {
type = string
}
variable "security_group_ingress_rules" {
type = map(object({
description = string
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
}))
}
variable "security_group_egress_rule" {
type = object({
from_port = number
to_port = number
protocol = string
cidr_blocks = list(string)
})
}
variable "instance_create" {
type = bool
}
variable "instance_type" {
type = string
}
variable "ami_from_instance_create" {
type = bool
}
variable "ami_name" {
type = string
}
''')

with open(os.path.join(ec2_dir, "terraform.tfvars"), "w") as ec2_tfvars_file:
ec2_tfvars_file.write('''
key_pair_create = true
key_pair_name = "ec2"
security_group_create = true
security_group_name = "my_rules"
security_group_ingress_rules = {
ssh_rule = {
description = "SSH Ingress"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
},
http_rule = {
description = "HTTP Ingress"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
security_group_egress_rule = {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
instance_create = false
instance_type = "t2.micro"
ami_from_instance_create = true
ami_name = "my-own-ami"
''')

with open(os.path.join(ec2_dir, "versions.tf"), "w") as ec2_versions_file:
ec2_versions_file.write('''
terraform {
required_version = ">= 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 5.20"
}
}
}
''')
25 changes: 25 additions & 0 deletions app/media/MyHelm/templates/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "myhelm.fullname" . }}
spec:
replicas: {{ .Values.web.replicas }}
selector:
matchLabels:
app: {{ include "myhelm.name" . }}
template:
metadata:
labels:
app: {{ include "myhelm.name" . }}
spec:
containers:
- name: {{ .Values.web.image }}
image: {{ .Values.web.image }}:latest
ports:
- containerPort: {{ .Values.web.targetPort }}
env:
{{- range .Values.web.env }}
- name: {{ .name }}
value: {{ .value }}
{{- end }}

14 changes: 14 additions & 0 deletions app/media/MyHelm/templates/helpers.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{/*
Helper template functions
*/}}
{{- define "myhelm.name" -}}
{{- if .Chart.Name -}}
{{ .Chart.Name | quote }}
{{- else -}}
""
{{- end -}}
{{- end -}}

{{- define "myhelm.fullname" -}}
{{- .Release.Name | replace "-" "" }}-{{ include "myhelm.name" . }}
{{- end -}}
7 changes: 7 additions & 0 deletions app/media/MyHelm/templates/secrets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: v1
kind: Secret
metadata:
name: {{ include "myhelm.fullname" . }}-secret
type: Opaque
data:
# Add your secret data here
11 changes: 11 additions & 0 deletions app/media/MyHelm/templates/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: v1
kind: Service
metadata:
name: {{ include "myhelm.fullname" . }}
spec:
type: ClusterIP
ports:
- port: {{ .Values.web.targetPort }}
targetPort: {{ .Values.web.targetPort }}
selector:
app: {{ include "myhelm.name" . }}
1 change: 1 addition & 0 deletions app/media/MyTerraform/.terraform/modules/modules.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"ec2","Source":"./modules/ec2","Dir":"modules/ec2"}]}
Loading

0 comments on commit e12629c

Please sign in to comment.