-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrolling-update.sh
More file actions
executable file
·1538 lines (1373 loc) · 55.9 KB
/
rolling-update.sh
File metadata and controls
executable file
·1538 lines (1373 loc) · 55.9 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)"
usage() {
cat <<'EOF'
Usage:
NODES="n1=raft-1.internal,n2=raft-2.internal,n3=raft-3.internal" ./scripts/rolling-update.sh
Required environment:
NODES
Comma-separated raft node map in rollout order: "<raftId>=<advertised-host>,..."
Optional environment:
ROLLING_UPDATE_ENV_FILE
Shell env file to source before evaluating the rest of the settings.
SSH_TARGETS
Comma-separated SSH target map when SSH hosts differ from advertised hosts:
"<raftId>=<ssh-host-or-user@host>,..."
If omitted, the script SSHes to the advertised host and prefixes SSH_USER.
ROLLING_ORDER
Comma-separated raft IDs to override the rollout order.
IMAGE
SSH_USER
CONTAINER_NAME
DATA_DIR
SERVER_ENTRYPOINT
RAFT_ENGINE
RAFT_PORT
REDIS_PORT
DYNAMO_PORT
RAFT_TO_REDIS_MAP
RAFT_TO_S3_MAP
S3_PORT
ENABLE_S3
S3_REGION
S3_CREDENTIALS_FILE
Path to an S3 credentials file on each target host. This file must already
exist and be readable on every remote node; it will be bind-mounted into
the container at the same path.
S3_PATH_STYLE_ONLY
SQS adapter (opt-in; ENABLE_SQS=true turns the listener on)
ENABLE_SQS
Master switch (default false). When true, the script forwards
--sqsAddress, --sqsRegion, and --raftSqsMap (plus optional
--sqsCredentialsFile / --sqsFifoPartitionMap) to docker run.
Required for the admin /admin/api/v1/sqs/* endpoints to mount —
main.go gates registration on r.sqsServer != nil, which only
happens when --sqsAddress is non-empty.
SQS_PORT
SQS HTTP listener port on each node (default 9324, the conventional
SQS-compatible-server port). Mirrors S3_PORT.
SQS_REGION
SigV4 region the adapter signs against (default us-east-1).
SQS_CREDENTIALS_FILE
Optional path to a JSON credentials file on each target host
(same shape as S3_CREDENTIALS_FILE). Empty value runs the adapter
as an open endpoint — clients may sign with any credentials.
RAFT_TO_SQS_MAP
Optional override; auto-derived from NODES + RAFT_PORT + SQS_PORT
when ENABLE_SQS=true and the variable is empty.
SQS_FIFO_PARTITION_MAP
Optional HT-FIFO partition routing map (queue.fifo:N=group_0,...).
Empty value disables the capability gate's coverage check;
partitioned queues route to the default Raft group.
HEALTH_TIMEOUT_SECONDS
LEADERSHIP_TRANSFER_TIMEOUT_SECONDS
LEADER_DISCOVERY_TIMEOUT_SECONDS
ROLLING_DELAY_SECONDS
SSH_CONNECT_TIMEOUT_SECONDS
SSH_STRICT_HOST_KEY_CHECKING
RAFTADMIN_BIN
RAFTADMIN_REMOTE_BIN
RAFTADMIN_RPC_TIMEOUT_SECONDS
RAFTADMIN_ALLOW_INSECURE
LEADERSHIP_TRANSFER_RETRY_ATTEMPTS
LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS
EXTRA_ENV
Whitespace-separated list of additional container environment variables to
forward to the remote docker run as `-e KEY=VALUE` flags. Format:
"KEY=VALUE [KEY=VALUE ...]" (e.g. "ELASTICKV_RAFT_DISPATCHER_LANES=1 ELASTICKV_PEBBLE_CACHE_MB=512").
Each pair must be KEY=VALUE with a non-empty KEY; pairs themselves must not
contain whitespace.
DEFAULT_EXTRA_ENV defaults to "GOMEMLIMIT=1800MiB" (Go runtime soft memory
ceiling; GC works harder before approaching the hard --memory limit so the
kernel OOM killer is not triggered). Merged with EXTRA_ENV before forwarding;
if a user-supplied EXTRA_ENV entry sets the same KEY, the user value wins.
Set DEFAULT_EXTRA_ENV="" to disable the default.
CONTAINER_MEMORY_LIMIT
docker run --memory value (default: 2500m). Hard container-scoped memory
ceiling; any OOM kill is contained to the elastickv container rather than
cascading to host processes (e.g. qemu-guest-agent, systemd). Paired with
GOMEMLIMIT=1800MiB so Go GC preempts the kill. Set to "" to disable.
Admin dashboard (opt-in; ADMIN_ENABLED=true turns the listener on)
ADMIN_ENABLED
Master switch (default false). When true, the listener is configured
using the rest of the ADMIN_* variables and the script bind-mounts the
referenced files (signing key, optional previous key, TLS pair) into
the container read-only. Required when enabled:
ADMIN_SESSION_SIGNING_KEY_FILE plus at least one of ADMIN_FULL_ACCESS_KEYS
/ ADMIN_READ_ONLY_ACCESS_KEYS.
ADMIN_ADDRESS
host:port for the admin listener (default: daemon-internal 127.0.0.1:8080).
Set to a reachable bind only with ADMIN_TLS_CERT_FILE/_KEY_FILE.
ADMIN_FULL_ACCESS_KEYS, ADMIN_READ_ONLY_ACCESS_KEYS
Comma-separated allow-lists. Same key must NOT appear in both. Sessions
re-validate against this list on every state-changing request, so
revoking a key takes effect on the next admin write rather than
waiting for the JWT to expire.
ADMIN_SESSION_SIGNING_KEY_FILE
Required. Path on the remote host to the base64-encoded HS256 key
(exactly 64 raw bytes — 88 base64 chars with standard padding, or 86
with RawURLEncoding). Bind-mounted read-only at the same path inside
the container. Must be identical on every node.
ADMIN_SESSION_SIGNING_KEY_PREVIOUS_FILE
Optional. Path to the previous HS256 key, used only for verification
during a rotation window. New tokens always sign with the primary key.
ADMIN_TLS_CERT_FILE, ADMIN_TLS_KEY_FILE
Optional PEM cert + key for the admin listener. Both must be set
together. Required when ADMIN_ADDRESS is non-loopback unless
ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK=true.
ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK
Default false. When true, allows the admin listener on a non-loopback
bind without TLS. Strongly discouraged. See docs/admin.md for the
interaction with ADMIN_ALLOW_INSECURE_DEV_COOKIE — without the
latter, the dashboard mints Secure cookies the browser will refuse
to send over plaintext, breaking sessions end-to-end.
ADMIN_ALLOW_INSECURE_DEV_COOKIE
Default false. When true, mints session cookies without the Secure
attribute (for local plaintext development only).
Notes:
- If RAFT_TO_REDIS_MAP is unset, it is derived automatically from NODES,
RAFT_PORT, and REDIS_PORT.
- If RAFT_TO_S3_MAP is unset, it is derived automatically from NODES,
RAFT_PORT, and S3_PORT.
- If RAFT_TO_SQS_MAP is unset and ENABLE_SQS=true, it is derived
automatically from NODES, RAFT_PORT, and SQS_PORT.
- If RAFTADMIN_BIN is set, it must already be executable on the local control host.
EOF
}
if [[ "${1:-}" == "--help" || "${1:-}" == "-h" ]]; then
usage
exit 0
fi
if [[ -n "${ROLLING_UPDATE_ENV_FILE:-}" ]]; then
if [[ ! -f "$ROLLING_UPDATE_ENV_FILE" ]]; then
echo "ROLLING_UPDATE_ENV_FILE not found: $ROLLING_UPDATE_ENV_FILE" >&2
exit 1
fi
# shellcheck disable=SC1090
source "$ROLLING_UPDATE_ENV_FILE"
fi
IMAGE="${IMAGE:-ghcr.io/bootjp/elastickv:latest}"
SSH_USER="${SSH_USER:-${USER:-$(id -un)}}"
CONTAINER_NAME="${CONTAINER_NAME:-elastickv}"
DATA_DIR="${DATA_DIR:-/var/lib/elastickv}"
SERVER_ENTRYPOINT="${SERVER_ENTRYPOINT:-/app}"
RAFT_ENGINE="${RAFT_ENGINE:-etcd}"
RAFT_PORT="${RAFT_PORT:-50051}"
REDIS_PORT="${REDIS_PORT:-6379}"
DYNAMO_PORT="${DYNAMO_PORT:-8000}"
S3_PORT="${S3_PORT:-9000}"
ENABLE_S3="${ENABLE_S3:-true}"
S3_REGION="${S3_REGION:-us-east-1}"
S3_CREDENTIALS_FILE="${S3_CREDENTIALS_FILE:-}"
S3_PATH_STYLE_ONLY="${S3_PATH_STYLE_ONLY:-true}"
# SQS adapter knobs (mirror the S3 shape). ENABLE_SQS is the master
# switch; when false, no SQS-related flags are passed to the
# container and admin SQS endpoints stay 404 because sqsServer is
# nil. Set ENABLE_SQS=true and the script forwards
# --sqsAddress / --sqsRegion / --raftSqsMap (and optionally
# --sqsCredentialsFile and --sqsFifoPartitionMap) to docker run.
ENABLE_SQS="${ENABLE_SQS:-false}"
SQS_PORT="${SQS_PORT:-9324}"
SQS_REGION="${SQS_REGION:-us-east-1}"
SQS_CREDENTIALS_FILE="${SQS_CREDENTIALS_FILE:-}"
# HT-FIFO partition routing map. Empty means "single-shard / no
# partitioned-FIFO routing"; the capability gate's coverage check
# is bypassed (resolver==nil) and partitioned queues land on the
# default group. Format documented at main.go::buildSQSFifoPartitionMap.
SQS_FIFO_PARTITION_MAP="${SQS_FIFO_PARTITION_MAP:-}"
# Wall-clock budget wait_for_grpc allows for the restarted container to
# bind its gRPC port. Sized to cover the worst-case cold-start path:
# 1. pebble.Open (fsm.db, ~seconds)
# 2. loadWalState -> restoreSnapshotState -> pebbleStore.Restore
# (restorePebbleNativeAtomic + swapInTempDB), which rewrites the
# entire FSM into a sibling temp directory before renaming it into
# place. For multi-GB FSMs on production hardware this is the
# dominant cost — we observed ~46 s on a 4-shard SSD-backed node
# with a ~5 M-key FSM; multi-GB datasets scale linearly with disk
# throughput.
# 3. WAL replay of the entries between the persisted snapshot index
# and the latest WAL entry (typically thousands, sub-second).
# 4. Raft follower-ization + gRPC.Serve bind.
# A 60 s ceiling guaranteed a timeout-induced restart loop on any node
# whose FSM exceeded ~1 GiB. 300 s comfortably absorbs (1)+(2)+(3)+(4)
# for the multi-GiB working sets we operate against today and still
# fails fast on genuine crash-loop bugs (which never finish (1) or (2)).
# Operators with smaller FSMs can shrink HEALTH_TIMEOUT_SECONDS via env.
# Root-cause fix (skip restoreSnapshotState when the on-disk FSM is
# already at >= snapshot.Metadata.Index): see PR #910 and
# docs/design/2026_06_02_idempotent_snapshot_restore.md. Once that
# lands and steady-state skip rates are observed in production, this
# default can be tightened back down.
HEALTH_TIMEOUT_SECONDS="${HEALTH_TIMEOUT_SECONDS:-300}"
LEADERSHIP_TRANSFER_TIMEOUT_SECONDS="${LEADERSHIP_TRANSFER_TIMEOUT_SECONDS:-30}"
LEADER_DISCOVERY_TIMEOUT_SECONDS="${LEADER_DISCOVERY_TIMEOUT_SECONDS:-30}"
ROLLING_DELAY_SECONDS="${ROLLING_DELAY_SECONDS:-2}"
SSH_CONNECT_TIMEOUT_SECONDS="${SSH_CONNECT_TIMEOUT_SECONDS:-10}"
SSH_STRICT_HOST_KEY_CHECKING="${SSH_STRICT_HOST_KEY_CHECKING:-accept-new}"
RAFTADMIN_REMOTE_BIN="${RAFTADMIN_REMOTE_BIN:-/tmp/elastickv-raftadmin}"
# Default raised from 5 s to 15 s after the 2026-05-21 reproduction
# (https://github.com/bootjp/elastickv/actions/runs/26198185540) where
# the previous node's restart left raft in a transient pre-stable state
# for the next leadership-transfer RPC. 5 s gave the RPC no headroom
# over a brief raft-internal abort and the script bailed out. 15 s is
# still small enough that a truly stuck call surfaces quickly.
RAFTADMIN_RPC_TIMEOUT_SECONDS="${RAFTADMIN_RPC_TIMEOUT_SECONDS:-15}"
RAFTADMIN_ALLOW_INSECURE="${RAFTADMIN_ALLOW_INSECURE:-true}"
# LEADERSHIP_TRANSFER_RETRY_ATTEMPTS bounds how many times we re-issue
# a leadership_transfer_to_server RPC when raft returns
# FailedPrecondition (e.g. "etcd raft leadership transfer aborted")
# because the candidate's log has not yet caught up or an in-flight
# conf change is blocking the transfer. The first attempt counts
# toward the budget; ATTEMPTS=1 means "no retry". Default 3 covers
# the ~10 s catch-up window that follows a peer's rolling restart.
LEADERSHIP_TRANSFER_RETRY_ATTEMPTS="${LEADERSHIP_TRANSFER_RETRY_ATTEMPTS:-3}"
LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS="${LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS:-5}"
NODES="${NODES:-}"
SSH_TARGETS="${SSH_TARGETS:-}"
ROLLING_ORDER="${ROLLING_ORDER:-}"
RAFT_TO_REDIS_MAP="${RAFT_TO_REDIS_MAP:-}"
RAFT_TO_S3_MAP="${RAFT_TO_S3_MAP:-}"
RAFT_TO_SQS_MAP="${RAFT_TO_SQS_MAP:-}"
# Admin dashboard knobs. ADMIN_ENABLED is the master switch; the
# remaining variables only take effect when ADMIN_ENABLED=true.
# Required when enabled: ADMIN_SESSION_SIGNING_KEY_FILE plus at
# least one of ADMIN_FULL_ACCESS_KEYS / ADMIN_READ_ONLY_ACCESS_KEYS.
# See docs/admin.md and docs/admin_deployment.md for the contract.
ADMIN_ENABLED="${ADMIN_ENABLED:-false}"
ADMIN_ADDRESS="${ADMIN_ADDRESS:-}"
ADMIN_FULL_ACCESS_KEYS="${ADMIN_FULL_ACCESS_KEYS:-}"
ADMIN_READ_ONLY_ACCESS_KEYS="${ADMIN_READ_ONLY_ACCESS_KEYS:-}"
ADMIN_SESSION_SIGNING_KEY_FILE="${ADMIN_SESSION_SIGNING_KEY_FILE:-}"
ADMIN_SESSION_SIGNING_KEY_PREVIOUS_FILE="${ADMIN_SESSION_SIGNING_KEY_PREVIOUS_FILE:-}"
ADMIN_TLS_CERT_FILE="${ADMIN_TLS_CERT_FILE:-}"
ADMIN_TLS_KEY_FILE="${ADMIN_TLS_KEY_FILE:-}"
ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK="${ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK:-false}"
ADMIN_ALLOW_INSECURE_DEV_COOKIE="${ADMIN_ALLOW_INSECURE_DEV_COOKIE:-false}"
# KeyViz heatmap sampler. KEYVIZ_ENABLED is the master switch; the
# remaining variables only take effect when KEYVIZ_ENABLED=true. The
# sampler is ungated when admin is disabled (it's read-only in-memory
# state); it just produces no callers without --adminEnabled.
KEYVIZ_ENABLED="${KEYVIZ_ENABLED:-false}"
KEYVIZ_FANOUT_NODES="${KEYVIZ_FANOUT_NODES:-}"
# KeyViz hot-key Top-K drill-down (Phase 2-A++). Master switch +
# four tuning knobs. When KEYVIZ_HOT_KEYS_ENABLED != "true" the
# build_keyviz_flags helper skips every --keyvizHotKeys* flag so
# existing deploys see no behaviour change. Defaults match the
# sampler-side constants in keyviz/sampler.go; empty values let
# the daemon use those same defaults — keep them empty here so
# bumping the Go-side default in a future release rolls out
# without a separate env-file update.
KEYVIZ_HOT_KEYS_ENABLED="${KEYVIZ_HOT_KEYS_ENABLED:-false}"
KEYVIZ_HOT_KEYS_PER_ROUTE="${KEYVIZ_HOT_KEYS_PER_ROUTE:-}"
KEYVIZ_HOT_KEYS_SAMPLE_RATE="${KEYVIZ_HOT_KEYS_SAMPLE_RATE:-}"
KEYVIZ_HOT_KEYS_QUEUE_SIZE="${KEYVIZ_HOT_KEYS_QUEUE_SIZE:-}"
KEYVIZ_HOT_KEYS_MAX_KEY_LEN="${KEYVIZ_HOT_KEYS_MAX_KEY_LEN:-}"
# Validate the three boolean ADMIN_* flags must be the literal "true"
# or "false" — they are forwarded to the remote shell unquoted (no
# printf %q) for readability, which is only safe when the value is
# already metacharacter-free. Reject anything else here so an operator
# who typed "True", "1", or a stray quote sees a script-level error
# pointing at the variable name instead of an inscrutable failure
# inside the SSH heredoc.
for _bool_var in ADMIN_ENABLED ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK ADMIN_ALLOW_INSECURE_DEV_COOKIE KEYVIZ_ENABLED KEYVIZ_HOT_KEYS_ENABLED ENABLE_S3 ENABLE_SQS; do
case "${!_bool_var}" in
true|false) ;;
*)
echo "rolling-update: ${_bool_var} must be 'true' or 'false', got '${!_bool_var}'" >&2
exit 1
;;
esac
done
unset _bool_var
# Container OOM defenses. See usage() for rationale. Empty string disables.
DEFAULT_EXTRA_ENV="${DEFAULT_EXTRA_ENV-GOMEMLIMIT=1800MiB}"
CONTAINER_MEMORY_LIMIT="${CONTAINER_MEMORY_LIMIT-2500m}"
if [[ -z "$NODES" ]]; then
echo "NODES is required" >&2
usage >&2
exit 1
fi
SSH_BASE_OPTS=(
-o BatchMode=yes
-o ConnectTimeout="${SSH_CONNECT_TIMEOUT_SECONDS}"
-o StrictHostKeyChecking="${SSH_STRICT_HOST_KEY_CHECKING}"
)
SCP_BASE_OPTS=(-q "${SSH_BASE_OPTS[@]}")
RAFTADMIN_LOCAL_BIN="${RAFTADMIN_BIN:-}"
RAFTADMIN_TMP_DIR=""
RAFTADMIN_LINUX_AMD64_BIN=""
RAFTADMIN_LINUX_ARM64_BIN=""
NODE_IDS=()
NODE_HOSTS=()
ROLLING_NODE_IDS=()
cleanup() {
if [[ -n "$RAFTADMIN_TMP_DIR" && -d "$RAFTADMIN_TMP_DIR" ]]; then
rm -rf "$RAFTADMIN_TMP_DIR"
fi
}
trap cleanup EXIT
contains_value() {
local needle="$1"
shift
local v
for v in "$@"; do
if [[ "$v" == "$needle" ]]; then
return 0
fi
done
return 1
}
lookup_mapping() {
local key="$1"
local mapping="$2"
local pair entry_key entry_value
[[ -n "$mapping" ]] || return 1
IFS=',' read -r -a pairs <<< "$mapping"
for pair in "${pairs[@]}"; do
pair="${pair//[[:space:]]/}"
[[ -n "$pair" ]] || continue
[[ "$pair" == *=* ]] || continue
entry_key="${pair%%=*}"
entry_value="${pair#*=}"
if [[ "$entry_key" == "$key" ]]; then
printf '%s\n' "$entry_value"
return 0
fi
done
return 1
}
parse_nodes() {
local pair node_id node_host
IFS=',' read -r -a pairs <<< "$NODES"
for pair in "${pairs[@]}"; do
pair="${pair//[[:space:]]/}"
[[ -n "$pair" ]] || continue
if [[ "$pair" != *=* ]]; then
echo "invalid NODES entry: $pair" >&2
exit 1
fi
node_id="${pair%%=*}"
node_host="${pair#*=}"
if [[ -z "$node_id" || -z "$node_host" ]]; then
echo "invalid NODES entry: $pair" >&2
exit 1
fi
if contains_value "$node_id" "${NODE_IDS[@]}"; then
echo "duplicate raft ID in NODES: $node_id" >&2
exit 1
fi
NODE_IDS+=("$node_id")
NODE_HOSTS+=("$node_host")
done
if [[ "${#NODE_IDS[@]}" -eq 0 ]]; then
echo "NODES did not contain any nodes" >&2
exit 1
fi
}
node_host_by_id() {
local wanted_id="$1"
local i
for i in "${!NODE_IDS[@]}"; do
if [[ "${NODE_IDS[$i]}" == "$wanted_id" ]]; then
printf '%s\n' "${NODE_HOSTS[$i]}"
return 0
fi
done
return 1
}
ssh_target_by_id() {
local node_id="$1"
local target
target="$(lookup_mapping "$node_id" "$SSH_TARGETS" || true)"
if [[ -z "$target" ]]; then
target="$(node_host_by_id "$node_id")"
fi
if [[ "$target" == *@* ]]; then
printf '%s\n' "$target"
return 0
fi
printf '%s@%s\n' "$SSH_USER" "$target"
}
prepare_rolling_order() {
local entry
if [[ -z "$ROLLING_ORDER" ]]; then
ROLLING_NODE_IDS=("${NODE_IDS[@]}")
return 0
fi
IFS=',' read -r -a entries <<< "$ROLLING_ORDER"
for entry in "${entries[@]}"; do
entry="${entry//[[:space:]]/}"
[[ -n "$entry" ]] || continue
if ! contains_value "$entry" "${NODE_IDS[@]}"; then
echo "ROLLING_ORDER references unknown raft ID: $entry" >&2
exit 1
fi
if contains_value "$entry" "${ROLLING_NODE_IDS[@]}"; then
echo "ROLLING_ORDER contains duplicate raft ID: $entry" >&2
exit 1
fi
ROLLING_NODE_IDS+=("$entry")
done
if [[ "${#ROLLING_NODE_IDS[@]}" -eq 0 ]]; then
echo "ROLLING_ORDER did not contain any nodes" >&2
exit 1
fi
}
derive_raft_to_redis_map() {
local parts=()
local i
for i in "${!NODE_IDS[@]}"; do
parts+=("${NODE_HOSTS[$i]}:${RAFT_PORT}=${NODE_HOSTS[$i]}:${REDIS_PORT}")
done
(
IFS=,
printf '%s\n' "${parts[*]}"
)
}
derive_raft_to_s3_map() {
local parts=()
local i
for i in "${!NODE_IDS[@]}"; do
parts+=("${NODE_HOSTS[$i]}:${RAFT_PORT}=${NODE_HOSTS[$i]}:${S3_PORT}")
done
(
IFS=,
printf '%s\n' "${parts[*]}"
)
}
derive_raft_to_sqs_map() {
local parts=()
local i
for i in "${!NODE_IDS[@]}"; do
parts+=("${NODE_HOSTS[$i]}:${RAFT_PORT}=${NODE_HOSTS[$i]}:${SQS_PORT}")
done
(
IFS=,
printf '%s\n' "${parts[*]}"
)
}
ensure_local_raftadmin() {
if [[ -n "$RAFTADMIN_LOCAL_BIN" ]]; then
if [[ ! -x "$RAFTADMIN_LOCAL_BIN" ]]; then
echo "RAFTADMIN_BIN is not executable: $RAFTADMIN_LOCAL_BIN" >&2
exit 1
fi
return 0
fi
RAFTADMIN_TMP_DIR="$(mktemp -d)"
echo "[rolling-update] preparing native raftadmin helper build workspace"
if [[ ! -f "${REPO_ROOT}/cmd/raftadmin/main.go" ]]; then
echo "failed to locate repo raftadmin helper source at ${REPO_ROOT}/cmd/raftadmin" >&2
exit 1
fi
}
build_raftadmin_variant() {
local goos="$1"
local goarch="$2"
local out
if [[ -n "$RAFTADMIN_LOCAL_BIN" ]]; then
printf '%s\n' "$RAFTADMIN_LOCAL_BIN"
return 0
fi
out="${RAFTADMIN_TMP_DIR}/raftadmin-${goos}-${goarch}"
if [[ -x "$out" ]]; then
printf '%s\n' "$out"
return 0
fi
echo "[rolling-update] building native raftadmin helper for ${goos}/${goarch}" >&2
CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch" \
go build -C "$REPO_ROOT" -o "$out" ./cmd/raftadmin
chmod +x "$out"
printf '%s\n' "$out"
}
ensure_remote_raftadmin_binaries() {
if [[ -n "$RAFTADMIN_LOCAL_BIN" ]]; then
return 0
fi
RAFTADMIN_LINUX_AMD64_BIN="$(build_raftadmin_variant linux amd64)"
RAFTADMIN_LINUX_ARM64_BIN="$(build_raftadmin_variant linux arm64)"
}
copy_raftadmin_to_remote() {
local node_id="$1"
local ssh_target="$2"
echo "==> [helper@${node_id}] copying raftadmin"
if [[ -n "$RAFTADMIN_LOCAL_BIN" ]]; then
scp "${SCP_BASE_OPTS[@]}" "$RAFTADMIN_LOCAL_BIN" "${ssh_target}:${RAFTADMIN_REMOTE_BIN}"
else
scp "${SCP_BASE_OPTS[@]}" "$RAFTADMIN_LINUX_AMD64_BIN" "${ssh_target}:${RAFTADMIN_REMOTE_BIN}-amd64"
scp "${SCP_BASE_OPTS[@]}" "$RAFTADMIN_LINUX_ARM64_BIN" "${ssh_target}:${RAFTADMIN_REMOTE_BIN}-arm64"
fi
ssh "${SSH_BASE_OPTS[@]}" "$ssh_target" \
RAFTADMIN_REMOTE_BIN="$RAFTADMIN_REMOTE_BIN" \
HAS_CUSTOM_RAFTADMIN_BIN="${RAFTADMIN_LOCAL_BIN:+1}" \
'bash -s' <<'REMOTE_HELPER'
set -euo pipefail
if [[ -n "${HAS_CUSTOM_RAFTADMIN_BIN:-}" ]]; then
chmod +x "$RAFTADMIN_REMOTE_BIN"
exit 0
fi
case "$(uname -m)" in
x86_64|amd64)
cp "${RAFTADMIN_REMOTE_BIN}-amd64" "$RAFTADMIN_REMOTE_BIN"
;;
aarch64|arm64)
cp "${RAFTADMIN_REMOTE_BIN}-arm64" "$RAFTADMIN_REMOTE_BIN"
;;
*)
echo "unsupported remote architecture: $(uname -m)" >&2
exit 1
;;
esac
chmod +x "$RAFTADMIN_REMOTE_BIN"
# Clean up architecture-specific helper binaries after installing the final binary.
rm -f "${RAFTADMIN_REMOTE_BIN}-amd64" "${RAFTADMIN_REMOTE_BIN}-arm64"
REMOTE_HELPER
}
update_one_node() {
local node_id="$1"
local node_host="$2"
local ssh_target="$3"
local all_node_ids_csv all_node_hosts_csv
all_node_ids_csv="$(IFS=,; echo "${NODE_IDS[*]}")"
all_node_hosts_csv="$(IFS=,; echo "${NODE_HOSTS[*]}")"
echo "==> [$node_id@$node_host] start"
copy_raftadmin_to_remote "$node_id" "$ssh_target"
ssh "${SSH_BASE_OPTS[@]}" "$ssh_target" \
env \
IMAGE="$IMAGE_Q" \
RAFTADMIN_BIN_PATH="$RAFTADMIN_REMOTE_BIN_Q" \
CONTAINER_NAME="$CONTAINER_NAME_Q" \
DATA_DIR="$DATA_DIR_Q" \
SERVER_ENTRYPOINT="$SERVER_ENTRYPOINT_Q" \
RAFT_ENGINE="$RAFT_ENGINE" \
RAFT_PORT="$RAFT_PORT" \
REDIS_PORT="$REDIS_PORT" \
DYNAMO_PORT="$DYNAMO_PORT" \
S3_PORT="$S3_PORT" \
ENABLE_S3="$ENABLE_S3" \
S3_REGION="$S3_REGION" \
S3_CREDENTIALS_FILE="$S3_CREDENTIALS_FILE_Q" \
S3_PATH_STYLE_ONLY="$S3_PATH_STYLE_ONLY" \
ENABLE_SQS="$ENABLE_SQS" \
SQS_PORT="$SQS_PORT_Q" \
SQS_REGION="$SQS_REGION_Q" \
SQS_CREDENTIALS_FILE="$SQS_CREDENTIALS_FILE_Q" \
SQS_FIFO_PARTITION_MAP="$SQS_FIFO_PARTITION_MAP_Q" \
HEALTH_TIMEOUT_SECONDS="$HEALTH_TIMEOUT_SECONDS" \
LEADERSHIP_TRANSFER_TIMEOUT_SECONDS="$LEADERSHIP_TRANSFER_TIMEOUT_SECONDS" \
LEADERSHIP_TRANSFER_RETRY_ATTEMPTS="$LEADERSHIP_TRANSFER_RETRY_ATTEMPTS" \
LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS="$LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS" \
LEADER_DISCOVERY_TIMEOUT_SECONDS="$LEADER_DISCOVERY_TIMEOUT_SECONDS" \
RAFTADMIN_RPC_TIMEOUT_SECONDS="$RAFTADMIN_RPC_TIMEOUT_SECONDS" \
RAFTADMIN_ALLOW_INSECURE="$RAFTADMIN_ALLOW_INSECURE" \
NODE_ID="$node_id" \
NODE_HOST="$node_host" \
ALL_NODE_IDS_CSV="$all_node_ids_csv" \
ALL_NODE_HOSTS_CSV="$all_node_hosts_csv" \
RAFT_TO_REDIS_MAP="$RAFT_TO_REDIS_MAP_Q" \
RAFT_TO_S3_MAP="$RAFT_TO_S3_MAP_Q" \
RAFT_TO_SQS_MAP="$RAFT_TO_SQS_MAP_Q" \
EXTRA_ENV="$EXTRA_ENV_Q" \
CONTAINER_MEMORY_LIMIT="$CONTAINER_MEMORY_LIMIT_Q" \
ADMIN_ENABLED="$ADMIN_ENABLED" \
ADMIN_ADDRESS="$ADMIN_ADDRESS_Q" \
ADMIN_FULL_ACCESS_KEYS="$ADMIN_FULL_ACCESS_KEYS_Q" \
ADMIN_READ_ONLY_ACCESS_KEYS="$ADMIN_READ_ONLY_ACCESS_KEYS_Q" \
ADMIN_SESSION_SIGNING_KEY_FILE="$ADMIN_SESSION_SIGNING_KEY_FILE_Q" \
ADMIN_SESSION_SIGNING_KEY_PREVIOUS_FILE="$ADMIN_SESSION_SIGNING_KEY_PREVIOUS_FILE_Q" \
ADMIN_TLS_CERT_FILE="$ADMIN_TLS_CERT_FILE_Q" \
ADMIN_TLS_KEY_FILE="$ADMIN_TLS_KEY_FILE_Q" \
ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK="$ADMIN_ALLOW_PLAINTEXT_NON_LOOPBACK" \
ADMIN_ALLOW_INSECURE_DEV_COOKIE="$ADMIN_ALLOW_INSECURE_DEV_COOKIE" \
KEYVIZ_ENABLED="$KEYVIZ_ENABLED" \
KEYVIZ_FANOUT_NODES="$KEYVIZ_FANOUT_NODES_Q" \
KEYVIZ_HOT_KEYS_ENABLED="$KEYVIZ_HOT_KEYS_ENABLED" \
KEYVIZ_HOT_KEYS_PER_ROUTE="$KEYVIZ_HOT_KEYS_PER_ROUTE_Q" \
KEYVIZ_HOT_KEYS_SAMPLE_RATE="$KEYVIZ_HOT_KEYS_SAMPLE_RATE_Q" \
KEYVIZ_HOT_KEYS_QUEUE_SIZE="$KEYVIZ_HOT_KEYS_QUEUE_SIZE_Q" \
KEYVIZ_HOT_KEYS_MAX_KEY_LEN="$KEYVIZ_HOT_KEYS_MAX_KEY_LEN_Q" \
bash -s <<'REMOTE'
set -euo pipefail
IFS=, read -r -a ALL_NODE_IDS <<< "$ALL_NODE_IDS_CSV"
IFS=, read -r -a ALL_NODE_HOSTS <<< "$ALL_NODE_HOSTS_CSV"
grpc_healthy() {
bash -lc "exec 3<>/dev/tcp/${NODE_HOST}/${RAFT_PORT}" 2>/dev/null
}
peer_grpc_healthy() {
local peer_host="$1"
bash -lc "exec 3<>/dev/tcp/${peer_host}/${RAFT_PORT}" 2>/dev/null
}
wait_for_grpc() {
local i
for ((i = 0; i < HEALTH_TIMEOUT_SECONDS; i++)); do
if grpc_healthy; then
return 0
fi
sleep 1
done
return 1
}
extract_proto_string() {
local field="$1"
local payload="$2"
printf '%s' "$payload" |
sed -nE "s/.*${field}:[[:space:]]+\"([^\"]*)\".*/\1/p" |
tail -n1
}
extract_proto_enum() {
local field="$1"
local payload="$2"
printf '%s' "$payload" |
sed -nE "s/.*${field}:[[:space:]]+([A-Z_]+).*/\1/p" |
tail -n1
}
raftadmin_text() {
local addr="$1"
shift
if command -v timeout >/dev/null 2>&1; then
timeout "${RAFTADMIN_RPC_TIMEOUT_SECONDS}s" "$RAFTADMIN_BIN_PATH" "$addr" "$@" 2>&1
return $?
fi
"$RAFTADMIN_BIN_PATH" "$addr" "$@" 2>&1
}
raft_leader_addr() {
local addr="$1"
local output
output="$(raftadmin_text "$addr" leader)" || return 1
extract_proto_string "address" "$output"
}
raft_state() {
local addr="$1"
local output
local state
output="$(raftadmin_text "$addr" state)" || return 1
state="$(extract_proto_enum "state" "$output")"
if [[ -z "$state" ]]; then
printf '%s\n' "FOLLOWER"
return 0
fi
printf '%s\n' "$state"
}
cluster_leader_addr() {
local i addr state leader
for i in "${!ALL_NODE_HOSTS[@]}"; do
addr="${ALL_NODE_HOSTS[$i]}:${RAFT_PORT}"
state="$(raft_state "$addr" || true)"
if [[ "$state" == "LEADER" ]]; then
printf '%s\n' "$addr"
return 0
fi
done
for i in "${!ALL_NODE_HOSTS[@]}"; do
addr="${ALL_NODE_HOSTS[$i]}:${RAFT_PORT}"
leader="$(raft_leader_addr "$addr" || true)"
if [[ -n "$leader" ]]; then
printf '%s\n' "$leader"
return 0
fi
done
return 1
}
wait_for_cluster_leader() {
local i leader
for ((i = 0; i < LEADER_DISCOVERY_TIMEOUT_SECONDS; i++)); do
leader="$(cluster_leader_addr || true)"
if [[ -n "$leader" ]]; then
printf '%s\n' "$leader"
return 0
fi
sleep 1
done
return 1
}
cluster_reachability_summary() {
local i addr summary reachable state
summary=()
for i in "${!ALL_NODE_HOSTS[@]}"; do
addr="${ALL_NODE_HOSTS[$i]}:${RAFT_PORT}"
if peer_grpc_healthy "${ALL_NODE_HOSTS[$i]}"; then
reachable="up"
state="$(raft_state "$addr" || echo unknown)"
else
reachable="down"
state="unreachable"
fi
summary+=("${ALL_NODE_IDS[$i]}=${addr}:${reachable}:${state}")
done
printf '%s\n' "${summary[*]}"
}
choose_transfer_candidate() {
local i
for i in "${!ALL_NODE_IDS[@]}"; do
if [[ "${ALL_NODE_IDS[$i]}" == "$NODE_ID" ]]; then
continue
fi
if peer_grpc_healthy "${ALL_NODE_HOSTS[$i]}"; then
printf '%s %s\n' "${ALL_NODE_IDS[$i]}" "${ALL_NODE_HOSTS[$i]}"
return 0
fi
done
return 1
}
wait_for_leader_change() {
local old_leader="$1"
local expected_leader="${2:-}"
local i leader
for ((i = 0; i < LEADERSHIP_TRANSFER_TIMEOUT_SECONDS; i++)); do
leader="$(cluster_leader_addr || true)"
if [[ -n "$leader" && "$leader" != "$old_leader" ]]; then
if [[ -n "$expected_leader" && "$leader" != "$expected_leader" ]]; then
echo "leadership moved away from $old_leader, but elected $leader instead of preferred $expected_leader"
else
echo "leadership moved from $old_leader to $leader"
fi
return 0
fi
sleep 1
done
return 1
}
ensure_not_leader_before_restart() {
local current_leader candidate_id candidate_host candidate_addr rpc_output local_state
current_leader="$(wait_for_cluster_leader || true)"
if [[ -z "$current_leader" ]]; then
local_state="$(raft_state "${NODE_HOST}:${RAFT_PORT}" || echo unknown)"
echo "unable to determine current cluster leader within ${LEADER_DISCOVERY_TIMEOUT_SECONDS}s; refusing to restart $NODE_ID safely" >&2
echo "local raft state on ${NODE_HOST}:${RAFT_PORT}: ${local_state}" >&2
echo "cluster reachability: $(cluster_reachability_summary)" >&2
return 1
fi
if [[ "$current_leader" != "${NODE_HOST}:${RAFT_PORT}" ]]; then
echo "node is not leader ($current_leader); safe to restart"
return 0
fi
if ! grpc_healthy; then
echo "node is current leader but its local gRPC endpoint is unreachable; refusing restart" >&2
return 1
fi
if ! read -r candidate_id candidate_host < <(choose_transfer_candidate); then
echo "node is leader but no healthy peer is available as transfer target" >&2
return 1
fi
candidate_addr="${candidate_host}:${RAFT_PORT}"
echo "node is leader; transferring leadership to ${candidate_id}@${candidate_addr}"
# Retry the targeted transfer up to LEADERSHIP_TRANSFER_RETRY_ATTEMPTS
# times. A common failure shape under rolling restarts is etcd raft
# rejecting the transfer with "etcd raft leadership transfer aborted"
# when the candidate's log has not yet caught up to the leader. The
# candidate typically becomes ready within a few seconds, so a brief
# backoff between attempts is usually enough. Only when ALL targeted
# retries are exhausted do we fall back to the generic transfer.
local attempt=1 transfer_succeeded=false
while (( attempt <= LEADERSHIP_TRANSFER_RETRY_ATTEMPTS )); do
if rpc_output="$(raftadmin_text "${NODE_HOST}:${RAFT_PORT}" leadership_transfer_to_server "${candidate_id}" "${candidate_addr}")"; then
transfer_succeeded=true
break
fi
echo "targeted leadership transfer attempt ${attempt}/${LEADERSHIP_TRANSFER_RETRY_ATTEMPTS} failed: $rpc_output" >&2
if (( attempt < LEADERSHIP_TRANSFER_RETRY_ATTEMPTS )); then
echo "retrying in ${LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS}s..." >&2
sleep "${LEADERSHIP_TRANSFER_RETRY_BACKOFF_SECONDS}"
fi
attempt=$(( attempt + 1 ))
done
if [[ "$transfer_succeeded" != "true" ]]; then
echo "falling back to generic leadership transfer"
rpc_output="$(raftadmin_text "${NODE_HOST}:${RAFT_PORT}" leadership_transfer)" || {
echo "generic leadership transfer RPC failed: $rpc_output" >&2
return 1
}
candidate_addr=""
fi
if ! wait_for_leader_change "${NODE_HOST}:${RAFT_PORT}" "$candidate_addr"; then
echo "leadership did not move away from ${NODE_HOST}:${RAFT_PORT} within ${LEADERSHIP_TRANSFER_TIMEOUT_SECONDS}s" >&2
return 1
fi
return 0
}
stop_container() {
docker rm -f "$CONTAINER_NAME" >/dev/null 2>&1 || true
}
run_container() {
local s3_creds_volume=()
local s3_creds_flag=()
local s3_flags=()
if [[ "${ENABLE_S3}" == "true" && -n "${S3_CREDENTIALS_FILE:-}" ]]; then
if [[ ! -f "$S3_CREDENTIALS_FILE" || ! -r "$S3_CREDENTIALS_FILE" ]]; then
echo "S3_CREDENTIALS_FILE is set to '$S3_CREDENTIALS_FILE' but the file is missing or not readable; aborting docker run" >&2
exit 1
fi
s3_creds_volume=(-v "${S3_CREDENTIALS_FILE}:${S3_CREDENTIALS_FILE}:ro")
s3_creds_flag=(--s3CredentialsFile "$S3_CREDENTIALS_FILE")
fi
if [[ "${ENABLE_S3}" == "true" ]]; then
s3_flags=(
--s3Address "${NODE_HOST}:${S3_PORT}"
--s3Region "$S3_REGION"
--s3PathStyleOnly="$S3_PATH_STYLE_ONLY"
--raftS3Map "$RAFT_TO_S3_MAP"
"${s3_creds_flag[@]}"
)
fi
# SQS adapter wiring mirrors S3: optional credentials file gets
# bind-mounted ro at the same host path; the rest of the SQS knobs
# are passed verbatim. ENABLE_SQS=false leaves all arrays empty so
# the docker run is byte-identical to a non-SQS deploy.
local sqs_creds_volume=()
local sqs_creds_flag=()
local sqs_flags=()
if [[ "${ENABLE_SQS}" == "true" && -n "${SQS_CREDENTIALS_FILE:-}" ]]; then
if [[ ! -f "$SQS_CREDENTIALS_FILE" || ! -r "$SQS_CREDENTIALS_FILE" ]]; then
echo "SQS_CREDENTIALS_FILE is set to '$SQS_CREDENTIALS_FILE' but the file is missing or not readable; aborting docker run" >&2
exit 1
fi
sqs_creds_volume=(-v "${SQS_CREDENTIALS_FILE}:${SQS_CREDENTIALS_FILE}:ro")
sqs_creds_flag=(--sqsCredentialsFile "$SQS_CREDENTIALS_FILE")
fi
if [[ "${ENABLE_SQS}" == "true" ]]; then
sqs_flags=(
--sqsAddress "${NODE_HOST}:${SQS_PORT}"
--sqsRegion "$SQS_REGION"
--raftSqsMap "$RAFT_TO_SQS_MAP"
"${sqs_creds_flag[@]}"
)
if [[ -n "${SQS_FIFO_PARTITION_MAP:-}" ]]; then
sqs_flags+=(--sqsFifoPartitionMap "$SQS_FIFO_PARTITION_MAP")
fi
fi
# Pass through additional container environment variables from EXTRA_ENV.
# Accepts a whitespace-separated list of KEY=VALUE pairs, e.g.:
# EXTRA_ENV="ELASTICKV_RAFT_DISPATCHER_LANES=1 ELASTICKV_PEBBLE_CACHE_MB=512"
# Each pair is forwarded as a single `-e KEY=VALUE` flag so VALUE may contain
# characters that bash would otherwise interpret; pairs themselves must not
# contain whitespace.
local extra_env_flags=()
if [[ -n "${EXTRA_ENV:-}" ]]; then
# Split on whitespace without triggering filename globbing. Normalise
# newlines to spaces first so multi-line EXTRA_ENV values (common in
# long deploy.env overrides) are fully parsed — a here-string consumes
# only up to the first newline, so without this step subsequent lines
# would be silently dropped.
local -a extra_env_pairs=()
local extra_env_normalised="${EXTRA_ENV//$'\n'/ }"
# Explicit IFS so splitting is immune to any earlier mutation. Default
# whitespace is already space/tab/newline; pinning it here makes the
# behaviour self-documenting.
IFS=$' \t\n' read -r -a extra_env_pairs <<< "${extra_env_normalised}"
local pair
for pair in "${extra_env_pairs[@]}"; do
if [[ "$pair" != *=* || "$pair" == =* ]]; then
echo "invalid EXTRA_ENV entry '$pair'; expected KEY=VALUE" >&2
exit 1
fi
local key="${pair%%=*}"
if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
echo "invalid EXTRA_ENV key '$key' in entry '$pair'; key must match [A-Za-z_][A-Za-z0-9_]*" >&2
exit 1
fi
extra_env_flags+=(-e "$pair")
done
fi
# Optional hard container-scoped memory limit. Keeps any OOM kill contained
# to the elastickv container rather than cascading to host processes
# (e.g. qemu-guest-agent, systemd). Pair with GOMEMLIMIT via EXTRA_ENV so
# the Go runtime GCs before the kernel kills the container.
local memory_flags=()
if [[ -n "${CONTAINER_MEMORY_LIMIT:-}" ]]; then
memory_flags=(--memory "$CONTAINER_MEMORY_LIMIT")
fi
# Admin dashboard plumbing. The admin listener is opt-in; when
# ADMIN_ENABLED=false the build_admin_flags helper produces no
# arguments and no extra bind-mounts, so existing deployments keep
# behaving identically. When enabled, the helper also validates that