Adds configuring vpp/hc on all nodes
[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   for val in ${performance_roles[@]}; do
59     if [ "$val" == "Compute" ]; then
60       parse_ext="${parse_ext} --compute-pre-config "
61     elif [ "$val" == "Controller" ]; then
62       parse_ext="${parse_ext} --controller-pre-config "
63     fi
64   done
65
66   if output=$(python3.4 -B $LIB/python/apex_python_utils.py parse-net-settings -s $NETSETS -i $net_isolation_enabled -e $CONFIG/network-environment.yaml $parse_ext); then
67       echo -e "${blue}${output}${reset}"
68       eval "$output"
69   else
70       echo -e "${red}ERROR: Failed to parse network settings file $NETSETS ${reset}"
71       exit 1
72   fi
73 }
74
75 ##parses deploy settings yaml into globals
76 parse_deploy_settings() {
77   local output
78   if output=$(python3.4 -B $LIB/python/apex_python_utils.py parse-deploy-settings -f $DEPLOY_SETTINGS_FILE); then
79       echo -e "${blue}${output}${reset}"
80       eval "$output"
81   else
82       echo -e "${red}ERROR: Failed to parse deploy settings file $DEPLOY_SETTINGS_FILE ${reset}"
83       exit 1
84   fi
85
86   if [ "${deploy_options_array['dataplane']}" == 'ovs_dpdk' ]; then
87     if [ "$net_isolation_enabled" == "FALSE" ]; then
88       echo -e "${red}ERROR: flat network is not supported with ovs-dpdk ${reset}"
89       exit 1
90     fi
91     if [[ ! $enabled_network_list =~ "private_network" ]]; then
92       echo -e "${red}ERROR: tenant network is not enabled for ovs-dpdk ${reset}"
93       exit 1
94     fi
95   fi
96 }
97
98 ##parses baremetal yaml settings into compatible json
99 ##writes the json to $CONFIG/instackenv_tmp.json
100 ##params: none
101 ##usage: parse_inventory_file
102 parse_inventory_file() {
103   local inventory=$(parse_yaml $INVENTORY_FILE)
104   local node_list
105   local node_prefix="node"
106   local node_count=0
107   local node_total
108   local inventory_list
109
110   # detect number of nodes
111   for entry in $inventory; do
112     if echo $entry | grep -Eo "^nodes_node[0-9]+_" > /dev/null; then
113       this_node=$(echo $entry | grep -Eo "^nodes_node[0-9]+_")
114       if [[ "$inventory_list" != *"$this_node"* ]]; then
115         inventory_list+="$this_node "
116       fi
117     fi
118   done
119
120   inventory_list=$(echo $inventory_list | sed 's/ $//')
121
122   for node in $inventory_list; do
123     ((node_count+=1))
124   done
125
126   node_total=$node_count
127
128   if [[ "$node_total" -lt 5 && "$ha_enabled" == "True" ]]; then
129     echo -e "${red}ERROR: You must provide at least 5 nodes for HA baremetal deployment${reset}"
130     exit 1
131   elif [[ "$node_total" -lt 2 ]]; then
132     echo -e "${red}ERROR: You must provide at least 2 nodes for non-HA baremetal deployment${reset}"
133     exit 1
134   fi
135
136   eval $(parse_yaml $INVENTORY_FILE) || {
137     echo "${red}Failed to parse inventory.yaml. Aborting.${reset}"
138     exit 1
139   }
140
141   instackenv_output="
142 {
143  \"nodes\" : [
144
145 "
146   node_count=0
147   for node in $inventory_list; do
148     ((node_count+=1))
149     node_output="
150         {
151           \"pm_password\": \"$(eval echo \${${node}ipmi_pass})\",
152           \"pm_type\": \"$(eval echo \${${node}pm_type})\",
153           \"mac\": [
154             \"$(eval echo \${${node}mac_address})\"
155           ],
156           \"cpu\": \"$(eval echo \${${node}cpus})\",
157           \"memory\": \"$(eval echo \${${node}memory})\",
158           \"disk\": \"$(eval echo \${${node}disk})\",
159           \"arch\": \"$(eval echo \${${node}arch})\",
160           \"pm_user\": \"$(eval echo \${${node}ipmi_user})\",
161           \"pm_addr\": \"$(eval echo \${${node}ipmi_ip})\",
162           \"capabilities\": \"$(eval echo \${${node}capabilities})\"
163 "
164     instackenv_output+=${node_output}
165     if [ $node_count -lt $node_total ]; then
166       instackenv_output+="        },"
167     else
168       instackenv_output+="        }"
169     fi
170   done
171
172   instackenv_output+='
173   ]
174 }
175 '
176   #Copy instackenv.json to undercloud for baremetal
177   echo -e "{blue}Parsed instackenv JSON:\n${instackenv_output}${reset}"
178   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
179 cat > instackenv.json << EOF
180 $instackenv_output
181 EOF
182 EOI
183
184 }