-
Notifications
You must be signed in to change notification settings - Fork 721
/
nomad.sh
4121 lines (3914 loc) · 182 KB
/
nomad.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
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
usage_nomadbackend() {
usage "nomad" "Backend: manages a Cardano cluster using Nomad" <<EOF
Please see documentation for 'wb backend' for the supported commands.
- Nomad backend specific:
$(blue Subcommands that need a RUN-DIR parameter:)
$(helpcmd task-program-start RUN-DIR NOMAD-TASK-NAME SUPERVISORCTL-PROGRAM)
$(helpcmd task-program-stop RUN-DIR NOMAD-TASK-NAME SUPERVISORCTL-PROGRAM)
$(helpcmd is-task-program-running RUN-DIR NOMAD-TASK-NAME SUPERVISORCTL-PROGRAM)
$(helpcmd task-supervisorctl RUN-DIR NOMAD-TASK-NAME SUPERVISORCTL-ACTION [ARGS])
$(blue Subcommands that don\'t need a RUN-DIR parameter:)
$(red nomad $(helpcmd \(job\|agents\|server\|client\|plugin\|all\)))
EOF
}
backend_nomad() {
op=${1:?$(usage_nomadbackend)}; shift
case "$op" in
############################################################################
# Functions to configure the cluster and start the Nomad job:
# - setenv-defaults BACKEND-DIR
# - allocate-run RUN-DIR
# - allocate-run-directory-nomad RUN-DIR (Nomad only)
# - allocate-run-directory-supervisor RUN-DIR (Nomad only)
# - allocate-run-directory-tracers RUN-DIR (Nomad only)
# - allocate-run-directory-nodes RUN-DIR (Nomad only)
# - allocate-run-directory-generator RUN-DIR (Nomad only)
# - allocate-run-directory-healthchecks RUN-DIR (Nomad only)
# - allocate-run-directory-latencies RUN-DIR (Nomad only)
# - allocate-run-nomad-job-patch-name RUN-DIR NAME (Nomad only)
# - allocate-run-nomad-job-patch-namespace RUN-DIR NAME (Nomad only)
# - allocate-run-nomad-job-patch-nix RUN-DIR (Nomad only)
# - describe-run RUN-DIR
############################################################################
# * Functions in the backend "interface" must use `fatal` when errors!
# Completely overrided by each sub-backend (exec.sh, podman.sh and cloud.sh)
setenv-defaults )
# The impossible just happened?
fatal "Function \"setenv-defaults\" is Nomad backend specific"
;;
# After `allocate-run` the Nomad job is running (supervisord) waiting for
# genesis to be deployed and tracer/cardano-nodes/generator to be started.
#
# "generator", "tracer", "node", "healthcheck" and "latency" folder contents
# (start.sh, config files, etc) are included in the Nomad Job spec file as
# "template" stanzas and are materialized inside the container when the job
# is started. This is how it works for every environment combination
# (podman/exec-(local/cloud)).
#
# But "genesis" and "CARDANO_MAINNET_MIRROR" are the deployment exceptions:
# - "CARDANO_MAINNET_MIRROR": is added as a Nix dependency using the
# `nix_installables` stanza when using the "exec" driver and is mounted as a
# local volume for "podman" that currently is only allowed to run locally.
# - "genesis": it's too big for a "template" stanza so we are mounting it
# locally for "podman" and uploading it to a cloud storage service to
# download it using "nomad exec" when the "exec" task driver is used, the
# latter means creating an HTTP server for local runs and using Amazon S2
# for cloud runs.
allocate-run )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
while test $# -gt 0
do case "$1" in
--* ) msg "FATAL: unknown flag '$1'"; usage_nomadbackend;;
* ) break;; esac; shift; done
# Create the dispatcher's local directories hierarchy.
mkdir "${dir}"/nomad
backend_nomad allocate-run-directory-nomad "${dir}"
backend_nomad allocate-run-directory-supervisor "${dir}"
backend_nomad allocate-run-directory-tracers "${dir}"
backend_nomad allocate-run-directory-nodes "${dir}"
backend_nomad allocate-run-directory-generator "${dir}"
backend_nomad allocate-run-directory-healthchecks "${dir}"
backend_nomad allocate-run-directory-latencies "${dir}"
# These ones are decided at "setenv-defaults" of each sub-backend.
local nomad_environment=$(envjqr 'nomad_environment')
local nomad_task_driver=$(envjqr 'nomad_task_driver')
# TODO: Store them on disk for later subcommands run from a different shell.
# echo "{\"nomad_environment\": $nomad_environment, }" > "$dir"/env.json
;;
allocate-run-directory-nomad )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local nomad_task_driver=$(envjqr 'nomad_task_driver')
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
# Creates Nomad specific folders to download the entrypoints scripts and
# its logs for every Nomad Task.
# The top level "nomad" folder is created at "allocate-run" of each
# sub-backend. ("podman", "exec", "cloud") and filled with the Nomad job
# spec file to use.
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
mkdir "${dir}"/nomad/"${node}"
done
# A "tracer"(s) is optional.
if jqtest ".node.tracer" "${dir}"/profile.json
then
# Create a unique dedicated tracer only when not one-tracer-per-node.
if ! test "${one_tracer_per_node}" = "true"
then
mkdir "${dir}"/nomad/tracer
fi
fi
;;
allocate-run-directory-supervisor )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
# One `supervisord` server is run for every Nomad task/container.
mkdir -p "${dir}"/supervisor
# The folders were the `supervisord` config and log files will be downloaded!
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
mkdir "${dir}"/supervisor/"${node}"
done
# A "tracer"(s) is optional.
if jqtest ".node.tracer" "${dir}"/profile.json
then
# "tracer" is run as a standalone Nomad Task ? Or part of another Task ?
if ! test "${one_tracer_per_node}" = "true"
then
mkdir "${dir}"/supervisor/tracer
fi
fi
;;
allocate-run-directory-tracers )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local nomad_task_driver=$(envjqr 'nomad_task_driver')
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
# A "tracer"(s) is optional.
if jqtest ".node.tracer" "${dir}"/profile.json
then
# Create tracer(s) director(y/ies).
mkdir -p "${dir}"/tracer
# If running "local" without "one_tracer_per_node" this directory will
# be populated using the workbench!
# Right now where are forcing "one_tracer_per_node=true" with "exec"
# For podman, than only runs local, this tracer directory is populated
# and mounted to every container when only one shared tracer is used.
if test "${one_tracer_per_node}" = "true"
then
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
mkdir -p "${dir}"/tracer/"${node}"
done
elif test "${nomad_task_driver}" = "podman"
then
cp $(jq '."config"' -r ${dir}/profile/tracer-service.json) "${dir}"/tracer/config.json
cp $(jq '."start"' -r ${dir}/profile/tracer-service.json) "${dir}"/tracer/start.sh
fi
fi
;;
allocate-run-directory-nodes )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
# For every node ...
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
# Files "start.sh", "config.json" and "topology.sh" that usually go in
# here are copied from the Task/container once it's started because the
# contents are created or patched using Nomad's "template" stanza in the
# job spec and we want to hold a copy of what was actually run.
mkdir "${dir}"/"${node}"
done
;;
allocate-run-directory-generator )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
# Not much to do!
# Generator actually runs inside Task/container "node-0" for local runs
# and "explorer" for cloud runs.
# See: `local generator_task=$(envjqr 'generator_task_name')`
mkdir -p "${dir}"/generator
;;
allocate-run-directory-healthchecks )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
mkdir "${dir}"/healthcheck
# For every node ...
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
# File "start.sh" that usually goes in here is copied from the
# Task/container once it's started because the contents are created or
# patched using Nomad's "template" stanza in the job spec and we want to
# hold a copy of what was actually run.
mkdir "${dir}"/healthcheck/"${node}"
done
;;
allocate-run-directory-latencies )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
mkdir "${dir}"/latency
# For every node ...
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
# File "start.sh" that usually goes in here is copied from the
# Task/container once it's started because the contents are created or
# patched using Nomad's "template" stanza in the job spec and we want to
# hold a copy of what was actually run.
mkdir "${dir}"/latency/"${node}"
done
;;
# Change the Nomad job name to the current run tag. This allows to run
# multiple clusters simulatenously (as long as the network isolation mode
# and/or topology.json is designed for no port clashing)
allocate-run-nomad-job-patch-name )
local usage="USAGE: wb backend $op RUN-DIR JOB-NAME"
local dir=${1:?$usage}; shift
local new_name=${1:?$usage}; shift
local old_name=$(jq -r '. ["job"] | keys[0]' "${dir}"/nomad/nomad-job.json)
jq ".[\"job\"][\"${new_name}\"] = .[\"job\"][\"${old_name}\"] | del(.[\"job\"][\"${old_name}\"])" "${dir}"/nomad/nomad-job.json | sponge "${dir}"/nomad/nomad-job.json
;;
allocate-run-nomad-job-patch-namespace )
local usage="USAGE: wb backend $op RUN-DIR NAMESPACE"
local dir=${1:?$usage}; shift
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "${dir}"/nomad/nomad-job.json)
if test $# -gt 0 && test -n "${1}"
then
local namespace
namespace="${1:?$usage}"; shift
msg "Setting Nomad job top level namespace to \"${namespace}\""
jq \
".job[\"${nomad_job_name}\"][\"namespace\"] = \"${namespace}\"" \
"${dir}"/nomad/nomad-job.json \
| sponge "${dir}"/nomad/nomad-job.json
else
msg "Setting Nomad job top level namespace to null"
jq \
".job[\"${nomad_job_name}\"][\"namespace\"] = null" \
"${dir}"/nomad/nomad-job.json \
| sponge "${dir}"/nomad/nomad-job.json
fi
;;
allocate-run-nomad-job-patch-nix )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local nomad_environment=$(envjqr 'nomad_environment')
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "${dir}"/nomad/nomad-job.json)
# When running local, we can take some shortcuts!
local installables_array
if test "${nomad_environment}" != "cloud"
then
installables_array=$(jq '.containerPkgs | map(."nix-store-path")' "${dir}"/container-specs.json)
else
installables_array=$(jq '.containerPkgs | map(.installable)' "${dir}"/container-specs.json)
fi
# nix_installables
local groups_array=$(jq -r ".[\"job\"][\"${nomad_job_name}\"][\"group\"] | keys | join (\" \")" "${dir}"/nomad/nomad-job.json)
for group_name in ${groups_array[*]}
do
local tasks_array=$(jq -r ".[\"job\"][\"${nomad_job_name}\"][\"group\"][\"${group_name}\"][\"task\"] | keys | join (\" \")" "${dir}"/nomad/nomad-job.json)
for task_name in ${tasks_array[*]}
do
jq ".[\"job\"][\"${nomad_job_name}\"][\"group\"][\"${group_name}\"][\"task\"][\"${task_name}\"][\"config\"][\"nix_installables\"] = \$installables_array" --argjson installables_array "${installables_array}" "${dir}"/nomad/nomad-job.json | sponge "${dir}"/nomad/nomad-job.json
done
done
;;
describe-run )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}
echo " - Nomad job: $(realpath ${dir})/nomad/nomad-job.json"
;;
############################################################################
# Functions to start the cluster:
# - is-running RUN-DIR
# - start-cluster RUN-DIR
# - start-config-download RUN-DIR (Nomad only)
# - start-services-download RUN-DIR (Nomad only)
# - deploy-genesis-wget RUN-DIR (Nomad only)
############################################################################
# * Functions in the backend "interface" must use `fatal` when errors!
is-running )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
## Our contract is to print all running component names.
##
if test -f "${dir}"/nomad/nomad-job.json
then
local nomad_job_name=$(jq -r ". [\"job\"]? | keys[0]?" "${dir}"/nomad/nomad-job.json)
if test -z "${nomad_job_name}" && test "${nomad_job_name}" != "null"
then
if nomad job status >/dev/null 2>&1
then
for allocation_file in "${dir}"/"${nomad_job_name}".run/allocation.*.final; do
local allocation_id=$(jq .ID "${allocation_file}")
local alloc_response=$(nomad alloc status -json "${allocation_id}")
# With only one allocation running we return true (zero return code)
if test $(echo "${alloc_status}" | jq .State) = "running"
then echo "nomad-allocation-${allocation_id}"
fi
done
else
msg "Can't access the Nomad server, assuming not running"
msg "(Remember: assumption is the mother/father of all f*** ups!)"
fi
# Use the hack for the local, non-cloud, cluster?
# Hack: Look for node-0's default port!
# test "$(sleep 0.5s; netstat -pltn 2>/dev/null | grep ':30000 ' | wc -l)" != "0"
fi
fi
;;
# All or clean up everything!
# Called by `run.sh` without exit trap (unlike `scenario_setup_exit_trap`)!
start-cluster )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
touch "${dir}"/starting
if ! backend_nomad start-nomad-job "${dir}"
then
fatal "Backend start failed!"
else
# Don't send to the background!
if ! backend_nomad start-config-download "${dir}" || ! backend_nomad start-services-download "${dir}"
then
msg "$(yellow "We don't want to start a job/cluster if we are not able to download what has been deployed (copies of dynamically generated files)")"
backend_nomad stop-nomad-job "${dir}" || msg "stop-nomad-job failed!"
fatal "Backend start failed!"
fi
fi
rm "${dir}"/starting; touch "${dir}"/started
;;
# Downloads all Nomad Job deployed files, the "template" stanzas that suffer
# interpolation, and the entrypoint script generated files.
start-config-download )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
# Do not download everything in parallel, first the entrypoints' files and
# then the templates:
# Error querying allocation: Unexpected response code: 429 (Your IP is issuing too many concurrent connections, please rate limit your calls)
# First entrypoints.
local jobs_entrypoints=()
msg "Fetch entrypoints generated files ..."
# For every node (not including a possible tracer Task) ...
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
# The entrypoint generated files of the Task were the node runs.
backend_nomad download-config-entrypoint "${dir}" "${node}" &
jobs_entrypoints+=("$!")
done
if ! test "${one_tracer_per_node}" = "true"
then
# The entrypoint generated files of the Task were the tracer runs.
backend_nomad download-config-entrypoint "${dir}" "tracer" &
jobs_entrypoints+=("$!")
fi
# Wait and check!
if test -n "${jobs_entrypoints}"
then
if ! wait_kill_em_all "${jobs_entrypoints[@]}"
then
msg "$(red "Config downloads failed!")"
return 1
else
msg "Finished fetching entrypoints generated files"
fi
fi
# Last the Tasks' template stanzas.
msg "Fetch Nomad generated files ..."
local jobs_tasks=()
# The `tx-generator` config files, running in one of the Tasks were
# `cardano-node` is deployed.
backend_nomad download-config-generator "${dir}" &
jobs_tasks+=("$!")
# For every node (not including a possible tracer Task) ...
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
# `cardano-node` config files.
backend_nomad download-config-node "${dir}" "${node}" &
jobs_tasks+=("$!")
done
if test "${one_tracer_per_node}" = "true"
then
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
# `cardano-tracer` config files, running in one of the Tasks were
# `cardano-node` is deployed.
backend_nomad download-config-tracer "${dir}" "${node}" &
jobs_tasks+=("$!")
done
else
# The tracer runs as an extra Nomad Task.
backend_nomad download-config-tracer "${dir}" "tracer" &
jobs_tasks+=("$!")
fi
# For every node (not including a possible tracer Task) ...
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
# Only used for debugging!
backend_nomad download-config-healthcheck "${dir}" "${node}" &
jobs_tasks+=("$!")
done
# DO NOT DOWNLOAD THE latency SCRIPTS/CONFIG EVERY TIME
if echo "${WB_SHELL_PROFILE}" | grep --quiet "latency"
then
# For every node (not including a possible tracer Task) ...
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
# Only used for debugging!
backend_nomad download-config-latency "${dir}" "${node}" &
jobs_tasks+=("$!")
done
fi
# Wait and check!
if test -n "${jobs_tasks}"
then
if ! wait_kill_em_all "${jobs_tasks[@]}"
then
msg "$(red "Config downloads failed!")"
return 1
else
msg "Finished fetching Nomad generated files"
fi
fi
;;
# Downloads all Nomad services definitions, the "service" stanzas.
start-services-download )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
msg "Fetch Nomad services definitions ..."
local jobs_array=()
# For every node ...
local i_array=($(jq_tolist 'map(.i)' "${dir}"/node-specs.json))
for i in ${i_array[*]}
do
local node_name
node_name=$(jq -r "map(select( .i == ${i} ))[0].name" "${dir}"/node-specs.json)
nomad service info -json "perf-node-${i}" > "${dir}"/nomad/"${node_name}"/service-info.json &
jobs_array+=("$!")
done
if ! test "${one_tracer_per_node}" = "true"
then
nomad service info -json "perf-tracer" > "${dir}"/nomad/tracer/service-info.json &
jobs_array+=("$!")
fi
# Wait and check!
if test -n "${jobs_array}"
then
if ! wait_kill_em_all "${jobs_array[@]}"
then
msg "$(red "Service definitions downloads failed!")"
return 1
else
msg "Finished fetching Nomad services definitions"
fi
fi
;;
# Completely overrided by each sub-backend (exec.sh, podman.sh and cloud.sh)
deploy-genesis )
# The impossible just happened?
fatal "Function \"deploy-genesis\" is Nomad backend specific"
;;
# Called by the sub-backends, don't use `fatal` and let them do the cleaning
deploy-genesis-wget )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local uri=${1:?$usage}; shift
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "${dir}"/nomad/nomad-job.json)
# Upload and unpack all "genesis" files in parallel!
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
local wget_path="$(jq -r ".containerPkgs.wget.\"nix-store-path\"" "${dir}"/container-specs.json)"
local uploads_array=()
for node in ${nodes[*]}
do
msg "$(blue Downloading) $(yellow "\"${uri}\"") from $(yellow "node \"${node}\"") ..."
# When executing commands the directories used depend on the filesystem
# isolation mode (AKA chroot or not).
local state_dir
state_dir="$(backend_nomad task-workbench-state-dir "${dir}" "${node}")"
backend_nomad task-exec "${dir}" "${node}" \
"${wget_path}"/bin/wget \
--output-document="${state_dir}"/genesis.tar.zst \
"${uri}" \
--no-verbose \
> /dev/null \
&
uploads_array+=("$!")
done
# Wait and check!
if test -n "${uploads_array}"
then
if ! wait_kill_em_all "${uploads_array[@]}"
then
msg "$(red "Failed to upload some genesis files")"
return 1
else
# Unpack!
local coreutils_path="$(jq -r ".containerPkgs.coreutils.\"nix-store-path\"" "${dir}"/container-specs.json)"
local tar_path="$(jq -r ".containerPkgs.gnutar.\"nix-store-path\"" "${dir}"/container-specs.json)"
local zstd_path="$(jq -r ".containerPkgs.zstd.\"nix-store-path\"" "${dir}"/container-specs.json)"
local unpacks_array=()
for node in ${nodes[*]}
do
# When executing commands the directories used depend on the
# filesystem isolation mode (AKA chroot or not).
local state_dir
state_dir="$(backend_nomad task-workbench-state-dir "${dir}" "${node}")"
backend_nomad task-exec "${dir}" "${node}" \
"${tar_path}"/bin/tar --extract \
--use-compress-program="${zstd_path}"/bin/zstd \
--file="${state_dir}"/genesis.tar.zst \
--one-top-level="${state_dir}"/genesis \
--same-permissions \
--no-same-owner \
--numeric-owner \
--owner="$(${coreutils_path}/bin/id --user)" \
--group="$(${coreutils_path}/bin/id --group)" \
&& backend_nomad task-exec "${dir}" "${node}" \
"${coreutils_path}"/bin/mv \
"${state_dir}"/genesis/db \
"${state_dir}"/"${node}" \
&& backend_nomad task-exec "${dir}" "${node}" \
"${coreutils_path}"/bin/rm -f \
"${state_dir}"/genesis/tar.zst &
unpacks_array+=("$!")
done
# Wait and check!
if test -n "${unpacks_array}"
then
if ! wait_kill_em_all "${unpacks_array[@]}"
then
msg "$(red "Failed to unpack some genesis files")"
return 1
fi
fi
fi
fi
;;
############################################################################
# Functions to start/stop the Nomad Job part of the Cardano cluster:
# - start-nomad-job RUN-DIR (Nomad only)
# - stop-nomad-job RUN-DIR (Nomad only)
############################################################################
start-nomad-job )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
backend_nomad nomad-agents-topology "${dir}"
local nomad_environment=$(envjqr 'nomad_environment')
local nomad_task_driver=$(envjqr 'nomad_task_driver')
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
local server_name=$(envjqr 'nomad_server_name')
local client_name=$(envjqr 'nomad_client_name')
local server_state_dir=$(wb_nomad server state-dir-path "${server_name}")
local client_state_dir=$(wb_nomad client state-dir-path "${client_name}")
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "$dir"/nomad/nomad-job.json)
# Reuse an already running cardano-workbench Nomad server!
local nomad_agents_were_already_running
if test "${nomad_environment}" = "cloud"
then
nomad_agents_were_already_running="true"
else
if wb_nomad server is-running "${server_name}" && wb_nomad client is-running "${client_name}"
then
# TODO/ENHANCE: Check matching needed capabilities before reuse?
nomad_agents_were_already_running="true"
setenvjqstr 'nomad_agents_were_already_running' "true"
msg "$(yellow "Reusing already up and running Nomad agents (server and client found)")"
else
nomad_agents_were_already_running="false"
setenvjqstr 'nomad_agents_were_already_running' "false"
# Start server, client and plugins.
wb_nomad agents start \
"${server_name}" "${client_name}" "${nomad_task_driver}"
fi
fi
# TODO: Genesis when "cloud"!
if test "${nomad_environment}" != "cloud"
then
# Links to Nomad agents logs.
ln -s "${server_state_dir}"/nomad.log "${dir}"/nomad/server-"${server_name}".log
ln -s "${server_state_dir}"/stdout "${dir}"/nomad/server-"${server_name}".stdout
ln -s "${server_state_dir}"/stderr "${dir}"/nomad/server-"${server_name}".stderr
ln -s "${client_state_dir}"/nomad.log "${dir}"/nomad/client-"${client_name}".log
ln -s "${client_state_dir}"/stdout "${dir}"/nomad/client-"${client_name}".stdout
ln -s "${client_state_dir}"/stderr "${dir}"/nomad/client-"${client_name}".stderr
fi
msg "$(blue Starting) Nomad $(yellow "Job \"${nomad_job_name}\"")..."
if ! wb_nomad job start "$dir/nomad/nomad-job.json" "${nomad_job_name}"
then
if test "${nomad_agents_were_already_running}" = "false"
then
# No checks, `agents stop` checks for errors and uses msg to show them!
wb_nomad agents stop \
"${server_name}" "${client_name}" "${nomad_task_driver}"
fi
fatal "Failed to start Nomad job"
else
touch "${dir}"/nomad/started
fi
# Create a symlink to the allocations IDs inside the run directory and
# the supervisord log files.
if test "${nomad_environment}" != "cloud"
then
mkdir "${dir}"/nomad/alloc
# For each node's Nomad Task
local nodes=($(jq_tolist keys "$dir"/node-specs.json))
for node in ${nodes[*]}
do
local alloc_id alloc_dir
alloc_id=$(wb_nomad job task-name-allocation-id \
"${dir}/nomad/nomad-job.json" \
"${node}")
alloc_dir="${client_state_dir}"/data/alloc/"${alloc_id}"/"${node}"
ln -s "${alloc_dir}" "${dir}"/nomad/alloc/"${node}"
ln -s \
../../nomad/alloc/"${node}"/local/run/current/supervisor/supervisord.log \
"${dir}"/supervisor/"${node}"/supervisord.log
done
# For the tracer's Nomad Task if any
if test "${one_tracer_per_node}" != "true"
then
local alloc_id alloc_dir
alloc_id=$(wb_nomad job task-name-allocation-id \
"${dir}/nomad/nomad-job.json" \
tracer)
alloc_dir="${client_state_dir}"/data/alloc/"${alloc_id}"/tracer
ln -s "${alloc_dir}" "${dir}"/nomad/alloc/tracer
ln -s \
../../nomad/alloc/tracer/local/run/current/supervisor/supervisord.log \
"${dir}"/supervisor/tracer/supervisord.log
fi
fi
;;
stop-nomad-job )
local dir=${1:?$usage}; shift
local nomad_task_driver=$(envjqr 'nomad_task_driver')
local server_name=$(envjqr 'nomad_server_name')
local client_name=$(envjqr 'nomad_client_name')
local nomad_agents_were_already_running=$(envjqr 'nomad_agents_were_already_running')
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "${dir}"/nomad/nomad-job.json)
touch "${dir}"/nomad/stopped
# No checks, `job stop` checks for errors and uses msg to show them!
wb_nomad job stop "${dir}/nomad/nomad-job.json" "${nomad_job_name}" > "${dir}/nomad/job.stop.stdout" 2> "$dir/nomad/job.stop.stderr"
if test "${nomad_agents_were_already_running}" = "false"
then
# No checks, `agents stop` checks for errors and uses msg to show them!
wb_nomad agents stop \
"${server_name}" "${client_name}" "${nomad_task_driver}"
fi
;;
nomad-agents-topology )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local nomad_task_driver=$(envjqr 'nomad_task_driver')
local nomad_job_name=$(jq -r ". [\"job\"] | keys[0]" "${dir}"/nomad/nomad-job.json)
# The server
echo "{ ">> "${dir}"/nomad/agents-topolgy.json
echo " \"servers\": { ">> "${dir}"/nomad/agents-topolgy.json
echo " \"srv1\": { ">> "${dir}"/nomad/agents-topolgy.json
echo " \"region\": \"r1\" ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"datacenter\": \"d1\" ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"ports\": { ">> "${dir}"/nomad/agents-topolgy.json
echo " \"http\": 4646 ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"rpc\": 4647 ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"serf\": 4648 ">> "${dir}"/nomad/agents-topolgy.json
echo " } ">> "${dir}"/nomad/agents-topolgy.json
echo " } ">> "${dir}"/nomad/agents-topolgy.json
echo " } ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"clients\": { ">> "${dir}"/nomad/agents-topolgy.json
# Task driver based suffix for clients (exec clients run with `sudo`)
local suffix
if test "${nomad_task_driver}" = "podman"
then
suffix="pod"
else
suffix="exe"
fi
local i=0 c=""
local groups_array=$(jq -r ".[\"job\"][\"${nomad_job_name}\"][\"group\"] | keys | join (\" \")" "${dir}"/nomad/nomad-job.json)
for group_name in ${groups_array[*]}
do
local client_name=("cli${group_name}-${suffix}")
echo " ${c} \"${client_name}\": { ">> "${dir}"/nomad/agents-topolgy.json
echo " \"region\": \"r1\" ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"datacenter\": \"d1\" ">> "${dir}"/nomad/agents-topolgy.json
i=$(( "${i}" + 1 ))
echo " , \"ports\": { ">> "${dir}"/nomad/agents-topolgy.json
echo " \"http\": ${i}4646 ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"rpc\": ${i}4647 ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"serf\": ${i}4648 ">> "${dir}"/nomad/agents-topolgy.json
echo " } ">> "${dir}"/nomad/agents-topolgy.json
echo " , \"servers\": [ ">> "${dir}"/nomad/agents-topolgy.json
echo " \"srv1\" ">> "${dir}"/nomad/agents-topolgy.json
echo " ] ">> "${dir}"/nomad/agents-topolgy.json
echo " } ">> "${dir}"/nomad/agents-topolgy.json
c=","
done
echo " } ">> "${dir}"/nomad/agents-topolgy.json
echo "} ">> "${dir}"/nomad/agents-topolgy.json
# TODO: Once we start testing multicluster deployments, the Nomad cluster
# must be created or obtained.
setenvjq 'nomad_topology' \
"{ \
\"servers\": { \
\"srv1\": { \
\"region\": \"r1\" \
, \"datacenter\": \"dc1\" \
, \"ports\": { \
\"http\": 4646 \
, \"rpc\": 4647 \
, \"serf\": 4648 \
} \
} \
} \
, \"clients\": { \
\"cli1\": { \
\"region\": \"r22\" \
, \"datacenter\": \"dc0\" \
, \"ports\":{ \
\"http\": 14646 \
, \"rpc\": 14647 \
} \
, \"servers\": [ \"srv1\" ] \
} \
} \
}"
;;
############################################################################
# Functions to stop the cluster:
# - stop-all RUN-DIR
# - stop-all-healthchecks RUN-DIR (Nomad only)
# - stop-all-latencies RUN-DIR (Nomad only)
# - stop-all-generator RUN-DIR (Nomad only)
# - stop-all-nodes RUN-DIR (Nomad only)
# - stop-all-tracers RUN-DIR (Nomad only)
# - fetch-logs RUN-DIR
# - stop-cluster-internal RUN-DIR
# - stop-cluster-local RUN-DIR
# - cleanup-cluster RUN-DIR
############################################################################
# * Functions in the backend "interface" must use `fatal` when errors!
stop-all )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local generator_task=$(envjqr 'generator_task_name')
# Stop latency(s).
##################
local jobs_latencies_array=()
for node in $(jq_tolist 'keys' "${dir}"/node-specs.json)
do
backend_nomad stop-all-latencies "${dir}" "${node}" &
jobs_latencies_array+=("$!")
done
if ! wait_all "${jobs_latencies_array[@]}"
then
msg "$(red "Failed to stop latency(ies)")"
fi
# Stop healthcheck(s).
#####################
local jobs_healthchecks_array=()
for node in $(jq_tolist 'keys' "${dir}"/node-specs.json)
do
backend_nomad stop-all-healthchecks "${dir}" "${node}" &
jobs_healthchecks_array+=("$!")
done
if ! wait_all "${jobs_healthchecks_array[@]}"
then
msg "$(red "Failed to stop healthcheck(s)")"
fi
# Stop generator.
#################
backend_nomad stop-all-generator "${dir}" "${generator_task}"
# Stop node(s).
###############
local jobs_nodes_array=()
for node in $(jq_tolist 'keys' "${dir}"/node-specs.json)
do
backend_nomad stop-all-nodes "${dir}" "${node}" &
jobs_nodes_array+=("$!")
done
if ! wait_all "${jobs_nodes_array[@]}"
then
msg "$(red "Failed to stop node(s)")"
fi
# Stop tracer(s).
#################
local one_tracer_per_node=$(envjqr 'one_tracer_per_node')
if test "${one_tracer_per_node}" = "true"
then
local jobs_tracers_array=()
local nodes=($(jq_tolist keys "${dir}"/node-specs.json))
for node in ${nodes[*]}
do
backend_nomad stop-all-tracers "${dir}" "${node}" &
jobs_tracers_array+=("$!")
done
if ! wait_all "${jobs_tracers_array[@]}"
then
msg "$(red "Failed to stop tracer(s)")"
fi
else
if ! backend_nomad stop-all-tracers "${dir}" "tracer"
then
msg "$(red "Failed to stop tracer")"
fi
fi
;;
stop-all-latencies )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local task=${1:?$usage}; shift
local task_dir="${dir}"/latency/"${task}"
if test -f "${task_dir}"/started && !(test -f "${task_dir}"/stopped || test -f "${task_dir}"/quit)
then
if backend_nomad is-task-program-running "${dir}" "${task}" latency
then
if ! backend_nomad task-program-stop "${dir}" "${task}" latency
then
msg "$(yellow "WARNING: Program \"latency\" inside Task \"${task}\" failed to stop")"
else
touch "${task_dir}"/stopped
msg "$(green "supervisord program \"latency\" inside Nomad Task \"${task}\" down!")"
fi
else
touch "${task_dir}"/quit
if backend_nomad is-task-program-failed "${dir}" "${task}" latency
then
local generator_task=$(envjqr 'generator_task_name')
# If the node quits (due to `--shutdown_on_slot_synced X` or
# `--shutdown_on_block_synced X`) the generator quits with an error.
local generator_can_fail=$(jq ".\"${task}\".shutdown_on_slot_synced or .\"${task}\".shutdown_on_block_synced" "${dir}"/node-specs.json)
if test "${generator_task}" != "${task}" || test "${generator_can_fail}" = "false" || backend_nomad is-task-program-running "${dir}" "${task}" "${task}"
then
# Do not fail here, because nobody will be able to stop the cluster!
msg "$(red "FATAL: \"latency\" inside Task \"${task}\" quit unexpectedly")"
else
msg "$(yellow "INFO: Program \"latency\" inside Task \"${task}\" failed, but expected when \"${task}\" automatically exits first and makes \"generator\" fail")"
fi
else
msg "$(yellow "WARNING: Program \"latency\" inside Task \"${task}\" was not running, should it?")"
fi
fi
fi
;;
stop-all-healthchecks )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local task=${1:?$usage}; shift
local task_dir="${dir}"/healthcheck/"${task}"
if test -f "${task_dir}"/started && !(test -f "${task_dir}"/stopped || test -f "${task_dir}"/quit)
then
if backend_nomad is-task-program-running "${dir}" "${task}" healthcheck
then
if ! backend_nomad task-program-stop "${dir}" "${task}" healthcheck
then
msg "$(yellow "WARNING: Program \"healthcheck\" inside Task \"${task}\" failed to stop")"
else
touch "${task_dir}"/stopped
msg "$(green "supervisord program \"healthcheck\" inside Nomad Task \"${task}\" down!")"
fi
else
touch "${task_dir}"/quit
if backend_nomad is-task-program-failed "${dir}" "${task}" healthcheck
then
local generator_task=$(envjqr 'generator_task_name')
# If the node quits (due to `--shutdown_on_slot_synced X` or
# `--shutdown_on_block_synced X`) the generator quits with an error.
local generator_can_fail=$(jq ".\"${task}\".shutdown_on_slot_synced or .\"${task}\".shutdown_on_block_synced" "${dir}"/node-specs.json)
if test "${generator_task}" != "${task}" || test "${generator_can_fail}" = "false" || backend_nomad is-task-program-running "${dir}" "${task}" "${task}"
then
# Do not fail here, because nobody will be able to stop the cluster!
msg "$(red "FATAL: \"healthcheck\" inside Task \"${task}\" quit unexpectedly")"
else
msg "$(yellow "INFO: Program \"healthcheck\" inside Task \"${task}\" failed, but expected when \"${task}\" automatically exits first and makes \"generator\" fail")"
fi
else
msg "$(yellow "WARNING: Program \"healthcheck\" inside Task \"${task}\" was not running, should it?")"
fi
fi
fi
;;
stop-all-generator )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local task=${1:?$usage}; shift
# Only if it was started
if test -f "${dir}"/generator/started && !(test -f "${dir}"/"${node}"/stopped || test -f "${dir}"/"${node}"/quit)
then
if backend_nomad is-task-program-running "${dir}" "${task}" generator
then
if ! backend_nomad task-program-stop "${dir}" "${task}" generator
then
# A race condition were we try to stop the generator just after it
# quits automatically can happen.
msg "$(yellow "WARNING: Program \"generator\" inside Task \"${task}\" failed to stop")"
else
touch "${dir}"/generator/stopped
msg "$(green "supervisord program \"generator\" inside Nomad Task \"${task}\" down!")"
fi
else
touch "${dir}"/generator/quit
if backend_nomad is-task-program-failed "${dir}" "${task}" generator
then
# If the node quits (due to `--shutdown_on_slot_synced X` or
# `--shutdown_on_block_synced X`) the generator quits with an error.
local generator_can_fail=$(jq ".\"${task}\".shutdown_on_slot_synced or .\"${task}\".shutdown_on_block_synced" "${dir}"/node-specs.json)
if test "${generator_can_fail}" = "false" || backend_nomad is-task-program-running "${dir}" "${task}" "${task}"
then
# Do not fail here, because nobody will be able to stop the cluster!
msg "$(red "FATAL: \"generator\" quit unexpectedly")"
else
msg "$(yellow "INFO: Program \"generator\" inside Task \"${task}\" failed, but expected when \"${task}\" automatically exits first")"
fi
else
msg "$(yellow "WARNING: Program \"generator\" inside Task \"${task}\" was not running, should it?")"
fi
fi
fi
;;
stop-all-nodes )
local usage="USAGE: wb backend $op RUN-DIR"
local dir=${1:?$usage}; shift
local node=${1:?$usage}; shift
# Only if it was started
if test -f "${dir}"/"${node}"/started && !(test -f "${dir}"/"${node}"/stopped || test -f "${dir}"/"${node}"/quit)
then
if backend_nomad is-task-program-running "${dir}" "${node}" "${node}"
then
# The `|| true` is to avoid a race condition were we try to stop
# the node just after it quits automatically.
if ! backend_nomad task-program-stop "${dir}" "${node}" "${node}"
then
msg "$(yellow "WARNING: Program \"${node}\" inside Task \"${node}\" failed to stop")"
else
touch "${dir}"/"${node}"/stopped
msg "$(green "supervisord program \"${node}\" inside Nomad Task \"${node}\" down!")"
fi
else
touch "${dir}"/"${node}"/quit
# Node may have already quit (due to --shutdown_on_slot_synced X or