e7410da7d46edc9ccb2d158288f55e67e918caa3
[apex.git] / lib / virtual-setup-functions.sh
1 #!/usr/bin/env bash
2 ##############################################################################
3 # Copyright (c) 2015 Tim Rozet (Red Hat), Dan Radez (Red Hat) and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 ##Create virtual nodes in virsh
12 ##params: vcpus, ramsize
13 function setup_virtual_baremetal {
14   local vcpus ramsize
15   if [ -z "$1" ]; then
16     vcpus=4
17     ramsize=8192
18   elif [ -z "$2" ]; then
19     vcpus=$1
20     ramsize=8192
21   else
22     vcpus=$1
23     ramsize=$(($2*1024))
24   fi
25   #start by generating the opening json for instackenv.json
26   cat > $CONFIG/instackenv-virt.json << EOF
27 {
28   "nodes": [
29 EOF
30
31   # next create the virtual machines and add their definitions to the file
32   if [ "$ha_enabled" == "False" ]; then
33       # 1 controller + computes
34       # zero based so just pass compute count
35       vm_index=$VM_COMPUTES
36   else
37       # 3 controller + computes
38       # zero based so add 2 to compute count
39       vm_index=$((2+$VM_COMPUTES))
40   fi
41
42   for i in $(seq 0 $vm_index); do
43     if ! virsh list --all | grep baremetal${i} > /dev/null; then
44       define_vm baremetal${i} network 41 'admin_network' $vcpus $ramsize
45       for n in private_network public_network storage_network api_network; do
46         if [[ $enabled_network_list =~ $n ]]; then
47           echo -n "$n "
48           virsh attach-interface --domain baremetal${i} --type network --source $n --model virtio --config
49         fi
50       done
51     else
52       echo "Found Baremetal ${i} VM, using existing VM"
53     fi
54     #virsh vol-list default | grep baremetal${i} 2>&1> /dev/null || virsh vol-create-as default baremetal${i}.qcow2 41G --format qcow2
55     mac=$(virsh domiflist baremetal${i} | grep admin_network | awk '{ print $5 }')
56
57     if [ "$VM_COMPUTES" -gt 0 ]; then
58       capability="profile:compute"
59       VM_COMPUTES=$((VM_COMPUTES - 1))
60     else
61       capability="profile:control"
62     fi
63
64     cat >> $CONFIG/instackenv-virt.json << EOF
65     {
66       "pm_addr": "192.168.122.1",
67       "pm_user": "root",
68       "pm_password": "INSERT_STACK_USER_PRIV_KEY",
69       "pm_type": "pxe_ssh",
70       "mac": [
71         "$mac"
72       ],
73       "cpu": "$vcpus",
74       "memory": "$ramsize",
75       "disk": "41",
76       "arch": "x86_64",
77       "capabilities": "$capability"
78     },
79 EOF
80   done
81
82   #truncate the last line to remove the comma behind the bracket
83   tail -n 1 $CONFIG/instackenv-virt.json | wc -c | xargs -I {} truncate $CONFIG/instackenv-virt.json -s -{}
84
85   #finally reclose the bracket and close the instackenv.json file
86   cat >> $CONFIG/instackenv-virt.json << EOF
87     }
88   ],
89   "arch": "x86_64",
90   "host-ip": "192.168.122.1",
91   "power_manager": "nova.virt.baremetal.virtual_power_driver.VirtualPowerManager",
92   "seed-ip": "",
93   "ssh-key": "INSERT_STACK_USER_PRIV_KEY",
94   "ssh-user": "root"
95 }
96 EOF
97   #Overwrite the tripleo-inclubator domain.xml with our own, keeping a backup.
98   if [ ! -f /usr/share/tripleo/templates/domain.xml.bak ]; then
99     /usr/bin/mv -f /usr/share/tripleo/templates/domain.xml /usr/share/tripleo/templates/domain.xml.bak
100   fi
101
102   /usr/bin/cp -f $LIB/installer/domain.xml /usr/share/tripleo/templates/domain.xml
103 }
104
105 ##Create virtual nodes in virsh
106 ##params: name - String: libvirt name for VM
107 ##        bootdev - String: boot device for the VM
108 ##        disksize - Number: size of the disk in GB
109 ##        ovs_bridges: - List: list of ovs bridges
110 ##        vcpus - Number of VCPUs to use (defaults to 4)
111 ##        ramsize - Size of RAM for VM in MB (defaults to 8192)
112 function define_vm () {
113   local vcpus ramsize
114
115   if [ -z "$5" ]; then
116     vcpus=4
117     ramsize=8388608
118   elif [ -z "$6" ]; then
119     vcpus=$5
120     ramsize=8388608
121   else
122     vcpus=$5
123     ramsize=$(($6*1024))
124   fi
125
126   # Create the libvirt storage volume
127   if virsh vol-list default | grep ${1}.qcow2 2>&1> /dev/null; then
128     volume_path=$(virsh vol-path --pool default ${1}.qcow2 || echo "/var/lib/libvirt/images/${1}.qcow2")
129     echo "Volume ${1} exists. Deleting Existing Volume $volume_path"
130     virsh vol-dumpxml ${1}.qcow2 --pool default > /dev/null || echo '' #ok for this to fail
131     touch $volume_path
132     virsh vol-delete ${1}.qcow2 --pool default
133   fi
134   virsh vol-create-as default ${1}.qcow2 ${3}G --format qcow2
135   volume_path=$(virsh vol-path --pool default ${1}.qcow2)
136   if [ ! -f $volume_path ]; then
137       echo "$volume_path Not created successfully... Aborting"
138       exit 1
139   fi
140
141   # create the VM
142   /usr/libexec/openstack-tripleo/configure-vm --name $1 \
143                                               --bootdev $2 \
144                                               --image "$volume_path" \
145                                               --diskbus sata \
146                                               --arch x86_64 \
147                                               --cpus $vcpus \
148                                               --memory $ramsize \
149                                               --libvirt-nic-driver virtio \
150                                               --baremetal-interface $4
151 }