-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·1430 lines (1264 loc) · 50.6 KB
/
setup.sh
File metadata and controls
executable file
·1430 lines (1264 loc) · 50.6 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
#!/bin/bash
# ============================================================
# Become CEO — One-Click Setup v2.3
# Supports: Ubuntu 22.04/24.04, Debian 12+, Amazon Linux 2023
# Architecture: amd64, arm64
# Modes: install (default), --upgrade, --uninstall, --reset
# ============================================================
SETUP_VERSION="2.3"
# ---- Strict mode ----
set -euo pipefail
trap 'on_error $LINENO' ERR
# ---- Colors & formatting (auto-detect TTY) ----
if [ -t 1 ] && [ "${NO_COLOR:-}" = "" ]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m'
else
# No colors when piped, redirected, or NO_COLOR is set
RED='' GREEN='' YELLOW='' BLUE='' CYAN='' BOLD='' DIM='' NC=''
fi
# ---- Globals ----
REPO_RAW="https://raw.githubusercontent.com/wanikua/become-ceo/main/become-ceo/references"
WORKSPACE="${CLAWD_WORKSPACE:-$HOME/clawd}"
CONFIG_DIR="$HOME/.clawdbot"
LOG_FILE="/tmp/become-ceo-setup-$(date +%Y%m%d-%H%M%S).log"
STEP_CURRENT=0
STEP_TOTAL=0
ERRORS=()
WARNINGS=()
SKIP_INTERACTIVE="${SKIP_INTERACTIVE:-}"
SKIP_OPTIONAL="${SKIP_OPTIONAL:-}"
DRY_RUN="${DRY_RUN:-}"
CHECK_ONLY="${CHECK_ONLY:-}"
UPGRADE_MODE="${UPGRADE_MODE:-}"
UNINSTALL_MODE="${UNINSTALL_MODE:-}"
RESET_MODE="${RESET_MODE:-}"
IN_CONTAINER=""
# ---- Logging ----
log() { echo "[$(date '+%H:%M:%S')] $*" >> "$LOG_FILE"; }
log_cmd() { "$@" >> "$LOG_FILE" 2>&1; }
# ---- Error handler ----
on_error() {
local line=$1
echo ""
echo -e "${RED}✗ Error on line $line${NC}"
echo -e " Log file: ${DIM}$LOG_FILE${NC}"
echo -e " Last 5 lines:"
tail -5 "$LOG_FILE" 2>/dev/null | sed 's/^/ /'
echo ""
echo -e "${YELLOW}Need help? Open an issue:${NC}"
echo -e " ${BLUE}https://github.com/wanikua/become-ceo/issues${NC}"
exit 1
}
# ---- Progress helpers ----
step() {
STEP_CURRENT=$((STEP_CURRENT + 1))
echo ""
echo -e "${CYAN}[$STEP_CURRENT/$STEP_TOTAL]${NC} ${BOLD}$1${NC}"
log "=== Step $STEP_CURRENT/$STEP_TOTAL: $1 ==="
}
ok() { echo -e " ${GREEN}✓${NC} $1"; log " OK: $1"; }
warn() { echo -e " ${YELLOW}⚠${NC} $1"; log " WARN: $1"; WARNINGS+=("$1"); }
skip() { echo -e " ${DIM}→ $1 (skipped)${NC}"; log " SKIP: $1"; }
info() { echo -e " ${DIM}$1${NC}"; }
# ---- Dry-run wrapper ----
# Use: run_cmd <description> <command...>
# In dry-run mode, prints what would happen instead of executing.
run_cmd() {
local desc="$1"; shift
if [ -n "$DRY_RUN" ]; then
echo -e " ${DIM}[dry-run] would run: $*${NC}"
log "[dry-run] $desc: $*"
return 0
fi
"$@"
}
# ---- Network connectivity check ----
check_network() {
local urls=("https://deb.nodesource.com" "https://github.com" "https://registry.npmjs.org")
local failed=()
for url in "${urls[@]}"; do
if ! curl -fsSL --connect-timeout 5 --max-time 10 "$url" -o /dev/null 2>/dev/null; then
failed+=("$url")
fi
done
if [ ${#failed[@]} -gt 0 ]; then
warn "Cannot reach: ${failed[*]}"
warn "Some downloads may fail. Check firewall/proxy settings."
return 1
fi
return 0
}
# ---- API key format validation ----
validate_api_key() {
local key="$1" provider="$2"
[ -z "$key" ] && return 0 # Empty is OK (user skipped)
case "$provider" in
anthropic)
if [[ ! "$key" =~ ^sk-ant- ]]; then
echo -e " ${YELLOW}⚠ Key doesn't look like an Anthropic key (expected sk-ant-...).${NC}"
echo -e " ${DIM} Continuing anyway — double-check in your provider dashboard.${NC}"
return 1
fi
;;
openai)
if [[ ! "$key" =~ ^sk- ]]; then
echo -e " ${YELLOW}⚠ Key doesn't look like an OpenAI key (expected sk-...).${NC}"
echo -e " ${DIM} Continuing anyway — double-check in your provider dashboard.${NC}"
return 1
fi
;;
esac
return 0
}
# ---- Notion token format validation ----
validate_notion_token() {
local token="$1"
[ -z "$token" ] && return 0
if [[ ! "$token" =~ ^(ntn_|secret_) ]]; then
echo -e " ${YELLOW}⚠ Token doesn't look like a Notion token (expected ntn_... or secret_...).${NC}"
echo -e " ${DIM} Continuing anyway — check notion.so/my-integrations.${NC}"
return 1
fi
return 0
}
# ---- Container/VM detection ----
detect_container() {
if [ -f /.dockerenv ] || grep -q 'docker\|containerd' /proc/1/cgroup 2>/dev/null; then
IN_CONTAINER="docker"
elif [ -f /run/systemd/container ] || grep -q 'lxc' /proc/1/cgroup 2>/dev/null; then
IN_CONTAINER="lxc"
elif systemd-detect-virt --container 2>/dev/null | grep -qv "none"; then
IN_CONTAINER="$(systemd-detect-virt --container 2>/dev/null)"
fi
}
# ---- Config backup ----
# Backs up existing config before any destructive operation
backup_config() {
local config_file="$CONFIG_DIR/clawdbot.json"
[ ! -f "$config_file" ] && return 0
local backup_dir="$CONFIG_DIR/backups"
local backup_name="clawdbot.json.$(date +%Y%m%d-%H%M%S).bak"
mkdir -p "$backup_dir"
if [ -n "$DRY_RUN" ]; then
echo -e " ${DIM}[dry-run] would backup $config_file → $backup_dir/$backup_name${NC}"
return 0
fi
cp "$config_file" "$backup_dir/$backup_name"
ok "Config backed up → backups/$backup_name"
log "Backup: $config_file → $backup_dir/$backup_name"
# Keep only last 5 backups
local count
count=$(ls -1 "$backup_dir"/clawdbot.json.*.bak 2>/dev/null | wc -l)
if [ "$count" -gt 5 ]; then
ls -1t "$backup_dir"/clawdbot.json.*.bak | tail -n +6 | xargs rm -f
log "Pruned old backups (kept 5)"
fi
}
# ---- Dependency checks ----
require_cmd() {
command -v "$1" &>/dev/null || return 1
}
require_root_or_sudo() {
if [ "$EUID" -eq 0 ]; then
SUDO=""
elif require_cmd sudo; then
SUDO="sudo"
else
echo -e "${RED}Error: This script needs root or sudo access.${NC}"
exit 1
fi
}
# ============================================================
# Pre-flight checks
# ============================================================
preflight() {
echo ""
echo -e "${BLUE}${BOLD}╔══════════════════════════════════════╗${NC}"
echo -e "${BLUE}${BOLD}║ 🏢 Become CEO — Setup v${SETUP_VERSION} ║${NC}"
echo -e "${BLUE}${BOLD}╚══════════════════════════════════════╝${NC}"
[ -n "$DRY_RUN" ] && echo -e "${YELLOW}${BOLD} ⚡ DRY-RUN MODE — no changes will be made${NC}"
echo ""
log "=== Setup started at $(date) ==="
# Detect container environment
detect_container
if [ -n "$IN_CONTAINER" ]; then
log "Container detected: $IN_CONTAINER"
fi
# Detect OS
if [ -f /etc/os-release ]; then
. /etc/os-release
OS_NAME="${ID:-unknown}"
OS_VERSION="${VERSION_ID:-unknown}"
OS_PRETTY="${PRETTY_NAME:-$OS_NAME $OS_VERSION}"
else
OS_NAME="unknown"
OS_VERSION="unknown"
OS_PRETTY="Unknown OS"
fi
# Detect architecture
ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH_LABEL="amd64" ;;
aarch64) ARCH_LABEL="arm64" ;;
armv7l) ARCH_LABEL="armv7" ;;
*) ARCH_LABEL="$ARCH" ;;
esac
# Detect package manager
if require_cmd apt-get; then
PKG_MGR="apt"
elif require_cmd dnf; then
PKG_MGR="dnf"
elif require_cmd yum; then
PKG_MGR="yum"
else
PKG_MGR="unknown"
fi
# Detect RAM
TOTAL_RAM_MB=$(free -m 2>/dev/null | awk '/^Mem:/{print $2}' || echo "0")
TOTAL_RAM_GB=$(awk "BEGIN{printf \"%.1f\", $TOTAL_RAM_MB/1024}")
# Detect disk space
FREE_DISK_MB=$(df -m "$HOME" 2>/dev/null | awk 'NR==2{print $4}' || echo "0")
FREE_DISK_GB=$(awk "BEGIN{printf \"%.1f\", $FREE_DISK_MB/1024}")
# Detect existing installations
HAS_NODE=$(require_cmd node && echo "yes" || echo "no")
HAS_GH=$(require_cmd gh && echo "yes" || echo "no")
HAS_CHROMIUM=$(require_cmd chromium-browser || require_cmd chromium && echo "yes" || echo "no")
HAS_CLAWDBOT=$(require_cmd clawdbot && echo "yes" || echo "no")
HAS_SWAP=$(swapon --show 2>/dev/null | grep -q . && echo "yes" || echo "no")
echo -e "${BOLD}Environment Detected:${NC}"
echo -e " OS: $OS_PRETTY ($ARCH_LABEL)"
echo -e " RAM: ${TOTAL_RAM_GB}GB | Disk free: ${FREE_DISK_GB}GB"
echo -e " Package: $PKG_MGR"
[ -n "$IN_CONTAINER" ] && echo -e " Container: ${YELLOW}$IN_CONTAINER${NC} (swap & systemd disabled)"
echo ""
# Existing software
echo -e "${BOLD}Existing Software:${NC}"
[ "$HAS_NODE" = "yes" ] && echo -e " Node.js: ${GREEN}$(node -v)${NC}" || echo -e " Node.js: ${DIM}not found${NC}"
[ "$HAS_GH" = "yes" ] && echo -e " GitHub CLI: ${GREEN}$(gh --version 2>/dev/null | head -1)${NC}" || echo -e " GitHub CLI: ${DIM}not found${NC}"
[ "$HAS_CHROMIUM" = "yes" ] && echo -e " Chromium: ${GREEN}installed${NC}" || echo -e " Chromium: ${DIM}not found${NC}"
[ "$HAS_CLAWDBOT" = "yes" ] && echo -e " OpenClaw: ${GREEN}$(clawdbot --version 2>/dev/null || echo 'installed')${NC}" || echo -e " OpenClaw: ${DIM}not found${NC}"
[ "$HAS_SWAP" = "yes" ] && echo -e " Swap: ${GREEN}active${NC}" || echo -e " Swap: ${DIM}none${NC}"
echo ""
log "OS=$OS_PRETTY ARCH=$ARCH_LABEL RAM=${TOTAL_RAM_GB}GB DISK=${FREE_DISK_GB}GB PKG=$PKG_MGR CONTAINER=$IN_CONTAINER"
log "Node=$HAS_NODE GH=$HAS_GH Chromium=$HAS_CHROMIUM Clawdbot=$HAS_CLAWDBOT Swap=$HAS_SWAP"
# Validate environment
if [ "$PKG_MGR" = "unknown" ]; then
echo -e "${RED}Error: No supported package manager found (apt/dnf/yum).${NC}"
echo -e "Supported distros: Ubuntu 22.04+, Debian 12+, Amazon Linux 2023, Fedora 38+"
exit 1
fi
if [ "$TOTAL_RAM_MB" -lt 512 ]; then
echo -e "${RED}Error: At least 512MB RAM required (found ${TOTAL_RAM_GB}GB).${NC}"
exit 1
fi
if [ "$FREE_DISK_MB" -lt 2048 ]; then
echo -e "${RED}Error: At least 2GB free disk space required (found ${FREE_DISK_GB}GB).${NC}"
exit 1
fi
if [ "$TOTAL_RAM_MB" -lt 2048 ]; then
warn "Low RAM (${TOTAL_RAM_GB}GB). Setup will create swap, but consider Oracle Cloud free tier (24GB)."
fi
# Network connectivity check
echo -e "${BOLD}Network:${NC}"
if check_network; then
echo -e " ${GREEN}✓${NC} All required endpoints reachable"
else
echo -e " ${YELLOW}⚠${NC} Some endpoints unreachable (see warnings above)"
fi
echo ""
# Count steps based on what needs installation
STEP_TOTAL=5 # Always: system update, workspace, config wizard, gateway, health check
[ "$HAS_SWAP" = "no" ] && [ "$TOTAL_RAM_MB" -lt 8192 ] && [ -z "$IN_CONTAINER" ] && STEP_TOTAL=$((STEP_TOTAL + 1))
{ [ "$HAS_NODE" = "no" ] || ! node -v 2>/dev/null | grep -q "^v2[2-9]"; } && STEP_TOTAL=$((STEP_TOTAL + 1))
[ "$HAS_GH" = "no" ] && [ -z "$SKIP_OPTIONAL" ] && STEP_TOTAL=$((STEP_TOTAL + 1))
[ "$HAS_CHROMIUM" = "no" ] && [ -z "$SKIP_OPTIONAL" ] && STEP_TOTAL=$((STEP_TOTAL + 1))
[ "$HAS_CLAWDBOT" = "no" ] && STEP_TOTAL=$((STEP_TOTAL + 1))
# --check mode: exit after showing environment info
if [ -n "$CHECK_ONLY" ]; then
echo -e "${GREEN}${BOLD}Pre-flight check complete.${NC}"
if [ ${#WARNINGS[@]} -gt 0 ]; then
echo -e "${YELLOW}Warnings: ${#WARNINGS[@]}${NC}"
for w in "${WARNINGS[@]}"; do echo -e " ${YELLOW}⚠${NC} $w"; done
else
echo -e "${GREEN}✓ No issues found. Ready to install.${NC}"
fi
exit 0
fi
require_root_or_sudo
# Confirmation
if [ -z "$SKIP_INTERACTIVE" ]; then
echo -e "${BOLD}Ready to install ($STEP_TOTAL steps). Continue? [Y/n]${NC} "
read -r confirm
if [[ "${confirm:-Y}" =~ ^[Nn] ]]; then
echo "Setup cancelled."
exit 0
fi
fi
}
# ============================================================
# Package installation helpers (multi-distro)
# ============================================================
pkg_update() {
case "$PKG_MGR" in
apt) $SUDO apt-get update -qq 2>> "$LOG_FILE" ;;
dnf) $SUDO dnf check-update -q 2>> "$LOG_FILE" || true ;;
yum) $SUDO yum check-update -q 2>> "$LOG_FILE" || true ;;
esac
}
pkg_install() {
case "$PKG_MGR" in
apt) $SUDO apt-get install -y -qq "$@" >> "$LOG_FILE" 2>&1 ;;
dnf) $SUDO dnf install -y -q "$@" >> "$LOG_FILE" 2>&1 ;;
yum) $SUDO yum install -y -q "$@" >> "$LOG_FILE" 2>&1 ;;
esac
}
# ============================================================
# Installation steps
# ============================================================
install_system_update() {
step "System update & firewall"
run_cmd "package update" pkg_update
ok "Package lists updated"
# Cloud-specific firewall rules (Oracle Cloud, etc.)
if $SUDO iptables -L INPUT 2>/dev/null | grep -q "REJECT"; then
run_cmd "remove REJECT rule (INPUT)" $SUDO iptables -D INPUT -j REJECT --reject-with icmp-host-prohibited 2>/dev/null || true
run_cmd "remove REJECT rule (FORWARD)" $SUDO iptables -D FORWARD -j REJECT --reject-with icmp-host-prohibited 2>/dev/null || true
if require_cmd netfilter-persistent; then
run_cmd "save firewall rules" $SUDO netfilter-persistent save >> "$LOG_FILE" 2>&1
fi
ok "Cloud firewall rules cleaned"
else
skip "No restrictive firewall rules found"
fi
}
install_swap() {
if [ "$HAS_SWAP" = "yes" ]; then
return
fi
if [ "$TOTAL_RAM_MB" -ge 8192 ]; then
return
fi
if [ -n "$IN_CONTAINER" ]; then
return # Swap is managed by the host, not the container
fi
step "Swap space"
# Calculate swap size based on RAM
if [ "$TOTAL_RAM_MB" -lt 2048 ]; then
SWAP_SIZE="4G"
elif [ "$TOTAL_RAM_MB" -lt 4096 ]; then
SWAP_SIZE="2G"
else
SWAP_SIZE="1G"
fi
if [ ! -f /swapfile ]; then
$SUDO fallocate -l "$SWAP_SIZE" /swapfile 2>/dev/null || $SUDO dd if=/dev/zero of=/swapfile bs=1M count=$((${SWAP_SIZE%G} * 1024)) >> "$LOG_FILE" 2>&1
$SUDO chmod 600 /swapfile
$SUDO mkswap /swapfile >> "$LOG_FILE" 2>&1
$SUDO swapon /swapfile
if ! grep -q '/swapfile' /etc/fstab; then
echo '/swapfile none swap sw 0 0' | $SUDO tee -a /etc/fstab > /dev/null
fi
ok "${SWAP_SIZE} swap created (RAM: ${TOTAL_RAM_GB}GB)"
else
$SUDO swapon /swapfile 2>/dev/null || true
ok "Existing swapfile activated"
fi
}
install_node() {
local need_install="no"
if [ "$HAS_NODE" = "no" ]; then
need_install="yes"
elif ! node -v 2>/dev/null | grep -q "^v2[2-9]"; then
need_install="yes"
warn "Node.js $(node -v) found but v22+ required"
fi
if [ "$need_install" = "no" ]; then
return
fi
step "Node.js 22"
if [ "$PKG_MGR" = "apt" ]; then
curl -fsSL https://deb.nodesource.com/setup_22.x | $SUDO -E bash - >> "$LOG_FILE" 2>&1
pkg_install nodejs
elif [ "$PKG_MGR" = "dnf" ] || [ "$PKG_MGR" = "yum" ]; then
curl -fsSL https://rpm.nodesource.com/setup_22.x | $SUDO bash - >> "$LOG_FILE" 2>&1
pkg_install nodejs
fi
if require_cmd node && node -v | grep -q "^v2[2-9]"; then
ok "Node.js $(node -v) installed"
else
echo -e "${RED}✗ Node.js installation failed. Check $LOG_FILE${NC}"
exit 1
fi
}
install_gh() {
[ "$HAS_GH" = "yes" ] && return
if [ -n "$SKIP_OPTIONAL" ]; then
skip "GitHub CLI (--skip-optional)"
return
fi
step "GitHub CLI"
if [ "$PKG_MGR" = "apt" ]; then
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \
| $SUDO dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg 2>/dev/null
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
| $SUDO tee /etc/apt/sources.list.d/github-cli.list > /dev/null
$SUDO apt-get update -qq >> "$LOG_FILE" 2>&1
pkg_install gh
elif [ "$PKG_MGR" = "dnf" ]; then
$SUDO dnf install -y 'dnf-command(config-manager)' >> "$LOG_FILE" 2>&1 || true
$SUDO dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo >> "$LOG_FILE" 2>&1
pkg_install gh
fi
if require_cmd gh; then
ok "GitHub CLI installed"
else
warn "GitHub CLI installation failed (optional — you can install later)"
fi
}
install_chromium() {
[ "$HAS_CHROMIUM" = "yes" ] && return
if [ -n "$SKIP_OPTIONAL" ]; then
skip "Chromium browser (--skip-optional)"
return
fi
step "Chromium browser"
if [ "$PKG_MGR" = "apt" ]; then
# Try snap first (Ubuntu), fall back to apt
if require_cmd snap; then
$SUDO snap install chromium >> "$LOG_FILE" 2>&1 && ok "Chromium installed (snap)" && return
fi
pkg_install chromium-browser && ok "Chromium installed (apt)" && return
elif [ "$PKG_MGR" = "dnf" ] || [ "$PKG_MGR" = "yum" ]; then
pkg_install chromium && ok "Chromium installed" && return
fi
warn "Chromium installation failed (optional — browser automation won't work without it)"
}
setup_chromium_env() {
# Set Puppeteer path for headless browser
local chrome_bin=""
if [ -f "/snap/chromium/current/usr/lib/chromium-browser/chrome" ]; then
chrome_bin="/snap/chromium/current/usr/lib/chromium-browser/chrome"
elif require_cmd chromium-browser; then
chrome_bin=$(which chromium-browser)
elif require_cmd chromium; then
chrome_bin=$(which chromium)
fi
if [ -n "$chrome_bin" ]; then
if ! grep -q PUPPETEER_EXECUTABLE_PATH ~/.bashrc 2>/dev/null; then
echo "export PUPPETEER_EXECUTABLE_PATH=\"$chrome_bin\"" >> ~/.bashrc
fi
fi
}
install_clawdbot() {
[ "$HAS_CLAWDBOT" = "yes" ] && return
step "Clawdbot"
$SUDO npm install -g clawdbot --loglevel=error >> "$LOG_FILE" 2>&1
if require_cmd clawdbot; then
ok "OpenClaw $(clawdbot --version 2>/dev/null || echo '') installed"
else
echo -e "${RED}✗ Clawdbot installation failed. Check $LOG_FILE${NC}"
exit 1
fi
}
setup_workspace() {
step "Workspace & templates"
mkdir -p "$WORKSPACE/memory"
mkdir -p "$CONFIG_DIR"
# Try local references first (git clone), fall back to GitHub download
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd 2>/dev/null || echo "")"
LOCAL_REF="${SCRIPT_DIR:+$SCRIPT_DIR/../references}"
[ -n "$LOCAL_REF" ] && [ ! -d "$LOCAL_REF" ] && LOCAL_REF=""
download_ref() {
local file="$1" dest="$2"
[ -f "$dest" ] && { skip "$file (already exists)"; return 0; }
if [ -n "${LOCAL_REF:-}" ] && [ -f "$LOCAL_REF/$file" ]; then
cp "$LOCAL_REF/$file" "$dest"
else
if ! curl -fsSL "$REPO_RAW/$file" -o "$dest" 2>> "$LOG_FILE"; then
warn "Failed to download $file"
return 1
fi
fi
ok "$file created"
}
for f in SOUL.md IDENTITY.md USER.md AGENTS.md; do
download_ref "$f" "$WORKSPACE/$f"
done
if [ ! -f "$CONFIG_DIR/clawdbot.json" ]; then
download_ref "openclaw-template.json" "$CONFIG_DIR/clawdbot.json"
sed -i "s|\$HOME|$HOME|g" "$CONFIG_DIR/clawdbot.json"
ok "Config template created at $CONFIG_DIR/clawdbot.json"
else
skip "Config already exists at $CONFIG_DIR/clawdbot.json"
fi
}
# ============================================================
# Interactive configuration wizard
# ============================================================
config_wizard() {
step "Configuration wizard"
local config_file="$CONFIG_DIR/clawdbot.json"
# Skip if not interactive or config already customized
if [ -n "$SKIP_INTERACTIVE" ]; then
info "Non-interactive mode — skipping wizard"
info "Edit $config_file manually to add your keys"
return
fi
# Check if config still has placeholder values
if ! grep -q '\$LLM_API_KEY' "$config_file" 2>/dev/null; then
skip "Config already customized"
return
fi
echo ""
echo -e " ${BOLD}Let's configure your AI team.${NC}"
echo -e " ${DIM}(Press Enter to skip any step — you can edit later)${NC}"
echo ""
# --- LLM Provider ---
echo -e " ${BOLD}LLM Provider${NC}"
echo -e " ${DIM}Which AI provider? (1=Anthropic, 2=OpenAI, 3=Other)${NC}"
read -r -p " Choice [1]: " provider_choice
provider_choice="${provider_choice:-1}"
local provider_name="" base_url="" api_format=""
case "$provider_choice" in
1)
provider_name="anthropic"
base_url="https://api.anthropic.com/v1"
api_format="anthropic"
;;
2)
provider_name="openai"
base_url="https://api.openai.com/v1"
api_format="openai"
;;
3)
read -r -p " Provider name: " provider_name
read -r -p " Base URL: " base_url
read -r -p " API format (openai/anthropic): " api_format
;;
esac
if [ -n "$provider_name" ]; then
# Replace provider placeholders
sed -i "s|\\\$LLM_PROVIDER|$provider_name|g" "$config_file"
sed -i "s|\\\$LLM_BASE_URL|$base_url|g" "$config_file"
sed -i "s|\\\$LLM_API_FORMAT|$api_format|g" "$config_file"
ok "Provider: $provider_name"
fi
# --- API Key ---
echo ""
echo -e " ${BOLD}API Key${NC}"
echo -e " ${DIM}Paste your LLM API key (input hidden):${NC}"
read -r -s -p " API Key: " api_key
echo ""
if [ -n "$api_key" ]; then
validate_api_key "$api_key" "${provider_name:-}" || true
# Escape special characters for sed
local escaped_key
escaped_key=$(printf '%s\n' "$api_key" | sed 's/[&/\]/\\&/g')
sed -i "s|\\\$LLM_API_KEY|$escaped_key|g" "$config_file"
ok "API key saved"
else
info "Skipped — add it later in $config_file"
fi
# --- Model Selection ---
echo ""
echo -e " ${BOLD}Model Selection${NC}"
echo -e " ${DIM}Which model should your agents use?${NC}"
echo ""
case "${provider_name:-anthropic}" in
anthropic)
echo -e " ${DIM}Popular Anthropic models:${NC}"
echo -e " 1) \$STRONG_MODEL (best quality — recommended for most agents)"
echo -e " 2) \$FAST_MODEL (faster & cheaper — good for Chief of Staff)"
echo -e " 3) Custom (enter a model name)"
read -r -p " Default model [1]: " model_choice
model_choice="${model_choice:-1}"
case "$model_choice" in
1) CHOSEN_MODEL="\$STRONG_MODEL" ;;
2) CHOSEN_MODEL="\$FAST_MODEL" ;;
3)
read -r -p " Model name: " CHOSEN_MODEL
;;
*) CHOSEN_MODEL="\$STRONG_MODEL" ;;
esac
;;
openai)
echo -e " ${DIM}Popular OpenAI models:${NC}"
echo -e " 1) \$STRONG_MODEL (best quality)"
echo -e " 2) \$FAST_MODEL (faster & cheaper)"
echo -e " 3) Custom (enter a model name)"
read -r -p " Default model [1]: " model_choice
model_choice="${model_choice:-1}"
case "$model_choice" in
1) CHOSEN_MODEL="\$STRONG_MODEL" ;;
2) CHOSEN_MODEL="\$FAST_MODEL" ;;
3)
read -r -p " Model name: " CHOSEN_MODEL
;;
*) CHOSEN_MODEL="\$STRONG_MODEL" ;;
esac
;;
*)
read -r -p " Default model name: " CHOSEN_MODEL
;;
esac
if [ -n "${CHOSEN_MODEL:-}" ]; then
sed -i "s|\\\$DEFAULT_MODEL|$CHOSEN_MODEL|g" "$config_file"
ok "Default model: $CHOSEN_MODEL"
info "Tip: Change per-agent models later in $config_file (use cheaper models for simple tasks)"
fi
# --- Discord Guild ID ---
echo ""
echo -e " ${BOLD}Discord Server ID${NC}"
echo -e " ${DIM}Right-click your server → Copy Server ID (enable Developer Mode in Discord settings)${NC}"
read -r -p " Guild ID: " guild_id
if [ -n "$guild_id" ]; then
sed -i "s|\\\$DISCORD_GUILD_ID|$guild_id|g" "$config_file"
ok "Guild ID: $guild_id"
else
info "Skipped — add it later in $config_file"
fi
# --- Discord Bot Tokens ---
local agents=("chief-of-staff" "engineering" "marketing" "finance" "devops" "legal" "management")
local agent_names=("Chief of Staff" "Engineering" "Marketing" "Finance" "DevOps" "Legal" "Management")
echo ""
echo -e " ${BOLD}Discord Bot Tokens${NC}"
echo -e " ${DIM}Paste each bot token (Enter to skip). You need 7 bots from discord.com/developers.${NC}"
echo ""
local tokens_set=0
for i in "${!agents[@]}"; do
read -r -s -p " ${agent_names[$i]} token: " token
echo ""
if [ -n "$token" ]; then
local escaped_token
escaped_token=$(printf '%s\n' "$token" | sed 's/[&/\]/\\&/g')
sed -i "s|\\\$DISCORD_TOKEN_${agents[$i]^^}|$escaped_token|g" "$config_file" 2>/dev/null || true
# Also handle the simpler placeholder pattern
sed -i "s|\"token\": \"\\\$DISCORD_BOT_TOKEN_$((i+1))\"|\"token\": \"$escaped_token\"|" "$config_file" 2>/dev/null || true
tokens_set=$((tokens_set + 1))
fi
done
if [ "$tokens_set" -gt 0 ]; then
ok "$tokens_set of 7 bot tokens configured"
else
info "No tokens entered — add them in $config_file"
fi
# --- Notion Token (optional) ---
echo ""
echo -e " ${BOLD}Notion Integration (optional)${NC}"
echo -e " ${DIM}Create at notion.so/my-integrations. Enables auto-reports, knowledge base.${NC}"
read -r -s -p " Notion token: " notion_token
echo ""
if [ -n "$notion_token" ]; then
validate_notion_token "$notion_token" || true
# Store in workspace env file for agents to pick up
local env_file="$WORKSPACE/.env"
if [ -n "$DRY_RUN" ]; then
echo -e " ${DIM}[dry-run] would write NOTION_TOKEN to $env_file${NC}"
else
mkdir -p "$WORKSPACE"
# Append or update NOTION_TOKEN
if grep -q '^NOTION_TOKEN=' "$env_file" 2>/dev/null; then
sed -i "s|^NOTION_TOKEN=.*|NOTION_TOKEN=$notion_token|" "$env_file"
else
echo "NOTION_TOKEN=$notion_token" >> "$env_file"
fi
chmod 600 "$env_file"
ok "Notion token saved to $env_file"
fi
else
info "Skipped — add NOTION_TOKEN to $WORKSPACE/.env later"
fi
# --- GitHub CLI Auth (optional) ---
echo ""
echo -e " ${BOLD}GitHub CLI (optional)${NC}"
echo -e " ${DIM}Authenticate gh CLI for Issues, PRs, CI/CD automation.${NC}"
if require_cmd gh; then
if gh auth status &>/dev/null; then
ok "GitHub CLI already authenticated"
else
echo -e " ${DIM}Run 'gh auth login' after setup to authenticate (requires browser or token).${NC}"
info "Skipping — authenticate with: gh auth login"
fi
else
info "gh CLI not installed yet — will be available after setup"
fi
echo ""
info "Config saved to: $config_file"
info "You can always edit it later: nano $config_file"
}
# ============================================================
# Gateway service
# ============================================================
install_gateway() {
step "Gateway service"
if ! require_cmd clawdbot; then
warn "OpenClaw/Clawdbot not found — install it first, then run 'clawdbot gateway install'"
return
fi
# Containers usually lack systemd — start gateway directly instead
if [ -n "$IN_CONTAINER" ]; then
info "Container detected — systemd service not available"
info "Start manually: clawdbot gateway start"
info "Or add to your container entrypoint / supervisor config"
ok "Gateway ready (manual start required in containers)"
return
fi
if run_cmd "install gateway service" clawdbot gateway install >> "$LOG_FILE" 2>&1; then
ok "Gateway service installed (auto-starts on boot)"
info "Start: systemctl --user start openclaw-gateway"
info "Logs: journalctl --user -u openclaw-gateway -f"
else
warn "Gateway service install failed — run 'clawdbot gateway install' after filling in config"
fi
}
# ============================================================
# Post-install health check
# ============================================================
health_check() {
step "Post-install health check"
local pass=0 fail=0
# Node.js
if require_cmd node && node -e "process.exit(0)" 2>/dev/null; then
ok "Node.js works ($(node -v))"
pass=$((pass + 1))
else
warn "Node.js not functional"
fail=$((fail + 1))
fi
# npm
if require_cmd npm && npm --version &>/dev/null; then
ok "npm works (v$(npm --version 2>/dev/null))"
pass=$((pass + 1))
else
warn "npm not functional"
fail=$((fail + 1))
fi
# Clawdbot
if require_cmd clawdbot && clawdbot --version &>/dev/null; then
ok "OpenClaw works ($(clawdbot --version 2>/dev/null))"
pass=$((pass + 1))
else
warn "Clawdbot not functional"
fail=$((fail + 1))
fi
# Config file valid JSON
local config_file="$CONFIG_DIR/clawdbot.json"
if [ -f "$config_file" ]; then
if node -e "JSON.parse(require('fs').readFileSync('$config_file','utf8'))" 2>/dev/null; then
ok "Config JSON is valid"
pass=$((pass + 1))
else
warn "Config JSON has syntax errors — run: node -e \"JSON.parse(require('fs').readFileSync('$config_file','utf8'))\""
fail=$((fail + 1))
fi
else
warn "Config file not found at $config_file"
fail=$((fail + 1))
fi
# Workspace files
local expected_files=("SOUL.md" "IDENTITY.md" "USER.md" "AGENTS.md")
local ws_ok=0 ws_miss=0
for f in "${expected_files[@]}"; do
if [ -f "$WORKSPACE/$f" ]; then
ws_ok=$((ws_ok + 1))
else
ws_miss=$((ws_miss + 1))
fi
done
if [ "$ws_miss" -eq 0 ]; then
ok "Workspace files present (${ws_ok}/${#expected_files[@]})"
pass=$((pass + 1))
else
warn "Missing workspace files ($ws_miss/${#expected_files[@]})"
fail=$((fail + 1))
fi
# Chromium (optional)
if require_cmd chromium-browser || require_cmd chromium; then
ok "Chromium available (browser automation ready)"
pass=$((pass + 1))
else
info "Chromium not found (browser automation unavailable — optional)"
fi
# GitHub CLI (optional)
if require_cmd gh; then
if gh auth status &>/dev/null 2>&1; then
ok "GitHub CLI authenticated"
else
info "GitHub CLI installed but not authenticated (run: gh auth login)"
fi
pass=$((pass + 1))
else
info "GitHub CLI not found (optional)"
fi
echo ""
if [ "$fail" -eq 0 ]; then
echo -e " ${GREEN}${BOLD}Health check: $pass/$pass passed ✓${NC}"
else
echo -e " ${YELLOW}${BOLD}Health check: $pass passed, $fail failed${NC}"
echo -e " ${DIM}Fix the warnings above, then re-run: bash setup.sh --check${NC}"
fi
log "Health check: pass=$pass fail=$fail"
}
# ============================================================
# Upgrade mode — update existing installation
# ============================================================
do_upgrade() {
echo ""
echo -e "${BLUE}${BOLD}╔══════════════════════════════════════╗${NC}"
echo -e "${BLUE}${BOLD}║ 🔄 Become CEO — Upgrade v${SETUP_VERSION} ║${NC}"
echo -e "${BLUE}${BOLD}╚══════════════════════════════════════╝${NC}"
echo ""
log "=== Upgrade started at $(date) ==="
STEP_TOTAL=4
require_root_or_sudo
# Step 1: Backup existing config
step "Backup existing configuration"
backup_config
# Step 2: Update Clawdbot
step "Update Clawdbot to latest"
if require_cmd clawdbot; then
local old_ver
old_ver=$(clawdbot --version 2>/dev/null || echo "unknown")
run_cmd "npm update clawdbot" $SUDO npm update -g clawdbot --loglevel=error >> "$LOG_FILE" 2>&1
local new_ver
new_ver=$(clawdbot --version 2>/dev/null || echo "unknown")
if [ "$old_ver" = "$new_ver" ]; then
ok "OpenClaw already at latest ($new_ver)"
else
ok "OpenClaw updated: $old_ver → $new_ver"
fi
else
warn "Clawdbot not installed — run setup without --upgrade first"
fi
# Step 3: Re-download reference templates (without overwriting existing)
step "Update reference templates"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd 2>/dev/null || echo "")"
LOCAL_REF="${SCRIPT_DIR:+$SCRIPT_DIR/../references}"
[ -n "$LOCAL_REF" ] && [ ! -d "$LOCAL_REF" ] && LOCAL_REF=""
# Download to a .new file, let user diff if different
local ref_dir="$WORKSPACE/.reference-updates"
mkdir -p "$ref_dir"
for f in SOUL.md IDENTITY.md USER.md AGENTS.md; do
local dest="$ref_dir/$f"
if [ -n "${LOCAL_REF:-}" ] && [ -f "$LOCAL_REF/$f" ]; then
cp "$LOCAL_REF/$f" "$dest"
else
curl -fsSL "$REPO_RAW/$f" -o "$dest" 2>> "$LOG_FILE" || continue
fi
if [ -f "$WORKSPACE/$f" ]; then
if diff -q "$WORKSPACE/$f" "$dest" &>/dev/null; then
skip "$f (unchanged)"
else
ok "$f has updates → saved to .reference-updates/$f"
info "Compare: diff $WORKSPACE/$f $ref_dir/$f"
fi
else
cp "$dest" "$WORKSPACE/$f"
ok "$f created"
fi
done
# Step 4: Health check
health_check
echo ""
echo -e "${GREEN}${BOLD}╔══════════════════════════════════════╗${NC}"
echo -e "${GREEN}${BOLD}║ ✅ Upgrade Complete! (v${SETUP_VERSION}) ║${NC}"
echo -e "${GREEN}${BOLD}╚══════════════════════════════════════╝${NC}"
echo ""