-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbalance-tracker.sh
executable file
·561 lines (497 loc) · 15.7 KB
/
balance-tracker.sh
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
#! /bin/sh
set -e
print_help() {
cat <<-END
Usage: $0 <command> [subcommand] [option(s)]
Commands:
build Build the project
help Show this help screen and exit
install Perform installation steps
process-blocks Process blocks
run-tests Runs tests
serve Start server
install-app Installs Balance Tracker in HAF
Install subcommands:
jmeter Installs Apache Jmeter
postgrest Installs PostgREST
backend-runtime-dependencies Installs backend runtime dependencies
frontend-runtime-dependencies Installs frontend runtime dependencies
test-dependencies Installs test dependencies
Serve subcommands:
frontend Start frontend server
postgrest-backend Start PostgREST backend server
test-results Serve test results generated by run-tests command
Balance Tracker installation (install-app) options:
--postgres-host=HOSTNAME PostgreSQL hostname (default: localhost)
--postgres-port=PORT PostgreSQL port (default: 5432)
--postgres-user=USERNAME PostgreSQL user name (default: haf_admin)
--postgres-url=URL PostgreSQL URL (if set, overrides three previous options, empty by default)
--no-context=true|false When set to true, do not create context (default: false)
--no-context The same as '--no-context=true'
Block processing (process-blocks) options:
--number-of-blocks=INTEGER Number of blocks to process (default: 10^9), if set to value greater than number of blocks in the database (or 0),
indexer will wait for new blocks
--postgres-host=HOSTNAME PostgreSQL hostname (default: localhost)
--postgres-port=PORT PostgreSQL port (default: 5432)
--postgres-user=USERNAME PostgreSQL user name (default: btracker_owner)
--postgres-url=URL PostgreSQL URL (if set, overrides three previous options, empty by default)
--log-dir=DIR-PATH Directory for block procesing logs (default: unset),
setting this parameter will cause the block processing to run in the background,
logs will be rotated once they reach 5M size
Test running options:
--test-report-dir=PATH Directory where HTML test report will be generated
--test-result-path=PATH File where JTL test result will be generated
--test-thread-count=NUMBER Number of threads to be used to run tests (default: 8)
--test-loop-count=NUMBER Number of loops to be run during tests (default: 60)
--backend-port=PORT Port used by the backend (default: 3000)
--backend-host=HOSTNAME Hostname of backend's host (default: localhost)
Serving options:
--frontend-port=PORT Frontend port (default: 4000)
--log-dir=PATH Log directory for frontend and/or backend logs
--postgres-host=HOSTNAME PostgreSQL hostname (default: localhost)
--postgres-port=PORT PostgreSQL port (default: 5432)
--postgres-user=USERNAME PostgreSQL user name (default: btracker_user)
--postgres-url=URL PostgreSQL URL (if set, overrides three previous options, empty by default)
--postgrest-host=HOST PostgREST bind address (default: !4)
See https://postgrest.org/en/stable/references/configuration.html#server-host for
possible values
--postgrest-port=PORT PostgREST bind port (default: 3000)
--postgrest-admin-server-port=PORT PostgREST admin port (default: unset, health checks not available)
See https://postgrest.org/en/stable/references/admin.html#health-check for details
--test-server-port=PORT Port on which the test report is served
--test-report-dir=PATH Directory where HTML test report is located
END
}
build() {
subcommand=$1
shift
case "$subcommand" in
frontend)
echo "Building frontend"
cd gui
export NODE_ENV=production
npm install
npm run build
npx react-inject-env set
cd ..
echo "Finished building frontend"
;;
*)
echo "Unknown subcommand: $subcommand"
print_help
exit 1
esac
}
install_jmeter() {
version="5.6.2"
bin_path="/usr/local/bin/jmeter"
src_path="/usr/local/src/jmeter-$version"
echo "Installing Jmeter $version to $bin_path..."
wget "https://downloads.apache.org/jmeter/binaries/apache-jmeter-${version}.zip" -O jmeter.zip
unzip "jmeter.zip"
rm "jmeter.zip"
sudo mv "apache-jmeter-${version}" "$src_path"
jmeter="$bin_path-$version"
cat <<-_jmeter | sudo tee "$jmeter"
#!/usr/bin/env bash
cd "$src_path/bin"
./jmeter.sh "\$@"
_jmeter
sudo chmod +x "$jmeter"
sudo ln -sf "$jmeter" "$bin_path"
echo "Finished installing Jmeter"
}
install_postgrest() {
version="v11.1.0"
path="/usr/local/bin/postgrest"
echo "Installing PostgREST $version to $path..."
wget https://github.com/PostgREST/postgrest/releases/download/$version/postgrest-$version-linux-static-x64.tar.xz -O postgrest.tar.xz
tar -xJf postgrest.tar.xz
sudo mv postgrest "$path"
rm postgrest.tar.xz
echo "Finished installing PostgrREST"
}
install_backend_runtime_dependencies() {
echo "Installing backend runtime dependencies..."
sudo apt-get update
sudo apt-get -y install \
apache2-utils \
curl \
postgresql-client \
wget \
xz-utils
echo "Finished installing backend runtime dependencies"
}
install_frontend_runtime_dependencies() {
echo "Installing frontend runtime dependencies..."
sudo apt-get update
sudo apt-get -y install curl
curl https://get.volta.sh | bash
export VOLTA_HOME="$HOME/.volta"
export PATH="$VOLTA_HOME/bin:$PATH"
cd gui
npm install
cd ..
echo "Finished installing frontend runtime dependencies"
}
install_test_dependencies() {
echo "Installing test dependencies..."
sudo apt-get update
sudo apt-get -y install \
openjdk-8-jdk-headless \
python3 \
unzip
echo "Finished installing test dependencies"
}
install() {
subcommand=$1
shift
case "$subcommand" in
jmeter)
install_jmeter
;;
postgrest)
install_postgrest
;;
backend-runtime-dependencies)
install_backend_runtime_dependencies
;;
frontend-runtime-dependencies)
install_frontend_runtime_dependencies
;;
test-dependencies)
install_test_dependencies
;;
*)
echo "Unknown subcommand: $subcommand"
print_help
exit 1
esac
}
process_blocks() {
echo "Running indexer..."
echo "Arguments: $*"
block_number=${BLOCK_NUMBER:-null}
postgres_user=${POSTGRES_USER:-"btracker_owner"}
postgres_host=${POSTGRES_HOST:-"localhost"}
postgres_port=${POSTGRES_PORT:-5432}
postgres_url=${POSTGRES_URL:-""}
btracker_schema=${BTRACKER_SCHEMA:-"btracker_app"}
log_dir=${LOG_DIR:-}
while [ $# -gt 0 ]; do
case "$1" in
--number-of-blocks=*)
block_number="${1#*=}"
;;
--postgres-host=*)
postgres_host="${1#*=}"
;;
--postgres-port=*)
postgres_port="${1#*=}"
;;
--postgres-user=*)
postgres_user="${1#*=}"
;;
--postgres-url=*)
postgres_url="${1#*=}"
;;
--schema=*)
btracker_schema="${1#*=}"
;;
--log-dir=*)
log_dir="${1#*=}"
;;
-*)
echo "Unknown option: $1"
print_help
exit 1
;;
*)
echo "Unknown argument: $1"
print_help
exit 2
;;
esac
shift
done
if [ -z "$block_number" ]; then
block_number=1000000000
echo 'Running indexer for existing blocks and expecting new blocks...'
fi
postgres_access=${postgres_url:-"postgresql://$postgres_user@$postgres_host:$postgres_port/haf_block_log?application_name=btracker_block_processing"}
if [ -z "$log_dir" ]; then
echo "Running indexer in the foreground"
psql -a -v "ON_ERROR_STOP=1" "$postgres_access" -c "SET SEARCH_PATH TO ${btracker_schema};" -c "call ${btracker_schema}.main('${btracker_schema}', $block_number);"
echo "Finished running indexer"
else
echo "Running indexer in the background"
mkdir -p "$log_dir"
psql -a -v "ON_ERROR_STOP=1" "$postgres_access" -c "SET SEARCH_PATH TO ${btracker_schema};" -c "call ${btracker_schema}.main('${btracker_schema}', $block_number);" 2>&1 | /usr/bin/rotatelogs "$log_dir/process-blocks.%Y-%m-%d-%H_%M_%S.log" 5M &
fi
}
run_tests() {
test_scenario_path="$(pwd)/tests/performance/test_scenarios.jmx"
test_result_path=${TEST_RESULT_PATH:-"$(pwd)/tests/performance/result.jtl"}
test_report_dir=${TEST_REPORT_DIR:-"$(pwd)/tests/performance/result_report"}
test_thread_count=${TEST_THREAD_COUNT:-8}
test_loop_count=${TEST_LOOP_COUNT:-60}
backend_port=${BACKEND_PORT:-3000}
backend_host=${BACKEND_HOST:-localhost}
postgres_user=${POSTGRES_USER:-"btracker_owner"}
postgres_host=${POSTGRES_HOST:-"localhost"}
postgres_port=${POSTGRES_PORT:-5432}
postgres_url=${POSTGRES_URL:-""}
btracker_schema=${BTRACKER_SCHEMA:-"btracker_app"}
while [ $# -gt 0 ]; do
case "$1" in
--test-report-dir=*)
test_report_dir="${1#*=}"
;;
--test-result-path=*)
test_result_path="${1#*=}"
;;
--test-thread-count=*)
test_thread_count="${1#*=}"
;;
--test-loop-count=*)
test_loop_count="${1#*=}"
;;
--postgres-host=*)
postgres_host="${1#*=}"
;;
--postgres-port=*)
postgres_port="${1#*=}"
;;
--postgres-user=*)
postgres_user="${1#*=}"
;;
--postgres-url=*)
postgres_url="${1#*=}"
;;
--schema=*)
btracker_schema="${1#*=}"
;;
--backend-port=*)
backend_port="${1#*=}"
;;
--backend-host=*)
backend_host="${1#*=}"
;;
-*)
echo "Unknown option: $1"
print_help
exit 1
;;
*)
echo "Unknown argument: $1"
print_help
exit 2
;;
esac
shift
done
postgres_access=${postgres_url:-"postgresql://$postgres_user@$postgres_host:$postgres_port/haf_block_log"}
psql -a -v "ON_ERROR_STOP=1" "$postgres_access" -c "SET SEARCH_PATH TO ${btracker_schema};" -c "SELECT ${btracker_schema}.create_btracker_indexes();"
test_summary_report_path="${test_result_path%jtl}xml"
rm -f "$test_result_path"
mkdir -p "${test_result_path%/*}"
rm -rf "$test_report_dir"
mkdir -p "$test_report_dir"
jmeter --nongui --testfile "$test_scenario_path" --logfile "$test_result_path" \
--reportatendofloadtests --reportoutputfolder "$test_report_dir" \
--jmeterproperty backend.port="$backend_port" --jmeterproperty backend.host="$backend_host" \
--jmeterproperty thread.count="$test_thread_count" --jmeterproperty loop.count="$test_loop_count" \
--jmeterproperty summary.report.path="$test_summary_report_path"
}
start_frontend() {
echo "Starting frontend..."
echo "Arguments: $*"
frontend_port=${FRONTEND_PORT:-}
log_dir=${LOG_DIR:-}
while [ $# -gt 0 ]; do
case "$1" in
--frontend-port=*)
frontend_port="${1#*=}"
;;
--log-dir=*)
log_dir="${1#*=}"
;;
-*)
echo "Unknown option: $1"
print_help
exit 1
;;
*)
echo "Unknown argument: $1"
print_help
exit 2
;;
esac
shift
done
[ -n "$frontend_port" ] && export PORT="$frontend_port"
if [ -z "$log_dir" ]; then
echo "Running frontend in the foreground"
cd gui
npm run start
cd ..
echo "Finished running frontend"
else
echo "Running frontend in the background"
mkdir -p "$log_dir"
cd gui
npm run start 2>&1 | /usr/bin/rotatelogs "$log_dir/frontend.%Y-%m-%d-%H_%M_%S.log" 5M &
cd ..
fi
}
start_postgrest() {
echo "Starting PostgREST..."
echo "Arguments: $*"
postgres_user=${POSTGRES_USER:-"btracker_user"}
postgres_host=${POSTGRES_HOST:-"localhost"}
postgres_port=${POSTGRES_PORT:-5432}
postgres_url=${POSTGRES_URL:-""}
PGRST_SERVER_HOST=${PGRST_SERVER_HOST:-"!4"}
PGRST_SERVER_PORT=${PGRST_SERVER_PORT:-3000}
PGRST_ADMIN_SERVER_PORT=${PGRST_ADMIN_SERVER_PORT:-}
log_dir=${LOG_DIR:-}
while [ $# -gt 0 ]; do
case "$1" in
--postgres-host=*)
postgres_host="${1#*=}"
;;
--postgres-port=*)
postgres_port="${1#*=}"
;;
--postgres-user=*)
postgres_user="${1#*=}"
;;
--postgres-url=*)
postgres_url="${1#*=}"
;;
--postgrest-host=*)
PGRST_SERVER_HOST="${1#*=}"
;;
--postgrest-port=*)
PGRST_SERVER_PORT="${1#*=}"
;;
--postgrest-admin-server-port=*)
PGRST_ADMIN_SERVER_PORT="${1#*=}"
;;
--log-dir=*)
log_dir="${1#*=}"
;;
-*)
echo "Unknown option: $1"
print_help
exit 1
;;
*)
echo "Unknown argument: $1"
print_help
exit 2
;;
esac
shift
done
export PGRST_DB_URI="${postgres_url:-postgresql://$postgres_user@$postgres_host:$postgres_port/haf_block_log}"
export PGRST_SERVER_HOST
export PGRST_SERVER_PORT
export PGRST_ADMIN_SERVER_PORT
if [ -z "$log_dir" ]; then
echo "Running PostgREST in the foreground"
postgrest postgrest.conf
echo "Finished running PostgREST"
else
echo "Running PostgREST in the background"
mkdir -p "$log_dir"
postgrest postgrest.conf 2>&1 | /usr/bin/rotatelogs "$log_dir/balance-tracker.%Y-%m-%d-%H_%M_%S.log" 5M &
fi
}
serve_test_results() {
echo "Starting test result server..."
echo "Arguments: $*"
port=${TEST_SERVER_PORT:-8000}
test_report_dir=${TEST_REPORT_DIR:-"$(pwd)/tests/performance/result_report"}
while [ $# -gt 0 ]; do
case "$1" in
--test-report-dir=*)
test_report_dir="${1#*=}"
;;
--test-server-port=*)
port="${1#*=}"
;;
-*)
echo "Unknown option: $1"
print_help
exit 1
;;
*)
echo "Unknown argument: $1"
print_help
exit 2
;;
esac
shift
done
python3 -m http.server --directory "$test_report_dir" "$port"
}
serve() {
echo "Starting server..."
echo "Arguments: $*"
subcommand=$1
shift
case "$subcommand" in
frontend)
start_frontend "$@"
;;
postgrest-backend)
start_postgrest "$@"
;;
test-results)
serve_test_results "$@"
;;
*)
echo "Unknown subcommand: $subcommand"
print_help
exit 1
esac
echo "Done"
}
install_app() {
echo "Installing Balance Tracker in HAF..."
echo "Arguments: $*"
./scripts/install_app.sh "$@"
echo "Finished installing Balance Tracker in HAF"
}
command=$1
shift
case "$command" in
build)
build "$@"
;;
install)
install "$@"
;;
process-blocks)
process_blocks "$@"
;;
run-tests)
run_tests "$@"
;;
serve)
serve "$@"
;;
install-app)
install_app "$@"
;;
help | --help | -?)
print_help
exit 0
;;
*)
echo "Unknown command: $command"
print_help
exit 1
;;
esac