AnyBot/deploy/update.sh

235 lines
10 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/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
# ---------- 簽章 ----------
# Who signs AnyBot, remembered OUTSIDE the source folder the first time this install sees
# deploy/allowed_signers (trust on first use). From then on an update must be signed by a key in
# it; a new list is only taken from a version that passed (that is how a key is changed).
PIN=${XDG_CONFIG_HOME:-$HOME/.config}/anybot/allowed_signers
signed_by_pin() {
git -C "$ROOT" -c gpg.format=ssh -c "gpg.ssh.allowedSignersFile=$PIN" verify-commit "$1" >/dev/null 2>&1
}
pin_signers() {
[ -s "$ROOT/deploy/allowed_signers" ] || return 0
if [ ! -s "$PIN" ]; then
mkdir -p "$(dirname "$PIN")" && cp "$ROOT/deploy/allowed_signers" "$PIN" || return 0
if signed_by_pin HEAD; then ok "記住了 AnyBot 的簽章金鑰:以後的更新都要由它簽過(${PIN})"
else rm -f "$PIN"; warn "這一版沒有簽章,還沒辦法檢查更新的來源"; fi
elif [ "$1" = 1 ] && ! cmp -s "$ROOT/deploy/allowed_signers" "$PIN"; then
cp "$ROOT/deploy/allowed_signers" "$PIN" && info "簽章金鑰的名單更新了(由舊金鑰簽過的版本帶來的)"
fi
}
# ---------- 原始碼 ----------
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)
if [ -s "$PIN" ] && [ "${ANYBOT_UNSIGNED:-0}" != 1 ]; then
# This install pinned who signs AnyBot: the new version is checked BEFORE any of it is used
# (by this, the version already here), so a break-in at the git server can't hand out a build.
git -C "$ROOT" fetch -q || die "沒辦法拉下新版。" "網路連得到原始碼的伺服器嗎?"
if [ "$(git -C "$ROOT" rev-parse HEAD)" != "$(git -C "$ROOT" rev-parse '@{u}')" ]; then
signed_by_pin '@{u}' || die "新版沒有通過簽章檢查,沒有更新。" "新版不是由這台記住的金鑰(${PIN})簽的。先向作者確認;確定沒問題才用 ANYBOT_UNSIGNED=1 跳過這次檢查。"
ok "新版的簽章對得上這台記住的金鑰"
fi
git -C "$ROOT" merge --ff-only -q '@{u}' || die "沒辦法拉下新版。" "你改過的檔案跟新版衝突,或這個分支跟遠端分岔了。處理好再試;或用 PULL=0 裝現在這份。"
verified=1
else
[ -s "$PIN" ] && warn "跳過簽章檢查(ANYBOT_UNSIGNED=1)"
git -C "$ROOT" pull --ff-only -q || die "沒辦法拉下新版。" "你改過的檔案跟新版衝突,或這個分支跟遠端分岔了。處理好再試;或用 PULL=0 裝現在這份。"
verified=0
fi
pin_signers "$verified"
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。"
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
[ -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
# 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 "伺服器換成新版了"
}
# ---------- 這台裝了什麼 ----------
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"