Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | baseImage="Ubuntu1604LTS" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 4 | iVmName="vInstaller" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 5 | iVmNetwork="vagrant-libvirt" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 6 | installerArchive="installer.tar.bz2" |
| 7 | installerDirectory="volthaInstaller" |
| 8 | installerPart="installer.part" |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 9 | shutdownTimeout=5 |
| 10 | ipTimeout=10 |
| 11 | |
| 12 | lBlue='\033[1;34m' |
| 13 | green='\033[0;32m' |
| 14 | orange='\033[0;33m' |
| 15 | NC='\033[0m' |
| 16 | red='\033[0;31m' |
| 17 | yellow='\033[1;33m' |
| 18 | dGrey='\033[1;30m' |
| 19 | lGrey='\033[1;37m' |
| 20 | lCyan='\033[1;36m' |
| 21 | |
| 22 | wd=`pwd` |
| 23 | |
| 24 | # Validate that vagrant is installed. |
| 25 | echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}" |
| 26 | vInst=`which vagrant` |
| 27 | |
| 28 | if [ -z "$vInst" ]; then |
| 29 | wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb |
| 30 | sudo dpkg -i vagrant_1.8.5_x86_64.deb |
| 31 | rm vagrant_1.8.5_x86_64.deb |
| 32 | fi |
| 33 | unset vInst |
| 34 | |
| 35 | # Validate that ansible is installed |
| 36 | echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}" |
| 37 | aInst=`which ansible` |
| 38 | |
| 39 | if [ -z "$aInst" ]; then |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 40 | sudo apt-get install -y software-properties-common |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 41 | sudo apt-add-repository ppa:ansible/ansible |
| 42 | sudo apt-get update |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 43 | sudo apt-get install -y ansible |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 44 | fi |
| 45 | unset vInst |
| 46 | |
| 47 | # Ensure that the voltha VM is running so that images can be secured |
| 48 | echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}" |
| 49 | vVM=`virsh list | grep voltha_voltha` |
| 50 | |
| 51 | if [ -z "$vVM" ]; then |
| 52 | ./BuildVoltha.sh |
| 53 | fi |
| 54 | |
| 55 | # Verify if this is intended to be a test environment, if so start 3 VMs |
| 56 | # to emulate the production installation cluster. |
| 57 | if [ $# -eq 1 -a "$1" == "test" ]; then |
| 58 | echo -e "${lBlue}Testing, create the ${lCyan}ha-serv${lBlue} VMs${NC}" |
| 59 | vagrant destroy ha-serv{1,2,3} |
| 60 | vagrant up ha-serv{1,2,3} |
| 61 | ./devSetHostList.sh |
| 62 | else |
| 63 | rm -fr .test |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 64 | # Clean out the install config file keeping only the commented lines |
| 65 | # which serve as documentation. |
| 66 | sed -i -e '/^#/!d' install.cfg |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 67 | fi |
| 68 | |
| 69 | # Shut down the domain in case it's running. |
| 70 | echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}" |
| 71 | ctr=0 |
| 72 | vStat=`virsh list | grep $iVmName` |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 73 | virsh shutdown $iVmName |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 74 | while [ ! -z "$vStat" ]; |
| 75 | do |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 76 | echo "Waiting for $iVmName to shut down" |
| 77 | sleep 2 |
| 78 | vStat=`virsh list | grep $iVmName` |
| 79 | ctr=`expr $ctr + 1` |
| 80 | if [ $ctr -eq $shutdownTimeout ]; then |
| 81 | echo -e "${red}Tired of waiting, forcing the VM off${NC}" |
| 82 | virsh destroy $iVmName |
| 83 | vStat=`virsh list | grep $iVmName` |
| 84 | fi |
| 85 | done |
| 86 | |
| 87 | |
| 88 | # Delete the VM and ignore any errors should they occur |
| 89 | echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}" |
| 90 | virsh undefine $iVmName |
| 91 | |
| 92 | # Remove the associated volume |
| 93 | echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}" |
| 94 | virsh vol-delete "${iVmName}.qcow2" default |
| 95 | |
| 96 | # Clone the base vanilla ubuntu install |
| 97 | echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}" |
| 98 | virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default |
| 99 | |
| 100 | # Create the xml file and define the VM for virsh |
| 101 | echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | virsh define tmp.xml |
| 105 | |
| 106 | rm tmp.xml |
| 107 | |
| 108 | # Start the VMm, if it's already running just ignore the error |
| 109 | echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 110 | virsh start $iVmName > /dev/null 2>&1 |
| 111 | |
| 112 | # Generate a keypair for communicating with the VM |
| 113 | echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}" |
| 114 | ssh-keygen -f ./key -t rsa -N '' |
| 115 | |
| 116 | mv key key.pem |
| 117 | |
| 118 | # Clone BashLogin.sh and add the public key to it for later use. |
| 119 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
| 120 | cp BashLogin.sh bash_login.sh |
| 121 | echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh |
| 122 | cat key.pub >> bash_login.sh |
| 123 | echo "HERE" >> bash_login.sh |
| 124 | echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh |
| 125 | echo "rm .bash_login" >> bash_login.sh |
| 126 | echo "logout" >> bash_login.sh |
| 127 | rm key.pub |
| 128 | |
| 129 | |
| 130 | |
| 131 | # Get the VM's IP address |
| 132 | ctr=0 |
| 133 | ipAddr="" |
| 134 | while [ -z "$ipAddr" ]; |
| 135 | do |
| 136 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 137 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 138 | sleep 3 |
| 139 | if [ $ctr -eq $ipTimeout ]; then |
| 140 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 141 | exit |
| 142 | fi |
| 143 | ctr=`expr $ctr + 1` |
| 144 | done |
| 145 | |
| 146 | echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}" |
| 147 | |
| 148 | # Copy the pre-config file to the VM |
| 149 | echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}" |
| 150 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login |
| 151 | |
| 152 | rm bash_login.sh |
| 153 | |
| 154 | # Run the pre-config file on the VM |
| 155 | echo -e "${lBlue}Running the pre-configuration script on the VM${NC}" |
| 156 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr |
| 157 | |
| 158 | # Install python which is required for ansible |
| 159 | echo -e "${lBlue}Installing python${NC}" |
| 160 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update |
| 161 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python |
| 162 | |
| 163 | # Make sure the VM is up-to-date |
| 164 | echo -e "${lBlue}Ensure that the VM is up-to-date${NC}" |
| 165 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update |
| 166 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y upgrade |
| 167 | |
| 168 | |
| 169 | |
| 170 | # Copy the apt repository to the VM because it's way too slow using ansible |
| 171 | #echo -e "${red}NOT COPYING${lBlue} the apt-repository to the VM, ${red}TESTING ONLY REMOVE FOR PRODUCTION${NC}" |
| 172 | #echo -e "${lBlue}Copy the apt-repository to the VM${NC}" |
| 173 | #scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem -r apt-mirror vinstall@$ipAddr:apt-mirror |
| 174 | |
| 175 | # Create the docker.cfg file in the ansible tree using the VMs IP address |
| 176 | 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 |
| 177 | |
| 178 | # Add the voltha vm's information to the ansible tree |
| 179 | echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}" |
| 180 | vIpAddr=`virsh domifaddr voltha_voltha | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 181 | echo "[voltha]" > ansible/hosts/voltha |
| 182 | echo $vIpAddr >> ansible/hosts/voltha |
| 183 | echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha/libvirt/private_key" > ansible/host_vars/$vIpAddr |
| 184 | |
| 185 | |
| 186 | # Prepare to launch the ansible playbook to configure the installer VM |
| 187 | echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}" |
| 188 | echo "[installer]" > ansible/hosts/installer |
| 189 | echo "$ipAddr" >> ansible/hosts/installer |
| 190 | echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr |
| 191 | |
| 192 | # Launch the ansible playbook |
| 193 | echo -e "${lBlue}Launching the ansible playbook${NC}" |
| 194 | ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 195 | if [ $? -ne 0 ]; then |
| 196 | echo -e "${red}PLAYBOOK FAILED, Exiting${NC}" |
| 197 | exit |
| 198 | fi |
Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 199 | ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 200 | if [ $? -ne 0 ]; then |
| 201 | echo -e "${red}PLAYBOOK FAILED, Exiting${NC}" |
| 202 | exit |
| 203 | fi |
| 204 | |
| 205 | if [ $# -eq 1 -a "$1" == "test" ]; then |
Sergio Slobodrian | d24189e | 2017-06-10 23:27:15 -0400 | [diff] [blame] | 206 | echo -e "${lBlue}Testing, the install image ${red}WILL NOT${lBlue} be built${NC}" |
Sergio Slobodrian | c547771 | 2017-06-07 11:56:56 -0400 | [diff] [blame] | 207 | else |
| 208 | echo -e "${lBlue}Building, the install image (this can take a while)${NC}" |
| 209 | # Create a temporary directory for all the installer files |
| 210 | mkdir tmp_installer |
| 211 | cp vmTemplate.xml tmp_installer |
| 212 | # Shut down the installer vm |
| 213 | ctr=0 |
| 214 | vStat=`virsh list | grep $iVmName` |
| 215 | virsh shutdown $iVmName |
| 216 | while [ ! -z "$vStat" ]; |
| 217 | do |
| 218 | echo "Waiting for $iVmName to shut down" |
| 219 | sleep 2 |
| 220 | vStat=`virsh list | grep $iVmName` |
| 221 | ctr=`expr $ctr + 1` |
| 222 | if [ $ctr -eq $shutdownTimeout ]; then |
| 223 | echo -e "${red}Tired of waiting, forcing the VM off${NC}" |
| 224 | virsh destroy $iVmName |
| 225 | vStat=`virsh list | grep $iVmName` |
| 226 | fi |
| 227 | done |
| 228 | # Copy the install bootstrap script to the installer directory |
| 229 | cp BootstrapInstaller.sh tmp_installer |
| 230 | # Copy the private key to access the VM |
| 231 | cp key.pem tmp_installer |
| 232 | pushd tmp_installer > /dev/null 2>&1 |
| 233 | # Copy the vm image to the installer directory |
| 234 | virsh vol-dumpxml $iVmName.qcow2 default | sed -e 's/<key.*key>//' | sed -e '/^[ ]*$/d' > ${iVmName}_volume.xml |
| 235 | virsh pool-create-as installer --type dir --target `pwd` |
| 236 | virsh vol-create-from installer ${iVmName}_volume.xml $iVmName.qcow2 --inputpool default |
| 237 | virsh pool-destroy installer |
| 238 | # The image is copied in as root. It needs to have ownership changed |
| 239 | # this will result in a password prompt. |
| 240 | sudo chown `whoami`.`whoami` $iVmName.qcow2 |
| 241 | # Now create the installer tar file |
| 242 | tar cjf ../$installerArchive . |
| 243 | popd > /dev/null 2>&1 |
| 244 | # Clean up |
| 245 | rm -fr tmp_installer |
| 246 | # Final location for the installer |
| 247 | rm -fr $installerDirectory |
| 248 | mkdir $installerDirectory |
| 249 | cp installVoltha.sh $installerDirectory |
| 250 | # Check the image size and determine if it needs to be split. |
| 251 | # To be safe, split the image into chunks smaller than 2G so that |
| 252 | # it will fit on a FAT32 volume. |
| 253 | fSize=`ls -l $installerArchive | awk '{print $5'}` |
| 254 | if [ $fSize -gt 2000000000 ]; then |
| 255 | echo -e "${lBlue}Installer file too large, breaking into parts${NC}" |
| 256 | # The file is too large, breaking it up into parts |
| 257 | sPos=0 |
| 258 | fnn="00" |
| 259 | while dd if=$installerArchive of=${installerDirectory}/${installerPart}$fnn \ |
| 260 | bs=1900MB count=1 skip=$sPos > /dev/null 2>&1 |
| 261 | do |
| 262 | sPos=`expr $sPos + 1` |
| 263 | if [ ! -s ${installerDirectory}/${installerPart}$fnn ]; then |
| 264 | rm -f ${installerDirectory}/${installerPart}$fnn |
| 265 | break |
| 266 | fi |
| 267 | if [ $sPos -lt 10 ]; then |
| 268 | fnn="0$sPos" |
| 269 | else |
| 270 | fnn="$sPos" |
| 271 | fi |
| 272 | done |
| 273 | else |
| 274 | cp $installerArchive $installerDirectory |
| 275 | fi |
| 276 | # Clean up |
| 277 | rm $installerArchive |
| 278 | echo -e "${lBlue}The install image is built and can be found in ${yellow}$installerDirectory${NC}" |
| 279 | echo -e "${lBlue}Copy all the files in ${yellow}$installerDirectory${lBlue} to the traasnport media${NC}" |
| 280 | fi |