3 # Deploy script to install provisioning server for OPNFV Apex
4 # author: Dan Radez (dradez@redhat.com)
5 # author: Tim Rozet (trozet@redhat.com)
7 # Based on RDO Manager http://www.rdoproject.org
12 if [ "$TERM" != "unknown" ]; then
27 ntp_server="pool.ntp.org"
28 net_isolation_enabled="TRUE"
32 declare -A deploy_options_array
34 SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error)
36 RESOURCES=/var/opt/opnfv/stack
38 INSTACKENV=$CONFIG/instackenv.json
39 NETENV=$CONFIG/network-environment.yaml
42 ##translates yaml into variables
43 ##params: filename, prefix (ex. "config_")
44 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
47 local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
48 sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
49 -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
51 indent = length($1)/2;
53 for (i in vname) {if (i > indent) {delete vname[i]}}
55 vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
56 printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
61 ##checks if prefix exists in string
62 ##params: string, prefix
63 ##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
67 if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
73 ##parses variable from a string with '='
74 ##and removes global prefix
75 ##params: string, prefix
76 ##usage: parse_setting_var 'deploy_myvar=2' 'deploy_'
80 if echo $mystr | grep -E "^.+\=" > /dev/null; then
81 echo $(echo $mystr | grep -Eo "^.+\=" | tr -d '=' | sed 's/^'"$prefix"'//')
86 ##parses value from a string with '='
88 ##usage: parse_setting_value
89 parse_setting_value() {
91 echo $(echo $mystr | grep -Eo "\=.*$" | tr -d '=')
93 ##parses deploy settings yaml into globals and options array
95 ##usage: parse_deploy_settings
96 parse_deploy_settings() {
97 local global_prefix="deploy_global_params_"
98 local options_prefix="deploy_deploy_options_"
100 local settings=$(parse_yaml $DEPLOY_SETTINGS_FILE "deploy_")
102 for this_setting in $settings; do
103 if contains_prefix $this_setting $global_prefix; then
104 myvar=$(parse_setting_var $this_setting $global_prefix)
105 if [ -z "$myvar" ]; then
106 echo -e "${red}ERROR: while parsing ${DEPLOY_SETTINGS_FILE} for setting: ${this_setting}${reset}"
108 myvalue=$(parse_setting_value $this_setting)
109 # Do not override variables set by cmdline
110 if [ -z "$(eval echo \$$myvar)" ]; then
111 eval "$myvar=\$myvalue"
112 echo -e "${blue}Global parameter set: ${myvar}:${myvalue}${reset}"
114 echo -e "${blue}Global parameter already set: ${myvar}${reset}"
116 elif contains_prefix $this_setting $options_prefix; then
117 myvar=$(parse_setting_var $this_setting $options_prefix)
118 if [ -z "$myvar" ]; then
119 echo -e "${red}ERROR: while parsing ${DEPLOY_SETTINGS_FILE} for setting: ${this_setting}${reset}"
121 myvalue=$(parse_setting_value $this_setting)
122 deploy_options_array[$myvar]=$myvalue
123 echo -e "${blue}Deploy option set: ${myvar}:${myvalue}${reset}"
127 ##parses baremetal yaml settings into compatible json
128 ##writes the json to $CONFIG/instackenv_tmp.json
130 ##usage: parse_inventory_file
131 parse_inventory_file() {
132 local inventory=$(parse_yaml $INVENTORY_FILE)
134 local node_prefix="node"
139 # detect number of nodes
140 for entry in $inventory; do
141 if echo $entry | grep -Eo "^nodes_node[0-9]+_" > /dev/null; then
142 this_node=$(echo $entry | grep -Eo "^nodes_node[0-9]+_")
143 if [[ $inventory_list != *"$this_node"* ]]; then
144 inventory_list+="$this_node "
149 inventory_list=$(echo $inventory_list | sed 's/ $//')
151 for node in $inventory_list; do
155 node_total=$node_count
157 if [[ "$node_total" -lt 5 && ha_enabled == "TRUE" ]]; then
158 echo -e "${red}ERROR: You must provide at least 5 nodes for HA baremetal deployment${reset}"
160 elif [[ "$node_total" -lt 2 ]]; then
161 echo -e "${red}ERROR: You must provide at least 2 nodes for non-HA baremetal deployment${reset}"
165 eval $(parse_yaml $INVENTORY_FILE)
173 for node in $inventory_list; do
177 \"pm_password\": \"$(eval echo \${${node}ipmi_pass})\",
178 \"pm_type\": \"pxe_ipmitool\",
180 \"$(eval echo \${${node}mac_address})\"
182 \"cpu\": \"$(eval echo \${${node}cpus})\",
183 \"memory\": \"$(eval echo \${${node}memory})\",
184 \"disk\": \"$(eval echo \${${node}disk})\",
185 \"arch\": \"$(eval echo \${${node}arch})\",
186 \"pm_user\": \"$(eval echo \${${node}ipmi_user})\",
187 \"pm_addr\": \"$(eval echo \${${node}ipmi_ip})\"
189 instack_env_output+=${node_output[$node]}
190 if [ $node_count -lt $node_total ]; then
191 instack_env_output+=" },"
193 instack_env_output+=" }"
197 instack_env_output+='
201 #Copy instackenv.json to undercloud for baremetal
202 echo -e "{blue}Parsed instackenv JSON:\n${instack_env_output}${reset}"
203 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" <<EOI
204 cat > instackenv.json << EOF
210 ##verify internet connectivity
212 function verify_internet {
213 if ping -c 2 $ping_site > /dev/null; then
214 if ping -c 2 www.google.com > /dev/null; then
215 echo "${blue}Internet connectivity detected${reset}"
218 echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
222 echo "${red}No internet connectivity detected${reset}"
227 ##download dependencies if missing and configure host
229 function configure_deps {
230 if ! verify_internet; then
231 echo "${red}Will not download dependencies${reset}"
235 # verify ip forwarding
236 if sysctl net.ipv4.ip_forward | grep 0; then
237 sudo sysctl -w net.ipv4.ip_forward=1
238 sudo sh -c "echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf"
241 # ensure brbm networks are configured
242 systemctl start openvswitch
243 ovs-vsctl list-br | grep brbm > /dev/null || ovs-vsctl add-br brbm
244 virsh net-list --all | grep brbm > /dev/null || virsh net-create $CONFIG/brbm-net.xml
245 virsh net-list | grep -E "brbm\s+active" > /dev/null || virsh net-start brbm
246 ovs-vsctl list-br | grep brbm1 > /dev/null || ovs-vsctl add-br brbm1
247 virsh net-list --all | grep brbm1 > /dev/null || virsh net-create $CONFIG/brbm1-net.xml
248 virsh net-list | grep -E "brbm1\s+active" > /dev/null || virsh net-start brbm1
250 # ensure storage pool exists and is started
251 virsh pool-list --all | grep default > /dev/null || virsh pool-create $CONFIG/default-pool.xml
252 virsh pool-list | grep -Eo "default\s+active" > /dev/null || virsh pool-start default
254 if virsh net-list | grep default > /dev/null; then
255 num_ints_same_subnet=$(ip addr show | grep "inet 192.168.122" | wc -l)
256 if [ "$num_ints_same_subnet" -gt 1 ]; then
257 virsh net-destroy default
258 ##go edit /etc/libvirt/qemu/networks/default.xml
259 sed -i 's/192.168.122/192.168.123/g' /etc/libvirt/qemu/networks/default.xml
260 sed -i 's/192.168.122/192.168.123/g' instackenv-virt.json
262 virsh net-start default
263 virsh net-autostart default
267 if ! egrep '^flags.*(vmx|svm)' /proc/cpuinfo > /dev/null; then
268 echo "${red}virtualization extensions not found, kvm kernel module insertion may fail.\n \
269 Are you sure you have enabled vmx in your bios or hypervisor?${reset}"
272 if ! lsmod | grep kvm > /dev/null; then modprobe kvm; fi
273 if ! lsmod | grep kvm_intel > /dev/null; then modprobe kvm_intel; fi
275 if ! lsmod | grep kvm > /dev/null; then
276 echo "${red}kvm kernel modules not loaded!${reset}"
281 if [ ! -e ~/.ssh/id_rsa.pub ]; then
282 ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
285 echo "${blue}All dependencies installed and running${reset}"
288 ##verify vm exists, an has a dhcp lease assigned to it
290 function setup_instack_vm {
291 if ! virsh list --all | grep instack > /dev/null; then
292 #virsh vol-create default instack.qcow2.xml
293 virsh define $CONFIG/instack.xml
295 #Upload instack image
296 #virsh vol-create default --file instack.qcow2.xml
297 virsh vol-create-as default instack.qcow2 30G --format qcow2
298 virsh vol-upload --pool default --vol instack.qcow2 --file $CONFIG/stack/instack.qcow2
300 sleep 1 # this was to let the copy settle, needed with vol-upload?
303 echo "Found Instack VM, using existing VM"
306 # if the VM is not running update the authkeys and start it
307 if ! virsh list | grep instack > /dev/null; then
308 echo "Injecting ssh key to instack VM"
309 virt-customize -c qemu:///system -d instack --upload ~/.ssh/id_rsa.pub:/root/.ssh/authorized_keys \
310 --run-command "chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys" \
311 --run-command "cp /root/.ssh/authorized_keys /home/stack/.ssh/" \
312 --run-command "chown stack:stack /home/stack/.ssh/authorized_keys && chmod 600 /home/stack/.ssh/authorized_keys"
316 sleep 3 # let DHCP happen
319 echo -n "${blue}Waiting for instack's dhcp address${reset}"
320 while ! grep instack /var/lib/libvirt/dnsmasq/default.leases > /dev/null && [ $CNT -gt 0 ]; do
326 # get the instack VM IP
327 UNDERCLOUD=$(grep instack /var/lib/libvirt/dnsmasq/default.leases | awk '{print $3}' | head -n 1)
328 if -n $UNDERCLOUD; then
329 echo "Never got IP for Instack. Can Not Continue."
332 echo -e "${blue}\rInstack VM has IP $UNDERCLOUD${reset}"
336 echo -en "${blue}\rValidating instack VM connectivity${reset}"
337 while ! ping -c 1 $UNDERCLOUD > /dev/null && [ $CNT -gt 0 ]; do
343 echo "Failed to contact Instack. Can Not Continue"
347 while ! ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "echo ''" 2>&1> /dev/null && [ $CNT -gt 0 ]; do
353 echo "Failed to connect to Instack. Can Not Continue"
357 # extra space to overwrite the previous connectivity output
358 echo -e "${blue}\r ${reset}"
360 #add the instack brbm1 interface
361 virsh attach-interface --domain instack --type network --source brbm1 --model rtl8139 --config --live
363 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "if ! ip a s eth2 | grep 192.168.37.1 > /dev/null; then ip a a 192.168.37.1/24 dev eth2; ip link set up dev eth2; fi"
365 # ssh key fix for stack user
366 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "restorecon -r /home/stack"
369 ##Create virtual nodes in virsh
371 function setup_virtual_baremetal {
372 for i in $(seq 0 $vm_index); do
373 if ! virsh list --all | grep baremetalbrbm_brbm1_${i} > /dev/null; then
374 if [ ! -e $CONFIG/baremetalbrbm_brbm1_${i}.xml ]; then
375 define_virtual_node baremetalbrbm_brbm1_${i}
377 virsh define $CONFIG/baremetalbrbm_brbm1_${i}.xml
379 echo "Found Baremetal ${i} VM, using existing VM"
381 virsh vol-list default | grep baremetalbrbm_brbm1_${i} 2>&1> /dev/null || virsh vol-create-as default baremetalbrbm_brbm1_${i}.qcow2 40G --format qcow2
385 ##Copy over the glance images and instack json file
387 function copy_materials_to_instack {
390 echo "Copying configuration file and disk images to instack"
391 scp ${SSH_OPTIONS[@]} $RESOURCES/deploy-ramdisk-ironic.initramfs "stack@$UNDERCLOUD":
392 scp ${SSH_OPTIONS[@]} $RESOURCES/deploy-ramdisk-ironic.kernel "stack@$UNDERCLOUD":
393 scp ${SSH_OPTIONS[@]} $RESOURCES/ironic-python-agent.initramfs "stack@$UNDERCLOUD":
394 scp ${SSH_OPTIONS[@]} $RESOURCES/ironic-python-agent.kernel "stack@$UNDERCLOUD":
395 scp ${SSH_OPTIONS[@]} $RESOURCES/ironic-python-agent.vmlinuz "stack@$UNDERCLOUD":
396 scp ${SSH_OPTIONS[@]} $RESOURCES/overcloud-full.initrd "stack@$UNDERCLOUD":
397 scp ${SSH_OPTIONS[@]} $RESOURCES/overcloud-full.qcow2 "stack@$UNDERCLOUD":
398 scp ${SSH_OPTIONS[@]} $RESOURCES/overcloud-full.vmlinuz "stack@$UNDERCLOUD":
399 scp ${SSH_OPTIONS[@]} $NETENV "stack@$UNDERCLOUD":
400 scp ${SSH_OPTIONS[@]} $CONFIG/opendaylight.yaml "stack@$UNDERCLOUD":
401 scp ${SSH_OPTIONS[@]} -r $CONFIG/nics/ "stack@$UNDERCLOUD":
403 if [[ ${#deploy_options_array[@]} -eq 0 || ${deploy_options_array['sdn_controller']} == 'opendaylight' ]]; then
404 DEPLOY_OPTIONS+=" -e opendaylight.yaml"
406 # when OpenDaylight lands in upstream RDO manager this can be removed
407 # apply the opendaylight patch
408 scp ${SSH_OPTIONS[@]} $CONFIG/opendaylight.patch "root@$UNDERCLOUD":
409 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cd /usr/share/openstack-tripleo-heat-templates/; patch -Np1 < /root/opendaylight.patch"
411 elif [ ${deploy_options_array['sdn_controller']} == 'onos' ]; then
412 echo -e "${red}ERROR: ONOS is currently unsupported...exiting${reset}"
414 elif [ ${deploy_options_array['sdn_controller']} == 'opencontrail' ]; then
415 echo -e "${red}ERROR: OpenContrail is currently unsupported...exiting${reset}"
419 # ensure stack user on instack machine has an ssh key
420 ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "if [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
422 if [ "$virtual" == "TRUE" ]; then
423 # fix MACs to match new setup
424 for i in $(seq 0 $vm_index); do
425 pyscript="import json
426 data = json.load(open('$CONFIG/instackenv-virt.json'))
427 print data['nodes'][$i]['mac'][0]"
429 old_mac=$(python -c "$pyscript")
430 new_mac=$(virsh dumpxml baremetalbrbm_brbm1_$i | grep "mac address" | cut -d = -f2 | grep -Eo "[0-9a-f:]+")
431 # this doesn't work with multiple vnics on the vms
432 #if [ "$old_mac" != "$new_mac" ]; then
433 # echo "${blue}Modifying MAC for node from $old_mac to ${new_mac}${reset}"
434 # sed -i 's/'"$old_mac"'/'"$new_mac"'/' $CONFIG/instackenv-virt.json
438 DEPLOY_OPTIONS+=" --libvirt-type qemu"
439 INSTACKENV=$CONFIG/instackenv-virt.json
440 NETENV=$CONFIG/network-environment.yaml
442 # upload instackenv file to Instack for virtual deployment
443 scp ${SSH_OPTIONS[@]} $INSTACKENV "stack@$UNDERCLOUD":instackenv.json
446 # allow stack to control power management on the hypervisor via sshkey
447 # only if this is a virtual deployment
448 if [ "$virtual" == "TRUE" ]; then
449 ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
450 while read -r line; do
451 stack_key=\${stack_key}\\\\\\\\n\${line}
452 done < <(cat ~/.ssh/id_rsa)
453 stack_key=\$(echo \$stack_key | sed 's/\\\\\\\\n//')
454 sed -i 's~INSERT_STACK_USER_PRIV_KEY~'"\$stack_key"'~' instackenv.json
458 # copy stack's ssh key to this users authorized keys
459 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
462 ##preping it for deployment and launch the deploy
464 function undercloud_prep_overcloud_deploy {
465 # check if HA is enabled
466 if [ $ha_enabled == "TRUE" ]; then
467 DEPLOY_OPTIONS+=" --control-scale 3 --compute-scale 2"
468 DEPLOY_OPTIONS+=" -e /usr/share/openstack-tripleo-heat-templates/environments/puppet-pacemaker.yaml"
469 DEPLOY_OPTIONS+=" --ntp-server $ntp_server"
472 if [ $net_isolation_enabled == "TRUE" ]; then
473 DEPLOY_OPTIONS+=" -e /usr/share/openstack-tripleo-heat-templates/environments/network-isolation.yaml"
474 DEPLOY_OPTIONS+=" -e network-environment.yaml"
477 ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
480 echo "Uploading overcloud glance images"
481 openstack overcloud image upload
482 echo "Configuring undercloud and discovering nodes"
483 openstack baremetal import --json instackenv.json
484 openstack baremetal configure boot
485 openstack baremetal introspection bulk start
486 echo "Configuring flavors"
487 openstack flavor list | grep baremetal || openstack flavor create --id auto --ram 4096 --disk 39 --vcpus 1 baremetal
488 openstack flavor set --property "cpu_arch"="x86_64" --property "capabilities:boot_option"="local" baremetal
489 echo "Configuring nameserver on ctlplane network"
490 neutron subnet-update \$(neutron subnet-list | grep -v id | grep -v \\\\-\\\\- | awk {'print \$2'}) --dns-nameserver 8.8.8.8
491 echo "Executing overcloud deployment, this should run for an extended period without output."
492 sleep 60 #wait for Hypervisor stats to check-in to nova
493 openstack overcloud deploy --templates $DEPLOY_OPTIONS
499 echo -e "Usage:\n$0 [arguments] \n"
500 echo -e " -c|--config : Directory to configuration files. Optional. Defaults to /var/opt/opnfv/ \n"
501 echo -e " -d|--deploy-settings : Full path to deploy settings yaml file. Optional. Defaults to null \n"
502 echo -e " -i|--inventory : Full path to inventory yaml file. Required only for baremetal \n"
503 echo -e " -n|--netenv : Full path to network environment file. Optional. Defaults to \$CONFIG/network-environment.yaml \n"
504 echo -e " -p|--ping-site : site to use to verify IP connectivity. Optional. Defaults to 8.8.8.8 \n"
505 echo -e " -r|--resources : Directory to deployment resources. Optional. Defaults to /var/opt/opnfv/stack \n"
506 echo -e " -v|--virtual : Virtualize overcloud nodes instead of using baremetal. \n"
507 echo -e " --no-ha : disable High Availability deployment scheme, this assumes a single controller and single compute node \n"
508 echo -e " --flat : disable Network Isolation and use a single flat network for the underlay network."
511 ##translates the command line parameters into variables
512 ##params: $@ the entire command line is passed
513 ##usage: parse_cmd_line() "$@"
515 echo -e "\n\n${blue}This script is used to deploy the Apex Installer and Provision OPNFV Target System${reset}\n\n"
516 echo "Use -h to display help"
519 while [ "${1:0:1}" = "-" ]
528 echo "Deployment Configuration Directory Overridden to: $2"
531 -d|--deploy-settings)
532 DEPLOY_SETTINGS_FILE=$2
533 echo "Deployment Configuration file: $2"
546 echo "Using $2 as the ping site"
551 echo "Deployment Resources Directory Overridden to: $2"
556 echo "Executing a Virtual Deployment"
561 echo "HA Deployment Disabled"
565 net_isolation_enabled="FALSE"
566 echo "Underlay Network Isolation Disabled: using flat configuration"
576 if [[ ! -z "$NETENV" && "$net_isolation_enabled" == "FALSE" ]]; then
577 echo -e "{red}WARN: Single flat network requested, but netenv specified. Ignoring netenv settings!${reset}"
578 elif [[ ! -z "$NETENV" && ! -z "$DEPLOY_SETTINGS_FILE" ]]; then
579 echo -e "${red}WARN: deploy_settings and netenv specified. Ignoring netenv settings! deploy_settings will contain \
583 if [[ -n "$virtual" && -n "$INVENTORY_FILE" ]]; then
584 echo -e "${red}ERROR: You should not specify an inventory with virtual deployments${reset}"
588 if [[ ! -z "$DEPLOY_SETTINGS_FILE" && ! -f "$DEPLOY_SETTINGS_FILE" ]]; then
589 echo -e "${red}ERROR: ${DEPLOY_SETTINGS_FILE} does not exist! Exiting...${reset}"
593 if [[ ! -z "$NETENV" && ! -f "$NETENV" ]]; then
594 echo -e "${red}ERROR: ${NETENV} does not exist! Exiting...${reset}"
598 if [[ ! -z "$INVENTORY_FILE" && ! -f "$INVENTORY_FILE" ]]; then
599 echo -e "{$red}ERROR: ${DEPLOY_SETTINGS_FILE} does not exist! Exiting...${reset}"
603 if [[ -z "$virtual" && -z "$INVENTORY_FILE" ]]; then
604 echo -e "${red}ERROR: You must specify an inventory file for baremetal deployments! Exiting...${reset}"
613 if ! configure_deps; then
614 echo "Dependency Validation Failed, Exiting."
616 if [ -n "$DEPLOY_SETTINGS_FILE" ]; then
617 parse_deploy_settings
620 if [ "$virtual" == "TRUE" ]; then
621 setup_virtual_baremetal
622 elif [ -n "$INVENTORY_FILE" ]; then
625 copy_materials_to_instack
626 undercloud_prep_overcloud_deploy