ef388f44e5b2ea3214eac9aa518aa18ef976d6b4
[apex.git] / lib / parse-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 # Parser functions used by OPNFV Apex
12
13 ##translates yaml into variables
14 ##params: filename, prefix (ex. "config_")
15 ##usage: parse_yaml opnfv_ksgen_settings.yml "config_"
16 parse_yaml() {
17    local prefix=$2
18    local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
19    sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
20         -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
21    awk -F$fs '{
22       indent = length($1)/2;
23       vname[indent] = $2;
24       for (i in vname) {if (i > indent) {delete vname[i]}}
25       if (length($3) > 0) {
26          vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
27          printf("%s%s%s=%s\n", "'$prefix'",vn, $2, $3);
28       }
29    }'
30 }
31
32 ##parses variable from a string with '='
33 ##and removes global prefix
34 ##params: string, prefix
35 ##usage: parse_setting_var 'deploy_myvar=2' 'deploy_'
36 parse_setting_var() {
37   local mystr=$1
38   local prefix=$2
39   if echo $mystr | grep -E "^.+\=" > /dev/null; then
40     echo $(echo $mystr | grep -Eo "^.+\=" | tr -d '=' |  sed 's/^'"$prefix"'//')
41   else
42     return 1
43   fi
44 }
45 ##parses value from a string with '='
46 ##params: string
47 ##usage: parse_setting_value
48 parse_setting_value() {
49   local mystr=$1
50   echo $(echo $mystr | grep -Eo "\=.*$" | tr -d '=')
51 }
52
53 ##parses network settings yaml into globals
54 parse_network_settings() {
55   local output parse_ext
56   parse_ext=''
57
58   if [[ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' || "${deploy_options_array['dataplane']}" == 'fdio' ]]; then
59       for val in ${performance_roles[@]}; do
60         if [ "$val" == "Compute" ]; then
61           parse_ext="${parse_ext} --compute-pre-config "
62         elif [ "$val" == "Controller" ]; then
63           parse_ext="${parse_ext} --controller-pre-config "
64         fi
65       done
66   fi
67
68   if output=$(python3 -B $LIB/python/apex_python_utils.py parse-net-settings -s $NETSETS $net_isolation_arg -td $APEX_TMP_DIR -e $CONFIG/network-environment.yaml $parse_ext); then
69       echo -e "${blue}${output}${reset}"
70       eval "$output"
71   else
72       echo -e "${red}ERROR: Failed to parse network settings file $NETSETS ${reset}"
73       exit 1
74   fi
75
76   if [ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' ]; then
77     if [ "$net_isolation_enabled" == "FALSE" ]; then
78       echo -e "${red}ERROR: flat network is not supported with ovs-dpdk ${reset}"
79       exit 1
80     fi
81     if [[ ! $enabled_network_list =~ "private_network" ]]; then
82       echo -e "${red}ERROR: tenant network is not enabled for ovs-dpdk ${reset}"
83       exit 1
84     fi
85   fi
86 }
87
88 ##parses deploy settings yaml into globals
89 parse_deploy_settings() {
90   local output
91   if output=$(python3 -B $LIB/python/apex_python_utils.py parse-deploy-settings -f $DEPLOY_SETTINGS_FILE); then
92       echo -e "${blue}${output}${reset}"
93       eval "$output"
94   else
95       echo -e "${red}ERROR: Failed to parse deploy settings file $DEPLOY_SETTINGS_FILE ${reset}"
96       exit 1
97   fi
98
99 }
100
101 ##parses baremetal yaml settings into compatible json
102 ##writes the json to undercloud:instackenv.json
103 ##params: none
104 ##usage: parse_inventory_file
105 parse_inventory_file() {
106   if [ "$virtual" == "TRUE" ]; then inv_virt="--virtual"; fi
107   if [[ "$ha_enabled" == "True" ]]; then inv_ha="--ha"; fi
108   instackenv_output=$(python3 -B $LIB/python/apex_python_utils.py parse-inventory -f $INVENTORY_FILE $inv_virt $inv_ha)
109   #Copy instackenv.json to undercloud
110   echo -e "${blue}Parsed instackenv JSON:\n${instackenv_output}${reset}"
111   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
112 cat > instackenv.json << EOF
113 $instackenv_output
114 EOF
115 EOI
116
117 }