Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 15 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 16 | lBlue='\033[1;34m' |
| 17 | green='\033[0;32m' |
| 18 | orange='\033[0;33m' |
| 19 | NC='\033[0m' |
| 20 | red='\033[0;31m' |
| 21 | yellow='\033[1;33m' |
| 22 | dGrey='\033[1;30m' |
| 23 | lGrey='\033[1;37m' |
| 24 | lCyan='\033[1;36m' |
| 25 | wd=`pwd` |
| 26 | |
| 27 | |
| 28 | # Clean up any prior executions |
| 29 | rm -fr .keys |
| 30 | rm -f ansible/hosts/cluster |
| 31 | rm -f ansible/host_vars/* |
| 32 | |
| 33 | # Source the configuration information |
| 34 | . install.cfg |
| 35 | |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 36 | if [ -z "$hosts" ]; then |
| 37 | echo -e "${red}No hosts specifed!!${NC}" |
| 38 | echo -e "${red}Did you forget to update the config file ${yellow}installer.cfg${red}?${NC}" |
| 39 | exit |
| 40 | fi |
| 41 | |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 42 | if [ "$iUser" == "voltha" ]; then |
| 43 | echo -e "${yellow}voltha ${red}can't be used as be install user!!!${NC}" |
| 44 | echo -e "${red}Please delete the ${yellow}voltha ${red}user on the targets and create a different installation user${NC}" |
| 45 | exit |
| 46 | fi |
| 47 | |
Sergio Slobodrian | 8725ea8 | 2017-08-27 23:47:41 -0400 | [diff] [blame] | 48 | # Configure barrier file sizes but only if a value was provided in the config file |
| 49 | |
| 50 | if [ -v logLimit ]; then |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 51 | sed -i -e "/logger_volume_size/s/.*/logger_volume_size: ${logLimit}/" ansible/group_vars/all |
Sergio Slobodrian | 8725ea8 | 2017-08-27 23:47:41 -0400 | [diff] [blame] | 52 | fi |
| 53 | if [ -v regLimit ]; then |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 54 | sed -i -e "/registry_volume_size/s/.*/registry_volume_size: ${regLimit}/" ansible/group_vars/all |
Sergio Slobodrian | 8725ea8 | 2017-08-27 23:47:41 -0400 | [diff] [blame] | 55 | fi |
| 56 | if [ -v consulLimit ]; then |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 57 | sed -i -e "/consul_volume_size/s/.*/consul_volume_size: ${consulLimit}/" ansible/group_vars/all |
Sergio Slobodrian | 8725ea8 | 2017-08-27 23:47:41 -0400 | [diff] [blame] | 58 | fi |
| 59 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 60 | # Create the key directory |
| 61 | mkdir .keys |
| 62 | |
| 63 | # Create the host list |
| 64 | echo "[cluster]" > ansible/hosts/cluster |
| 65 | |
| 66 | # Silence SSH and avoid prompts |
| 67 | rm -f ~/.ssh/config |
| 68 | echo "Host *" > ~/.ssh/config |
| 69 | echo " StrictHostKeyChecking no" >> ~/.ssh/config |
| 70 | echo " UserKnownHostsFile /dev/null" >> ~/.ssh/config |
| 71 | |
| 72 | sudo cp ~/.ssh/config /root/.ssh/config |
| 73 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 74 | for i in $hosts |
| 75 | do |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 76 | # Generate the key for the host |
| 77 | echo -e "${lBlue}Generating the key-pair for communication with host ${yellow}$i${NC}" |
| 78 | ssh-keygen -f ./$i -t rsa -N '' |
| 79 | mv $i .keys |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 80 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 81 | # Generate the pre-configuration script |
| 82 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
| 83 | head -n +1 BashLoginTarget.sh > bash_login.sh |
| 84 | echo "" >> bash_login.sh |
| 85 | echo -n 'key="' >> bash_login.sh |
| 86 | sed -i -e 's/$/"/' $i.pub |
| 87 | cat $i.pub >> bash_login.sh |
| 88 | tail -n +2 BashLoginTarget.sh | grep -v "{{ key }}" >> bash_login.sh |
| 89 | rm $i.pub |
Sergio Slobodrian | b92e513 | 2017-09-13 13:04:05 -0400 | [diff] [blame] | 90 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 91 | # Copy the pre-config file to the VM |
| 92 | echo -e "${lBlue}Transfering pre-configuration script to ${yellow}$i${NC}" |
| 93 | if [ -d ".test" ]; then |
| 94 | echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}" |
| 95 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i bash_login.sh $iUser@$i:.bash_login |
| 96 | else |
| 97 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh $iUser@$i:.bash_login |
| 98 | fi |
| 99 | rm bash_login.sh |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 100 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 101 | # Run the pre-config file on the VM |
| 102 | echo -e "${lBlue}Running the pre-configuration script on ${yellow}$i${NC}" |
| 103 | if [ -d ".test" ]; then |
| 104 | echo -e "${red}Test mode set!!${lBlue} Using pre-populated ssh key for ${yellow}$i${NC}" |
| 105 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .test/$i $iUser@$i |
| 106 | else |
| 107 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $iUser@$i |
| 108 | fi |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 109 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 110 | # Configure ansible and ssh for silent operation |
| 111 | echo -e "${lBlue}Configuring ansible${NC}" |
| 112 | echo $i >> ansible/hosts/cluster |
| 113 | echo "ansible_ssh_private_key_file: $wd/.keys/$i" > ansible/host_vars/$i |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 114 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 115 | # Create the tunnel to the registry to allow pulls from localhost |
| 116 | echo -e "${lBlue}Creating a secure shell tunnel to the registry for ${yellow}$i${NC}" |
| 117 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -f voltha@$i -R 5000:localhost:5000 -N |
| 118 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 119 | done |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 120 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 121 | # Add the dependent software list to the cluster variables |
| 122 | echo -e "${lBlue}Setting up dependent software${NC}" |
| 123 | # Delete any grub updates since the boot disk is almost |
| 124 | # guaranteed not to be the same device as the installer. |
| 125 | mkdir grub_updates |
| 126 | sudo mv deb_files/*grub* grub_updates |
| 127 | # Sort the packages in dependency order to get rid of scary non-errors |
| 128 | # that are issued by ansible. |
| 129 | #echo -e "${lBlue}Dependency sorting dependent software${NC}" |
| 130 | #./sort_packages.sh |
| 131 | #echo "deb_files:" >> ansible/group_vars/all |
| 132 | #for i in `cat sortedDebs.txt` |
| 133 | #do |
| 134 | #echo " - $i" >> ansible/group_vars/all |
| 135 | #done |
| 136 | |
| 137 | # Make sure the ssh keys propagate to all hosts allowing passwordless logins between them |
| 138 | echo -e "${lBlue}Propagating ssh keys${NC}" |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 139 | cp -r .keys ansible/roles/cluster-host/files |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 140 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 141 | # Install python on all the 3 servers since python is required for |
| 142 | for i in $hosts |
| 143 | do |
| 144 | echo -e "${lBlue}Installing ${lCyan}Python${lBlue}${NC}" |
| 145 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i -r python-deb voltha@$i:. |
| 146 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i "sudo dpkg -i /home/voltha/python-deb/*minimal*" |
| 147 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo dpkg -i -R /home/voltha/python-deb |
| 148 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i rm -fr python-deb |
| 149 | |
| 150 | done |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 151 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 152 | if [ "$cluster_framework" == "kubernetes" ]; then |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 153 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 154 | echo -e "${green}Deploying kubernetes${NC}" |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 155 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 156 | # Remove previously created inventory if it exists |
Stephane Barbarie | 78d9fa6 | 2018-04-19 14:11:05 -0400 | [diff] [blame] | 157 | cp -rfp kubespray/inventory/sample kubespray/inventory/voltha |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 158 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 159 | # Adjust kubespray configuration |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 160 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 161 | # Destination OS |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 162 | sed -i -e "/bootstrap_os: none/s/.*/bootstrap_os: ubuntu/" \ |
| 163 | kubespray/inventory/voltha/group_vars/all.yml |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 164 | |
| 165 | # Subnet used for deployed k8s services |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 166 | sed -i -e "/kube_service_addresses: 10.233.0.0\/18/s/.*/kube_service_addresses: $cluster_service_subnet/" \ |
| 167 | kubespray/inventory/voltha/group_vars/k8s-cluster.yml |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 168 | |
| 169 | # Subnet used for deployed k8s pods |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 170 | sed -i -e "/kube_pods_subnet: 10.233.64.0\/18/s/.*/kube_pods_subnet: $cluster_pod_subnet/" \ |
| 171 | kubespray/inventory/voltha/group_vars/k8s-cluster.yml |
Sergio Slobodrian | 37f4a0e | 2017-06-14 07:50:01 -0400 | [diff] [blame] | 172 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 173 | # Prevent any downloads from kubespray |
| 174 | sed -i -e "s/skip_downloads: false/skip_downloads: true/" \ |
| 175 | kubespray/cluster.yml |
| 176 | sed -i -e "s/- { role: docker, tags: docker }/#&/" \ |
| 177 | kubespray/cluster.yml |
| 178 | sed -i -e "s/skip_downloads: false/skip_downloads: true/" \ |
| 179 | kubespray/roles/download/defaults/main.yml |
| 180 | sed -i -e "s/when: ansible_os_family == \"Debian\"/& and skip_downloads == \"false\" /" \ |
| 181 | kubespray/roles/kubernetes/preinstall/tasks/main.yml |
| 182 | sed -i -e "s/or is_atomic)/& and skip_downloads == \"false\" /" \ |
| 183 | kubespray/roles/kubernetes/preinstall/tasks/main.yml |
| 184 | |
Stephane Barbarie | 78d9fa6 | 2018-04-19 14:11:05 -0400 | [diff] [blame] | 185 | # Configure failover parameters |
| 186 | sed -i -e "s/kube_controller_node_monitor_grace_period: .*/kube_controller_node_monitor_grace_period: 20s/" \ |
| 187 | kubespray/roles/kubernetes/master/defaults/main.yml |
| 188 | sed -i -e "s/kube_controller_pod_eviction_timeout: .*/kube_controller_pod_eviction_timeout: 30s/" \ |
| 189 | kubespray/roles/kubernetes/master/defaults/main.yml |
| 190 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 191 | # Construct node inventory |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 192 | CONFIG_FILE=kubespray/inventory/voltha/hosts.ini python3 \ |
| 193 | kubespray/contrib/inventory_builder/inventory.py $hosts |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 194 | |
Stephane Barbarie | 78d9fa6 | 2018-04-19 14:11:05 -0400 | [diff] [blame] | 195 | # The inventory builder configures 2 masters. |
| 196 | # Due to non-stable behaviours, force the use of a single master |
| 197 | cat kubespray/inventory/voltha/hosts.ini \ |
| 198 | | sed -e ':begin;$!N;s/\(\[kube-master\]\)\n/\1/;tbegin;P;D' \ |
| 199 | | sed -e '/\[kube-master\].*/,/\[kube-node\]/{//!d}' \ |
| 200 | | sed -e 's/\(\[kube-master\]\)\(.*\)/\1\n\2\n/' \ |
| 201 | > kubespray/inventory/voltha/hosts.ini.tmp |
| 202 | |
| 203 | mv kubespray/inventory/voltha/hosts.ini.tmp kubespray/inventory/voltha/hosts.ini |
| 204 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 205 | ordered_nodes=`CONFIG_FILE=kubespray/inventory/voltha/hosts.ini python3 \ |
| 206 | kubespray/contrib/inventory_builder/inventory.py print_ips` |
| 207 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 208 | echo "[k8s-master]" > ansible/hosts/k8s-master |
| 209 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 210 | mkdir -p kubespray/inventory/voltha/host_vars |
| 211 | |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 212 | ctr=1 |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 213 | for i in $ordered_nodes |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 214 | do |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 215 | echo -e "${lBlue}Adding SSH keys to kubespray ansible${NC}" |
| 216 | echo "ansible_ssh_private_key_file: $wd/.keys/$i" > kubespray/inventory/voltha/host_vars/node$ctr |
| 217 | |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 218 | if [ $ctr -eq 1 ]; then |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 219 | echo $i >> ansible/hosts/k8s-master |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 220 | fi |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 221 | ctr=$((ctr + 1)) |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 222 | done |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 223 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 224 | # Prepare Voltha |
| 225 | # ... Prepares environment and copies all required container images |
| 226 | # ... including the ones needed by kubespray |
| 227 | cp ansible/ansible.cfg .ansible.cfg |
| 228 | ansible-playbook -v ansible/voltha-k8s.yml -i ansible/hosts/cluster -e 'config_voltha=true' |
| 229 | |
| 230 | # Deploy kubernetes |
| 231 | ANSIBLE_CONFIG=kubespray/ansible.cfg ansible-playbook -v -b \ |
| 232 | --become-method=sudo --become-user root -u voltha \ |
| 233 | -i kubespray/inventory/voltha/hosts.ini kubespray/cluster.yml |
| 234 | |
Stephane Barbarie | 78d9fa6 | 2018-04-19 14:11:05 -0400 | [diff] [blame] | 235 | # Now all 3 servers need to be rebooted because of software installs. |
| 236 | # Reboot them and wait patiently until they all come back. |
| 237 | # Note this destroys the registry tunnel wich is no longer needed. |
| 238 | hList="" |
| 239 | for i in $hosts |
| 240 | do |
| 241 | echo -e "${lBlue}Rebooting cluster hosts${NC}" |
| 242 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6 |
| 243 | hList="$i $hList" |
| 244 | done |
| 245 | |
| 246 | # Give the hosts time to shut down so that pings stop working or the |
| 247 | # script just falls through the next loop and the rest fails. |
| 248 | echo -e "${lBlue}Waiting for shutdown${NC}" |
| 249 | sleep 5 |
| 250 | |
| 251 | |
| 252 | while [ ! -z "$hList" ]; |
| 253 | do |
| 254 | # Attempt to ping the VMs on the list one by one. |
| 255 | echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}" |
| 256 | for i in $hList |
| 257 | do |
| 258 | ping -q -c 1 $i > /dev/null 2>&1 |
| 259 | ret=$? |
| 260 | if [ $ret -eq 0 ]; then |
| 261 | ipExpr=`echo $i | sed -e "s/\./[.]/g"` |
| 262 | hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"` |
| 263 | fi |
| 264 | done |
| 265 | |
| 266 | done |
| 267 | |
| 268 | # Wait for kubernetes to settle after reboot |
| 269 | k8sIsUp="no" |
| 270 | while [ "$k8sIsUp" == "no" ]; |
| 271 | do |
| 272 | # Attempt to ping the VMs on the list one by one. |
| 273 | echo -e "${lBlue}Waiting for kubernetes to settle${NC}" |
| 274 | for i in $hosts |
| 275 | do |
| 276 | nc -vz $i 6443 > /dev/null 2>&1 |
| 277 | ret=$? |
| 278 | if [ $ret -eq 0 ]; then |
| 279 | k8sIsUp="yes" |
| 280 | break |
| 281 | fi |
| 282 | sleep 1 |
| 283 | done |
| 284 | done |
| 285 | echo -e "${lBlue}Kubernetes is up and running${NC}" |
| 286 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 287 | # Deploy Voltha |
| 288 | ansible-playbook -v ansible/voltha-k8s.yml -i ansible/hosts/k8s-master -e 'deploy_voltha=true' |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 289 | |
| 290 | else |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 291 | # Legacy swarm instructions |
Stephane Barbarie | bcea2f4 | 2018-03-02 18:46:32 -0500 | [diff] [blame] | 292 | |
| 293 | # Create the daemon.json file for the swarm |
| 294 | echo "{" > daemon.json |
| 295 | echo -n ' "insecure-registries" : [' >> daemon.json |
| 296 | first="" |
| 297 | for i in .keys/* |
| 298 | do |
| 299 | if [ -z "$first" ]; then |
| 300 | echo -n '"'`basename $i`':5001"' >> daemon.json |
| 301 | first="not" |
| 302 | else |
| 303 | echo -n ' , "'`basename $i`':5001"' >> daemon.json |
| 304 | fi |
| 305 | done |
| 306 | echo "]" >> daemon.json |
| 307 | echo "}" >> daemon.json |
| 308 | unset first |
| 309 | |
| 310 | # Running ansible |
| 311 | echo -e "${lBlue}Running ansible${NC}" |
| 312 | cp ansible/ansible.cfg .ansible.cfg |
| 313 | ansible-playbook ansible/voltha.yml -i ansible/hosts/cluster |
| 314 | |
| 315 | # Now all 3 servers need to be rebooted because of software installs. |
| 316 | # Reboot them and wait patiently until they all come back. |
| 317 | # Note this destroys the registry tunnel wich is no longer needed. |
| 318 | hList="" |
| 319 | for i in $hosts |
| 320 | do |
| 321 | echo -e "${lBlue}Rebooting cluster hosts${NC}" |
| 322 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i .keys/$i voltha@$i sudo telinit 6 |
| 323 | hList="$i $hList" |
| 324 | done |
| 325 | |
| 326 | # Give the hosts time to shut down so that pings stop working or the |
| 327 | # script just falls through the next loop and the rest fails. |
| 328 | echo -e "${lBlue}Waiting for shutdown${NC}" |
| 329 | sleep 5 |
| 330 | |
| 331 | |
| 332 | while [ ! -z "$hList" ]; |
| 333 | do |
| 334 | # Attempt to ping the VMs on the list one by one. |
| 335 | echo -e "${lBlue}Waiting for hosts to reboot ${yellow}$hList${NC}" |
| 336 | for i in $hList |
| 337 | do |
| 338 | ping -q -c 1 $i > /dev/null 2>&1 |
| 339 | ret=$? |
| 340 | if [ $ret -eq 0 ]; then |
| 341 | ipExpr=`echo $i | sed -e "s/\./[.]/g"` |
| 342 | hList=`echo $hList | sed -e "s/$ipExpr//" | sed -e "s/^ //" | sed -e "s/ $//"` |
| 343 | fi |
| 344 | done |
| 345 | |
| 346 | done |
| 347 | |
| 348 | # Now initialize the the docker swarm cluster with managers. |
| 349 | # The first server needs to be the primary swarm manager |
| 350 | # the other nodes are backup mangers that join the swarm. |
| 351 | # In the future, worker nodes will likely be added. |
| 352 | |
| 353 | echo "[swarm-master]" > ansible/hosts/swarm-master |
| 354 | echo "[swarm-master-backup]" > ansible/hosts/swarm-master-backup |
| 355 | |
| 356 | ctr=1 |
| 357 | for i in $hosts |
| 358 | do |
| 359 | if [ $ctr -eq 1 ]; then |
| 360 | echo $i >> ansible/hosts/swarm-master |
| 361 | echo "swarm_master_addr: \"$i\"" >> ansible/group_vars/all |
| 362 | ctr=0 |
| 363 | else |
| 364 | echo $i >> ansible/hosts/swarm-master-backup |
| 365 | fi |
| 366 | done |
| 367 | ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master |
| 368 | ansible-playbook ansible/swarm.yml -i ansible/hosts/swarm-master-backup |
| 369 | ansible-playbook ansible/voltha.yml -i ansible/hosts/swarm-master |
| 370 | |
| 371 | fi |