Skip to content

Commit

Permalink
ansible installation [wip]
Browse files Browse the repository at this point in the history
  • Loading branch information
Limmen committed Mar 10, 2024
1 parent 095f1a0 commit 1fe947f
Show file tree
Hide file tree
Showing 12 changed files with 733 additions and 541 deletions.
6 changes: 6 additions & 0 deletions ansible/install.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
---
- import_playbook: install_setup.yml
- import_playbook: install_metastore.yml
- import_playbook: install_metastore_leader.yml
- import_playbook: install_simulation_system.yml
- import_playbook: install_simulation_system_leader.yml
- import_playbook: install_emulation_system.yml
- import_playbook: install_emulation_system_leader.yml
- import_playbook: install_emulation_system_worker.yml
- import_playbook: install_management_system.yml
- import_playbook: install_management_system_leader.yml
- import_playbook: start.yml
228 changes: 119 additions & 109 deletions ansible/install_emulation_system.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,114 +2,124 @@

- hosts: all

vars:
spark_base: "csle_spark_base"
spark_derived: "csle_spark_1"

tasks:

- name: Check if apt keyrings directory exist
become: true
stat:
path: /etc/apt/keyrings
register: keyrings_directory_exists

- name: Create keyrings directory if it does not exist
become: true
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: "0755"
when: not keyrings_directory_exists.stat.exists

- name: Download Docker GPG key and install GPG key
shell: |
expect -c '
spawn /bin/bash -c "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg"
expect -re ".*Overwrite?.*"
send "y\r"
interact
'
- name: Install docker apt keys
become: true
shell: echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

- name: Update package cache
become: true
apt:
update_cache: yes

- name: Install Docker packages
become: true
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: present

- name: Add Docker group
become: true
group:
name: docker
state: present

- name: Add user to Docker group
become: true
shell: "sudo usermod -aG docker {{ user }}"

- name: Initialize Docker Swarm
command: docker swarm init --advertise-addr "{{ leader_ip }}"
ignore_errors: yes

- name: Pulling base Docker images
shell: "cd /home/{{ user }}/csle/emulation-system/base_images && make pull"

- name: Pulling derived Docker images
shell: |
cd "/home/{{ user }}/csle/emulation-system/derived_images" && \
make pull
args:
executable: /bin/bash

- name: Installing the emulation environments on the leader node
shell: |
source "/home/{{ user }}/anaconda3/bin/activate {{ conda_environment_name }}" && \
cd "/home/{{ user }}/csle/emulation-system/envs" && \
make install
args:
executable: /bin/bash

- name: Set max_map_count kernel parameter
become: true
sysctl:
name: vm.max_map_count
value: 262144
sysctl_set: yes
reload: yes
state: present

- name: Add line to limits.conf if not exists
become: true
ansible.builtin.lineinfile:
path: /etc/security/limits.conf
line: "{{ user }}\tsoft\tnofile\t102400"
regexp: "^{{ user }}\\s+soft\\s+nofile\\s+102400$"
state: present
register: line_added
changed_when: false

- name: Add second line to limits.conf if not exists
become: true
ansible.builtin.lineinfile:
path: /etc/security/limits.conf
line: "{{ user }}\thard\tnofile\t1024000"
regexp: "^{{ user }}\\s+hard\\s+nofile\\s+1024000$"
state: present
register: line_added
changed_when: false

- name: Set fs.inotify.max_user_watches in sysctl.conf
become: true
ansible.builtin.shell: "echo 'fs.inotify.max_user_watches=524288' | sudo tee -a /etc/sysctl.conf"

- name: Reload sysctl
become: true
ansible.builtin.shell: "sudo sysctl -p"
- name: Check if apt keyrings directory exist
become: true
stat:
path: /etc/apt/keyrings
register: keyrings_directory_exists

- name: Create keyrings directory if it does not exist
become: true
ansible.builtin.file:
path: /etc/apt/keyrings
state: directory
mode: "0755"
when: not keyrings_directory_exists.stat.exists

- name: Add Docker GPG apt Key
become: true
ansible.builtin.apt_key:
url: https://download.docker.com/linux/ubuntu/gpg
state: present

- name: Add repository into sources list
become: true
ansible.builtin.apt_repository:
repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_lsb.codename }} stable
state: present
filename: docker

