diff --git a/Makefile b/Makefile index b4b6602..fac342d 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ RELAY := $(if $(CARGO_TARGET_DIR),$(CARGO_TARGET_DIR),relay/target)/release/an # 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 update update-app update-hands update-server 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 server-logs hands server dist clean help check-rust lint-sh all: build @@ -49,7 +49,9 @@ help: @echo " make setup 把這台接到你的 AnyBot(終端機裡的引導畫面)" @echo " make hands Linux:裝好虛擬螢幕等,再接成大腦的手腳" @echo " make relay-run 在這台跑帳號伺服器(埠 8080,資料在 ./relay-data)" - @echo " make server 在伺服器上架好帳號伺服器 + 大腦 + HTTPS(Docker)" + @echo " make server 在伺服器上架好帳號伺服器 + 大腦 + HTTPS(Docker);再跑一次可以改設定" + @echo " make server-logs 看伺服器上大腦的紀錄" + @echo " 伺服器的設定只在 deploy/server/.env;改了之後 make update-server" @echo "" @echo " make dist 打包發行檔到 dist/(Mac:.app 的 zip;Linux:三個 tar.gz)" @echo " make clean 清掉建置結果" @@ -146,6 +148,9 @@ update-hands: update-server: @PULL=$(PULL) MAKE="$(MAKE)" sh deploy/update.sh server +server-logs: + @bash deploy/server/setup.sh --logs + server: sh ./install.sh server diff --git a/deploy/server/.env.example b/deploy/server/.env.example index e5dda00..73fd1b8 100644 --- a/deploy/server/.env.example +++ b/deploy/server/.env.example @@ -1,4 +1,5 @@ -# Copy to .env and fill in. +# make server writes .env for you (or copy this to .env and fill it in). This file is the only +# place the server's settings live: change it, then make update-server. ANYBOT_USER=daniel ANYBOT_PASSWORD=change-me-please ANYBOT_HOST_NAME=雲端 AnyBot @@ -13,8 +14,14 @@ GEMINI_API_KEY= # Where the brain downloads the local model for memory search (default Hugging Face), e.g. # https://hf-mirror.com when huggingface.co can't be reached from this server. #ANYBOT_HF_BASE= -# Public address, e.g. https://anybot.example.com +# Public address, e.g. https://anybot.example.com. With Tailscale (ANYBOT_TAILSCALE=1) the port in +# it is the HTTPS port `tailscale serve` gets, e.g. https://name.tailnet.ts.net:8443; none = 443. ANYBOT_RELAY_PUBLIC_URL= +#ANYBOT_TAILSCALE=1 +#COMPOSE_FILE=docker-compose.yml:docker-compose.tailscale.yml +# The Docker this server lives on (written by make server at the first setup), e.g. +# /run/user/1000/docker.sock for rootless Docker. Every start uses it, whatever plain `docker` reaches. +#DOCKER_SOCKET=/var/run/docker.sock # The local network's own address with HTTPS (https://10.0.0.25), certificate signed by this # server itself: set it and add docker-compose.lan.yml to COMPOSE_FILE (setup.sh 「區網 IP」 does both). #ANYBOT_LAN_IP=10.0.0.25 diff --git a/deploy/server/docker-compose.yml b/deploy/server/docker-compose.yml index e347194..2d19b39 100644 --- a/deploy/server/docker-compose.yml +++ b/deploy/server/docker-compose.yml @@ -1,10 +1,10 @@ # Everything on one small server: the account server (relay), HTTPS (Caddy), and AnyBot itself. -# Easiest: ./setup.sh (asks for the domain, account and keys, writes .env and Caddyfile, starts -# everything and checks it). By hand: -# 1. Point your domain at the server; copy ../../relay/Caddyfile here and put the domain in it. -# 2. Put your account name and password in .env (see .env.example). -# 3. docker compose up -d -# 4. On the phone open https://your-domain, create the account (first time), and use it. +# Set up: make server (= ./setup.sh: asks for the address, account and keys, writes .env and +# Caddyfile, starts everything and checks it). Settings live only in .env; after changing it, or +# to update: make update-server. Logs: make server-logs. +# Don't start this with a bare `docker compose up`: on a host with both rootless and root Docker +# that can reach the daemon without this server's data and bring up a second, empty server; the +# make targets always use the one in .env (DOCKER_SOCKET) and the HTTPS port in its address. # AI keys: set them in .env, or set ANYBOT_RESTORE_FROM_ACCOUNT=1 to start from the newest backup # your Mac made (bots, chats, routines and the keys you set there). # @@ -118,9 +118,9 @@ services: NETWORKS: "1" # inspect/attach (docker run needs the bridge network) POST: "1" GET: "1" - # On a rootless-Docker host this has to be DOCKER_SOCKET=/run/user//docker.sock in .env: - # setup.sh finds it and writes it (also into a kept .env). The rootful default is - # /var/run/docker.sock; on a rootless host that is another daemon and the proxy answers 503. + # DOCKER_SOCKET in .env: the Docker this server lives on (setup.sh writes it at the first setup, + # e.g. /run/user//docker.sock for rootless Docker, and starts compose on that same one). + # The default /var/run/docker.sock is the root daemon; on a rootless host the proxy answers 503. volumes: - ${DOCKER_SOCKET:-/var/run/docker.sock}:/var/run/docker.sock:ro volumes: diff --git a/deploy/server/setup.sh b/deploy/server/setup.sh index 02c4924..91aca59 100755 --- a/deploy/server/setup.sh +++ b/deploy/server/setup.sh @@ -1,9 +1,16 @@ #!/usr/bin/env bash # AnyBot 伺服器設定: the account server, HTTPS and the brain on this server, step by step. # -# cd app/deploy/server && ./setup.sh ask, write .env and Caddyfile, start, check +# make server (= ./setup.sh) ask, write .env and Caddyfile, start, check +# make update-server (= ./setup.sh --start after git pull) start with .env as it is: no questions +# make server-logs (= ./setup.sh --logs) follow the brain's log # ./setup.sh --dry-run ask and write the files, but don't start anything # +# The one place this server's settings live is .env (written here; edit it, then make update-server). +# Every start goes through this script, so it always comes up on the same Docker (DOCKER_SOCKET), +# with the same address and HTTPS port (ANYBOT_RELAY_PUBLIC_URL). Plain `docker compose` from a +# shell may reach another Docker daemon; use the make targets. +# # Run it again any time: it offers to keep the current settings (to update and restart). # # Without questions (scripts, Ansible…): give the answers as environment variables. @@ -14,10 +21,42 @@ # An existing setup is kept (updated and restarted). set -u cd "$(dirname "$0")" -DRY=0 -[ "${1:-}" = "--dry-run" ] && DRY=1 +DRY=0; START=0; LOGS=0 +case "${1:-}" in + --dry-run) DRY=1 ;; + --start) START=1 ;; + --logs) LOGS=1 ;; +esac +if [ "$START" = 1 ] || [ "$LOGS" = 1 ]; then + [ -f .env ] || { printf '這台還沒架伺服器。第一次架:make server\n' >&2; exit 1; } +fi AUTO=0 [ -n "${ANYBOT_SETUP_USER:-}" ] && [ -n "${ANYBOT_SETUP_PASSWORD:-}" ] && AUTO=1 +# --start asks nothing: it keeps .env as it is. +[ "$START" = 1 ] && AUTO=1 + +# The Docker this server lives on: the daemon that holds its data (the volumes). Picked once, at +# the first setup, from the docker CLI as it was then, and kept in .env as DOCKER_SOCKET; every +# start after that uses it, whatever plain `docker` points at now. A host can have two daemons +# (rootless Docker for this user, and the root one reached through the docker group); a start on +# the other one comes up as a second, empty server next to the real one. +stack_socket() { + local s h + s=$(sed -n "s/^DOCKER_SOCKET=['\"]\{0,1\}\([^'\"]*\)['\"]\{0,1\}\$/\1/p" .env 2>/dev/null | tail -n 1) + if [ -z "$s" ]; then + h=${DOCKER_HOST:-} + [ -z "$h" ] && h=$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null) + case "$h" in unix://*) s=${h#unix://} ;; esac + fi + printf '%s' "$s" +} +SOCK=$(stack_socket) +if [ -n "$SOCK" ]; then + export DOCKER_HOST="unix://$SOCK" DOCKER_SOCKET="$SOCK" +fi +if [ "$LOGS" = 1 ]; then + exec docker compose logs -f --tail 100 "${2:-anybot}" +fi # 語音 on this server (docker-compose's voice service, speaches): speech to text with Whisper on # the CPU. large-v3-turbo got every word of a Mandarin test right, about 3.5 s for 11 s of speech # on 12 cores; Systran/faster-whisper-small is three times faster and slips a word now and then @@ -39,6 +78,8 @@ STEPS=("檢查" "網址" "帳號" "啟動") AT=0 header() { + # --start runs inside make update(-server): keep what was printed before it on the screen. + [ "$START" = 1 ] && return printf '\e[2J\e[H\n' printf ' %s◆%s %sAnyBot 伺服器設定%s ' "$A" "$N" "$B" "$N" local i @@ -136,24 +177,28 @@ lan_ip() { [ -z "$a" ] && a=$(hostname -I 2>/dev/null | awk '{print $1}') printf '%s' "$a" } -# The socket docker-socket-proxy has to mount: the one of the daemon this script runs compose -# against. Only rootless Docker needs it spelled out (/run/user//docker.sock): there the -# compose default /var/run/docker.sock is another daemon (the root one, or none) and every Docker -# call of the brain comes back 503. Prints nothing when the default is right. -rootless_socket() { - docker info --format '{{json .SecurityOptions}}' 2>/dev/null | grep -q 'name=rootless' || return 0 - local h=${DOCKER_HOST:-} - [ -z "$h" ] && h=$(docker context inspect --format '{{.Endpoints.docker.Host}}' 2>/dev/null) - case "$h" in unix://*) printf '%s' "${h#unix://}" ;; esac -} - # ---------- 0. this server ---------- AT=0; header; card_open "檢查這台伺服器" FAIL=0; WARNED=0 if command -v docker >/dev/null; then ok "Docker 已安裝"; else bad "沒有 Docker:curl -fsSL https://get.docker.com | sh"; FAIL=1; fi if docker compose version >/dev/null 2>&1; then ok "docker compose 可以用"; elif [ $FAIL = 0 ]; then bad "沒有 docker compose(Docker 太舊?請更新 Docker)"; FAIL=1; fi -if [ $FAIL = 0 ] && ! docker info >/dev/null 2>&1; then bad "沒有權限使用 Docker:sudo usermod -aG docker \$USER,登出再登入"; FAIL=1; fi -if command -v ss >/dev/null; then +if [ $FAIL = 0 ] && ! docker info >/dev/null 2>&1; then + if [ -n "$SOCK" ] && [ ! -S "$SOCK" ]; then + # Never fall back to another daemon: this server's data is in this one. + case "$SOCK" in + /run/user/*) bad "這台伺服器的 Docker(rootless,${SOCK})沒有在跑:systemctl --user start docker" ;; + *) bad "這台伺服器的 Docker(${SOCK})沒有在跑:sudo systemctl start docker" ;; + esac + else + bad "沒有權限使用 Docker:sudo usermod -aG docker \$USER,登出再登入" + fi + FAIL=1 +fi +[ $FAIL = 0 ] && [ -n "$SOCK" ] && ok "用這個 Docker:$SOCK" +# A kept Tailscale-only setup has no Caddy: 80 and 443 aren't this server's business then. +TSONLY=0 +grep -q "^ANYBOT_TAILSCALE=['\"]\{0,1\}1" .env 2>/dev/null && ! grep -q '^ANYBOT_LAN_IP=' .env && TSONLY=1 +if command -v ss >/dev/null && [ "$TSONLY" = 0 ]; then # (Tailscale's own 443 on its 100.x address isn't in the way.) busy=$(ss -ltnH 2>/dev/null | awk '{print $4}' | grep -E ':(80|443)$' | grep -v docker | grep -vE '^(100\.|\[fd7a:115c:a1e0:)' || true) if [ -n "$busy" ] && ! docker compose ps -q caddy 2>/dev/null | grep -q .; then warn "80 或 443 埠已被別的程式佔用(HTTPS 需要它們)"; else ok "80、443 埠可以用"; fi @@ -166,8 +211,8 @@ if [ $FAIL = 1 ] && [ $DRY = 0 ]; then note ""; note "修好上面的問題後 # All fine: go on by itself. if [ $WARNED = 1 ]; then card_close; pause; else card_close; sleep 1; fi -# Keep the current settings? -if [ -f .env ] && [ -f Caddyfile ]; then +# Keep the current settings? (--start always does.) +if [ -f .env ] && { [ -f Caddyfile ] || [ "$START" = 1 ]; }; then # shellcheck disable=SC1091 set -a; . ./.env; set +a if grep -q "^COMPOSE_PROFILES=.*voice" .env; then @@ -326,26 +371,23 @@ if [ "${SKIP:-0}" != 1 ]; then fi fi -# Rootless Docker: pin its socket in .env, whether .env was just written or kept from an earlier -# setup (one made before docker-socket-proxy came along has no DOCKER_SOCKET). -SOCK=$(rootless_socket) -if [ -n "$SOCK" ]; then - if ! grep -qxF "DOCKER_SOCKET='$SOCK'" .env; then - (umask 077; { grep -v '^DOCKER_SOCKET=' .env; printf "DOCKER_SOCKET='%s'\n" "$SOCK"; } >.env.new) \ - && mv .env.new .env - fi - export DOCKER_SOCKET=$SOCK +# Pin this server's Docker in .env (see stack_socket), whether .env was just written or kept from +# an earlier setup (one made before this has no DOCKER_SOCKET, or only a rootless one has it). +# docker-socket-proxy mounts the same socket, so the brain drives the bots' computer there too. +if [ -n "$SOCK" ] && ! grep -qxF "DOCKER_SOCKET='$SOCK'" .env; then + (umask 077; { grep -v '^DOCKER_SOCKET=' .env; printf "DOCKER_SOCKET='%s'\n" "$SOCK"; } >.env.new) \ + && mv .env.new .env fi # ---------- 4. start ---------- AT=3; header; card_open "啟動" -ok "寫好設定(.env、Caddyfile)" -[ -n "$SOCK" ] && ok "Docker 是 rootless:大腦經由 $SOCK 管 Bot 的電腦" +[ "$START" = 1 ] && ok "用現有設定(.env)" || ok "寫好設定(.env、Caddyfile)" if [ $DRY = 1 ]; then - warn "試跑(--dry-run):沒有啟動。要啟動:docker compose up -d --build" + warn "試跑(--dry-run):沒有啟動。要啟動:make update-server" card_close; exit 0 fi -if [ "${SKIP:-0}" = 1 ] && [ -d ../../.git ] && command -v git >/dev/null; then +# --start: make update(-server) has pulled already (or was told not to, PULL=0). +if [ "${SKIP:-0}" = 1 ] && [ "$START" = 0 ] && [ -d ../../.git ] && command -v git >/dev/null; then spin "拉下最新版" git -C ../.. pull --ff-only || true fi # Small servers run out of memory compiling many crates at once: one compile per 2 GB. @@ -359,11 +401,19 @@ spin "建置並啟動(第一次約 5–15 分鐘)" docker compose up -d --bu [ "${ANYBOT_TAILSCALE:-}" = 1 ] && TS=1 if [ "${TS:-0}" = 1 ]; then # Tailscale gives the address HTTPS and passes it to the account server (remembered across reboots). - if tailscale serve --bg --https=443 http://127.0.0.1:8780 >/dev/null 2>&1 \ - || sudo tailscale serve --bg --https=443 http://127.0.0.1:8780 >/dev/null 2>&1; then + # The HTTPS port is the one in the address (ANYBOT_RELAY_PUBLIC_URL in .env, e.g. + # https://name.ts.net:8443); 443 when it has none. + TSPORT=443 + case "$DOMAIN" in *:*) TSPORT=${DOMAIN##*:} ;; esac + served() { + tailscale serve status --json 2>/dev/null | tr -d ' \n' \ + | grep -q "\"[^\"]*:$TSPORT\":{\"Handlers\":{\"/\":{\"Proxy\":\"http://127.0.0.1:8780\"}" + } + if served || tailscale serve --bg --https="$TSPORT" http://127.0.0.1:8780 >/dev/null 2>&1 \ + || sudo tailscale serve --bg --https="$TSPORT" http://127.0.0.1:8780 >/dev/null 2>&1; then ok "Tailscale:https://$DOMAIN → 帳號伺服器" else - bad "沒辦法設定 tailscale serve。請執行:sudo tailscale serve --bg --https=443 http://127.0.0.1:8780" + bad "沒辦法設定 tailscale serve。請執行:sudo tailscale serve --bg --https=$TSPORT http://127.0.0.1:8780" fi fi @@ -392,7 +442,7 @@ if [ "$DOMAIN" != "${LANIP:-}" ]; then printf '\r\e[K' if [ -n "$https" ]; then ok "https://$DOMAIN 可以連" elif [ "${TS:-0}" = 1 ]; then warn "https://$DOMAIN 還連不到。Tailscale 的 HTTPS 要在管理頁打開(DNS › HTTPS Certificates),看設定:tailscale serve status" - else warn "https://$DOMAIN 還連不到(DNS 還沒生效,或 80/443 埠被防火牆擋住)。看紀錄:docker compose logs caddy"; fi + else warn "https://$DOMAIN 還連不到(DNS 還沒生效,或 80/443 埠被防火牆擋住)。看紀錄:deploy/server/setup.sh --logs caddy"; fi fi # The local network's address: checked with the certificate Caddy signed it with. if [ -n "${LANIP:-}" ]; then @@ -404,7 +454,13 @@ if [ -n "${LANIP:-}" ]; then done printf '\r\e[K'; rm -f "$ca" if [ -n "$lanok" ]; then ok "https://$LANIP 可以連(憑證:http://$LANIP/anybot-ca.crt)" - else warn "https://$LANIP 還連不到(80/443 埠被防火牆擋住?)。看紀錄:docker compose logs caddy"; fi + else warn "https://$LANIP 還連不到(80/443 埠被防火牆擋住?)。看紀錄:deploy/server/setup.sh --logs caddy"; fi +fi +if [ "$START" = 1 ]; then + docker compose ps --format ' {{.Service}} {{.Status}}' 2>/dev/null | while IFS= read -r l; do note "$l"; done + note "看紀錄:make server-logs" + card_close + [ -n "$joined" ]; exit $? fi card_close @@ -421,7 +477,8 @@ line "${B}電腦${N} AnyBot › 設定 › 一般 › 手機與帳號 登 line "${B}AI 金鑰${N} 手機或電腦的 AnyBot › 設定 › 模型(只存在大腦上)" line "${B}加一台電腦${N} 手機 AnyBot 右上 ⋯ › 加一台電腦,把那一行貼到那台的終端機" line "" -note "看紀錄 docker compose logs -f anybot" -note "更新 ./setup.sh(選「用現有設定」)" +note "看紀錄 make server-logs" +note "更新 make update-server(拉新版、重建、用 .env 重新啟動)" +note "改設定 改 deploy/server/.env 再 make update-server,或再跑一次 make server" card_close printf '\n' diff --git a/deploy/update.sh b/deploy/update.sh index e3576ec..c1d0d21 100755 --- a/deploy/update.sh +++ b/deploy/update.sh @@ -159,23 +159,22 @@ update_server() { srv=$ROOT/deploy/server [ -f "$srv/.env" ] || die "這台還沒架伺服器。" "第一次架:make server" have docker || die "找不到 Docker。" "伺服器跑在 Docker 裡:先裝好 Docker,或再執行一次 make server。" + have bash || die "找不到 bash。" "伺服器的設定腳本要用 bash:sudo apt-get install bash" + # This server's Docker is the one .env names (DOCKER_SOCKET, written by make server), not whatever + # plain `docker` reaches: with both a rootless and the root daemon on a host, that can be the other + # one, and a start there comes up as a second, empty server. + sock=$(sed -n "s/^DOCKER_SOCKET=['\"]\{0,1\}\([^'\"]*\)['\"]\{0,1\}\$/\1/p" "$srv/.env" | tail -n 1) 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 + [ -n "$sock" ] && dk="env DOCKER_HOST=unix://$sock docker" + run= + if ! $dk info >/dev/null 2>&1; then + if [ "$PLAT" = linux ] && have sudo && sudo $dk info >/dev/null 2>&1; then run="sudo -E" + else die "沒辦法使用這台伺服器的 Docker${sock:+(${sock})}。" "Docker 有在跑嗎?試試:sudo systemctl start docker(rootless:systemctl --user 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" + # The same start as make server's (build, start, Tailscale's HTTPS port, checks), with .env as it is. + $run bash "$srv/setup.sh" --start || die "伺服器沒有好好起來。" "看紀錄:make server-logs" ok "伺服器換成新版了" - (cd "$srv" && $dk compose ps --format ' {{.Service}} {{.Status}}' 2>/dev/null) || true - info "看紀錄:cd ${srv} && docker compose logs -f anybot" } # ---------- 這台裝了什麼 ----------