Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 3 | lBlue='\033[1;34m' |
| 4 | green='\033[0;32m' |
| 5 | orange='\033[0;33m' |
| 6 | NC='\033[0m' |
| 7 | red='\033[0;31m' |
| 8 | yellow='\033[1;33m' |
| 9 | dGrey='\033[1;30m' |
| 10 | lGrey='\033[1;37m' |
| 11 | lCyan='\033[1;36m' |
| 12 | wd=`pwd` |
| 13 | |
| 14 | |
| 15 | # Clean up any prior executions |
| 16 | rm -fr .keys |
| 17 | rm -f ansible/hosts/cluster |
| 18 | rm -f ansible/host_vars/* |
| 19 | |
| 20 | # Source the configuration information |
| 21 | . install.cfg |
| 22 | |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 23 | if [ -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 |
| 27 | fi |
| 28 | |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 29 | if [ "$iUser" == "voltha" ]; then |
| 30 | echo -e "${yellow}voltha ${red}can't be used as be install user!!!${NC}" |
| 31 | echo -e "${red}Please delete the ${yellow}voltha ${red}user on the targets and create a different installation user${NC}" |
| 32 | exit |
| 33 | fi |
| 34 | |
Sergio Slobodrian | 8725ea8 | 2017-08-27 23:47:41 -0400 | [diff] [blame] | 35 | # Configure barrier file sizes but only if a value was provided in the config file |
| 36 | |
| 37 | if [ -v logLimit ]; then |
| 38 | sed -i -e "/logger_volume_size/s/.*/logger_volume_size: ${logLimit}/" ansible/group_vars/all |
| 39 | fi |
| 40 | if [ -v regLimit ]; then |
| 41 | sed -i -e "/registry_volume_size/s/.*/registry_volume_size: ${regLimit}/" ansible/group_vars/all |
| 42 | fi |
| 43 | if [ -v consulLimit ]; then |
| 44 | sed -i -e "/consul_volume_size/s/.*/consul_volume_size: ${consulLimit}/" ansible/group_vars/all |
| 45 | fi |
| 46 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 47 | # Create the key directory |
| 48 | mkdir .keys |
| 49 | |
| 50 | # Create the host list |
| 51 | echo "[cluster]" > ansible/hosts/cluster |
| 52 | |
| 53 | # Silence SSH and avoid prompts |
| 54 | rm -f ~/.ssh/config |
| 55 | echo "Host *" > ~/.ssh/config |
| 56 | echo " StrictHostKeyChecking no" >> ~/.ssh/config |
| 57 | echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config |
| 58 | |
| 59 | sudo cp ~/.ssh/config /root/.ssh/config |
| 60 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 61 | for i in $hosts |
| 62 | do |
| 63 | # Generate the key for the host |
| 64 | echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}" |
| 65 | ssh-keygen -f ./$i -t rsa -N '' |
| 66 | mv $i .keys |
| 67 | |
| 68 | # Generate the pre-configuration script |
| 69 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 70 | head -n +1 BashLoginTarget.sh > bash_login.sh |
| 71 | echo "" >> bash_login.sh |
| 72 | echo -n 'key="' >> bash_login.sh |
| 73 | sed -i -e 's/$/"/' $i.pub |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 74 | cat $i.pub >> bash_login.sh |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 75 | tail -n +2 BashLoginTarget.sh | grep -v "{{ key }}" >> bash_login.sh |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 76 | rm $i.pub |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 77 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 78 | # Copy the pre-config file to the VM |
| 79 | echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}" |
| 80 | if [ -d ".test" ]; then |
| 81 | echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 82 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 83 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 84 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 85 | fi |
| 86 | rm bash_login.sh |
| 87 | |
| 88 | # Run the pre-config file on the VM |
| 89 | echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}" |
| 90 | if [ -d ".test" ]; then |
| 91 | echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 92 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 93 | else |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 94 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 95 | fi |
| 96 | |
| 97 | # Configure ansible and ssh for silent operation |
| 98 | echo -e "${lBlue}Configuring ansible${NC}" |
| 99 | echo $i >> ansible/hosts/cluster |
| 100 | echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i |
| 101 | |
| 102 | # Create the tunnel to the registry to allow pulls from localhost |
| 103 | echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}" |
| 104 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N |
| 105 | |
| 106 | done |
| 107 | # Add the dependent software list to the cluster variables |
| 108 | echo -e "${lBlue}Setting up dependent software${NC}" |
Sergio Slobodrian | 263900e | 2017-06-16 21:39:04 -0400 | [diff] [blame] | 109 | # Delete any grub updates since the boot disk is almost |
| 110 | # guaranteed not to be the same device as the installer. |
| 111 | mkdir grub_updates |
| 112 | sudo mv deb_files/*grub* grub_updates |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 113 | # Sort the packages in dependency order to get rid of scary non-errors |
| 114 | # that are issued by ansible. |
| 115 | #echo -e "${lBlue}Dependency sorting dependent software${NC}" |
| 116 | #./sort_packages.sh |
| 117 | #echo "deb_files:" >> ansible/group_vars/all |
| 118 | #for i in `cat sortedDebs.txt` |
| 119 | #do |
| 120 | #echo " - $i" >> ansible/group_vars/all |
| 121 | #done |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 122 | |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 123 | # Make sure the ssh keys propagate to all hosts allowing passwordless logins between them |
| 124 | echo -e "${lBlue}Propagating ssh keys${NC}" |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 125 | cp -r .keys ansible/roles/cluster-host/files |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 126 | |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 127 | # Install python on all the 3 servers since python is required for |
| 128 | for i in $hosts |
| 129 | do |
Sergio Slobodrian | cd43feb | 2017-07-25 16:26:38 -0400 | [diff] [blame] | 130 | echo -e "${lBlue}Installing ${lCyan}Python${lBlue}${NC}" |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 131 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -r python-deb voltha@$i:. |
Sergio Slobodrian | cd43feb | 2017-07-25 16:26:38 -0400 | [diff] [blame] | 132 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i "sudo dpkg -i /home/voltha/python-deb/*minimal*" |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 133 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo dpkg -i -R /home/voltha/python-deb |
| 134 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i rm -fr python-deb |
| 135 | |
| 136 | done |
| 137 | |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 138 | # Create the daemon.json file for the swarm |
| 139 | echo "{" > daemon.json |
| 140 | echo -n ' "insecure-registries" : [' >> daemon.json |
| 141 | first="" |
| 142 | for i in .keys/* |
| 143 | do |
| 144 | if [ -z "$first" ]; then |
| 145 | echo -n '"'`basename $i`':5001"' >> daemon.json |
| 146 | first="not" |
| 147 | else |
| 148 | echo -n ' , "'`basename $i`':5001"' >> daemon.json |
| 149 | fi |
| 150 | done |
| 151 | echo "]" >> daemon.json |
| 152 | echo "}" >> daemon.json |
| 153 | unset first |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 154 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 155 | # Running ansible |
| 156 | echo -e "${lBlue}Running ansible${NC}" |
| 157 | cp ansible/ansible.cfg .ansible.cfg |
Sergio Slobodrian | 5727e98 | 2017-06-28 21:02:27 -0400 | [diff] [blame] | 158 | ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 159 | |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 160 | # Now all 3 servers need to be rebooted because of software installs. |
| 161 | # Reboot them and wait patiently until they all come back. |
| 162 | # Note this destroys the registry tunnel wich is no longer needed. |
| 163 | hList="" |
| 164 | for i in $hosts |
| 165 | do |
| 166 | echo -e "${lBlue}Rebooting cluster hosts${NC}" |
| 167 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6 |
| 168 | hList="$i $hList" |
| 169 | done |
| 170 | |
| 171 | # Give the hosts time to shut down so that pings stop working or the |
| 172 | # script just falls through the next loop and the rest fails. |
| 173 | echo -e "${lBlue}Waiting for shutdown${NC}" |
| 174 | sleep 5 |
| 175 | |
| 176 | |
| 177 | while [ ! -z "$hList" ]; |
| 178 | do |
| 179 | # Attempt to ping the VMs on the list one by one. |
| 180 | echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}" |
| 181 | for i in $hList |
| 182 | do |
| 183 | ping -q -c 1 $i > /dev/null 2>&1 |
| 184 | ret=$? |
| 185 | if [ $ret -eq 0 ]; then |
| 186 | ipExpr=`echo $i | sed -e "s/\./[.]/g"` |
| 187 | hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"` |
| 188 | fi |
| 189 | done |
| 190 | |
| 191 | done |
| 192 | |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 193 | # Now initialize the the docker swarm cluster with managers. |
| 194 | # The first server needs to be the primary swarm manager |
| 195 | # the other nodes are backup mangers that join the swarm. |
| 196 | # In the future, worker nodes will likely be added. |
| 197 | |
| 198 | echo "[swarm-master]" > ansible/hosts/swarm-master |
| 199 | echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup |
| 200 | |
| 201 | ctr=1 |
| 202 | for i in $hosts |
| 203 | do |
| 204 | if [ $ctr -eq 1 ]; then |
| 205 | echo $i >> ansible/hosts/swarm-master |
| 206 | echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all |
| 207 | ctr=0 |
| 208 | else |
| 209 | echo $i >> ansible/hosts/swarm-master-backup |
| 210 | fi |
| 211 | done |
Sergio Slobodrian | 5727e98 | 2017-06-28 21:02:27 -0400 | [diff] [blame] | 212 | ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master |
| 213 | ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master-backup |
| 214 | ansible-playbook ansible/voltha.yml -i ansible/hosts/swarm-master |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 215 | |