Skip to content

Commit

Permalink
Fix/ansible templates (#142)
Browse files Browse the repository at this point in the history
* feat(compose): comelete compose prompt

* fix(kuber): remove lb

* feat(compose): compelete compose prompt

* nothing

* fix(compose): totally restructre docker compose generator

* fix(compose): directory builder

* fix(compose): compelete compose generation allgorithm

* fix(compose): edit default values for documentation

* feat(compose): add union type input for networks

* fix(routes): add /api to all routes

* fix(installation): fix terraform installation process and model

* fix(installation): create MyBash for scripts

* fix(bash): edit bi/bash

* fix(docker install): fix it

* feat(install): add jenkins and gitlab installation

* Update unit-test.yml

* fix(install): fix jenkins and gitlab

* fix(crawler): fix the crowler to crawl 2 aws urls

* fix(ansible_nginx): fix nginx file generartion

* fix(docker_ansible): fix docker ansible template generation

* fix(ansible_kuber): fix ansible kuber section

* fix(kuber playbook): fix kuber playbook

* fix(kuber playbook): fix kuber playbook

* fix(ansible kuber): fix yaml formats

* fix(ansible kuber): fix yaml formats
  • Loading branch information
abolfazl8131 authored Dec 12, 2024
1 parent b502288 commit a4f7042
Show file tree
Hide file tree
Showing 20 changed files with 1,087 additions and 1,871 deletions.
883 changes: 77 additions & 806 deletions app/directory_generators/ansible_generator.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion app/media/MyAnsible/group_vars/all
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ docker_apt_repo: "https://download.docker.com/linux/ubuntu"
kubernetes_gpg_keyring_path: "/etc/apt/keyrings/kubernetes-apt-keyring.gpg"
kubernetes_gpg_key_url: "https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key"
kubernetes_apt_repo: "https://pkgs.k8s.io/core:/stable:/v1.31/deb/"
k8s_version: "1.31.2" # see here https://kubernetes.io/releases/patch-releases/ and https://github.com/kubernetes/kubernetes/releases
k8s_version: 1.31 # see here https://kubernetes.io/releases/patch-releases/ and https://github.com/kubernetes/kubernetes/releases

# CRI
cri_socket: unix:///var/run/containerd/containerd.sock
Expand All @@ -35,3 +35,4 @@ ansible_port: 22
ansible_python_interpreter: "/usr/bin/python3"
domain: "devopsgpt.com"
apiserver_url: "devopsgpt.com"

2 changes: 1 addition & 1 deletion app/media/MyAnsible/hosts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ string
string

[k8s_workers]
string
string
3 changes: 3 additions & 0 deletions app/media/MyAnsible/kubernetes_playbook.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

- hosts: all
roles:
- role: preinstall
Expand Down Expand Up @@ -36,3 +37,5 @@
gather_facts: yes
any_errors_fatal: true
tags: [join_worker]


2 changes: 2 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/cni.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@
delegate_to: "{{ groups['k8s_masters'][0] }}"
when: calico_crd_check.rc != 0
run_once: true


1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/initk8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@
- name: Example Task After Reboot
debug:
msg: "Server back online and ready for tasks."

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/init_k8s/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@

- name: Initialize Calico CNI
include_tasks: cni.yml

Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,4 @@
state: present
when:
- inventory_hostname in groups['k8s_masters']

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/join_master/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

- name: Join master(s) node to cluster
include_tasks: join_master.yml

Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
when:
- inventory_hostname not in groups['k8s_masters']
- not kubeadm_already_run.stat.exists

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/join_worker/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@

- name: Join worker(s) node to cluster
include_tasks: join_worker.yml

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/k8s/handlers/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
service:
name: kubelet
state: restarted

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/k8s/tasks/k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,4 @@
- name: Pull Kubernetes images | If you got error check your dns and sanction
command:
cmd: kubeadm config images pull

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/k8s/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: Install kubernetes packages
include_tasks: k8s.yml

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/preinstall/tasks/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,4 @@
- name: Fix broken packages
apt:
state: fixed

1 change: 1 addition & 0 deletions app/media/MyAnsible/roles/preinstall/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
- name: basic setup
include_tasks: basic.yml

39 changes: 25 additions & 14 deletions app/routes/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,54 @@

from app.template_generators.ansible.install.main import ansible_install_template
import os
import shutil

@app.post("/api/ansible-install/nginx/")
async def ansible_install_generation_nginx(request:AnsibleInstallNginx) -> Output:



if os.environ.get("TEST"):
return Output(output='output')
generated_prompt = ansible_install_template(request,"nginx")

dir = 'app/media/MyAnsible'
if os.path.exists(dir):
shutil.rmtree(dir)

ansible_install_template(request,"nginx")

output = gpt_service(generated_prompt)
edit_directory_generator("ansible_generator",output)
execute_pythonfile("MyAnsible","ansible_generator")

return Output(output='output')


@app.post("/api/ansible-install/docker/")
async def ansible_install_generation_docker(request:AnsibleInstallDocker) -> Output:


if os.environ.get("TEST"):
return Output(output='output')
generated_prompt = ansible_install_template(request,"docker")

dir = 'app/media/MyAnsible'
if os.path.exists(dir):
shutil.rmtree(dir)

ansible_install_template(request,"docker")

output = gpt_service(generated_prompt)
edit_directory_generator("ansible_generator",output)
execute_pythonfile("MyAnsible","ansible_generator")
return Output(output='output')


@app.post("/api/ansible-install/kuber/")
async def ansible_install_generation_kuber(request:AnsibleInstallKuber) -> Output:



if os.environ.get("TEST"):
return Output(output='output')
generated_prompt = ansible_install_template(request,"kuber")

output = gpt_service(generated_prompt)
edit_directory_generator("ansible_generator",output)
execute_pythonfile("MyAnsible","ansible_generator")

dir = 'app/media/MyAnsible'
if os.path.exists(dir):
shutil.rmtree(dir)

ansible_install_template(request,"kuber")
add_files_to_folder(files = ['app/media/kuber_configs/resolv.conf.j2'] , folder='app/media/MyAnsible/roles/preinstall/templates/')
add_files_to_folder(files = ['app/media/kuber_configs/kubeadmcnf.yml.j2'] , folder='app/media/MyAnsible/roles/init_k8s/templates/')
add_files_to_folder(files = ['app/media/kuber_configs/kubeadmcnf-join.yml.j2'] , folder='app/media/MyAnsible/roles/join_master/templates/')
Expand Down
201 changes: 81 additions & 120 deletions app/template_generators/ansible/install/docker.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
def ansible_docker_install(input):

docker_hosts = input.hosts
Expand All @@ -13,131 +14,91 @@ def ansible_docker_install(input):



prompt = f"""
Generate a Python code to generate an Ansible project (project name is app/media/MyAnsible)
that dynamically provisions Ansible resources ensuring a modular, flexible structure. Only provide
Python code, no explanations or markdown formatting, without ```python entry.
The project should be organized as follows:



The structure of this project must be as follows:
```
├── ansible.cfg
├── group_vars
│   |── docker_nodes
│  
├── hosts
├── host_vars
├── docker_playbook.yml
└── roles
└── install_docker
├── defaults
│   └── main.yml
├── files
│   └── sample.sh
├── handlers
│   └── main.yml
├── tasks
│   └── main.yml
├── templates
│   └── sample.j2
└── vars
└── main.yml
```
- The content of ansible.cfg must be as follows:
```
[defaults]
host_key_checking=false
```
- group_vars directory includes a single file called "docker_nodes" and the content of this file must be as follows:
```
ansible_port: {docker_ansible_port}
ansible_user: {docker_ansible_user}
```
- there is file called "hosts" which its content must be as follows:
```
{docker_inventory}
```
- There is an empty directory called "host_vars" with no files included
- There is a file called "docker_playbook.yml" which its content must be as follows:
```
- hosts: all
roles:
- install_docker
```
- There is a directory called "roles" which a sub-directory called "install_docker" (roles/install_docker)
"install_docker" has multiple sub-directories, so let's dive deeper into each its sub-directories:
- (install_docker/tasks): This path has a file called "main.yml" which its content must be as follows:
```
---
- name: Install prerequisite packages
apt:
name: "{docker_items_in_task}"
state: present
loop: "{docker_prerequisite_packages_in_task}""
- name: Create directory for Docker keyrings
file:
path: /etc/apt/keyrings
state: directory
mode: '0755'
- name: Download Docker's official GPG key
get_url:
url: https://download.docker.com/linux/ubuntu/gpg
dest: /etc/apt/keyrings/docker.asc
mode: '0644'
- name: Add Docker repository to apt sources
copy:
content: |
deb [arch={ansible_architecture_in_task} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {ansible_distribution_release_in_task} stable
dest: /etc/apt/sources.list.d/docker.list
- name: Update apt cache after adding Docker repo
apt:
update_cache: yes
- name: Install Docker packages
apt:
name: "{docker_items_in_task}"
state: present
loop: "{docker_packages_in_task}""
- name: Ensure Docker and containerd services are started and enabled
service:
name: "{docker_items_in_task}"
state: started
enabled: yes
loop: "{docker_services_in_task}""
```
- (install_docker/vars): This path has a file called "main.yml" which its content must be as follows:
```
prerequisite_packages:
- ca-certificates
- curl
project_name = "app/media/MyAnsible"

docker_services:
- docker
- containerd
# Create project directories
os.makedirs(os.path.join(project_name, "group_vars"), exist_ok=True)
os.makedirs(os.path.join(project_name, "host_vars"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "defaults"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "files"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "handlers"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "tasks"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "templates"), exist_ok=True)
os.makedirs(os.path.join(project_name, "roles", "install_docker", "vars"), exist_ok=True)

docker_packages:
- docker-ce
- docker-ce-cli
- containerd.io
- docker-buildx-plugin
- docker-compose-plugin
```
# Create ansible.cfg
with open(os.path.join(project_name, "ansible.cfg"), "w") as ansible_cfg:
ansible_cfg.write("[defaults]\n")
ansible_cfg.write("host_key_checking=false\n")

finally just give me a python code without any note that can generate a project folder with the
given schema without ```python entry. and we dont need any base directory in the python code.
the final ansible template must work very well without any error!
# Create group_vars/docker_nodes
with open(os.path.join(project_name, "group_vars", "docker_nodes"), "w") as docker_nodes:
docker_nodes.write(f"ansible_port: {docker_ansible_port}\n")
docker_nodes.write(f"ansible_user: {docker_ansible_user}\n")

the python code you give me, must have structure like that:
# Create hosts
with open(os.path.join(project_name, "hosts"), "w") as hosts_file:
hosts_file.write(f"{docker_inventory}\n")


import os
project_name = "app/media/MyAnsible"
foo_dir = os.path.join(project_name, "bar")
x_dir = os.path.join(modules_dir, "y")
# Create docker_playbook.yml
with open(os.path.join(project_name, "docker_playbook.yml"), "w") as playbook:
playbook.write("- hosts: all\n")
playbook.write(" roles:\n")
playbook.write(" - install_docker\n")

# Create project directories
os.makedirs(ansible_dir, exist_ok=True)
# Create install_docker/tasks/main.yml
with open(os.path.join(project_name, "roles", "install_docker", "tasks", "main.yml"), "w") as tasks_file:
tasks_file.write("---\n")
tasks_file.write("- name: Install prerequisite packages\n")
tasks_file.write(" apt:\n")
tasks_file.write(" name: \"{{ item }}\"\n")
tasks_file.write(" state: present\n")
tasks_file.write(" loop: \"{{ prerequisite_packages }}\"\n")
tasks_file.write("- name: Create directory for Docker keyrings\n")
tasks_file.write(" file:\n")
tasks_file.write(" path: /etc/apt/keyrings\n")
tasks_file.write(" state: directory\n")
tasks_file.write(" mode: '0755'\n")
tasks_file.write("- name: Download Docker's official GPG key\n")
tasks_file.write(" get_url:\n")
tasks_file.write(" url: https://download.docker.com/linux/ubuntu/gpg\n")
tasks_file.write(" dest: /etc/apt/keyrings/docker.asc\n")
tasks_file.write(" mode: '0644'\n")
tasks_file.write("- name: Add Docker repository to apt sources\n")
tasks_file.write(" copy:\n")
tasks_file.write(" content: |\n")
tasks_file.write(" deb [arch={{ ansible_architecture }} signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable\n")
tasks_file.write(" dest: /etc/apt/sources.list.d/docker.list\n")
tasks_file.write("- name: Update apt cache after adding Docker repo\n")
tasks_file.write(" apt:\n")
tasks_file.write(" update_cache: yes\n")
tasks_file.write("- name: Install Docker packages\n")
tasks_file.write(" apt:\n")
tasks_file.write(" name: \"{{ item }}\"\n")
tasks_file.write(" state: present\n")
tasks_file.write(" loop: \"{{ docker_packages }}\"\n")
tasks_file.write("- name: Ensure Docker and containerd services are started and enabled\n")
tasks_file.write(" service:\n")
tasks_file.write(" name: \"{{ item }}\"\n")
tasks_file.write(" state: started\n")
tasks_file.write(" enabled: yes\n")
tasks_file.write(" loop: \"{{ docker_services }}\"\n")

# Create main.tf
with open(os.path.join(project_name, "main.tf"), "w") as main_file:
# any thing you need
"""
return prompt
# Create install_docker/vars/main.yml
with open(os.path.join(project_name, "roles", "install_docker", "vars", "main.yml"), "w") as vars_file:
vars_file.write("prerequisite_packages:\n")
vars_file.write(" - ca-certificates\n")
vars_file.write(" - curl\n\n")
vars_file.write("docker_services:\n")
vars_file.write(" - docker\n")
vars_file.write(" - containerd\n\n")
vars_file.write("docker_packages:\n")
vars_file.write(" - docker-ce\n")
vars_file.write(" - docker-ce-cli\n")
vars_file.write(" - containerd.io\n")
vars_file.write(" - docker-buildx-plugin\n")
vars_file.write(" - docker-compose-plugin\n")
Loading

0 comments on commit a4f7042

Please sign in to comment.