Skip to content
Snippets Groups Projects
Commit 6bc72214 authored by Benedetto Debora's avatar Benedetto Debora
Browse files

Refactoring: remove old files, add output folder for genereted files, add ansible templates

parent 761266d3
No related branches found
No related tags found
No related merge requests found
Showing
with 2 additions and 233 deletions
---
db_user: app1user
db_password: app1user
db_name: app1
\ No newline at end of file
resource "aws_subnet" "aws_subnet" {
vpc_id = aws_vpc.aws_vpc.id
cidr_block = "10.0.1.0/24"
tags = {
Name = "piacere_subnet"
}
}
resource "aws_vpc" "aws_vpc" {
cidr = "10.0.0.0/16"
tags = {
Name = "piacere_vpc"
}
}
resource "aws_db_instance" "education" {
identifier = "education"
instance_class = "db.t3.micro"
allocated_storage = 5
engine = "postgres"
engine_version = "13.1"
username = "edu"
password = var.db_password
db_subnet_group_name = aws_db_subnet_group.education.name
vpc_security_group_ids = [aws_security_group.rds.id]
parameter_group_name = aws_db_parameter_group.education.name
publicly_accessible = true
skip_final_snapshot = true
}
data "aws_ami" "ami1" {
#executable_users = ["self"]
most_recent = true
name_regex = "ubuntu*"
#owners = ["self"]
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "instance1" {
ami = data.aws_ami.ami1.id
instance_type = "t2.micro"
tags = {
Name = "firstvm"
}
}
data "aws_ami" "ami2" {
#executable_users = ["self"]
most_recent = true
name_regex = "ubuntu*"
#owners = ["self"]
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "instance2" {
ami = data.aws_ami.ami2.id
instance_type = "t2.micro"
tags = {
Name = "secondvm"
}
}
---
db_user: app1user
db_password: app1user
db_name: app1
\ No newline at end of file
---
WORDPRESS_DB_HOST: 10.10.10.10
WORDPRESS_DB_USER: app1user
WORDPRESS_DB_PASSWORD: app1user
WORDPRESS_DB_NAME: app1
WORDPRESS_TABLE_PREFIX: wp
\ No newline at end of file
File deleted
File deleted
import ansibleUtils
class InputData:
app_type: str
code_path: str
template_type: str
template_path: str
template_data: map
def __init__(self, app_type, code_path, template_type, template_path, template_data):
self.app_type = app_type
self.code_path = code_path
self.template_type = template_type
self.template_path = template_path
self.template_data = template_data
class TemplateInfo:
path: str
type: str
data: map
def __init__(self, input_data: InputData):
self.path = input_data.template_path
self.type = input_data.template_type
self.data = input_data.template_data
class AnsibleModule:
def get_template(self, template_complete_path):
template = open(template_complete_path, "r")
return template.readlines()
def edit_template(self, template_type, template_list, template_data, kind):
if template_type == "postgres":
new_file = ansibleUtils.databases_postgres(template_list, template_data, kind)
if template_type == "mysql":
new_file = ansibleUtils.databases_mysql(template_list, template_data, kind)
if template_type == "wordpress":
new_file = ansibleUtils.service_wordpress(template_list, template_data, kind)
return new_file
def write_file(self, edited_content, code_path: str):
file = open(code_path, "w+")
file.write(edited_content)
file.close()
class AnsibleICG:
def generate_code(self, input_data: InputData):
templateFile = TemplateInfo(input_data)
ansibleModule = AnsibleModule()
kinds = ["play", "vars"]
for kind in kinds:
template_complete_path = templateFile.path + templateFile.type + "-" + kind + ".tpl"
template_list = ansibleModule.get_template(template_complete_path)
edited_content = ansibleModule.edit_template(templateFile.type, template_list, templateFile.data, kind)
code_path = input_data.code_path + templateFile.type + "-" + kind + ".yml"
ansibleModule.write_file(edited_content, code_path)
from ansibleBuilder import *
def generic_matcher(template_list, specific_data):
matching_lines = [s for s in template_list if "###" in s]
for line in matching_lines:
line_plit = line.split('###')
var = line_plit[1]
index = template_list.index(line)
template_list[index] = line_plit[0]+specific_data[var]+line_plit[2]
return template_list
def databases_postgres(template_list, template_data, kind):
if kind == "vars":
new_template_list = generic_matcher(template_list, template_data[kind])
if kind == "play":
if template_data[kind]["OS"] == "debian":
specific_data = {"OS": "apt", "OS_PACKETS": " - postgresql-10"}
elif template_data[kind]["OS"] == "centos":
specific_data = {"OS": "yum", "OS_PACKETS": " - postgresql10\n - postgresql10-server\n - postgresql10-contrib\n - postgresql10-libs"}
new_template_list = generic_matcher(template_list, specific_data)
return "".join(new_template_list)
def databases_mysql(template_list, template_data, kind):
if kind == "vars":
new_template_list = generic_matcher(template_list, template_data[kind])
if kind == "play":
if template_data[kind]["OS"] == "debian":
specific_data = {"OS": "apt", "OS_PACKETS": " - mysql-server\n - mysql-client\n - python-setuptools\n - python-mysqldb\n - libmysqlclient-dev\n - python3-pip"}
elif template_data[kind]["OS"] == "centos":
specific_data = {"OS": "yum", "OS_PACKETS": " - postgresql10\n - postgresql10-server\n - postgresql10-contrib\n - postgresql10-libs"}
new_template_list = generic_matcher(template_list, specific_data)
return "".join(new_template_list)
def service_wordpress(template_list, template_data, kind):
if kind == "vars":
new_template_list = generic_matcher(template_list, template_data[kind])
if kind == "play":
if template_data[kind]["OS"] == "debian":
specific_data = {"OS": "apt", "OS_PACKETS": " - python3\n - python3-pip\n - docker\n - docker.io"}
elif template_data[kind]["OS"] == "centos":
specific_data = {"OS": "yum", "OS_PACKETS": " - docker"}
new_template_list = generic_matcher(template_list, specific_data)
return "".join(new_template_list)
\ No newline at end of file
...@@ -21,7 +21,7 @@ def create_infrastructure_files(intermediate_representation: dict = Body(...)): ...@@ -21,7 +21,7 @@ def create_infrastructure_files(intermediate_representation: dict = Body(...)):
return FileResponse(compress_file_path, media_type='application/octet-stream', filename=compress_file_name) return FileResponse(compress_file_path, media_type='application/octet-stream', filename=compress_file_name)
def choose_plugin(parameters): def choose_plugin(parameters):
# os.system('rm -f /opt/Output-code/*') # os.system('rm -f /opt/output_files_generated/*')
for step in parameters["steps"]: for step in parameters["steps"]:
if step["programming_language"] == "ansible": if step["programming_language"] == "ansible":
input_data = step["data"] input_data = step["data"]
......
{ {
"output_path": "output_file_example/nginx_openstack/", "output_path": "output_files_generated/nginx_openstack/",
"steps": [ "steps": [
{ {
"programming_language": "terraform", "programming_language": "terraform",
......
{"provider": "aws", "network":{"subname": "Danilo", "vpcname": "Molteni"}, "vm":{"os": "ubuntu"}}
\ No newline at end of file
{"provider": "aws", "anagrafica":{"nome": "Danilo", "cognome": "Molteni"}, "vm":{"os": "ubuntu"}}
\ No newline at end of file
{"provider": "azurerm", "network":{"subname": "Danilo", "vpcname": "Molteni"}, "vm":{"source": "hashicorp/azurerm", "version": "~>2.0"}}
\ No newline at end of file
{"provider": "azurerm", "anagrafica":{"nome": "Danilo", "cognome": "Molteni"}, "vm":{"source": "hashicorp/azurerm", "version": "~>2.0"}}
\ No newline at end of file
{"provider": "gcp", "network":{"subname": "Danilo", "vpcname": "Molteni"}, "vm":{"name": "flask-vm-$8"}}
\ No newline at end of file
{"provider": "gcp", "anagrafica":{"nome": "Danilo", "cognome": "Molteni"}, "vm":{"name": "flask-vm-$8"}}
\ No newline at end of file
{
"provider": "aws",
"network": [
{
"id": 1,
"subname": "Danilo",
"vpcname": "Molteni"
}
],
"vm": [
{
"id": 1,
"ram": 128,
"cpu": 32
},
{
"id": 2,
"ram": 256,
"cpu": 64
}
],
"db": [
{
"id": 1,
"engine": "postgres",
"username": "danilo",
"storage": 1
}
]
}
\ No newline at end of file
---
provider: aws
network:
- id: 1
subname: Danilo
vpcname: Molteni
vm:
- id: 1
ram : 128
cpu : 32
- id: 2
ram : 256
cpu : 64
db:
- id: 1
engine: postgres
username: danilo
storage: 1
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment