Bumps OVS version to 2.8 for OVN
[apex.git] / lib / undercloud-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 ##verify vm exists, an has a dhcp lease assigned to it
12 ##params: none
13 function setup_undercloud_vm {
14   local libvirt_imgs=/var/lib/libvirt/images
15   if ! virsh list --all | grep undercloud > /dev/null; then
16       undercloud_nets="default admin"
17       if [[ $enabled_network_list =~ "external" ]]; then
18         undercloud_nets+=" external"
19       fi
20       define_vm undercloud hd 30 "$undercloud_nets" 4 12288
21
22       ### this doesn't work for some reason I was getting hangup events so using cp instead
23       #virsh vol-upload --pool default --vol undercloud.qcow2 --file $BASE/stack/undercloud.qcow2
24       #2015-12-05 12:57:20.569+0000: 8755: info : libvirt version: 1.2.8, package: 16.el7_1.5 (CentOS BuildSystem <http://bugs.centos.org>, 2015-11-03-13:56:46, worker1.bsys.centos.org)
25       #2015-12-05 12:57:20.569+0000: 8755: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
26       #2015-12-05 12:57:20.569+0000: 8756: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
27       #error: cannot close volume undercloud.qcow2
28       #error: internal error: received hangup / error event on socket
29       #error: Reconnected to the hypervisor
30
31       cp -f $IMAGES/undercloud.qcow2 $libvirt_imgs/undercloud.qcow2
32       cp -f $IMAGES/overcloud-full.vmlinuz $libvirt_imgs/overcloud-full.vmlinuz
33       cp -f $IMAGES/overcloud-full.initrd $libvirt_imgs/overcloud-full.initrd
34
35       # resize Undercloud machine
36       echo "Checking if Undercloud needs to be resized..."
37       undercloud_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a $libvirt_imgs/undercloud.qcow2 |grep device | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
38       if [ "$undercloud_size" -lt 30 ]; then
39         qemu-img resize /var/lib/libvirt/images/undercloud.qcow2 +25G
40         LIBGUESTFS_BACKEND=direct virt-resize --expand /dev/sda1 $IMAGES/undercloud.qcow2 $libvirt_imgs/undercloud.qcow2
41         LIBGUESTFS_BACKEND=direct virt-customize -a $libvirt_imgs/undercloud.qcow2 --run-command 'xfs_growfs -d /dev/sda1 || true'
42         new_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a $libvirt_imgs/undercloud.qcow2 |grep filesystem | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
43         if [ "$new_size" -lt 30 ]; then
44           echo "Error resizing Undercloud machine, disk size is ${new_size}"
45           exit 1
46         else
47           echo "Undercloud successfully resized"
48         fi
49       else
50         echo "Skipped Undercloud resize, upstream is large enough"
51       fi
52
53   else
54       echo "Found existing Undercloud VM, exiting."
55       exit 1
56   fi
57
58   # if the VM is not running update the authkeys and start it
59   if ! virsh list | grep undercloud > /dev/null; then
60     if [ "$debug" == 'TRUE' ]; then
61       LIBGUESTFS_BACKEND=direct virt-customize -a $libvirt_imgs/undercloud.qcow2 --root-password password:opnfvapex
62     fi
63
64     echo "Injecting ssh key to Undercloud VM"
65     LIBGUESTFS_BACKEND=direct virt-customize -a $libvirt_imgs/undercloud.qcow2 --run-command "mkdir -p /root/.ssh/" \
66         --upload ~/.ssh/id_rsa.pub:/root/.ssh/authorized_keys \
67         --run-command "chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys" \
68         --run-command "cp /root/.ssh/authorized_keys /home/stack/.ssh/" \
69         --run-command "chown stack:stack /home/stack/.ssh/authorized_keys && chmod 600 /home/stack/.ssh/authorized_keys"
70     virsh start undercloud
71     virsh autostart undercloud
72   fi
73
74   sleep 10 # let undercloud get started up
75
76   # get the undercloud VM IP
77   CNT=10
78   echo -n "${blue}Waiting for Undercloud's dhcp address${reset}"
79   undercloud_mac=$(virsh domiflist undercloud | grep default | awk '{ print $5 }')
80   while ! $(arp -en | grep ${undercloud_mac} > /dev/null) && [ $CNT -gt 0 ]; do
81       echo -n "."
82       sleep 10
83       CNT=$((CNT-1))
84   done
85   UNDERCLOUD=$(arp -en | grep ${undercloud_mac} | awk {'print $1'})
86
87   if [ -z "$UNDERCLOUD" ]; then
88     echo "\n\nCan't get IP for Undercloud. Can Not Continue."
89     exit 1
90   else
91      echo -e "${blue}\rUndercloud VM has IP $UNDERCLOUD${reset}"
92   fi
93
94   CNT=10
95   echo -en "${blue}\rValidating Undercloud VM connectivity${reset}"
96   while ! ping -c 1 $UNDERCLOUD > /dev/null && [ $CNT -gt 0 ]; do
97       echo -n "."
98       sleep 3
99       CNT=$((CNT-1))
100   done
101   if [ "$CNT" -eq 0 ]; then
102       echo "Failed to contact Undercloud. Can Not Continue"
103       exit 1
104   fi
105   CNT=10
106   while ! ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "echo ''" 2>&1> /dev/null && [ $CNT -gt 0 ]; do
107       echo -n "."
108       sleep 3
109       CNT=$((CNT-1))
110   done
111   if [ "$CNT" -eq 0 ]; then
112       echo "Failed to connect to Undercloud. Can Not Continue"
113       exit 1
114   fi
115
116   # extra space to overwrite the previous connectivity output
117   echo -e "${blue}\r                                                                 ${reset}"
118   sleep 1
119
120   # ensure stack user on Undercloud machine has an ssh key
121   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "if [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
122
123   # ssh key fix for stack user
124   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "restorecon -r /home/stack"
125 }
126
127 ##Copy over the glance images and instackenv json file
128 ##params: none
129 function configure_undercloud {
130   local controller_nic_template compute_nic_template
131   echo
132   echo "Copying configuration files to Undercloud"
133   echo -e "${blue}Network Environment set for Deployment: ${reset}"
134   cat $APEX_TMP_DIR/network-environment.yaml
135   scp ${SSH_OPTIONS[@]} $APEX_TMP_DIR/network-environment.yaml "stack@$UNDERCLOUD":
136
137   # check for ODL L3/ONOS
138   if [ "${deploy_options_array['dataplane']}" == 'fdio' ]; then
139     ext_net_type=vpp_interface
140   else
141     ext_net_type=br-ex
142   fi
143
144   if [ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' ]; then
145     ovs_dpdk_bridge='br-phy'
146   else
147     ovs_dpdk_bridge=''
148   fi
149
150   # for some reason putting IP on the bridge fails with pinging validation in OOO
151   if [ "${deploy_options_array['sfc']}" == 'True' ]; then
152     controller_external='interface'
153   else
154     controller_external='br-ex'
155   fi
156
157   if ! controller_nic_template=$(python3 -B $LIB/python/apex_python_utils.py nic-template -r controller -s $NETSETS -t $BASE/nics-template.yaml.jinja2 -e $controller_external --deploy-settings-file $DEPLOY_SETTINGS_FILE); then
158     echo -e "${red}ERROR: Failed to generate controller NIC heat template ${reset}"
159     exit 1
160   fi
161
162   if ! compute_nic_template=$(python3 -B $LIB/python/apex_python_utils.py nic-template -r compute -s $NETSETS -t $BASE/nics-template.yaml.jinja2 -e $ext_net_type -d "$ovs_dpdk_bridge" --deploy-settings-file $DEPLOY_SETTINGS_FILE); then
163     echo -e "${red}ERROR: Failed to generate compute NIC heat template ${reset}"
164     exit 1
165   fi
166   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
167 mkdir nics/
168 cat > nics/controller.yaml << EOF
169 $controller_nic_template
170 EOF
171 cat > nics/compute.yaml << EOF
172 $compute_nic_template
173 EOF
174 EOI
175
176   # disable requiretty for sudo
177   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "sed -i 's/Defaults\s*requiretty//'" /etc/sudoers
178
179   # configure undercloud on Undercloud VM
180   echo "Running undercloud installation and configuration."
181   echo "Logging undercloud installation to stack@undercloud:/home/stack/apex-undercloud-install.log"
182   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
183 set -e
184 openstack-config --set undercloud.conf DEFAULT local_ip ${admin_installer_vm_ip}/${admin_cidr##*/}
185 openstack-config --set undercloud.conf DEFAULT network_gateway ${admin_installer_vm_ip}
186 openstack-config --set undercloud.conf DEFAULT network_cidr ${admin_cidr}
187 openstack-config --set undercloud.conf DEFAULT dhcp_start ${admin_dhcp_range%%,*}
188 openstack-config --set undercloud.conf DEFAULT dhcp_end ${admin_dhcp_range##*,}
189 openstack-config --set undercloud.conf DEFAULT inspection_iprange ${admin_introspection_range}
190 openstack-config --set undercloud.conf DEFAULT undercloud_debug false
191 openstack-config --set undercloud.conf DEFAULT undercloud_hostname "undercloud.${domain_name}"
192 openstack-config --set undercloud.conf DEFAULT enable_ui false
193 openstack-config --set undercloud.conf DEFAULT undercloud_update_packages false
194 sudo openstack-config --set /etc/ironic/ironic.conf disk_utils iscsi_verify_attempts 30
195 sudo openstack-config --set /etc/ironic/ironic.conf disk_partitioner check_device_max_retries 40
196
197 if [[ -n "${deploy_options_array['ceph_device']}" ]]; then
198     sed -i '/ExtraConfig/a\\    ceph::profile::params::osds: {\\x27${deploy_options_array['ceph_device']}\\x27: {}}' ${ENV_FILE}
199 fi
200
201 sudo sed -i '/CephClusterFSID:/c\\  CephClusterFSID: \\x27$(cat /proc/sys/kernel/random/uuid)\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
202 sudo sed -i '/CephMonKey:/c\\  CephMonKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
203 sudo sed -i '/CephAdminKey:/c\\  CephAdminKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
204
205 if [ "\$(uname -i)" == 'aarch64' ]; then
206
207 # These two fixes are done in the base OOO image build right now
208 # keeping them here to know that they are done and in case we need
209 # to take care of them in the future.
210 #    # remove syslinux references for aarch64
211 #    sudo sh -xc 'cd /etc/puppet/modules/ironic/manifests && patch -p0 < puppet-ironic-manifests-pxe-pp-aarch64.patch'
212 #    sudo sed -i '/syslinux-extlinux/d' /usr/share/instack-undercloud/puppet-stack-config/puppet-stack-config.pp
213 #
214 #    # disable use_linkat in swift
215 #    sudo sed -i 's/o_tmpfile_supported()/False/' /usr/lib/python2.7/site-packages/swift/obj/diskfile.py
216
217     openstack-config --set undercloud.conf DEFAULT ipxe_enabled false
218     sudo sed -i '/    _link_ip_address_pxe_configs/a\\        _link_mac_pxe_configs(task)' /usr/lib/python2.7/site-packages/ironic/common/pxe_utils.py
219 fi
220
221 openstack undercloud install &> apex-undercloud-install.log || {
222     # cat the undercloud install log incase it fails
223     echo "ERROR: openstack undercloud install has failed. Dumping Log:"
224     cat apex-undercloud-install.log
225     exit 1
226 }
227
228 if [ "\$(uname -i)" == 'aarch64' ]; then
229 sudo yum -y reinstall grub2-efi shim
230 sudo cp /boot/efi/EFI/centos/grubaa64.efi /tftpboot/grubaa64.efi
231 sudo mkdir -p /tftpboot/EFI/centos
232 sudo tee /tftpboot/EFI/centos/grub.cfg > /dev/null << EOF
233 set default=master
234 set timeout=5
235 set hidden_timeout_quiet=false
236
237 menuentry "master"  {
238 configfile /tftpboot/\\\$net_default_ip.conf
239 }
240 EOF
241 sudo chmod 644 /tftpboot/EFI/centos/grub.cfg
242 sudo openstack-config --set /etc/ironic/ironic.conf pxe uefi_pxe_config_template \\\$pybasedir/drivers/modules/pxe_grub_config.template
243 sudo openstack-config --set /etc/ironic/ironic.conf pxe uefi_pxe_bootfile_name grubaa64.efi
244 sudo service openstack-ironic-conductor restart
245 sudo sed -i 's/linuxefi/linux/g' /usr/lib/python2.7/site-packages/ironic/drivers/modules/pxe_grub_config.template
246 sudo sed -i 's/initrdefi/initrd/g' /usr/lib/python2.7/site-packages/ironic/drivers/modules/pxe_grub_config.template
247 echo '' | sudo tee --append /tftpboot/map-file > /dev/null
248 echo 'r ^/EFI/centos/grub.cfg-(.*) /tftpboot/pxelinux.cfg/\\1' | sudo tee --append /tftpboot/map-file > /dev/null
249 sudo service xinetd restart
250 fi
251
252 # Set nova domain name
253 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dns_domain ${domain_name}
254 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dhcp_domain ${domain_name}
255 sudo systemctl restart openstack-nova-conductor
256 sudo systemctl restart openstack-nova-compute
257 sudo systemctl restart openstack-nova-api
258 sudo systemctl restart openstack-nova-scheduler
259
260 # Set neutron domain name
261 sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT dns_domain ${domain_name}
262 sudo systemctl restart neutron-server
263 sudo systemctl restart neutron-dhcp-agent
264 EOI
265
266 # configure external network
267 if [[ "$enabled_network_list" =~ "external" ]]; then
268   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" << EOI
269 if [[ "$external_installer_vm_vlan" != "native" ]]; then
270   cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-vlan${external_installer_vm_vlan}
271 DEVICE=vlan${external_installer_vm_vlan}
272 ONBOOT=yes
273 DEVICETYPE=ovs
274 TYPE=OVSIntPort
275 BOOTPROTO=static
276 IPADDR=${external_installer_vm_ip}
277 PREFIX=${external_cidr##*/}
278 OVS_BRIDGE=br-ctlplane
279 OVS_OPTIONS="tag=${external_installer_vm_vlan}"
280 EOF
281   ifup vlan${external_installer_vm_vlan}
282 else
283   if ! ip a s eth2 | grep ${external_installer_vm_ip} > /dev/null; then
284       ip a a ${external_installer_vm_ip}/${external_cidr##*/} dev eth2
285       ip link set up dev eth2
286   fi
287 fi
288 EOI
289 fi
290
291 }