Disable tripleo_ui
[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 $BASE/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 $IMAGES/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 $IMAGES/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     if [ "$debug" == 'TRUE' ]; then
59       LIBGUESTFS_BACKEND=direct virt-customize -a $undercloud_dst --root-password password:opnfvapex
60     fi
61
62     echo "Injecting ssh key to Undercloud VM"
63     LIBGUESTFS_BACKEND=direct virt-customize -a $undercloud_dst --run-command "mkdir -p /root/.ssh/" \
64         --upload ~/.ssh/id_rsa.pub:/root/.ssh/authorized_keys \
65         --run-command "chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys" \
66         --run-command "cp /root/.ssh/authorized_keys /home/stack/.ssh/" \
67         --run-command "chown stack:stack /home/stack/.ssh/authorized_keys && chmod 600 /home/stack/.ssh/authorized_keys"
68     virsh start undercloud
69     virsh autostart undercloud
70   fi
71
72   sleep 10 # let undercloud get started up
73
74   # get the undercloud VM IP
75   CNT=10
76   echo -n "${blue}Waiting for Undercloud's dhcp address${reset}"
77   undercloud_mac=$(virsh domiflist undercloud | grep default | awk '{ print $5 }')
78   while ! $(arp -en | grep ${undercloud_mac} > /dev/null) && [ $CNT -gt 0 ]; do
79       echo -n "."
80       sleep 10
81       CNT=$((CNT-1))
82   done
83   UNDERCLOUD=$(arp -en | grep ${undercloud_mac} | awk {'print $1'})
84
85   if [ -z "$UNDERCLOUD" ]; then
86     echo "\n\nCan't get IP for Undercloud. Can Not Continue."
87     exit 1
88   else
89      echo -e "${blue}\rUndercloud VM has IP $UNDERCLOUD${reset}"
90   fi
91
92   CNT=10
93   echo -en "${blue}\rValidating Undercloud VM connectivity${reset}"
94   while ! ping -c 1 $UNDERCLOUD > /dev/null && [ $CNT -gt 0 ]; do
95       echo -n "."
96       sleep 3
97       CNT=$((CNT-1))
98   done
99   if [ "$CNT" -eq 0 ]; then
100       echo "Failed to contact Undercloud. Can Not Continue"
101       exit 1
102   fi
103   CNT=10
104   while ! ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "echo ''" 2>&1> /dev/null && [ $CNT -gt 0 ]; do
105       echo -n "."
106       sleep 3
107       CNT=$((CNT-1))
108   done
109   if [ "$CNT" -eq 0 ]; then
110       echo "Failed to connect to Undercloud. Can Not Continue"
111       exit 1
112   fi
113
114   # extra space to overwrite the previous connectivity output
115   echo -e "${blue}\r                                                                 ${reset}"
116   sleep 1
117
118   # ssh key fix for stack user
119   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "restorecon -r /home/stack"
120 }
121
122 ##Copy over the glance images and instackenv json file
123 ##params: none
124 function configure_undercloud {
125   local controller_nic_template compute_nic_template
126   echo
127   echo "Copying configuration files to Undercloud"
128   echo -e "${blue}Network Environment set for Deployment: ${reset}"
129   cat $APEX_TMP_DIR/network-environment.yaml
130   scp ${SSH_OPTIONS[@]} $APEX_TMP_DIR/network-environment.yaml "stack@$UNDERCLOUD":
131
132   # check for ODL L3/ONOS
133   if [ "${deploy_options_array['sdn_l3']}" == 'True' ]; then
134     if [ "${deploy_options_array['dataplane']}" == 'fdio' ]; then
135       ext_net_type=vpp_interface
136     else
137       ext_net_type=br-ex
138     fi
139   fi
140
141   if [ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' ]; then
142     ovs_dpdk_bridge='br-phy'
143   else
144     ovs_dpdk_bridge=''
145   fi
146
147   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 "br-ex" --deploy-settings-file $DEPLOY_SETTINGS_FILE); then
148     echo -e "${red}ERROR: Failed to generate controller NIC heat template ${reset}"
149     exit 1
150   fi
151
152   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
153     echo -e "${red}ERROR: Failed to generate compute NIC heat template ${reset}"
154     exit 1
155   fi
156   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
157 mkdir nics/
158 cat > nics/controller.yaml << EOF
159 $controller_nic_template
160 EOF
161 cat > nics/compute.yaml << EOF
162 $compute_nic_template
163 EOF
164 EOI
165
166   # ensure stack user on Undercloud machine has an ssh key
167   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "if [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
168
169   if [ "$virtual" == "TRUE" ]; then
170
171       # copy the Undercloud VM's stack user's pub key to
172       # root's auth keys so that Undercloud can control
173       # vm power on the hypervisor
174       ssh ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> /root/.ssh/authorized_keys
175   fi
176
177   # allow stack to control power management on the hypervisor via sshkey
178   # only if this is a virtual deployment
179   if [ "$virtual" == "TRUE" ]; then
180       ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
181 while read -r line; do
182   stack_key=\${stack_key}\\\\\\\\n\${line}
183 done < <(cat ~/.ssh/id_rsa)
184 stack_key=\$(echo \$stack_key | sed 's/\\\\\\\\n//')
185 sed -i 's~INSERT_STACK_USER_PRIV_KEY~'"\$stack_key"'~' instackenv.json
186 EOI
187   fi
188
189   # copy stack's ssh key to this users authorized keys
190   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
191
192   # disable requiretty for sudo
193   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "sed -i 's/Defaults\s*requiretty//'" /etc/sudoers
194
195   # configure undercloud on Undercloud VM
196   echo "Running undercloud configuration."
197   echo "Logging undercloud configuration to undercloud:/home/stack/apex-undercloud-install.log"
198   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" << EOI
199 sed -i 's/#local_ip/local_ip/' undercloud.conf
200 sed -i 's/#network_gateway/network_gateway/' undercloud.conf
201 sed -i 's/#network_cidr/network_cidr/' undercloud.conf
202 sed -i 's/#dhcp_start/dhcp_start/' undercloud.conf
203 sed -i 's/#dhcp_end/dhcp_end/' undercloud.conf
204 sed -i 's/#inspection_iprange/inspection_iprange/' undercloud.conf
205 sed -i 's/#undercloud_debug/undercloud_debug/' undercloud.conf
206
207 openstack-config --set undercloud.conf DEFAULT local_ip ${admin_installer_vm_ip}/${admin_cidr##*/}
208 openstack-config --set undercloud.conf DEFAULT network_gateway ${admin_installer_vm_ip}
209 openstack-config --set undercloud.conf DEFAULT network_cidr ${admin_cidr}
210 openstack-config --set undercloud.conf DEFAULT dhcp_start ${admin_dhcp_range%%,*}
211 openstack-config --set undercloud.conf DEFAULT dhcp_end ${admin_dhcp_range##*,}
212 openstack-config --set undercloud.conf DEFAULT inspection_iprange ${admin_introspection_range}
213 openstack-config --set undercloud.conf DEFAULT undercloud_debug false
214 openstack-config --set undercloud.conf DEFAULT undercloud_hostname "undercloud.${domain_name}"
215 openstack-config --set undercloud.conf DEFAULT enable_ui false
216 sudo openstack-config --set /etc/ironic/ironic.conf disk_utils iscsi_verify_attempts 30
217 sudo openstack-config --set /etc/ironic/ironic.conf disk_partitioner check_device_max_retries 40
218
219 if [[ -n "${deploy_options_array['ceph_device']}" ]]; then
220     sed -i '/ExtraConfig/a\\    ceph::profile::params::osds: {\\x27${deploy_options_array['ceph_device']}\\x27: {}}' ${ENV_FILE}
221 fi
222
223 sudo sed -i '/CephClusterFSID:/c\\  CephClusterFSID: \\x27$(cat /proc/sys/kernel/random/uuid)\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
224 sudo sed -i '/CephMonKey:/c\\  CephMonKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
225 sudo sed -i '/CephAdminKey:/c\\  CephAdminKey: \\x27'"\$(ceph-authtool --gen-print-key)"'\\x27' /usr/share/openstack-tripleo-heat-templates/environments/storage-environment.yaml
226
227 # we assume that packages will not need to be updated with undercloud install
228 # and that it will be used only to configure the undercloud
229 # packages updates would need to be handled manually with yum update
230 sudo cp -f /usr/share/diskimage-builder/elements/yum/bin/install-packages /usr/share/diskimage-builder/elements/yum/bin/install-packages.bak
231 cat << 'EOF' | sudo tee /usr/share/diskimage-builder/elements/yum/bin/install-packages > /dev/null
232 #!/bin/sh
233 exit 0
234 EOF
235
236 openstack undercloud install &> apex-undercloud-install.log || {
237     # cat the undercloud install log incase it fails
238     echo "ERROR: openstack undercloud install has failed. Dumping Log:"
239     cat apex-undercloud-install.log
240     exit 1
241 }
242
243 sleep 30
244 sudo systemctl restart openstack-glance-api
245 # Set nova domain name
246 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dns_domain ${domain_name}
247 sudo openstack-config --set /etc/nova/nova.conf DEFAULT dhcp_domain ${domain_name}
248 sudo systemctl restart openstack-nova-conductor
249 sudo systemctl restart openstack-nova-compute
250 sudo systemctl restart openstack-nova-api
251 sudo systemctl restart openstack-nova-scheduler
252
253 # Set neutron domain name
254 sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT dns_domain ${domain_name}
255 sudo systemctl restart neutron-server
256 sudo systemctl restart neutron-dhcp-agent
257
258 sudo sed -i '/num_engine_workers/c\num_engine_workers = 2' /etc/heat/heat.conf
259 sudo sed -i '/#workers\s=/c\workers = 2' /etc/heat/heat.conf
260 sudo systemctl restart openstack-heat-engine
261 sudo systemctl restart openstack-heat-api
262 EOI
263
264 # configure external network
265 if [[ "$enabled_network_list" =~ "external" ]]; then
266   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" << EOI
267 if [[ "$external_installer_vm_vlan" != "native" ]]; then
268   cat <<EOF > /etc/sysconfig/network-scripts/ifcfg-vlan${external_installer_vm_vlan}
269 DEVICE=vlan${external_installer_vm_vlan}
270 ONBOOT=yes
271 DEVICETYPE=ovs
272 TYPE=OVSIntPort
273 BOOTPROTO=static
274 IPADDR=${external_installer_vm_ip}
275 PREFIX=${external_cidr##*/}
276 OVS_BRIDGE=br-ctlplane
277 OVS_OPTIONS="tag=${external_installer_vm_vlan}"
278 EOF
279   ifup vlan${external_installer_vm_vlan}
280 else
281   if ! ip a s eth2 | grep ${external_installer_vm_ip} > /dev/null; then
282       ip a a ${external_installer_vm_ip}/${external_cidr##*/} dev eth2
283       ip link set up dev eth2
284   fi
285 fi
286 EOI
287 fi
288
289 # WORKAROUND: must restart the above services to fix sync problem with nova compute manager
290 # TODO: revisit and file a bug if necessary. This should eventually be removed
291 # as well as glance api problem
292 echo -e "${blue}INFO: Sleeping 15 seconds while services come back from restart${reset}"
293 sleep 15
294
295 }