forked from bcgov/von-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage
executable file
·1025 lines (878 loc) · 31.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
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
#!/bin/bash
export MSYS_NO_PATHCONV=1
# Default Options
export TA_RATIFICATION_TIME_OPS='+%s --date='
export STAT_OPS='-c '%a''
# MAC OS Options
if [[ $OSTYPE == 'darwin'* ]]; then
# Set default platform to linux/amd64 when running on Arm based MAC since there are no arm based images available currently.
architecture=$(uname -m)
if [[ "${architecture}" == 'arm'* ]] || [[ "${architecture}" == 'aarch'* ]]; then
export DOCKER_DEFAULT_PLATFORM=linux/amd64
fi
# Set the date and stat options appropriatly for MAC OS.
export TA_RATIFICATION_TIME_OPS='-jf '%Y-%m-%dT%H:%M:%S%Z' +%s '
export STAT_OPS='-f '%A''
fi
# getDockerHost; for details refer to https://github.com/bcgov/DITP-DevOps/tree/main/code/snippets#getdockerhost
. /dev/stdin <<<"$(cat <(curl -s --raw https://raw.githubusercontent.com/bcgov/DITP-DevOps/main/code/snippets/getDockerHost))"
export DOCKERHOST=$(getDockerHost)
SCRIPT_HOME="$( cd "$( dirname "$0" )" && pwd )"
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-von}"
export TMP_FOLDER='./tmp'
export CLI_SCRIPTS_FOLDER='./cli-scripts'
export DEFAULT_CLI_SCRIPT_DIR="${CLI_SCRIPTS_FOLDER}"
export LEDGER_TIMEOUT="${LEDGER_TIMEOUT:-60}"
export LEDGER_URL_CONFIG="${LEDGER_URL_CONFIG}"
export ROOT_BACKUP_DIR=backup
export SHELL_CMD='bash'
# ========================================================================================================
# Check Docker Compose
# --------------------------------------------------------------------------------------------------------
# Default to deprecated V1 'docker-compose'.
dockerCompose="docker-compose --log-level ERROR"
# Prefer 'docker compose' V2 if available
if [[ $(docker compose version 2> /dev/null) == 'Docker Compose'* ]]; then
dockerCompose="docker --log-level error compose"
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
You can include a '--wait' parameter which will wait until the ledger is active
You can include a '--taa-sample' parameter which will install the sample Transaction Authorisation Agreement
files into the ledger. Alternatively you can create your own config/aml.json and config/taa.json files which
will get installed into the ledger at start (see config/sample_aml.json and config/sample_taa.json for format)
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> &
$0 start --wait --logs
$0 start --taa-sample
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.
generateSecrets - Generate a random set of secrets using openssl; a Seed and a Key.
generateDid - Generates a DID and Verkey from a Seed.
$0 generateDid [seed]
- Optional [seed]; if one is not provided a random one will be generated using openssl.
generateGenesisFiles - Generates pool and domain genesis files from data input via csv files.
$0 generategenesisfiles <trustees_csv_file> <stewards_csv_file>
This is a convenience command wrapped around the Steward Tools script for generating genesis files found here;
https://github.com/sovrin-foundation/steward-tools/tree/master/create_genesis
The script is downloaded and hosted in a running container which has the required packages installed.
Examples of the csv files can be downloaded from here;
https://docs.google.com/spreadsheets/d/1LDduIeZp7pansd9deXeVSqGgdf0VdAHNMc7xYli3QAY/edit#gid=0
Download each sheet separately in csv format and fill them out with the data specific to your network.
The input csv files must be placed into the ./tmp/ folder.
The resulting output 'pool_transactions_genesis' and 'domain_transactions_genesis' files will be placed
in the ./tmp/ folder.
Example:
$0 generategenesisfiles "./tmp/CANdy Beta Genesis File Node info - Trustees.csv" "./tmp/CANdy Beta Genesis File Node info - Stewards.csv"
apply-taa - Registers an AML and TAA on a given network.
This is a convent wrapper around the cli-scripts/apply-taa batch script,
which simplifies and automates the process of registering an AML and TAA.
The following parameters are simply the standard inputs for the indy-cli batch script.
Refer to the cli-scripts/apply-taa documentation for details;
- walletName - Required
- storageType - Optional
- storageConfig - Optional
- storageCredentials - Optional
- poolName - Required
- useDid - Required
The following parameters are required:
amlUrl
- The URL to the raw content of the AML. Provided the link contains the AML expected content in json format
(contained in {}s) it will be parsed from the link's text.
amlVersion
- The version of the AML.
taaUrl
- The URL to the raw content of the TAA in markdown format.
taaVersion
- The version of the TAA
taaRatificationTime
- The date and time of TAA ratification by network government.
- On Linux and Windows (Git Bash):
- The date format is "%Y-%m-%dT%H:%M:%S%Z", where `%Z` is a numeric time zone offset, or an alphabetic time zone abbreviation.
- Examples:
- "2022-08-09T11:05:00-0700"
- "2022-08-09T11:05:00PDT"
- On Mac:
- The date format is "%Y-%m-%dT%H:%M:%S%Z", where `%Z` is an alphabetic time zone abbreviation.
- Example:
- "2022-08-09T11:05:00PDT"
Example:
$0 apply-taa \
walletName=local_net_trustee_wallet \
poolName=local_net \
useDid=V4SGRU86Z58d6TV7PBUe6f \
amlUrl='https://raw.githubusercontent.com/wiki/bcgov/bc-vcpedia/(Layer-1)-CANdy-Acceptance-Mechanism-List-(AML).md' \
amlVersion=0.1 \
taaUrl='https://raw.githubusercontent.com/wiki/bcgov/bc-vcpedia/(Layer-1)-CANdy-Transaction-Author-Agreement.md' \
taaVersion=0.1 \
taaRatificationTime="2022-08-09T11:05:00PDT"
truncateLogs - Truncate the container logs.
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.
backup [description] - Backup the current von-network environment.
Creates a set of tar.gz archives of each of the environment's volumes.
Backup sets are stored in a ./backup/date/time folder structure.
Examples:
$0 backup
$0 backup "The description of my environment's current state."
- Make a backup and include the description in a ReadMe.txt file.
restore [filter] - Restore a given backup set. Defaults to restoring the most recent backup.
Examples:
$0 restore
- Restore the most recent backup.
$0 restore 13-15-37
- Restore the backup from 13-15-37 today.
$0 restore von_client-data_2021-08-23_08-21-08.tar
- Restore the backup set containing the von_client-data_2021-08-23_08-21-08.tar archive.
$0 restore 2021-08-23
- Restore the most recent backup set from 2021-08-23.
restoreArchive <archive> <volume> [tarOptions]- Restore a tar.gz archive to a named volume.
Useful for restoring an archive for inspection and debugging purposes.
Examples:
$0 restoreArchive ./backup/Node3-ledger-backup.tar.gz node3-bcovrin-tes
$0 restoreArchive ./backup/Node3-ledger-backup.tar.gz node3-bcovrin-test --strip=1
- Restore the archive to the named volume, stripping the first level directory from the archive.
- Useful in the scenario where the archive contains additional directory levels that aren’t needed in the restored copy.
debugVolume <volume> [volumeMountFolder] - Mount a named volume into a 'debug' instance of the 'von-network-base' image with an interactive shell.
Provides a containerized environment to perform analysis on the ledger databases and files.
Starting with 'bcgovimages/von-image:node-1.12-4' the base image for von-network contains the RocksDB sst_dump tool that can be used to verify
and inspect the RocksDB database files; '*.sst' files.
For example the command 'find /debug_volume/ -name "*.sst" | xargs -I {} sst_dump --file={} --command=verify' can be used to do a quick
verification on all the database files once the container starts.
Usage information for sst_dump can be found here; https://github.com/facebook/rocksdb/wiki/Administration-and-Data-Access-Tool
Examples:
$0 debugVolume node3-bcovrin-test
$0 debugVolume node1-bcovrin-test /home/indy/ledger
- Mount the named volume to '/home/indy/ledger'
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.
$0 indy-cli --help
- Get usage information for the indy-cli.
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
;;
--wait)
WAIT_FOR_LEDGER=1
;;
--taa-sample)
USE_SAMPLE_TAA=1
;;
*)
# If not recognized, save it for later processing ...
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 LEDGER_SEED=${LEDGER_SEED}
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 processing ...
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="${dockerCompose} \
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+=" -v '${path}:/home/indy/${mountPoint}:Z'"
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' | 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
${dockerCompose} \
logs \
"${log_args[@]}" "$@"
fi
)
}
pingLedger(){
ledger_url=${1}
# ping ledger web browser for genesis txns
local rtnCd=$(curl -s --write-out '%{http_code}' --output /dev/null ${ledger_url}/genesis)
if (( ${rtnCd} == 200 )); then
return 0
else
return 1
fi
}
function wait_for_ledger() {
(
# if flag is set, wait for ledger to activate before continuing
local rtnCd=0
if [ ! -z "${WAIT_FOR_LEDGER}" ]; then
# Wait for ledger server to start ...
local startTime=${SECONDS}
# use global LEDGER_URL
local LEDGER_URL="${LEDGER_URL_CONFIG:-http://localhost:9000}"
printf "waiting for ledger to start"
while ! pingLedger "$LEDGER_URL"; do
printf "."
local duration=$(($SECONDS - $startTime))
if (( ${duration} >= ${LEDGER_TIMEOUT} )); then
echoRed "\nThe Indy Ledger failed to start within ${duration} seconds.\n"
rtnCd=1
break
fi
sleep 1
done
fi
return ${rtnCd}
)
}
function install_taa() {
(
# if flag is set, copy the sample config/sample_aml.json and config/sample_taa.json
# to config/aml.json and config/taa.json. Also create a marker file so that we
# clean up on shutdown and still support backward compatibility where people previously
# their own custom versions and don't want them removed.
local rtnCd=0
if [ ! -z "${USE_SAMPLE_TAA}" ]; then
rtnCd=$(cp -f ./config/sample_aml.json ./config/aml.json) && $(cp -f ./config/sample_taa.json ./config/taa.json)
touch ./config/.samples.used
fi
return ${rtnCd}
)
}
function remove_taa() {
(
# if the marker exists indicating we created the aml and taa files, make sure we remove them to clean up.
if [ -f "./config/.samples.used" ]; then
rm -f ./config/aml.json ./config/taa.json ./config/.samples.used
fi
)
}
function generateKey(){
(
_length=${1:-48}
# Format can be `-base64` or `-hex`
_format=${2:--base64}
echo $(openssl rand ${_format} ${_length})
)
}
function generateSeed(){
(
_prefix=${1}
_seed=$(echo "${_prefix}$(generateKey 32)" | fold -w 32 | head -n 1 )
_seed=$(echo -n "${_seed}")
echo ${_seed}
)
}
function generateSecrets() {
echo
echo "Seed: $(generateSeed)"
echo "Key: $(generateKey)"
echo
}
function generateDid() {
seed=${1}
if [ -z ${seed} ]; then
seed=$(generateSeed)
fi
runCliCommand python cli-scripts/generate_did.py --seed ${seed}
}
function generateGenesisFiles() {
trustee_csv="${1}"
steward_csv="${2}"
genesis_from_files_filename="genesis_from_files.py"
genesis_from_files_url="https://raw.githubusercontent.com/sovrin-foundation/steward-tools/master/create_genesis/genesis_from_files.py"
if [ -z "${trustee_csv}" ] || [ -z "${steward_csv}" ]; then
echoYellow "You must supply both the trustee and steward csv files."
exit 1
fi
if [[ "${trustee_csv}" != ${TMP_FOLDER}/* ]]; then
trustee_csv="${TMP_FOLDER}/${trustee_csv}"
fi
if [ ! -f "${trustee_csv}" ]; then
echoYellow "${trustee_csv} not found, please make sure you placed ${trustee_csv} in the ${TMP_FOLDER} folder."
exit 1
fi
if [[ "${steward_csv}" != ${TMP_FOLDER}/* ]]; then
steward_csv="${TMP_FOLDER}/${steward_csv}"
fi
if [ ! -f "${steward_csv}" ]; then
echoYellow "${steward_csv} not found, please make sure you placed ${steward_csv} in the ${TMP_FOLDER} folder."
exit 1
fi
if [ ! -f "${CLI_SCRIPTS_FOLDER}/${genesis_from_files_filename}" ]; then
echo "Downloading the latest version of ${genesis_from_files_filename} from ${genesis_from_files_url} ..."
curl -s -L -o ${CLI_SCRIPTS_FOLDER}/${genesis_from_files_filename} ${genesis_from_files_url}
chmod +x ${CLI_SCRIPTS_FOLDER}/${genesis_from_files_filename}
fi
# Escape spaces in path ...
trustee_csv_esc=$(echo ${trustee_csv##*/} | sed 's/ /\\ /g')
steward_csv_esc=$(echo ${steward_csv##*/} | sed 's/ /\\ /g')
runCliCommand cli-scripts/${genesis_from_files_filename} --pool /tmp/pool_transactions --domain /tmp/domain_transactions --trustees /tmp/${trustee_csv_esc} --stewards /tmp/${steward_csv_esc}
}
function apply-taa() {
# Parse Args
for arg in "$@"; do
# Remove recognized arguments from the list after processing.
shift
case "${arg}" in
*=*)
if [[ "${arg}" == *"amlUrl"* ]]; then
amlUrl=${arg#*=}
continue
fi
if [[ "${arg}" == *"taaUrl"* ]]; then
taaUrl=${arg#*=}
continue
fi
if [[ "${arg}" == *"taaRatificationTime"* ]]; then
taaRatificationTime=${arg#*=}
taaRatificationTimestamp=$(date ${TA_RATIFICATION_TIME_OPS}"${taaRatificationTime}")
continue
fi
batchFileArgs+=" ${arg}"
;;
esac
done
# Create temp files for the AML and ATT
amlPath="${TMP_FOLDER}/aml-$(date +%s).txt"
taaPath="${TMP_FOLDER}/att-$(date +%s).txt"
# Download the AML and ATT files
curl -s -o ${amlPath} "${amlUrl}"
curl -s -o ${taaPath} "${taaUrl}"
# Parse the AML content from the file; the bit inside the '{}'s
amlContent="{$(sed -n '/{/,/}/{/{/!{/}/!p;};}' ${amlPath} | sed 's/^ *//g')}"
echo ${amlContent} > ${amlPath}
# Generate the required apply-taa batch script parameters
batchFileArgs+=" amlFile=/tmp/${amlPath##*/}"
batchFileArgs+=" amlContext=${amlUrl}"
batchFileArgs+=" taaFile=/tmp/${taaPath##*/}"
batchFileArgs+=" taaRatificationTimestamp=${taaRatificationTimestamp}"
# Run the apply-taa batch script
runCliCommand indy-cli --config /tmp/cliconfig.json apply-taa \
${batchFileArgs}
# Remove the temp files
rm "${amlPath}" "${taaPath}"
}
function backup() {
(
_msg=$@
volumes=$(${dockerCompose} config --volumes)
timeStamp=`date +\%Y-\%m-\%d_%H-%M-%S`
datePart=${timeStamp%%_*}
timePart=${timeStamp#*_}
backupDir=${ROOT_BACKUP_DIR}/${datePart}/${timePart}
backupVolumeMount=$(getVolumeMount ./${ROOT_BACKUP_DIR})/
mkdir -p ./${backupDir}
chmod -R ug+rw ./${backupDir}
if [ ! -z "${_msg}" ]; then
echo "${_msg}" > ./${backupDir}/ReadMe.txt
fi
for volume in ${volumes}; do
volume=$(echo ${volume} |sed 's~\r$~~')
sourceVolume=${COMPOSE_PROJECT_NAME}_${volume}
archiveName=${sourceVolume}_${timeStamp}.tar.gz
archivePath="/${backupDir}/${archiveName}"
echoYellow \\n"Backing up ${sourceVolume} to ${archivePath} ..."
docker run \
--rm \
--name von-network-backup \
-v ${backupVolumeMount}:/${ROOT_BACKUP_DIR} \
-v ${sourceVolume}:/source_volume von-network-base \
tar -czvf ${archivePath} -C /source_volume/ .
done
)
}
function restore() {
(
_fileName=${1}
archivePath=$(findBackup ${1})
archiveDirectory=${archivePath%/*}
datePart=$(echo ${archivePath} | awk -F_ '{print $3}')
timePart=$(echo ${archivePath} | awk -F_ '{print $4}')
archiveSuffix="${datePart}_${timePart}"
if promptForConfirmation "You are about to restore from the '${archiveDirectory}' backup set.\nYour existing data will be lost if not backed up first."; then
volumes=$(${dockerCompose} config --volumes)
for volume in ${volumes}; do
volume=$(echo ${volume} |sed 's~\r$~~')
targetVolume=${COMPOSE_PROJECT_NAME}_${volume}
archiveName=${targetVolume}_${archiveSuffix}
archivePath="${archiveDirectory}/${archiveName}"
restoreArchive -q "${archivePath}" "${targetVolume}"
done
else
echo -e \\n"Restore aborted."
fi
)
}
function restoreArchive()
{
(
local OPTIND
local quiet
unset quiet
while getopts q FLAG; do
case $FLAG in
q ) quiet=1 ;;
esac
done
shift $((OPTIND-1))
archive=${1}
volume=${2}
tarOptions=${3} # Example "--strip=1", to remove the first directory level.
if [ -z ${archive} ] || [ -z ${volume} ]; then
echoYellow "You must supply the path to the archive and the name of the volume to which the archive will be restored."
exit 1
fi
archiveFolder=${archive%/*}
archiveName=${archive##*/}
archiveToRestore=/${ROOT_BACKUP_DIR}/${archiveName}
archiveVolumeMount=$(getVolumeMount ${archiveFolder})
if [ ! -z "${quiet}" ] || promptForConfirmation "You are about to restore '${archive}' to ${volume}.\nYour existing data will be lost if not backed up first." ; then
deleteVolume ${volume}
echoYellow \\n"Restoring ${volume} from ${archive} ..."
docker run \
--rm \
--name von-network-restore \
--user root \
-v ${archiveVolumeMount}:/${ROOT_BACKUP_DIR} \
-v ${volume}:/target_volume von-network-base \
tar --same-owner -xvpf ${archiveToRestore} -C /target_volume/ ${tarOptions}
else
echo -e \\n"Restore aborted."
fi
)
}
function debugVolume()
{
(
volume=${1}
volumeMountFolder=${2:-/debug_volume}
if [ -z ${volume} ]; then
echoYellow "You must supply the name of the volume to attach to the debug session."
exit 1
fi
backupVolumeMount=$(getVolumeMount ./${ROOT_BACKUP_DIR})/
echo -e "\nOpening a debug session with the following volume mounts:\n - '${volume}':'${volumeMountFolder}'\n - '${backupVolumeMount}':'/${ROOT_BACKUP_DIR}'\n"
docker run \
--rm \
-it \
--network="host" \
--user root \
-v ${backupVolumeMount}:/${ROOT_BACKUP_DIR} \
-v ${volume}:${volumeMountFolder} \
--entrypoint ${SHELL_CMD} \
von-network-base
)
}
function findBackup(){
(
_fileName=${1}
# If no backup file was specified, find the most recent set.
# Otherwise treat the value provided as a filter to find the most recent backup set matching the filter.
if [ -z "${_fileName}" ]; then
_fileName=$(find ${ROOT_BACKUP_DIR}* -type f -printf '%T@ %p\n' | grep .tar.gz | sort | tail -n 1 | sed 's~^.* \(.*$\)~\1~')
else
_fileName=$(find ${ROOT_BACKUP_DIR}* -type f -printf '%T@ %p\n' | grep .tar.gz | grep ${_fileName} | sort | tail -n 1 | sed 's~^.* \(.*$\)~\1~')
fi
echo "${_fileName}"
)
}
# OSX ships with an old version of Bash that does not support the \e escape character.
function echoYellow (){
(
_msg=${1}
_yellow='\x1B[33m'
_nc='\x1B[0m' # No Color
echo -e "${_yellow}${_msg}${_nc}" >&2
)
}
function promptForConfirmation(){
(
_msg=${@}
echoYellow "\n${_msg}"
read -n1 -s -r -p $'\x1B[33mWould you like to continue?\x1B[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 deleteVolume() {
(
volume=${1}
echoYellow \\n"Deleting volume '${volume}' ..."
containerId=$(docker volume rm ${volume} 2>&1 >/dev/null | sed -e 's~.*\[\(.*\)\]~\1~' | grep -v ${volume})
if [ ! -z "${containerId}" ]; then
# The volume is in use by a container. Remove the container before deleting the volume.
docker stop ${containerId} > /dev/null 2>&1
docker rm ${containerId} > /dev/null 2>&1
docker volume rm ${volume} > /dev/null 2>&1
fi
)
}
function deleteVolumes() {
(
_projectName=${COMPOSE_PROJECT_NAME:-docker}
echoYellow \\n"Stopping and removing any running containers ..."
${dockerCompose} down -v
_pattern="^${_projectName}_\|^docker_"
_volumes=$(docker volume ls -q | grep ${_pattern})
if [ ! -z "${_volumes}" ]; then
echoYellow "Removing project volumes ..."
echo ${_volumes} | xargs docker volume rm
fi
)
}
function getVolumeMount() {
path=${1}
path=$(realpath ${path})
path=${path%%+(/)}
if [[ "$OSTYPE" == "msys" ]]; then
# When running on Windows, you need to prefix the path with an extra '/'
path="/${path}"
fi
echo ${path}
}
function checkFolderPermissions() {
# Create the tmp folder if it does not exist ...
if [ ! -d "${TMP_FOLDER}" ]; then
echo "Creating ${TMP_FOLDER} ..."
mkdir -p tmp
fi
# Ensure folder permissions are set correctly for use inside the docker container ...
setFolderReadWriteAll "${TMP_FOLDER}"
setFolderReadWriteAll "${CLI_SCRIPTS_FOLDER}"
}
function setFolderReadWriteAll() {
# This has no impact on Windows. The chmod command will run but the permissions don't actually change.
if [[ "$OSTYPE" != "msys" ]]; then
folder=${1}
permissions=$(stat ${STAT_OPS} ${folder})
if [[ "${permissions:0-1}" != 7 ]]; then
echo "Setting ${folder} to read/write for all users ..."
chmod a+rws ${folder}
fi
fi
}
function isUsingWSL() {
kernelVersion=$(docker -l error info 2> /dev/null | grep "Kernel Version")
if [[ "$OSTYPE" == "msys" ]] && [[ "${kernelVersion}" == *'WSL'* ]]; then
return 0
else
return 1
fi
}
function getContainers() {
initEnv "$@"
${dockerCompose} \
ps --all | tail -n +2 | awk '{ print $1 }'
}
varDockerFolder="/var/lib/docker"
varDockerFolderMsys="//wsl$/docker-desktop-data/version-pack-data/community/docker"
function truncateLogs() {
if [[ "$OSTYPE" == "msys" ]] && ! isUsingWSL ; then
echoYellow "Currently, the truncateLogs function is only supported in Windows when using Docker on WSL"
exit 1
fi
containers=$(getContainers)
for container in ${containers}; do
log=$(docker inspect -f '{{.LogPath}}' ${container} 2> /dev/null)
if [[ "$OSTYPE" == "msys" ]]; then
log="${log/${varDockerFolder}/${varDockerFolderMsys}}"
fi
if [ -f "${log}" ]; then
echo "Truncating log for ${container}: ${log}"
truncate -s 0 ${log}
else
echoYellow "Unable to locate log for ${container}: ${log}"
fi
done
}
# =================================================================================================================
pushd "${SCRIPT_HOME}" >/dev/null
checkFolderPermissions
COMMAND=$(toLower ${1})
shift || COMMAND=usage
case "${COMMAND}" in
start|up)
initEnv "$@"
export LEDGER_SEED=${LEDGER_SEED:-000000000000000000000000Trustee1}
install_taa
${dockerCompose} \
up \
-d webserver node1 node2 node3 node4
wait_for_ledger
logs
echo 'Want to see the scrolling container logs? Run "./manage logs"'
;;
start-combined)
initEnv "$@"
install_taa
${dockerCompose} \
up \
-d webserver nodes
wait_for_ledger
logs
;;
start-web)
initEnv "$@"
${dockerCompose} \
up \
-d webserver
wait_for_ledger
logs webserver
;;
synctest)
initEnv "$@"
${dockerCompose} \
up \
-d synctest node1 node2 node3 node4
logs -f synctest
;;
cli)
runCliCommand $@
;;
indy-cli)
runCliCommand indy-cli $@
;;
logs)
initEnv "$@"
logs -f "$@"
;;
stop)
initEnv "$@"
${dockerCompose} \
stop
remove_taa
;;
down|rm)
initEnv "$@"
deleteVolumes
remove_taa
;;
build)
docker build $(initDockerBuildArgs) -t von-network-base .
;;
rebuild)
docker build --no-cache --progress plain $(initDockerBuildArgs) -t von-network-base .
;;
dockerhost)
echo -e \\n"DockerHost: ${DOCKERHOST}"\\n
;;
generatesecrets)
generateSecrets
;;
generatedid)
generateDid $@
;;
generategenesisfiles)
trustee_csv="${1}"
steward_csv="${2}"
generateGenesisFiles "${trustee_csv}" "${steward_csv}"
;;
apply-taa)
apply-taa $@
;;
backup)