blob: c140ab7a8e2db37749b45a8215da88bc8a1dedf4 [file] [log] [blame]
Sergio Slobodrianc5477712017-06-07 11:56:56 -04001#!/bin/bash
2
3baseImage="Ubuntu1604LTS"
4iVmName="vInstaller"
5iVmNetwork="default"
6shutdownTimeout=5
7ipTimeout=20
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# Update the XML file with the VM information
21echo -e "${lBlue}Defining the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
22cat vmTemplate.xml | sed -e "s/{{ VMName }}/$iVmName/g" | sed -e "s/{{ VMNetwork }}/$iVmNetwork/g" > tmp.xml
23
24# Copy the vm image to the default storage pool
25echo -e "${lBlue}Creating the storage for the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
26# Copy the vm image to the installer directory
27virsh pool-create-as installer --type dir --target `pwd`
28virsh vol-create-from default ${iVmName}_volume.xml $iVmName.qcow2 --inputpool installer
29virsh pool-destroy installer
30
31# Create the VM using the updated xml file and the uploaded image
32virsh define tmp.xml
33
34rm tmp.xml
35
36# Start the VMm, if it's already running just ignore the error
37echo -e "${lBlue}Starting the ${lCyan}$iVmName${lBlue} virtual machine${NC}"
38virsh start $iVmName > /dev/null 2>&1
39
40# Get the VM's IP address
41ctr=0
42ipAddr=""
43while [ -z "$ipAddr" ];
44do
45 echo -e "${lBlue}Waiting for the VM's IP address${NC}"
46 ipAddr=`virsh domifaddr $iVmName | tail -n +3 | awk '{ print $4 }' | sed -e 's~/.*~~'`
47 sleep 3
48 if [ $ctr -eq $ipTimeout ]; then
49 echo -e "${red}Tired of waiting, please adjust the ipTimeout if the VM is slow to start${NC}"
50 exit
51 fi
52 ctr=`expr $ctr + 1`
53done
54
55# Log into the vm
56ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i key.pem vinstall@$ipAddr