Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1268 lines (1223 loc) · 74.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1268 lines (1223 loc) · 74.1 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
# install.sh — interactive installer for source-truth (customer AWS account, multi-project).
#
# A friendly front-end over deploy-all.sh / deploy_project.sh. After a dependency + AWS-identity
# check it shows an arrow-key MENU of four flows:
# • 初始化环境 / init environment only — provision the shared base host, no project
# (deploy-all.sh --skip-projects). Lets you stand up AWS first, configure git later.
# • 添加项目 / add a project (DEFAULT) — collect projectId + git repos + bridge port + a Feishu
# app, auto-create its Secrets Manager secrets (feishu-<id>, and the global git credential on
# first run), write the .local/projects.json entry, create the base if missing, then deploy
# that project and smoke-test it with real questions. Glossary + monitoring are opt-in (asked).
# • 重新部署现有项目 / redeploy — pick a declared project and re-run deploy_project.sh.
# • 删除项目 / remove a project — destructive, double-confirmed; tears down its units/runtime/
# repo copies + removes it from projects.json (keeps secrets by default; never the global
# git credential).
#
# Code repos live in .local/projects.json — never on the CLI. Each repo is either a git source
# (cloned + auto-pulled) or a local source (pushed via push-local-repo.sh); add-project asks which.
# The single read-only git credential (R-cred-1) is shared across git-source repos. Re-runs pre-fill
# region/spec from .local/deploy-config.
#
# Non-interactive: --yes accepts pre-filled/default answers; flows needing human-only input
# (first Feishu/git secret) still hard-stop.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
source "$SCRIPT_DIR/lib/common.sh"
source "$SCRIPT_DIR/lib/env-utils.sh"
CONFIG_FILE="$ROOT/.local/deploy-config"
ASSUME_YES=false
LOCAL_MODE=false
LOCAL_FLAG=() # forwarded to deploy-all.sh: (--local) in single-host mode, else empty
DOMAIN_FLAG=() # forwarded to deploy-all.sh: (--feishu-domain lark) for an international tenant
LOCALE_FLAG=() # forwarded to deploy-all.sh: (--locale en) for English cards
GMF_FLAG=() # forwarded to deploy-all.sh: (--glossary-max-files N) cost cap
GLOS_FLAG=() # (--with-glossary) — skips the "enable glossary?" question
MON_FLAG=() # (--with-monitoring) — skips the "enable monitoring?" question
PROBE_FLAG=() # (--no-probe) — also exported as NO_PROBE=1 for the direct deploy_project.sh execs
REGION_PREFILL="" # pre-fills the region prompt instead of being silently dropped
ACTION="" # --action init|add|redeploy|remove : pick the flow without the menu (unattended re-runs)
PROJECT_PREFILL="" # --project <pid> : the project for redeploy/remove (else --yes takes the sole one)
# _imds_region / _is_index_host : is THIS machine the source-truth index host? (IMDSv2). Used to
# auto-enter single-host mode — see the LOCAL_MODE auto-detect below.
# The timeouts are load-bearing, not defensive dressing: this runs before the banner, so on a
# laptop behind a VPN, in a container, or on any network that BLACKHOLES link-local instead of
# refusing it, an untimed curl means the first thing a first-time user sees is a silent terminal
# with no output at all and no idea whether the installer is working.
_imds_get() { # _imds_get <metadata-path>
local tok
tok="$(curl -fsS --connect-timeout 1 --max-time 2 -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60" 2>/dev/null || true)"
curl -fsS --connect-timeout 1 --max-time 2 ${tok:+-H "X-aws-ec2-metadata-token: $tok"} "http://169.254.169.254/latest/meta-data/$1" 2>/dev/null || true
}
_imds_region() { _imds_get "placement/region"; }
_is_index_host() { [[ "$(_imds_get "iam/security-credentials/")" == *source-truth-index* ]]; }
# Arg parsing takes VALUES, so it cannot be a `for a in "$@"` loop. It used to be one, with no
# default case — which meant every flag this script does not itself implement was silently
# discarded. The costly one was --feishu-domain: README tells an international-Lark operator to
# pass it to the documented entry point (this script, via get.sh), the token was dropped on the
# floor, deploy-all defaulted to feishu AND PERSISTED it, and the operator got precisely the
# never-receives-events failure the README warned them about while having done as instructed.
# --region and --glossary-max-files were swallowed the same way, the latter meaning an uncapped
# (potentially hundreds of dollars) glossary build from a command that looked like it capped it.
# Flag-value validators. All four value-taking flags were added without any of this, and the gaps
# were not cosmetic:
# * a flag as the LAST argument shifted twice and underflowed; under `set -euo pipefail` bash
# exited 1 with NO OUTPUT AT ALL — on the documented `curl | bash -s --` entry point.
# * `--region --local` took the next FLAG as its value, so the region became "--local" AND the
# requested single-host topology was silently dropped.
# * `--glossary-max-files abc` / `=` reached deploy-all unvalidated and resolved to 0 = UNCAPPED,
# reintroducing the exact "a command that looked like it capped it" failure the flag was added
# to prevent.
# * the tenant was compared with a bare `== "lark"` here while every downstream layer case-folds,
# so `--feishu-domain Lark` probed valid international credentials against open.feishu.cn and
# told the operator their app was not in the Lark tenant — which is what they had got right.
_need_val() { # _need_val <flag> <remaining-argc>
[[ "$2" -ge 2 ]] || { printf 'flag %s needs a value\n' "$1" >&2; exit 2; }
}
_reject_flaglike() { case "$1" in -?*) printf 'flag %s: %s looks like another flag, not a value\n' "$2" "$1" >&2; exit 2 ;; esac; }
_norm_tenant() {
_reject_flaglike "$1" --feishu-domain
local v; v="$(printf '%s' "$1" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')"
case "$v" in feishu|lark) printf '%s' "$v" ;;
*) printf -- "--feishu-domain must be 'feishu' or 'lark', got '%s'\n" "$1" >&2; exit 2 ;; esac
}
_norm_locale() {
_reject_flaglike "$1" --locale
local v; v="$(printf '%s' "$1" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')"
case "$v" in zh|en) printf '%s' "$v" ;;
*) printf -- "--locale must be 'zh' or 'en', got '%s'\n" "$1" >&2; exit 2 ;; esac
}
_norm_region() {
_reject_flaglike "$1" --region
[[ "$1" =~ ^[a-z]{2}(-[a-z]+)+-[0-9]+$ ]] || { printf -- "--region '%s' is not an AWS region code\n" "$1" >&2; exit 2; }
printf '%s' "$1"
}
_norm_bool() { # _norm_bool <value> <flag> -> true|false
case "$1" in
true|yes|on|1) printf 'true' ;;
false|no|off|0) printf 'false' ;;
*) printf '%s must be true or false, got %s\n' "$2" "$1" >&2; exit 2 ;;
esac
}
_norm_action() { # --action <init|add|redeploy|remove>
_reject_flaglike "$1" --action
local v; v="$(printf '%s' "$1" | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]')"
case "$v" in init|add|redeploy|remove) printf '%s' "$v" ;;
*) printf -- "--action must be one of init|add|redeploy|remove, got '%s'\n" "$1" >&2; exit 2 ;; esac
}
_norm_pid() { # --project <projectId> (same charset as add-project)
_reject_flaglike "$1" --project
[[ "$1" =~ ^[a-z0-9][a-z0-9-]*$ ]] || { printf -- "--project '%s' is not a valid projectId\n" "$1" >&2; exit 2; }
printf '%s' "$1"
}
_norm_count() {
_reject_flaglike "$1" --glossary-max-files
[[ "$1" =~ ^[0-9]+$ ]] || { printf -- "--glossary-max-files must be a non-negative integer (0 = uncapped), got '%s'\n" "$1" >&2; exit 2; }
printf '%s' "$1"
}
while [[ $# -gt 0 ]]; do
a="$1"
case "$a" in
-y|--yes) ASSUME_YES=true ;;
--local) LOCAL_MODE=true; LOCAL_FLAG=(--local) ;;
--feishu-domain) _need_val "$a" $#; DOMAIN_FLAG=(--feishu-domain "$(_norm_tenant "$2")"); shift ;;
--feishu-domain=*) DOMAIN_FLAG=(--feishu-domain "$(_norm_tenant "${a#*=}")") ;;
--locale) _need_val "$a" $#; LOCALE_FLAG=(--locale "$(_norm_locale "$2")"); shift ;;
--locale=*) LOCALE_FLAG=(--locale "$(_norm_locale "${a#*=}")") ;;
--region) _need_val "$a" $#; REGION_PREFILL="$(_norm_region "$2")"; shift ;;
--region=*) REGION_PREFILL="$(_norm_region "${a#*=}")" ;;
--glossary-max-files) _need_val "$a" $#; GMF_FLAG=(--glossary-max-files "$(_norm_count "$2")"); shift ;;
--glossary-max-files=*) GMF_FLAG=(--glossary-max-files "$(_norm_count "${a#*=}")") ;;
--with-glossary) GLOS_FLAG=(--with-glossary) ;;
--with-glossary=*) GLOS_FLAG=(--with-glossary="$(_norm_bool "${a#*=}" --with-glossary)") ;;
--with-monitoring) MON_FLAG=(--with-monitoring) ;;
--with-monitoring=*) MON_FLAG=(--with-monitoring="$(_norm_bool "${a#*=}" --with-monitoring)") ;;
--no-probe) PROBE_FLAG=(--no-probe); export NO_PROBE=1 ;;
--action) _need_val "$a" $#; ACTION="$(_norm_action "$2")"; shift ;;
--action=*) ACTION="$(_norm_action "${a#*=}")" ;;
--project) _need_val "$a" $#; PROJECT_PREFILL="$(_norm_pid "$2")"; shift ;;
--project=*) PROJECT_PREFILL="$(_norm_pid "${a#*=}")" ;;
-h|--help)
cat <<EOF
Usage: ./scripts/install.sh [--yes] [--action <init|add|redeploy|remove>] [--project <pid>]
[--local] [--feishu-domain <feishu|lark>]
[--locale <zh|en>] [--region <aws-region>]
[--with-glossary[=true|false]] [--glossary-max-files <n>]
[--with-monitoring[=true|false]] [--no-probe]
Interactive installer. Shows an arrow-key menu: init environment / add a project /
redeploy a project / remove a project. Code repos (git or local source) live in
.local/projects.json; per-project Feishu + the shared git credential are created in
Secrets Manager. Re-runs pre-fill region/spec from .local/deploy-config.
--yes Accept all pre-filled/default answers without prompting (headless). A FIRST
"add a project" needs a projectId + secrets nobody can default, so it stops
early with exit 2 — use the scripted path (runbook appendix A) instead.
Once a base host exists, --yes needs --action to say which flow to run
(the menu default "add a project" cannot run unattended) — else exit 2.
--action <init|add|redeploy|remove>
Run that flow directly, no menu (also --action=<name>). Unattended re-runs:
--yes --action redeploy [--project <pid>] (redeploy; sole project auto-picked)
--yes --action init --region <r> (base host only)
'remove' still asks you to type the projectId, so it is interactive-only.
--project <pid>
The project for redeploy / remove. Under --yes with several projects it is
required; with exactly one project it may be omitted.
--local Single-host mode: deploy onto THIS EC2 (reuse its VPC/role), don't
create a separate index host. Forwarded to deploy-all.sh. Auto-enabled
when run ON the index host, so re-runs (add-project / redeploy) don't
need it. Normally set for you by scripts/lib/prepare-local-host.sh /
scripts/launch-host.sh.
--feishu-domain <feishu|lark>
Tenant domain. 'feishu' = 飞书 / China (open.feishu.cn), 'lark' =
international Lark (open.larksuite.com). MUST match the console the
app was created in — a mismatch authenticates and then never
receives a single event. Forwarded to deploy-all.sh.
--locale <zh|en>
Card / message language. Defaults to 'en' when --feishu-domain is
'lark', otherwise 'zh'. Forwarded to deploy-all.sh.
--region <aws-region>
Pre-fills the region prompt (still confirmable when interactive).
--with-glossary[=true|false]
Enable the 中文→code-symbol glossary (default OFF; the flows ask
otherwise, default no). Its build costs model calls. Forwarded;
=false turns it off explicitly (redeploy persists it too). Flipping the
switch re-bootstraps the index host in place (minutes of bot downtime).
--glossary-max-files <n>
Cap the glossary build when it is enabled (default 400). 0 = uncapped;
on a very large repo that can cost hundreds of dollars. Forwarded.
--with-monitoring[=true|false]
Enable CloudWatch monitoring (default OFF; the flows ask otherwise,
default no). Forwarded; deploy-all persists the choice; add-project /
redeploy apply it after the project is up. =false turns it off.
An existing deploy-config WITHOUT the DEPLOY_WITH_* keys predates
these switches: glossary/monitoring default to ON there (warned).
--no-probe
Skip the post-deploy smoke (1 code question through the runtime).
EOF
exit 0 ;;
*) printf 'unknown flag: %s (see --help)\n' "$a" >&2; exit 2 ;;
esac
shift
done
# Auto-enter single-host mode when running ON the index host itself, even without --local. Operators
# re-run install by hand there for add-project / redeploy; without this the base-deploy would take
# the two-machine path (create VPC/NAT, ReplaceRoute) under the instance role and fail — and it must
# reuse this box, not build a second network. A plain operator laptop isn't the index host, so the
# two-machine path is unaffected.
if [[ "$LOCAL_MODE" != true ]] && _is_index_host; then
LOCAL_MODE=true; LOCAL_FLAG=(--local)
say info "检测到本机即索引主机 —— 自动进入单机模式(--local)"
fi
# ---- tiny prompt helpers -------------------------------------------------------
# ask <var> <prompt> <default> : read a value, showing the default in [brackets];
# empty input keeps the default. In --yes mode, takes the default with no prompt.
ask() {
local __var="$1" __prompt="$2" __default="${3:-}" __reply
if [[ "$ASSUME_YES" == true ]]; then
printf -v "$__var" '%s' "$__default"; return
fi
if [[ -n "$__default" ]]; then
read -rp "$(printf '%s [%s]: ' "$__prompt" "$__default")" __reply || true
printf -v "$__var" '%s' "${__reply:-$__default}"
else
read -rp "$(printf '%s: ' "$__prompt")" __reply || true
printf -v "$__var" '%s' "$__reply"
fi
}
# ask_secret <var> <prompt> : read a secret, echoing one '*' per char (the real
# value never appears on screen, but the operator gets typed feedback + length).
# Handles backspace/DEL. Falls back to silent read where there's no TTY (CI).
ask_secret() {
local __var="$1" __prompt="$2" __reply="" __ch
printf '%s: ' "$__prompt" >&2
if [[ ! -t 0 ]]; then # no interactive stdin (CI / piped): silent read
read -r __reply || true
printf -v "$__var" '%s' "$__reply"; return
fi
# Read char-by-char; mask with '*'. IFS= + -N1 keeps spaces; -r keeps backslashes.
while IFS= read -rsN1 __ch; do
[[ -z "$__ch" || "$__ch" == $'\n' || "$__ch" == $'\r' ]] && break
if [[ "$__ch" == $'\177' || "$__ch" == $'\b' ]]; then # backspace / DEL
if [[ -n "$__reply" ]]; then __reply="${__reply%?}"; printf '\b \b' >&2; fi
continue
fi
__reply+="$__ch"; printf '*' >&2
done
printf '\n' >&2
printf -v "$__var" '%s' "$__reply"
}
# ask_valid <var> <prompt> <regex> <errmsg> [allow_empty] : ask until the reply
# matches <regex> (or is empty when allow_empty=1). Keeps the value the operator
# already typed in scope — re-prompts only this field, not the whole flow.
ask_valid() {
local __var="$1" __prompt="$2" __re="$3" __err="$4" __empty="${5:-}" __val
while true; do
ask __val "$__prompt" ""
if [[ -z "$__val" && -n "$__empty" ]]; then printf -v "$__var" '%s' ""; return; fi
if [[ "$__val" =~ $__re ]]; then printf -v "$__var" '%s' "$__val"; return; fi
say warn "$__err"
# Headless runs cannot answer a re-prompt. `ask` returns the (empty) default instantly under
# --yes, which never matches a required pattern, so this loop spun forever printing the same
# warning — a documented unattended path that hung instead of failing. Fail loudly instead.
if [[ "$ASSUME_YES" == true ]]; then
say err "--yes 模式下无法重新询问「${__prompt}」/ cannot re-prompt under --yes; supply this value non-interactively or run interactively"
exit 1
fi
# A closed stdin (a pipe that ended) cannot answer either, and would spin identically.
[[ -t 0 ]] || { say err "stdin 非交互且取值无效「${__prompt}」/ non-interactive stdin with no valid value"; exit 1; }
done
}
# confirm <prompt> : y/N. --yes mode auto-confirms.
confirm() {
[[ "$ASSUME_YES" == true ]] && return 0
local __reply
read -rp "$(printf '%s [y/N]: ' "$1")" __reply || true
[[ "$__reply" =~ ^[Yy] ]]
}
# ask_yn <prompt> <default y|n> : y/n with an explicit default (shown as [Y/n] or [y/N]); empty
# input takes the default. --yes takes the default too (unlike confirm, which auto-accepts).
ask_yn() {
local __d="${2:-n}" __reply
[[ "$ASSUME_YES" == true ]] && { [[ "$__d" == y ]]; return; }
if [[ "$__d" == y ]]; then
read -rp "$(printf '%s [Y/n]: ' "$1")" __reply || true
[[ ! "$__reply" =~ ^[Nn] ]]
else
read -rp "$(printf '%s [y/N]: ' "$1")" __reply || true
[[ "$__reply" =~ ^[Yy] ]]
fi
}
# Sentinel for the last menu entry: fall through to a free-text prompt for a value
# not in the list (region/model can be anything; the menu just covers common ones).
MANUAL_SENTINEL="↳ 手动输入其他 / enter manually"
# pick <var> <default_index> <item...> : arrow-key menu (mirrors the reference
# installer's picker). ↑/↓ move, Enter selects; sets <var> to the chosen LABEL line.
# Falls back to the default item with no UI when headless (--yes) or no controlling
# TTY, so CI never blocks on a key read.
pick() {
local __var="$1" __def="${2:-0}"; shift 2
local -a __it=("$@")
local __n=${#__it[@]} __sel="$__def" __key __rest i
if [[ "$ASSUME_YES" == true || ! -e /dev/tty || ! -t 1 ]]; then
printf -v "$__var" '%s' "${__it[$__def]}"; return
fi
# Clamp via `if` (NOT `(( … )) && …`): under `set -e` a false (( )) as the last
# command of a branch aborts the whole script — the exact trap documented in
# deploy-all.sh's preflight_quota.
if (( __sel < 0 || __sel >= __n )); then __sel=0; fi
_pick_draw() {
for ((i = 0; i < __n; i++)); do
if (( i == __sel )); then
# highlight via common.sh's bg-aware cyan (bright on dark bg, standard on light) so the
# selected row stays readable on both themes; _C_CYAN is '' under NO_COLOR / non-TTY.
printf '%s ❯ %s%s\n' "$_C_CYAN" "${__it[$i]}" "$_C_RESET" >/dev/tty
else
printf ' %s\n' "${__it[$i]}" >/dev/tty
fi
done
}
printf '\033[?25l' >/dev/tty # hide cursor
# Restore the cursor on Ctrl-C and bail with the conventional 130, so an aborted
# install never leaves the terminal with a hidden cursor.
trap 'printf "\033[?25h" >/dev/tty; exit 130' INT
_pick_draw
while IFS= read -rsn1 __key </dev/tty; do
if [[ "$__key" == $'\x1b' ]]; then
read -rsn2 __rest </dev/tty || true
case "$__rest" in
'[A') __sel=$(( (__sel - 1 + __n) % __n )) ;; # assignment form always
'[B') __sel=$(( (__sel + 1) % __n )) ;; # returns 0 — set -e safe
*) continue ;;
esac
elif [[ -z "$__key" || "$__key" == $'\n' ]]; then
break
else
continue
fi
printf '\033[%dA' "$__n" >/dev/tty # cursor back up to redraw in place
_pick_draw
done
printf '\033[?25h' >/dev/tty # show cursor
trap - INT
printf -v "$__var" '%s' "${__it[$__sel]}"
}
# index_of_token <token> <item...> : 0-based index of the first item whose leading
# whitespace-delimited token == <token>, else -1. Used to pre-select the persisted
# value when re-running (so an upgrade keeps the prior choice highlighted).
index_of_token() {
local want="$1"; shift
local i=0 it
for it in "$@"; do
if [[ "${it%%[[:space:]]*}" == "$want" ]]; then echo "$i"; return; fi
i=$((i + 1))
done
echo -1
}
# pick_field <outvar> <header> <persisted> <manual-prompt> <item...>
# Show <header>, then an arrow-key menu whose LAST item is MANUAL_SENTINEL. Sets
# <outvar> to the chosen line's leading token (e.g. region code / instance type),
# or to free text if the manual entry is chosen. Honors the --yes "keep persisted"
# contract: headless with a persisted value takes it verbatim (even if off-menu).
pick_field() {
local __out="$1" __header="$2" __persist="$3" __manual="$4"; shift 4
local -a __items=("$@")
if [[ "$ASSUME_YES" == true && -n "$__persist" ]]; then
printf -v "$__out" '%s' "$__persist"; return
fi
echo " $__header"
local __def
__def="$(index_of_token "$__persist" "${__items[@]}")"
if (( __def < 0 )); then __def=0; fi
local __chosen
pick __chosen "$__def" "${__items[@]}"
if [[ "$__chosen" == "$MANUAL_SENTINEL" ]]; then
ask "$__out" "$__manual" "$__persist"
else
printf -v "$__out" '%s' "${__chosen%%[[:space:]]*}"
fi
}
# --- menu option lists (label = leading token IS the value; trailing text is help) ---
REGION_OPTIONS=(
"ap-northeast-1 Tokyo 东京"
"ap-southeast-1 Singapore 新加坡"
"us-east-1 Virginia 弗吉尼亚"
"us-west-2 Oregon 俄勒冈"
"$MANUAL_SENTINEL"
)
# Bedrock model per project (written to projects.json's `model`; deploy_project resolves it to the
# region's actual inference profile, so a region-agnostic `global.…` id is the right thing to store
# — see resolve_model_for_region). Leading token IS the value; trailing text is help.
MODEL_OPTIONS=(
"global.anthropic.claude-opus-4-8 Opus 4.8 · 默认"
"global.anthropic.claude-opus-4-6 Opus 4.6"
"global.anthropic.claude-sonnet-4-6 Sonnet 4.6"
"$MANUAL_SENTINEL"
)
OPENAI_MODEL_OPTIONS=(
"global.openai.gpt-6-astra GPT-6 Astra · 默认"
"$MANUAL_SENTINEL"
)
SDK_OPTIONS=(
"openai OpenAI Agents SDK · 默认 / default"
"claude Claude Agent SDK"
)
# index-service host (ARM Graviton). codegraph indexing is memory-bound and scales
# with repo size; t4g = burstable/cheap, m7g = sustained memory-optimized for big repos.
INSTANCE_OPTIONS=(
"t4g.large 2 vCPU / 8 GiB 小中仓·默认 (small/medium repo, default)"
"t4g.xlarge 4 vCPU / 16 GiB 中大仓 (medium/large repo)"
"m7g.large 2 vCPU / 8 GiB 稳定性能 (steadier CPU)"
"m7g.xlarge 4 vCPU / 16 GiB 大仓·稳定 (large repo, steadier CPU)"
"m7g.2xlarge 8 vCPU / 32 GiB 超大仓/多仓 (very large, or several repos on one host)"
)
# Root gp3 volume: holds the repo copy + graph.db + staged tarball.
DISK_OPTIONS=(
"30 GiB 小中仓·默认 (small/medium repo, default)"
"50 GiB"
"100 GiB 大仓 (large repo)"
"200 GiB 超大仓/多仓 (very large, or several repos on one host)"
"$MANUAL_SENTINEL"
)
# Term-glossary build file cap (per repo). cc scans this many files to build the
# 中文→英文符号 map; higher = more coverage but more $ (a full scan of a large repo
# can run into the hundreds of USD, one-time). 0 = no cap (whole repo).
GLOSSARY_OPTIONS=(
"400 控成本·推荐首次部署 (bounded cost — recommended for a first deploy)"
"1000 更广覆盖 (wider coverage, higher one-off cost)"
"4000 大仓深覆盖 (deep coverage on a large repo, higher one-off cost)"
"0 不限·全量·扫每个文件 (NO CAP — scans every file; measured ~\$372 on a 14k-file repo)"
"$MANUAL_SENTINEL"
)
# ask_extras <base|project> : the opt-in extras. A flag wins (--with-x / --with-x=false);
# otherwise ask, with the default = the persisted choice from the last run. No persisted choice but
# a host already exists ⇒ the deploy predates the switches (they were always on) ⇒ default yes.
# Fills EXTRA_FLAGS for deploy-all.sh (which persists DEPLOY_WITH_GLOSSARY / DEPLOY_WITH_MONITORING)
# — the OFF answer is forwarded explicitly (=false) so deploy-all cannot re-apply the legacy default
# over it. Mode base (init-env) asks only the glossary: monitoring builds on a gateway's logs, so it
# is applied by add-project once a project exists. Also sets GLOSSARY_ON / MONITORING_ON.
EXTRA_FLAGS=(); GLOSSARY_ON=false; MONITORING_ON=false
optin_default() { # optin_default <persisted-value> -> y|n
if [[ "$1" == true ]]; then printf 'y'
elif [[ -z "$1" && -n "${INDEX_SERVICE_INSTANCE:-}" ]]; then printf 'y' # legacy: predates the switch
else printf 'n'; fi
}
flag_value() { # flag_value <flag-array-first-elem> -> true|false|"" (unset)
case "${1:-}" in "") printf '' ;; *=false) printf 'false' ;; *) printf 'true' ;; esac
}
# glossary_changed <switch true|false|""> <cap|""> : rc 0 when a GIVEN glossary flag differs from the
# persisted choice. The index host bakes glossary=<on/off>|gmf=<cap> into its artifact signature
# (provision_index_service.sh), so a change must go through deploy-all to re-bootstrap the host;
# deploy_project.sh alone never touches it. Absent key + host ⇒ legacy ON; absent cap ⇒ 400.
glossary_changed() {
local want="$1" cap="$2" prev="${DEPLOY_WITH_GLOSSARY:-}"
[[ -z "$prev" && -n "${INDEX_SERVICE_INSTANCE:-}" ]] && prev=true
[[ -n "$want" && "$want" != "${prev:-false}" ]] && return 0
[[ -n "$cap" && "$cap" != "${DEPLOY_GLOSSARY_MAX_FILES:-400}" ]] && return 0
return 1
}
# Record the unfinished host-wide rollout before provisioning changes global
# state. A failed/interrupted fan-out must remain retryable even after the new
# switch/cap has been persisted by deploy-all.
mark_glossary_pending() {
local pending
pending="$(project_ids)" || return 1
pending="${pending//$'\n'/ }"
update_env "$CONFIG_FILE" DEPLOY_GLOSSARY_PENDING_PROJECTS "$pending"
}
# Reconcile only pending projects; keep failed ones durable across installer
# invocations. rc 3 still means configuration was applied, so it is not retried.
reconcile_glossary_pending() {
local selected="$1" region="$2" selected_rc="$3" p prc current
local -a others=() failed=()
safe_source_env "$CONFIG_FILE"
current="$(project_ids)" || return 1
current=" ${current//$'\n'/ } "
for p in ${DEPLOY_GLOSSARY_PENDING_PROJECTS:-}; do
# The declaration is authoritative: a removed project has no rollout left
# to complete and must not poison every later redeploy.
[[ "$current" == *" $p "* ]] || continue
if [[ "$p" == "$selected" ]]; then
[[ "$selected_rc" == 0 || "$selected_rc" == 3 ]] || failed+=("$p")
else
others+=("$p")
fi
done
if [[ ${#others[@]} -gt 0 ]]; then
say info "术语表开关是主机级的,其余 ${#others[@]} 个项目也重新下发 / glossary switch is host-wide; redeploying the other ${#others[@]} projects: ${others[*]-}"
for p in ${others[@]+"${others[@]-}"}; do
say step "重新下发项目 / redeploying project $p (no smoke probe)"
prc=0
env NO_PROBE=1 ${DOMAIN_FLAG[1]:+FEISHU_DOMAIN="${DOMAIN_FLAG[1]}"} ${LOCALE_FLAG[1]:+LOCALE="${LOCALE_FLAG[1]}"} \
bash "$SCRIPT_DIR/lib/deploy_project.sh" "$region" "$p" || prc=$?
if [[ "$prc" != 0 && "$prc" != 3 ]]; then
failed+=("$p")
say warn "项目 $p 重新下发失败 / redeploy of $p failed — re-run: ./scripts/install.sh --action redeploy --project $p"
fi
done
fi
update_env "$CONFIG_FILE" DEPLOY_GLOSSARY_PENDING_PROJECTS "${failed[*]-}" || return 1
if [[ ${#failed[@]} -gt 0 ]]; then
say err "其余项目重新下发失败 / the other projects failed to redeploy: ${failed[*]-}"
return 1
fi
}
ask_extras() {
local mode="${1:-project}" def fv GLOSSARY_MAX_FILES
EXTRA_FLAGS=(); GLOSSARY_ON=false; MONITORING_ON=false
GLOSSARY_CAP="${DEPLOY_GLOSSARY_MAX_FILES:-400}"
def="$(optin_default "${DEPLOY_WITH_GLOSSARY:-}")"
fv="$(flag_value "${GLOS_FLAG[0]:-}")"
# the legacy-default warning is about the DEFAULT — an explicit flag makes it moot, so keep quiet then
[[ "$def" == y && -z "${DEPLOY_WITH_GLOSSARY:-}" && -z "$fv" ]] && say warn "现有部署早于开关,术语表默认保持开启 / existing deployment predates the switches — glossary defaults to ON (answer n or pass --with-glossary=false to turn off)"
if [[ -n "$fv" ]]; then
GLOSSARY_ON="$fv"; say info "术语表 / glossary: $([[ "$fv" == true ]] && echo 启用 || echo 关闭)(来自 ${GLOS_FLAG[0]})"
elif ask_yn "启用术语表(中文术语→代码符号映射;构建有模型费用)?/ enable the glossary (build costs model calls)?" "$def"; then
GLOSSARY_ON=true
fi
if [[ "$GLOSSARY_ON" == true ]]; then
EXTRA_FLAGS+=(--with-glossary)
if [[ ${#GMF_FLAG[@]} -gt 0 ]]; then
GLOSSARY_MAX_FILES="${GMF_FLAG[1]}"
say info "术语表构建文件上限 / glossary build cap: ${GLOSSARY_MAX_FILES}(来自 --glossary-max-files)"
else
pick_field GLOSSARY_MAX_FILES "术语表构建文件上限 (0=不限,可能数百美元) / glossary build cap (0 = uncapped, may cost hundreds of USD)" \
"${DEPLOY_GLOSSARY_MAX_FILES:-400}" "文件数 (0=不限) / file cap (0 = uncapped)" "${GLOSSARY_OPTIONS[@]}"
while ! [[ "$GLOSSARY_MAX_FILES" =~ ^[0-9]+$ ]]; do
[[ "$ASSUME_YES" == true ]] && { say err "术语表上限无效 / invalid glossary cap '$GLOSSARY_MAX_FILES'"; exit 1; }
say warn "需为非负整数 (0=不限) / must be a non-negative integer (0 = no cap)."
ask GLOSSARY_MAX_FILES "文件数 (0=不限)" "400"
done
fi
EXTRA_FLAGS+=(--glossary-max-files "$GLOSSARY_MAX_FILES")
GLOSSARY_CAP="$GLOSSARY_MAX_FILES"
else
EXTRA_FLAGS+=(--with-glossary=false)
say info "术语表 / glossary: 关闭(稍后可用 deploy-all.sh --with-glossary 开启)"
fi
if [[ "$mode" == base ]]; then
# init-env: no project → nothing to monitor yet; only forward an explicit flag (persisted).
[[ ${#MON_FLAG[@]} -gt 0 ]] && { EXTRA_FLAGS+=("${MON_FLAG[0]}"); MONITORING_ON="$(flag_value "${MON_FLAG[0]}")"; }
return 0
fi
def="$(optin_default "${DEPLOY_WITH_MONITORING:-}")"
fv="$(flag_value "${MON_FLAG[0]:-}")"
[[ "$def" == y && -z "${DEPLOY_WITH_MONITORING:-}" && -z "$fv" ]] && say warn "现有部署早于开关,监控默认保持开启 / existing deployment predates the switches — monitoring defaults to ON (answer n or pass --with-monitoring=false to turn off)"
if [[ -n "$fv" ]]; then
MONITORING_ON="$fv"; say info "监控 / monitoring: $([[ "$fv" == true ]] && echo 启用 || echo 关闭)(来自 ${MON_FLAG[0]})"
elif ask_yn "启用 CloudWatch 监控(仪表盘/告警/日活)?/ enable CloudWatch monitoring (dashboards, alarms, DAU)?" "$def"; then
MONITORING_ON=true
fi
if [[ "$MONITORING_ON" == true ]]; then
EXTRA_FLAGS+=(--with-monitoring)
else
EXTRA_FLAGS+=(--with-monitoring=false)
say info "监控 / monitoring: 关闭(稍后可跑 ./scripts/apply-monitoring.sh --region <r>)"
fi
}
# finish_project_deploy <rc> <pid> <region> <apply-monitoring true|false> : shared tail of the
# add-project / redeploy flows. deploy_project.sh exit codes: 0 = up and the smoke probe passed
# (or skipped); 3 = up, but the probe failed — the project IS deployed, so monitoring still applies
# and we say exactly that; anything else = deploy failed. Exits with deploy_project's rc.
finish_project_deploy() {
local rc="$1" pid="$2" region="$3" mon="$4"
if [[ ( "$rc" -eq 0 || "$rc" -eq 3 ) && "$mon" == true ]]; then
say step "应用监控 / applying CloudWatch monitoring (best-effort)"
bash "$SCRIPT_DIR/apply-monitoring.sh" --region "$region" \
|| say warn "监控部分阶段失败(不影响机器人)/ some monitoring stages failed — re-run ./scripts/apply-monitoring.sh --region $region"
fi
case "$rc" in
0) ;;
3) say warn "项目 $pid 已部署,但真实问答验收未通过 / project $pid deployed but did not answer the smoke questions — see runbook §5/§8 (docs/runbook_zh.md §五/§八)" ;;
*) say err "项目 $pid 部署失败 (rc $rc) / project $pid deploy failed — fix the cause above and re-run" ;;
esac
exit "$rc"
}
echo
say step "source-truth installer"
echo " 把项目最新主分支的真实代码,变成飞书里能问的 AI 助手。"
echo " This installer deploys the full backend + Feishu gateway into your AWS account."
echo
# ---- 1. dependency check -------------------------------------------------------
say step "检查依赖 / Checking dependencies"
DEPS_OK=true
for c in aws python3 docker git; do
if have_cmd "$c"; then
say ok "$c"
else
say err "$c 缺失 / missing"
DEPS_OK=false
fi
done
if [[ "$DEPS_OK" != true ]]; then
say err "请先安装缺失的依赖再重试 / install the missing tools and re-run."
say info " aws CLI v2, python3, docker (ARM64-capable buildx), git"
exit 1
fi
# zip is OPTIONAL: only the monitoring DAU-lambda stage needs it (apply-dau-lambda.sh packages
# the function with `zip`). Its absence is non-fatal — the deploy + bot work fine, only the 日活
# widget stays empty — so WARN, don't block. (--local's prepare-local-host.sh installs it.)
have_cmd zip || say info "zip 未安装 / zip absent — fine, but the monitoring 日活 widget stays empty until you install zip (用你系统的包管理器 / your OS package manager) and re-run: ./scripts/apply-monitoring.sh --only dau"
# (No docker-daemon liveness check here: deploy-all.sh's preflight_docker runs the same
# `docker info` probe up front — before any billable resource — so this would be a duplicate.)
# gh is OPTIONAL — only needed to auto-download codegraph-server from a PRIVATE repo's Release (gh
# carries auth). In --local the index host fetches the binary itself (S3 → Release) and prepare has
# already run `gh auth login`, so this hint is just noise there — skip it. Otherwise warn, don't block.
if [[ "$LOCAL_MODE" != true ]]; then
if have_cmd gh && gh auth status >/dev/null 2>&1; then
say ok "gh (authenticated — can fetch codegraph-server from a private Release)"
else
say info "gh 未安装或未登录 / gh absent or not logged in — fine if the repo is public or"
say info " codegraph-server is already local. For a PRIVATE repo's auto-download, run"
say info " 'gh auth login', or set CODEGRAPH_SERVER_BIN=/path/to/codegraph-server."
fi
fi
# AWS identity (also proves credentials work before we collect anything).
if ! ACCOUNT="$(aws sts get-caller-identity --query Account --output text 2>/dev/null)"; then
say err "AWS 凭证无效 / AWS credentials not working — run 'aws configure' or set AWS_PROFILE."
exit 1
fi
say ok "AWS account: $ACCOUNT"
# Pre-fill defaults from the last run, if any.
safe_source_env "$CONFIG_FILE"
PROJECTS_CFG="$ROOT/.local/projects.json"
# ask_region <var> : the region menu is shared by every flow (pre-selects persisted). In single-host
# mode (LOCAL_MODE — set explicitly or auto-detected up top) region is NOT a choice: we deploy onto
# THIS EC2, whose region is fixed. Read it from IMDS; asking would just invite the wrong pick (e.g.
# a stale Tokyo default while the box is in us-east-1). Otherwise (operator laptop) show the menu.
ask_region() {
# An explicit --region wins over everything, including the IMDS auto-detect: the operator named
# a region on the command line, and silently deploying somewhere else is worse than being wrong
# loudly. (Before, --region was swallowed entirely by the arg loop.)
if [[ -n "$REGION_PREFILL" ]]; then
require_deploy_region "${DEPLOY_REGION:-}" "$REGION_PREFILL" || exit 2
printf -v "$1" '%s' "$REGION_PREFILL"
say info "区域 / region: $REGION_PREFILL(来自 --region)"
return
fi
if [[ "$LOCAL_MODE" == true ]]; then
local imds_region; imds_region="$(_imds_region)"
if [[ -n "$imds_region" ]]; then
require_deploy_region "${DEPLOY_REGION:-}" "$imds_region" || exit 2
printf -v "$1" '%s' "$imds_region"
say info "区域 / region: $imds_region(本机所在区域,自动检测)"
return
fi
say warn "无法从实例元数据读取区域;回退到手动选择。"
fi
pick_field "$1" "AWS 区域 / region (↑/↓ 选择,回车确认)" \
"${DEPLOY_REGION:-ap-northeast-1}" "AWS 区域代码 / region code" "${REGION_OPTIONS[@]}"
require_deploy_region "${DEPLOY_REGION:-}" "${!1}" || exit 2
}
# project_ids : print existing projectIds from .local/projects.json, one per line (empty if none).
# FAIL-LOUD on a broken file: swallowing the parse error made remove/redeploy report the
# misleading "清单无项目 / no projects" instead of the real problem (hand-edited bad JSON).
project_ids() {
[[ -f "$PROJECTS_CFG" ]] || return 0
python3 -c 'import json,sys
try:
print("\n".join(json.load(open(sys.argv[1])).get("projects",{})))
except Exception as e:
sys.stderr.write(f"projects.json 解析失败 / failed to parse {sys.argv[1]}: {e}\n")
sys.exit(1)' "$PROJECTS_CFG"
}
# choose_project <var> <pid...> : which project to redeploy/remove. --project wins (must be in the
# list); under --yes with exactly one project take it, with several stop (exit 2) instead of
# silently picking index 0; otherwise the arrow-key menu.
choose_project() {
local __var="$1"; shift
local p
if [[ -n "$PROJECT_PREFILL" ]]; then
for p in "$@"; do [[ "$p" == "$PROJECT_PREFILL" ]] && { printf -v "$__var" '%s' "$p"; say info "项目 / project: $p(来自 --project)"; return 0; }; done
say err "--project '$PROJECT_PREFILL' 不在清单中 / not in projects.json (有 / have: $*)"; exit 2
fi
if [[ "$ASSUME_YES" == true ]]; then
if [[ $# -eq 1 ]]; then printf -v "$__var" '%s' "$1"; say info "项目 / project: $1(清单中唯一项目 / the only project)"; return 0; fi
say err "--yes 下清单有 $# 个项目,请用 --project <pid> 指定 / --yes with $# projects needs --project <pid>: $*"; exit 2
fi
pick "$__var" 0 "$@"
}
# ============================================================
# FLOW: 初始化环境 / init environment only (shared base host, no project)
# ============================================================
flow_init_env() {
echo; say step "初始化环境(不挂项目)/ init environment only"
local REGION INSTANCE_TYPE ROOT_VOLUME_GB
local HW_FLAGS=() # --instance-type/--root-volume-gb — only meaningful when WE create the host
ask_region REGION
if [[ "$LOCAL_MODE" == true ]]; then
# --local deploys onto THIS existing EC2; its type/disk were fixed at launch (launch-host.sh),
# and deploy-all --local reuses the box in place — asking would just mislead. Skip, show actual.
say info "机型/磁盘 / type & disk: 沿用本机($( (curl -fsS -H "X-aws-ec2-metadata-token: $(curl -fsS -X PUT http://169.254.169.254/latest/api/token -H 'X-aws-ec2-metadata-token-ttl-seconds: 60' 2>/dev/null)" http://169.254.169.254/latest/meta-data/instance-type 2>/dev/null) || echo '本机机型' ) )—— 由 launch-host 起机时决定,如需变更请换机"
else
pick_field INSTANCE_TYPE "索引主机机型 (ARM·决定 CPU/内存) / index host type" \
"${DEPLOY_INSTANCE_TYPE:-t4g.large}" "EC2 机型 (ARM)" "${INSTANCE_OPTIONS[@]}"
pick_field ROOT_VOLUME_GB "索引主机磁盘 / index host disk GiB" \
"${DEPLOY_ROOT_VOLUME_GB:-30}" "磁盘大小 GiB" "${DISK_OPTIONS[@]}"
while ! [[ "$ROOT_VOLUME_GB" =~ ^[0-9]+$ ]] || (( ROOT_VOLUME_GB < 8 )); do
[[ "$ASSUME_YES" == true ]] && { say err "磁盘大小无效 / invalid disk size '$ROOT_VOLUME_GB'"; exit 1; }
say warn "磁盘大小需为 ≥8 的整数 GiB / disk must be an integer GiB ≥ 8."
ask ROOT_VOLUME_GB "磁盘大小 GiB" "30"
done
HW_FLAGS=(--instance-type "$INSTANCE_TYPE" --root-volume-gb "$ROOT_VOLUME_GB")
fi
ask_extras base # glossary (+cap) only: monitoring needs a project's gateway first
echo; say info "将只起共享底座(VPC/NAT/EC2/镜像),不挂任何项目。之后用「添加项目」上线机器人。"
confirm "开始初始化环境?/ Initialize the base environment now?" || { say info "已取消"; exit 0; }
say step "部署底座 / Deploying base host (several minutes)"
exec "$SCRIPT_DIR/deploy-all.sh" --region "$REGION" ${HW_FLAGS[@]+"${HW_FLAGS[@]}"} \
${EXTRA_FLAGS[@]+"${EXTRA_FLAGS[@]}"} ${PROBE_FLAG[@]+"${PROBE_FLAG[@]}"} --skip-projects ${LOCAL_FLAG[@]+"${LOCAL_FLAG[@]}"} \
${DOMAIN_FLAG[@]+"${DOMAIN_FLAG[@]}"} ${LOCALE_FLAG[@]+"${LOCALE_FLAG[@]}"}
}
# ============================================================
# FLOW: 添加项目 / add a project (interactive → projects.json + secrets → deploy)
# ============================================================
flow_add_project() {
echo; say step "添加项目 / add a project"
local REGION; ask_region REGION
mkdir -p "$ROOT/.local"
[[ -f "$PROJECTS_CFG" ]] || echo '{"refreshIntervalSec":300,"projects":{}}' > "$PROJECTS_CFG"
local PID EXISTING_PIDS
# --yes cannot invent a projectId (no default exists), so it used to die later with
# "projectId 非法". Stop early, and point at the path that works without a terminal.
if [[ "$ASSUME_YES" == true ]]; then
say err "非交互首次添加项目请走脚本化路径(预建密钥 + .local/projects.json + deploy-all.sh,见 runbook 附录 A)/ non-interactive first add-project is not supported; use the scripted path (pre-create the secrets + .local/projects.json, then deploy-all.sh — runbook appendix A)"
exit 2
fi
ask PID "项目 ID(小写字母数字与连字符)/ projectId (^[a-z0-9-]+$)" ""
[[ "$PID" =~ ^[a-z0-9][a-z0-9-]*$ ]] || { say err "projectId 非法 / invalid projectId '$PID'"; exit 1; }
# project_ids fails loud on a broken projects.json (its stderr has the reason) — abort,
# don't fall through to "not exists" and then corrupt/overwrite the file further down.
EXISTING_PIDS="$(project_ids)" || { say err "修复 .local/projects.json 后重试 / fix projects.json and retry"; exit 1; }
if grep -qx "$PID" <<< "$EXISTING_PIDS"; then
say err "项目 '$PID' 已存在 / already exists — use 'redeploy' to update it."; exit 1
fi
# repos: loop git URL + subdir + ref until blank. N counts repos ALREADY added, so the
# prompt announces which repo you're entering ("第 1 个仓库" first, then 2, 3, …) — without
# it a multi-repo project gives no signal of how many are in or which one you're on.
local REPOS_JSON="[]" RGIT RSUB RREF RSRC SRC_CHOICE N=0
say info "逐个添加该项目的代码仓库(仓库名留空结束)/ add repos (blank subdir = done):"
while true; do
ask RSUB " 第 $((N + 1)) 个仓库 · on-host 子目录名 / repo #$((N + 1)) subdir (^[a-z0-9-]+$, blank=done)" ""
[[ -z "$RSUB" ]] && break
[[ "$RSUB" =~ ^[a-z0-9][a-z0-9-]*$ ]] || { say warn "subdir 非法,跳过 / invalid subdir, skipped"; continue; }
# The local-repo option is offered ONLY in single-host mode. In the default two-machine
# topology the index host is launched into a PRIVATE subnet with no key pair and no public IP,
# and push-local-repo.sh needs a real SSH host (it deliberately refuses --ssh-opts, so there is
# no SSM ProxyCommand escape either). Offering it there produced the worst possible outcome:
# activate_project succeeds without code present, the deploy prints "fully deployed", the
# installer then prints a push command that CANNOT work, and the bot answers "not found"
# forever while every health signal looks fine.
if [[ "$LOCAL_MODE" == true ]]; then
pick SRC_CHOICE 0 \
"git 远程 git 仓(自动定时刷新)/ remote git repo (auto-refresh)" \
"local 本地仓(rsync 直推 + 手动刷新)/ local repo (rsync push + manual refresh)"
RSRC="${SRC_CHOICE%%[[:space:]]*}"
else
RSRC="git"
say info " 仓库来源:git(默认拓扑的索引主机在私有子网、无密钥对、无公网 IP,无法 rsync 推送本地仓;如需本地仓请用 --local 单机拓扑)"
fi
if [[ "$RSRC" == "git" ]]; then
ask RGIT " git 地址 / repo git URL" ""
[[ -n "$RGIT" ]] || { say warn "git 仓必须有地址,跳过 / git repo needs a URL, skipped"; continue; }
ask RREF " 分支/标签(留空=默认分支)/ ref (blank=default)" ""
REPOS_JSON="$(RGIT="$RGIT" RSUB="$RSUB" RREF="$RREF" python3 -c '
import json,os,sys
a=json.loads(sys.argv[1]); a.append({"subdir":os.environ["RSUB"],"source":"git","git":os.environ["RGIT"],"ref":os.environ["RREF"]}); print(json.dumps(a))' "$REPOS_JSON")"
say ok " 已加入 git 仓 / git repo: $RSUB ← $RGIT${RREF:+ @$RREF}"
else
REPOS_JSON="$(RSUB="$RSUB" python3 -c '
import json,os,sys
a=json.loads(sys.argv[1]); a.append({"subdir":os.environ["RSUB"],"source":"local"}); print(json.dumps(a))' "$REPOS_JSON")"
say ok " 已加入本地仓 / local repo: $RSUB (装服务会先起好后端,之后本机跑 scripts/push-local-repo.sh 推代码即建图上线)"
fi
N=$((N + 1))
done
[[ "$REPOS_JSON" != "[]" ]] || { say err "至少要一个仓库 / need at least one repo"; exit 1; }
# subdir 是全局键(决定 /data/repo/<subdir>、graph.db、systemd 单元名)。两个项目用同名
# subdir 会共享同一份 graph.db(违反单写者,索引会损坏),删一个项目还会误删另一个的副本。
# 所以这里既查本项目内部重复,也查与已有项目的冲突,命中即 fail-loud。
if ! REPOS_JSON="$REPOS_JSON" python3 -c '
import json, os, sys
new = [r["subdir"] for r in json.loads(os.environ["REPOS_JSON"])]
if len(new) != len(set(new)):
sys.stderr.write("本项目内 subdir 重复 / duplicate subdir within this project\n"); sys.exit(1)
try:
projects = json.load(open(sys.argv[1])).get("projects", {})
except Exception:
projects = {}
used = {r["subdir"]: pid for pid, p in projects.items() for r in p.get("repos", [])}
clash = [(s, used[s]) for s in new if s in used]
if clash:
sys.stderr.write("subdir 已被其他项目占用 / subdir already used by another project: "
+ ", ".join(f"{s} (项目 {pid})" for s, pid in clash) + "\n"); sys.exit(1)
' "$PROJECTS_CFG"; then
say err "subdir 冲突——换个不重名的子目录名 / subdir clash; pick unique subdir names"; exit 1
fi
# port: 在 8080-8099(安全组放行段)里挑第一个没被占用的;占满则报错(最多 20 个项目)。
local SUGGEST_PORT PORT
SUGGEST_PORT="$(python3 -c 'import json,sys
try: used={p.get("port") for p in json.load(open(sys.argv[1])).get("projects",{}).values()}
except Exception: used=set()
free=[p for p in range(8080,8100) if p not in used]
print(free[0] if free else "")' "$PROJECTS_CFG")"
[[ -n "$SUGGEST_PORT" ]] || { say err "8080-8099 端口已占满(最多 20 个项目)/ no free port in 8080-8099 (max 20 projects)"; exit 1; }
ask PORT "bridge 端口(建议未用值)/ bridge port" "$SUGGEST_PORT"
# 必须落在安全组放行的 8080-8099 内——否则 runtime 连不上 bridge,会静默退化成「无证据」回答。
if ! [[ "$PORT" =~ ^[0-9]+$ ]] || (( PORT < 8080 || PORT > 8099 )); then
say err "端口必须在 8080-8099(安全组只放行这一段)/ port must be 8080-8099 (only this range is open in the SG); got '$PORT'"; exit 1
fi
# 一次 python3 遍历判冲突(同上面 subdir 冲突检查的写法)。不要用 while+&& 管道:
# 循环体末条命令对不匹配项目返回 1,会在 set -e + pipefail 下把整条管道判非零,
# 让「已占用」误报成「空闲」——两个项目共用一个 bridge 端口,后启动的起不来。
if ! python3 -c 'import json,sys
try: projects=json.load(open(sys.argv[1])).get("projects",{})
except Exception: projects={}
clash=[pid for pid,p in projects.items() if p.get("port")==int(sys.argv[2])]
if clash:
sys.stderr.write("port used by: "+", ".join(clash)+"\n"); sys.exit(1)
' "$PROJECTS_CFG" "$PORT"; then
say err "端口 $PORT 已被占用 / port already used by another project"; exit 1
fi
# Model for this project's runtime (stored in projects.json; empty = global default at deploy).
local MODEL AGENT_SDK GLOSSARY_MODEL
pick_field AGENT_SDK "问答与术语表 SDK / SDK for Q&A and glossary" \
"openai" "SDK" "${SDK_OPTIONS[@]}"
if [[ "$AGENT_SDK" == "openai" ]]; then
pick_field MODEL "回答模型 / answer model" \
"global.openai.gpt-6-astra" "Bedrock profile id" "${OPENAI_MODEL_OPTIONS[@]}"
else
pick_field MODEL "回答模型 / answer model" \
"global.anthropic.claude-opus-4-8" "Bedrock profile id" "${MODEL_OPTIONS[@]}"
fi
# Keep the first install short: glossary follows the answer model. Advanced
# deployments can override agent.glossaryModel in projects.json.
GLOSSARY_MODEL="$MODEL"
PYTHONPATH="$ROOT/agent-container" python3 - "$AGENT_SDK" "$MODEL" "$GLOSSARY_MODEL" <<'PY'
import sys
from agent_settings import AgentSettings
AgentSettings(*sys.argv[1:])
PY
# Feishu app credentials → source-truth/feishu-<pid> (auto secret id).
# Validate at the prompt (re-ask the bad field only) so a typo'd App ID / secret
# is caught here, not 10 minutes later when the bot silently fails to start.
local FEISHU_APP_ID FEISHU_BOT_OPEN_ID SECRET_ID
# `local +x` strips any inherited export attribute: bash keeps it when the name was already
# exported in the caller's environment, which would hand the typed value to every child of
# this function, including deploy-all.sh and the exec'd deploy_project.sh.
local +x FEISHU_APP_SECRET
# The tenant the app belongs to decides which console — and which API host — is correct.
# Asked before the credentials because it selects the endpoint they are validated against.
local FEISHU_DOMAIN_SEL="${DOMAIN_FLAG[1]:-${DEPLOY_FEISHU_DOMAIN:-feishu}}"
if [[ ${#DOMAIN_FLAG[@]} -eq 0 ]]; then
pick_field FEISHU_DOMAIN_SEL "飞书租户 / tenant (↑/↓ 选择,回车确认)" \
"${DEPLOY_FEISHU_DOMAIN:-feishu}" "租户 / tenant" \
"feishu:飞书 · 中国版 (open.feishu.cn)" "lark:Lark · 国际版 (open.larksuite.com)"
DOMAIN_FLAG=(--feishu-domain "$FEISHU_DOMAIN_SEL")
fi
local FEISHU_API_HOST="https://open.feishu.cn"
[[ "$FEISHU_DOMAIN_SEL" == "lark" ]] && FEISHU_API_HOST="https://open.larksuite.com"
while true; do
ask_valid FEISHU_APP_ID "飞书 App ID(cli_…)" '^cli_[A-Za-z0-9]+$' \
"App ID 应形如 cli_xxxxxxxx / App ID must look like cli_..."
while true; do
ask_secret FEISHU_APP_SECRET "飞书 App Secret(输入以 * 回显)/ (echoed as *)"
[[ -n "$FEISHU_APP_SECRET" ]] && break
say warn "App Secret 必填 / App Secret is required"
# ask_secret has no --yes branch and returns empty at EOF, so on a pipe this loop spun
# forever. There is no non-interactive way to supply a secret here by design (it must not
# come from argv, where it would land in the process list and shell history).
if [[ "$ASSUME_YES" == true || ! -t 0 ]]; then
say err "无法在非交互模式下读取 App Secret / cannot read App Secret non-interactively"
say info "请交互运行安装器,或先手动创建密钥 source-truth/feishu-<projectId>(含 app_id / app_secret / bot_open_id)后再运行。"
exit 1
fi
done
# REAL validation, not just a shape check. The regex above only proves the App ID looks like
# an App ID; the comment claiming a typo is "caught here, not 10 minutes later" was false
# until this probe existed. tenant_access_token/internal needs NO scopes and NO published
# version, so it is valid this early, costs one request, and distinguishes bad credentials
# from a tenant mismatch — the two failures that otherwise surface as a gateway restart loop
# in a log file on an EC2 instance the operator reaches through SSM.
local PROBE_RC=0 PROBE_OUT
PROBE_OUT="$(_H="$FEISHU_API_HOST" _AID="$FEISHU_APP_ID" _AS="$FEISHU_APP_SECRET" python3 - <<'PY' 2>&1
import json, os, sys, urllib.request, urllib.error
req = urllib.request.Request(
os.environ["_H"] + "/open-apis/auth/v3/tenant_access_token/internal",
data=json.dumps({"app_id": os.environ["_AID"], "app_secret": os.environ["_AS"]}).encode(),
headers={"Content-Type": "application/json; charset=utf-8"})
try:
body = json.loads(urllib.request.urlopen(req, timeout=15).read().decode())
except urllib.error.HTTPError as e:
try: body = json.loads(e.read().decode())
except Exception: print("HTTP %s" % e.code); sys.exit(3)
except Exception as e:
print("NETWORK %s" % type(e).__name__); sys.exit(4)
code = body.get("code")
if code == 0 and body.get("tenant_access_token"):
print("OK"); sys.exit(0)
print("code=%s msg=%s" % (code, str(body.get("msg"))[:120])); sys.exit(1)
PY
)" || PROBE_RC=$?
if [[ $PROBE_RC -eq 0 ]]; then
say ok "飞书凭证已验证 / credentials verified against $FEISHU_API_HOST"
break
elif [[ $PROBE_RC -eq 4 ]]; then
# Cannot reach Feishu at all — do not punish the operator for our network.
say warn "无法连通 $FEISHU_API_HOST($PROBE_OUT)——跳过凭证校验 / cannot reach Feishu, skipping validation"
break
else
say err "凭证校验失败 / credentials rejected by $FEISHU_API_HOST: $PROBE_OUT"
say info "请检查:App ID / Secret 是否抄错;以及该应用是否属于「${FEISHU_DOMAIN_SEL}」租户(中国版与国际版的应用互不相通)。"
[[ "$ASSUME_YES" == true ]] && { say err "--yes 模式下无法重试 / cannot re-prompt under --yes"; exit 1; }
FEISHU_APP_SECRET=""
fi
done
# open_id is optional, but if given it must look like ou_… (a wrong value breaks the
# group @-gate). Empty is allowed (FEISHU_BOT_OPEN_ID unset → 'any mention triggers').
# The bot's own open_id gates group @-mentions. The runbook used to tell operators to "note it
# from the bot page", but that page shows the APP identity, not an ou_-prefixed open_id — so the
# realistic outcomes were a guess or a blank. Derive it from the API instead, using the token we
# just proved works. Blank is still permitted, but the prompt now names what blank COSTS: the
# gate degrades to "any mention triggers", so @-ing a colleague makes the bot answer unbidden.
FEISHU_BOT_OPEN_ID=""
local DERIVED_OPEN_ID
DERIVED_OPEN_ID="$(_H="$FEISHU_API_HOST" _AID="$FEISHU_APP_ID" _AS="$FEISHU_APP_SECRET" python3 - <<'PY' 2>/dev/null || true
import json, os, urllib.request
h = os.environ["_H"]
def post(path, payload):
req = urllib.request.Request(h + path, data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json; charset=utf-8"})
return json.loads(urllib.request.urlopen(req, timeout=15).read().decode())
try:
tok = post("/open-apis/auth/v3/tenant_access_token/internal",
{"app_id": os.environ["_AID"], "app_secret": os.environ["_AS"]}).get("tenant_access_token")
req = urllib.request.Request(h + "/open-apis/bot/v3/info",
headers={"Authorization": "Bearer " + tok})
info = json.loads(urllib.request.urlopen(req, timeout=15).read().decode())
oid = (info.get("bot") or {}).get("open_id") or ""
if oid.startswith("ou_"):
print(oid)
except Exception:
pass
PY
)"
if [[ -n "$DERIVED_OPEN_ID" ]]; then
FEISHU_BOT_OPEN_ID="$DERIVED_OPEN_ID"
say ok "机器人 open_id 自动获取 / bot open_id derived: $FEISHU_BOT_OPEN_ID"
else
say warn "无法自动获取机器人 open_id(通常是「机器人」能力未开启,或版本未发布)"
ask_valid FEISHU_BOT_OPEN_ID "机器人 open_id(ou_…;留空则群里 @ 任何人都会触发机器人)/ blank = ANY @-mention triggers the bot" \
'^ou_[A-Za-z0-9]+$' "open_id 应形如 ou_xxxxxxxx,或留空 / must look like ou_... or be blank" allow_empty
fi
SECRET_ID="source-truth/feishu-${PID}"
local SJSON
SJSON="$(_AID="$FEISHU_APP_ID" _AS="$FEISHU_APP_SECRET" _BO="${FEISHU_BOT_OPEN_ID:-}" python3 -c '
import os,json; print(json.dumps({"app_id":os.environ["_AID"],"app_secret":os.environ["_AS"],"bot_open_id":os.environ.get("_BO","")}))')"
# Secret goes in via stdin, never argv: argv is visible in `ps` and shell history.
printf '%s' "$SJSON" | aws secretsmanager create-secret --name "$SECRET_ID" --secret-string file:///dev/stdin --region "$REGION" \
--description "source-truth Feishu app creds for project $PID" >/dev/null 2>&1 \
|| printf '%s' "$SJSON" | aws secretsmanager put-secret-value --secret-id "$SECRET_ID" --secret-string file:///dev/stdin --region "$REGION" >/dev/null
FEISHU_APP_SECRET=""; unset SJSON
say ok "飞书凭证已写入 / stored: $SECRET_ID"