Skip to content

Commit

Permalink
Merge pull request #116 from abolfazl8131/115
Browse files Browse the repository at this point in the history
fix file adding problem with adding 'shutil'
  • Loading branch information
mohammadll authored Dec 1, 2024
2 parents 4aa2e1a + 472ad30 commit 13be510
Show file tree
Hide file tree
Showing 24 changed files with 696 additions and 136 deletions.
269 changes: 194 additions & 75 deletions app/directory_generators/ansible_generator.py
Original file line number Diff line number Diff line change
@@ -1,103 +1,222 @@
import os

project_name = "app/media/MyAnsible"

ansible_dir = project_name
group_vars_dir = os.path.join(ansible_dir, "group_vars")
host_vars_dir = os.path.join(ansible_dir, "host_vars")
roles_dir = os.path.join(ansible_dir, "roles")
install_docker_dir = os.path.join(roles_dir, "install_docker")
tasks_dir = os.path.join(install_docker_dir, "tasks")
vars_dir = os.path.join(install_docker_dir, "vars")
files_dir = os.path.join(install_docker_dir, "files")
handlers_dir = os.path.join(install_docker_dir, "handlers")
templates_dir = os.path.join(install_docker_dir, "templates")
preinstall_dir = os.path.join(roles_dir, "preinstall")
tasks_dir = os.path.join(preinstall_dir, "tasks")
defaults_dir = os.path.join(preinstall_dir, "defaults")
files_dir = os.path.join(preinstall_dir, "files")
handlers_dir = os.path.join(preinstall_dir, "handlers")
templates_dir = os.path.join(preinstall_dir, "templates")
vars_dir = os.path.join(preinstall_dir, "vars")

# Create project directories
os.makedirs(ansible_dir, exist_ok=True)
os.makedirs(group_vars_dir, exist_ok=True)
os.makedirs(host_vars_dir, exist_ok=True)
os.makedirs(roles_dir, exist_ok=True)
os.makedirs(install_docker_dir, exist_ok=True)
os.makedirs(preinstall_dir, exist_ok=True)
os.makedirs(tasks_dir, exist_ok=True)
os.makedirs(vars_dir, exist_ok=True)
os.makedirs(defaults_dir, exist_ok=True)
os.makedirs(files_dir, exist_ok=True)
os.makedirs(handlers_dir, exist_ok=True)
os.makedirs(templates_dir, exist_ok=True)
os.makedirs(vars_dir, exist_ok=True)

# Create ansible.cfg
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as ansible_cfg:
ansible_cfg.write("[defaults]\n")
ansible_cfg.write("host_key_checking=false\n")
with open(os.path.join(ansible_dir, "ansible.cfg"), "w") as cfg_file:
cfg_file.write("[defaults]\n")
cfg_file.write("host_key_checking=false\n")

