-
Notifications
You must be signed in to change notification settings - Fork 2
/
hive-ctl
executable file
·213 lines (192 loc) · 4.64 KB
/
hive-ctl
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
#!/usr/bin/env bash
COMMAND="${1}"
shift
set -e
if [ -n "${HW_BW_BASE_DIR}" ]; then
if [ -f "${HW_BW_BASE_DIR}/bin/common-functions.sh" ]; then
. "${HW_BW_BASE_DIR}/bin/common-functions.sh"
else
echo >&2 "${HW_BW_BASE_DIR}/bin/common-functions.sh not found"
exit 1
fi
elif [ -f "${PWD}/bin/common-functions.sh" ]; then
. "${PWD}/bin/common-functions.sh"
HW_BW_BASE_DIR="${PWD}"
else
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
if [ -f "${SCRIPT_DIR}/bin/common-functions.sh" ]; then
. "${SCRIPT_DIR}/bin/common-functions.sh"
HW_BW_BASE_DIR="${SCRIPT_DIR}"
else
echo >&2 "common-functions.sh not found"
exit 1
fi
fi
#
# print usage info (help)
#
usage() {
echo "Usage:"
echo "${BASH_SOURCE[0]} <command> [args]"
echo "Commands:"
echo "* 'check': check if can run."
echo "* 'config', 'reconfig': configure application before start & reconfigure after update."
echo "* 'run': run application interactively (logs in console)."
echo "* 'start' start application detached and wait while ready."
echo "* 'stop' stop application."
echo "* 'get-config-value' get config value."
echo "Notes:"
echo "* config performed automatically on first run."
echo "* need to run config/reconfig after application update."
echo "* need to run config/reconfig after any changes in 'config/user.ini'."
echo "* if you start the application using the 'run' command, you will not"
echo " get default 'root@ro.ot' user password in the console. "
echo " But you may get it with command '${BASH_SOURCE[0]} get-config-value b.root.password'"
}
#
# Print error message, print usage and exit
# args:
# * error_message: message to print
#
error_usage_exit() {
local error_message=$1
print_level_message 2 ERROR "${error_message}"
usage >&2
exit 1
}
#
# show ascii logo file if exists
#
show_logo() {
if [ -f "${HW_BW_BASE_DIR}/ascii-logo.txt" ]; then
cat "${HW_BW_BASE_DIR}/ascii-logo.txt"
fi
}
#
# Run any long task and log BEGIN and FINISH or FAILED
#
run_long_task_with_log() {
local run_cmd=$1; shift;
local message=$1; shift;
print_level_message 2 INFO "BEGIN ${message}"
local cmd_exit_usage
"${run_cmd}" "$@"
cmd_exit_usage="$?"
if [ "${cmd_exit_usage}" == 0 ]; then
print_level_message 2 SUCCESS "FINISH ${message}"
elif [ "${cmd_exit_usage}" == 130 ]; then
# By Ctrl+C we got 2 (SIGINT) + 128 = 130
print_level_message 2 SUCCESS "STOPPED ${message}"
else
print_level_message 2 ERROR "FAILED ${message}"
fi
}
#
# load images attached to fat bundles
#
load_images() {
if [ -f load-images.sh ]; then
echo "Load docker images..."
if ! bash -e load-images.sh; then
fail_installation "Can not load images"
fi
echo "... DONE"
fi
}
app_check() {
show_logo
check_prereq "${MIN_CPU}" "${MIN_RAM}"
}
#
# config application
#
app_reconfig() {
show_logo
load_images
check_prereq "${MIN_CPU}" "${MIN_RAM}"
make_reconfig
}
#
# start application
#
app_start() {
show_logo
check_prereq "${MIN_CPU}" "${MIN_RAM}"
if ! is_configured; then
load_images
run_long_task_with_log make_reconfig "config application" || return 1
fi
run_docker_compose up --detach
service_await_and_welcome
}
#
# stop application
#
app_stop() {
if ! is_configured; then
print_level_message 2 ERROR "Application not configure yet so nothing to stop."
return 1
fi
run_docker_compose down
}
#
# run application
#
app_run() {
show_logo
check_prereq "${MIN_CPU}" "${MIN_RAM}"
if ! is_configured; then
load_images
run_long_task_with_log make_reconfig "config application" || return 1
fi
run_docker_compose up
}
#
# get config value
#
app_get_config_value() {
local property_key=$1
local property_value
property_value=$(find_in_configs "${property_key}")
if [ "${property_value}" = "<unknown>" ]; then
echo >&2 "config property '${property_key}' not found"
exit 1
fi
echo "${property_value}"
}
#
# run command supported
#
run_command() {
local command=$1; shift;
case "${command}" in
help|--help)
usage
;;
check)
app_check "$@"
;;
config|reconfig)
run_long_task_with_log app_reconfig "config application" "$@"
;;
start)
run_long_task_with_log app_start "start application detached" "$@"
;;
stop)
run_long_task_with_log app_stop "stop application" "$@"
;;
run)
run_long_task_with_log app_run "run application interactively" "$@"
;;
get-config-value)
app_get_config_value "$@"
;;
*)
error_usage_exit "Unknown command: ${command}"
;;
esac
}
if [ -z "${COMMAND}" ]; then
error_usage_exit "No command provided"
fi
run_command "${COMMAND}" "$@"