blob: 9c5d70879753cc9acb4c5094840ef0fd0675adff [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
2
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04003lBlue='\033[1;34m'
4green='\033[0;32m'
5orange='\033[0;33m'
6NC='\033[0m'
7red='\033[0;31m'
8yellow='\033[1;33m'
9dGrey='\033[1;30m'
10lGrey='\033[1;37m'
11lCyan='\033[1;36m'
12wd=`pwd`
13
14
15# Clean up any prior executions
16rm -fr .keys
17rm -f ansible/hosts/cluster
18rm -f ansible/host_vars/*
19
20# Source the configuration information
21. install.cfg
22
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -040023if [ -z "$hosts" ]; then
24 echo -e "${red}No hosts specifed!!${NC}"
25 echo -e "${red}Did you forget to update the config file ${yellow}installer.cfg${red}?${NC}"
26 exit
27fi
28
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040029# Create the key directory
30mkdir .keys
31
32# Create the host list
33echo "[cluster]" > ansible/hosts/cluster
34
35# Silence SSH and avoid prompts
36rm -f ~/.ssh/config
37echo "Host *" > ~/.ssh/config
38echo " StrictHostKeyChecking no" >> ~/.ssh/config
39echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
40
41sudo cp ~/.ssh/config /root/.ssh/config
42
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040043for i in $hosts
44do
45 # Generate the key for the host
46 echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}"
47 ssh-keygen -f ./$i -t rsa -N ''
48 mv $i .keys
49
50 # Generate the pre-configuration script
51 echo -e "${lBlue}Creating the pre-configuration script${NC}"
52 cat <<HERE > bash_login.sh
53#!/bin/bash
54 echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp
55 sudo chown root.root tmp
56 sudo mv tmp /etc/sudoers.d/voltha
57 sudo mkdir /home/voltha
58 mkdir voltha_ssh
59 ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N ''
60 sudo mv voltha_ssh /home/voltha/.ssh
61HERE
62 echo "sudo cat <<HERE > /home/voltha/.ssh/authorized_keys" >> bash_login.sh
63 cat $i.pub >> bash_login.sh
64 echo "HERE" >> bash_login.sh
65 echo "chmod 400 /home/voltha/.ssh/authorized_keys" >> bash_login.sh
66 echo "sudo useradd -b /home -d /home/voltha voltha -s /bin/bash" >> bash_login.sh
67 echo "sudo chown -R voltha.voltha /home/voltha" >> bash_login.sh
68 echo "echo 'voltha:voltha' | sudo chpasswd" >> bash_login.sh
69 echo "rm .bash_login" >> bash_login.sh
70 echo "logout" >> bash_login.sh
71 rm $i.pub
72 # Copy the pre-config file to the VM
73 echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
74 if [ -d ".test" ]; then
75 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040076 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040077 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -040078 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040079 fi
80 rm bash_login.sh
81
82 # Run the pre-config file on the VM
83 echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}"
84 if [ -d ".test" ]; then
85 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040086 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040087 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -040088 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040089 fi
90
91 # Configure ansible and ssh for silent operation
92 echo -e "${lBlue}Configuring ansible${NC}"
93 echo $i >> ansible/hosts/cluster
94 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i
95
96 # Create the tunnel to the registry to allow pulls from localhost
97 echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}"
98 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N
99
100done
101# Add the dependent software list to the cluster variables
102echo -e "${lBlue}Setting up dependent software${NC}"
103echo "deb_files:" >> ansible/group_vars/all
104for i in deb_files/*.deb
105do
106echo " - `basename $i`" >> ansible/group_vars/all
107done
108
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400109# Make sure the ssh keys propagate to all hosts allowing passwordless logins between them
110echo -e "${lBlue}Propagating ssh keys${NC}"
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400111cp -r .keys ansible/roles/cluster-host/files
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400112
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400113# Running ansible
114echo -e "${lBlue}Running ansible${NC}"
115cp ansible/ansible.cfg .ansible.cfg
116sudo ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster
117
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400118# Now all 3 servers need to be rebooted because of software installs.
119# Reboot them and wait patiently until they all come back.
120# Note this destroys the registry tunnel wich is no longer needed.
121hList=""
122for i in $hosts
123do
124 echo -e "${lBlue}Rebooting cluster hosts${NC}"
125 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6
126 hList="$i $hList"
127done
128
129# Give the hosts time to shut down so that pings stop working or the
130# script just falls through the next loop and the rest fails.
131echo -e "${lBlue}Waiting for shutdown${NC}"
132sleep 5
133
134
135while [ ! -z "$hList" ];
136do
137 # Attempt to ping the VMs on the list one by one.
138 echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}"
139 for i in $hList
140 do
141 ping -q -c 1 $i > /dev/null 2>&1
142 ret=$?
143 if [ $ret -eq 0 ]; then
144 ipExpr=`echo $i | sed -e "s/\./[.]/g"`
145 hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"`
146 fi
147 done
148
149done
150
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400151# Now initialize the the docker swarm cluster with managers.
152# The first server needs to be the primary swarm manager
153# the other nodes are backup mangers that join the swarm.
154# In the future, worker nodes will likely be added.
155
156echo "[swarm-master]" > ansible/hosts/swarm-master
157echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup
158
159ctr=1
160for i in $hosts
161do
162 if [ $ctr -eq 1 ]; then
163 echo $i >> ansible/hosts/swarm-master
164 echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all
165 ctr=0
166 else
167 echo $i >> ansible/hosts/swarm-master-backup
168 fi
169done
170sudo ansible-playbook ansible/swarm-master.yml -i ansible/hosts/swarm-master
171sudo ansible-playbook ansible/swarm-master-backup.yml -i ansible/hosts/swarm-master-backup
172