# Create group_vars/docker_nodes
with open(os.path.join(group_vars_dir, "docker_nodes"), "w") as docker_nodes:
docker_nodes.write("ansible_port: 22\n")
docker_nodes.write("ansible_user: root\n")
# Create group_vars/all
with open(os.path.join(group_vars_dir, "all"), "w") as all_file:
all_file.write("# General\n")
all_file.write('install_ansible_modules: "true"\n')
all_file.write('disable_transparent_huge_pages: "true"\n')
all_file.write('setup_interface: "false"\n')
all_file.write("\n")
all_file.write("# Network Calico see here for more details https://github.com/projectcalico/calico/releases\n")
all_file.write('calico_operator_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/tigera-operator.yaml"\n')
all_file.write('calico_crd_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/custom-resources.yaml"\n')
all_file.write('pod_network_cidr: "192.168.0.0/16"\n')
all_file.write("\n")
all_file.write("# DNS\n")
all_file.write('resolv_nameservers: [8.8.8.8, 4.2.2.4] # 403.online\n')
all_file.write("\n")
all_file.write("# Sanction shekan\n")
all_file.write('use_iran: "true" # change it to "false" if you are outside of iran\n')
all_file.write("\n")
all_file.write("# Docker\n")
all_file.write('docker_gpg_key_url: "https://download.docker.com/linux/ubuntu/gpg"\n')
all_file.write('docker_gpg_key_path: "/etc/apt/keyrings/docker.gpg"\n')
all_file.write('docker_apt_repo: "https://download.docker.com/linux/ubuntu"\n')
all_file.write("\n")
all_file.write("# Kubernetes\n")
all_file.write('kubernetes_gpg_keyring_path: "/etc/apt/keyrings/kubernetes-apt-keyring.gpg"\n')
all_file.write('kubernetes_gpg_key_url: "https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key"\n')
all_file.write('kubernetes_apt_repo: "https://pkgs.k8s.io/core:/stable:/v1.31/deb/"\n')
all_file.write('k8s_version: "1.31.2" # see here https://kubernetes.io/releases/patch-releases/ and https://github.com/kubernetes/kubernetes/releases\n')
all_file.write("\n")
all_file.write("# CRI\n")
all_file.write('cri_socket: unix:///var/run/containerd/containerd.sock\n')
all_file.write("\n")
all_file.write("# VRRP and HAProxy\n")
all_file.write('interface_name: "enp0s8"\n')
all_file.write('virtual_ip: "192.168.178.100"\n')
all_file.write('haproxy_frontend_password: "password"\n')
all_file.write("\n")
all_file.write("# Ansible Connection\n")
all_file.write("\n")
all_file.write('ansible_user: root\n')
all_file.write('ansible_port: 22\n')
all_file.write('ansible_python_interpreter: "/usr/bin/python3"\n')
all_file.write('domain="devopsgpt.com"\n')
all_file.write('apiserver_url="devopsgpt.com"\n')

# Create hosts
with open(os.path.join(ansible_dir, "hosts"), "w") as hosts_file:
hosts_file.write("[docker_nodes]\n")
hosts_file.write("www.example.com\n")
hosts_file.write("[all]\n")
hosts_file.write("string private_ip=x.x.x.x\n")
hosts_file.write("string private_ip=x.x.x.x\n")
hosts_file.write("string private_ip=x.x.x.x\n")
hosts_file.write("\n")
hosts_file.write("[k8s]\n")
hosts_file.write("string\n")
hosts_file.write("string\n")
hosts_file.write("\n")
hosts_file.write("[k8s_masters]\n")
hosts_file.write("string\n")
hosts_file.write("\n")
hosts_file.write("[k8s_workers]\n")
hosts_file.write("string\n")
hosts_file.write("\n")
hosts_file.write("[lb]\n")
hosts_file.write("string\n")

# Create kubernetes_playbook.yml
with open(os.path.join(ansible_dir, "kubernetes_playbook.yml"), "w") as playbook_file:
playbook_file.write("- hosts: all\n")
playbook_file.write(" roles:\n")
playbook_file.write(" - role: preinstall\n")
playbook_file.write(" gather_facts: yes\n")
playbook_file.write(" any_errors_fatal: true\n")
playbook_file.write(" tags: [preinstall]\n")

