dde9041a67a22b9d7ff0d62442f8414c8d275e9c
[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
56   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); then
57       echo -e "${blue}${output}${reset}"
58       eval "$output"
59   else
60       echo -e "${red}ERROR: Failed to parse network settings file $NETSETS ${reset}"
61       exit 1
62   fi
63 }
64
65 ##parses deploy settings yaml into globals
66 parse_deploy_settings() {
67   local output
68   if output=$(python3.4 -B $LIB/python/apex_python_utils.py parse-deploy-settings -f $DEPLOY_SETTINGS_FILE); then
69       echo -e "${blue}${output}${reset}"
70       eval "$output"
71   else
72       echo -e "${red}ERROR: Failed to parse deploy settings file $DEPLOY_SETTINGS_FILE ${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 baremetal yaml settings into compatible json
89 ##writes the json to $CONFIG/instackenv_tmp.json
90 ##params: none
91 ##usage: parse_inventory_file
92 parse_inventory_file() {
93   local inventory=$(parse_yaml $INVENTORY_FILE)
94   local node_list
95   local node_prefix="node"
96   local node_count=0
97   local node_total
98   local inventory_list
99
100   # detect number of nodes
101   for entry in $inventory; do
102     if echo $entry | grep -Eo "^nodes_node[0-9]+_" > /dev/null; then
103       this_node=$(echo $entry | grep -Eo "^nodes_node[0-9]+_")
104       if [[ "$inventory_list" != *"$this_node"* ]]; then
105         inventory_list+="$this_node "
106       fi
107     fi
108   done
109
110   inventory_list=$(echo $inventory_list | sed 's/ $//')
111
112   for node in $inventory_list; do
113     ((node_count+=1))
114   done
115
116   node_total=$node_count
117
118   if [[ "$node_total" -lt 5 && "$ha_enabled" == "True" ]]; then
119     echo -e "${red}ERROR: You must provide at least 5 nodes for HA baremetal deployment${reset}"
120     exit 1
121   elif [[ "$node_total" -lt 2 ]]; then
122     echo -e "${red}ERROR: You must provide at least 2 nodes for non-HA baremetal deployment${reset}"
123     exit 1
124   fi
125
126   eval $(parse_yaml $INVENTORY_FILE) || {
127     echo "${red}Failed to parse inventory.yaml. Aborting.${reset}"
128     exit 1
129   }
130
131   instackenv_output="
132 {
133  \"nodes\" : [
134
135 "
136   node_count=0
137   for node in $inventory_list; do
138     ((node_count+=1))
139     node_output="
140         {
141           \"pm_password\": \"$(eval echo \${${node}ipmi_pass})\",
142           \"pm_type\": \"$(eval echo \${${node}pm_type})\",
143           \"mac\": [
144             \"$(eval echo \${${node}mac_address})\"
145           ],
146           \"cpu\": \"$(eval echo \${${node}cpus})\",
147           \"memory\": \"$(eval echo \${${node}memory})\",
148           \"disk\": \"$(eval echo \${${node}disk})\",
149           \"arch\": \"$(eval echo \${${node}arch})\",
150           \"pm_user\": \"$(eval echo \${${node}ipmi_user})\",
151           \"pm_addr\": \"$(eval echo \${${node}ipmi_ip})\",
152           \"capabilities\": \"$(eval echo \${${node}capabilities})\"
153 "
154     instackenv_output+=${node_output}
155     if [ $node_count -lt $node_total ]; then
156       instackenv_output+="        },"
157     else
158       instackenv_output+="        }"
159     fi
160   done
161
162   instackenv_output+='
163   ]
164 }
165 '
166   #Copy instackenv.json to undercloud for baremetal
167   echo -e "{blue}Parsed instackenv JSON:\n${instackenv_output}${reset}"
168   ssh -T ${SSH_OPTIONS[@]} "stack@$UNDERCLOUD" <<EOI
169 cat > instackenv.json << EOF
170 $instackenv_output
171 EOF
172 EOI
173
174 }