-
Notifications
You must be signed in to change notification settings - Fork 27
/
manage
executable file
·649 lines (584 loc) · 18.8 KB
/
manage
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#!/bin/bash
export MSYS_NO_PATHCONV=1
export DOCKERHOST=${APPLICATION_URL-$(docker run --rm --net=host eclipse/che-ip)}
SCRIPT_HOME="$(cd "$(dirname "$0")" && pwd)"
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-indy-node-monitor-stack}"
export APP_NAME="Indy Node Monitoring Stack"
set -e
# =================================================================================================================
# Usage:
# -----------------------------------------------------------------------------------------------------------------
usage() {
cat <<-EOF
Usage:
$0 [options] [command] [args]
Commands:
up|start [service] - Spin up the ${APP_NAME} environment.
Optionally specify the service to spin up (dependencies will also be spun up).
down|stop [service] - Tear down the ${APP_NAME} environment.
Optionally specify the service to tear down.
restart [service] - Restart the ${APP_NAME} environment.
Optionally specify the service to restart.
list-services - Get a list the services that make up the ${APP_NAME}.
logs <service> - Stream the logs from a given container.
shell <service> [bash|sh] - Open a shell on a given container.
Optional - Specify bash or sh as the shell; defaults to bash.
install-plugin - Install a Grafana plug-in on the Grafana container.
You must restart the ${APP_NAME} environment for it to be loaded.
delete-data [service] - Delete the data for a given service, or all services by default.
Useful to clear data from Prometheus and/or InfluxDB when making changes to how the data is collected.
clean - Cleans up all environment resources.
Deletes data from all services.
Deletes all containers images and prunes any dangling images.
influx-cli - Open a shell to the Influx CLI on the influxdb container
flux-repl - Open a shell to the Flux REPL on the influxdb container
build [service] - Use for troubleshooting builds when making image configuration changes.
Builds container images based on the docker-compose configuration.
sed-test - Run a 'sed' parsing test on your machine. Helps diagnose issues with the version
of 'sed on some machines.
Options:
-h - Print this help documentation.
--nightly - Build/Use 'nightly' Influx images, 'latest' images are built/used by default.
Works with the up|start and restart commands.
EOF
exit 1
}
# -----------------------------------------------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------------------------------------------
function toLower() {
(
echo $(echo ${@} | tr '[:upper:]' '[:lower:]')
)
}
function echoBlue (){
(
_msg=${1}
_blue='\e[34m'
_nc='\e[0m' # No Color
echo -e "${_blue}${_msg}${_nc}" >&2
)
}
function echoGreen (){
(
_msg=${1}
_blue='\e[32m'
_nc='\e[0m' # No Color
echo -e "${_blue}${_msg}${_nc}" >&2
)
}
function echoYellow (){
(
_msg=${1}
_yellow='\e[33m'
_nc='\e[0m' # No Color
echo -e "${_yellow}${_msg}${_nc}" >&2
)
}
function echoError (){
(
_msg=${1}
_red='\e[31m'
_nc='\e[0m' # No Color
echo -e "${_red}${_msg}${_nc}" >&2
)
}
function printAndWait() {
(
_msg=${@}
if [ ! -z "${_msg}" ]; then
echoYellow "\n${_msg}"
fi
read -n1 -s -r -p $'Press any key to continue ...' key
echo -e \\n
return 0
)
}
function promptForConfirmation(){
(
_msg=${@}
echoYellow "\n${_msg}"
read -n1 -s -r -p $'\e[33mWould you like to continue?\e[0m Press \'Y\' to continue, or any other key to exit ...\n' key
if [[ $(toLower ${key}) == 'y' ]]; then
return 0
else
return 1
fi
)
}
function functionExists() {
(
if [ ! -z ${1} ] && type ${1} &>/dev/null; then
return 0
else
return 1
fi
)
}
function isInstalled() {
(
if [ -x "$(command -v ${@})" ]; then
return 0
else
return 1
fi
)
}
function openInBrowser(){
(
unset openBrowserCmd
if [[ "$OSTYPE" == "msys" ]]; then
openBrowserCmd='cmd "/c start ${url}"'
elif [ $(uname) == "Darwin" ]; then
openBrowserCmd='open ${url}'
elif [ $(uname) == "Linux" ]; then
openBrowserCmd='xdg-open ${url}'
fi
if [ ! -z "${openBrowserCmd}" ]; then
for url in ${@}; do
echoBlue "\nOpening ${url} in your default browser ..."
eval ${openBrowserCmd}
done
else
echoYellow "Unable to detect browser start command. Please browser to the following application URLs:"
for url in ${@}; do
echo " - ${url}"
done
fi
)
}
function logs() {
(
if [ ! -z "${@}" ] && isService ${@}; then
echoBlue "Following container logs for ${@} ..."
${DOCKER_COMPOSE_EXE} logs -f ${@}
fi
)
}
function openShell() {
(
service=${1}
shellCmd=${2:-bash}
if [ ! -z "${service}" ] && isService ${service}; then
echoBlue "Connecting remote shell to ${service} (type 'exit' to exit) ..."
${DOCKER_COMPOSE_EXE} exec ${service} ${shellCmd}
fi
)
}
function installPlugin() {
(
echoGreen "Installing Grafana plugin; ${@} ... "
${DOCKER_COMPOSE_EXE} exec grafana grafana-cli plugins install ${@}
)
}
function readEnvFile(){
(
envFile=${1}
if [ -f ${envFile} ]; then
# Remove blank lines ...
filters="/^[[:blank:]]*$/d;"
# Remove full line comments ...
filters+="/^[[:blank:]]*#/d;"
# Remove any comments from the end of a line ...
filters+="s/#.*//;"
# Remove leading whitespace ...
filters+="s/^ *//;"
# Remove trailing whitespace ...
filters+="s/ *$//;"
_value=$(${SED_EXE} "${filters}" ${envFile})
fi
echo "${_value}"
)
}
function loadEnvVariables(){
for envTag in ${@}; do
for item in $(readEnvFile ".env-${envTag}"); do
export "${item}"
done
done
}
function deleteData(){
(
deleteList=${@:-influxdb prometheus grafana}
unset influxdb_data
unset prometheus_data
unset grafana_data
unset delete_msg
for item in ${deleteList}; do
case ${item} in
influxdb)
influxdb_data=1 ;;
prometheus)
prometheus_data=1 ;;
grafana)
grafana_data=1 ;;
*)
influxdb_data=1
prometheus_data=1
grafana_data=1
;;
esac
delete_msg+=" ${item}"
done
msg_start=$(echo ${delete_msg} | awk '{$NF="";sub(/[ \t]+$/,"")}1')
msg_end=$(echo ${delete_msg} | awk '{print $(NF)}')
delete_msg="${msg_start}${msg_start:+ and }${msg_end}"
if promptForConfirmation "If you continue all ${delete_msg} data will be deleted and reprovisioned from scratch next time you start the monitor.
If you want to save any of your work, ensure you export it and update your provisioning configurations before continuing.\n"; then
if [ ! -z ${influxdb_data} ]; then
echo "Deleting all InfluxDB data..."
rm -rf influxdb/data
fi
if [ ! -z ${prometheus_data} ]; then
echo "Deleting all Prometheus data..."
$(docker volume rm indy-node-monitor-stack_prometheus_data -f >/dev/null 2>&1)
fi
if [ ! -z ${grafana_data} ]; then
echo "Deleting all Grafana data..."
$(docker volume rm indy-node-monitor-stack_grafana_data -f >/dev/null 2>&1)
fi
fi
)
}
function getServices(){
(
ymlFile=${1}
unset services
if [ -f ${ymlFile} ]; then
services="$(${YQ_EXE} eval '.services | keys | .[]' ${ymlFile})"
fi
echo "${services}"
)
}
function getImages(){
(
ymlFile=${1}
unset images
if [ -f ${ymlFile} ]; then
image="$(${YQ_EXE} eval '.services[].image' ${ymlFile})"
fi
echo "${image}"
)
}
function contains(){
(
local _item
_find=${1}
shift
for _item in ${@}; do [[ "${_item}" == "${_find}" ]] && return 0; done
return 1
)
}
function isService(){
(
serviceName=${1}
if contains "${serviceName}" "${_services[@]}"; then
return 0
else
echoYellow "No such service: ${serviceName}"
listServices
return 1
fi
)
}
function listServices(){
(
echoBlue "Available Services:"
for service in ${_services}; do
echoBlue " - ${service}"
done
)
}
function build(){
(
echoGreen "\nBuilding ${@} ...\n"
${DOCKER_COMPOSE_EXE} up --build --no-start ${@}
)
}
function up(){
(
echoGreen "\nStarting up the ${APP_NAME} ...\n"
${DOCKER_COMPOSE_EXE} up -d --build ${@}
sleep 3
case "${1}" in
indy-node-monitor)
openInBrowser "http://localhost:8080"
;;
*)
openInBrowser "http://localhost:3000"
;;
esac
)
}
function down(){
(
echoGreen "\nStopping the ${APP_NAME} ...\n"
${DOCKER_COMPOSE_EXE} down ${@}
)
}
function restart(){
(
echoGreen "\nRestarting the ${APP_NAME} ...\n"
down ${@}
up ${@}
)
}
function clean(){
(
down
deleteData
echoGreen "\nRemoving project images ...\n"
${DOCKER_EXE} image remove --force ${_images} 2>/dev/null
${DOCKER_EXE} image prune --force > /dev/null 2>&1
)
}
function openInfluxCli(){
(
echoBlue "Opening the influx cli (type 'exit' to exit) ..."
${DOCKER_COMPOSE_EXE} exec influxdb /usr/bin/influx
)
}
function openFluxRepl(){
(
echoBlue "Opening the flux REPL (press Ctrl-D to exit) ..."
${DOCKER_COMPOSE_EXE} exec influxdb /usr/bin/influx -type flux
)
}
function writeEnvSeeds(){
(
cat <<-EOF > .env-seeds
# =======================================================
# Network Monitor Seeds:
# - Leave blank if you do not have a
# Network Monitor DID registered on the Network
# - This file is ignored by Git
#
# Format:
# <Network_ID>_SEED=<Your_Seed>
# -------------------------------------------------------
SBN_SEED=
SSN_SEED=
SMN_SEED=
# =======================================================
EOF
)
}
function sedTest(){
(
sedTestFile=sed-test.txt
testPhrase='This is the test Phrase.'
cat <<-EOF > ${sedTestFile}
# ===========================================================
# This is a file to test parsing with 'sed' on your machine.
# - All of the blank lines, comments, and leading and
# trailing whitespace should be stripped out of this
# file and you should only be left with the
# test phrase wrapped in single quotes:
#
# '${testPhrase}'
#
# -----------------------------------------------------------
# This line should be removed, it's a comment
# The leading and trailing whitespace around the test
# phrase should be removed too.
${testPhrase} # This comment should be removed
# ===========================================================
EOF
result="'$(readEnvFile ${sedTestFile})'"
echoBlue "\nTest Results for 'sed':"
if [[ ${result} == "'${testPhrase}'" ]]; then
echoGreen " - Pass"
rm -rf ${sedTestFile}
else
echoError " - Fail"
echoBlue "\n==================================================================================="
echoBlue "# Result of parsing the file using 'sed':"
echoBlue "-----------------------------------------------------------------------------------"
echo ${result}
echoBlue "===================================================================================\n"
echoBlue "==================================================================================="
echoBlue "# Original test file:"
echoBlue "-----------------------------------------------------------------------------------"
cat ${sedTestFile}
echoBlue "==================================================================================="
rm -rf ${sedTestFile}
printAndWait "Please review the results and then,"
echoBlue "================================================================================================================================================================="
echoBlue "# Special instructions for MAC Setup:"
echoBlue "-----------------------------------------------------------------------------------------------------------------------------------------------------------------"
cat <<-EOF
These scripts use 'sed' and regular expression processing. The default version of 'sed' on MAC does support some of the processing.
Details can be found here;
[Differences between sed on Mac OSX and other "standard" sed?](https://unix.stackexchange.com/questions/13711/differences-between-sed-on-mac-osx-and-other-standard-sed)
Please install 'gnu-sed'.
Using [Homebrew](https://brew.sh):
'''
brew install gnu-sed
'''
Then update your path and prepend '/usr/local/opt/gnu-sed/libexec/gnubin:' to your existing path so that the system defaults to using 'sed' rather than 'gsed'.
Append this line to your '~\.bashrc' file:
'''
[[ ":\$PATH:" != *"/usr/local/opt/gnu-sed/libexec/gnubin:"* ]] && export PATH="/usr/local/opt/gnu-sed/libexec/gnubin:\$PATH"
'''
Also make sure 'usr/local/bin' is at a higher priority on your **PATH** than 'usr/bin'. You can do this by making sure 'usr/local/bin' is to the left of 'usr/bin',
preceding it in the **PATH** string. This will ensure that packages installed by Homebrew override system binaries; in this case 'sed'.
Append this line to your '~\.bashrc' file:
'''
[[ ":\$PATH:" != *"/usr/local/bin:"* ]] && export PATH="/usr/local/bin:\$PATH"
'''
'brew doctor' can help diagnose such issues.
EOF
echoBlue "================================================================================================================================================================="
fi
)
}
# -----------------------------------------------------------------------------------------------------------------
# Check for dependancies
# -----------------------------------------------------------------------------------------------------------------
DOCKER_EXE="docker"
if ! isInstalled ${DOCKER_EXE}; then
echoYellow "${DOCKER_EXE} is not installed."
exit 1
fi
DOCKER_COMPOSE_EXE="${DOCKER_EXE} compose"
if ! isInstalled ${DOCKER_COMPOSE_EXE}; then
echoYellow "${DOCKER_COMPOSE_EXE} is not installed."
exit 1
fi
SED_EXE=sed
if ! isInstalled ${SED_EXE}; then
echoYellow "The ${SED_EXE} executable is required and was not found on your path."
cat <<-EOF
The recommended approach to installing the required package(s) is to use either [Homebrew](https://brew.sh/) (MAC)
or [Chocolatey](https://chocolatey.org/) (Windows) or [GitHub](https://github.com/mirror/sed) (Linux). For more information visit https://www.gnu.org/software/sed/
Windows:
- chocolatey install sed
MAC:
- brew install gnu-sed
Debian/Ubuntu:
- sudo apt-get install sed
EOF
exit 1
fi
YQ_EXE=yq
if ! isInstalled ${YQ_EXE}; then
echoYellow "The ${YQ_EXE} executable is required and was not found on your path."
cat <<-EOF
The recommended approach to installing the required package(s) is to use either [Homebrew](https://brew.sh/) (MAC)
or [Chocolatey](https://chocolatey.org/) (Windows) or [GitHub](https://github.com/mikefarah/yq/) (Linux). For more information visit https://mikefarah.github.io/yq/
Windows:
- chocolatey install ${YQ_EXE}
MAC:
- brew install ${YQ_EXE}
Linux(x86_64):
- wget https://github.com/mikefarah/yq/releases/latest/download/yq_linux_amd64 -O /usr/bin/yq && chmod +x /usr/bin/yq
EOF
exit 1
fi
# -----------------------------------------------------------------------------------------------------------------
# Initialization:
# -----------------------------------------------------------------------------------------------------------------
_envTags="default"
_imageTag="latest"
while getopts ":h-:" FLAG; do
case $FLAG in
h ) usage ;;
\? ) #unrecognized option - show help
echoError "\nInvalid script option: -${OPTARG}"
usage
;;
- )
case ${OPTARG} in
"nightly"*)
_imageTag="nightlies"
;;
esac
esac
done
shift $((OPTIND-1))
_envTags+=" ${_imageTag}"
# Create the directories expected by the docker-compose project ...
INFLUX_DB_DIR="./influxdb/data"
if [ -d "${INFLUX_DB_DIR}" ]; then
mkdir -p "${INFLUX_DB_DIR}"
fi
NODE_MONITOR_CONF="./fetch-validator-status/conf"
if [ -d "${NODE_MONITOR_CONF}" ]; then
mkdir -p "${NODE_MONITOR_CONF}"
fi
NODE_MONITOR_LOGS="./fetch-validator-status/logs"
if [ -d "${NODE_MONITOR_LOGS}" ]; then
mkdir -p "${NODE_MONITOR_LOGS}"
fi
# Load network monitor seeds if the '.env-seeds' file is found.
# The file is ignored by Git so seeds don't end up in source control.
if [ -f ".env-seeds" ]; then
_envTags+=" seeds"
else
echoYellow "\nNo '.env-seeds' file was found. \
This file is used to inject your network monitor seeds into Telegaf so it can make privileged queries to the Indy Node Monitor web service. \
This file is ignored by git so it does not accidentally make its way into source control. \
If you do not supply a set of network monitor seeds Telegaf will only be able to make unprivileged queries to the Indy Node Monitor. \
The responses to unprivileged queries only allow node monitoring at a very high level, i.e. up/down.\n\n\
To make privileged queries, add you seeds to the generated file and run './manage restart'."
if promptForConfirmation "Would you like to create a template '.env-seeds' file now?"; then
writeEnvSeeds
fi
fi
# Get the list of services ...
_services=$(getServices "./docker-compose.yml")
_images=$(getImages "./docker-compose.yml")
# =================================================================================================================
# =================================================================================================================
# Main Script:
# -----------------------------------------------------------------------------------------------------------------
pushd ${SCRIPT_HOME} >/dev/null
COMMAND=$(toLower ${1})
shift || COMMAND=usage
loadEnvVariables "${_envTags}"
case "${COMMAND}" in
start|up)
up ${@}
;;
stop|down)
down ${@}
;;
restart)
restart ${@}
;;
logs)
logs ${@}
;;
shell)
openShell ${@}
;;
install-plugin)
installPlugin ${@}
;;
delete-data)
deleteData ${@}
;;
clean)
clean ${@}
;;
influx-cli)
openInfluxCli ${@}
;;
flux-repl)
openFluxRepl ${@}
;;
build)
build ${@}
;;
sed-test)
sedTest ${@}
;;
list-services)
listServices ${@}
;;
*)
usage
;;
esac
popd >/dev/null
# -----------------------------------------------------------------------------------------------------------------