blob: 4279c3818f99ab11c606dc9aec4dadbcc1d64ec5 [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 Slobodrian8725ea82017-08-27 23:47:41 -040029# Configure barrier file sizes but only if a value was provided in the config file
30
31if [ -v logLimit ]; then
32 sed -i -e "/logger_volume_size/s/.*/logger_volume_size: ${logLimit}/" ansible/group_vars/all
33fi
34if [ -v regLimit ]; then
35 sed -i -e "/registry_volume_size/s/.*/registry_volume_size: ${regLimit}/" ansible/group_vars/all
36fi
37if [ -v consulLimit ]; then
38 sed -i -e "/consul_volume_size/s/.*/consul_volume_size: ${consulLimit}/" ansible/group_vars/all
39fi
40
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040041# Create the key directory
42mkdir .keys
43
44# Create the host list
45echo "[cluster]" > ansible/hosts/cluster
46
47# Silence SSH and avoid prompts
48rm -f ~/.ssh/config
49echo "Host *" > ~/.ssh/config
50echo " StrictHostKeyChecking no" >> ~/.ssh/config
51echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config
52
53sudo cp ~/.ssh/config /root/.ssh/config
54
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040055for i in $hosts
56do
57 # Generate the key for the host
58 echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}"
59 ssh-keygen -f ./$i -t rsa -N ''
60 mv $i .keys
61
62 # Generate the pre-configuration script
63 echo -e "${lBlue}Creating the pre-configuration script${NC}"
64 cat <<HERE > bash_login.sh
65#!/bin/bash
66 echo "voltha ALL=(ALL) NOPASSWD:ALL" > tmp
67 sudo chown root.root tmp
68 sudo mv tmp /etc/sudoers.d/voltha
69 sudo mkdir /home/voltha
70 mkdir voltha_ssh
71 ssh-keygen -f ~/voltha_ssh/id_rsa -t rsa -N ''
72 sudo mv voltha_ssh /home/voltha/.ssh
73HERE
74 echo "sudo cat <<HERE > /home/voltha/.ssh/authorized_keys" >> bash_login.sh
75 cat $i.pub >> bash_login.sh
76 echo "HERE" >> bash_login.sh
77 echo "chmod 400 /home/voltha/.ssh/authorized_keys" >> bash_login.sh
78 echo "sudo useradd -b /home -d /home/voltha voltha -s /bin/bash" >> bash_login.sh
79 echo "sudo chown -R voltha.voltha /home/voltha" >> bash_login.sh
80 echo "echo 'voltha:voltha' | sudo chpasswd" >> bash_login.sh
81 echo "rm .bash_login" >> bash_login.sh
82 echo "logout" >> bash_login.sh
83 rm $i.pub
84 # Copy the pre-config file to the VM
85 echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}"
86 if [ -d ".test" ]; then
87 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040088 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 -040089 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -040090 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040091 fi
92 rm bash_login.sh
93
94 # Run the pre-config file on the VM
95 echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}"
96 if [ -d ".test" ]; then
97 echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040098 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040099 else
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400100 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400101 fi
102
103 # Configure ansible and ssh for silent operation
104 echo -e "${lBlue}Configuring ansible${NC}"
105 echo $i >> ansible/hosts/cluster
106 echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i
107
108 # Create the tunnel to the registry to allow pulls from localhost
109 echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}"
110 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N
111
112done
113# Add the dependent software list to the cluster variables
114echo -e "${lBlue}Setting up dependent software${NC}"
Sergio Slobodrian263900e2017-06-16 21:39:04 -0400115# Delete any grub updates since the boot disk is almost
116# guaranteed not to be the same device as the installer.
117mkdir grub_updates
118sudo mv deb_files/*grub* grub_updates
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400119# Sort the packages in dependency order to get rid of scary non-errors
120# that are issued by ansible.
121#echo -e "${lBlue}Dependency sorting dependent software${NC}"
122#./sort_packages.sh
123#echo "deb_files:" >> ansible/group_vars/all
124#for i in `cat sortedDebs.txt`
125#do
126#echo " - $i" >> ansible/group_vars/all
127#done
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400128
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400129# Make sure the ssh keys propagate to all hosts allowing passwordless logins between them
130echo -e "${lBlue}Propagating ssh keys${NC}"
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400131cp -r .keys ansible/roles/cluster-host/files
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400132
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400133# Install python on all the 3 servers since python is required for
134for i in $hosts
135do
Sergio Slobodriancd43feb2017-07-25 16:26:38 -0400136 echo -e "${lBlue}Installing ${lCyan}Python${lBlue}${NC}"
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400137 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -r python-deb voltha@$i:.
Sergio Slobodriancd43feb2017-07-25 16:26:38 -0400138 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i "sudo dpkg -i /home/voltha/python-deb/*minimal*"
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400139 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo dpkg -i -R /home/voltha/python-deb
140 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i rm -fr python-deb
141
142done
143
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400144# Create the daemon.json file for the swarm
145echo "{" > daemon.json
146echo -n ' "insecure-registries" : [' >> daemon.json
147first=""
148for i in .keys/*
149do
150 if [ -z "$first" ]; then
151 echo -n '"'`basename $i`':5001"' >> daemon.json
152 first="not"
153 else
154 echo -n ' , "'`basename $i`':5001"' >> daemon.json
155 fi
156done
157echo "]" >> daemon.json
158echo "}" >> daemon.json
159unset first
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400160
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400161# Running ansible
162echo -e "${lBlue}Running ansible${NC}"
163cp ansible/ansible.cfg .ansible.cfg
Sergio Slobodrian5727e982017-06-28 21:02:27 -0400164ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400165
Sergio Slobodrian37f4a0e2017-06-14 07:50:01 -0400166# Now all 3 servers need to be rebooted because of software installs.
167# Reboot them and wait patiently until they all come back.
168# Note this destroys the registry tunnel wich is no longer needed.
169hList=""
170for i in $hosts
171do
172 echo -e "${lBlue}Rebooting cluster hosts${NC}"
173 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6
174 hList="$i $hList"
175done
176
177# Give the hosts time to shut down so that pings stop working or the
178# script just falls through the next loop and the rest fails.
179echo -e "${lBlue}Waiting for shutdown${NC}"
180sleep 5
181
182
183while [ ! -z "$hList" ];
184do
185 # Attempt to ping the VMs on the list one by one.
186 echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}"
187 for i in $hList
188 do
189 ping -q -c 1 $i > /dev/null 2>&1
190 ret=$?
191 if [ $ret -eq 0 ]; then
192 ipExpr=`echo $i | sed -e "s/\./[.]/g"`
193 hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"`
194 fi
195 done
196
197done
198
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400199# Now initialize the the docker swarm cluster with managers.
200# The first server needs to be the primary swarm manager
201# the other nodes are backup mangers that join the swarm.
202# In the future, worker nodes will likely be added.
203
204echo "[swarm-master]" > ansible/hosts/swarm-master
205echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup
206
207ctr=1
208for i in $hosts
209do
210 if [ $ctr -eq 1 ]; then
211 echo $i >> ansible/hosts/swarm-master
212 echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all
213 ctr=0
214 else
215 echo $i >> ansible/hosts/swarm-master-backup
216 fi
217done
Sergio Slobodrian5727e982017-06-28 21:02:27 -0400218ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master
219ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master-backup
220ansible-playbook ansible/voltha.yml -i ansible/hosts/swarm-master
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400221