-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.sh
executable file
·424 lines (322 loc) · 9.36 KB
/
common.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#!/bin/bash
command -v fzf > /dev/null && FZF=true
command -v tput > /dev/null && TPUT=true
_echo() {
if [ "${TPUT}" != "" ] && [ "$2" != "" ]; then
echo -e "${L_PAD}$(tput setaf $2)$1$(tput sgr0)"
else
echo -e "${L_PAD}$1"
fi
}
_read() {
echo
if [ "${3}" == "S" ]; then
if [ "${TPUT}" != "" ] && [ "$2" != "" ]; then
read -s -p "${L_PAD}$(tput setaf $2)$1$(tput sgr0)" ANSWER
else
read -s -p "${L_PAD}$1" ANSWER
fi
else
if [ "${TPUT}" != "" ] && [ "$2" != "" ]; then
read -p "${L_PAD}$(tput setaf $2)$1$(tput sgr0)" ANSWER
else
read -p "${L_PAD}$1" ANSWER
fi
fi
}
_replace() {
if [ "${OS_NAME}" == "darwin" ]; then
sed -i "" -e "$1" $2
else
sed -i -e "$1" $2
fi
}
_result() {
echo
_echo "# $@" 4
}
_command() {
echo
_echo "$ $@" 3
}
_success() {
echo
_echo "+ $@" 2
_exit 0
}
_error() {
echo
_echo "- $@" 1
_exit 1
}
_exit() {
echo
exit $1
}
question() {
_read "${1:-"Enter your choice : "}" 6
if [ ! -z ${2} ]; then
if ! [[ ${ANSWER} =~ ${2} ]]; then
ANSWER=
fi
fi
}
password() {
_read "${1:-"Enter your password : "}" 6 S
}
print_list() {
IDX=0
while read VAL; do
IDX=$(( ${IDX} + 1 ))
printf "%2s. %s\n" "${IDX}" "${VAL}"
done < ${LIST}
}
select_one() {
OPT=$1
SELECTED=
CNT=$(cat ${LIST} | wc -l | xargs)
if [ "x${CNT}" == "x0" ]; then
return
fi
if [ "${OPT}" != "" ] && [ "x${CNT}" == "x1" ]; then
SELECTED="$(cat ${LIST} | xargs)"
else
echo
print_list
if [ "${CNT}" != "1" ]; then
CNT="1-${CNT}"
fi
_read "Please select one. (${CNT}) : " 6
if [ -z ${ANSWER} ]; then
return
fi
TEST='^[0-9]+$'
if ! [[ ${ANSWER} =~ ${TEST} ]]; then
return
fi
SELECTED=$(sed -n ${ANSWER}p ${LIST})
fi
}
progress() {
if [ "$1" == "start" ]; then
printf '%2s'
elif [ "$1" == "end" ]; then
printf '.\n'
else
printf '.'
sleep 2
fi
}
waiting() {
SEC=${1:-2}
echo
progress start
IDX=0
while true; do
if [ "${IDX}" == "${SEC}" ]; then
break
fi
IDX=$(( ${IDX} + 1 ))
progress ${IDX}
done
progress end
echo
}
get_az_list() {
if [ -z ${AZ_LIST} ]; then
AZ_LIST="$(aws ec2 describe-availability-zones | jq -r '.AvailabilityZones[].ZoneName' | head -3 | tr -s '\r\n' ',' | sed 's/.$//')"
fi
}
get_template() {
__FROM=${SHELL_DIR}/${1}
__DIST=${2}
mkdir -p ${SHELL_DIR}/build/${THIS_NAME}
rm -rf ${__DIST}
if [ -f ${__FROM} ]; then
cat ${__FROM} > ${__DIST}
else
curl -sL https://raw.githubusercontent.com/${THIS_REPO}/${THIS_NAME}/master/${1} > ${__DIST}
fi
if [ ! -f ${__DIST} ]; then
_error "Template does not exists. [${1}]"
fi
}
update_tools() {
curl -sL toast.sh/tools | bash
_success "Please restart!"
}
update_self() {
pushd ${SHELL_DIR}
git pull
popd
_success "Please restart!"
}
logo() {
if [ "${TPUT}" != "" ]; then
tput clear
tput setaf 3
fi
cat ${SHELL_DIR}/templates/logo-${THIS_NAME}.txt
echo
if [ "${TPUT}" != "" ]; then
tput sgr0
fi
}
config_load() {
COUNT=$(kubectl get pod -n kube-system | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_error "Unable to connect to the cluster."
fi
_command "kubectl get secret ${THIS_NAME}-config -n default"
COUNT=$(kubectl get secret -n default | grep ${THIS_NAME}-config | wc -l | xargs)
if [ "x${COUNT}" != "x0" ]; then
mkdir -p ${SHELL_DIR}/build/${CLUSTER_NAME}
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/config.sh
kubectl get secret ${THIS_NAME}-config -n default -o json | \
jq -r '.data.text' | base64 --decode > ${CONFIG}
_command "cat ${THIS_NAME}-config"
cat ${CONFIG} | grep '='
. ${CONFIG}
fi
}
config_edit() {
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/config.sh
_command "kubectl get secret ${THIS_NAME}-config -n default"
kubectl get secret ${THIS_NAME}-config -n default -o json | \
jq -r '.data.text' | base64 --decode > ${CONFIG}
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/config-list
cat ${CONFIG} | grep '=' | cut -d'=' -f1 > ${LIST}
# select
select_one
if [ "${SELECTED}" == "" ]; then
return
fi
DEFAULT="$(cat ${CONFIG} | grep '=' | grep ${SELECTED} | cut -d'=' -f2)"
question "${SELECTED} = [${DEFAULT}] : "
if [ "${ANSWER}" == "" ]; then
ANSWER=${DEFAULT}
fi
_replace "s|${SELECTED}=.*|${SELECTED}=${ANSWER}|" ${CONFIG}
# _command "load ${THIS_NAME}-config"
# cat ${CONFIG} | grep '='
. ${CONFIG}
CONFIG_SAVE=true
config_save
}
config_save() {
if [ "${CONFIG_SAVE}" == "" ]; then
return
fi
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/config.sh
echo "# ${THIS_NAME} config" > ${CONFIG}
echo "CLUSTER_NAME=${CLUSTER_NAME}" >> ${CONFIG}
echo "ROOT_DOMAIN=${ROOT_DOMAIN}" >> ${CONFIG}
echo "BASE_DOMAIN=${BASE_DOMAIN}" >> ${CONFIG}
echo "EFS_ID=${EFS_ID}" >> ${CONFIG}
echo "ISTIO=${ISTIO}" >> ${CONFIG}
echo "ISTIO_DOMAIN=${ISTIO_DOMAIN}" >> ${CONFIG}
_command "cat ${THIS_NAME}-config"
cat ${CONFIG} | grep '='
ENCODED=${SHELL_DIR}/build/${CLUSTER_NAME}/config.txt
if [ "${OS_NAME}" == "darwin" ]; then
cat ${CONFIG} | base64 > ${ENCODED}
else
cat ${CONFIG} | base64 -w 0 > ${ENCODED}
fi
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/secret-config.yaml
get_template templates/secret-config.yaml ${CONFIG}
_replace "s/REPLACE-ME/${THIS_NAME}-config/" ${CONFIG}
sed "s/^/ /" ${ENCODED} >> ${CONFIG}
_command "kubectl apply -f ${CONFIG} -n default"
kubectl apply -f ${CONFIG} -n default
CONFIG_SAVE=
}
variables_domain() {
__KEY=${1}
__VAL=$(kubectl get ing --all-namespaces | grep devops | grep ${__KEY} | awk '{print $3}' | cut -d',' -f1)
echo "@Field" >> ${CONFIG}
echo "def ${__KEY} = \"${__VAL}\"" >> ${CONFIG}
}
variables_load() {
COUNT=$(kubectl get secret -n default | grep groovy-variables | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_result "No have groovy-variables."
return
fi
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/variables.groovy
_command "kubectl get secret groovy-variables -n default"
kubectl get secret groovy-variables -n default -o json | \
jq -r '.data.groovy' | base64 --decode > ${CONFIG}
_command "cat variables.groovy"
cat ${CONFIG} | grep 'def '
}
variables_edit() {
COUNT=$(kubectl get secret -n default | grep groovy-variables | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
_result "No have groovy-variables."
return
fi
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/variables.groovy
_command "kubectl get secret groovy-variables -n default"
kubectl get secret groovy-variables -n default -o json | \
jq -r '.data.groovy' | base64 --decode > ${CONFIG}
LIST=${SHELL_DIR}/build/${CLUSTER_NAME}/variables-list
cat ${CONFIG} | grep 'def ' | cut -d' ' -f2 > ${LIST}
# select
select_one
if [ "${SELECTED}" == "" ]; then
return
fi
DEFAULT="$(cat ${CONFIG} | grep 'def ' | grep ${SELECTED} | cut -d'"' -f2)"
question "${SELECTED} = [${DEFAULT}] : "
if [ "${ANSWER}" == "" ]; then
ANSWER=${DEFAULT}
fi
_replace "s|def ${SELECTED} = .*|def ${SELECTED} = \"${ANSWER}\"|" ${CONFIG}
variables_save
}
variables_auto() {
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/variables.groovy
echo "#!/usr/bin/groovy" > ${CONFIG}
echo "import groovy.transform.Field" >> ${CONFIG}
COUNT=$(kubectl get ing --all-namespaces | grep devops | wc -l | xargs)
if [ "x${COUNT}" == "x0" ]; then
echo "@Field" >> ${CONFIG}
echo "def cluster = \"${CLUSTER_NAME}\"" >> ${CONFIG}
else
echo "@Field" >> ${CONFIG}
echo "def cluster = \"devops\"" >> ${CONFIG}
fi
echo "@Field" >> ${CONFIG}
echo "def root_domain = \"${ROOT_DOMAIN}\"" >> ${CONFIG}
echo "@Field" >> ${CONFIG}
echo "def base_domain = \"${BASE_DOMAIN}\"" >> ${CONFIG}
if [ "x${COUNT}" != "x0" ]; then
variables_domain "chartmuseum"
variables_domain "harbor"
variables_domain "jenkins"
variables_domain "nexus"
variables_domain "registry"
variables_domain "sonarqube"
fi
echo "@Field" >> ${CONFIG}
echo "def slack_token = \"\"" >> ${CONFIG}
echo "return this" >> ${CONFIG}
variables_save
}
variables_save() {
_command "cat variables.groovy"
cat ${CONFIG} | grep 'def '
ENCODED=${SHELL_DIR}/build/${CLUSTER_NAME}/variables.txt
if [ "${OS_NAME}" == "darwin" ]; then
cat ${CONFIG} | base64 > ${ENCODED}
else
cat ${CONFIG} | base64 -w 0 > ${ENCODED}
fi
CONFIG=${SHELL_DIR}/build/${CLUSTER_NAME}/secret-groovy.yaml
get_template templates/secret-groovy.yaml ${CONFIG}
_replace "s/REPLACE-ME/groovy-variables/" ${CONFIG}
sed "s/^/ /" ${ENCODED} >> ${CONFIG}
_command "kubectl apply -f ${CONFIG} -n default"
kubectl apply -f ${CONFIG} -n default
}