-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathrun
executable file
·1460 lines (1237 loc) · 39.6 KB
/
run
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# .___
# ____ ____ _____ _____ _____ ____ __| _/______
# _/ ___\/ _ \ / \ / \\__ \ / \ / __ |/ ___/
# \ \__( <_> ) Y Y \ Y Y \/ __ \| | \/ /_/ |\___ \
# \___ >____/|__|_| /__|_| (____ /___| /\____ /____ >
# \/ \/ \/ \/ \/ \/ \/
#
# Boilerplate for creating a bash program with commands.
#
# Depends on:
# list
# of
# programs
# expected
# in
# environment
#
# Bash Boilerplate: https://github.com/alphabetum/bash-boilerplate
#
# Copyright (c) 2015 William Melody • [email protected]
# Notes #######################################################################
# Extensive descriptions are included for easy reference.
#
# Explicitness and clarity are generally preferable, especially since bash can
# be difficult to read. This leads to noisier, longer code, but should be
# easier to maintain. As a result, some general design preferences:
#
# - Use leading underscores on internal variable and function names in order
# to avoid name collisions. For unintentionally global variables defined
# without `local`, such as those defined outside of a function or
# automatically through a `for` loop, prefix with double underscores.
# - Always use braces when referencing variables, preferring `${NAME}` instead
# of `$NAME`. Braces are only required for variable references in some cases,
# but the cognitive overhead involved in keeping track of which cases require
# braces can be reduced by simply always using them.
# - Prefer `printf` over `echo`. For more information, see:
# http://unix.stackexchange.com/a/65819
# - Prefer `$_explicit_variable_name` over names like `$var`.
# - Use the `#!/usr/bin/env bash` shebang in order to run the preferred
# Bash version rather than hard-coding a `bash` executable path.
# - Prefer splitting statements across multiple lines rather than writing
# one-liners.
# - Group related code into sections with large, easily scannable headers.
# - Describe behavior in comments as much as possible, assuming the reader is
# a programmer familiar with the shell, but not necessarily experienced
# writing shell scripts.
###############################################################################
# Strict Mode
###############################################################################
# Treat unset variables and parameters other than the special parameters ‘@’ or
# ‘*’ as an error when performing parameter expansion. An 'unbound variable'
# error message will be written to the standard error, and a non-interactive
# shell will exit.
#
# This requires using parameter expansion to test for unset variables.
#
# http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
#
# The two approaches that are probably the most appropriate are:
#
# ${parameter:-word}
# If parameter is unset or null, the expansion of word is substituted.
# Otherwise, the value of parameter is substituted. In other words, "word"
# acts as a default value when the value of "$parameter" is blank. If "word"
# is not present, then the default is blank (essentially an empty string).
#
# ${parameter:?word}
# If parameter is null or unset, the expansion of word (or a message to that
# effect if word is not present) is written to the standard error and the
# shell, if it is not interactive, exits. Otherwise, the value of parameter
# is substituted.
#
# Examples
# ========
#
# Arrays:
#
# ${some_array[@]:-} # blank default value
# ${some_array[*]:-} # blank default value
# ${some_array[0]:-} # blank default value
# ${some_array[0]:-default_value} # default value: the string 'default_value'
#
# Positional variables:
#
# ${1:-alternative} # default value: the string 'alternative'
# ${2:-} # blank default value
#
# With an error message:
#
# ${1:?'error message'} # exit with 'error message' if variable is unbound
#
# Short form: set -u
set -o nounset
# Exit immediately if a pipeline returns non-zero.
#
# NOTE: this has issues. When using read -rd '' with a heredoc, the exit
# status is non-zero, even though there isn't an error, and this setting
# then causes the script to exit. read -rd '' is synonymous to read -d $'\0',
# which means read until it finds a NUL byte, but it reaches the EOF (end of
# heredoc) without finding one and exits with a 1 status. Therefore, when
# reading from heredocs with set -e, there are three potential solutions:
#
# Solution 1. set +e / set -e again:
#
# set +e
# read -rd '' variable <<EOF
# EOF
# set -e
#
# Solution 2. <<EOF || true:
#
# read -rd '' variable <<EOF || true
# EOF
#
# Solution 3. Don't use set -e or set -o errexit at all.
#
# More information:
#
# https://www.mail-archive.com/[email protected]/msg12170.html
#
# Short form: set -e
set -o errexit
# Print a helpful message if a pipeline with non-zero exit code causes the
# script to exit as described above.
trap 'echo "Aborting due to errexit on line $LINENO. Exit code: $?" >&2' ERR
# Allow the above trap be inherited by all functions in the script.
#
# Short form: set -E
set -o errtrace
# Return value of a pipeline is the value of the last (rightmost) command to
# exit with a non-zero status, or zero if all commands in the pipeline exit
# successfully.
set -o pipefail
# Set $IFS to only newline and tab.
#
# http://www.dwheeler.com/essays/filenames-in-shell.html
IFS=$'\n\t'
###############################################################################
# Globals
###############################################################################
# $_VERSION
#
# Manually set this to to current version of the program. Adhere to the
# semantic versioning specification: http://semver.org
_VERSION="1.0"
# $DEFAULT_COMMAND
#
# The command to be run by default, when no command name is specified. If the
# environment has an existing $DEFAULT_COMMAND set, then that value is used.
DEFAULT_COMMAND="${DEFAULT_COMMAND:-help}"
# $OPENSSL_IMAGE_NAME
#
# Name of the docker image which will be used for openssl cli
OPENSSL_IMAGE_NAME="openssl"
###############################################################################
# Debug
###############################################################################
# _debug()
#
# Usage:
# _debug printf "Debug info. Variable: %s\\n" "$0"
#
# A simple function for executing a specified command if the `$_USE_DEBUG`
# variable has been set. The command is expected to print a message and
# should typically be either `echo`, `printf`, or `cat`.
__DEBUG_COUNTER=0
_debug() {
if [[ "${_USE_DEBUG:-"0"}" -eq 1 ]]
then
__DEBUG_COUNTER=$((__DEBUG_COUNTER+1))
# Prefix debug message with "bug (U+1F41B)"
printf "🐛 %s " "${__DEBUG_COUNTER}"
"${@}"
printf "――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――\\n"
fi
}
# debug()
#
# Usage:
# debug "Debug info. Variable: $0"
#
# Print the specified message if the `$_USE_DEBUG` variable has been set.
#
# This is a shortcut for the _debug() function that simply echos the message.
debug() {
_debug echo "${@}"
}
###############################################################################
# Die
###############################################################################
# _die()
#
# Usage:
# _die printf "Error message. Variable: %s\\n" "$0"
#
# A simple function for exiting with an error after executing the specified
# command. The command is expected to print a message and should typically
# be either `echo`, `printf`, or `cat`.
_die() {
# Prefix die message with "cross mark (U+274C)", often displayed as a red x.
printf "❌ "
"${@}" 1>&2
exit 1
}
# die()
#
# Usage:
# die "Error message. Variable: $0"
#
# Exit with an error and print the specified message.
#
# This is a shortcut for the _die() function that simply echos the message.
die() {
_die echo "${@}"
}
###############################################################################
# Options
#
# NOTE: The `getops` builtin command only parses short options and BSD `getopt`
# does not support long arguments (GNU `getopt` does), so the most portable
# and clear way to parse options is often to just use a `while` loop.
#
# For a pure bash `getopt` function, try pure-getopt:
# https://github.com/agriffis/pure-getopt
#
# More info:
# http://wiki.bash-hackers.org/scripting/posparams
# http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html
# http://stackoverflow.com/a/14203146
# http://stackoverflow.com/a/7948533
# https://stackoverflow.com/a/12026302
# https://stackoverflow.com/a/402410
###############################################################################
# Get raw options for any commands that expect them.
_RAW_OPTIONS="${*:-}"
# Parse Options ###############################################################
# Initialize $_COMMAND_ARGV array
#
# This array contains all of the arguments that get passed along to each
# command. This is essentially the same as the program arguments, minus those
# that have been filtered out in the program option parsing loop. This array
# is initialized with $0, which is the program's name.
_COMMAND_ARGV=("${0}")
# Initialize $_CMD and `$_USE_DEBUG`, which can continue to be blank depending
# on what the program needs.
_CMD=""
_USE_DEBUG=0
_INTERACTIVE=1
while [[ ${#} -gt 0 ]]
do
__opt="${1}"
shift
case "${__opt}" in
-h|--help)
_CMD="help"
;;
--version)
_CMD="version"
;;
--debug)
_USE_DEBUG=1
;;
--non-interactive)
_INTERACTIVE=0
;;
*)
# The first non-option argument is assumed to be the command name.
# All subsequent arguments are added to $_COMMAND_ARGV.
if [[ -n "${_CMD}" ]]
then
_COMMAND_ARGV+=("${__opt}")
else
_CMD="${__opt}"
fi
;;
esac
done
# Set $_COMMAND_PARAMETERS to $_COMMAND_ARGV, minus the initial element, $0. This
# provides an array that is equivalent to $* and $@ within each command
# function, though the array is zero-indexed, which could lead to confusion.
#
# Use `unset` to remove the first element rather than slicing (e.g.,
# `_COMMAND_PARAMETERS=("${_COMMAND_ARGV[@]:1}")`) because under bash 3.2 the
# resulting slice is treated as a quoted string and doesn't easily get coaxed
# into a new array.
_COMMAND_PARAMETERS=(${_COMMAND_ARGV[*]})
unset "_COMMAND_PARAMETERS[0]"
_debug printf "\${_CMD}: %s\\n" "${_CMD}"
_debug printf "\${_RAW_OPTIONS} (one per line):\\n%s\\n" "${_RAW_OPTIONS}"
_debug printf "\${_COMMAND_ARGV[*]}: %s\\n" "${_COMMAND_ARGV[*]}"
_debug printf \
"\${_COMMAND_PARAMETERS[*]:-}: %s\\n" \
"${_COMMAND_PARAMETERS[*]:-}"
###############################################################################
# Environment
###############################################################################
# $_ME
#
# Set to the program's basename.
_ME=$(basename "${0}")
_debug printf "\${_ME}: %s\\n" "${_ME}"
###############################################################################
# Load Commands
###############################################################################
# Initialize $_DEFINED_COMMANDS array.
_DEFINED_COMMANDS=()
# _load_commands()
#
# Usage:
# _load_commands
#
# Loads all of the commands sourced in the environment.
_load_commands() {
_debug printf "_load_commands(): entering...\\n"
_debug printf "_load_commands() declare -F:\\n%s\\n" "$(declare -F)"
# declare is a bash built-in shell function that, when called with the '-F'
# option, displays all of the functions with the format
# `declare -f function_name`. These are then assigned as elements in the
# $function_list array.
local _function_list
_function_list=($(declare -F))
_debug printf \
"_load_commands() \${_function_list[@]}: %s\\n" \
"${_function_list[@]}"
for __name in "${_function_list[@]}"
do
_debug printf \
"_load_commands() \${__name}: %s\\n" \
"${__name}"
# Each element has the format `declare -f function_name`, so set the name
# to only the 'function_name' part of the string.
local _function_name
_function_name=$(printf "%s" "${__name}" | awk '{ print $3 }')
_debug printf \
"_load_commands() \${_function_name}: %s\\n" \
"${_function_name}"
# Add the function name to the $_DEFINED_COMMANDS array unless it starts
# with an underscore or is one of the desc(), debug(), or die() functions,
# since these are treated as having 'private' visibility.
if ! ( [[ "${_function_name}" =~ ^_(.*) ]] || \
[[ "${_function_name}" == "desc" ]] || \
[[ "${_function_name}" == "debug" ]] || \
[[ "${_function_name}" == "die" ]]
)
then
_DEFINED_COMMANDS+=("${_function_name}")
fi
done
_debug printf \
"commands() \${_DEFINED_COMMANDS[*]:-}:\\n%s\\n" \
"${_DEFINED_COMMANDS[*]:-}"
}
###############################################################################
# Main
###############################################################################
# _main()
#
# Usage:
# _main
#
# The primary function for starting the program.
#
# NOTE: must be called at end of program after all commands have been defined.
_main() {
_debug printf "main(): entering...\\n"
_debug printf "main() \${_CMD} (upon entering): %s\\n" "${_CMD}"
# If $_CMD is blank, then set to `$DEFAULT_COMMAND`
if [[ -z "${_CMD}" ]]
then
_CMD="${DEFAULT_COMMAND}"
fi
# Load all of the commands.
_load_commands
# If the command is defined, run it, otherwise return an error.
if _contains "${_CMD}" "${_DEFINED_COMMANDS[*]:-}"
then
# Pass all comment arguments to the program except for the first ($0).
${_CMD} "${_COMMAND_PARAMETERS[@]:-}"
else
_die printf "Unknown command: %s\\n" "${_CMD}"
fi
}
###############################################################################
# Utility Functions
###############################################################################
# _function_exists()
#
# Usage:
# _function_exists "possible_function_name"
#
# Returns:
# 0 If a function with the given name is defined in the current environment.
# 1 If not.
#
# Other implementations, some with better performance:
# http://stackoverflow.com/q/85880
_function_exists() {
[ "$(type -t "${1}")" == 'function' ]
}
# _command_exists()
#
# Usage:
# _command_exists "possible_command_name"
#
# Returns:
# 0 If a command with the given name is defined in the current environment.
# 1 If not.
#
# Information on why `hash` is used here:
# http://stackoverflow.com/a/677212
_command_exists() {
hash "${1}" 2>/dev/null
}
# _contains()
#
# Usage:
# _contains "$item" "${list[*]}"
#
# Returns:
# 0 If the item is included in the list.
# 1 If not.
_contains() {
local _test_list=(${*:2})
for __test_element in "${_test_list[@]:-}"
do
_debug printf "_contains() \${__test_element}: %s\\n" "${__test_element}"
if [[ "${__test_element}" == "${1}" ]]
then
_debug printf "_contains() match: %s\\n" "${1}"
return 0
fi
done
return 1
}
# _join()
#
# Usage:
# _join <separator> <array>
#
# Examples:
# _join , a "b c" d => a,b c,d
# _join / var local tmp => var/local/tmp
# _join , "${FOO[@]}" => a,b,c
#
# More Information:
# http://stackoverflow.com/a/17841619
_join() {
local IFS="${1}"
shift
printf "%s\\n" "${*}"
}
# _command_argv_includes()
#
# Usage:
# _command_argv_includes "an_argument"
#
# Returns:
# 0 If the argument is included in `$_COMMAND_ARGV`, the program's command
# argument list.
# 1 If not.
#
# This is a shortcut for simple cases where a command wants to check for the
# presence of options quickly without parsing the options again.
_command_argv_includes() {
_contains "${1}" "${_COMMAND_ARGV[*]}"
}
# _blank()
#
# Usage:
# _blank "$an_argument"
#
# Returns:
# 0 If the argument is not present or null.
# 1 If the argument is present and not null.
_blank() {
[[ -z "${1:-}" ]]
}
# _present()
#
# Usage:
# _present "$an_argument"
#
# Returns:
# 0 If the argument is present and not null.
# 1 If the argument is not present or null.
_present() {
[[ -n "${1:-}" ]]
}
# _interactive_input()
#
# Usage:
# _interactive_input
#
# Returns:
# 0 If the current input is interactive (eg, a shell).
# 1 If the current input is stdin / piped input.
_interactive_input() {
[[ -t 0 ]]
}
# _piped_input()
#
# Usage:
# _piped_input
#
# Returns:
# 0 If the current input is stdin / piped input.
# 1 If the current input is interactive (eg, a shell).
_piped_input() {
! _interactive_input
}
###############################################################################
# desc
###############################################################################
# desc()
#
# Usage:
# desc <name> <description>
# desc --get <name>
#
# Options:
# --get Print the description for <name> if one has been set.
#
# Examples:
# ```
# desc "list" <<HEREDOC
# Usage:
# ${_ME} list
#
# Description:
# List items.
# HEREDOC
#
# desc --get "list"
# ```
#
# Set or print a description for a specified command or function <name>. The
# <description> text can be passed as the second argument or as standard input.
#
# To make the <description> text available to other functions, `desc()` assigns
# the text to a variable with the format `$___desc_<name>`.
#
# When the `--get` option is used, the description for <name> is printed, if
# one has been set.
#
# NOTE:
#
# The `read` form of assignment is used for a balance of ease of
# implementation and simplicity. There is an alternative assignment form
# that could be used here:
#
# var="$(cat <<'HEREDOC'
# some message
# HEREDOC
# )
#
# However, this form appears to require trailing space after backslases to
# preserve newlines, which is unexpected. Using `read` simply requires
# escaping backslashes, which is more common.
desc() {
_debug printf "desc() \${*}: %s\\n" "$@"
[[ -z "${1:-}" ]] && _die printf "desc(): No command name specified.\\n"
if [[ "${1}" == "--get" ]]
then # get ------------------------------------------------------------------
[[ -z "${2:-}" ]] && _die printf "desc(): No command name specified.\\n"
local _name="${2:-}"
local _desc_var="___desc_${_name}"
if [[ -n "${!_desc_var:-}" ]]
then
printf "%s\\n" "${!_desc_var}"
else
printf "No additional information for \`%s\`\\n" "${_name}"
fi
else # set ------------------------------------------------------------------
if [[ -n "${2:-}" ]]
then # argument is present
read -r -d '' "___desc_${1}" <<HEREDOC
${2}
HEREDOC
_debug printf "desc() set with argument: \${___desc_%s}\\n" "${1}"
else # no argument is present, so assume piped input
# `read` exits with non-zero status when a delimeter is not found, so
# avoid errors by ending statement with `|| true`.
read -r -d '' "___desc_${1}" || true
_debug printf "desc() set with pipe: \${___desc_%s}\\n" "${1}"
fi
fi
}
###############################################################################
# Default Commands
###############################################################################
# Version #####################################################################
desc "version" <<HEREDOC
Usage:
${_ME} ( version | --version )
Description:
Display the current program version.
To save you the trouble, the current version is ${_VERSION}
HEREDOC
version() {
printf "%s\\n" "${_VERSION}"
}
# Help ########################################################################
desc "help" <<HEREDOC
Usage:
${_ME} help [<command>]
Description:
Display help information for ${_ME} or a specified command.
HEREDOC
help() {
if [[ "${1:-}" ]]
then
desc --get "${1}"
else
cat <<HEREDOC
####################
# infoCMDB Console #
####################
Utility for handling an infoCMDB installation
Usage:
${_ME} <command> [--non-interactive] [--command-options] [<arguments>]
${_ME} -h | --help
Options:
-h --help Display this help information.
--non-interactive Run without asking questions and apply configuration defaults
Help:
${_ME} help [<command>]
$(commands)
HEREDOC
fi
}
# Command List ################################################################
desc "commands" <<HEREDOC
Usage:
${_ME} commands [--raw]
Options:
--raw Display the command list without formatting.
Description:
Display the list of available commands.
HEREDOC
commands() {
if _command_argv_includes "--raw"
then
printf "%s\\n" "${_DEFINED_COMMANDS[@]}"
else
printf "Available commands:\\n"
printf " %s\\n" "${_DEFINED_COMMANDS[@]}"
fi
}
desc "setup_env" <<HEREDOC
Usage:
${_ME} setup_env
Description:
Create .env file if not exists
HEREDOC
setup_env() {
echo "Running setup_env..."
if [ -e .env ]; then
echo ".env File Already exists"
return
fi
echo "Create .env file..."
given_image_tag=${IMAGE_TAG:-}
if [[ "${given_image_tag:-}" == "" ]]; then
given_image_tag=latest
if [[ "${_INTERACTIVE}" == 1 ]]; then
read -rp "Choose an IMAGE_TAG you want to use [Env: IMAGE_TAG][latest]: " given_image_tag
given_image_tag=${given_image_tag:-latest}
fi
fi
cp -v .env.example .env
echo "Setting image tag: ${given_image_tag}"
sed -i -e "s/IMAGE_TAG=.*/IMAGE_TAG=${given_image_tag}/" .env
echo "Generating random Root Password"
MYSQL_ROOT_PW=$(head -c100 < /dev/random | base64 | tr -dc _A-Z-a-z-0-9 | head -c 40)
sed -i -e "s/DB_ROOT_PASSWORD=.*/DB_ROOT_PASSWORD=${MYSQL_ROOT_PW}/" .env
echo "Generating random 'infocmdb' User Password"
MYSQL_PW=$(head -c100 < /dev/random | base64 | tr -dc _A-Z-a-z-0-9 | head -c 40)
sed -i -e "s/DB_PASSWORD=.*/DB_PASSWORD=${MYSQL_PW}/" .env
}
desc "setup_docker" <<HEREDOC
Usage:
${_ME} setup_docker [<env>]
Description:
Setup docker-compose
Possible environment values: prod / dev / test
HEREDOC
setup_docker() {
config_file="docker-compose-prod.yml"
override_file="docker-compose.override.yml"
check_env_exists
echo "Running setup_docker...."
if [[ -e "${override_file}" ]]; then
echo "${override_file} Already exists."
return
fi
if [[ ! -e ${config_file} ]]; then
# Since the "docker-compose-prod.yml" doesn't exist we assume this is a infocmdb-compose setup
# exit quietly since this is not unexpected
return
fi
echo "Setting up docker override per environment."
given_env=${COMPOSE_ENV:-""}
if [[ ${given_env} == '' && "${_INTERACTIVE}" == 1 ]]; then
read -rp "Choose Environment [Env: COMPOSE_ENV] (prod/dev/test) [prod]: " given_env
given_env=${given_env:-prod}
fi
case "${given_env}" in
"prod")
;;
"dev")
config_file="docker-compose-dev.yml"
;;
"test")
config_file="docker-compose-test.yml"
;;
*)
echo "please choose 'prod', 'dev' or 'test'!"
exit 1
esac
echo " Creating symlink: ${config_file} --> ${override_file}"
ln -sf "${config_file}" "${override_file}"
}
desc "setup_lib" <<HEREDOC
Usage:
${_ME} setup_lib
Description:
Install Perl libs
HEREDOC
setup_lib() {
echo "Setup perl modules"
if [ -e library/perl ]; then
echo " Already installed."
return
fi
check_env_exists
source .env
git clone "${APP_LIBRARY_PERL_REPO}" library/perl
}
desc "setup_nginx" <<HEREDOC
Usage:
${_ME} setup_nginx
Description:
Create config for nginx
HEREDOC
setup_nginx() {
echo "Running setup_nginx..."
check_env_exists
ENV_DOCKER_WEB_HOSTNAME=${DOCKER_WEB_HOSTNAME:-}
# .env is the authority if it must be changed do it in the .env file
source .env
# if an env DOCKER_WEB_HOSTNAME is set and the .env is not yet defined use the env-var
if [[ "${DOCKER_WEB_HOSTNAME:-}" == "" && ! "${ENV_DOCKER_WEB_HOSTNAME}" == "" ]]; then
DOCKER_WEB_HOSTNAME=${ENV_DOCKER_WEB_HOSTNAME}
fi
if [[ "${DOCKER_WEB_HOSTNAME}" == "" && "${_INTERACTIVE}" == 1 ]]; then
read -rp "Choose hostname for nginx vhost [Env: DOCKER_WEB_HOSTNAME][localhost]: " given_web_hostname
DOCKER_WEB_HOSTNAME=${given_web_hostname:-localhost}
fi
echo "Set '${DOCKER_WEB_HOSTNAME}' as DOCKER_WEB_HOSTNAME in .env file"
sed -i -e "s/DOCKER_WEB_HOSTNAME.*$/DOCKER_WEB_HOSTNAME=${DOCKER_WEB_HOSTNAME}/" .env
rm -f .env-e # on mac -i is used for the backup extension, thus needs to clean the -e files
if [[ -e "docker/nginx/custom-conf/conf.d/default.conf" ]]; then
echo "[WARNING] Your 'docker/nginx/custom-conf/conf.d/default.conf' overrides the generated vhost config!"
fi
}
desc "generate_dhparam" <<HEREDOC
Usage:
${_ME} generate_dhparam
Description:
Create dhparam for nginx
HEREDOC
generate_dhparam() {
if [[ ! ${DHPARAM_SKIP:-} == "" ]]; then
echo "[WARNING] DHPARAM_SKIP is set, SKIPPING dhparam generation."
return
fi
echo "Running generate_dhparam..."
DHPARAM_FILE="docker/nginx/custom-conf/conf.d/ssl/dhparam.pem"
if [[ -e ${DHPARAM_FILE} ]]; then
echo " Already exists '${DHPARAM_FILE}'."
return
fi
mkdir -p docker/nginx/custom-conf/conf.d/ssl/
echo "Create Diffie Hellman param for nginx"
docker run -i --rm -v "$(pwd):/app" alpine \
sh -c "apk update && apk add --no-cache openssl &&
openssl dhparam -out /app/${DHPARAM_FILE} 4096"
}
desc "setup" <<HEREDOC
Usage:
${_ME} setup
Description:
is an alias for setup_all
HEREDOC
setup() {
setup_all
}
desc "setup_all" <<HEREDOC
Usage:
${_ME} setup_all
Description:
* setup environment - .env file with random db-passwords
* setup nginx - .env vhost configuration used by the nginx/web container
* setup docker - based on the choosen environment prod/dev/test
HEREDOC
setup_all() {
setup_env
setup_nginx
setup_docker
source .env
echo
echo "Use this command to start application:"
echo " ./run up "
echo
}
desc "up" <<HEREDOC
Usage:
${_ME} up
Description:
starts the docker-compose
HEREDOC
up() {
setup
echo "starting up..."
docker-compose up -d
CONTAINER_PHP=$(./run container_name php)
RETRY_COUNT=10
RETRIES=0
RETRY_WAIT=10
while [[ ${RETRIES} -lt ${RETRY_COUNT} ]]; do
STATUS=$(docker inspect --format '{{.State.Health.Status}}' "${CONTAINER_PHP}")
if [[ ${STATUS} == "healthy" ]]; then
welcome_prompt
exit 0
fi
RETRIES=$((RETRIES + 1))
echo "waiting for php container to startup. (sleeping ${RETRY_WAIT}) [${RETRIES}/${RETRY_COUNT}]"
sleep 10
done
echo "Container didn't become healthy within $((RETRY_WAIT * RETRY_COUNT)) seconds."
exit 1
}
desc "destroy" <<HEREDOC
Usage:
${_ME} destroy
Description:
stops and deletes docker data!!!!
HEREDOC
destroy() {
echo "THIS WILL DELETE ALL DATA IN DOCKER VOLUMES!"
echo "ARE YOU SURE? ctrl+c TO STOP!"
read -r
echo "ARE YOU REALLY SURE? ctrl+c TO STOP!"
read -r
docker-compose down -v --remove-orphans
}
desc "build" <<HEREDOC
Usage:
${_ME} build [arguments...]
Example:
${_ME} build --parallel php web
Description:
Runs the docker-compose build command, with given arguments