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