Skip to content

Commit c35a3a5

Browse files
authored
Merge pull request #111 from abolfazl8131/master
feat(ansible kuber): add the route and config adding to add configs t…
2 parents adc6763 + a3076bc commit c35a3a5

8 files changed

Lines changed: 75 additions & 29 deletions

File tree

app/directory_generators/ansible_generator.py

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

33
project_name = "app/media/MyAnsible"
4+
45
ansible_dir = project_name
56
group_vars_dir = os.path.join(ansible_dir, "group_vars")
67
host_vars_dir = os.path.join(ansible_dir, "host_vars")
@@ -25,27 +26,30 @@
2526
os.makedirs(templates_dir, exist_ok=True)
2627

2728
# Create ansible.cfg
28-
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as cfg_file:
29-
cfg_file.write("[defaults]\n")
30-
cfg_file.write("host_key_checking=false\n")
29+
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as ansible_cfg:
30+
ansible_cfg.write("[defaults]\n")
31+
ansible_cfg.write("host_key_checking=false\n")
3132

3233
# Create group_vars/docker_nodes
33-
with open(os.path.join(group_vars_dir, "docker_nodes"), "w") as gv_file:
34-
gv_file.write("ansible_port: 28\n")
35-
gv_file.write("ansible_user: root\n")
34+
with open(os.path.join(group_vars_dir, "docker_nodes"), "w") as docker_nodes:
35+
docker_nodes.write("ansible_port: 22\n")
36+
docker_nodes.write("ansible_user: root\n")
3637

3738
# Create hosts
3839
with open(os.path.join(ansible_dir, "hosts"), "w") as hosts_file:
3940
hosts_file.write("[docker_nodes]\n")
4041
hosts_file.write("www.example.com\n")
4142

43+
# Create empty host_vars directory
44+
open(os.path.join(host_vars_dir, ".gitkeep"), 'a').close()
45+
4246
# Create docker_playbook.yml
43-
with open(os.path.join(ansible_dir, "docker_playbook.yml"), "w") as playbook_file:
44-
playbook_file.write("- hosts: all\n")
45-
playbook_file.write(" roles:\n")
46-
playbook_file.write(" - install_docker\n")
47+
with open(os.path.join(ansible_dir, "docker_playbook.yml"), "w") as playbook:
48+
playbook.write("- hosts: all\n")
49+
playbook.write(" roles:\n")
50+
playbook.write(" - install_docker\n")
4751

48-
# Create tasks/main.yml
52+
# Create install_docker/tasks/main.yml
4953
with open(os.path.join(tasks_dir, "main.yml"), "w") as tasks_file:
5054
tasks_file.write("---\n")
5155
tasks_file.write("- name: Install prerequisite packages\n")
@@ -83,7 +87,7 @@
8387
tasks_file.write(" enabled: yes\n")
8488
tasks_file.write(" loop: \"{{ docker_services }}\"\n")
8589

86-
# Create vars/main.yml
90+
# Create install_docker/vars/main.yml
8791
with open(os.path.join(vars_dir, "main.yml"), "w") as vars_file:
8892
vars_file.write("prerequisite_packages:\n")
8993
vars_file.write(" - ca-certificates\n")
@@ -96,16 +100,4 @@
96100
vars_file.write(" - docker-ce-cli\n")
97101
vars_file.write(" - containerd.io\n")
98102
vars_file.write(" - docker-buildx-plugin\n")
99-
vars_file.write(" - docker-compose-plugin\n")
100-
101-
# Create empty host_vars directory
102-
os.makedirs(host_vars_dir, exist_ok=True)
103-
104-
# Create empty files directory
105-
os.makedirs(files_dir, exist_ok=True)
106-
107-
# Create empty handlers directory
108-
os.makedirs(handlers_dir, exist_ok=True)
109-
110-
# Create empty templates directory
111-
os.makedirs(templates_dir, exist_ok=True)
103+
vars_file.write(" - docker-compose-plugin\n")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
ansible_port: 28
1+
ansible_port: 22
22
ansible_user: root

app/media/MyAnsible/host_vars/.gitkeep

Whitespace-only changes.

