blob: a68063924ab86a95ef9dc7af70f77983d32860ae [file] [log] [blame]
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -04001#!/bin/bash
Zack Williams41513bf2018-07-07 20:08:35 -07002# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040015
Sergio Slobodrian7c483622017-06-13 15:51:34 -040016
Sergio Slobodrianc5477712017-06-07 11:56:56 -040017iVmName="vInstaller"
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -040018vVmName="voltha_voltha"
Sergio Slobodrian7c483622017-06-13 15:51:34 -040019baseImage="Ubuntu1604LTS"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040020iVmNetwork="vagrant-libvirt"
Sergio Slobodrianc5477712017-06-07 11:56:56 -040021installerArchive="installer.tar.bz2"
22installerDirectory="volthaInstaller"
23installerPart="installer.part"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040024shutdownTimeout=5
25ipTimeout=10
26
Sergio Slobodrianf74fa072017-06-28 09:33:24 -040027# Command line argument variables
28testMode="no"
Sergio Slobodriancab0a392017-07-13 08:42:10 -040029rebuildVoltha="no"
Stephane Barbarie2cbffca2018-03-26 16:20:03 -040030useKubernetes="no"
Sergio Slobodrianf74fa072017-06-28 09:33:24 -040031
32
33
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040034lBlue='\033[1;34m'
35green='\033[0;32m'
36orange='\033[0;33m'
37NC='\033[0m'
38red='\033[0;31m'
39yellow='\033[1;33m'
40dGrey='\033[1;30m'
41lGrey='\033[1;37m'
42lCyan='\033[1;36m'
43
Sergio Slobodrian7c483622017-06-13 15:51:34 -040044uId=`id -u`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040045wd=`pwd`
46
Sergio Slobodrianf74fa072017-06-28 09:33:24 -040047parse_args()
48{
49 for i in $@
50 do
51 case "$i" in
52 "test" )
53 testMode="yes"
54 echo -e "${lBlue}Test mode is ${green}enabled${NC}"
55 ;;
Sergio Slobodriancab0a392017-07-13 08:42:10 -040056 "rebuild" )
57 rebuildVoltha="yes"
58 echo -e "${lBlue}Voltha rebuild is ${green}enabled${NC}"
59 ;;
Stephane Barbarie2cbffca2018-03-26 16:20:03 -040060 "k8s" )
61 useKubernetes="yes"
62 echo -e "${lBlue}Kubernetes framework is ${green}enabled${NC}"
63 ;;
Sergio Slobodrianf74fa072017-06-28 09:33:24 -040064 esac
65 done
66}
67
68
69######################################
70# MAIN MAIN MAIN MAIN MAIN MAIN MAIN #
71######################################
72parse_args $@
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040073# Validate that vagrant is installed.
74echo -e "${lBlue}Ensure that ${lCyan}vagrant${lBlue} is installed${NC}"
75vInst=`which vagrant`
76
77if [ -z "$vInst" ]; then
78 wget https://releases.hashicorp.com/vagrant/1.9.5/vagrant_1.9.5_x86_64.deb
79 sudo dpkg -i vagrant_1.8.5_x86_64.deb
80 rm vagrant_1.8.5_x86_64.deb
81fi
82unset vInst
83
84# Validate that ansible is installed
85echo -e "${lBlue}Ensure that ${lCyan}ansible${lBlue} is installed${NC}"
86aInst=`which ansible`
87
88if [ -z "$aInst" ]; then
Sergio Slobodrianc5477712017-06-07 11:56:56 -040089 sudo apt-get install -y software-properties-common
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040090 sudo apt-add-repository ppa:ansible/ansible
91 sudo apt-get update
Sergio Slobodrianc5477712017-06-07 11:56:56 -040092 sudo apt-get install -y ansible
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -040093fi
94unset vInst
95
Sergio Slobodrian61287792017-06-27 12:14:05 -040096# Verify if this is intended to be a test environment, if so
97# configure the 3 VMs which will be started later to emulate
98# the production installation cluster.
Sergio Slobodrianf74fa072017-06-28 09:33:24 -040099if [ "$testMode" == "yes" ]; then
100 echo -e "${lBlue}Test mode ${green}enabled${lBlue}, configure the ${lCyan}ha-serv${lBlue} VMs${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400101 # Update the vagrant settings file
102 sed -i -e '/server_name/s/.*/server_name: "ha-serv'${uId}'-"/' settings.vagrant.yaml
103 sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall'${uId}':5000"/' ansible/group_vars/all
104 sed -i -e "/vinstall/s/vinstall/vinstall${uId}/" ../ansible/roles/docker/templates/daemon.json
105
106 # Set the insecure registry configuration based on the installer hostname
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400107 echo -e "${lBlue}Set up the insecure registry config for hostname ${lCyan}vinstall${uId}${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400108 echo '{' > ansible/roles/voltha/templates/daemon.json
109 echo '"insecure-registries" : ["vinstall'${uId}':5000"]' >> ansible/roles/voltha/templates/daemon.json
110 echo '}' >> ansible/roles/voltha/templates/daemon.json
111
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400112 # Change the installer name
113 iVmName="vInstaller${uId}"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400114else
115 rm -fr .test
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400116 # Clean out the install config file keeping only the commented lines
117 # which serve as documentation.
118 sed -i -e '/^#/!d' install.cfg
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400119 # Set the insecure registry configuration based on the installer hostname
Sergio Slobodrian497e6e82017-07-18 15:10:38 -0400120 echo -e "${lBlue}Set up the inescure registry config for hostname ${lCyan}vinstall${NC}"
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400121 sed -i -e '/docker_push_registry/s/.*/docker_push_registry: "vinstall:5000"/' ansible/group_vars/all
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400122 echo '{' > ansible/roles/voltha/templates/daemon.json
123 echo '"insecure-registries" : ["vinstall:5000"]' >> ansible/roles/voltha/templates/daemon.json
124 echo '}' >> ansible/roles/voltha/templates/daemon.json
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400125fi
126
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400127# Check to make sure that the vagrant-libvirt network is both defined and started
128echo -e "${lBlue}Verify tha the ${lCyan}vagrant-libvirt${lBlue} network is defined and started${NC}"
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400129virsh net-list --all | grep "vagrant-libvirt" > /dev/null
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400130rtrn=$?
131if [ $rtrn -eq 1 ]; then
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400132 # Not defined
133 echo -e "${lBlue}Defining the ${lCyan}vagrant-libvirt${lBlue} network${NC}"
134 virsh net-define vagrant-libvirt.xml
135 echo -e "${lBlue}Starting the ${lCyan}vagrant-libvirt${lBlue} network${NC}"
136 virsh net-start vagrant-libvirt
137else
138 virsh net-list | grep "vagrant-libvirt" > /dev/null
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400139 rtrn=$?
140 if [ $rtrn -eq 1 ]; then
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400141 # Defined but not started
142 echo -e "${lBlue}Starting the ${lCyan}vagrant-libvirt${lBlue} network${NC}"
143 virsh net-start vagrant-libvirt
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400144
145 else
146 # Defined and running
147 echo -e "${lBlue}The ${lCyan}vagrant-libvirt${lBlue} network is ${green} running${NC}"
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400148 fi
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400149fi
150
151# Check that the default storage pool exists and create it if it doesn't
152virsh pool-list --all | grep default > /dev/null
153rtrn=$?
154if [ $rtrn -eq 1 ]; then
155 # Not defined
156 echo -e "${lBlue}Defining the ${lCyan}defaul${lBlue} storage pool${NC}"
157 virsh pool-define-as --name default --type dir --target /var/lib/libvirt/images/
158 virsh pool-autostart default
159 echo -e "${lBlue}Starting the ${lCyan}defaul${lBlue} storage pool${NC}"
160 virsh pool-start default
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400161else
Sergio Slobodrian7c5e8852017-07-31 20:17:14 -0400162 virsh pool-list | grep default > /dev/null
163 rtrn=$?
164 if [ $rtrn -eq 1 ]; then
165 # Defined but not started
166 echo -e "${lBlue}Starting the ${lCyan}defaul${lBlue} storage pool${NC}"
167 virsh pool-start default
168 else
169 # Defined and running
170 echo -e "${lBlue}The ${lCyan}default${lBlue} storage pool ${green} running${NC}"
171 fi
Sergio Slobodrian73e311a2017-07-26 11:12:45 -0400172fi
173
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400174
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400175# Shut down the domain in case it's running.
176echo -e "${lBlue}Shut down the ${lCyan}$iVmName${lBlue} VM if running${NC}"
177ctr=0
178vStat=`virsh list | grep $iVmName`
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400179virsh shutdown $iVmName
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400180while [ ! -z "$vStat" ];
181do
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400182 echo "Waiting for $iVmName to shut down"
183 sleep 2
Sergio Slobodrian497e6e82017-07-18 15:10:38 -0400184 vStat=`virsh list | grep "$iVmName "`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400185 ctr=`expr $ctr + 1`
186 if [ $ctr -eq $shutdownTimeout ]; then
187 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
188 virsh destroy $iVmName
Sergio Slobodrian497e6e82017-07-18 15:10:38 -0400189 vStat=`virsh list | grep "$iVmName "`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400190 fi
191done
192
193
194# Delete the VM and ignore any errors should they occur
195echo -e "${lBlue}Undefining the ${lCyan}$iVmName${lBlue} domain${NC}"
196virsh undefine $iVmName
197
198# Remove the associated volume
199echo -e "${lBlue}Removing the ${lCyan}$iVmName.qcow2${lBlue} volume${NC}"
200virsh vol-delete "${iVmName}.qcow2" default
201
202# Clone the base vanilla ubuntu install
203echo -e "${lBlue}Cloning the ${lCyan}$baseImage.qcow2${lBlue} to ${lCyan}$iVmName.qcow2${NC}"
204virsh vol-clone "${baseImage}.qcow2" "${iVmName}.qcow2" default
205
206# Create the xml file and define the VM for virsh
207echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400208cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400209
210virsh define tmp.xml
211
212rm tmp.xml
213
214# Start the VMm, if it's already running just ignore the error
215echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
216virsh start $iVmName > /dev/null 2>&1
217
218# Generate a keypair for communicating with the VM
219echo -e "${lBlue}Generating the key-pair for communication with the VM${NC}"
220ssh-keygen -f ./key -t rsa -N ''
221
222mv key key.pem
223
224# Clone BashLogin.sh and add the public key to it for later use.
225echo -e "${lBlue}Creating the pre-configuration script${NC}"
226cp BashLogin.sh bash_login.sh
227echo "cat <<HERE > .ssh/authorized_keys" >> bash_login.sh
228cat key.pub >> bash_login.sh
229echo "HERE" >> bash_login.sh
230echo "chmod 400 .ssh/authorized_keys" >> bash_login.sh
231echo "rm .bash_login" >> bash_login.sh
232echo "logout" >> bash_login.sh
233rm key.pub
234
235
236
237# Get the VM's IP address
238ctr=0
239ipAddr=""
240while [ -z "$ipAddr" ];
241do
242 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
243 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
244 sleep 3
245 if [ $ctr -eq $ipTimeout ]; then
246 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
247 exit
248 fi
249 ctr=`expr $ctr + 1`
250done
251
252echo -e "${lBlue}The IP address is: ${lCyan}$ipAddr${NC}"
253
254# Copy the pre-config file to the VM
255echo -e "${lBlue}Transfering pre-configuration script to the VM${NC}"
256scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no bash_login.sh vinstall@$ipAddr:.bash_login
257
258rm bash_login.sh
259
260# Run the pre-config file on the VM
261echo -e "${lBlue}Running the pre-configuration script on the VM${NC}"
262ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no vinstall@$ipAddr
263
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400264# If we're in test mode, change the hostname of the installer vm
Sergio Slobodrian61287792017-06-27 12:14:05 -0400265# also start the 3 vagrant target VMs
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400266if [ "$testMode" == "yes" ]; then
267 echo -e "${lBlue}Test mode, change the installer host name to ${lCyan}vinstall${uId}${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400268 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
269 sudo hostnamectl set-hostname vinstall${uId}
270 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr \
271 sudo service networking restart
Sergio Slobodrian61287792017-06-27 12:14:05 -0400272
273 echo -e "${lBlue}Testing, start the ${lCyan}ha-serv${lBlue} VMs${NC}"
274 vagrant destroy ha-serv${uId}-{1,2,3}
275 vagrant up ha-serv${uId}-{1,2,3}
276 ./devSetHostList.sh
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400277
278 if [ "$useKubernetes" == "yes" ]; then
279 ./devSetKubernetes.sh
280 fi
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400281fi
282
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400283# Ensure that the voltha VM is running so that images can be secured
284echo -e "${lBlue}Ensure that the ${lCyan}voltha VM${lBlue} is running${NC}"
Sergio Slobodriancab0a392017-07-13 08:42:10 -0400285vVm=`virsh list | grep "voltha_voltha${uId}"`
286#echo "vVm: $vVm"
287#echo "rebuildVoltha: $rebuildVoltha"
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400288
Sergio Slobodriancab0a392017-07-13 08:42:10 -0400289
290if [ -z "$vVm" -o "$rebuildVoltha" == "yes" ]; then
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400291 if [ "$testMode" == "yes" ]; then
Sergio Slobodrian92136d02017-08-22 21:48:42 -0400292 ./BuildVoltha.sh "test"
Sergio Slobodrian61287792017-06-27 12:14:05 -0400293 rtrn=$?
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400294 else
295 # Default to installer mode
Sergio Slobodrian92136d02017-08-22 21:48:42 -0400296 ./BuildVoltha.sh "install"
Sergio Slobodrian61287792017-06-27 12:14:05 -0400297 rtrn=$?
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400298 fi
299 if [ $rtrn -ne 0 ]; then
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400300 echo -e "${red}Voltha build failed!! ${lCyan}Please review the log and correct${lBlue} is running${NC}"
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400301 exit 1
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400302 fi
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400303
304 if [ "$useKubernetes" == "yes" ]; then
305 # Load required k8s libraries on the voltha instance
306 ./preloadKubernetes.sh
307 fi
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400308fi
309
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400310# Extract all the image names and tags from the running voltha VM
Sergio Slobodrian61287792017-06-27 12:14:05 -0400311# when running in test mode. This will provide the entire suite
312# of available containers to the VM cluster.
313
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400314if [ "$testMode" == "yes" ]; then
Sergio Slobodrian61287792017-06-27 12:14:05 -0400315 echo -e "${lBlue}Extracting the docker image list from the voltha VM${NC}"
316 volIpAddr=`virsh domifaddr $vVmName${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
317 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i ../.vagrant/machines/voltha${uId}/libvirt/private_key vagrant@$volIpAddr "docker image ls" > images.tmp
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400318 # Construct list of images; exclude all entries that point to the registry
319 cat images.tmp | grep -v :5000 | tail -n +2 | awk '{printf(" - %s:%s\n", $1, $2)}' | grep -v "<none>" > image-list.cfg
Sergio Slobodrian61287792017-06-27 12:14:05 -0400320 rm -f images.tmp
321 sed -i -e '/voltha_containers:/,$d' ansible/group_vars/all
322 echo "voltha_containers:" >> ansible/group_vars/all
323 cat image-list.cfg >> ansible/group_vars/all
324 rm -f image-list.cfg
Sergio Slobodrian6e270c12017-08-09 23:06:49 -0400325 echo -e "${lBlue}Gussing at the cord home directory for ${yellow}`whoami`${NC}"
326 sed -i -e "/cord_home:/s#.*#cord_home: `pwd | sed -e 's~/incubator/voltha/install~~'`#" ansible/group_vars/all
Sergio Slobodrian61287792017-06-27 12:14:05 -0400327else
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400328 echo -e "${lBlue}Set up the docker image list from ${lCyan}containers.cfg${NC}"
Sergio Slobodrian61287792017-06-27 12:14:05 -0400329 sed -i -e '/voltha_containers:/,$d' ansible/group_vars/all
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400330
331 if [ "$useKubernetes" == "yes" ]; then
332 cat containers.cfg.k8s >> ansible/group_vars/all
333 else
334 cat containers.cfg >> ansible/group_vars/all
335 fi
Sergio Slobodrian61287792017-06-27 12:14:05 -0400336fi
Sergio Slobodrianba9cbd82017-06-22 11:45:49 -0400337
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400338
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400339# Install python which is required for ansible
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400340echo -e "${lBlue}Installing ${lCyan}Python${NC}"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400341ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get update
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400342ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo apt-get -y install python python-netaddr
Sergio Slobodrian9d9c8442017-07-25 07:55:42 -0400343
344# Move all the python deb files to their own directory so they can be installed first
345echo -e "${lBlue}Caching ${lCyan}Python${lBlue} install${NC}"
346ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr mkdir python-deb
347ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo mv /var/cache/apt/archives/*.deb /home/vinstall/python-deb"
348ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo chown -R vinstall.vinstall /home/vinstall/python-deb"
349
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400350if [ "$useKubernetes" == "yes" ]; then
351 echo -e "${lBlue}Cloning ${lCyan}Kubespray${lBlue} repository${NC}"
Stephane Barbarie78d9fa62018-04-19 14:11:05 -0400352 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "git clone --branch v2.5.0 https://github.com/kubernetes-incubator/kubespray.git /home/vinstall/kubespray"
Stephane Barbarie2cbffca2018-03-26 16:20:03 -0400353 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr "sudo chown -R vinstall.vinstall /home/vinstall/kubespray"
354fi
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400355
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400356# Create the docker.cfg file in the ansible tree using the VMs IP address
357echo '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
358
359# Add the voltha vm's information to the ansible tree
360echo -e "${lBlue}Add the voltha vm and key to the ansible accessible hosts${NC}"
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400361vIpAddr=`virsh domifaddr voltha_voltha${uId} | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400362echo "[voltha]" > ansible/hosts/voltha
363echo $vIpAddr >> ansible/hosts/voltha
Sergio Slobodrian7c483622017-06-13 15:51:34 -0400364echo "ansible_ssh_private_key_file: $wd/../.vagrant/machines/voltha${uId}/libvirt/private_key" > ansible/host_vars/$vIpAddr
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400365
366
367# Prepare to launch the ansible playbook to configure the installer VM
368echo -e "${lBlue}Prepare to launch the ansible playbook to configure the VM${NC}"
369echo "[installer]" > ansible/hosts/installer
370echo "$ipAddr" >> ansible/hosts/installer
371echo "ansible_ssh_private_key_file: $wd/key.pem" > ansible/host_vars/$ipAddr
372
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400373# Launch the ansible playbooks
374
375echo -e "${lBlue}Launching the ${lCyan}volthainstall${lBlue} ansible playbook on the installer vm${NC}"
Sergio Slobodrianee4b2bc2017-06-05 10:08:59 -0400376ansible-playbook ansible/volthainstall.yml -i ansible/hosts/installer
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400377rtrn=$?
378if [ $rtrn -ne 0 ]; then
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400379 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
380 exit
381fi
382
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400383
384echo -e "${lBlue}Launching the ${lCyan}volthainstall${lBlue} ansible playbook on the voltha vm${NC}"
385ansible-playbook ansible/volthainstall.yml -i ansible/hosts/voltha
386rtrn=$?
387if [ $rtrn -ne 0 ]; then
388 echo -e "${red}PLAYBOOK FAILED, Exiting${NC}"
389 exit
390fi
391
392if [ "$testMode" == "yes" ]; then
Sergio Slobodriand24189e2017-06-10 23:27:15 -0400393 echo -e "${lBlue}Testing, the install image ${red}WILL NOT${lBlue} be built${NC}"
Sergio Slobodrian61287792017-06-27 12:14:05 -0400394
395
396 # Reboot the installer
397 echo -e "${lBlue}Rebooting the installer${NC}"
398 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr sudo telinit 6
399 # Wait for the host to shut down
400 sleep 5
401
402 ctr=0
403 ipAddr=""
404 while [ -z "$ipAddr" ];
405 do
406 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
407 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
408 sleep 3
409 if [ $ctr -eq $ipTimeout ]; then
410 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
411 exit
412 fi
413 ctr=`expr $ctr + 1`
414 done
415
416 echo -e "${lBlue}Running the installer${NC}"
417 echo "~/installer.sh" > tmp_bash_login
418 echo "rm ~/.bash_login" >> tmp_bash_login
419 echo "logout" >> tmp_bash_login
420 scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem tmp_bash_login vinstall@$ipAddr:.bash_login
421 rm -f tmp_bash_login
422 ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr
423
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400424else
425 echo -e "${lBlue}Building, the install image (this can take a while)${NC}"
426 # Create a temporary directory for all the installer files
427 mkdir tmp_installer
428 cp vmTemplate.xml tmp_installer
429 # Shut down the installer vm
430 ctr=0
431 vStat=`virsh list | grep $iVmName`
432 virsh shutdown $iVmName
433 while [ ! -z "$vStat" ];
434 do
435 echo "Waiting for $iVmName to shut down"
436 sleep 2
Sergio Slobodrian497e6e82017-07-18 15:10:38 -0400437 vStat=`virsh list | grep "$iVmName "`
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400438 ctr=`expr $ctr + 1`
439 if [ $ctr -eq $shutdownTimeout ]; then
440 echo -e "${red}Tired of waiting, forcing the VM off${NC}"
441 virsh destroy $iVmName
Sergio Slobodrian497e6e82017-07-18 15:10:38 -0400442 vStat=`virsh list | grep "$iVmName "`
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400443 fi
444 done
445 # Copy the install bootstrap script to the installer directory
446 cp BootstrapInstaller.sh tmp_installer
447 # Copy the private key to access the VM
448 cp key.pem tmp_installer
449 pushd tmp_installer > /dev/null 2>&1
450 # Copy the vm image to the installer directory
451 virsh vol-dumpxml $iVmName.qcow2 default | sed -e 's/<key.*key>//' | sed -e '/^[ ]*$/d' > ${iVmName}_volume.xml
452 virsh pool-create-as installer --type dir --target `pwd`
453 virsh vol-create-from installer ${iVmName}_volume.xml $iVmName.qcow2 --inputpool default
454 virsh pool-destroy installer
455 # The image is copied in as root. It needs to have ownership changed
456 # this will result in a password prompt.
457 sudo chown `whoami`.`whoami` $iVmName.qcow2
458 # Now create the installer tar file
459 tar cjf ../$installerArchive .
460 popd > /dev/null 2>&1
461 # Clean up
462 rm -fr tmp_installer
463 # Final location for the installer
464 rm -fr $installerDirectory
465 mkdir $installerDirectory
Sergio Slobodrian36e16552017-06-19 11:00:45 -0400466 cp deployInstaller.sh $installerDirectory
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400467 # Check the image size and determine if it needs to be split.
468 # To be safe, split the image into chunks smaller than 2G so that
469 # it will fit on a FAT32 volume.
470 fSize=`ls -l $installerArchive | awk '{print $5'}`
471 if [ $fSize -gt 2000000000 ]; then
472 echo -e "${lBlue}Installer file too large, breaking into parts${NC}"
473 # The file is too large, breaking it up into parts
474 sPos=0
475 fnn="00"
476 while dd if=$installerArchive of=${installerDirectory}/${installerPart}$fnn \
477 bs=1900MB count=1 skip=$sPos > /dev/null 2>&1
478 do
479 sPos=`expr $sPos + 1`
480 if [ ! -s ${installerDirectory}/${installerPart}$fnn ]; then
481 rm -f ${installerDirectory}/${installerPart}$fnn
482 break
483 fi
484 if [ $sPos -lt 10 ]; then
485 fnn="0$sPos"
486 else
487 fnn="$sPos"
488 fi
489 done
490 else
491 cp $installerArchive $installerDirectory
492 fi
493 # Clean up
494 rm $installerArchive
Sergio Slobodrianf74fa072017-06-28 09:33:24 -0400495 echo -e "${lBlue}The install image is built and can be found in ${lCyan}$installerDirectory${NC}"
496 echo -e "${lBlue}Copy all the files in ${lCyan}$installerDirectory${lBlue} to the traasnport media${NC}"
Sergio Slobodrianc5477712017-06-07 11:56:56 -0400497fi