1
0
mirror of https://github.com/jcwimer/startup-infrastructure synced 2026-03-24 22:34:42 +00:00

Create swarm and test swarm creation. test function needed renamed because test is a bash thing already

This commit is contained in:
2018-10-10 14:24:42 -04:00
parent 2c0ca361de
commit 9f4b12185a
8 changed files with 127 additions and 28 deletions

View File

@@ -12,7 +12,8 @@ chosen_timezone: "America/New_York"
# this will allow automatic dns for for things like dokuwiki.test.com and portainer.test.com # this will allow automatic dns for for things like dokuwiki.test.com and portainer.test.com
root_domain: test.com root_domain: test.com
# interface for the swarm network
swarm_network_interface: eth1
### Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following: ### Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following:
# nfs # nfs

View File

@@ -19,6 +19,8 @@
tasks: tasks:
- include: ../roles/common/tasks/main.yml - include: ../roles/common/tasks/main.yml
- import_playbook: swarm.yml
# - name: Deploy startup-infrastructure swarm stack # - name: Deploy startup-infrastructure swarm stack
# hosts: bootstrap # hosts: bootstrap
# user: root # user: root

64
playbooks/swarm.yml Normal file
View File

@@ -0,0 +1,64 @@
---
- name: Initialize the swarm
hosts: bootstrap
user: root
gather_facts: true
serial: 100%
tasks:
- name: Print ansible interfaces
debug:
msg: "{{ ansible_interfaces }}"
# - name: Get ip of swarm_network_interface by parsing all interfaces
# set_fact:
# swarm_init_ip={{hostvars[inventory_hostname]['ansible_item']['ipv4']['address']}}
# when: (item == swarm_network_interface)
# with_items:
# - "{{ ansible_interfaces }}"
- name: Set interface var name fact
set_fact:
swarm_interface_var_name: "ansible_{{ swarm_network_interface }}"
- name: Set swarm advertise ip address
set_fact:
swarm_init_ip: "{{ hostvars[inventory_hostname][swarm_interface_var_name]['ipv4']['address'] }}"
- name: Print swarm init ip address
debug:
msg: "{{ swarm_init_ip }}"
- include_tasks: ../tasks/swarm-bootstrap.yml
vars:
join_addr: "{{ swarm_init_ip }}"
- name: Add additional managers to the swarm
hosts: managers
user: root
gather_facts: false
serial: 100%
vars:
manager_join_key:
"{{ hostvars[groups['bootstrap'][0]]['manager_key']['stdout'] }}"
swarm_init_ip:
"{{ hostvars[groups['bootstrap'][0]]['swarm_init_ip'] }}"
tasks:
- include_tasks: ../tasks/swarm-join.yml
vars:
join_addr: "{{ swarm_init_ip }}"
join_key: "{{ manager_join_key }}"
- name: Add workers to the swarm
hosts: workers
user: root
gather_facts: false
serial: 100%
vars:
worker_join_key:
"{{ hostvars[groups['bootstrap'][0]]['worker_key']['stdout'] }}"
swarm_init_ip:
"{{ hostvars[groups['bootstrap'][0]]['swarm_init_ip'] }}"
tasks:
- include_tasks: ../tasks/swarm-join.yml
vars:
join_addr: "{{ swarm_init_ip }}"
join_key: "{{ worker_join_key }}"

View File

@@ -1,22 +1,23 @@
--- ---
- name: Initialize swarm on the bootstrap manager - name: Initialize swarm on the bootstrap manager
command: > command: >
docker swarm init --advertise-addr "{{ ansible_eth1.ipv4.address }}" docker swarm init --advertise-addr "{{ join_addr }}"
register: docker_swarm_init register: docker_swarm_init
changed_when: docker_swarm_init.rc == 0 changed_when: docker_swarm_init.rc == 0
ignore_errors: true ignore_errors: true
- name: Set manager key variable - name: Set manager key variable
command: docker swarm join-token -q manager command: docker swarm join-token -q manager
register: manager_key register: manager_key
changed_when: manager_key.rc == 0 changed_when: manager_key.rc == 0
- name: Set worker key variable - name: Set worker key variable
command: docker swarm join-token -q worker command: docker swarm join-token -q worker
register: worker_key register: worker_key
changed_when: worker_key.rc == 0 changed_when: worker_key.rc == 0
- name: Set work and manager key facts - name: Set work and manager key facts
set_fact: set_fact:
manager_key: "{{ manager_key }}" manager_key: "{{ manager_key }}"
worker_key: "{{ worker_key }}" worker_key: "{{ worker_key }}"
swarm_init_ip: "{{ join_addr }}"

7
tasks/swarm-join.yml Normal file
View File

@@ -0,0 +1,7 @@
---
- name: Add swarm node to the cluster
command: >
docker swarm join --token "{{ join_key }}" "{{ join_addr }}":2377
register: docker_swarm_join
changed_when: docker_swarm_join.rc == 0
ignore_errors: true

View File

@@ -12,7 +12,8 @@ chosen_timezone: "America/New_York"
# this will allow automatic dns for for things like dokuwiki.test.com and portainer.test.com # this will allow automatic dns for for things like dokuwiki.test.com and portainer.test.com
root_domain: test.com root_domain: test.com
# interface for the swarm network
swarm_network_interface: enp0s8
### Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following: ### Persistent storage if you are doing a single machine deploy, local is an option. If you are doing multi instance deploy, choose one of the following:
# nfs # nfs

View File

@@ -5,7 +5,7 @@ green=`tput setaf 2`
reset=`tput sgr0` reset=`tput sgr0`
#echo "${red}red text ${green}green text${reset}" #echo "${red}red text ${green}green text${reset}"
function test { function testbash() {
local name="${1}" local name="${1}"
shift shift
local command="${@}" local command="${@}"

View File

@@ -8,19 +8,42 @@ function main {
run-tests run-tests
destroy-infrastructure destroy-infrastructure
} }
function run-tests { function run-tests {
trap "destroy-infrastructure; exit 1" ERR trap "destroy-infrastructure; exit 1" ERR
echo Building vagrant infrastructure echo Building vagrant infrastructure
vagrant up vagrant up
test "Running command on a vagrant node should not fail." \ testbash "Running command on a vagrant node should not fail." \
"vagrant ssh client -c 'ls /vagrant'" "vagrant ssh client -c 'ls /vagrant'"
test "Client vagrant machine can ssh into bootstrap." \ testbash "Client vagrant machine can ssh into bootstrap." \
"vagrant ssh client -c 'ssh -o StrictHostKeyChecking=no -i /home/vagrant/test_rsa vagrant@192.168.254.2 ls'" "vagrant ssh client -c 'ssh -o StrictHostKeyChecking=no -i /home/vagrant/test_rsa vagrant@192.168.254.2 ls'"
test "Running deploy script should not fail." \ testbash "Running deploy script should not fail." \
"vagrant ssh client -c 'bash /vagrant/tests/files/run-test-deploy.sh'" "vagrant ssh client -c 'bash /vagrant/tests/files/run-test-deploy.sh'"
local -r node_ls_output=$(vagrant ssh bootstrap \
-c "docker node ls --format '{{.Hostname}} {{.Status}} {{.Availability}} {{.ManagerStatus}}'"
)
echo docker node ls output is:
echo $node_ls_output
local -r number_of_docker_leaders=$(echo "${node_ls_output}" \
| grep -v 'Connection' \
| awk '{ print $4 }' \
| grep '^Leader$' \
| wc -l)
local -r number_of_docker_nodes=$(echo "${node_ls_output}" \
| grep -v 'Connection' \
| awk '{ print $1 }' \
| wc -l)
testbash "There are 2 docker swarm nodes" \
"test ${number_of_docker_nodes} -eq 2"
testbash "The swarm has a leader" \
"test ${number_of_docker_leaders} -eq 1"
} }
function destroy-infrastructure { function destroy-infrastructure {