forked from bcgov/von-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage
executable file
·362 lines (306 loc) · 9.41 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
#!/bin/bash
export MSYS_NO_PATHCONV=1
export DOCKERHOST=${APPLICATION_URL-$(docker run --rm --net=host eclipse/che-ip)}
set -e
SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )"
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-von}"
export DEFAULT_CLI_SCRIPT_DIR='./cli-scripts'
# Running on Windows?
if [[ "$OSTYPE" == "msys" ]]; then
# Prefix interactive terminal commands ...
terminalEmu="winpty"
fi
# =================================================================================================================
# Usage:
# -----------------------------------------------------------------------------------------------------------------
usage () {
cat <<-EOF
Usage: $0 [command] [--logs] [options]
Commands:
build - Build the docker images for the project.
You need to do this first.
start | up - Starts all containers
When using the '--logs' option, use ctrl-c to exit logging. Use "down" or "stop" to stop the run.
Examples:
$0 start
$0 start --logs
$0 start <ip_proxy_1>,<ip_proxy_2>,<ip_proxy_3>,<ip_proxy_4> &
start-web - Start the web server to monitor an existing ledger, requires GENESIS_URL and LEDGER_SEED params
Example:
$0 start-web GENESIS_URL=http://foo.bar/genesis.txt LEDGER_SEED=00000000000000000000000000000012
logs - To tail the logs of running containers (ctrl-c to exit).
Use the '--no-tail' option to only print log without tailing.
Examples:
$0 logs
$0 logs --no-tail
down | rm - Brings down the services and removes the volumes (storage) and containers.
stop - Stops the services. This is a non-destructive process. The volumes and containers
are not deleted so they will be reused the next time you run start.
rebuild - Rebuild the docker images.
dockerhost - Print the ip address of the Docker Host Adapter as it is seen by containers running in docker.
indy-cli - Run Indy-Cli commands in a Indy-Cli container environment.
$0 indy-cli -h
- Display specific help documentation.
cli - Run a command in an Indy-Cli container.
$0 cli -h
- Display specific help documentation.
EOF
exit 1
}
indyCliUsage () {
cat <<-EOF
Usage:
$0 [options] indy-cli [-h] [command] [parameters]
Run Indy-Cli commands in a Indy-Cli container environment.
- Refer to the cli-scripts directory for available scripts and their parameters.
- Refer to './docs/Writing Transactions to a Ledger for an Un-privileged Author.md' for
additional examples.
Options:
-v <FullyQualifiedPathToScripts/>
- Mount a script volume to the container. By default the 'cli-scripts' directory is mounted to the container.
Examples:
$0 indy-cli
- Start an interactive indy-cli session in your Indy-Cli Container.
EOF
exit 1
}
cliUsage () {
cat <<-EOF
Usage:
$0 [options] cli [-h] [command]
Run a command in an Indy-Cli container.
Options:
-v <FullyQualifiedPathToScripts/>
- Mount a script volume to the container. By default the 'cli-scripts' directory is mounted to the container.
Examples:
$0 cli reset
- Reset your Indy-CLI container's environment
$0 cli init-pool localpool http://192.168.65.3:9000/genesis
$0 cli init-pool MainNet https://raw.githubusercontent.com/sovrin-foundation/sovrin/stable/sovrin/pool_transactions_live_genesis
- Initialize the pool for your Indy-CLI container's environment.
EOF
exit 1
}
# -----------------------------------------------------------------------------------------------------------------
# Initialization:
# -----------------------------------------------------------------------------------------------------------------
while getopts v:h FLAG; do
case $FLAG in
v ) VOLUMES=$OPTARG ;;
h ) usage ;;
\? ) #unrecognized option - show help
echo -e \\n"Invalid script option: -${OPTARG}"\\n
usage
;;
esac
done
shift $((OPTIND-1))
# -----------------------------------------------------------------------------------------------------------------
# Functions:
# -----------------------------------------------------------------------------------------------------------------
function toLower() {
echo $(echo ${@} | tr '[:upper:]' '[:lower:]')
}
function initDockerBuildArgs() {
dockerBuildArgs=""
# HTTP proxy, prefer lower case
if [[ "${http_proxy}" ]]; then
dockerBuildArgs=" ${dockerBuildArgs} --build-arg http_proxy=${http_proxy}"
else
if [[ "${HTTP_PROXY}" ]]; then
dockerBuildArgs=" ${dockerBuildArgs} --build-arg http_proxy=${HTTP_PROXY}"
fi
fi
# HTTPS proxy, prefer lower case
if [[ "${https_proxy}" ]]; then
dockerBuildArgs=" ${dockerBuildArgs} --build-arg https_proxy=${https_proxy}"
else
if [[ "${HTTPS_PROXY}" ]]; then
dockerBuildArgs=" ${dockerBuildArgs} --build-arg https_proxy=${HTTPS_PROXY}"
fi
fi
echo ${dockerBuildArgs}
}
function initEnv() {
if [ -f .env ]; then
while read line; do
if [[ ! "$line" =~ ^\# ]] && [[ "$line" =~ .*= ]]; then
export ${line//[$'\r\n']}
fi
done <.env
fi
for arg in "$@"; do
# Remove recognized arguments from the list after processing.
shift
case "$arg" in
*=*)
export "${arg}"
;;
--logs)
TAIL_LOGS=1
;;
*)
# If not recognized, save it for later procesing ...
set -- "$@" "$arg"
;;
esac
done
IP=""
IPS=""
if [ ! -z $(echo ${1} | grep '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}') ]; then
if [[ $1 == *","* ]]; then
IPS="$1"
else
IP="$1"
fi
fi
export IP="$IP" IPS="$IPS"
export LOG_LEVEL=${LOG_LEVEL:-info}
export RUST_LOG=${RUST_LOG:-warning}
}
function runCliCommand() {
unset displayCliUsage
for arg in "$@"; do
# Remove recognized arguments from the list after processing.
shift
case "$arg" in
-h)
displayCliUsage=1
;;
*)
# If not recognized, save it for later procesing ...
set -- "$@" "$arg"
;;
esac
done
initEnv "$@"
cliCmd="${1}"
shift || cliCmd=""
if [ ! -z "${displayCliUsage}" ] && [[ "${cliCmd}" == "indy-cli" ]]; then
indyCliUsage
elif [ ! -z "${displayCliUsage}" ] && [[ -z "${cliCmd}" ]]; then
cliUsage
fi
cmd="${terminalEmu} docker-compose \
--log-level ERROR run "
if [ -z "${VOLUMES}" ] && [ -d "${DEFAULT_CLI_SCRIPT_DIR}" ] ; then
VOLUMES=$(realpath ${DEFAULT_CLI_SCRIPT_DIR})
fi
if [ ! -z "${VOLUMES}" ]; then
shopt -s extglob
paths=$(echo "${VOLUMES}" | sed -n 1'p' | tr ',' '\n')
for path in ${paths}; do
path=${path%%+(/)}
mountPoint=${path##*/}
if [[ "$OSTYPE" == "msys" ]]; then
# When running on Windows, you need to prefix the path with an extra '/'
path="/${path}"
fi
cmd+=" --volume='${path}:/home/indy/${mountPoint}'"
done
fi
# Need to escape quotes and commas so they don't get removed along the way ...
escapedArgs=$(echo $@ | sed "s~'~\\\'~g" | sed 's~\"~\\"~g')
# Quote the escaped args so docker-compose does not try to perform any processing on them ...
# Separate the command and the args so they don't get treated as one argument by the scripts in the container ...
cmd+="
--rm client \
./scripts/manage ${cliCmd} \"${escapedArgs}\""
eval ${cmd}
}
function logs() {
(
local OPTIND
local unset _force
local unset no_tail
while getopts ":f-:" FLAG; do
case $FLAG in
f ) local _force=1 ;;
- )
case ${OPTARG} in
"no-tail"*) no_tail=1
;;
esac
esac
done
shift $((OPTIND-1))
log_args=()
(( no_tail != 1 )) && log_args+=( '-f' )
if [ ! -z "${TAIL_LOGS}" ] || [ ! -z "${_force}" ]; then
docker-compose \
--log-level ERROR logs \
"${log_args[@]}" "$@"
fi
)
}
# =================================================================================================================
pushd ${SCRIPT_HOME} >/dev/null
COMMAND=$(toLower ${1})
shift || COMMAND=usage
case "${COMMAND}" in
start|up)
initEnv "$@"
docker-compose \
--log-level ERROR up \
-d webserver node1 node2 node3 node4
logs
echo 'Want to see the scrolling container logs? Run "./manage logs"'
;;
start-combined)
initEnv "$@"
docker-compose \
--log-level ERROR up \
-d webserver nodes
logs
;;
start-web)
initEnv "$@"
if [ -z "$LEDGER_SEED" ]; then
export ANONYMOUS=1
fi
docker-compose \
--log-level ERROR up \
-d webserver
logs webserver
;;
synctest)
initEnv "$@"
docker-compose \
--log-level ERROR up \
-d synctest node1 node2 node3 node4
logs -f synctest
;;
cli)
runCliCommand $@
;;
indy-cli)
runCliCommand indy-cli $@
;;
logs)
initEnv "$@"
logs -f "$@"
;;
stop)
initEnv "$@"
docker-compose \
--log-level ERROR stop
;;
down|rm)
initEnv "$@"
docker-compose \
--log-level ERROR down \
-v
;;
build)
docker build $(initDockerBuildArgs) -t von-network-base .
;;
rebuild)
docker build --no-cache $(initDockerBuildArgs) -t von-network-base .
;;
dockerhost)
echo -e \\n"DockerHost: ${DOCKERHOST}"\\n
;;
*)
usage;;
esac
popd >/dev/null