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 | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 16 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 17 | iVmName="vInstaller" |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 18 | vVmName="voltha_voltha" |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 19 | baseImage="Ubuntu1604LTS" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 20 | iVmNetwork="vagrant-libvirt" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 21 | installerArchive="installer.tar.bz2" |
| 22 | installerDirectory="volthaInstaller" |
| 23 | installerPart="installer.part" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 24 | shutdownTimeout=5 |
| 25 | ipTimeout=10 |
| 26 | |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 27 | # Command line argument variables |
| 28 | testMode="no" |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 29 | rebuildVoltha="no" |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 30 | useKubernetes="no" |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 31 | |
| 32 | |
| 33 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 34 | lBlue='\033[1;34m' |
| 35 | green='\033[0;32m' |
| 36 | orange='\033[0;33m' |
| 37 | NC='\033[0m' |
| 38 | red='\033[0;31m' |
| 39 | yellow='\033[1;33m' |
| 40 | dGrey='\033[1;30m' |
| 41 | lGrey='\033[1;37m' |
| 42 | lCyan='\033[1;36m' |
| 43 | |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 44 | uId=`id -u` |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 45 | wd=`pwd` |
| 46 | |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 47 | parse_args() |
| 48 | { |
| 49 | for i in $@ |
| 50 | do |
| 51 | case "$i" in |
| 52 | "test" ) |
| 53 | testMode="yes" |
| 54 | echo -e "${lBlue}Test mode is ${green}enabled${NC}" |
| 55 | ;; |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 56 | "rebuild" ) |
| 57 | rebuildVoltha="yes" |
| 58 | echo -e "${lBlue}Voltha rebuild is ${green}enabled${NC}" |
| 59 | ;; |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 60 | "k8s" ) |
| 61 | useKubernetes="yes" |
| 62 | echo -e "${lBlue}Kubernetes framework is ${green}enabled${NC}" |
| 63 | ;; |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 64 | esac |
| 65 | done |
| 66 | } |
| 67 | |
| 68 | |
| 69 | ###################################### |
| 70 | # MAIN MAIN MAIN MAIN MAIN MAIN MAIN # |
| 71 | ###################################### |
| 72 | parse_args $@ |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 73 | # Validate that vagrant is installed. |
| 74 | echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}" |
| 75 | vInst=`which vagrant` |
| 76 | |
| 77 | if [ -z "$vInst" ]; then |
| 78 | wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb |
| 79 | sudo dpkg -i vagrant_1.8.5_x86_64.deb |
| 80 | rm vagrant_1.8.5_x86_64.deb |
| 81 | fi |
| 82 | unset vInst |
| 83 | |
| 84 | # Validate that ansible is installed |
| 85 | echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}" |
| 86 | aInst=`which ansible` |
| 87 | |
| 88 | if [ -z "$aInst" ]; then |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 89 | sudo apt-get install -y software-properties-common |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 90 | sudo apt-add-repository ppa:ansible/ansible |
| 91 | sudo apt-get update |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 92 | sudo apt-get install -y ansible |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 93 | fi |
| 94 | unset vInst |
| 95 | |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 96 | # Verify if this is intended to be a test environment, if so |
| 97 | # configure the 3 VMs which will be started later to emulate |
| 98 | # the production installation cluster. |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 99 | if [ "$testMode" == "yes" ]; then |
| 100 | echo -e "${lBlue}Test mode ${green}enabled${lBlue}, configure the ${lCyan}ha-serv${lBlue} VMs${NC}" |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 101 | # Update the vagrant settings file |
| 102 | sed -i -e '/server_name/s/.*/server_name: "ha-serv'${uId}'-"/' settings.vagrant.yaml |
| 103 | sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall'${uId}':5000"/' ansible/group_vars/all |
| 104 | sed -i -e "/vinstall/s/vinstall/vinstall${uId}/" ../ansible/roles/docker/templates/daemon.json |
| 105 | |
| 106 | # Set the insecure registry configuration based on the installer hostname |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 107 | echo -e "${lBlue}Set up the insecure registry config for hostname ${lCyan}vinstall${uId}${NC}" |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 108 | echo '{' > ansible/roles/voltha/templates/daemon.json |
| 109 | echo '"insecure-registries" : ["vinstall'${uId}':5000"]' >> ansible/roles/voltha/templates/daemon.json |
| 110 | echo '}' >> ansible/roles/voltha/templates/daemon.json |
| 111 | |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 112 | # Change the installer name |
| 113 | iVmName="vInstaller${uId}" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 114 | else |
| 115 | rm -fr .test |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 116 | # Clean out the install config file keeping only the commented lines |
| 117 | # which serve as documentation. |
| 118 | sed -i -e '/^#/!d' install.cfg |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 119 | # Set the insecure registry configuration based on the installer hostname |
Sergio Slobodrian | 497e6e8 | 2017-07-18 15:10:38 -0400 | [diff] [blame] | 120 | echo -e "${lBlue}Set up the inescure registry config for hostname ${lCyan}vinstall${NC}" |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 121 | sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall:5000"/' ansible/group_vars/all |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 122 | echo '{' > ansible/roles/voltha/templates/daemon.json |
| 123 | echo '"insecure-registries" : ["vinstall:5000"]' >> ansible/roles/voltha/templates/daemon.json |
| 124 | echo '}' >> ansible/roles/voltha/templates/daemon.json |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 125 | fi |
| 126 | |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 127 | # Check to make sure that the vagrant-libvirt network is both defined and started |
| 128 | echo -e "${lBlue}Verify tha the ${lCyan}vagrant-libvirt${lBlue} network is defined and started${NC}" |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 129 | virsh net-list --all | grep "vagrant-libvirt" > /dev/null |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 130 | rtrn=$? |
| 131 | if [ $rtrn -eq 1 ]; then |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 132 | # Not defined |
| 133 | echo -e "${lBlue}Defining the ${lCyan}vagrant-libvirt${lBlue} network${NC}" |
| 134 | virsh net-define vagrant-libvirt.xml |
| 135 | echo -e "${lBlue}Starting the ${lCyan}vagrant-libvirt${lBlue} network${NC}" |
| 136 | virsh net-start vagrant-libvirt |
| 137 | else |
| 138 | virsh net-list | grep "vagrant-libvirt" > /dev/null |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 139 | rtrn=$? |
| 140 | if [ $rtrn -eq 1 ]; then |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 141 | # Defined but not started |
| 142 | echo -e "${lBlue}Starting the ${lCyan}vagrant-libvirt${lBlue} network${NC}" |
| 143 | virsh net-start vagrant-libvirt |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 144 | |
| 145 | else |
| 146 | # Defined and running |
| 147 | echo -e "${lBlue}The ${lCyan}vagrant-libvirt${lBlue} network is ${green} running${NC}" |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 148 | fi |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 149 | fi |
| 150 | |
| 151 | # Check that the default storage pool exists and create it if it doesn't |
| 152 | virsh pool-list --all | grep default > /dev/null |
| 153 | rtrn=$? |
| 154 | if [ $rtrn -eq 1 ]; then |
| 155 | # Not defined |
| 156 | echo -e "${lBlue}Defining the ${lCyan}defaul${lBlue} storage pool${NC}" |
| 157 | virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images/ |
| 158 | virsh pool-autostart default |
| 159 | echo -e "${lBlue}Starting the ${lCyan}defaul${lBlue} storage pool${NC}" |
| 160 | virsh pool-start default |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 161 | else |
Sergio Slobodrian | 7c5e885 | 2017-07-31 20:17:14 -0400 | [diff] [blame] | 162 | virsh pool-list | grep default > /dev/null |
| 163 | rtrn=$? |
| 164 | if [ $rtrn -eq 1 ]; then |
| 165 | # Defined but not started |
| 166 | echo -e "${lBlue}Starting the ${lCyan}defaul${lBlue} storage pool${NC}" |
| 167 | virsh pool-start default |
| 168 | else |
| 169 | # Defined and running |
| 170 | echo -e "${lBlue}The ${lCyan}default${lBlue} storage pool ${green} running${NC}" |
| 171 | fi |
Sergio Slobodrian | 73e311a | 2017-07-26 11:12:45 -0400 | [diff] [blame] | 172 | fi |
| 173 | |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 174 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 175 | # Shut down the domain in case it's running. |
| 176 | echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}" |
| 177 | ctr=0 |
| 178 | vStat=`virsh list | grep $iVmName` |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 179 | virsh shutdown $iVmName |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 180 | while [ ! -z "$vStat" ]; |
| 181 | do |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 182 | echo "Waiting for $iVmName to shut down" |
| 183 | sleep 2 |
Sergio Slobodrian | 497e6e8 | 2017-07-18 15:10:38 -0400 | [diff] [blame] | 184 | vStat=`virsh list | grep "$iVmName "` |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 185 | ctr=`expr $ctr + 1` |
| 186 | if [ $ctr -eq $shutdownTimeout ]; then |
| 187 | echo -e "${red}Tired of waiting, forcing the VM off${NC}" |
| 188 | virsh destroy $iVmName |
Sergio Slobodrian | 497e6e8 | 2017-07-18 15:10:38 -0400 | [diff] [blame] | 189 | vStat=`virsh list | grep "$iVmName "` |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 190 | fi |
| 191 | done |
| 192 | |
| 193 | |
| 194 | # Delete the VM and ignore any errors should they occur |
| 195 | echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}" |
| 196 | virsh undefine $iVmName |
| 197 | |
| 198 | # Remove the associated volume |
| 199 | echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}" |
| 200 | virsh vol-delete "${iVmName}.qcow2" default |
| 201 | |
| 202 | # Clone the base vanilla ubuntu install |
| 203 | echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}" |
| 204 | virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default |
| 205 | |
| 206 | # Create the xml file and define the VM for virsh |
| 207 | echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 208 | cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 209 | |
| 210 | virsh define tmp.xml |
| 211 | |
| 212 | rm tmp.xml |
| 213 | |
| 214 | # Start the VMm, if it's already running just ignore the error |
| 215 | echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 216 | virsh start $iVmName > /dev/null 2>&1 |
| 217 | |
| 218 | # Generate a keypair for communicating with the VM |
| 219 | echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}" |
| 220 | ssh-keygen -f ./key -t rsa -N '' |
| 221 | |
| 222 | mv key key.pem |
| 223 | |
| 224 | # Clone BashLogin.sh and add the public key to it for later use. |
| 225 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
| 226 | cp BashLogin.sh bash_login.sh |
| 227 | echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh |
| 228 | cat key.pub >> bash_login.sh |
| 229 | echo "HERE" >> bash_login.sh |
| 230 | echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh |
| 231 | echo "rm .bash_login" >> bash_login.sh |
| 232 | echo "logout" >> bash_login.sh |
| 233 | rm key.pub |
| 234 | |
| 235 | |
| 236 | |
| 237 | # Get the VM's IP address |
| 238 | ctr=0 |
| 239 | ipAddr="" |
| 240 | while [ -z "$ipAddr" ]; |
| 241 | do |
| 242 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 243 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 244 | sleep 3 |
| 245 | if [ $ctr -eq $ipTimeout ]; then |
| 246 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 247 | exit |
| 248 | fi |
| 249 | ctr=`expr $ctr + 1` |
| 250 | done |
| 251 | |
| 252 | echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}" |
| 253 | |
| 254 | # Copy the pre-config file to the VM |
| 255 | echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}" |
| 256 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login |
| 257 | |
| 258 | rm bash_login.sh |
| 259 | |
| 260 | # Run the pre-config file on the VM |
| 261 | echo -e "${lBlue}Running the pre-configuration script on the VM${NC}" |
| 262 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr |
| 263 | |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 264 | # If we're in test mode, change the hostname of the installer vm |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 265 | # also start the 3 vagrant target VMs |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 266 | if [ "$testMode" == "yes" ]; then |
| 267 | echo -e "${lBlue}Test mode, change the installer host name to ${lCyan}vinstall${uId}${NC}" |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 268 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \ |
| 269 | sudo hostnamectl set-hostname vinstall${uId} |
| 270 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \ |
| 271 | sudo service networking restart |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 272 | |
| 273 | echo -e "${lBlue}Testing, start the ${lCyan}ha-serv${lBlue} VMs${NC}" |
| 274 | vagrant destroy ha-serv${uId}-{1,2,3} |
| 275 | vagrant up ha-serv${uId}-{1,2,3} |
| 276 | ./devSetHostList.sh |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 277 | |
| 278 | if [ "$useKubernetes" == "yes" ]; then |
| 279 | ./devSetKubernetes.sh |
| 280 | fi |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 281 | fi |
| 282 | |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 283 | # Ensure that the voltha VM is running so that images can be secured |
| 284 | echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}" |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 285 | vVm=`virsh list | grep "voltha_voltha${uId}"` |
| 286 | #echo "vVm: $vVm" |
| 287 | #echo "rebuildVoltha: $rebuildVoltha" |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 288 | |
Sergio Slobodrian | cab0a39 | 2017-07-13 08:42:10 -0400 | [diff] [blame] | 289 | |
| 290 | if [ -z "$vVm" -o "$rebuildVoltha" == "yes" ]; then |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 291 | if [ "$testMode" == "yes" ]; then |
Sergio Slobodrian | 92136d0 | 2017-08-22 21:48:42 -0400 | [diff] [blame] | 292 | ./BuildVoltha.sh "test" |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 293 | rtrn=$? |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 294 | else |
| 295 | # Default to installer mode |
Sergio Slobodrian | 92136d0 | 2017-08-22 21:48:42 -0400 | [diff] [blame] | 296 | ./BuildVoltha.sh "install" |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 297 | rtrn=$? |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 298 | fi |
| 299 | if [ $rtrn -ne 0 ]; then |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 300 | echo -e "${red}Voltha build failed!! ${lCyan}Please review the log and correct${lBlue} is running${NC}" |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 301 | exit 1 |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 302 | fi |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 303 | |
| 304 | if [ "$useKubernetes" == "yes" ]; then |
| 305 | # Load required k8s libraries on the voltha instance |
| 306 | ./preloadKubernetes.sh |
| 307 | fi |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 308 | fi |
| 309 | |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 310 | # Extract all the image names and tags from the running voltha VM |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 311 | # when running in test mode. This will provide the entire suite |
| 312 | # of available containers to the VM cluster. |
| 313 | |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 314 | if [ "$testMode" == "yes" ]; then |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 315 | echo -e "${lBlue}Extracting the docker image list from the voltha VM${NC}" |
| 316 | volIpAddr=`virsh domifaddr $vVmName${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 317 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ../.vagrant/machines/voltha${uId}/libvirt/private_key vagrant@$volIpAddr "docker image ls" > images.tmp |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 318 | # Construct list of images; exclude all entries that point to the registry |
| 319 | cat images.tmp | grep -v :5000 | tail -n +2 | awk '{printf(" - %s:%s\n", $1, $2)}' | grep -v "<none>" > image-list.cfg |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 320 | rm -f images.tmp |
| 321 | sed -i -e '/voltha_containers:/,$d' ansible/group_vars/all |
| 322 | echo "voltha_containers:" >> ansible/group_vars/all |
| 323 | cat image-list.cfg >> ansible/group_vars/all |
| 324 | rm -f image-list.cfg |
Sergio Slobodrian | 6e270c1 | 2017-08-09 23:06:49 -0400 | [diff] [blame] | 325 | echo -e "${lBlue}Gussing at the cord home directory for ${yellow}`whoami`${NC}" |
| 326 | sed -i -e "/cord_home:/s#.*#cord_home: `pwd | sed -e 's~/incubator/voltha/install~~'`#" ansible/group_vars/all |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 327 | else |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 328 | echo -e "${lBlue}Set up the docker image list from ${lCyan}containers.cfg${NC}" |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 329 | sed -i -e '/voltha_containers:/,$d' ansible/group_vars/all |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 330 | |
| 331 | if [ "$useKubernetes" == "yes" ]; then |
| 332 | cat containers.cfg.k8s >> ansible/group_vars/all |
| 333 | else |
| 334 | cat containers.cfg >> ansible/group_vars/all |
| 335 | fi |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 336 | fi |
Sergio Slobodrian | ba9cbd8 | 2017-06-22 11:45:49 -0400 | [diff] [blame] | 337 | |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 338 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 339 | # Install python which is required for ansible |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 340 | echo -e "${lBlue}Installing ${lCyan}Python${NC}" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 341 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 342 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python python-netaddr |
Sergio Slobodrian | 9d9c844 | 2017-07-25 07:55:42 -0400 | [diff] [blame] | 343 | |
| 344 | # Move all the python deb files to their own directory so they can be installed first |
| 345 | echo -e "${lBlue}Caching ${lCyan}Python${lBlue} install${NC}" |
| 346 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr mkdir python-deb |
| 347 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo mv /var/cache/apt/archives/*.deb /home/vinstall/python-deb" |
| 348 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo chown -R vinstall.vinstall /home/vinstall/python-deb" |
| 349 | |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 350 | if [ "$useKubernetes" == "yes" ]; then |
| 351 | echo -e "${lBlue}Cloning ${lCyan}Kubespray${lBlue} repository${NC}" |
Stephane Barbarie | 78d9fa6 | 2018-04-19 14:11:05 -0400 | [diff] [blame] | 352 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "git clone --branch v2.5.0 https://github.com/kubernetes-incubator/kubespray.git /home/vinstall/kubespray" |
Stephane Barbarie | 2cbffca | 2018-03-26 16:20:03 -0400 | [diff] [blame] | 353 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo chown -R vinstall.vinstall /home/vinstall/kubespray" |
| 354 | fi |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 355 | |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 356 | # Create the docker.cfg file in the ansible tree using the VMs IP address |
| 357 | echo 'DOCKER_OPTS="$DOCKER_OPTS --insecure-registry '$ipAddr':5000 -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --registry-mirror=http://'$ipAddr':5001"' > ansible/roles/docker/templates/docker.cfg |
| 358 | |
| 359 | # Add the voltha vm's information to the ansible tree |
| 360 | echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}" |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 361 | vIpAddr=`virsh domifaddr voltha_voltha${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 362 | echo "[voltha]" > ansible/hosts/voltha |
| 363 | echo $vIpAddr >> ansible/hosts/voltha |
Sergio Slobodrian | 7c48362 | 2017-06-13 15:51:34 -0400 | [diff] [blame] | 364 | echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha${uId}/libvirt/private_key" > ansible/host_vars/$vIpAddr |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 365 | |
| 366 | |
| 367 | # Prepare to launch the ansible playbook to configure the installer VM |
| 368 | echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}" |
| 369 | echo "[installer]" > ansible/hosts/installer |
| 370 | echo "$ipAddr" >> ansible/hosts/installer |
| 371 | echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr |
| 372 | |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 373 | # Launch the ansible playbooks |
| 374 | |
| 375 | echo -e "${lBlue}Launching the ${lCyan}volthainstall${lBlue} ansible playbook on the installer vm${NC}" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 376 | ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 377 | rtrn=$? |
| 378 | if [ $rtrn -ne 0 ]; then |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 379 | echo -e "${red}PLAYBOOK FAILED, Exiting${NC}" |
| 380 | exit |
| 381 | fi |
| 382 | |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 383 | |
| 384 | echo -e "${lBlue}Launching the ${lCyan}volthainstall${lBlue} ansible playbook on the voltha vm${NC}" |
| 385 | ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha |
| 386 | rtrn=$? |
| 387 | if [ $rtrn -ne 0 ]; then |
| 388 | echo -e "${red}PLAYBOOK FAILED, Exiting${NC}" |
| 389 | exit |
| 390 | fi |
| 391 | |
| 392 | if [ "$testMode" == "yes" ]; then |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 393 | echo -e "${lBlue}Testing, the install image ${red}WILL NOT${lBlue} be built${NC}" |
Sergio Slobodrian | 6128779 | 2017-06-27 12:14:05 -0400 | [diff] [blame] | 394 | |
| 395 | |
| 396 | # Reboot the installer |
| 397 | echo -e "${lBlue}Rebooting the installer${NC}" |
| 398 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo telinit 6 |
| 399 | # Wait for the host to shut down |
| 400 | sleep 5 |
| 401 | |
| 402 | ctr=0 |
| 403 | ipAddr="" |
| 404 | while [ -z "$ipAddr" ]; |
| 405 | do |
| 406 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 407 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 408 | sleep 3 |
| 409 | if [ $ctr -eq $ipTimeout ]; then |
| 410 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 411 | exit |
| 412 | fi |
| 413 | ctr=`expr $ctr + 1` |
| 414 | done |
| 415 | |
| 416 | echo -e "${lBlue}Running the installer${NC}" |
| 417 | echo "~/installer.sh" > tmp_bash_login |
| 418 | echo "rm ~/.bash_login" >> tmp_bash_login |
| 419 | echo "logout" >> tmp_bash_login |
| 420 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem tmp_bash_login vinstall@$ipAddr:.bash_login |
| 421 | rm -f tmp_bash_login |
| 422 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr |
| 423 | |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 424 | else |
| 425 | echo -e "${lBlue}Building, the install image (this can take a while)${NC}" |
| 426 | # Create a temporary directory for all the installer files |
| 427 | mkdir tmp_installer |
| 428 | cp vmTemplate.xml tmp_installer |
| 429 | # Shut down the installer vm |
| 430 | ctr=0 |
| 431 | vStat=`virsh list | grep $iVmName` |
| 432 | virsh shutdown $iVmName |
| 433 | while [ ! -z "$vStat" ]; |
| 434 | do |
| 435 | echo "Waiting for $iVmName to shut down" |
| 436 | sleep 2 |
Sergio Slobodrian | 497e6e8 | 2017-07-18 15:10:38 -0400 | [diff] [blame] | 437 | vStat=`virsh list | grep "$iVmName "` |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 438 | ctr=`expr $ctr + 1` |
| 439 | if [ $ctr -eq $shutdownTimeout ]; then |
| 440 | echo -e "${red}Tired of waiting, forcing the VM off${NC}" |
| 441 | virsh destroy $iVmName |
Sergio Slobodrian | 497e6e8 | 2017-07-18 15:10:38 -0400 | [diff] [blame] | 442 | vStat=`virsh list | grep "$iVmName "` |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 443 | fi |
| 444 | done |
| 445 | # Copy the install bootstrap script to the installer directory |
| 446 | cp BootstrapInstaller.sh tmp_installer |
| 447 | # Copy the private key to access the VM |
| 448 | cp key.pem tmp_installer |
| 449 | pushd tmp_installer > /dev/null 2>&1 |
| 450 | # Copy the vm image to the installer directory |
| 451 | virsh vol-dumpxml $iVmName.qcow2 default | sed -e 's/<key.*key>//' | sed -e '/^[ ]*$/d' > ${iVmName}_volume.xml |
| 452 | virsh pool-create-as installer --type dir --target `pwd` |
| 453 | virsh vol-create-from installer ${iVmName}_volume.xml $iVmName.qcow2 --inputpool default |
| 454 | virsh pool-destroy installer |
| 455 | # The image is copied in as root. It needs to have ownership changed |
| 456 | # this will result in a password prompt. |
| 457 | sudo chown `whoami`.`whoami` $iVmName.qcow2 |
| 458 | # Now create the installer tar file |
| 459 | tar cjf ../$installerArchive . |
| 460 | popd > /dev/null 2>&1 |
| 461 | # Clean up |
| 462 | rm -fr tmp_installer |
| 463 | # Final location for the installer |
| 464 | rm -fr $installerDirectory |
| 465 | mkdir $installerDirectory |
Sergio Slobodrian | 36e1655 | 2017-06-19 11:00:45 -0400 | [diff] [blame] | 466 | cp deployInstaller.sh $installerDirectory |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 467 | # Check the image size and determine if it needs to be split. |
| 468 | # To be safe, split the image into chunks smaller than 2G so that |
| 469 | # it will fit on a FAT32 volume. |
| 470 | fSize=`ls -l $installerArchive | awk '{print $5'}` |
| 471 | if [ $fSize -gt 2000000000 ]; then |
| 472 | echo -e "${lBlue}Installer file too large, breaking into parts${NC}" |
| 473 | # The file is too large, breaking it up into parts |
| 474 | sPos=0 |
| 475 | fnn="00" |
| 476 | while dd if=$installerArchive of=${installerDirectory}/${installerPart}$fnn \ |
| 477 | bs=1900MB count=1 skip=$sPos > /dev/null 2>&1 |
| 478 | do |
| 479 | sPos=`expr $sPos + 1` |
| 480 | if [ ! -s ${installerDirectory}/${installerPart}$fnn ]; then |
| 481 | rm -f ${installerDirectory}/${installerPart}$fnn |
| 482 | break |
| 483 | fi |
| 484 | if [ $sPos -lt 10 ]; then |
| 485 | fnn="0$sPos" |
| 486 | else |
| 487 | fnn="$sPos" |
| 488 | fi |
| 489 | done |
| 490 | else |
| 491 | cp $installerArchive $installerDirectory |
| 492 | fi |
| 493 | # Clean up |
| 494 | rm $installerArchive |
Sergio Slobodrian | f74fa07 | 2017-06-28 09:33:24 -0400 | [diff] [blame] | 495 | echo -e "${lBlue}The install image is built and can be found in ${lCyan}$installerDirectory${NC}" |
| 496 | echo -e "${lBlue}Copy all the files in ${lCyan}$installerDirectory${lBlue} to the traasnport media${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 497 | fi |