# Create preinstall/tasks/basic.yml
with open(os.path.join(tasks_dir, "basic.yml"), "w") as basic_file:
basic_file.write("- name: Set timezone to UTC\n")
basic_file.write(" timezone:\n")
basic_file.write(" name: Etc/UTC\n")
basic_file.write("\n")
basic_file.write("- name: Set hostname\n")
basic_file.write(" command: hostnamectl set-hostname {{ inventory_hostname }}\n")
basic_file.write("\n")
basic_file.write("- name: Remove symlink resolve.conf\n")
basic_file.write(" file:\n")
basic_file.write(" path: \"/etc/resolv.conf\"\n")
basic_file.write(" state: absent\n")
basic_file.write(" ignore_errors: true\n")
basic_file.write(" when: use_iran == \"true\"\n")
basic_file.write("\n")
basic_file.write("- name: Configure resolv.conf\n")
basic_file.write(" template:\n")
basic_file.write(" src: \"resolv.conf.j2\"\n")
basic_file.write(" dest: \"/etc/resolv.conf\"\n")
basic_file.write(" mode: \"0644\"\n")
basic_file.write(" when: use_iran == \"true\"\n")
basic_file.write("\n")
basic_file.write("- name: Add hostname\n")
basic_file.write(" lineinfile:\n")
basic_file.write(" path: /etc/hosts\n")
basic_file.write(" regexp: '^127\\.0\\.0\\.1'\n")
basic_file.write(" line: \"127.0.0.1 {{ inventory_hostname }} localhost\"\n")
basic_file.write(" owner: root\n")
basic_file.write(" group: root\n")
basic_file.write(" mode: 0644\n")
basic_file.write("\n")
basic_file.write("- name: Install necessary tools\n")
basic_file.write(" apt:\n")
basic_file.write(" update_cache: true\n")
basic_file.write(" name:\n")
basic_file.write(" - vim\n")
basic_file.write(" - sudo\n")
basic_file.write(" - wget\n")
basic_file.write(" - curl\n")
basic_file.write(" - telnet\n")
basic_file.write(" - nload\n")
basic_file.write(" - s3cmd\n")
basic_file.write(" - cron\n")
basic_file.write(" - ipset\n")
basic_file.write(" - lvm2\n")
basic_file.write(" - python3\n")
basic_file.write(" - python3-setuptools\n")
basic_file.write(" - python3-pip\n")
basic_file.write(" - python3-apt\n")
basic_file.write(" - intel-microcode\n")
basic_file.write(" - htop\n")
basic_file.write(" - tcpdump\n")
basic_file.write(" - net-tools\n")
basic_file.write(" - screen\n")
basic_file.write(" - tmux\n")
basic_file.write(" - byobu\n")
basic_file.write(" - iftop\n")
basic_file.write(" - bmon\n")
basic_file.write(" - iperf\n")
basic_file.write(" - sysstat\n")
basic_file.write(" - ethtool\n")
basic_file.write(" - plocate\n")
basic_file.write(" - thin-provisioning-tools\n")
basic_file.write(" - conntrack\n")
basic_file.write(" - stress\n")
basic_file.write(" - cpufrequtils\n")
basic_file.write(" - rsync\n")
basic_file.write(" - xz-utils\n")
basic_file.write(" - build-essential\n")
basic_file.write(" - apt-transport-https\n")
basic_file.write(" - ca-certificates\n")
basic_file.write(" - software-properties-common\n")
basic_file.write(" - gnupg-agent\n")
basic_file.write(" - iptables-persistent\n")
basic_file.write(" - open-iscsi\n")
basic_file.write(" - nfs-common\n")
basic_file.write(" - tzdata\n")
basic_file.write(" - tree\n")
basic_file.write(" state: latest\n")
basic_file.write("\n")
basic_file.write("- name: Fix broken packages\n")
basic_file.write(" apt:\n")
basic_file.write(" state: fixed\n")

# Create preinstall/tasks/main.yml
with open(os.path.join(tasks_dir, "main.yml"), "w") as main_file:
main_file.write("---\n")
main_file.write("- name: basic setup\n")
main_file.write(" include_tasks: basic.yml\n")

# Create preinstall/defaults/main.yml
with open(os.path.join(defaults_dir, "main.yml"), "w") as defaults_file:
defaults_file.write("# Default variables for preinstall role\n")

# Create empty host_vars directory
open(os.path.join(host_vars_dir, ".gitkeep"), 'a').close()
# Create preinstall/files/sample.sh
with open(os.path.join(files_dir, "sample.sh"), "w") as sample_file:
sample_file.write("#!/bin/bash\n")
sample_file.write("# Sample script\n")

