syntax fixes for if statements
[apex.git] / ci / deploy.sh
1 #!/bin/bash
2
3 # Deploy script to install provisioning server for OPNFV Apex
4 # author: Dan Radez (dradez@redhat.com)
5 # author: Tim Rozet (trozet@redhat.com)
6 #
7 # Based on RDO Manager http://www.rdoproject.org
8
9 set -e
10
11 ##VARIABLES
12 if [ "$TERM" != "unknown" ]; then
13   reset=$(tput sgr0)
14   blue=$(tput setaf 4)
15   red=$(tput setaf 1)
16   green=$(tput setaf 2)
17 else
18   reset=""
19   blue=""
20   red=""
21   green=""
22 fi
23
24 vm_index=4
25 ha_enabled="TRUE"
26 ping_site="8.8.8.8"
27 ntp_server="pool.ntp.org"
28 net_isolation_enabled="TRUE"
29
30 declare -i CNT
31 declare UNDERCLOUD
32 declare -A deploy_options_array
33
34 SSH_OPTIONS=(-o StrictHostKeyChecking=no -o GlobalKnownHostsFile=/dev/null -o UserKnownHostsFile=/dev/null -o LogLevel=error)
35 DEPLOY_OPTIONS=""
36 RESOURCES=/var/opt/opnfv/stack
37 CONFIG=/var/opt/opnfv
38 INSTACKENV=$CONFIG/instackenv.json
39 NETENV=$CONFIG/network-environment.yaml
40
41 ##FUNCTIONS
42 ##translates yaml into variables
43 ##params: filename, prefix (ex. "config_")
44 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
45 parse_yaml() {
46    local prefix=$2
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 |
50    awk -F$fs '{
51       indent = length($1)/2;
52       vname[indent] = $2;
53       for (i in vname) {if (i > indent) {delete vname[i]}}
54       if (length($3) > 0) {
55          vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
56          printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
57       }
58    }'
59 }
60
61 ##checks if prefix exists in string
62 ##params: string, prefix
63 ##usage: contains_prefix "deploy_setting_launcher=1" "deploy_setting"
64 contains_prefix() {
65   local mystr=$1
66   local prefix=$2
67   if echo $mystr | grep -E "^$prefix.*$" > /dev/null; then
68     return 0
69   else
70     return 1
71   fi
72 }
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_'
77 parse_setting_var() {
78   local mystr=$1
79   local prefix=$2
80   if echo $mystr | grep -E "^.+\=" > /dev/null; then
81     echo $(echo $mystr | grep -Eo "^.+\=" | tr -d '=' |  sed 's/^'"$prefix"'//')
82   else
83     return 1
84   fi
85 }
86 ##parses value from a string with '='
87 ##params: string
88 ##usage: parse_setting_value
89 parse_setting_value() {
90   local mystr=$1
91   echo $(echo $mystr | grep -Eo "\=.*$" | tr -d '=')
92 }
93 ##parses deploy settings yaml into globals and options array
94 ##params: none
95 ##usage:  parse_deploy_settings
96 parse_deploy_settings() {
97   local global_prefix="deploy_global_params_"
98   local options_prefix="deploy_deploy_options_"
99   local myvar myvalue
100   local settings=$(parse_yaml $DEPLOY_SETTINGS_FILE "deploy_")
101
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}"
107       fi
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}"
113       else
114         echo -e "${blue}Global parameter already set: ${myvar}${reset}"
115       fi
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}"
120       fi
121       myvalue=$(parse_setting_value $this_setting)
122       deploy_options_array[$myvar]=$myvalue
123       echo -e "${blue}Deploy option set: ${myvar}:${myvalue}${reset}"
124     fi
125   done
126 }
127 ##parses baremetal yaml settings into compatible json
128 ##writes the json to $CONFIG/instackenv_tmp.json
129 ##params: none
130 ##usage: parse_inventory_file
131 parse_inventory_file() {
132   local inventory=$(parse_yaml $INVENTORY_FILE)
133   local node_list
134   local node_prefix="node"
135   local node_count=0
136   local node_total
137   local inventory_list
138
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 "
145       fi
146     fi
147   done
148
149   inventory_list=$(echo $inventory_list | sed 's/ $//')
150
151   for node in $inventory_list; do
152     ((node_count+=1))
153   done
154
155   node_total=$node_count
156
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}"
159     exit 1
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}"
162     exit 1
163   fi
164
165   eval $(parse_yaml $INVENTORY_FILE)
166
167   instack_env_output="
168 {
169  \"nodes\" : [
170
171 "
172   node_count=0
173   for node in $inventory_list; do
174     ((node_count+=1))
175     node_output="
176         {
177           \"pm_password\": \"$(eval echo \${${node}ipmi_pass})\",
178           \"pm_type\": \"pxe_ipmitool\",
179           \"mac\": [
180             \"$(eval echo \${${node}mac_address})\"
181           ],
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})\"
188 "
189     instack_env_output+=${node_output}
190     if [ $node_count -lt $node_total ]; then
191       instack_env_output+="        },"
192     else
193       instack_env_output+="        }"
194     fi
195   done
196
197   instack_env_output+='
198   ]
199 }
200 '
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[@]} "stack@$UNDERCLOUD" <<EOI
204 cat > instackenv.json << EOF
205 $instack_env_output
206 EOF
207 EOI
208
209 }
210 ##verify internet connectivity
211 #params: none
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}"
216       return 0
217     else
218       echo "${red}Internet connectivity detected, but DNS lookup failed${reset}"
219       return 1
220     fi
221   else
222     echo "${red}No internet connectivity detected${reset}"
223     return 1
224   fi
225 }
226
227 ##download dependencies if missing and configure host
228 #params: none
229 function configure_deps {
230   if ! verify_internet; then
231     echo "${red}Will not download dependencies${reset}"
232     internet=false
233   fi
234
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"
239   fi
240
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
249
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
253
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
261       sleep 5
262       virsh net-start default
263       virsh net-autostart default
264     fi
265   fi
266
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}"
270   fi
271
272   if ! lsmod | grep kvm > /dev/null; then modprobe kvm; fi
273   if ! lsmod | grep kvm_intel > /dev/null; then modprobe kvm_intel; fi
274
275   if ! lsmod | grep kvm > /dev/null; then
276     echo "${red}kvm kernel modules not loaded!${reset}"
277     return 1
278   fi
279
280   ##sshkeygen for root
281   if [ ! -e ~/.ssh/id_rsa.pub ]; then
282     ssh-keygen -t rsa -N "" -f ~/.ssh/id_rsa
283   fi
284
285   echo "${blue}All dependencies installed and running${reset}"
286 }
287
288 ##verify vm exists, an has a dhcp lease assigned to it
289 ##params: none
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
294
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
299       ### this doesn't work for some reason I was getting hangup events so using cp instead
300       #virsh vol-upload --pool default --vol instack.qcow2 --file $CONFIG/stack/instack.qcow2
301       #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)
302       #2015-12-05 12:57:20.569+0000: 8755: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
303       #2015-12-05 12:57:20.569+0000: 8756: warning : virKeepAliveTimerInternal:143 : No response from client 0x7ff1e231e630 after 6 keepalive messages in 35 seconds
304       #error: cannot close volume instack.qcow2
305       #error: internal error: received hangup / error event on socket
306       #error: Reconnected to the hypervisor
307
308       cp -f $RESOURCES/instack.qcow2 /var/lib/libvirt/images/instack.qcow2
309
310   else
311       echo "Found Instack VM, using existing VM"
312   fi
313
314   # if the VM is not running update the authkeys and start it
315   if ! virsh list | grep instack > /dev/null; then
316     echo "Injecting ssh key to instack VM"
317     virt-customize -c qemu:///system -d instack --run-command "mkdir /root/.ssh/" \
318         --upload ~/.ssh/id_rsa.pub:/root/.ssh/authorized_keys \
319         --run-command "chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys" \
320         --run-command "cp /root/.ssh/authorized_keys /home/stack/.ssh/" \
321         --run-command "chown stack:stack /home/stack/.ssh/authorized_keys && chmod 600 /home/stack/.ssh/authorized_keys"
322     virsh start instack
323   fi
324
325   sleep 3 # let DHCP happen
326
327   CNT=10
328   echo -n "${blue}Waiting for instack's dhcp address${reset}"
329   while ! grep instack /var/lib/libvirt/dnsmasq/default.leases > /dev/null && [ $CNT -gt 0 ]; do
330       echo -n "."
331       sleep 3
332       CNT=CNT-1
333   done
334
335   # get the instack VM IP
336   UNDERCLOUD=$(grep instack /var/lib/libvirt/dnsmasq/default.leases | awk '{print $3}' | head -n 1)
337   if [ -z "$UNDERCLOUD" ]; then
338      echo "\n\nNever got IP for Instack. Can Not Continue."
339      exit 1
340   else
341      echo -e "${blue}\rInstack VM has IP $UNDERCLOUD${reset}"
342   fi
343
344   CNT=10
345   echo -en "${blue}\rValidating instack VM connectivity${reset}"
346   while ! ping -c 1 $UNDERCLOUD > /dev/null && [ $CNT -gt 0 ]; do
347       echo -n "."
348       sleep 3
349       CNT=$CNT-1
350   done
351   if [ "$CNT" -eq 0 ]; then
352       echo "Failed to contact Instack. Can Not Continue"
353       exit 1
354   fi
355   CNT=10
356   while ! ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "echo ''" 2>&1> /dev/null && [ $CNT -gt 0 ]; do
357       echo -n "."
358       sleep 3
359       CNT=$CNT-1
360   done
361   if [ "$CNT" -eq 0 ]; then
362       echo "Failed to connect to Instack. Can Not Continue"
363       exit 1
364   fi
365
366   # extra space to overwrite the previous connectivity output
367   echo -e "${blue}\r                                                                 ${reset}"
368
369   #add the instack brbm1 interface
370   virsh attach-interface --domain instack --type network --source brbm1 --model rtl8139 --config --live
371   sleep 1
372   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"
373
374   # ssh key fix for stack user
375   ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "restorecon -r /home/stack"
376 }
377
378 ##Create virtual nodes in virsh
379 ##params: none
380 function setup_virtual_baremetal {
381   for i in $(seq 0 $vm_index); do
382     if ! virsh list --all | grep baremetalbrbm_brbm1_${i} > /dev/null; then
383       if [ ! -e $CONFIG/baremetalbrbm_brbm1_${i}.xml ]; then
384         define_virtual_node baremetalbrbm_brbm1_${i}
385       fi
386       virsh define $CONFIG/baremetalbrbm_brbm1_${i}.xml
387     else
388       echo "Found Baremetal ${i} VM, using existing VM"
389     fi
390     virsh vol-list default | grep baremetalbrbm_brbm1_${i} 2>&1> /dev/null || virsh vol-create-as default baremetalbrbm_brbm1_${i}.qcow2 40G --format qcow2
391   done
392
393 }
394
395 ##Copy over the glance images and instack json file
396 ##params: none
397 function copy_materials_to_instack {
398
399   echo
400   echo "Copying configuration file and disk images to instack"
401   scp ${SSH_OPTIONS[@]} $RESOURCES/overcloud-full.qcow2 "stack@$UNDERCLOUD":
402   scp ${SSH_OPTIONS[@]} $NETENV "stack@$UNDERCLOUD":
403   scp ${SSH_OPTIONS[@]} $CONFIG/opendaylight.yaml "stack@$UNDERCLOUD":
404   scp ${SSH_OPTIONS[@]} -r $CONFIG/nics/ "stack@$UNDERCLOUD":
405
406   if [[ ${#deploy_options_array[@]} -eq 0 || ${deploy_options_array['sdn_controller']} == 'opendaylight' ]]; then
407     DEPLOY_OPTIONS+=" -e opendaylight.yaml"
408     ## WORK AROUND
409     # when OpenDaylight lands in upstream RDO manager this can be removed
410     # apply the opendaylight patch
411     scp ${SSH_OPTIONS[@]} $CONFIG/opendaylight.patch "root@$UNDERCLOUD":
412     ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cd /usr/share/openstack-tripleo-heat-templates/; patch -Np1 < /root/opendaylight.patch"
413     ## END WORK AROUND
414   elif [ ${deploy_options_array['sdn_controller']} == 'onos' ]; then
415     echo -e "${red}ERROR: ONOS is currently unsupported...exiting${reset}"
416     exit 1
417   elif [ ${deploy_options_array['sdn_controller']} == 'opencontrail' ]; then
418     echo -e "${red}ERROR: OpenContrail is currently unsupported...exiting${reset}"
419     exit 1
420   fi
421
422   # ensure stack user on instack machine has an ssh key
423   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "if [ ! -e ~/.ssh/id_rsa.pub ]; then ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa; fi"
424
425   if [ "$virtual" == "TRUE" ]; then
426
427       # copy the instack vm's stack user's pub key to
428       # root's auth keys so that instack can control
429       # vm power on the hypervisor
430       ssh ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> /root/.ssh/authorized_keys
431
432       # fix MACs to match new setup
433       for i in $(seq 0 $vm_index); do
434         pyscript="import json
435 data = json.load(open('$CONFIG/instackenv-virt.json'))
436 print data['nodes'][$i]['mac'][0]"
437
438         old_mac=$(python -c "$pyscript")
439         new_mac=$(virsh dumpxml baremetalbrbm_brbm1_$i | grep "mac address" | cut -d = -f2 | grep -Eo "[0-9a-f:]+")
440         # this doesn't work with multiple vnics on the vms
441         #if [ "$old_mac" != "$new_mac" ]; then
442         #  echo "${blue}Modifying MAC for node from $old_mac to ${new_mac}${reset}"
443         #  sed -i 's/'"$old_mac"'/'"$new_mac"'/' $CONFIG/instackenv-virt.json
444         #fi
445       done
446
447       DEPLOY_OPTIONS+=" --libvirt-type qemu"
448       INSTACKENV=$CONFIG/instackenv-virt.json
449       NETENV=$CONFIG/network-environment.yaml
450
451       # upload instackenv file to Instack for virtual deployment
452       scp ${SSH_OPTIONS[@]} $INSTACKENV "stack@$UNDERCLOUD":instackenv.json
453   fi
454
455   # allow stack to control power management on the hypervisor via sshkey
456   # only if this is a virtual deployment
457   if [ "$virtual" == "TRUE" ]; then
458       ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
459 while read -r line; do
460   stack_key=\${stack_key}\\\\\\\\n\${line}
461 done < <(cat ~/.ssh/id_rsa)
462 stack_key=\$(echo \$stack_key | sed 's/\\\\\\\\n//')
463 sed -i 's~INSERT_STACK_USER_PRIV_KEY~'"\$stack_key"'~' instackenv.json
464 EOI
465   fi
466
467 # copy stack's ssh key to this users authorized keys
468 ssh -T ${SSH_OPTIONS[@]} "root@$UNDERCLOUD" "cat /home/stack/.ssh/id_rsa.pub" >> ~/.ssh/authorized_keys
469 }
470
471 ##preping it for deployment and launch the deploy
472 ##params: none
473 function undercloud_prep_overcloud_deploy {
474 # configure undercloud on Undercloud VM
475 ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" "openstack undercloud install > apex-undercloud-install.log"
476
477   # check if HA is enabled
478   if [ $ha_enabled == "TRUE" ]; then
479      DEPLOY_OPTIONS+=" --control-scale 3 --compute-scale 2"
480      DEPLOY_OPTIONS+=" -e /usr/share/openstack-tripleo-heat-templates/environments/puppet-pacemaker.yaml"
481      DEPLOY_OPTIONS+=" --ntp-server $ntp_server"
482   fi
483
484   if [ $net_isolation_enabled == "TRUE" ]; then
485      DEPLOY_OPTIONS+=" -e /usr/share/openstack-tripleo-heat-templates/environments/network-isolation.yaml"
486      DEPLOY_OPTIONS+=" -e network-environment.yaml"
487   fi
488
489   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
490 source stackrc
491 set -o errexit
492 echo "Uploading overcloud glance images"
493 openstack overcloud image upload
494 echo "Configuring undercloud and discovering nodes"
495 openstack baremetal import --json instackenv.json
496 openstack baremetal configure boot
497 openstack baremetal introspection bulk start
498 echo "Configuring flavors"
499 openstack flavor list | grep baremetal || openstack flavor create --id auto --ram 4096 --disk 39 --vcpus 1 baremetal
500 openstack flavor set --property "cpu_arch"="x86_64" --property "capabilities:boot_option"="local" baremetal
501 echo "Configuring nameserver on ctlplane network"
502 neutron subnet-update \$(neutron subnet-list | grep -v id | grep -v \\\\-\\\\- | awk {'print \$2'}) --dns-nameserver 8.8.8.8
503 echo "Executing overcloud deployment, this should run for an extended period without output."
504 sleep 60 #wait for Hypervisor stats to check-in to nova
505 openstack overcloud deploy --templates $DEPLOY_OPTIONS
506 EOI
507
508 }
509
510 display_usage() {
511   echo -e "Usage:\n$0 [arguments] \n"
512   echo -e "   -c|--config : Directory to configuration files. Optional.  Defaults to /var/opt/opnfv/ \n"
513   echo -e "   -d|--deploy-settings : Full path to deploy settings yaml file. Optional.  Defaults to null \n"
514   echo -e "   -i|--inventory : Full path to inventory yaml file. Required only for baremetal \n"
515   echo -e "   -n|--netenv : Full path to network environment file. Optional. Defaults to \$CONFIG/network-environment.yaml \n"
516   echo -e "   -p|--ping-site : site to use to verify IP connectivity. Optional. Defaults to 8.8.8.8 \n"
517   echo -e "   -r|--resources : Directory to deployment resources. Optional.  Defaults to /var/opt/opnfv/stack \n"
518   echo -e "   -v|--virtual : Virtualize overcloud nodes instead of using baremetal. \n"
519   echo -e "   --no-ha : disable High Availability deployment scheme, this assumes a single controller and single compute node \n"
520   echo -e "   --flat : disable Network Isolation and use a single flat network for the underlay network."
521 }
522
523 ##translates the command line parameters into variables
524 ##params: $@ the entire command line is passed
525 ##usage: parse_cmd_line() "$@"
526 parse_cmdline() {
527   echo -e "\n\n${blue}This script is used to deploy the Apex Installer and Provision OPNFV Target System${reset}\n\n"
528   echo "Use -h to display help"
529   sleep 2
530
531   while [ "${1:0:1}" = "-" ]
532   do
533     case "$1" in
534         -h|--help)
535                 display_usage
536                 exit 0
537             ;;
538         -c|--config)
539                 CONFIG=$2
540                 echo "Deployment Configuration Directory Overridden to: $2"
541                 shift 2
542             ;;
543         -d|--deploy-settings)
544                 DEPLOY_SETTINGS_FILE=$2
545                 echo "Deployment Configuration file: $2"
546                 shift 2
547             ;;
548         -i|--inventory)
549                 INVENTORY_FILE=$2
550                 shift 2
551             ;;
552         -n|--netenv)
553                 NETENV=$2
554                 shift 2
555             ;;
556         -p|--ping-site)
557                 ping_site=$2
558                 echo "Using $2 as the ping site"
559                 shift 2
560             ;;
561         -r|--resources)
562                 RESOURCES=$2
563                 echo "Deployment Resources Directory Overridden to: $2"
564                 shift 2
565             ;;
566         -v|--virtual)
567                 virtual="TRUE"
568                 echo "Executing a Virtual Deployment"
569                 shift 1
570             ;;
571         --no-ha )
572                 ha_enabled="FALSE"
573                 echo "HA Deployment Disabled"
574                 shift 1
575             ;;
576         --flat )
577                 net_isolation_enabled="FALSE"
578                 echo "Underlay Network Isolation Disabled: using flat configuration"
579                 shift 1
580             ;;
581         *)
582                 display_usage
583                 exit 1
584             ;;
585     esac
586   done
587
588   if [[ ! -z "$NETENV" && "$net_isolation_enabled" == "FALSE" ]]; then
589     echo -e "{red}WARN: Single flat network requested, but netenv specified.  Ignoring netenv settings!${reset}"
590   elif [[ ! -z "$NETENV" && ! -z "$DEPLOY_SETTINGS_FILE" ]]; then
591     echo -e "${red}WARN: deploy_settings and netenv specified.  Ignoring netenv settings! deploy_settings will contain \
592 netenv${reset}"
593   fi
594
595   if [[ -n "$virtual" && -n "$INVENTORY_FILE" ]]; then
596     echo -e "${red}ERROR: You should not specify an inventory with virtual deployments${reset}"
597     exit 1
598   fi
599
600   if [[ ! -z "$DEPLOY_SETTINGS_FILE" && ! -f "$DEPLOY_SETTINGS_FILE" ]]; then
601     echo -e "${red}ERROR: ${DEPLOY_SETTINGS_FILE} does not exist! Exiting...${reset}"
602     exit 1
603   fi
604
605   if [[ ! -z "$NETENV" && ! -f "$NETENV" ]]; then
606     echo -e "${red}ERROR: ${NETENV} does not exist! Exiting...${reset}"
607     exit 1
608   fi
609
610   if [[ ! -z "$INVENTORY_FILE" && ! -f "$INVENTORY_FILE" ]]; then
611     echo -e "{$red}ERROR: ${DEPLOY_SETTINGS_FILE} does not exist! Exiting...${reset}"
612     exit 1
613   fi
614
615   if [[ -z "$virtual" && -z "$INVENTORY_FILE" ]]; then
616     echo -e "${red}ERROR: You must specify an inventory file for baremetal deployments! Exiting...${reset}"
617     exit 1
618   fi
619 }
620
621 ##END FUNCTIONS
622
623 main() {
624   parse_cmdline "$@"
625   if ! configure_deps; then
626     echo "Dependency Validation Failed, Exiting."
627   fi
628   if [ -n "$DEPLOY_SETTINGS_FILE" ]; then
629     parse_deploy_settings
630   fi
631   setup_instack_vm
632   if [ "$virtual" == "TRUE" ]; then
633     setup_virtual_baremetal
634   elif [ -n "$INVENTORY_FILE" ]; then
635     parse_inventory_file
636   fi
637   copy_materials_to_instack
638   undercloud_prep_overcloud_deploy
639 }
640
641 main "$@"