Build Congress RPM package
[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 [ "$VM_COMPUTES" -gt 0 ]; then
44       capability="profile:compute"
45       VM_COMPUTES=$((VM_COMPUTES - 1))
46     else
47       capability="profile:control"
48       if [[ "${deploy_options_array['sdn_controller']}" == 'opendaylight' && "$ha_enabled" == "True" && "$ramsize" -lt 10240 ]]; then
49          echo "WARN: RAM per controller too low.  OpenDaylight specified in HA deployment requires at least 10GB"
50          echo "INFO: Increasing RAM per controller to 10GB"
51          ramsize=10240
52       fi
53     fi
54     if ! virsh list --all | grep baremetal${i} > /dev/null; then
55       define_vm baremetal${i} network 41 'admin_network' $vcpus $ramsize
56       for n in private_network public_network storage_network api_network; do
57         if [[ $enabled_network_list =~ $n ]]; then
58           echo -n "$n "
59           virsh attach-interface --domain baremetal${i} --type network --source $n --model virtio --config
60         fi
61       done
62     else
63       echo "Found Baremetal ${i} VM, using existing VM"
64     fi
65     #virsh vol-list default | grep baremetal${i} 2>&1> /dev/null || virsh vol-create-as default baremetal${i}.qcow2 41G --format qcow2
66     mac=$(virsh domiflist baremetal${i} | grep admin_network | awk '{ print $5 }')
67
68     cat >> $CONFIG/instackenv-virt.json << EOF
69     {
70       "pm_addr": "192.168.122.1",
71       "pm_user": "root",
72       "pm_password": "INSERT_STACK_USER_PRIV_KEY",
73       "pm_type": "pxe_ssh",
74       "mac": [
75         "$mac"
76       ],
77       "cpu": "$vcpus",
78       "memory": "$ramsize",
79       "disk": "41",
80       "arch": "x86_64",
81       "capabilities": "$capability"
82     },
83 EOF
84   done
85
86   #truncate the last line to remove the comma behind the bracket
87   tail -n 1 $CONFIG/instackenv-virt.json | wc -c | xargs -I {} truncate $CONFIG/instackenv-virt.json -s -{}
88
89   #finally reclose the bracket and close the instackenv.json file
90   cat >> $CONFIG/instackenv-virt.json << EOF
91     }
92   ],
93   "arch": "x86_64",
94   "host-ip": "192.168.122.1",
95   "power_manager": "nova.virt.baremetal.virtual_power_driver.VirtualPowerManager",
96   "seed-ip": "",
97   "ssh-key": "INSERT_STACK_USER_PRIV_KEY",
98   "ssh-user": "root"
99 }
100 EOF
101   #Overwrite the tripleo-inclubator domain.xml with our own, keeping a backup.
102   if [ ! -f /usr/share/tripleo/templates/domain.xml.bak ]; then
103     /usr/bin/mv -f /usr/share/tripleo/templates/domain.xml /usr/share/tripleo/templates/domain.xml.bak
104   fi
105
106   /usr/bin/cp -f $LIB/installer/domain.xml /usr/share/tripleo/templates/domain.xml
107 }
108
109 ##Create virtual nodes in virsh
110 ##params: name - String: libvirt name for VM
111 ##        bootdev - String: boot device for the VM
112 ##        disksize - Number: size of the disk in GB
113 ##        ovs_bridges: - List: list of ovs bridges
114 ##        vcpus - Number of VCPUs to use (defaults to 4)
115 ##        ramsize - Size of RAM for VM in MB (defaults to 8192)
116 function define_vm () {
117   local vcpus ramsize
118
119   if [ -z "$5" ]; then
120     vcpus=4
121     ramsize=8388608
122   elif [ -z "$6" ]; then
123     vcpus=$5
124     ramsize=8388608
125   else
126     vcpus=$5
127     ramsize=$(($6*1024))
128   fi
129
130   # Create the libvirt storage volume
131   if virsh vol-list default | grep ${1}.qcow2 2>&1> /dev/null; then
132     volume_path=$(virsh vol-path --pool default ${1}.qcow2 || echo "/var/lib/libvirt/images/${1}.qcow2")
133     echo "Volume ${1} exists. Deleting Existing Volume $volume_path"
134     virsh vol-dumpxml ${1}.qcow2 --pool default > /dev/null || echo '' #ok for this to fail
135     touch $volume_path
136     virsh vol-delete ${1}.qcow2 --pool default
137   fi
138   virsh vol-create-as default ${1}.qcow2 ${3}G --format qcow2
139   volume_path=$(virsh vol-path --pool default ${1}.qcow2)
140   if [ ! -f $volume_path ]; then
141       echo "$volume_path Not created successfully... Aborting"
142       exit 1
143   fi
144
145   # create the VM
146   /usr/libexec/openstack-tripleo/configure-vm --name $1 \
147                                               --bootdev $2 \
148                                               --image "$volume_path" \
149                                               --diskbus sata \
150                                               --arch x86_64 \
151                                               --cpus $vcpus \
152                                               --memory $ramsize \
153                                               --libvirt-nic-driver virtio \
154                                               --baremetal-interface $4
155 }