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