Skip to content

Commit

Permalink
Merge pull request #187 from MsysTechnologiesllc/harry/parse-env-var
Browse files Browse the repository at this point in the history
add python script to parse env variables
  • Loading branch information
btm authored Jan 27, 2017
2 parents c5ea48b + b6e3ddd commit fdc12eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
9 changes: 9 additions & 0 deletions ChefExtensionHandler/bin/parse_env_variables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import json
import sys
with open(sys.argv[1]) as data_file:
data = json.load(data_file, strict=False)
data = data['runtimeSettings'][0]['handlerSettings']['publicSettings']['environment_variables']
commands=""
for key, value in data.items():
commands=commands+'export '+key+'="'+value+'";'
print commands
21 changes: 8 additions & 13 deletions ChefExtensionHandler/bin/shared.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,29 @@ get_config_settings_file() {
echo $config_file_name
}

extract_environment_variables_list() {
env_vars_list=$(sed ':a;N;$!ba;s/\n//g' $1 | tr -d ' ' | sed 's/.*environment_variables":{\(.*\)}/\1/g' | awk -F\} '{print $1}' | awk '{n=split($0,arr,",");for(i=1;i<=n;i++) print arr[i]}')

echo $env_vars_list
get_file_path_to_parse_env_variables(){
path_to_parse_env_variables="$1/bin/parse_env_variables.py"
echo $path_to_parse_env_variables
}

export_env_vars() {
eval "export $1=$2"
commands="`python $path_to_parse_env_variables \"$1\"`"
eval $commands
}

read_environment_variables(){
echo "[$(date)] Reading environment variables"
config_file_name=$(get_config_settings_file $1)
path_to_parse_env_variables=$(get_file_path_to_parse_env_variables $1)

if [ -z "$config_file_name" ]; then
echo "Configuration error. Azure chef extension's config/settings file missing."
exit 1
else
if cat $config_file_name 2>/dev/null | grep -q "environment_variables"; then
env_vars_list=$(extract_environment_variables_list $config_file_name)

for i in $env_vars_list
do
env_var_name=$(echo $i | awk -F':' '{print $1}')
env_var_value=$(echo $i | awk -F':' '{OFS=":";$1="";print $0}' | sed 's/^:*//g;s/^ *//g;s/ *$//g')
export_env_vars $env_var_name $env_var_value
done
export_env_vars $config_file_name
echo "[$(date)] Environment variables read operation completed"
echo "`env`"
else
echo "[$(date)] No environment variables found"
fi
Expand Down
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PESTER_SANDBOX = './PESTER_SANDBOX'
LINUX_PACKAGE_LIST = [
{"ChefExtensionHandler/*.sh" => "#{CHEF_BUILD_DIR}/"},
{"ChefExtensionHandler/bin/*.sh" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.py" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/*.rb" => "#{CHEF_BUILD_DIR}/bin"},
{"ChefExtensionHandler/bin/chef-client" => "#{CHEF_BUILD_DIR}/bin"},
{"*.gem" => "#{CHEF_BUILD_DIR}/gems"},
Expand Down

0 comments on commit fdc12eb

Please sign in to comment.