Sergio Slobodrian | ee4b2bc | 2017-06-05 10:08:59 -0400 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | baseImage="Ubuntu1604LTS" |
| 4 | iVmName="Ubuntu1604LTS-1" |
| 5 | iVmNetwork="vagrant-libvirt" |
| 6 | shutdownTimeout=5 |
| 7 | ipTimeout=10 |
| 8 | |
| 9 | lBlue='\033[1;34m' |
| 10 | green='\033[0;32m' |
| 11 | orange='\033[0;33m' |
| 12 | NC='\033[0m' |
| 13 | red='\033[0;31m' |
| 14 | yellow='\033[1;33m' |
| 15 | dGrey='\033[1;30m' |
| 16 | lGrey='\033[1;37m' |
| 17 | lCyan='\033[1;36m' |
| 18 | |
| 19 | # Shut down the domain in case it's running. |
| 20 | #echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}" |
| 21 | #ctr=0 |
| 22 | #vStat=`virsh list | grep $iVmName` |
| 23 | #while [ ! -z "$vStat" ]; |
| 24 | #do |
| 25 | # virsh shutdown $iVmName |
| 26 | # echo "Waiting for $iVmName to shut down" |
| 27 | # sleep 2 |
| 28 | # vStat=`virsh list | grep $iVmName` |
| 29 | # ctr=`expr $ctr + 1` |
| 30 | # if [ $ctr -eq $shutdownTimeout ]; then |
| 31 | # echo -e "${red}Tired of waiting, forcing the VM off${NC}" |
| 32 | # virsh destroy $iVmName |
| 33 | # vStat=`virsh list | grep $iVmName` |
| 34 | # fi |
| 35 | #done |
| 36 | |
| 37 | |
| 38 | # Delete the VM and ignore any errors should they occur |
| 39 | #echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}" |
| 40 | #virsh undefine $iVmName |
| 41 | |
| 42 | # Remove the associated volume |
| 43 | #echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}" |
| 44 | #virsh vol-delete "${iVmName}.qcow2" default |
| 45 | |
| 46 | # Clone the base vanilla ubuntu install |
| 47 | #echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}" |
| 48 | #virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default |
| 49 | |
| 50 | # Create the xml file and define the VM for virsh |
| 51 | #echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 52 | #cat vmTemplate.xml | sed -e "s/{{VMName}}/$iVmName/g" | sed -e "s/{{VMNetwork}}/$iVmNetwork/g" > tmp.xml |
| 53 | |
| 54 | #virsh define tmp.xml |
| 55 | |
| 56 | #rm tmp.xml |
| 57 | |
| 58 | # Start the VMm, if it's already running just ignore the error |
| 59 | #echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}" |
| 60 | #virsh start $iVmName > /dev/null 2>&1 |
| 61 | |
| 62 | |
| 63 | # Configure ansible's key for communicating with the VMs... Testing only, this will |
| 64 | # be taken care of by the installer in the future. |
| 65 | for i in install_ha-serv1 install_ha-serv2 install_ha-serv3 |
| 66 | do |
| 67 | ipAddr=`virsh domifaddr $i | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 68 | m=`echo $i | sed -e 's/install_//'` |
| 69 | echo "ansible_ssh_private_key_file: .vagrant/machines/$m/libvirt/private_key" > ansible/host_vars/$ipAddr |
| 70 | done |
| 71 | |
| 72 | exit |
| 73 | |
| 74 | echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}" |
| 75 | ssh-keygen -f ./key -t rsa -N '' |
| 76 | |
| 77 | mv key key.pem |
| 78 | |
| 79 | # Clone BashLogin.sh and add the public key to it for later use. |
| 80 | echo -e "${lBlue}Creating the pre-configuration script${NC}" |
| 81 | cp BashLogin.sh bash_login.sh |
| 82 | echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh |
| 83 | cat key.pub >> bash_login.sh |
| 84 | echo "HERE" >> bash_login.sh |
| 85 | echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh |
| 86 | echo "rm .bash_login" >> bash_login.sh |
| 87 | echo "logout" >> bash_login.sh |
| 88 | rm key.pub |
| 89 | |
| 90 | |
| 91 | |
| 92 | # Get the VM's IP address |
| 93 | ctr=0 |
| 94 | ipAddr="" |
| 95 | while [ -z "$ipAddr" ]; |
| 96 | do |
| 97 | echo -e "${lBlue}Waiting for the VM's IP address${NC}" |
| 98 | ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'` |
| 99 | sleep 2 |
| 100 | if [ $ctr -eq $ipTimeout ]; then |
| 101 | echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}" |
| 102 | exit |
| 103 | fi |
| 104 | ctr=`expr $ctr + 1` |
| 105 | done |
| 106 | |
| 107 | echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}" |
| 108 | |
| 109 | # Copy the pre-config file to the VM |
| 110 | echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}" |
| 111 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login |
| 112 | |
| 113 | rm bash_login.sh |
| 114 | |
| 115 | # Run the pre-config file on the VM |
| 116 | echo -e "${lBlue}Running the pre-configuration script on the VM${NC}" |
| 117 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr |
| 118 | |
| 119 | # Make sure the VM is up-to-date |
| 120 | echo -e "${lBlue}Ensure that the VM is up-to-date${NC}" |
| 121 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update |
| 122 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y upgrade |
| 123 | |
| 124 | # Create the docker.cfg file in the ansible tree using the VMs IP address |
| 125 | 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 |
| 126 | |
| 127 | # Install ansible on the vm, it'll be used both here and for the install |
| 128 | echo -e "${lBlue}Installing ansible on the VM${NC}" |
| 129 | #ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get install software-properties-common |
| 130 | #ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-add-repository ppa:ansible/ansible |
| 131 | #ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update |
| 132 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install ansible |
| 133 | |
| 134 | # Copy the ansible files to the VM |
| 135 | echo -e "${lBlue}Transferring the ansible directory to the VM${NC}" |
| 136 | scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem -r ansible vinstall@$ipAddr:ansible |
| 137 | |
| 138 | # Get the GPG key for docker otherwise ansible calls break |
| 139 | echo -e "${lBlue}Get the GPG key for docker to allow ansible playbooks to run successfully${NC}" |
| 140 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D" |
| 141 | |
| 142 | # Bootstrap ansible |
| 143 | echo -e "${lBlue}Bootstrap ansible${NC}" |
| 144 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr ansible/scripts/bootstrap_ansible.sh |
| 145 | |
| 146 | # Run the ansible script to initialize the installer environment |
| 147 | echo -e "${lBlue}Run the nsible playbook for the installer${NC}" |
| 148 | ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo PYTHONUNBUFFERED=1 ansible-playbook /home/vinstall/ansible/volthainstall.yml -c local |