diff --git a/Makefile b/Makefile index 0bda545..b4b6602 100644 --- a/Makefile +++ b/Makefile @@ -19,8 +19,10 @@ TARGET := $(or $(CARGO_TARGET_DIR),target) APPDIR ?= $(HOME)/Applications BIN := $(TARGET)/release/anybot RELAY := $(if $(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR),relay/target)/release/anybot-relay +# make update PULL=0: install this checkout as it is, without pulling the newest first. +PULL ?= 1 -.PHONY: all build app relay run serve setup relay-run test deps doctor install uninstall hands server dist clean help check-rust lint-sh +.PHONY: all build app relay run serve setup relay-run test deps doctor install uninstall update update-app update-hands update-server hands server dist clean help check-rust lint-sh all: build @@ -31,6 +33,13 @@ help: @echo " make run 直接打開 AnyBot(不安裝)" @echo " make install 安裝:Mac → $(APPDIR)/AnyBot.app;Linux → ~/.local/bin/anybot 和選單" @echo " make uninstall 移除安裝的程式(你的 Bot 和聊天不會刪)" + @echo "" + @echo " make update 更新這台裝的 AnyBot(伺服器、App、手腳,自己認)" + @echo " make update-app 只更新桌面 App(有背景服務也一起重新啟動)" + @echo " make update-hands 只更新 Linux 手腳(換新程式、重新啟動背景服務)" + @echo " make update-server 只更新伺服器(帳號伺服器 + 大腦,Docker)" + @echo " 加 PULL=0:不拉新版,裝現在這份原始碼" + @echo "" @echo " make deps 裝好建置需要的東西:系統套件和 Rust(Linux 會問 sudo 密碼)" @echo " make doctor 檢查這台缺什麼" @echo " make test 跑測試" @@ -124,6 +133,19 @@ endif hands: app sh deploy/linux/hands.sh --deps +# Pull, rebuild, swap the installed program, restart what runs in the background (deploy/update.sh). +update: + @PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh auto + +update-app: + @PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh app + +update-hands: + @PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh hands + +update-server: + @PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh server + server: sh ./install.sh server diff --git a/deploy/update.sh b/deploy/update.sh new file mode 100755 index 0000000..e3576ec --- /dev/null +++ b/deploy/update.sh @@ -0,0 +1,201 @@ +#!/bin/sh +# 更新 AnyBot:拉下最新的原始碼、重新編譯、換掉裝好的程式,再重新啟動在背景跑的部分。 +# 你的 Bot、聊天和設定不在程式裡,更新不會動到它們。 +# +# make update 這台裝了什麼就更新什麼(伺服器、App、手腳,自己認) +# make update-app 桌面 App:Mac → ~/Applications/AnyBot.app;Linux → ~/.local/bin/anybot +# make update-hands Linux 手腳:換新程式、重新啟動背景服務,確認它有起來 +# make update-server 伺服器(帳號伺服器 + 大腦):重建 Docker 映像並重新啟動 +# +# PULL=0 make update-app 不拉新版,裝現在這份原始碼(自己改過程式時) +# +# 不用 make 也可以:sh deploy/update.sh [auto|app|hands|server] +set -eu +cd "$(dirname "$0")/.." +ROOT=$(pwd) +MAKE=${MAKE:-make} +PULL=${PULL:-1} +WHAT=${1:-auto} + +if [ -t 1 ]; then B=$(printf '\033[1m'); D=$(printf '\033[90m'); G=$(printf '\033[32m'); Y=$(printf '\033[33m'); R=$(printf '\033[31m'); C=$(printf '\033[36m'); N=$(printf '\033[0m') +else B=; D=; G=; Y=; R=; C=; N=; fi +step() { printf '\n%s▸%s %s%s%s\n' "$C" "$N" "$B" "$1" "$N"; } +ok() { printf ' %s✓%s %s\n' "$G" "$N" "$1"; } +info() { printf ' %s%s%s\n' "$D" "$1" "$N"; } +warn() { printf ' %s!%s %s\n' "$Y" "$N" "$1"; } +die() { printf '\n %s✗ %s%s\n' "$R" "$1" "$N"; [ $# -gt 1 ] && printf ' %s\n' "$2"; exit 1; } +have() { command -v "$1" >/dev/null 2>&1; } + +case $WHAT in + auto|app|hands|server) ;; + -h|--help|help) sed -n '2,12p' "$ROOT/deploy/update.sh" | sed 's/^# \{0,1\}//'; exit 0 ;; + *) die "不認得「${WHAT}」。" "可以用:auto、app、hands、server" ;; +esac + +case "$(uname -s 2>/dev/null || echo unknown)" in + Darwin) PLAT=mac ;; + Linux) PLAT=linux ;; + *) PLAT=other ;; +esac +# systemctl --user as root (a server's hands) needs its runtime folder, as install.sh sets up. +if [ "$PLAT" = linux ] && [ "$(id -u)" = 0 ]; then + export XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR:-/run/user/0} +fi + +APP_BIN=$HOME/.local/bin/anybot +[ "$PLAT" = mac ] && APP_BIN=$HOME/Applications/AnyBot.app/Contents/MacOS/AnyBot +LAUNCHER=$HOME/.local/bin/anybot-run +MAC_SERVICE=$HOME/Library/LaunchAgents/com.anybot.serve.plist + +# ---------- 原始碼 ---------- +pulled=${ANYBOT_UPDATE_PULLED:-0} +pull() { + [ "$pulled" = 1 ] && return 0 + pulled=1 + if [ "$PULL" = 0 ]; then + info "不拉新版(PULL=0),裝現在這份原始碼" + return 0 + fi + if ! have git || [ ! -d "$ROOT/.git" ]; then + warn "這份原始碼不是用 git 下載的,沒辦法拉新版,用現有的版本" + return 0 + fi + old=$(git -C "$ROOT" rev-parse --short HEAD) + git -C "$ROOT" pull --ff-only -q || die "沒辦法拉下新版。" "你改過的檔案跟新版衝突,或這個分支跟遠端分岔了。處理好再試;或用 PULL=0 裝現在這份。" + new=$(git -C "$ROOT" rev-parse --short HEAD) + if [ "$old" = "$new" ]; then + ok "原始碼已經是最新的(${new})" + return 0 + fi + ok "原始碼 ${old} → ${new}" + git -C "$ROOT" log --oneline "${old}..${new}" | head -n 10 | sed 's/^/ /' + # This script may have changed too: carry on with the new one. + if ! git -C "$ROOT" diff --quiet "$old" "$new" -- deploy/update.sh; then + info "更新程式本身也改了,用新版繼續" + ANYBOT_UPDATE_PULLED=1 exec sh "$ROOT/deploy/update.sh" "$WHAT" + fi +} + +# ---------- 這台的程式(App、手腳)---------- +installed=0 +install_local() { + [ "$installed" = 1 ] && return 0 + installed=1 + [ "$PLAT" = other ] && die "這支只更新 Mac 和 Linux。" "Windows:再執行一次 install.ps1(README 裡那一行)。" + # Small computers run out of memory compiling many crates at once: one compile per 2 GB. + if [ -z "${CARGO_BUILD_JOBS:-}" ] && [ -r /proc/meminfo ]; then + mem=$(awk '/MemTotal/ {m=$2} /SwapTotal/ {s=$2} END {print int((m + s / 2) / 1024)}' /proc/meminfo) + j=$((mem / 2000)); [ "$j" -lt 1 ] && j=1 + export CARGO_BUILD_JOBS=$j + fi + info "編譯中(只重編改過的部分,通常 1–2 分鐘)…" + log=$ROOT/target/update-build.log + mkdir -p "$ROOT/target" + if "$MAKE" -C "$ROOT" --no-print-directory install >"$log" 2>&1; then + ok "裝好新版:${APP_BIN}" + else + tail -n 25 "$log" | sed 's/^/ /' + die "編譯或安裝失敗(完整紀錄:${log})" "缺東西的話先 make deps;Rust 太舊:rustup update stable。" + fi +} + +# Set up by `anybot setup` to start with the computer. +service_on() { + if [ "$PLAT" = mac ]; then [ -f "$MAC_SERVICE" ]; else [ -f "$LAUNCHER" ]; fi +} + +refresh_service() { + if out=$("$APP_BIN" setup --refresh-service 2>&1); then + ok "背景服務換成新版、重新啟動了" + else + printf '%s\n' "$out" | sed 's/^/ /' + die "背景服務沒能重新啟動。" "Linux 看紀錄:journalctl --user -u anybot -n 50" + fi +} + +# The window doesn't swap itself: it keeps running the old copy until it is reopened. +window_open() { + ps -A -o args= 2>/dev/null | grep -v -e '--serve' -e 'grep' | grep -Eq 'AnyBot\.app/Contents/MacOS/|(^|/)anybot$' +} + +update_app() { + step "桌面 App" + pull + install_local + service_on && refresh_service + if window_open; then + warn "AnyBot 開著:結束再打開,才會換成新版" + fi +} + +update_hands() { + if [ "$PLAT" = mac ]; then + info "Mac 的手腳就是 AnyBot.app,跟 App 一起更新" + update_app + return 0 + fi + [ "$PLAT" = linux ] || die "這支只更新 Mac 和 Linux。" "Windows:再執行一次 install.ps1(README 裡那一行)。" + step "手腳(Linux)" + [ -x "$APP_BIN" ] || die "這台還沒裝手腳。" "第一次裝:sh install.sh hands 網址 配對碼" + pull + install_local + if ! service_on; then + warn "這台還沒接到大腦,沒有背景服務可以重新啟動。接上:anybot setup 網址 配對碼" + return 0 + fi + refresh_service + # A unit that fails at once restarts in a loop and still looks "active" for a moment. + sleep 3 + if systemctl --user is-active --quiet anybot 2>/dev/null; then + ok "手腳在跑,大腦看得到這台" + else + die "背景服務沒有起來。" "看紀錄:journalctl --user -u anybot -n 50" + fi +} + +# ---------- 伺服器(Docker)---------- +update_server() { + step "伺服器(帳號伺服器 + 大腦)" + srv=$ROOT/deploy/server + [ -f "$srv/.env" ] || die "這台還沒架伺服器。" "第一次架:make server" + have docker || die "找不到 Docker。" "伺服器跑在 Docker 裡:先裝好 Docker,或再執行一次 make server。" + dk=docker + if ! docker info >/dev/null 2>&1; then + if [ "$PLAT" = linux ] && have sudo && sudo docker info >/dev/null 2>&1; then dk="sudo docker" + else die "沒辦法使用 Docker。" "Docker 有在跑嗎?試試:sudo systemctl start docker"; fi + fi + pull + if [ -z "${ANYBOT_BUILD_JOBS:-}" ] && [ -r /proc/meminfo ]; then + mem=$(awk '/MemTotal/ {m=$2} /SwapTotal/ {s=$2} END {print int((m + s / 2) / 1024)}' /proc/meminfo) + j=$((mem / 2000)); [ "$j" -lt 1 ] && j=1 + export ANYBOT_BUILD_JOBS=$j + fi + info "重建映像並重新啟動(沒改到的部分很快就過)…" + # .env names the compose files (Tailscale, LAN) and the Docker socket, as setup.sh wrote them. + (cd "$srv" && $dk compose up -d --build) || die "建置或啟動失敗。" "看紀錄:cd ${srv} && docker compose logs --tail 50" + ok "伺服器換成新版了" + (cd "$srv" && $dk compose ps --format ' {{.Service}} {{.Status}}' 2>/dev/null) || true + info "看紀錄:cd ${srv} && docker compose logs -f anybot" +} + +# ---------- 這台裝了什麼 ---------- +is_hands() { + [ -f "$LAUNCHER" ] && grep -q "accessibility bus" "$LAUNCHER" +} + +case $WHAT in + app) update_app ;; + hands) update_hands ;; + server) update_server ;; + auto) + found=0 + if [ -f "$ROOT/deploy/server/.env" ]; then update_server; found=1; fi + if [ "$PLAT" = mac ] && [ -d "$HOME/Applications/AnyBot.app" ]; then update_app; found=1; fi + if [ "$PLAT" = linux ] && [ -x "$APP_BIN" ]; then + if is_hands; then update_hands; else update_app; fi + found=1 + fi + [ "$found" = 1 ] || die "這台還沒裝 AnyBot,沒有東西可以更新。" "第一次裝:sh install.sh(桌面 App、手腳、伺服器都從這裡開始)" + ;; +esac +printf '\n %s更新好了。%s你的 Bot、聊天和設定都還在。\n' "$G$B" "$N" diff --git a/docs/en/install.md b/docs/en/install.md index ace8268..54e6fe6 100644 --- a/docs/en/install.md +++ b/docs/en/install.md @@ -15,7 +15,7 @@ Canonical document: [docs/install.md](../install.md) (Chinese). ## Make targets -`make deps` (system packages + Rust, one time) · `make` (build) · `make install` (Mac → `~/Applications/AnyBot.app`, Linux → `~/.local/bin/anybot` + menu entry) · `make run` · `make test` · `make doctor` (what's missing on this machine) · `make serve` (headless) · `make setup` (pair this machine to your brain) · `make server` (host the account server here) · `make dist` (release archives into `dist/`) · `make relay` (build the relay alone). +`make deps` (system packages + Rust, one time) · `make` (build) · `make install` (Mac → `~/Applications/AnyBot.app`, Linux → `~/.local/bin/anybot` + menu entry) · `make run` · `make test` · `make doctor` (what's missing on this machine) · `make serve` (headless) · `make setup` (pair this machine to your brain) · `make server` (host the account server here) · `make update` (pull, rebuild, swap what's installed here and restart its background service; or one at a time: `update-app`, `update-hands`, `update-server`; `PULL=0` installs this checkout without pulling) · `make dist` (release archives into `dist/`) · `make relay` (build the relay alone). ## FAQ diff --git a/docs/install.md b/docs/install.md index d3271e4..ff161fa 100644 --- a/docs/install.md +++ b/docs/install.md @@ -70,13 +70,25 @@ make install ## 更新 -再執行一次安裝的那一行就好。自己建置的話: +再執行一次安裝的那一行就好。或在原始碼資料夾(預設 `~/AnyBot`)裡: ```sh -git pull -make install +make update # 這台裝了什麼就更新什麼(伺服器、App、手腳,自己認) +make update-app # 只更新桌面 App +make update-hands # 只更新 Linux 手腳 +make update-server # 只更新伺服器(帳號伺服器 + 大腦) ``` +每一個都會先拉新版(`git pull`)、重新編譯、換掉裝好的程式,再重新啟動在背景跑的部分: + +| | 換掉什麼 | 重新啟動什麼 | +|---|---|---| +| App | Mac:`~/Applications/AnyBot.app`;Linux:`~/.local/bin/anybot` | 有設成開機自動執行的話,那個背景服務。開著的視窗要自己關掉再打開 | +| 手腳 | `~/.local/bin/anybot` | 背景服務(systemd 的 `anybot`),並確認它有起來 | +| 伺服器 | Docker 映像 | `deploy/server` 裡的容器(照 `.env` 的設定,Tailscale、區網都一樣) | + +自己改過程式、不想拉新版:`make update-app PULL=0`(裝現在這份原始碼)。Mac 的手腳就是 App,`make update-hands` 在 Mac 上等於 `make update-app`。 + 你的 Bot、聊天和設定不在程式裡,更新不會動到它們。 ## 移除