|
| 1 | +#!/bin/bash |
| 2 | +set -eo pipefail |
| 3 | +usage() |
| 4 | +{ |
| 5 | +cat << EOF |
| 6 | +usage: $0 options |
| 7 | +
|
| 8 | +This script needs to be executed with below options. |
| 9 | +
|
| 10 | +OPTIONS: |
| 11 | + -e environment |
| 12 | + -t type appenv,appconf and appjson |
| 13 | + -p parameter store path without final slash |
| 14 | + -l parameter store list without final slash |
| 15 | +
|
| 16 | +EOF |
| 17 | +} |
| 18 | + |
| 19 | +create_env_file_format() |
| 20 | +{ |
| 21 | + file_name=$1 |
| 22 | + fetch_path=$2 |
| 23 | + echo $fetch_path |
| 24 | + echo $file_name |
| 25 | + aws ssm get-parameters-by-path --with-decryption --path $fetch_path --query "Parameters[*].{Name:Name, Value:Value}" >fetched_parameters.json |
| 26 | + cat fetched_parameters.json | jq -r '.[] | "export " + .Name + "=\"" + .Value + "\"" ' | sed -e "s~$fetch_path/~~" >${file_name}_env |
| 27 | + rm -rf fetched_parameters.json |
| 28 | +} |
| 29 | + |
| 30 | +create_conf_file_format() |
| 31 | +{ |
| 32 | + file_name=$1 |
| 33 | + fetch_path=$2 |
| 34 | + aws ssm get-parameters-by-path --with-decryption --path $fetch_path --query "Parameters[*].{Name:Name, Value:Value}" >fetched_parameters.json |
| 35 | + cat fetched_parameters.json | jq -r '.[] | .Name + "=\"" + .Value + "\"" ' | sed -e "s~$fetch_path/~~" >${file_name}.conf |
| 36 | + rm -rf fetched_parameters.json |
| 37 | +} |
| 38 | + |
| 39 | +create_json_file_format() |
| 40 | +{ |
| 41 | + file_name=$1 |
| 42 | + fetch_path=$2 |
| 43 | + echo $fetch_path |
| 44 | + echo $file_name |
| 45 | + echo "aws ssm get-parameters-by-path --with-decryption --path $fetch_path --query \"Parameters[*].{Name:Name, Value:Value}\"" |
| 46 | + aws ssm get-parameters-by-path --with-decryption --path $fetch_path --query "Parameters[*].{Name:Name, Value:Value}" >fetched_parameters.json |
| 47 | + cat fetched_parameters.json | jq -r ' . |= (map({ (.Name): .Value }) | add)' | sed -e "s~$fetch_path/~~" >${file_name}.json |
| 48 | +# rm -rf fetched_parameters.json |
| 49 | +} |
| 50 | + |
| 51 | +fetching_specific_path() |
| 52 | +{ |
| 53 | + type_to_fetch=$1 |
| 54 | + PS_PATH=${PS_PATH%/} |
| 55 | + fname=${PS_PATH##*/} |
| 56 | + fpath=$PS_PATH |
| 57 | + echo $fpath |
| 58 | + echo $PS_PATH |
| 59 | + if [ "$type_to_fetch" == "appenv" ] |
| 60 | + then |
| 61 | + create_env_file_format $fname $fpath |
| 62 | + fi |
| 63 | + if [ "$type_to_fetch" == "appconf" ] |
| 64 | + then |
| 65 | + create_conf_file_format $fname $fpath |
| 66 | + fi |
| 67 | + if [ "$type_to_fetch" == "appjson" ] |
| 68 | + then |
| 69 | + create_json_file_format $fname $fpath |
| 70 | + fi |
| 71 | +} |
| 72 | + |
| 73 | +fetching_multiple_path() |
| 74 | +{ |
| 75 | + type_to_fetch=$1 |
| 76 | + Buffer_seclist=$(echo $PS_PATH_LIST | sed 's/,/ /g' ) |
| 77 | + for listname in $Buffer_seclist; |
| 78 | + do |
| 79 | + listname=${listname%/} |
| 80 | + fname=${listname##*/} |
| 81 | + fpath=$listname |
| 82 | + if [ "$type_to_fetch" == "appenv" ] |
| 83 | + then |
| 84 | + create_env_file_format $fname $fpath |
| 85 | + fi |
| 86 | + if [ "$type_to_fetch" == "appconf" ] |
| 87 | + then |
| 88 | + create_conf_file_format $fname $fpath |
| 89 | + fi |
| 90 | + if [ "$type_to_fetch" == "appjson" ] |
| 91 | + then |
| 92 | + create_json_file_format $fname $fpath |
| 93 | + fi |
| 94 | + done |
| 95 | +} |
| 96 | + |
| 97 | + |
| 98 | +while getopts .t:e:p:l:. OPTION |
| 99 | +do |
| 100 | + case $OPTION in |
| 101 | + e) |
| 102 | + ENV=$OPTARG |
| 103 | + ;; |
| 104 | + t) |
| 105 | + APP_TYPE=$OPTARG |
| 106 | + ;; |
| 107 | + p) |
| 108 | + PS_PATH=$OPTARG |
| 109 | + ;; |
| 110 | + l) |
| 111 | + PS_PATH_LIST=$OPTARG |
| 112 | + ;; |
| 113 | + ?) |
| 114 | + log "additional param required" |
| 115 | + usage |
| 116 | + exit |
| 117 | + ;; |
| 118 | + esac |
| 119 | +done |
| 120 | + |
| 121 | +ENV_CONFIG=`echo "$ENV" | tr '[:upper:]' '[:lower:]'` |
| 122 | +APP_TYPE_LOWERCASE=`echo "$APP_TYPE" | tr '[:upper:]' '[:lower:]'` |
| 123 | + |
| 124 | +echo "APP_TYPE: $APP_TYPE_LOWERCASE" |
| 125 | +echo "PS_PATH: $PS_PATH" |
| 126 | +echo "PS_PATH_LIST: $PS_PATH_LIST" |
| 127 | + |
| 128 | +if [ "$APP_TYPE_LOWERCASE" == "appenv" ] |
| 129 | +then |
| 130 | + echo "env configuration" |
| 131 | + if [ -z $PS_PATH ]; |
| 132 | + then |
| 133 | + echo "Info: no ps path" |
| 134 | + else |
| 135 | + fetching_specific_path $APP_TYPE_LOWERCASE |
| 136 | + fi |
| 137 | + if [ -z $PS_PATH_LIST ]; |
| 138 | + then |
| 139 | + echo "Info: no path list" |
| 140 | + else |
| 141 | + fetching_multiple_path $APP_TYPE_LOWERCASE |
| 142 | + fi |
| 143 | +fi |
| 144 | + |
| 145 | +if [ "$APP_TYPE_LOWERCASE" == "appconf" ] |
| 146 | +then |
| 147 | + echo "conf file configuration" |
| 148 | + if [ -z $PS_PATH ]; |
| 149 | + then |
| 150 | + echo "Info: no ps path" |
| 151 | + else |
| 152 | + fetching_specific_path $APP_TYPE_LOWERCASE |
| 153 | + fi |
| 154 | + if [ -z $PS_PATH_LIST ]; |
| 155 | + then |
| 156 | + echo "Info: no path list" |
| 157 | + else |
| 158 | + fetching_multiple_path $APP_TYPE_LOWERCASE |
| 159 | + fi |
| 160 | +fi |
| 161 | + |
| 162 | +if [ "$APP_TYPE_LOWERCASE" == "appjson" ] |
| 163 | +then |
| 164 | + echo "json file configuration" |
| 165 | + if [ -z $PS_PATH ]; |
| 166 | + then |
| 167 | + echo "Info: no ps path" |
| 168 | + else |
| 169 | + fetching_specific_path $APP_TYPE_LOWERCASE |
| 170 | + fi |
| 171 | + if [ -z $PS_PATH_LIST ]; |
| 172 | + then |
| 173 | + echo "Info: no path list" |
| 174 | + else |
| 175 | + fetching_multiple_path $APP_TYPE_LOWERCASE |
| 176 | + fi |
| 177 | +fi |
0 commit comments