app/models/ansible_models.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,22 @@ def validator_os(cls, value):
2929
valid_oss = ['ubuntu']
3030
if value not in valid_oss:
3131
raise ValueError(f"your selected OS must be in {valid_oss}")
32-
return value
32+
return value
33+
34+
35+
36+
class AnsibleInstallKuber(AnsibleBase):
37+
os: str = 'ubuntu'
38+
k8s_worker_nodes: List[str]
39+
k8s_master_nodes: List[str]
40+
lb_nodes: List[str]
41+
version:str = "1.31.2"
42+
43+
44+
@validator("os")
45+
def validator_os(cls, value):
46+
valid_oss = ['ubuntu']
47+
if value not in valid_oss:
48+
raise ValueError(f"your selected OS must be in {valid_oss}")
49+
return value
50+

app/routes/ansible.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from app.app_instance import app
22
from app.gpt_services import gpt_service
33
from app.services import (write_installation,edit_directory_generator,execute_pythonfile)
4-
5-
from app.models import (AnsibleInstallNginx,AnsibleInstallDocker,Output)
4+
from app.routes.utils import add_files_to_folder
5+
from app.models import (AnsibleInstallNginx,AnsibleInstallDocker,Output,AnsibleInstallKuber)
66

77
from app.models import (AnsibleInstallNginx,Output)
88

@@ -32,4 +32,25 @@ async def ansible_install_generation_docker(request:AnsibleInstallDocker) -> Out
3232
output = gpt_service(generated_prompt)
3333
edit_directory_generator("ansible_generator",output)
3434
execute_pythonfile("MyAnsible","ansible_generator")
35+
return Output(output='output')
36+
37+
38+
@app.post("/ansible-install/kuber/")
39+
async def ansible_install_generation_kuber(request:AnsibleInstallKuber) -> Output:
40+
41+
if os.environ.get("TEST"):
42+
return Output(output='output')
43+
generated_prompt = ansible_install_template(request,"kuber")
44+
45+
output = gpt_service(generated_prompt)
46+
edit_directory_generator("ansible_generator",output)
47+
execute_pythonfile("MyAnsible","ansible_generator")
48+
add_files_to_folder(files = ['app/media/kuber_configs/resolv.conf.j2'] , folder='app/media/MyAnsible/roles/preinstall/templates/')
49+
add_files_to_folder(files = ['app/media/kuber_configs/kubeadmcnf.yml.j2'] , folder='app/media/MyAnsible/roles/init_k8s/templates')
50+
add_files_to_folder(files = ['app/media/kuber_configs/kubeadmcnf-join.yml.j2'] , folder='app/media/MyAnsible/roles/join_master/templates')
51+
add_files_to_folder(files = ['app/media/kuber_configs/check_apiserveer.sh.j2',
52+
'app/media/kuber_configs/haproxy.cfg.j2',
53+
'app/media/kuber_configs/keepalived.conf.j2'
54+
] , folder='app/media/MyAnsible/roles/lb/templates')
55+
3556
return Output(output='output')

app/routes/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ def zip_folder(folder_path: str, output_zip_path: str):
1414
# Add file to the zip file
1515
zip_file.write(file_path, os.path.relpath(file_path, folder_path))
1616

17+
def add_files_to_folder(files:list,folder:str):
18+
19+
os.makedirs(folder, exist_ok=True)
1720

21+
for filename in files:
22+
os.path.join(folder, filename)
1823

1924

2025
@app.get("/download-folder{folder_name}/{source}")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
def ansible_kuber_install(input):
3+
pass
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
from .docker import ansible_docker_install
22
from .nginx import ansible_nginx_install
3+
from .kuber import ansible_kuber_install
34
from fastapi import HTTPException
45

56
def ansible_install_template(input_, tool:str):
7+
68
match tool:
9+
710
case 'nginx':
811
return ansible_nginx_install(input_)
12+
913
case 'docker':
1014
return ansible_docker_install(input_)
1115

16+
case 'kuber':
17+
return ansible_kuber_install(input_)
18+
1219
case _:
1320
raise HTTPException(400,"please select a valid tool for installation")

0 commit comments

Comments
 (0)