blob: 3a9b06d488c34b57c8e92084c42bf61bb055d5b6 [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
2
3baseImage="Ubuntu1604LTS"
4iVmName="Ubuntu1604LTS-1"
5iVmNetwork="vagrant-libvirt"
6shutdownTimeout=5
7ipTimeout=10
8
9lBlue='\033[1;34m'
10green='\033[0;32m'
11orange='\033[0;33m'
12NC='\033[0m'
13red='\033[0;31m'
14yellow='\033[1;33m'
15dGrey='\033[1;30m'
16lGrey='\033[1;37m'
17lCyan='\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.
65for i in install_ha-serv1 install_ha-serv2 install_ha-serv3
66do
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
70done
71
72exit
73
74echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}"
75ssh-keygen -f ./key -t rsa -N ''
76
77mv key key.pem
78
79# Clone BashLogin.sh and add the public key to it for later use.
80echo -e "${lBlue}Creating the pre-configuration script${NC}"
81cp BashLogin.sh bash_login.sh
82echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh
83cat key.pub >> bash_login.sh
84echo "HERE" >> bash_login.sh
85echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh
86echo "rm .bash_login" >> bash_login.sh
87echo "logout" >> bash_login.sh
88rm key.pub
89
90
91
92# Get the VM's IP address
93ctr=0
94ipAddr=""
95while [ -z "$ipAddr" ];
96do
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`
105done
106
107echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}"
108
109# Copy the pre-config file to the VM
110echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}"
111scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login
112
113rm bash_login.sh
114
115# Run the pre-config file on the VM
116echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
117ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr
118
119# Make sure the VM is up-to-date
120echo -e "${lBlue}Ensure that the VM is up-to-date${NC}"
121ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
122ssh -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
125echo '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
128echo -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
132ssh -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
135echo -e "${lBlue}Transferring the ansible directory to the VM${NC}"
136scp -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
139echo -e "${lBlue}Get the GPG key for docker to allow ansible playbooks to run successfully${NC}"
140ssh -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
143echo -e "${lBlue}Bootstrap ansible${NC}"
144ssh -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
147echo -e "${lBlue}Run the nsible playbook for the installer${NC}"
148ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo PYTHONUNBUFFERED=1 ansible-playbook /home/vinstall/ansible/volthainstall.yml -c local