migrating to proposed common network settings file
[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 yaml for the inventory-virt.yaml file
26   cat > $APEX_TMP_DIR/inventory-virt.yaml << EOF
27 nodes:
28 EOF
29
30   # next create the virtual machines and add their definitions to the file
31   if [ "$ha_enabled" == "False" ]; then
32       controller_index=0
33   else
34       controller_index=2
35       # 3 controller + computes
36       # zero based so add 2 to compute count
37       if [ $VM_COMPUTES -lt 2 ]; then
38           VM_COMPUTES=2
39       fi
40   fi
41
42   for i in $(seq 0 $(($controller_index+$VM_COMPUTES))); do
43       if [ $i -gt $controller_index ]; then
44       capability="profile:compute"
45     else
46       capability="profile:control"
47       if [[ "${deploy_options_array['sdn_controller']}" == 'opendaylight' && "$ramsize" -lt 10240 ]]; then
48          echo "WARN: RAM per controller too low.  OpenDaylight specified in HA deployment requires at least 10GB"
49          echo "INFO: Increasing RAM per controller to 10GB"
50          ramsize=10240
51       fi
52     fi
53     if ! virsh list --all | grep baremetal${i} > /dev/null; then
54       define_vm baremetal${i} network 41 'admin' $vcpus $ramsize
55       for n in tenant external storage api; do
56         if [[ $enabled_network_list =~ $n ]]; then
57           echo -n "$n "
58           virsh attach-interface --domain baremetal${i} --type network --source $n --model virtio --config
59         fi
60       done
61     else
62       echo "Found baremetal${i} VM, using existing VM"
63     fi
64     #virsh vol-list default | grep baremetal${i} 2>&1> /dev/null || virsh vol-create-as default baremetal${i}.qcow2 41G --format qcow2
65     mac=$(virsh domiflist baremetal${i} | grep admin | awk '{ print $5 }')
66
67     cat >> $APEX_TMP_DIR/inventory-virt.yaml << EOF
68   node${i}:
69     mac_address: "$mac"
70     ipmi_ip: 192.168.122.1
71     ipmi_user: root
72     ipmi_pass: "INSERT_STACK_USER_PRIV_KEY"
73     pm_type: "pxe_ssh"
74     cpus: $vcpus
75     memory: $ramsize
76     disk: 41
77     arch: "x86_64"
78     capabilities: "$capability"
79 EOF
80   done
81
82   #Overwrite the tripleo-inclubator domain.xml with our own, keeping a backup.
83   if [ ! -f /usr/share/tripleo/templates/domain.xml.bak ]; then
84     /usr/bin/mv -f /usr/share/tripleo/templates/domain.xml /usr/share/tripleo/templates/domain.xml.bak
85   fi
86
87   /usr/bin/cp -f $LIB/installer/domain.xml /usr/share/tripleo/templates/domain.xml
88 }
89
90 ##Create virtual nodes in virsh
91 ##params: name - String: libvirt name for VM
92 ##        bootdev - String: boot device for the VM
93 ##        disksize - Number: size of the disk in GB
94 ##        ovs_bridges: - List: list of ovs bridges
95 ##        vcpus - Number of VCPUs to use (defaults to 4)
96 ##        ramsize - Size of RAM for VM in MB (defaults to 8192)
97 function define_vm () {
98   local vcpus ramsize
99
100   if [ -z "$5" ]; then
101     vcpus=4
102     ramsize=8388608
103   elif [ -z "$6" ]; then
104     vcpus=$5
105     ramsize=8388608
106   else
107     vcpus=$5
108     ramsize=$(($6*1024))
109   fi
110
111   # Create the libvirt storage volume
112   if virsh vol-list default | grep ${1}.qcow2 2>&1> /dev/null; then
113     volume_path=$(virsh vol-path --pool default ${1}.qcow2 || echo "/var/lib/libvirt/images/${1}.qcow2")
114     echo "Volume ${1} exists. Deleting Existing Volume $volume_path"
115     virsh vol-dumpxml ${1}.qcow2 --pool default > /dev/null || echo '' #ok for this to fail
116     touch $volume_path
117     virsh vol-delete ${1}.qcow2 --pool default
118   fi
119   virsh vol-create-as default ${1}.qcow2 ${3}G --format qcow2
120   volume_path=$(virsh vol-path --pool default ${1}.qcow2)
121   if [ ! -f $volume_path ]; then
122       echo "$volume_path Not created successfully... Aborting"
123       exit 1
124   fi
125
126   # create the VM
127   /usr/libexec/openstack-tripleo/configure-vm --name $1 \
128                                               --bootdev $2 \
129                                               --image "$volume_path" \
130                                               --diskbus sata \
131                                               --arch x86_64 \
132                                               --cpus $vcpus \
133                                               --memory $ramsize \
134                                               --libvirt-nic-driver virtio \
135                                               --baremetal-interface $4
136 }