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