@@ -20,26 +20,35 @@ K8S_SIGTERM=${K8S_SIGTERM:-0}
20
20
# seconds to wait before sending sigterm to app on exit
21
21
# only used if K8S_SIGTERM=1
22
22
GRACE_PERIOD=6
23
- NODE=$( which node)
23
+ NODE=$( command -v node)
24
24
25
25
# this function checks all arguments given and outputs them. All parameter pairs where key is ending with "password"
26
26
# are replaced with string "<set>" instead of real password (e.g. "--redis-password XYZ" => "--redis-password <set>")
27
27
safe_print_args () {
28
- echo " $@ " | tr ' ' ' \n' | while read item; do
28
+ echo " $@ " | tr ' ' ' \n' | while read -r item; do
29
29
if [ " ${item% password} " != " ${item} " ]; then
30
- echo $item " <set>"
31
- read item;
30
+ printf " %s <set> " " $item "
31
+ read -r item;
32
32
else
33
- echo $item
33
+ printf " %s " " $item "
34
34
fi
35
35
done
36
36
}
37
37
38
+ parse_boolean () {
39
+ # shell true/false values returned here
40
+ if [ " $1 " = " 1" ] || [ " $1 " = " true" ] || [ " $1 " = " yes" ] || [ " $1 " = " on" ]; then
41
+ return 0
42
+ else
43
+ return 1
44
+ fi
45
+ }
46
+
38
47
writeDefaultConfigBeginning () {
39
48
echo " Creating custom redis-commander config '${CONFIG_FILE} '."
40
49
41
50
# =============== generate beginning of redis-commander config =============== #
42
- cat > ${CONFIG_FILE} << EOF
51
+ cat > " ${CONFIG_FILE} " << EOF
43
52
{
44
53
EOF
45
54
# ============= end generate beginning of redis-commander config ============= #
48
57
49
58
writeDefaultConfigEnd () {
50
59
# ================== generate end of redis-commander config ================== #
51
- cat >> ${CONFIG_FILE} << EOF
60
+ cat >> " ${CONFIG_FILE} " << EOF
52
61
}
53
62
EOF
54
63
# ================ end generate end of redis-commander config ================ #
@@ -61,15 +70,15 @@ parseRedisHosts() {
61
70
# split REDIS_HOSTS on comma (,)
62
71
# local:localhost:6379,custom-label:my.hostname
63
72
# -> local:localhost:6379 custom-label:my.hostname
64
- redis_hosts_split=" $( echo ${REDIS_HOSTS} | sed " s/,/ /g" ) "
73
+ redis_hosts_split=" $( echo " ${REDIS_HOSTS} " | sed " s/,/ /g" ) "
65
74
66
75
# get hosts count
67
- num_redis_hosts=" $( echo ${redis_hosts_split} | wc -w) "
76
+ num_redis_hosts=" $( echo " ${redis_hosts_split} " | wc -w) "
68
77
69
78
echo " Parsing $num_redis_hosts REDIS_HOSTS into custom redis-commander config '${CONFIG_FILE} '."
70
79
71
- if [ $num_redis_hosts -gt 0 ]; then
72
- cat >> ${CONFIG_FILE} << EOF
80
+ if [ " $num_redis_hosts " -gt 0 ]; then
81
+ cat >> " ${CONFIG_FILE} " << EOF
73
82
"connections": [
74
83
EOF
75
84
fi
92
101
# split redis_host on colon (:)
93
102
# local:localhost:6379
94
103
# -> local localhost 6379
95
- host_split=" $( echo ${redis_host} | sed " s/:/ /g" ) "
104
+ host_split=" $( echo " ${redis_host} " | sed " s/:/ /g" ) "
96
105
97
106
# get host param count
98
107
num_host_params=" $( echo " ${host_split} " | wc -w) "
@@ -105,31 +114,31 @@ EOF
105
114
106
115
if [ " ${num_host_params} " -eq 1 ]; then
107
116
label=default
108
- host=" $( echo ${host_split} | cut -d" " -f1) "
117
+ host=" $( echo " ${host_split} " | cut -d" " -f1) "
109
118
else
110
- label=" $( echo ${host_split} | cut -d" " -f1) "
111
- host=" $( echo ${host_split} | cut -d" " -f2) "
119
+ label=" $( echo " ${host_split} " | cut -d" " -f1) "
120
+ host=" $( echo " ${host_split} " | cut -d" " -f2) "
112
121
fi
113
122
114
123
[ " ${num_host_params} " -lt 3 ] \
115
124
&& port=6379 \
116
- || port=" $( echo ${host_split} | cut -d" " -f3) "
125
+ || port=" $( echo " ${host_split} " | cut -d" " -f3) "
117
126
118
127
[ " ${num_host_params} " -lt 4 ] \
119
128
&& db_index=0 \
120
- || db_index=" $( echo ${host_split} | cut -d" " -f4) "
129
+ || db_index=" $( echo " ${host_split} " | cut -d" " -f4) "
121
130
122
131
123
132
[ " ${num_host_params} " -lt 5 ] \
124
133
&& password=' ' \
125
- || password=" $( echo ${host_split} | cut -d" " -f5) "
134
+ || password=" $( echo " ${host_split} " | cut -d" " -f5) "
126
135
127
136
[ " ${counter} " -eq " ${num_redis_hosts} " ] \
128
137
&& comma=' ' \
129
138
|| comma=' ,'
130
139
131
140
# generate host config
132
- cat >> ${CONFIG_FILE} << EOF
141
+ cat >> " ${CONFIG_FILE} " << EOF
133
142
{
134
143
"label":"${label} ",
135
144
"host":"${host} ",
141
150
142
151
done
143
152
# ================ end loop on redis hosts and generate config =============== #
144
- if [ $num_redis_hosts -gt 0 ]; then
145
- cat >> ${CONFIG_FILE} << EOF
153
+ if [ " $num_redis_hosts " -gt 0 ]; then
154
+ cat >> " ${CONFIG_FILE} " << EOF
146
155
]
147
156
EOF
148
157
fi
@@ -151,20 +160,20 @@ EOF
151
160
}
152
161
153
162
# if REDIS_HOSTS is set parse it and create custom config
154
- [[ ! -z " ${REDIS_HOSTS} " ] ] && parseRedisHosts
163
+ [ -n " ${REDIS_HOSTS} " ] && parseRedisHosts
155
164
156
165
# Fallback - write default config if not supplied otherwise (already exists or written by
157
166
# parsing REDIS_HOSTS env var)
158
- if [[ ! -e ${CONFIG_FILE} ] ]; then
167
+ if [ ! -e " ${CONFIG_FILE} " ]; then
159
168
writeDefaultConfigBeginning
160
169
writeDefaultConfigEnd
161
170
fi
162
171
163
172
# load HTTP password from a file (e.g. a Docker secret mounted in the container)
164
173
HTTP_PASSWORD_FILE=${HTTP_PASSWORD_FILE:-/ }
165
174
166
- if [ -f $HTTP_PASSWORD_FILE ]; then
167
- HTTP_PASSWORD=$( cat $HTTP_PASSWORD_FILE )
175
+ if [ -f " $HTTP_PASSWORD_FILE " ]; then
176
+ HTTP_PASSWORD=$( cat " $HTTP_PASSWORD_FILE " )
168
177
# this env var is evaluated by node-config module, not set as cli param
169
178
# to not show it in process listing / write to docker logs ...
170
179
export HTTP_PASSWORD
174
183
REDIS_PASSWORD_FILE=${REDIS_PASSWORD_FILE:-/ }
175
184
SENTINEL_PASSWORD_FILE=${SENTINEL_PASSWORD_FILE:-/ }
176
185
177
- if [ -f $REDIS_PASSWORD_FILE ]; then
178
- REDIS_PASSWORD=$( cat $REDIS_PASSWORD_FILE )
186
+ if [ -f " $REDIS_PASSWORD_FILE " ]; then
187
+ REDIS_PASSWORD=$( cat " $REDIS_PASSWORD_FILE " )
179
188
# evaluated below and added to cli params
180
189
export REDIS_PASSWORD
181
190
fi
182
- if [ -f $SENTINEL_PASSWORD_FILE ]; then
183
- SENTINEL_PASSWORD=$( cat $SENTINEL_PASSWORD_FILE )
191
+ if [ -f " $SENTINEL_PASSWORD_FILE " ]; then
192
+ SENTINEL_PASSWORD=$( cat " $SENTINEL_PASSWORD_FILE " )
184
193
# evaluated below and added to cli params
185
194
export SENTINEL_PASSWORD
186
195
fi
191
200
# for an complete list of all other env vars with their mapping
192
201
# see file "config/custom_environment_variables.json"
193
202
194
- if [[ ! -z " $REDIS_PORT " ] ]; then
195
- set -- " $@ " " --redis-port $REDIS_PORT "
203
+ if [ -n " $REDIS_PORT " ]; then
204
+ set -- " $@ " " --redis-port" " $REDIS_PORT "
196
205
fi
197
206
198
- if [[ ! -z " $REDIS_HOST " ] ]; then
199
- set -- " $@ " " --redis-host $REDIS_HOST "
207
+ if [ -n " $REDIS_HOST " ]; then
208
+ set -- " $@ " " --redis-host" " $REDIS_HOST "
200
209
fi
201
210
202
- if [[ ! -z " $REDIS_SOCKET " ] ]; then
203
- set -- " $@ " " --redis-socket $REDIS_SOCKET "
211
+ if [ -n " $REDIS_SOCKET " ]; then
212
+ set -- " $@ " " --redis-socket" " $REDIS_SOCKET "
204
213
fi
205
214
206
- if [[ ! -z " $REDIS_TLS " ]]; then
207
- if [[ " $REDIS_TLS " = " 1" || " $REDIS_TLS " = " true" || " $REDIS_TLS " = " yes" || " $REDIS_TLS " = " on" ]]; then
208
- set -- " $@ " " --redis-tls"
209
- fi
215
+ if [ -n " $REDIS_TLS " ] && parse_boolean " $REDIS_TLS " ; then
216
+ set -- " $@ " " --redis-tls"
210
217
fi
211
218
212
- if [[ ! -z " $REDIS_PASSWORD " ] ]; then
213
- set -- " $@ " " --redis-password $REDIS_PASSWORD "
219
+ if [ -n " $REDIS_PASSWORD " ]; then
220
+ set -- " $@ " " --redis-password" " $REDIS_PASSWORD "
214
221
fi
215
222
216
- if [[ ! -z " $REDIS_DB " ] ]; then
217
- set -- " $@ " " --redis-db $REDIS_DB "
223
+ if [ -n " $REDIS_DB " ]; then
224
+ set -- " $@ " " --redis-db" " $REDIS_DB "
218
225
fi
219
226
220
- if [[ ! -z " $REDIS_OPTIONAL " ]]; then
221
- if [[ " $REDIS_OPTIONAL " = " 1" || " $REDIS_OPTIONAL " = " true" || " $REDIS_OPTIONAL " = " yes" || " $REDIS_OPTIONAL " = " on" ]]; then
222
- set -- " $@ " " --redis-optional"
223
- fi
227
+ if [ -n " $REDIS_OPTIONAL " ] && parse_boolean " $REDIS_OPTIONAL " ; then
228
+ set -- " $@ " " --redis-optional"
224
229
fi
225
230
226
- if [[ ! -z " $SENTINEL_PORT " ] ]; then
227
- set -- " $@ " " --sentinel-port $SENTINEL_PORT "
231
+ if [ -n " $SENTINEL_PORT " ]; then
232
+ set -- " $@ " " --sentinel-port" " $SENTINEL_PORT "
228
233
fi
229
234
230
- if [[ ! -z " $SENTINEL_HOST " ] ]; then
231
- set -- " $@ " " --sentinel-host $SENTINEL_HOST "
235
+ if [ -n " $SENTINEL_HOST " ]; then
236
+ set -- " $@ " " --sentinel-host" " $SENTINEL_HOST "
232
237
fi
233
238
234
- if [[ ! -z " $SENTINELS " ] ]; then
235
- set -- " $@ " " --sentinels $SENTINELS "
239
+ if [ -n " $SENTINELS " ]; then
240
+ set -- " $@ " " --sentinels" " $SENTINELS "
236
241
fi
237
242
238
- if [[ ! -z " $SENTINEL_NAME " ] ]; then
239
- set -- " $@ " " --sentinel-name $SENTINEL_NAME "
243
+ if [ -n " $SENTINEL_NAME " ]; then
244
+ set -- " $@ " " --sentinel-name" " $SENTINEL_NAME "
240
245
fi
241
246
242
- if [[ ! -z " $SENTINEL_PASSWORD " ] ]; then
243
- set -- " $@ " " --sentinel-password $SENTINEL_PASSWORD "
247
+ if [ -n " $SENTINEL_PASSWORD " ]; then
248
+ set -- " $@ " " --sentinel-password" " $SENTINEL_PASSWORD "
244
249
fi
245
250
246
- if [[ ! -z " $REPLACE_CONFIG_ENV " ] ]; then
251
+ if [ -n " $REPLACE_CONFIG_ENV " ]; then
247
252
# special case for more complex docker setup with multiple connections
248
253
# to unix sockets, sentinels and normal redis server not configurable
249
254
# via REDIS_HOSTS...
@@ -252,14 +257,14 @@ if [[ ! -z "$REPLACE_CONFIG_ENV" ]]; then
252
257
# set $REPLACE_CONFIG_ENV=REDIS_PASS_1 and env REDIS_PASS_1=mypass
253
258
# now search config files for string "REDIS_PASS_1" and write there "mypass" instead
254
259
255
- env_vars_replace=" $( echo ${REPLACE_CONFIG_ENV} | sed " s/,/ /g" ) "
260
+ env_vars_replace=" $( echo " ${REPLACE_CONFIG_ENV} " | sed " s/,/ /g" ) "
256
261
echo " Going to replace this env vars inside config files: $env_vars_replace "
257
262
258
263
for env_var in ${env_vars_replace} ; do
259
264
for json_conf in config/* .json; do
260
- if [ $json_conf != " config/custom-environment-variables.json" ]; then
265
+ if [ " $json_conf " != " config/custom-environment-variables.json" ]; then
261
266
# need to replace &\/ from content of env_var var as they are special chars in sed
262
- sed -i ' s/"' $env_var ' "/"' $( printenv $env_var | sed ' s/\\/\\\\/g; s/&/\\&/g; s#/#\\/#g;' ) ' "/g' $json_conf
267
+ sed -i ' s/"' " $env_var " ' "/"' " $( printenv " $env_var " | sed ' s/\\/\\\\/g; s/&/\\&/g; s#/#\\/#g;' ) " ' "/g' " $json_conf "
263
268
fi
264
269
done
265
270
done
268
273
269
274
# syntax check of all config files to help detecting invalid ones early
270
275
for i in config/* .json; do
271
- cat ${i} | jq empty
272
- if [[ $? -ne 0 ]]; then
276
+ if ! jq empty < " ${i} " ; then
273
277
echo " ERROR: config file ${i} has invalid json syntax"
274
278
exit 1
275
279
fi
276
- # if [ $i != "config/custom-environment-variables.json" ]; then
277
- # echo .
278
- # echo "###config file: $i"
279
- # cat $i
280
- # fi
281
280
done
282
281
283
282
# install trap for SIGTERM to delay end of app a bit for Kubernetes
284
283
# otherwise container might get requests after exiting itself
285
284
exitTrap () {
286
285
echo " Got signal, wait a bit before exit"
287
286
sleep $GRACE_PERIOD
288
- kill -TERM $NODE_PID
287
+ kill -TERM " $NODE_PID "
289
288
}
290
289
291
- if [[ " $K8S_SIGTERM " = " 1" ] ]; then
290
+ if [ " $K8S_SIGTERM " = " 1" ]; then
292
291
trap exitTrap TERM INT
293
- echo " node ./bin/redis-commander " $( safe_print_args $@ ) " for k8s"
294
- setsid $NODE ./bin/redis-commander $@ &
292
+ echo " node ./bin/redis-commander $( safe_print_args " $@ " ) for k8s"
293
+ setsid " $NODE " ./bin/redis-commander " $@ " &
295
294
NODE_PID=$!
296
295
wait $NODE_PID
297
296
trap - TERM INT
298
297
wait $NODE_PID
299
298
else
300
- echo " node ./bin/redis-commander " $( safe_print_args $@ ) " "
301
- exec $NODE ./bin/redis-commander $@
299
+ echo " node ./bin/redis-commander $( safe_print_args " $@ " ) "
300
+ exec " $NODE " ./bin/redis-commander " $@ "
302
301
fi
303
302
0 commit comments