migrating to proposed common network settings file
[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   if ! virsh list --all | grep undercloud > /dev/null; then
15       undercloud_nets="default admin"
16       if [[ $enabled_network_list =~ "external" ]]; then
17         undercloud_nets+=" external"
18       fi
19       define_vm undercloud hd 30 "$undercloud_nets" 4 12288
20
21       ### this doesn't work for some reason I was getting hangup events so using cp instead
22       #virsh vol-upload --pool default --vol undercloud.qcow2 --file $CONFIG/stack/undercloud.qcow2
23       #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)
24       #2015-12-05 12:57:20.569+0000: 8755: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
25       #2015-12-05 12:57:20.569+0000: 8756: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
26       #error: cannot close volume undercloud.qcow2
27       #error: internal error: received hangup / error event on socket
28       #error: Reconnected to the hypervisor
29
30       local undercloud_dst=/var/lib/libvirt/images/undercloud.qcow2
31       cp -f $RESOURCES/undercloud.qcow2 $undercloud_dst
32
33       # resize Undercloud machine
34       echo "Checking if Undercloud needs to be resized..."
35       undercloud_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a $undercloud_dst |grep device | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
36       if [ "$undercloud_size" -lt 30 ]; then
37         qemu-img resize /var/lib/libvirt/images/undercloud.qcow2 +25G
38         LIBGUESTFS_BACKEND=direct virt-resize --expand /dev/sda1 $RESOURCES/undercloud.qcow2 $undercloud_dst
39         LIBGUESTFS_BACKEND=direct virt-customize -a $undercloud_dst --run-command 'xfs_growfs -d /dev/sda1 || true'
40         new_size=$(LIBGUESTFS_BACKEND=direct virt-filesystems --long -h --all -a $undercloud_dst |grep filesystem | grep -Eo "[0-9\.]+G" | sed -n 's/\([0-9][0-9]*\).*/\1/p')
41         if [ "$new_size" -lt 30 ]; then
42           echo "Error resizing Undercloud machine, disk size is ${new_size}"
43           exit 1
44         else
45           echo "Undercloud successfully resized"
46         fi
47       else
48         echo "Skipped Undercloud resize, upstream is large enough"
49       fi
50
51   else
52       echo "Found existing Undercloud VM, exiting."
53       exit 1
54   fi
55
56   # if the VM is not running update the authkeys and start it
57   if ! virsh list | grep undercloud > /dev/null; then
58     echo "Injecting ssh key to Undercloud VM"
59     LIBGUESTFS_BACKEND=direct virt-customize -a $undercloud_dst --run-command "mkdir -p /root/.ssh/" \
60         --upload ~/.ssh/id_rsa.pub:/root/.ssh/authorized_keys \
61         --run-command "chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys" \
62         --run-command "cp /root/.ssh/authorized_keys /home/stack/.ssh/" \
63         --run-command "chown stack:stack /home/stack/.ssh/authorized_keys && chmod 600 /home/stack/.ssh/authorized_keys"
64     virsh start undercloud
65   fi
66
67   sleep 10 # let undercloud get started up
68
69   # get the undercloud VM IP
70   CNT=10
71   echo -n "${blue}Waiting for Undercloud's dhcp address${reset}"
72   undercloud_mac=$(virsh domiflist undercloud | grep default | awk '{ print $5 }')
73   while ! $(arp -e | grep ${undercloud_mac} > /dev/null) && [ $CNT -gt 0 ]; do
74       echo -n "."
75       sleep 10
76       CNT=$((CNT-1))
77   done
78   UNDERCLOUD=$(arp -e | grep ${undercloud_mac} | awk {'print $1'})
79
80   if [ -z "$UNDERCLOUD" ]; then
81     echo "\n\nCan't get IP for Undercloud. Can Not Continue."
82     exit 1
83   else
84      echo -e "${blue}\rUndercloud VM has IP $UNDERCLOUD${reset}"
85   fi
86
87   CNT=10
88   echo -en "${blue}\rValidating Undercloud VM connectivity${reset}"
89   while ! ping -c 1 $UNDERCLOUD > /dev/null && [ $CNT -gt 0 ]; do
90       echo -n "."
91       sleep 3
92       CNT=$((CNT-1))
93   done
94   if [ "$CNT" -eq 0 ]; then
95       echo "Failed to contact Undercloud. Can Not Continue"
96       exit 1
97   fi
98   CNT=10
99   while ! ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "echo ''" 2>&1> /dev/null && [ $CNT -gt 0 ]; do
100       echo -n "."
101       sleep 3
102       CNT=$((CNT-1))
103   done
104   if [ "$CNT" -eq 0 ]; then
105       echo "Failed to connect to Undercloud. Can Not Continue"
106       exit 1
107   fi
108
109   # extra space to overwrite the previous connectivity output
110   echo -e "${blue}\r                                                                 ${reset}"
111   sleep 1
112
113   # ssh key fix for stack user
114   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "restorecon -r /home/stack"
115 }
116
117 ##Copy over the glance images and instackenv json file
118 ##params: none
119 function configure_undercloud {
120   local controller_nic_template compute_nic_template
121   echo
122   echo "Copying configuration files to Undercloud"
123   if [[ "$net_isolation_enabled" == "TRUE" ]]; then
124     echo -e "${blue}Network Environment set for Deployment: ${reset}"
125     cat $APEX_TMP_DIR/network-environment.yaml
126     scp ${SSH_OPTIONS[@]} $APEX_TMP_DIR/network-environment.yaml "stack@$UNDERCLOUD":
127
128     # check for ODL L3/ONOS
129     if [ "${deploy_options_array['sdn_l3']}" == 'True' ]; then
130       ext_net_type=br-ex
131     fi
132
133     if [ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' ]; then
134       ovs_dpdk_bridge='br-phy'
135     else
136       ovs_dpdk_bridge=''
137     fi
138
139     if ! controller_nic_template=$(python3 -B $LIB/python/apex_python_utils.py nic-template -r controller -s $NETSETS $net_isolation_arg -t $CONFIG/nics-template.yaml.jinja2 -e "br-ex"); then
140       echo -e "${red}ERROR: Failed to generate controller NIC heat template ${reset}"
141       exit 1
142     fi
143
144     if ! compute_nic_template=$(python3 -B $LIB/python/apex_python_utils.py nic-template -r compute -s $NETSETS $net_isolation_arg -t $CONFIG/nics-template.yaml.jinja2 -e $ext_net_type -d "$ovs_dpdk_bridge"); then
145       echo -e "${red}ERROR: Failed to generate compute NIC heat template ${reset}"
146       exit 1
147     fi
148     ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
149 mkdir nics/
150 cat > nics/controller.yaml << EOF
151 $controller_nic_template
152 EOF
153 cat > nics/compute.yaml << EOF
154 $compute_nic_template
155 EOF
156 EOI
157   fi
158
159   # ensure stack user on Undercloud machine has an ssh key
160   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "if [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
161
162   if [ "$virtual" == "TRUE" ]; then
163
164       # copy the Undercloud VM's stack user's pub key to
165       # root's auth keys so that Undercloud can control
166       # vm power on the hypervisor
167       ssh ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> /root/.ssh/authorized_keys
168   fi
169
170   # allow stack to control power management on the hypervisor via sshkey
171   # only if this is a virtual deployment
172   if [ "$virtual" == "TRUE" ]; then
173       ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
174 while read -r line; do
175   stack_key=\${stack_key}\\\\\\\\n\${line}
176 done < <(cat ~/.ssh/id_rsa)
177 stack_key=\$(echo \$stack_key | sed 's/\\\\\\\\n//')
178 sed -i 's~INSERT_STACK_USER_PRIV_KEY~'"\$stack_key"'~' instackenv.json
179 EOI
180   fi
181
182   # copy stack's ssh key to this users authorized keys
183   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
184
185   # disable requiretty for sudo
186   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "sed -i 's/Defaults\s*requiretty//'" /etc/sudoers
187
188   # configure undercloud on Undercloud VM
189   echo "Running undercloud configuration."
190   echo "Logging undercloud configuration to undercloud:/home/stack/apex-undercloud-install.log"
191   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
192 if [[ "$net_isolation_enabled" == "TRUE" ]]; then
193   sed -i 's/#local_ip/local_ip/' undercloud.conf
194   sed -i 's/#network_gateway/network_gateway/' undercloud.conf
195   sed -i 's/#network_cidr/network_cidr/' undercloud.conf
196   sed -i 's/#dhcp_start/dhcp_start/' undercloud.conf
197   sed -i 's/#dhcp_end/dhcp_end/' undercloud.conf
198   sed -i 's/#inspection_iprange/inspection_iprange/' undercloud.conf
199   sed -i 's/#undercloud_debug/undercloud_debug/' undercloud.conf
200
201   openstack-config --set undercloud.conf DEFAULT local_ip ${admin_installer_vm_ip}/${admin_cidr##*/}
202   openstack-config --set undercloud.conf DEFAULT network_gateway ${admin_installer_vm_ip}
203   openstack-config --set undercloud.conf DEFAULT network_cidr ${admin_cidr}
204   openstack-config --set undercloud.conf DEFAULT dhcp_start ${admin_dhcp_range%%,*}
205   openstack-config --set undercloud.conf DEFAULT dhcp_end ${admin_dhcp_range##*,}
206   openstack-config --set undercloud.conf DEFAULT inspection_iprange ${admin_introspection_range}
207   openstack-config --set undercloud.conf DEFAULT undercloud_debug false
208   openstack-config --set undercloud.conf DEFAULT undercloud_hostname "undercloud.${domain_name}"
209   sudo openstack-config --set /etc/ironic/ironic.conf disk_utils iscsi_verify_attempts 30
210   sudo openstack-config --set /etc/ironic/ironic.conf disk_partitioner check_device_max_retries 40
211
212 fi
213
214 sudo sed -i '/CephClusterFSID:/c\\  CephClusterFSID: \\x27$(cat /proc/sys/kernel/random/uuid)\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
215 sudo sed -i '/CephMonKey:/c\\  CephMonKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
216 sudo sed -i '/CephAdminKey:/c\\  CephAdminKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
217
218 # we assume that packages will not need to be updated with undercloud install
219 # and that it will be used only to configure the undercloud
220 # packages updates would need to be handled manually with yum update
221 sudo cp -f /usr/share/diskimage-builder/elements/yum/bin/install-packages /usr/share/diskimage-builder/elements/yum/bin/install-packages.bak
222 cat << 'EOF' | sudo tee /usr/share/diskimage-builder/elements/yum/bin/install-packages > /dev/null
223 #!/bin/sh
224 exit 0
225 EOF
226
227 openstack undercloud install &> apex-undercloud-install.log || {
228     # cat the undercloud install log incase it fails
229     echo "ERROR: openstack undercloud install has failed. Dumping Log:"
230     cat apex-undercloud-install.log
231     exit 1
232 }
233
234 sleep 30
235 sudo systemctl restart openstack-glance-api
236 # Set nova domain name
237 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dns_domain ${domain_name}
238 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dhcp_domain ${domain_name}
239 sudo systemctl restart openstack-nova-conductor
240 sudo systemctl restart openstack-nova-compute
241 sudo systemctl restart openstack-nova-api
242 sudo systemctl restart openstack-nova-scheduler
243
244 # Set neutron domain name
245 sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT dns_domain ${domain_name}
246 sudo systemctl restart neutron-server
247 sudo systemctl restart neutron-dhcp-agent
248
249 sudo sed -i '/num_engine_workers/c\num_engine_workers = 2' /etc/heat/heat.conf
250 sudo sed -i '/#workers\s=/c\workers = 2' /etc/heat/heat.conf
251 sudo systemctl restart openstack-heat-engine
252 sudo systemctl restart openstack-heat-api
253 EOI
254
255 # configure external network
256   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" << EOI
257 if [[ "$external_installer_vm_vlan" != "native" ]]; then
258   cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-vlan${external_installer_vm_vlan}
259 DEVICE=vlan${external_installer_vm_vlan}
260 ONBOOT=yes
261 DEVICETYPE=ovs
262 TYPE=OVSIntPort
263 BOOTPROTO=static
264 IPADDR=${external_installer_vm_ip}
265 PREFIX=${external_cidr##*/}
266 OVS_BRIDGE=br-ctlplane
267 OVS_OPTIONS="tag=${external_installer_vm_vlan}"
268 EOF
269   ifup vlan${external_installer_vm_vlan}
270 else
271   if ! ip a s eth2 | grep ${external_installer_vm_ip} > /dev/null; then
272       ip a a ${external_installer_vm_ip}/${external_cidr##*/} dev eth2
273       ip link set up dev eth2
274   fi
275 fi
276 EOI
277
278 # WORKAROUND: must restart the above services to fix sync problem with nova compute manager
279 # TODO: revisit and file a bug if necessary. This should eventually be removed
280 # as well as glance api problem
281 echo -e "${blue}INFO: Sleeping 15 seconds while services come back from restart${reset}"
282 sleep 15
283
284 }