-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1319 lines (1208 loc) · 48.5 KB
/
install.sh
File metadata and controls
executable file
·1319 lines (1208 loc) · 48.5 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
# ──────────────────────────────────────────────────────────────────────
# Nerve Installer — one-command setup for the Nerve web interface
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/daggerhashimoto/openclaw-nerve/master/install.sh | bash
#
# Or with options:
# curl -fsSL ... | bash -s -- --dir ~/nerve --version v1.4.4
# curl -fsSL ... | bash -s -- --dir ~/nerve --branch main
# curl -fsSL ... | bash -s -- --gateway-url https://gw.example.com --gateway-token <token> --skip-setup
# ──────────────────────────────────────────────────────────────────────
set -euo pipefail
# ── Cleanup trap ──────────────────────────────────────────────────────
TEMP_FILES=()
RWD_PIDS=()
cleanup() {
# Kill any lingering run_with_dots background processes
for pid in "${RWD_PIDS[@]}"; do
kill -0 "$pid" 2>/dev/null && kill "$pid" 2>/dev/null || true
done
# Remove temp files and directories (stderr captures, build backups)
for f in "${TEMP_FILES[@]}"; do
rm -rf "$f" 2>/dev/null || true
done
}
trap cleanup EXIT
# ── Defaults ──────────────────────────────────────────────────────────
INSTALL_DIR="${NERVE_INSTALL_DIR:-${HOME}/nerve}"
BRANCH="master"
BRANCH_EXPLICIT=false
VERSION=""
REPO="https://github.com/daggerhashimoto/openclaw-nerve.git"
NODE_MIN=22
SKIP_SETUP=false
DRY_RUN=false
GATEWAY_TOKEN=""
GATEWAY_URL_OVERRIDE=""
ACCESS_MODE=""
ENV_MISSING=false
# ── Colors ────────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
ORANGE='\033[38;5;208m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
RAIL="${DIM}│${NC}"
ok() { echo -e " ${RAIL} ${GREEN}✓${NC} $*"; }
warn() { echo -e " ${RAIL} ${YELLOW}⚠${NC} $*"; }
fail() { echo -e " ${RAIL} ${RED}✗${NC} $*"; }
info() { echo -e " ${RAIL} ${CYAN}→${NC} $*"; }
dry() { echo -e " ${RAIL} ${YELLOW}⊘${NC} ${DIM}[dry-run]${NC} $*"; }
# ── Helpers ────────────────────────────────────────────────────────────
# Detect OS family once
IS_MAC=false; IS_DEBIAN=false; IS_FEDORA=false
if [[ "$(uname -s)" == "Darwin" ]]; then IS_MAC=true;
elif command -v apt-get &>/dev/null; then IS_DEBIAN=true;
elif command -v dnf &>/dev/null || command -v yum &>/dev/null; then IS_FEDORA=true; fi
# Display a copy-pasteable command hint
hint() { echo -e " ${RAIL}"; echo -e " ${RAIL} ${BOLD}$1${NC}"; echo -e " ${RAIL}"; }
cmd() { echo -e " ${RAIL} ${CYAN}\$ $1${NC}"; }
print_deployment_guides() {
local guides_file="${INSTALL_DIR}/scripts/lib/deployment-guides.json"
local rendered_guides
[[ -r "$guides_file" ]] || return 1
rendered_guides="$(node - "$guides_file" <<'EOF'
const fs = require('node:fs');
const guidesPath = process.argv[2];
try {
const guides = JSON.parse(fs.readFileSync(guidesPath, 'utf8'));
if (!Array.isArray(guides)) process.exit(0);
const rendered = [];
for (const guide of guides) {
if (guide && typeof guide.title === 'string' && typeof guide.url === 'string') {
rendered.push(` ${guide.title}: ${guide.url}`);
}
}
process.stdout.write(rendered.join('\n'));
} catch {
process.exit(0);
}
EOF
)" || return 1
[[ -n "$rendered_guides" ]] || return 1
echo " Deployment guides:"
printf '%s\n' "$rendered_guides"
}
# Check if a port is already in use. Returns 0 if port is free, 1 if occupied.
check_port() {
local port="$1"
if command -v ss &>/dev/null; then
ss -tlnH "sport = :${port}" 2>/dev/null | grep -q . && return 1
elif command -v lsof &>/dev/null; then
lsof -iTCP:"${port}" -sTCP:LISTEN -P -n &>/dev/null && return 1
elif command -v netstat &>/dev/null; then
netstat -tlnp 2>/dev/null | grep -q ":${port} " && return 1
fi
return 0
}
repo_has_local_changes() {
local repo_dir="$1"
git -C "$repo_dir" status --porcelain --untracked-files=normal 2>/dev/null | grep -q .
}
# Animated dots while a background process runs
# Usage: run_with_dots "message" command arg1 arg2 ...
# Sets RWD_EXIT to the command's exit code after completion.
run_with_dots() {
local msg="$1"; shift
local stderr_file
stderr_file=$(mktemp /tmp/nerve-rwd-XXXXXX)
TEMP_FILES+=("$stderr_file")
printf " ${RAIL} ${CYAN}→${NC} %s " "$msg"
"$@" 2>"$stderr_file" &
local pid=$!
RWD_PIDS+=("$pid")
while kill -0 "$pid" 2>/dev/null; do
printf "."
sleep 1
done
if wait "$pid"; then
RWD_EXIT=0
else
RWD_EXIT=$?
fi
echo ""
if [[ $RWD_EXIT -ne 0 && -s "$stderr_file" ]]; then
echo -e " ${RAIL} ${RED}stderr:${NC}"
while IFS= read -r line; do
echo -e " ${RAIL} ${DIM}${line}${NC}"
done < "$stderr_file"
fi
return $RWD_EXIT
}
# Read the real gateway token. Systemd service file takes priority because
# the gateway process uses its env var over openclaw.json (known 2026.2.19 bug).
detect_gateway_token() {
local token=""
# 1. Check systemd service file (source of truth when present)
local svc_files=(
"${HOME}/.config/systemd/user/openclaw-gateway.service"
"/etc/systemd/system/openclaw-gateway.service"
)
for svc in "${svc_files[@]}"; do
if [[ -f "$svc" ]]; then
token=$(grep -oP 'OPENCLAW_GATEWAY_TOKEN=\K\S+' "$svc" 2>/dev/null || true)
if [[ -n "$token" ]]; then
echo "$token"
return 0
fi
fi
done
# 2. Fall back to openclaw.json
local config_file="${HOME}/.openclaw/openclaw.json"
if [[ -f "$config_file" ]]; then
token=$(node -e "try{const c=JSON.parse(require('fs').readFileSync('$config_file','utf8'));console.log(c.gateway?.auth?.token??'')}catch{}" 2>/dev/null || echo "")
if [[ -n "$token" ]]; then
echo "$token"
return 0
fi
fi
echo ""
}
normalize_version_tag() {
local raw="$1"
local normalized="${raw#v}"
if [[ "$normalized" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "v${normalized}"
return 0
fi
return 1
}
github_repo_path_from_url() {
local url="$1"
local path=""
if [[ "$url" =~ ^https://github.com/([^/]+)/([^/]+)(\.git)?/?$ ]]; then
path="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
elif [[ "$url" =~ ^git@github.com:([^/]+)/([^/]+)(\.git)?/?$ ]]; then
path="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
elif [[ "$url" =~ ^ssh://git@github.com/([^/]+)/([^/]+)(\.git)?/?$ ]]; then
path="${BASH_REMATCH[1]}/${BASH_REMATCH[2]}"
else
return 1
fi
path="${path%.git}"
echo "$path"
}
fetch_latest_release_tag() {
local repo_path
repo_path=$(github_repo_path_from_url "$REPO") || return 1
local api_url="https://api.github.com/repos/${repo_path}/releases/latest"
local response
local token="${GITHUB_TOKEN:-${GH_TOKEN:-}}"
if [[ -n "$token" ]]; then
response=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "User-Agent: nerve-installer" \
-H "Authorization: Bearer ${token}" \
"$api_url" 2>/dev/null) || return 1
else
response=$(curl -fsSL \
-H "Accept: application/vnd.github+json" \
-H "User-Agent: nerve-installer" \
"$api_url" 2>/dev/null) || return 1
fi
local tag
tag=$(printf '%s' "$response" | node -e 'let d="";process.stdin.on("data",c=>d+=c);process.stdin.on("end",()=>{try{const j=JSON.parse(d);if(typeof j.tag_name==="string")process.stdout.write(j.tag_name);}catch{}});') || return 1
normalize_version_tag "$tag" || return 1
}
STAGE_CURRENT=0
STAGE_TOTAL=5
stage() {
STAGE_CURRENT=$((STAGE_CURRENT + 1))
if [[ $STAGE_CURRENT -gt 1 ]]; then
echo -e " ${RAIL}"
fi
echo -e " ${ORANGE}●${NC} ${ORANGE}${BOLD}${1}${NC} ${DIM}[${STAGE_CURRENT}/${STAGE_TOTAL}]${NC}"
echo -e " ${RAIL}"
}
stage_done() {
echo -e " ${RAIL}"
}
# ── Parse args ────────────────────────────────────────────────────────
while [[ $# -gt 0 ]]; do
case "$1" in
--dir) [[ $# -ge 2 ]] || { echo "Missing value for --dir"; exit 1; }; INSTALL_DIR="$2"; shift 2 ;;
--branch) [[ $# -ge 2 ]] || { echo "Missing value for --branch"; exit 1; }; BRANCH="$2"; BRANCH_EXPLICIT=true; shift 2 ;;
--version) [[ $# -ge 2 ]] || { echo "Missing value for --version"; exit 1; }; VERSION="$2"; shift 2 ;;
--repo) [[ $# -ge 2 ]] || { echo "Missing value for --repo"; exit 1; }; REPO="$2"; shift 2 ;;
--skip-setup) SKIP_SETUP=true; shift ;;
--dry-run) DRY_RUN=true; shift ;;
--gateway-token) [[ $# -ge 2 ]] || { echo "Missing value for --gateway-token"; exit 1; }; GATEWAY_TOKEN="$2"; shift 2 ;;
--gateway-url) [[ $# -ge 2 ]] || { echo "Missing value for --gateway-url"; exit 1; }; GATEWAY_URL_OVERRIDE="$2"; shift 2 ;;
--access-mode) [[ $# -ge 2 ]] || { echo "Missing value for --access-mode"; exit 1; }; ACCESS_MODE="$2"; shift 2 ;;
--help|-h)
echo "Nerve Installer"
echo ""
echo "Options:"
echo " --dir <path> Install directory (default: ~/nerve)"
echo " --version <vX.Y.Z> Install a specific release version"
echo " --branch <name> Install from a branch (dev override; bypasses release mode)"
echo " --repo <url> Git repo URL"
echo " --skip-setup Skip the interactive setup wizard"
echo " --gateway-token <t> Gateway token (for non-interactive installs)"
echo " --gateway-url <url> Gateway URL (for remote/non-interactive installs)"
echo " --access-mode <m> Setup access mode: local|network|custom|tailscale-ip|tailscale-serve"
echo " --dry-run Simulate the install without changing anything"
echo " --help Show this help"
exit 0
;;
*) echo "Unknown option: $1"; exit 1 ;;
esac
done
if [[ -n "$VERSION" && "$BRANCH_EXPLICIT" == "true" ]]; then
fail "Use either --version or --branch, not both"
exit 1
fi
normalize_access_mode() {
case "$1" in
"") echo "" ;;
tailscale) echo "tailscale-ip" ;;
local|network|custom|tailscale-ip|tailscale-serve) echo "$1" ;;
*)
fail "Invalid --access-mode: $1"
echo " Supported values: local, network, custom, tailscale-ip, tailscale-serve"
exit 1
;;
esac
}
normalize_gateway_url() {
local url="$1"
if command -v node &>/dev/null; then
node -e 'const input=process.argv[1];try{const u=new URL(input);if(!["http:","https:"].includes(u.protocol))throw new Error("protocol");if(u.search||u.hash)throw new Error("query-or-fragment");process.stdout.write(u.toString().replace(/\/+$/,""));}catch{process.exit(1)}' "$url" 2>/dev/null || return 1
else
[[ "$url" =~ ^https?://[^[:space:]?#]+$ ]] || return 1
printf '%s' "${url%/}"
fi
}
ACCESS_MODE=$(normalize_access_mode "$ACCESS_MODE")
if [[ -n "$GATEWAY_URL_OVERRIDE" ]]; then
normalized_gateway_url=$(normalize_gateway_url "$GATEWAY_URL_OVERRIDE" || true)
if [[ -z "$normalized_gateway_url" ]]; then
fail "Invalid --gateway-url: $GATEWAY_URL_OVERRIDE"
echo " Expected an absolute http:// or https:// URL without query or fragment"
exit 1
fi
GATEWAY_URL_OVERRIDE="$normalized_gateway_url"
fi
# ── Detect interactive mode ───────────────────────────────────────────
# When piped via curl | bash, stdin is the pipe — but /dev/tty still
# provides access to the controlling terminal for interactive prompts.
# Only treat it as interactive when that controlling terminal is real.
INTERACTIVE=false
if [[ -t 0 ]]; then
INTERACTIVE=true
elif { tty -s < /dev/tty; } 2>/dev/null; then
INTERACTIVE=true
fi
# ── Banner ────────────────────────────────────────────────────────────
echo ""
echo -e " ${ORANGE}██████ █████ ██████████ ███████████ █████ █████ ██████████${NC}"
echo -e " ${ORANGE}░░██████ ░░███ ░░███░░░░░█░░███░░░░░███ ░░███ ░░███ ░░███░░░░░█${NC}"
echo -e " ${ORANGE} ░███░███ ░███ ░███ █ ░ ░███ ░███ ░███ ░███ ░███ █ ░${NC}"
echo -e " ${ORANGE} ░███░░███░███ ░██████ ░██████████ ░███ ░███ ░██████${NC}"
echo -e " ${ORANGE} ░███ ░░██████ ░███░░█ ░███░░░░░███ ░░███ ███ ░███░░█${NC}"
echo -e " ${ORANGE} ░███ ░░█████ ░███ ░ █ ░███ ░███ ░░░█████░ ░███ ░ █${NC}"
echo -e " ${ORANGE} █████ ░░█████ ██████████ █████ █████ ░░███ ██████████${NC}"
echo -e " ${ORANGE}░░░░░ ░░░░░ ░░░░░░░░░░ ░░░░░ ░░░░░ ░░░ ░░░░░░░░░░${NC}"
echo ""
echo -e " ${DIM} Web interface for OpenClaw${NC}"
echo ""
if [[ "$DRY_RUN" == "true" ]]; then
echo -e " ${YELLOW}${BOLD} ⊘ DRY RUN — nothing will be modified${NC}"
echo ""
fi
echo -e " ${DIM}│${NC}"
# ── Check: OpenClaw installed ─────────────────────────────────────────
check_openclaw() {
if command -v openclaw &>/dev/null; then
local ver
ver=$(openclaw --version 2>/dev/null | head -1 || echo "unknown")
ok "OpenClaw found: ${ver}"
return 0
fi
# Check common paths
local candidates=(
"${HOME}/.nvm/versions/node/"*/bin/openclaw
/opt/homebrew/bin/openclaw
/usr/local/bin/openclaw
/usr/bin/openclaw
"${HOME}/.volta/bin/openclaw"
"${HOME}/.fnm/aliases/default/bin/openclaw"
)
for c in "${candidates[@]}"; do
if [[ -x "$c" ]]; then
ok "OpenClaw found: ${c}"
export PATH="$(dirname "$c"):$PATH"
return 0
fi
done
fail "OpenClaw not found"
echo ""
hint "Install OpenClaw:"
cmd "npm install -g openclaw"
echo ""
echo -e " ${RAIL} ${DIM}Docs: https://github.com/openclaw/openclaw${NC}"
echo ""
exit 1
}
# ── Check: Node.js ────────────────────────────────────────────────────
check_node() {
if ! command -v node &>/dev/null; then
fail "Node.js not found — version ${NODE_MIN}+ is required"
echo ""
hint "Install Node.js via nvm (recommended):"
cmd "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash"
cmd "source ~/.bashrc"
cmd "nvm install ${NODE_MIN}"
echo ""
if $IS_MAC; then
echo -e " ${RAIL} ${DIM}Or via Homebrew: brew install node@${NODE_MIN}${NC}"
elif $IS_DEBIAN; then
echo -e " ${RAIL} ${DIM}Or via apt: https://deb.nodesource.com${NC}"
fi
echo ""
exit 1
fi
local node_ver
node_ver=$(node -v | sed 's/^v//')
local node_major
node_major=$(echo "$node_ver" | cut -d. -f1)
if [[ "$node_major" -ge "$NODE_MIN" ]]; then
ok "Node.js v${node_ver} (≥${NODE_MIN} required)"
else
fail "Node.js v${node_ver} — version ${NODE_MIN}+ is required"
echo ""
# Detect how Node was installed and suggest the right upgrade
local node_path
node_path=$(which node 2>/dev/null || echo "")
if [[ "$node_path" == *".nvm/"* ]]; then
hint "Upgrade via nvm:"
cmd "nvm install ${NODE_MIN}"
cmd "nvm use ${NODE_MIN}"
elif [[ "$node_path" == *"homebrew"* || "$node_path" == *"Cellar"* ]]; then
hint "Upgrade via Homebrew:"
cmd "brew install node@${NODE_MIN}"
elif $IS_DEBIAN; then
hint "Upgrade via nvm (recommended):"
cmd "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash"
cmd "nvm install ${NODE_MIN}"
else
hint "Upgrade Node.js:"
cmd "nvm install ${NODE_MIN}"
fi
echo ""
exit 1
fi
}
check_npm() {
if command -v npm &>/dev/null; then
ok "npm $(npm -v 2>/dev/null)"
else
fail "npm not found — it ships with Node.js"
echo ""
hint "Reinstall Node.js to get npm:"
cmd "nvm install ${NODE_MIN}"
echo ""
echo -e " ${RAIL} ${DIM}If using a system package, npm may be separate: sudo apt install npm${NC}"
echo ""
exit 1
fi
}
check_git() {
if command -v git &>/dev/null; then
ok "git $(git --version 2>/dev/null | awk '{print $3}')"
else
fail "git not found — required to clone the repo"
echo ""
if $IS_MAC; then
hint "Install git:"
cmd "xcode-select --install"
echo -e " ${RAIL} ${DIM}Or: brew install git${NC}"
elif $IS_DEBIAN; then
hint "Install git:"
cmd "sudo apt install git"
elif $IS_FEDORA; then
hint "Install git:"
cmd "sudo dnf install git"
else
hint "Install git:"
cmd "sudo apt install git"
echo -e " ${RAIL} ${DIM}Or use your system's package manager${NC}"
fi
echo ""
exit 1
fi
}
# ── Check: Build tools (needed for node-pty native compilation) ───────
check_build_tools() {
if command -v make &>/dev/null && command -v g++ &>/dev/null; then
ok "Build tools available (make, g++)"
return 0
fi
warn "Build tools (make, g++) not found — required for native modules"
# Auto-install on Debian/Ubuntu
if command -v apt-get &>/dev/null; then
if [[ "$DRY_RUN" == "true" ]]; then
if [[ $EUID -eq 0 ]]; then
dry "Would install build-essential via apt"
else
dry "Would require manual install: sudo apt install build-essential"
fi
return 0
fi
if [[ $EUID -eq 0 ]]; then
run_with_dots "Installing build tools" bash -c "DEBIAN_FRONTEND=noninteractive apt-get update -qq &>/dev/null && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq build-essential &>/dev/null"
if command -v make &>/dev/null && command -v g++ &>/dev/null; then
ok "Build tools installed"
return 0
else
fail "Failed to install build-essential"
fi
else
warn "Build tools can be auto-installed only when running as root"
echo ""
hint "Install build tools:"
cmd "sudo apt install build-essential"
echo ""
exit 1
fi
fi
# Auto-install on macOS via Xcode Command Line Tools
if [[ "$(uname -s)" == "Darwin" ]]; then
if [[ "$DRY_RUN" == "true" ]]; then
dry "Would install Xcode Command Line Tools"
return 0
fi
info "Installing Xcode Command Line Tools (this may take a few minutes)..."
xcode-select --install 2>/dev/null || true
# Wait for the install to complete — xcode-select --install is async (opens GUI dialog)
printf " ${RAIL} ${CYAN}→${NC} Waiting for Xcode CLT "
until xcode-select -p &>/dev/null; do
printf "."
sleep 5
done
echo ""
if command -v make &>/dev/null; then
ok "Xcode Command Line Tools installed"
return 0
else
fail "Xcode CLT install did not provide build tools"
fi
fi
# Can't auto-install — tell the user
echo ""
echo -e " Install build tools manually:"
echo -e " ${CYAN}Debian/Ubuntu:${NC} sudo apt install build-essential"
echo -e " ${CYAN}Fedora/RHEL:${NC} sudo dnf groupinstall 'Development Tools'"
echo -e " ${CYAN}macOS:${NC} xcode-select --install"
echo ""
exit 1
}
# ── Check: Gateway reachable ──────────────────────────────────────────
check_gateway() {
local gw_url="${GATEWAY_URL_OVERRIDE:-http://127.0.0.1:18789}"
# Try to read from openclaw.json when no explicit gateway URL was provided
local config_file="${HOME}/.openclaw/openclaw.json"
if [[ -z "$GATEWAY_URL_OVERRIDE" && -f "$config_file" ]]; then
local port
port=$(node -e "try{const c=JSON.parse(require('fs').readFileSync('$config_file','utf8'));console.log(c.gateway?.port??18789)}catch{console.log(18789)}" 2>/dev/null || echo "18789")
gw_url="http://127.0.0.1:${port}"
fi
if curl -sf "${gw_url}/health" &>/dev/null || curl -sf "${gw_url}/" &>/dev/null; then
ok "OpenClaw gateway reachable at ${gw_url}"
else
warn "Gateway not reachable at ${gw_url} — start it with: openclaw gateway start"
fi
# Verify auth token exists (needed for .env generation and service connectivity)
local gw_token="${GATEWAY_TOKEN:-}"
if [[ -z "$gw_token" ]]; then
gw_token=$(detect_gateway_token)
fi
if [[ -n "$gw_token" ]]; then
ok "Gateway auth token present"
else
warn "No gateway auth token found — run: ${CYAN}openclaw onboard --install-daemon${NC}"
fi
}
# ── [1/5] Prerequisites ───────────────────────────────────────────────
stage "Prerequisites"
check_node
check_npm
check_git
check_build_tools
check_openclaw
check_gateway
# ── [2/5] Clone or update ────────────────────────────────────────────
stage "Download"
TARGET_REF=""
TARGET_REF_KIND=""
if [[ -n "$VERSION" ]]; then
if ! TARGET_REF=$(normalize_version_tag "$VERSION"); then
fail "Invalid --version: ${VERSION} (expected vX.Y.Z)"
exit 1
fi
TARGET_REF_KIND="version"
elif [[ "$BRANCH_EXPLICIT" == "true" ]]; then
TARGET_REF="$BRANCH"
TARGET_REF_KIND="branch"
else
TARGET_REF=$(fetch_latest_release_tag || true)
if [[ -n "$TARGET_REF" ]]; then
TARGET_REF_KIND="release"
else
TARGET_REF="$BRANCH"
TARGET_REF_KIND="branch-fallback"
warn "Could not resolve latest GitHub release — falling back to branch ${BRANCH}"
fi
fi
if [[ "$TARGET_REF_KIND" == "release" || "$TARGET_REF_KIND" == "version" ]]; then
info "Using ref ${TARGET_REF} (${TARGET_REF_KIND})"
else
info "Using ref ${TARGET_REF} (${TARGET_REF_KIND})"
fi
if [[ "$DRY_RUN" == "true" ]]; then
if [[ -d "$INSTALL_DIR/.git" ]]; then
dry "Would update existing installation in ${INSTALL_DIR}"
dry "Would warn before overwriting local repo changes"
dry "Would checkout ${TARGET_REF}"
else
dry "Would clone ${REPO}"
dry "Would checkout ${TARGET_REF}"
dry "Would install to ${INSTALL_DIR}"
fi
else
if [[ -d "$INSTALL_DIR/.git" ]]; then
if repo_has_local_changes "$INSTALL_DIR"; then
warn "Existing installation has local repo changes"
info "Updating will discard tracked edits in ${INSTALL_DIR}"
if [[ "$INTERACTIVE" == "true" ]]; then
printf " ${RAIL} ${YELLOW}?${NC} Continue and overwrite local changes? (y/N) "
if read -r answer < /dev/tty 2>/dev/null; then
if [[ "$(echo "$answer" | tr "[:upper:]" "[:lower:]")" != "y" ]]; then
fail "Aborted to avoid overwriting local changes"
exit 1
fi
else
fail "Cannot confirm destructive update safely"
exit 1
fi
else
fail "Refusing to overwrite a dirty install in non-interactive mode"
info "Commit, stash, or back up ${INSTALL_DIR}, then rerun the installer"
exit 1
fi
fi
cd "$INSTALL_DIR"
if [[ "$TARGET_REF_KIND" == "branch" || "$TARGET_REF_KIND" == "branch-fallback" ]]; then
run_with_dots "Fetching ${TARGET_REF}" git fetch origin "$TARGET_REF" -q
run_with_dots "Checking out ${TARGET_REF}" git checkout --force "$TARGET_REF" -q
run_with_dots "Resetting to origin/${TARGET_REF}" git reset --hard "origin/${TARGET_REF}" -q
else
run_with_dots "Fetching tags" git fetch --tags origin -q
run_with_dots "Checking out ${TARGET_REF}" git checkout --force "$TARGET_REF" -q
fi
ok "Updated to ${TARGET_REF}"
else
if [[ "$TARGET_REF_KIND" == "branch" || "$TARGET_REF_KIND" == "branch-fallback" ]]; then
run_with_dots "Cloning Nerve" git clone --branch "$TARGET_REF" --depth 1 -q "$REPO" "$INSTALL_DIR"
else
run_with_dots "Cloning Nerve" git clone --depth 1 -q "$REPO" "$INSTALL_DIR"
cd "$INSTALL_DIR"
run_with_dots "Fetching tags" git fetch --tags origin -q
run_with_dots "Checking out ${TARGET_REF}" git checkout --force "$TARGET_REF" -q
fi
ok "Cloned to ${INSTALL_DIR}"
fi
cd "$INSTALL_DIR"
fi
# ── [3/5] Install & Build ────────────────────────────────────────────
stage "Install & Build"
if [[ "$DRY_RUN" == "true" ]]; then
dry "Would run: npm ci"
dry "Would run: npm run build"
else
npm_log=$(mktemp /tmp/nerve-npm-install-XXXXXX)
run_with_dots "Installing dependencies" bash -c "npm ci --loglevel=error > '$npm_log' 2>&1"
if [[ $RWD_EXIT -eq 0 ]]; then
ok "Dependencies installed"
# Back up existing build outputs for rollback on failure
BUILD_BACKUP=""
if [[ -d dist || -d server-dist ]]; then
BUILD_BACKUP=$(mktemp -d /tmp/nerve-build-backup-XXXXXX)
TEMP_FILES+=("$BUILD_BACKUP")
[[ -d dist ]] && cp -a dist "$BUILD_BACKUP/dist"
[[ -d server-dist ]] && cp -a server-dist "$BUILD_BACKUP/server-dist"
fi
else
fail "npm ci failed"
echo ""
# Show the last meaningful lines
echo -e " ${RAIL} ${DIM}── Last 10 lines ──${NC}"
tail -10 "$npm_log" | while IFS= read -r line; do
echo -e " ${RAIL} ${DIM}${line}${NC}"
done
echo -e " ${RAIL} ${DIM}── Full log: ${npm_log} ──${NC}"
echo ""
# Detect common failure patterns and suggest fixes
if grep -qi 'EACCES\|permission denied' "$npm_log"; then
hint "Permissions issue — try installing Node via nvm instead of system packages:"
cmd "curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash"
cmd "nvm install ${NODE_MIN}"
echo -e " ${RAIL} ${DIM}nvm installs to your home directory — no sudo needed${NC}"
elif grep -qi 'node-gyp\|gyp ERR\|make.*Error\|g++.*not found\|cc.*not found' "$npm_log"; then
hint "Native module compilation failed — install build tools:"
if $IS_MAC; then
cmd "xcode-select --install"
elif $IS_DEBIAN; then
cmd "sudo apt install build-essential"
elif $IS_FEDORA; then
cmd "sudo dnf groupinstall 'Development Tools'"
else
cmd "sudo apt install build-essential"
fi
elif grep -qi 'ERESOLVE\|peer dep\|could not resolve' "$npm_log"; then
hint "Dependency conflict — try with a clean slate:"
cmd "rm -rf node_modules package-lock.json"
cmd "npm install"
else
hint "Troubleshooting:"
echo -e " ${RAIL} ${DIM}1. Check the full log: cat ${npm_log}${NC}"
echo -e " ${RAIL} ${DIM}2. Ensure Node ${NODE_MIN}+ and build tools are installed${NC}"
echo -e " ${RAIL} ${DIM}3. Try: rm -rf node_modules && npm install${NC}"
fi
echo ""
exit 1
fi
build_log=$(mktemp /tmp/nerve-build-XXXXXX)
run_with_dots "Building project" bash -c "npm run build > '$build_log' 2>&1"
if [[ $RWD_EXIT -eq 0 ]]; then
ok "Client and server built"
else
fail "Build failed"
# Rollback to previous build output if available
if [[ -n "${BUILD_BACKUP:-}" ]]; then
rm -rf dist server-dist 2>/dev/null
[[ -d "$BUILD_BACKUP/dist" ]] && cp -a "$BUILD_BACKUP/dist" dist
[[ -d "$BUILD_BACKUP/server-dist" ]] && cp -a "$BUILD_BACKUP/server-dist" server-dist
warn "Restored previous build output"
fi
echo ""
echo -e " ${RAIL} ${DIM}── Last 10 lines ──${NC}"
tail -10 "$build_log" | while IFS= read -r line; do
echo -e " ${RAIL} ${DIM}${line}${NC}"
done
echo -e " ${RAIL} ${DIM}── Full log: ${build_log} ──${NC}"
echo ""
hint "Troubleshooting:"
echo -e " ${RAIL} ${DIM}1. Check the full log: cat ${build_log}${NC}"
echo -e " ${RAIL} ${DIM}2. Try rebuilding: npm run build${NC}"
echo ""
exit 1
fi
# Clean up temp logs on success
rm -f "$npm_log" "$build_log" 2>/dev/null
# ── Download speech model (for local voice input) ──────────────────
# Keep installer bootstrap in sync with UI/server default (multilingual base).
WHISPER_MODEL_DIR="${HOME}/.nerve/models"
WHISPER_MODEL_KEY="base"
if [[ -f .env ]]; then
EXISTING_WHISPER_MODEL=$(grep -E '^WHISPER_MODEL=' .env 2>/dev/null | tail -1 | cut -d= -f2- | tr -d '\r' || true)
if [[ -n "$EXISTING_WHISPER_MODEL" ]]; then
WHISPER_MODEL_KEY="$EXISTING_WHISPER_MODEL"
fi
fi
# Normalize .env value: trim whitespace, strip inline comments and wrapping quotes.
WHISPER_MODEL_KEY=$(printf '%s' "$WHISPER_MODEL_KEY" | sed -E 's/[[:space:]]+#.*$//; s/^[[:space:]]+//; s/[[:space:]]+$//')
if [[ "$WHISPER_MODEL_KEY" =~ ^\".*\"$ || "$WHISPER_MODEL_KEY" =~ ^\'.*\'$ ]]; then
WHISPER_MODEL_KEY="${WHISPER_MODEL_KEY:1:${#WHISPER_MODEL_KEY}-2}"
fi
WHISPER_MODEL_KEY=$(printf '%s' "$WHISPER_MODEL_KEY" | sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//' | tr '[:upper:]' '[:lower:]')
WHISPER_MODEL_SIZE="75MB"
case "$WHISPER_MODEL_KEY" in
tiny.en) WHISPER_MODEL_FILE="ggml-tiny.en.bin" ; WHISPER_MODEL_SIZE="75MB" ;;
base.en) WHISPER_MODEL_FILE="ggml-base.en.bin" ; WHISPER_MODEL_SIZE="142MB" ;;
small.en) WHISPER_MODEL_FILE="ggml-small.en.bin"; WHISPER_MODEL_SIZE="466MB" ;;
tiny) WHISPER_MODEL_FILE="ggml-tiny.bin" ; WHISPER_MODEL_SIZE="75MB" ;;
base) WHISPER_MODEL_FILE="ggml-base.bin" ; WHISPER_MODEL_SIZE="142MB" ;;
small) WHISPER_MODEL_FILE="ggml-small.bin" ; WHISPER_MODEL_SIZE="466MB" ;;
*)
warn "Unknown WHISPER_MODEL='${WHISPER_MODEL_KEY}' in .env — defaulting to base"
WHISPER_MODEL_KEY="base"
WHISPER_MODEL_FILE="ggml-base.bin"
WHISPER_MODEL_SIZE="142MB"
;;
esac
WHISPER_MODEL_PATH="${WHISPER_MODEL_DIR}/${WHISPER_MODEL_FILE}"
WHISPER_MODEL_URL="https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${WHISPER_MODEL_FILE}"
if [[ -f "$WHISPER_MODEL_PATH" ]]; then
ok "Speech model already downloaded (${WHISPER_MODEL_KEY})"
else
mkdir -p "$WHISPER_MODEL_DIR"
run_with_dots "Downloading speech model ${WHISPER_MODEL_KEY} (${WHISPER_MODEL_SIZE})" bash -c "curl -fsSL -o '$WHISPER_MODEL_PATH' '$WHISPER_MODEL_URL' 2>/dev/null"
if [[ $RWD_EXIT -eq 0 ]]; then
ok "Speech model ready (${WHISPER_MODEL_KEY})"
else
warn "Speech model download failed — local STT may fail unless STT_PROVIDER=openai with OPENAI_API_KEY"
rm -f "$WHISPER_MODEL_PATH" 2>/dev/null
fi
fi
# ── Check for ffmpeg (needed for voice input) ──────────────────────
if ! command -v ffmpeg &>/dev/null; then
if $IS_MAC; then
warn "ffmpeg not found — needed for voice input"
hint "Install with:"
cmd "brew install ffmpeg"
elif $IS_DEBIAN; then
run_with_dots "Installing ffmpeg" bash -c "sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq ffmpeg &>/dev/null"
if [[ $RWD_EXIT -eq 0 ]]; then
ok "ffmpeg installed"
else
warn "ffmpeg install failed — voice input may not work"
fi
elif $IS_FEDORA; then
run_with_dots "Installing ffmpeg" bash -c "sudo dnf install -y -q ffmpeg &>/dev/null"
if [[ $RWD_EXIT -eq 0 ]]; then
ok "ffmpeg installed"
else
warn "ffmpeg install failed — voice input may not work"
fi
fi
fi
fi
# ── Auto-generate .env from OpenClaw gateway config ───────────────────
generate_env_from_gateway() {
# Already have an .env? Don't overwrite.
if [[ -f .env ]]; then
ok "Existing .env found — keeping current configuration"
return 0
fi
local gw_token="${GATEWAY_TOKEN:-}"
local gw_url="${GATEWAY_URL_OVERRIDE:-}"
local gw_port="18789"
local config_file="${HOME}/.openclaw/openclaw.json"
# Read token from systemd/config if no --gateway-token was passed
if [[ -z "$gw_token" ]]; then
gw_token=$(detect_gateway_token)
fi
if [[ -z "$gw_url" && -f "$config_file" ]]; then
gw_port=$(node -e "try{const c=JSON.parse(require('fs').readFileSync('$config_file','utf8'));console.log(c.gateway?.port??18789)}catch{console.log(18789)}" 2>/dev/null || echo "18789")
gw_url="http://127.0.0.1:${gw_port}"
fi
if [[ -z "$gw_url" ]]; then
gw_url="http://127.0.0.1:${gw_port}"
fi
if [[ -n "$gw_token" ]]; then
local nerve_port=3080
if ! check_port "$nerve_port"; then
if [[ "$INTERACTIVE" != "true" ]]; then
fail "Port ${nerve_port} is already in use. Set a different PORT in .env or free the port."
exit 1
else
warn "Port ${nerve_port} is already in use"
while true; do
printf " ${RAIL} ${CYAN}→${NC} Enter an available port: "
if ! read -r nerve_port < /dev/tty 2>/dev/null; then
fail "Cannot read from terminal"
exit 1
fi
if [[ ! "$nerve_port" =~ ^[0-9]+$ ]] || (( nerve_port < 1 || nerve_port > 65535 )); then
warn "Invalid port number"
continue
fi
if check_port "$nerve_port"; then
break
fi
warn "Port ${nerve_port} is also in use"
done
fi
fi
cat > .env <<ENVEOF
GATEWAY_URL=${gw_url}
GATEWAY_TOKEN=${gw_token}
PORT=${nerve_port}
ENVEOF
ok "Generated .env from OpenClaw gateway config"
else
warn "Cannot auto-generate .env — no gateway token found"
warn "Run: ${CYAN}npm run setup${NC} to configure manually"
ENV_MISSING=true
fi
}
# ── [4/5] Configure ──────────────────────────────────────────────────
stage "Configure"
if [[ "$DRY_RUN" == "true" ]]; then
if [[ "$SKIP_SETUP" == "true" ]]; then
dry "Would skip setup wizard (--skip-setup)"
elif [[ -n "$ACCESS_MODE" ]]; then
dry "Would run non-interactive setup wizard (--defaults --access-mode ${ACCESS_MODE})"
else
dry "Would launch interactive setup wizard"
dry "Would prompt for: gateway token, port, TTS config"
fi
if [[ -n "$GATEWAY_URL_OVERRIDE" ]]; then
dry "Would write GATEWAY_URL=${GATEWAY_URL_OVERRIDE}"
fi
else
if [[ "$SKIP_SETUP" == "true" ]]; then
if [[ -f .env ]]; then
ok "Skipping setup (--skip-setup flag, .env exists)"
else
info "Skipping wizard — generating .env from gateway config..."
generate_env_from_gateway
fi
else
if [[ -f .env ]]; then
if [[ "$INTERACTIVE" == "true" && -z "$ACCESS_MODE" ]]; then
ok "Existing .env found"
printf " ${RAIL} ${YELLOW}?${NC} Run setup wizard anyway? (y/N) "
if read -r answer < /dev/tty 2>/dev/null; then
if [[ "$(echo "$answer" | tr "[:upper:]" "[:lower:]")" == "y" ]]; then
echo ""
NERVE_INSTALLER=1 npm run setup < /dev/tty 2>/dev/null || {
warn "Setup wizard failed (no TTY?) — run ${CYAN}npm run setup${NC} manually"
}
else
ok "Keeping existing configuration"
fi
else
warn "Cannot read input — run ${CYAN}npm run setup${NC} manually to reconfigure"
fi
else
ok "Existing .env found — keeping current configuration"
fi
elif [[ -n "$ACCESS_MODE" ]]; then
info "Explicit access mode requested — running non-interactive setup wizard..."
NERVE_INSTALLER=1 npm run setup -- --defaults --access-mode "$ACCESS_MODE" || {
fail "Setup failed for --access-mode ${ACCESS_MODE}"
exit 1
}
elif [[ "$INTERACTIVE" == "true" ]]; then
NERVE_INSTALLER=1 npm run setup < /dev/tty 2>/dev/null || {
warn "Setup wizard failed — attempting auto-config from gateway..."
generate_env_from_gateway
}
else
info "Non-interactive mode — generating .env from gateway config..."
generate_env_from_gateway
fi
fi
fi
# ── [5/5] Systemd service ────────────────────────────────────────────
stage "Service"
setup_systemd() {
local service_file="/etc/systemd/system/nerve.service"
local node_bin
node_bin=$(which node)
local working_dir="$INSTALL_DIR"
local node_dir
node_dir=$(dirname "${node_bin}")
# Run as the installing user (who has openclaw config)
local install_user="${SUDO_USER:-${USER}}"
local install_home="${HOME}"
# If running via sudo, get the real user's home (no eval — safe from injection)
if [[ -n "${SUDO_USER:-}" ]]; then
if command -v getent &>/dev/null; then