# Create docker_playbook.yml
with open(os.path.join(ansible_dir, "docker_playbook.yml"), "w") as playbook:
playbook.write("- hosts: all\n")
playbook.write(" roles:\n")
playbook.write(" - install_docker\n")
# Create preinstall/handlers/main.yml
with open(os.path.join(handlers_dir, "main.yml"), "w") as handlers_file:
handlers_file.write("# Handlers for preinstall role\n")

# Create install_docker/tasks/main.yml
with open(os.path.join(tasks_dir, "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 preinstall/templates/resolv.conf.j2
with open(os.path.join(templates_dir, "resolv.conf.j2"), "w") as resolv_file:
resolv_file.write("# Generated resolv.conf\n")
resolv_file.write("nameserver {{ item }}\n")
resolv_file.write("{% for item in resolv_nameservers %}\n")
resolv_file.write(" {{ item }}\n")
resolv_file.write("{% endfor %}\n")

# Create install_docker/vars/main.yml
# Create preinstall/vars/main.yml
with open(os.path.join(vars_dir, "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")
vars_file.write("# Variable definitions for preinstall role\n")
3 changes: 0 additions & 3 deletions app/media/MyAnsible/docker_playbook.yml

This file was deleted.

42 changes: 42 additions & 0 deletions app/media/MyAnsible/group_vars/all
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# General
install_ansible_modules: "true"
disable_transparent_huge_pages: "true"
setup_interface: "false"

# Network Calico see here for more details https://github.com/projectcalico/calico/releases
calico_operator_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/tigera-operator.yaml"
calico_crd_url: "https://raw.githubusercontent.com/projectcalico/calico/v3.29.0/manifests/custom-resources.yaml"
pod_network_cidr: "192.168.0.0/16"

# DNS
resolv_nameservers: [8.8.8.8, 4.2.2.4] # 403.online

# Sanction shekan
use_iran: "true" # change it to "false" if you are outside of iran

# Docker
docker_gpg_key_url: "https://download.docker.com/linux/ubuntu/gpg"
docker_gpg_key_path: "/etc/apt/keyrings/docker.gpg"
docker_apt_repo: "https://download.docker.com/linux/ubuntu"

# Kubernetes
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

# CRI
cri_socket: unix:///var/run/containerd/containerd.sock

# VRRP and HAProxy
interface_name: "enp0s8"
virtual_ip: "192.168.178.100"
haproxy_frontend_password: "password"

# Ansible Connection

ansible_user: root
ansible_port: 22
ansible_python_interpreter: "/usr/bin/python3"
domain="devopsgpt.com"
apiserver_url="devopsgpt.com"
2 changes: 0 additions & 2 deletions app/media/MyAnsible/group_vars/docker_nodes

This file was deleted.

Empty file.
19 changes: 17 additions & 2 deletions app/media/MyAnsible/hosts
Original file line number Diff line number Diff line change
@@ -1,2 +1,17 @@
[docker_nodes]
www.example.com
[all]
string private_ip=x.x.x.x
string private_ip=x.x.x.x
string private_ip=x.x.x.x

[k8s]
string
string

[k8s_masters]
string

[k8s_workers]
string

[lb]
string
6 changes: 6 additions & 0 deletions app/media/MyAnsible/kubernetes_playbook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
- hosts: all
roles:
- role: preinstall
gather_facts: yes
any_errors_fatal: true
tags: [preinstall]
13 changes: 13 additions & 0 deletions app/media/MyAnsible/roles/init_k8s/templates/kubeadmcnf.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
kind: InitConfiguration
apiVersion: kubeadm.k8s.io/v1beta3
nodeRegistration:
criSocket: {{ cri_socket }}
imagePullPolicy: IfNotPresent
---
kind: ClusterConfiguration
apiVersion: kubeadm.k8s.io/v1beta3
kubernetesVersion: "{{ k8s_version }}"
controlPlaneEndpoint: "{{ apiserver_url }}"
certificatesDir: /etc/kubernetes/pki
networking:
podSubnet: {{ pod_network_cidr }}
Loading

0 comments on commit 13be510

Please sign in to comment.