-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocalnet-cloud.sh
More file actions
executable file
·1140 lines (967 loc) · 33 KB
/
Copy pathlocalnet-cloud.sh
File metadata and controls
executable file
·1140 lines (967 loc) · 33 KB
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
PROJECT_ROOT="${CANTON_LOCALNET_PROJECT_ROOT:-$(pwd)}"
QUICKSTART_DIR="${CANTON_LOCALNET_QUICKSTART_DIR:-${REPO_ROOT}/libs/cn-quickstart/quickstart}"
DOCKERD_PID_FILE="/tmp/localnet-dockerd.pid"
DOCKERD_LOG_FILE="/tmp/localnet-dockerd.log"
HOSTS_ENTRY="127.0.0.1 scan.localhost sv.localhost wallet.localhost"
CURL_CONNECT_TIMEOUT=2
CURL_MAX_TIME=5
LOCALNET_SPLICE_VERSION="${CANTON_LOCALNET_SPLICE_VERSION:-}"
LOCALNET_SCRIBE_VERSION="${CANTON_LOCALNET_SCRIBE_VERSION:-}"
LOCALNET_PROTOCOL_VERSION="${CANTON_LOCALNET_PROTOCOL_VERSION:-}"
VALIDATOR_READY_ATTEMPTS="${CANTON_LOCALNET_VALIDATOR_READY_ATTEMPTS:-90}"
SCAN_READY_ATTEMPTS="${CANTON_LOCALNET_SCAN_READY_ATTEMPTS:-90}"
SPLICE_CONFIG_PENDING_KEY="CANTON_NODE_SDK_SPLICE_CONFIG_PENDING"
SPLICE_CONFIG_CHANGED="false"
log() {
printf '[localnet] %s\n' "$*"
}
is_truthy() {
# Portable lowercase (Bash 3.2+ / macOS /bin/bash); avoid ${1,,} (Bash 4+).
case "$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')" in
1 | true | yes | on)
return 0
;;
*)
return 1
;;
esac
}
require_command() {
local cmd="$1"
if ! command -v "${cmd}" >/dev/null 2>&1; then
log "Missing required command: ${cmd}"
exit 1
fi
}
require_positive_integer() {
local name="$1"
local value="$2"
if [[ ! "${value}" =~ ^[1-9][0-9]*$ ]]; then
log "${name} must be a positive integer; received '${value}'."
exit 1
fi
}
validate_configuration() {
require_positive_integer "CANTON_LOCALNET_VALIDATOR_READY_ATTEMPTS" "${VALIDATOR_READY_ATTEMPTS}"
require_positive_integer "CANTON_LOCALNET_SCAN_READY_ATTEMPTS" "${SCAN_READY_ATTEMPTS}"
}
ensure_sudo() {
if ! sudo_noninteractive_available; then
log "Passwordless sudo is required in this cloud environment."
exit 1
fi
}
sudo_noninteractive_available() {
command -v sudo >/dev/null 2>&1 && sudo -n true >/dev/null 2>&1
}
ensure_docker_packages() {
if command -v docker >/dev/null 2>&1 \
&& docker compose version >/dev/null 2>&1; then
return
fi
if ! sudo_noninteractive_available || ! command -v apt-get >/dev/null 2>&1; then
log "Docker with Compose is required. Install Docker and start it, then retry."
exit 1
fi
log "Installing Docker packages..."
sudo apt-get update
sudo apt-get install -y docker.io docker-compose docker-compose-v2
}
ensure_legacy_iptables() {
if ! command -v update-alternatives >/dev/null 2>&1 \
|| [[ ! -x /usr/sbin/iptables-legacy || ! -x /usr/sbin/ip6tables-legacy ]]; then
log "iptables legacy binaries unavailable; skipping backend switch."
return
fi
if ! iptables --version 2>/dev/null | grep -q 'legacy'; then
log "Switching iptables to legacy backend..."
sudo update-alternatives --set iptables /usr/sbin/iptables-legacy
fi
if ! ip6tables --version 2>/dev/null | grep -q 'legacy'; then
log "Switching ip6tables to legacy backend..."
sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy
fi
}
docker_ready() {
docker info >/dev/null 2>&1 || (sudo_noninteractive_available && sudo docker info >/dev/null 2>&1)
}
configure_docker_socket_permissions() {
if [[ ! -S /var/run/docker.sock ]]; then
return
fi
if ! sudo_noninteractive_available; then
return
fi
sudo groupadd -f docker >/dev/null 2>&1 || true
sudo chown root:docker /var/run/docker.sock >/dev/null 2>&1 || true
sudo chmod 660 /var/run/docker.sock || true
}
run_docker() {
if docker info >/dev/null 2>&1; then
docker "$@"
return
fi
if ! sudo_noninteractive_available || ! sudo docker info >/dev/null 2>&1; then
log "Docker daemon is not available."
exit 1
fi
sudo docker "$@"
}
resolve_quickstart_image_tag() {
local env_file=""
local splice_version=""
local parsed_value=""
for env_file in "${QUICKSTART_DIR}/.env" "${QUICKSTART_DIR}/.env.local"; do
if [[ ! -f "${env_file}" ]]; then
continue
fi
parsed_value="$(awk -F= -v key="SPLICE_VERSION" '
$0 ~ /^[[:space:]]*#/ { next }
$1 == key {
value = substr($0, index($0, "=") + 1)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
gsub(/^"|"$/, "", value)
gsub(/^'\''|'\''$/, "", value)
parsed = value
}
END {
if (parsed != "") {
print parsed
}
}
' "${env_file}")"
if [[ -n "${parsed_value}" ]]; then
splice_version="${parsed_value}"
fi
done
printf '%s' "${splice_version}"
}
run_quickstart_command() {
local command="$1"
local docker_shim_dir=""
local quickstart_image_tag=""
local quickstart_path="${HOME}/.dpm/bin:${HOME}/.daml/bin:${PATH}"
local status=0
quickstart_image_tag="$(resolve_quickstart_image_tag)"
if ! docker info >/dev/null 2>&1 && sudo docker info >/dev/null 2>&1; then
docker_shim_dir="$(mktemp -d "/tmp/canton-localnet-docker-shim.XXXXXX")"
cat >"${docker_shim_dir}/docker" <<'EOF'
#!/usr/bin/env bash
exec sudo -E docker "$@"
EOF
chmod +x "${docker_shim_dir}/docker"
log "Using sudo docker shim for quickstart command."
fi
if [[ -n "${docker_shim_dir}" ]]; then
quickstart_path="${docker_shim_dir}:${quickstart_path}"
fi
set +e
(
cd "${QUICKSTART_DIR}"
MODULES_DIR="${QUICKSTART_DIR}/docker/modules" \
LOCALNET_DIR="${QUICKSTART_DIR}/docker/modules/localnet" \
IMAGE_TAG="${quickstart_image_tag}" \
PATH="${quickstart_path}" \
bash -lc "${command}"
)
status=$?
set -e
if [[ -n "${docker_shim_dir}" ]]; then
rm -rf "${docker_shim_dir}"
fi
return "${status}"
}
run_quickstart_make() {
local target="$1"
run_quickstart_command "make ${target}"
}
run_infra_compose() {
local compose_args="$1"
local keycloak_compose_file=""
local keycloak_env_file=""
local keycloak_profile=""
if [[ "$(current_auth_mode)" == "oauth2" ]]; then
keycloak_compose_file='-f "${MODULES_DIR}/keycloak/compose.yaml"'
keycloak_env_file='--env-file "${MODULES_DIR}/keycloak/compose.env"'
keycloak_profile='--profile keycloak'
fi
run_quickstart_command "docker compose \
-f \"\${LOCALNET_DIR}/compose.yaml\" \
${keycloak_compose_file} \
--env-file .env \
--env-file .env.local \
--env-file \"\${LOCALNET_DIR}/compose.env\" \
--env-file \"\${LOCALNET_DIR}/env/common.env\" \
${keycloak_env_file} \
--profile app-provider \
--profile app-user \
--profile sv \
${keycloak_profile} \
${compose_args}"
}
start_docker_daemon() {
local dockerd_pid=""
if docker_ready; then
configure_docker_socket_permissions
return
fi
if ! sudo_noninteractive_available; then
log "Docker daemon is not running. Start Docker and retry."
exit 1
fi
ensure_legacy_iptables
log "Starting Docker daemon with vfs storage driver..."
sudo nohup dockerd --host=unix:///var/run/docker.sock --pidfile="${DOCKERD_PID_FILE}" --storage-driver=vfs >"${DOCKERD_LOG_FILE}" 2>&1 &
for _ in $(seq 1 60); do
if sudo docker info >/dev/null 2>&1; then
configure_docker_socket_permissions
return
fi
if [[ -f "${DOCKERD_PID_FILE}" ]]; then
dockerd_pid="$(cat "${DOCKERD_PID_FILE}" 2>/dev/null || true)"
if [[ -n "${dockerd_pid}" ]] && ! ps -p "${dockerd_pid}" >/dev/null 2>&1; then
log "Docker daemon exited before becoming ready."
log "Inspect ${DOCKERD_LOG_FILE}."
exit 1
fi
fi
sleep 1
done
log "Docker failed to start. Inspect ${DOCKERD_LOG_FILE}."
exit 1
}
ensure_submodules() {
if [[ -n "${CANTON_LOCALNET_QUICKSTART_DIR:-}" ]]; then
if [[ -d "${QUICKSTART_DIR}" ]]; then
return
fi
log "Configured CANTON_LOCALNET_QUICKSTART_DIR does not exist: ${QUICKSTART_DIR}"
exit 1
fi
if [[ -d "${REPO_ROOT}/libs/splice" && -d "${QUICKSTART_DIR}" ]]; then
return
fi
require_command git
if ! git -C "${REPO_ROOT}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
log "Missing localnet assets. Set CANTON_LOCALNET_QUICKSTART_DIR or use the canton-dev-tools CLI to fetch cn-quickstart into the cache."
exit 1
fi
if [[ ! -d "${REPO_ROOT}/libs/splice" ]]; then
log "Initializing libs/splice submodule..."
git -C "${REPO_ROOT}" submodule update --init --depth 1 libs/splice
fi
if [[ ! -d "${REPO_ROOT}/libs/cn-quickstart" ]]; then
log "Initializing libs/cn-quickstart submodule..."
git -C "${REPO_ROOT}" submodule update --init --recursive libs/cn-quickstart
fi
if [[ ! -d "${QUICKSTART_DIR}" ]]; then
log "cn-quickstart directory not found after submodule init."
exit 1
fi
}
ensure_hosts_entries() {
if ! grep -Eq '(^|[[:space:]])scan\.localhost([[:space:]]|$)' /etc/hosts \
|| ! grep -Eq '(^|[[:space:]])sv\.localhost([[:space:]]|$)' /etc/hosts \
|| ! grep -Eq '(^|[[:space:]])wallet\.localhost([[:space:]]|$)' /etc/hosts; then
log "Adding localnet host aliases to /etc/hosts..."
if sudo_noninteractive_available; then
echo "${HOSTS_ENTRY}" | sudo tee -a /etc/hosts >/dev/null
return
fi
if [[ -t 0 ]] && command -v sudo >/dev/null 2>&1; then
echo "${HOSTS_ENTRY}" | sudo tee -a /etc/hosts >/dev/null
return
fi
log "Missing localnet host aliases. Add this line to /etc/hosts, then retry:"
log "${HOSTS_ENTRY}"
exit 1
fi
}
set_quickstart_env_value() {
local file="$1"
local key="$2"
local value="$3"
if [[ ! -f "${file}" ]]; then
return
fi
if grep -Eq "^${key}=" "${file}"; then
KEY="${key}" VALUE="${value}" perl -0pi -e '
my $key = $ENV{"KEY"};
my $value = $ENV{"VALUE"};
s/^\Q$key\E=.*/$key=$value/m;
' "${file}"
else
printf '\n%s=%s\n' "${key}" "${value}" >>"${file}"
fi
}
patch_quickstart_canton_healthcheck() {
local healthcheck="${QUICKSTART_DIR}/docker/modules/localnet/docker/canton/health-check.sh"
if [[ ! -f "${healthcheck}" ]]; then
log "Canton quickstart healthcheck not found: ${healthcheck}"
exit 1
fi
cat >"${healthcheck}" <<'EOF'
#!/bin/bash
set -euo pipefail
http_check() {
local port="$1"
local path="$2"
local status=""
echo "Checking http://localhost:${port}${path}"
exec 3<>"/dev/tcp/127.0.0.1/${port}"
printf 'GET %s HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n' "${path}" >&3
IFS=$'\r' read -r status <&3 || true
exec 3<&-
exec 3>&-
case "${status}" in
HTTP/*\ 2*) ;;
*)
echo "Unexpected status from ${port}${path}: ${status}"
return 1
;;
esac
}
if [ "${APP_USER_PROFILE:-off}" = "on" ]; then
http_check "2${CANTON_HTTP_HEALTHCHECK_PORT_SUFFIX}" "/health"
fi
if [ "${APP_PROVIDER_PROFILE:-off}" = "on" ]; then
http_check "3${CANTON_HTTP_HEALTHCHECK_PORT_SUFFIX}" "/health"
fi
if [ "${SV_PROFILE:-off}" = "on" ]; then
http_check "4${CANTON_HTTP_HEALTHCHECK_PORT_SUFFIX}" "/health"
fi
EOF
chmod +x "${healthcheck}"
}
patch_quickstart_splice_healthcheck() {
local healthcheck="${QUICKSTART_DIR}/docker/modules/localnet/docker/splice/health-check.sh"
if [[ ! -f "${healthcheck}" ]]; then
log "Splice quickstart healthcheck not found: ${healthcheck}"
exit 1
fi
cat >"${healthcheck}" <<'EOF'
#!/bin/bash
set -euo pipefail
http_check() {
local port="$1"
local path="$2"
local status=""
echo "Checking http://localhost:${port}${path}"
exec 3<>"/dev/tcp/127.0.0.1/${port}"
printf 'GET %s HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n' "${path}" >&3
IFS=$'\r' read -r status <&3 || true
exec 3<&-
exec 3>&-
case "${status}" in
HTTP/*\ 2*) ;;
*)
echo "Unexpected status from ${port}${path}: ${status}"
return 1
;;
esac
}
if [ "${APP_USER_PROFILE:-off}" = "on" ]; then
http_check "2${VALIDATOR_ADMIN_API_PORT_SUFFIX}" "/api/validator/readyz"
fi
if [ "${APP_PROVIDER_PROFILE:-off}" = "on" ]; then
http_check "3${VALIDATOR_ADMIN_API_PORT_SUFFIX}" "/api/validator/readyz"
fi
if [ "${SV_PROFILE:-off}" = "on" ]; then
http_check "4${VALIDATOR_ADMIN_API_PORT_SUFFIX}" "/api/validator/readyz"
http_check "5012" "/api/scan/readyz"
http_check "5014" "/api/sv/readyz"
fi
EOF
chmod +x "${healthcheck}"
}
configure_quickstart_localnet() {
local env_file=""
local canton_conf="${QUICKSTART_DIR}/docker/modules/localnet/conf/canton/app.conf"
local splice_sv_conf="${QUICKSTART_DIR}/docker/modules/localnet/conf/splice/sv/app.conf"
SPLICE_CONFIG_CHANGED="false"
if [[ "$(quickstart_env_value "${SPLICE_CONFIG_PENDING_KEY}")" == "true" ]]; then
SPLICE_CONFIG_CHANGED="true"
fi
for env_file in "${QUICKSTART_DIR}/.env" "${QUICKSTART_DIR}/.env.local"; do
if [[ -n "${LOCALNET_SPLICE_VERSION}" ]]; then
set_quickstart_env_value "${env_file}" "SPLICE_VERSION" "${LOCALNET_SPLICE_VERSION}"
fi
if [[ -n "${LOCALNET_SCRIBE_VERSION}" ]]; then
set_quickstart_env_value "${env_file}" "SCRIBE_VERSION" "${LOCALNET_SCRIBE_VERSION}"
fi
if [[ -n "${CANTON_LOCALNET_DAML_RUNTIME_VERSION:-}" ]]; then
set_quickstart_env_value "${env_file}" "DAML_RUNTIME_VERSION" "${CANTON_LOCALNET_DAML_RUNTIME_VERSION}"
fi
done
if [[ ! -f "${canton_conf}" ]]; then
log "Canton quickstart config not found: ${canton_conf}"
exit 1
fi
if [[ ! -f "${splice_sv_conf}" ]]; then
log "Splice SV quickstart config not found: ${splice_sv_conf}"
exit 1
fi
if ! grep -Eq '^[[:space:]]*canton\.scan-apps\.scan-app\.enable-forced-acs-snapshots[[:space:]]*=[[:space:]]*(true|yes|on)[[:space:]]*$' "${splice_sv_conf}"; then
cat >>"${splice_sv_conf}" <<'EOF'
# LocalNet integration tests use this endpoint to create deterministic snapshot-backed fixtures.
canton.scan-apps.scan-app.enable-forced-acs-snapshots = true
EOF
set_quickstart_env_value "${QUICKSTART_DIR}/.env.local" "${SPLICE_CONFIG_PENDING_KEY}" "true"
SPLICE_CONFIG_CHANGED="true"
fi
if [[ -n "${LOCALNET_PROTOCOL_VERSION}" ]]; then
PROTOCOL_VERSION="${LOCALNET_PROTOCOL_VERSION}" perl -0pi -e '
my $protocol_version = $ENV{"PROTOCOL_VERSION"};
s/initial-protocol-version = \d+/initial-protocol-version = $protocol_version/g;
s/(non-standard-config = yes\n)(?![[:space:]]*alpha-version-support = yes)/$1 alpha-version-support = yes\n/;
s/(initial-protocol-version = \d+\n)(?![[:space:]]*alpha-version-support = yes)/$1 alpha-version-support = yes\n/;
' "${canton_conf}"
fi
patch_quickstart_canton_healthcheck
patch_quickstart_splice_healthcheck
}
requested_auth_mode() {
printf '%s' "${CANTON_LOCALNET_AUTH_MODE:-oauth2}"
}
quickstart_env_value() {
local key="$1"
local parsed_value=""
if [[ -f "${QUICKSTART_DIR}/.env.local" ]]; then
parsed_value="$(awk -F= -v key="${key}" '
$0 ~ /^[[:space:]]*#/ { next }
$1 == key {
value = substr($0, index($0, "=") + 1)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", value)
gsub(/^"|"$/, "", value)
gsub(/^'\''|'\''$/, "", value)
parsed = value
}
END {
if (parsed != "") {
print parsed
}
}
' "${QUICKSTART_DIR}/.env.local")"
fi
printf '%s' "${parsed_value}"
}
configured_auth_mode() {
quickstart_env_value "AUTH_MODE"
}
quickstart_profile_config_complete() {
local key=""
for key in OBSERVABILITY_ENABLED AUTH_MODE PARTY_HINT TEST_MODE; do
if [[ -z "$(quickstart_env_value "${key}")" ]]; then
return 1
fi
done
return 0
}
verify_configured_auth_mode() {
local expected_auth_mode="$1"
local actual_auth_mode=""
actual_auth_mode="$(configured_auth_mode)"
if [[ "${actual_auth_mode}" != "${expected_auth_mode}" ]]; then
log "cn-quickstart setup did not configure AUTH_MODE=${expected_auth_mode}; found '${actual_auth_mode:-unset}'."
exit 1
fi
}
current_auth_mode() {
local configured=""
configured="$(configured_auth_mode)"
if [[ -n "${configured}" ]]; then
printf '%s' "${configured}"
return
fi
requested_auth_mode
}
run_quickstart_setup() {
local auth_mode=""
auth_mode="$(requested_auth_mode)"
case "${auth_mode}" in
shared-secret)
log "Running cn-quickstart setup (shared-secret mode)..."
(
cd "${QUICKSTART_DIR}"
printf 'Y\nn\n\n' | make setup || true
)
verify_configured_auth_mode "shared-secret"
;;
oauth2)
log "Running cn-quickstart setup (OAuth2 enabled)..."
(
cd "${QUICKSTART_DIR}"
# Match CI behavior first, then fall back to prompt-based answers for newer setup flows.
echo "2" | make setup || true
if [[ "$(configured_auth_mode)" != "oauth2" ]]; then
printf 'y\ny\n\nn\n' | make setup
fi
)
verify_configured_auth_mode "oauth2"
;;
*)
log "Unsupported CANTON_LOCALNET_AUTH_MODE: ${auth_mode}"
exit 1
;;
esac
}
quickstart_setup() {
if [[ ! -f "${QUICKSTART_DIR}/.env.local" ]]; then
run_quickstart_setup
elif ! quickstart_profile_config_complete; then
log "Regenerating incomplete cn-quickstart config."
rm -f "${QUICKSTART_DIR}/.env.local"
run_quickstart_setup
elif [[ -n "${CANTON_LOCALNET_AUTH_MODE:-}" && "$(configured_auth_mode)" != "${CANTON_LOCALNET_AUTH_MODE}" ]]; then
log "Regenerating cn-quickstart config for CANTON_LOCALNET_AUTH_MODE=${CANTON_LOCALNET_AUTH_MODE}."
rm -f "${QUICKSTART_DIR}/.env.local"
run_quickstart_setup
else
log "Reusing existing ${QUICKSTART_DIR}/.env.local."
fi
if [[ ! -f "${QUICKSTART_DIR}/.env.local" ]]; then
log "cn-quickstart setup failed: ${QUICKSTART_DIR}/.env.local was not created."
exit 1
fi
configure_quickstart_localnet
if quickstart_infra_only_enabled; then
log "Skipping Daml SDK install for infrastructure-only localnet."
return
fi
if [[ ! -x "${HOME}/.daml/bin/daml" ]]; then
log "Installing Daml SDK..."
(
cd "${QUICKSTART_DIR}"
make install-daml-sdk
)
else
log "Reusing existing Daml SDK at ${HOME}/.daml/bin/daml."
fi
}
quickstart_fast_start_enabled() {
is_truthy "${CANTON_LOCALNET_FAST_START:-true}"
}
quickstart_force_full_start() {
is_truthy "${CANTON_LOCALNET_FORCE_FULL_START:-false}"
}
quickstart_infra_only_enabled() {
is_truthy "${CANTON_LOCALNET_INFRA_ONLY:-false}"
}
quickstart_build_artifacts_ready() {
local missing_paths=()
if [[ ! -f "${QUICKSTART_DIR}/backend/build/distributions/backend.tar" ]]; then
missing_paths+=("backend/build/distributions/backend.tar")
fi
if [[ ! -d "${QUICKSTART_DIR}/frontend/dist" ]]; then
missing_paths+=("frontend/dist")
fi
if ! compgen -G "${QUICKSTART_DIR}/daml/licensing/.daml/dist/*.dar" >/dev/null; then
missing_paths+=("daml/licensing/.daml/dist/*.dar")
fi
if ! compgen -G "${QUICKSTART_DIR}/backend/build/otel-agent/opentelemetry-javaagent-*.jar" >/dev/null; then
missing_paths+=("backend/build/otel-agent/opentelemetry-javaagent-*.jar")
fi
if [[ ${#missing_paths[@]} -gt 0 ]]; then
log "Fast start unavailable; missing quickstart build artifacts: ${missing_paths[*]}"
return 1
fi
return 0
}
extract_quickstart_compose_up_command() {
(
cd "${QUICKSTART_DIR}"
make -n start 2>/dev/null | awk '/docker compose .* up -d --no-recreate/{line=$0} END{print line}'
)
}
try_fast_start_localnet() {
local compose_up_command=""
compose_up_command="$(extract_quickstart_compose_up_command)"
if [[ -z "${compose_up_command}" ]]; then
log "Unable to determine quickstart compose startup command."
return 1
fi
log "Starting cn-quickstart with fast path (skip quickstart rebuild)..."
run_quickstart_command "${compose_up_command}"
}
start_infra_only_localnet() {
log "Starting cn-quickstart LocalNet infrastructure only (skip quickstart app, PQS, and onboarding)."
run_infra_compose 'up -d --no-recreate'
}
splice_container_running() {
local running=""
running="$(run_docker inspect --format '{{.State.Running}}' splice 2>/dev/null || true)"
[[ "${running}" == "true" ]]
}
recreate_splice_after_config_change() {
local should_recreate="$1"
if [[ "${should_recreate}" != "true" ]]; then
return
fi
log "Recreating the running Splice service to apply updated LocalNet configuration."
run_infra_compose 'up -d --no-deps --force-recreate splice'
}
mark_splice_config_applied() {
if [[ "${SPLICE_CONFIG_CHANGED}" != "true" ]]; then
return
fi
set_quickstart_env_value "${QUICKSTART_DIR}/.env.local" "${SPLICE_CONFIG_PENDING_KEY}" "false"
SPLICE_CONFIG_CHANGED="false"
}
stop_infra_only_localnet() {
if [[ ! -f "${QUICKSTART_DIR}/.env.local" ]]; then
return
fi
log "Stopping cn-quickstart LocalNet infrastructure-only stack..."
run_infra_compose down || true
}
wait_for_services() {
local auth_mode=""
local code=""
local ledger_code=""
auth_mode="$(current_auth_mode)"
if [[ "${auth_mode}" == "oauth2" ]]; then
log "Waiting for Keycloak..."
for _ in $(seq 1 30); do
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://localhost:8082/realms/AppProvider >/dev/null 2>&1; then
break
fi
sleep 2
done
if ! curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://localhost:8082/realms/AppProvider >/dev/null 2>&1; then
log "Keycloak did not become ready."
exit 1
fi
else
log "Checking for Keycloak (optional in ${auth_mode} mode)..."
for _ in $(seq 1 5); do
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://localhost:8082/realms/AppProvider >/dev/null 2>&1; then
log "Keycloak is ready."
break
fi
sleep 2
done
fi
log "Waiting for Validator API..."
for _ in $(seq 1 "${VALIDATOR_READY_ATTEMPTS}"); do
code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3903/api/validator/v0/wallet/user-status || true)"
if [[ "${code}" == "200" || "${code}" == "401" ]]; then
break
fi
sleep 2
done
code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3903/api/validator/v0/wallet/user-status || true)"
if [[ "${code}" != "200" && "${code}" != "401" ]]; then
log "Validator API did not become ready."
exit 1
fi
log "Waiting for Scan API..."
for _ in $(seq 1 "${SCAN_READY_ATTEMPTS}"); do
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://scan.localhost:4000/api/scan/v0/dso-party-id >/dev/null 2>&1; then
break
fi
sleep 2
done
if ! curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://scan.localhost:4000/api/scan/v0/dso-party-id >/dev/null 2>&1; then
log "Scan API did not become ready."
exit 1
fi
log "Waiting for Ledger JSON API..."
for _ in $(seq 1 60); do
ledger_code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3975/v2/version || true)"
if [[ "${ledger_code}" == "200" || "${ledger_code}" == "401" ]]; then
break
fi
sleep 2
done
ledger_code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3975/v2/version || true)"
if [[ "${ledger_code}" != "200" && "${ledger_code}" != "401" ]]; then
log "Ledger JSON API did not become ready (HTTP ${ledger_code})."
exit 1
fi
log "All localnet services are ready."
}
start_localnet() {
local should_recreate_splice="false"
if [[ "${SPLICE_CONFIG_CHANGED}" == "true" ]] && splice_container_running; then
should_recreate_splice="true"
fi
if quickstart_force_full_start; then
log "Forcing full cn-quickstart start (CANTON_LOCALNET_FORCE_FULL_START=true)."
run_quickstart_make start
recreate_splice_after_config_change "${should_recreate_splice}"
wait_for_services
mark_splice_config_applied
return
fi
if quickstart_infra_only_enabled; then
start_infra_only_localnet
recreate_splice_after_config_change "${should_recreate_splice}"
wait_for_services
mark_splice_config_applied
return
fi
if quickstart_fast_start_enabled && quickstart_build_artifacts_ready; then
if try_fast_start_localnet; then
recreate_splice_after_config_change "${should_recreate_splice}"
wait_for_services
mark_splice_config_applied
return
fi
log "Fast start failed; falling back to full cn-quickstart start."
fi
log "Starting cn-quickstart with full build..."
run_quickstart_make start
recreate_splice_after_config_change "${should_recreate_splice}"
wait_for_services
mark_splice_config_applied
}
stop_localnet() {
if [[ ! -d "${QUICKSTART_DIR}" ]]; then
log "cn-quickstart directory not found; nothing to stop."
stop_managed_dockerd
return
fi
log "Stopping cn-quickstart..."
stop_infra_only_localnet
if [[ -f "${QUICKSTART_DIR}/Makefile" ]]; then
run_quickstart_make stop || true
else
log "Quickstart Makefile not found; skipping quickstart stop target."
fi
stop_managed_dockerd
}
stop_managed_dockerd() {
local pid=""
local cmd=""
if [[ ! -f "${DOCKERD_PID_FILE}" ]]; then
return
fi
pid="$(cat "${DOCKERD_PID_FILE}" 2>/dev/null || true)"
if [[ -z "${pid}" ]]; then
rm -f "${DOCKERD_PID_FILE}" "${DOCKERD_LOG_FILE}"
return
fi
if ! ps -p "${pid}" >/dev/null 2>&1; then
rm -f "${DOCKERD_PID_FILE}" "${DOCKERD_LOG_FILE}"
return
fi
cmd="$(ps -p "${pid}" -o comm= 2>/dev/null | tr -d '[:space:]')"
if [[ "${cmd}" != "dockerd" ]]; then
log "PID ${pid} is not dockerd; skipping daemon cleanup."
rm -f "${DOCKERD_PID_FILE}"
return
fi
if ! sudo -n true >/dev/null 2>&1; then
log "Cannot stop managed dockerd without passwordless sudo."
return
fi
log "Stopping managed dockerd (pid ${pid})..."
sudo kill -TERM "${pid}" >/dev/null 2>&1 || true
for _ in $(seq 1 10); do
if ! ps -p "${pid}" >/dev/null 2>&1; then
break
fi
sleep 1
done
if ps -p "${pid}" >/dev/null 2>&1; then
sudo kill -KILL "${pid}" >/dev/null 2>&1 || true
fi
rm -f "${DOCKERD_PID_FILE}" "${DOCKERD_LOG_FILE}"
}
status_localnet() {
if docker_ready; then
log "Docker daemon is running."
else
log "Docker daemon is not running."
exit 1
fi
run_docker ps --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
echo
keycloak_ok="no"
validator_ok="no"
scan_ok="no"
ledger_ok="no"
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://localhost:8082/realms/AppProvider >/dev/null 2>&1; then
keycloak_ok="yes"
fi
validator_code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3903/api/validator/v0/wallet/user-status || true)"
if [[ "${validator_code}" == "200" || "${validator_code}" == "401" ]]; then
validator_ok="yes"
fi
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://scan.localhost:4000/api/scan/v0/dso-party-id >/dev/null 2>&1; then
scan_ok="yes"
fi
ledger_code="$(curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -sS -o /dev/null -w '%{http_code}' http://localhost:3975/v2/version || true)"
if [[ "${ledger_code}" == "200" || "${ledger_code}" == "401" ]]; then
ledger_ok="yes"
fi
printf 'Keycloak ready: %s\n' "${keycloak_ok}"
printf 'Validator ready: %s (HTTP %s)\n' "${validator_ok}" "${validator_code:-n/a}"
printf 'Scan ready: %s\n' "${scan_ok}"
printf 'Ledger JSON API ready: %s (HTTP %s)\n' "${ledger_ok}" "${ledger_code:-n/a}"
}
show_localnet_logs() {
log "==================== Docker Containers ===================="
if docker_ready; then
run_docker ps -a || true
else
log "Docker daemon is not running."
fi
echo
log "==================== Docker Compose Logs ===================="
if [[ -f "${QUICKSTART_DIR}/.env.local" ]]; then
run_infra_compose 'logs --tail=100' || true
elif [[ -d "${QUICKSTART_DIR}" ]]; then
log "Quickstart local env not found; skipping compose logs: ${QUICKSTART_DIR}/.env.local"
else
log "cn-quickstart directory not found: ${QUICKSTART_DIR}"
fi
echo
log "==================== Canton Logs ===================="
if [[ -f "${QUICKSTART_DIR}/logs/canton.log" ]]; then
tail -100 "${QUICKSTART_DIR}/logs/canton.log" || true
else
log "Canton log not found: ${QUICKSTART_DIR}/logs/canton.log"
fi
if [[ -f "${DOCKERD_LOG_FILE}" ]]; then
echo
log "==================== Managed Docker Daemon Logs ===================="
tail -100 "${DOCKERD_LOG_FILE}" || true
fi
}
run_smoke() {
local auth_mode=""
local validator_code=""
local ledger_code=""
log "Running localnet smoke checks..."
auth_mode="$(current_auth_mode)"
if curl --connect-timeout "${CURL_CONNECT_TIMEOUT}" --max-time "${CURL_MAX_TIME}" -fsS http://localhost:8082/realms/AppProvider >/dev/null 2>&1; then
log "Keycloak is reachable."
elif [[ "${auth_mode}" == "oauth2" ]]; then
log "Keycloak is not reachable."
exit 1