blob: bd253b6e6b320743fa08e3d51086d929ffe9658f [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
19wd=`pwd`
20
21# Validate that vagrant is installed.
22echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}"
23vInst=`which vagrant`
24
25if [ -z "$vInst" ]; then
26 wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb
27 sudo dpkg -i vagrant_1.8.5_x86_64.deb
28 rm vagrant_1.8.5_x86_64.deb
29fi
30unset vInst
31
32# Validate that ansible is installed
33echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}"
34aInst=`which ansible`
35
36if [ -z "$aInst" ]; then
37 sudo apt-get install software-properties-common
38 sudo apt-add-repository ppa:ansible/ansible
39 sudo apt-get update
40 sudo apt-get install ansible
41fi
42unset vInst
43
44# Ensure that the voltha VM is running so that images can be secured
45echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}"
46vVM=`virsh list | grep voltha_voltha`
47
48if [ -z "$vVM" ]; then
49 ./BuildVoltha.sh
50fi
51
52# Verify if this is intended to be a test environment, if so start 3 VMs
53# to emulate the production installation cluster.
54if [ $# -eq 1 -a "$1" == "test" ]; then
55 echo -e "${lBlue}Testing, create the ${lCyan}ha-serv${lBlue} VMs${NC}"
56 vagrant destroy ha-serv{1,2,3}
57 vagrant up ha-serv{1,2,3}
58 ./devSetHostList.sh
59else
60 rm -fr .test
61fi
62
63# Shut down the domain in case it's running.
64echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}"
65ctr=0
66vStat=`virsh list | grep $iVmName`
67while [ ! -z "$vStat" ];
68do
69 virsh shutdown $iVmName
70 echo "Waiting for $iVmName to shut down"
71 sleep 2
72 vStat=`virsh list | grep $iVmName`
73 ctr=`expr $ctr + 1`
74 if [ $ctr -eq $shutdownTimeout ]; then
75 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
76 virsh destroy $iVmName
77 vStat=`virsh list | grep $iVmName`
78 fi
79done
80
81
82# Delete the VM and ignore any errors should they occur
83echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}"
84virsh undefine $iVmName
85
86# Remove the associated volume
87echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}"
88virsh vol-delete "${iVmName}.qcow2" default
89
90# Clone the base vanilla ubuntu install
91echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}"
92virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default
93
94# Create the xml file and define the VM for virsh
95echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
96cat vmTemplate.xml | sed -e "s/{{VMName}}/$iVmName/g" | sed -e "s/{{VMNetwork}}/$iVmNetwork/g" > tmp.xml
97
98virsh define tmp.xml
99
100rm tmp.xml
101
102# Start the VMm, if it's already running just ignore the error
103echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
104virsh start $iVmName > /dev/null 2>&1
105
106# Generate a keypair for communicating with the VM
107echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}"
108ssh-keygen -f ./key -t rsa -N ''
109
110mv key key.pem
111
112# Clone BashLogin.sh and add the public key to it for later use.
113echo -e "${lBlue}Creating the pre-configuration script${NC}"
114cp BashLogin.sh bash_login.sh
115echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh
116cat key.pub >> bash_login.sh
117echo "HERE" >> bash_login.sh
118echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh
119echo "rm .bash_login" >> bash_login.sh
120echo "logout" >> bash_login.sh
121rm key.pub
122
123
124
125# Get the VM's IP address
126ctr=0
127ipAddr=""
128while [ -z "$ipAddr" ];
129do
130 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
131 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
132 sleep 3
133 if [ $ctr -eq $ipTimeout ]; then
134 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
135 exit
136 fi
137 ctr=`expr $ctr + 1`
138done
139
140echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}"
141
142# Copy the pre-config file to the VM
143echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}"
144scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login
145
146rm bash_login.sh
147
148# Run the pre-config file on the VM
149echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
150ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr
151
152# Install python which is required for ansible
153echo -e "${lBlue}Installing python${NC}"
154ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
155ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python
156
157# Make sure the VM is up-to-date
158echo -e "${lBlue}Ensure that the VM is up-to-date${NC}"
159ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
160ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y upgrade
161
162
163
164# Copy the apt repository to the VM because it's way too slow using ansible
165#echo -e "${red}NOT COPYING${lBlue} the apt-repository to the VM, ${red}TESTING ONLY REMOVE FOR PRODUCTION${NC}"
166#echo -e "${lBlue}Copy the apt-repository to the VM${NC}"
167#scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem -r apt-mirror vinstall@$ipAddr:apt-mirror
168
169# Create the docker.cfg file in the ansible tree using the VMs IP address
170echo '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
171
172# Add the voltha vm's information to the ansible tree
173echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}"
174vIpAddr=`virsh domifaddr voltha_voltha | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
175echo "[voltha]" > ansible/hosts/voltha
176echo $vIpAddr >> ansible/hosts/voltha
177echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha/libvirt/private_key" > ansible/host_vars/$vIpAddr
178
179
180# Prepare to launch the ansible playbook to configure the installer VM
181echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}"
182echo "[installer]" > ansible/hosts/installer
183echo "$ipAddr" >> ansible/hosts/installer
184echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr
185
186# Launch the ansible playbook
187echo -e "${lBlue}Launching the ansible playbook${NC}"
188ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer
189ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha