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