- name: Update package cache
become: true
apt:
update_cache: yes

- name: Install Docker packages
become: true
apt:
name:
- docker-ce
- docker-ce-cli
- containerd.io
state: present

- name: Add Docker group
become: true
group:
name: docker
state: present

- name: Add user to Docker group
become: true
shell: "sudo usermod -aG docker {{ user }}"

- name: List all docker images
become: true
shell: "docker images"
args:
executable: /bin/bash
register: docker_images_list
changed_when: false

- name: Check if the base images are pulled
set_fact:
base_images_pulled: "{{ spark_base in docker_images_list.stdout }}"

- name: Check if the derived images are pulled
set_fact:
derived_images_pulled: "{{ spark_derived in docker_images_list.stdout }}"

- name: Pulling base Docker images
shell: "cd /home/{{ user }}/csle/emulation-system/base_images && make pull"
args:
executable: /bin/bash
when: not base_images_pulled

- name: Pulling derived Docker images
shell: "cd /home/{{ user }}/csle/emulation-system/derived_images && make pull"
args:
executable: /bin/bash
when: not derived_images_pulled

- name: Set max_map_count kernel parameter
become: true
sysctl:
name: vm.max_map_count
value: 262144
sysctl_set: yes
reload: yes
state: present

- name: Add line to limits.conf if not exists
become: true
ansible.builtin.lineinfile:
path: /etc/security/limits.conf
line: "{{ user }}\tsoft\tnofile\t102400"
regexp: "^{{ user }}\\s+soft\\s+nofile\\s+102400$"
state: present
register: line_added
changed_when: false

- name: Add second line to limits.conf if not exists
become: true
ansible.builtin.lineinfile:
path: /etc/security/limits.conf
line: "{{ user }}\thard\tnofile\t1024000"
regexp: "^{{ user }}\\s+hard\\s+nofile\\s+1024000$"
state: present
register: line_added
changed_when: false

- name: Set fs.inotify.max_user_watches in sysctl.conf
become: true
ansible.builtin.shell: "echo 'fs.inotify.max_user_watches=524288' | sudo tee -a /etc/sysctl.conf"

- name: Reload sysctl
become: true
ansible.builtin.shell: "sudo sysctl -p"
64 changes: 64 additions & 0 deletions ansible/install_emulation_system_leader.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---

- hosts: leader

vars:
active: "Active"

tasks:

- name: List installed emulation environments
become: yes
become_method: sudo
become_user: postgres
community.postgresql.postgresql_query:
db: csle
login_user: postgres
login_password: "{{ postgres_password }}"
query: "SELECT id FROM emulations;"
register: emulations

- name: Check if the emulation environments are installed
set_fact:
emulation_environments_installed: "{{ emulations.rowcount > 0 }}"

- name: Installing the emulation environments on the leader node
shell: "source /home/{{ user }}/anaconda3/bin/activate {{ conda_environment_name }}; cd /home/{{ user }}/csle/emulation-system/envs && make install"
args:
executable: /bin/bash
when: not emulation_environments_installed

- name: List docker swarm status
become: true
shell: "docker node ls"
args:
executable: /bin/bash
register: docker_swarm_status
changed_when: false

- name: Check if the docker swarm has been initialized
set_fact:
swarm_initialized: "{{ active in docker_swarm_status.stdout }}"

- name: Initialize Docker Swarm
become: true
command: docker swarm init --advertise-addr "{{ leader_ip }}"
ignore_errors: yes
when: not swarm_initialized

- name: List docker swarm join command
become: true
shell: "docker swarm join-token worker"
args:
executable: /bin/bash
register: docker_swarm_join_command_output
changed_when: false

- name: Extract join token
set_fact:
docker_swarm_join_command: "{{ docker_swarm_join_command_output.stdout | regex_search(stdout_regex, multiline=True)}}"
vars:
stdout_regex: 'docker swarm join --token (.*)'

- debug:
var: hostvars[leader_ip]['docker_swarm_join_command']
10 changes: 10 additions & 0 deletions ansible/install_emulation_system_worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---

- hosts: worker

tasks:

- name: Join Docker Swarm
become: true
command: "{{ hostvars[leader_ip]['docker_swarm_join_command'] }}"
ignore_errors: yes
Loading

0 comments on commit 1fe947f

Please sign in to comment.