From 0ac16f25b8e2dd12546448f616f895b684957e42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=80=A7=E9=A9=8A?= Date: Thu, 19 Nov 2026 17:13:51 +0800 Subject: [PATCH] =?UTF-8?q?migrate=20=E5=8A=A0=E8=87=AA=E5=8B=95=E6=95=B4?= =?UTF-8?q?=E5=82=99=EF=BC=9A=E6=B2=92=E9=91=B0=E5=8C=99=E8=87=AA=E5=8B=95?= =?UTF-8?q?=20ssh-copy-id=EF=BC=88=E7=8F=BE=E5=A0=B4=E8=BC=B8=E4=B8=80?= =?UTF-8?q?=E6=AC=A1=E5=AF=86=E7=A2=BC=EF=BC=89=E3=80=81=E7=BC=BA=20Rust?= =?UTF-8?q?=20=E8=87=AA=E5=8B=95=E8=A3=9D=20rustup=E3=80=81=E7=BC=BA=20Tai?= =?UTF-8?q?lscale=20=E8=A9=A6=E5=85=8D=E5=AF=86=20sudo=20=E8=A3=9D?= =?UTF-8?q?=EF=BC=9Bts.net=20=E5=90=8D=E7=A8=B1=E6=94=B9=E7=94=A8=E7=84=A1?= =?UTF-8?q?=E4=BE=9D=E8=B3=B4=E7=9A=84=20sed=20=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/migrate-relay.sh | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/deploy/migrate-relay.sh b/deploy/migrate-relay.sh index 322792b..a3afd43 100755 --- a/deploy/migrate-relay.sh +++ b/deploy/migrate-relay.sh @@ -38,12 +38,29 @@ say "1/7 連到大腦那台(SSH)" ask "那台的 SSH(例:daniel@cloudbrain 或 daniel@100.x.y.z): " read -r SSH_TARGET [ -n "$SSH_TARGET" ] || { bad "要填。"; exit 1; } -ssh -o BatchMode=yes -o ConnectTimeout=10 "$SSH_TARGET" true 2>/dev/null && ok "SSH 連線正常" || { bad "SSH 連不上(${SSH_TARGET})。先設好免密登入(ssh-copy-id)再來。"; exit 1; } +if ! ssh -o BatchMode=yes -o ConnectTimeout=10 "$SSH_TARGET" true 2>/dev/null; then + echo " 還沒免密登入——現在幫你裝鑰匙:接下來請打一次那台的密碼(只這一次,之後都不用)。" + [ -f "$HOME/.ssh/id_ed25519" ] || ssh-keygen -t ed25519 -f "$HOME/.ssh/id_ed25519" -N '' -q + ssh-copy-id -o ConnectTimeout=15 "$SSH_TARGET" >/dev/null 2>&1 && ok "鑰匙已裝好" || { bad "裝鑰匙失敗——先確認你能手動 ssh 進那台(密碼登入也要能進)。"; exit 1; } +fi +ssh -o BatchMode=yes -o ConnectTimeout=10 "$SSH_TARGET" true && ok "SSH 連線正常" || { bad "SSH 連不上(${SSH_TARGET})。"; exit 1; } SRV_OS=$(ssh -o BatchMode=yes "$SSH_TARGET" 'uname -s; uname -m' | tr '\n' ' ') ok "那台系統:$SRV_OS" # 非互動 SSH 不讀 .bashrc:rustup 裝在 ~/.cargo/bin,手動補進 PATH。 -ssh -o BatchMode=yes "$SSH_TARGET" 'export PATH="$HOME/.cargo/bin:$PATH"; command -v cargo' >/dev/null 2>&1 && ok "那台有 Rust 工具鏈(編譯 relay 用)" || { bad "那台沒有 cargo。先裝 Rust:ssh 進去後跑 curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y"; exit 1; } -ssh -o BatchMode=yes "$SSH_TARGET" 'command -v tailscale' >/dev/null 2>&1 && ok "那台有 Tailscale" || { bad "那台沒有 Tailscale(要它給 relay 一個 https 網址)。"; exit 1; } +if ssh -o BatchMode=yes "$SSH_TARGET" 'export PATH="$HOME/.cargo/bin:$PATH"; command -v cargo' >/dev/null 2>&1; then + ok "那台有 Rust 工具鏈(編譯 relay 用)" +else + echo " 那台沒有 Rust——自動安裝(約 1–2 分鐘,只裝在你的帳號下,不碰系統)。" + ssh -o BatchMode=yes "$SSH_TARGET" 'curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --profile minimal' >/dev/null 2>&1 + ssh -o BatchMode=yes "$SSH_TARGET" 'export PATH="$HOME/.cargo/bin:$PATH"; command -v cargo' >/dev/null 2>&1 && ok "Rust 裝好了" || { bad "Rust 自動安裝失敗——SSH 進那台手動跑:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y,然後重跑本腳本。"; exit 1; } +fi +if ssh -o BatchMode=yes "$SSH_TARGET" 'command -v tailscale' >/dev/null 2>&1; then + ok "那台有 Tailscale" +else + echo " 那台沒有 Tailscale——試自動安裝(要 root 權限;失敗會給你指令)。" + ssh -o BatchMode=yes "$SSH_TARGET" 'sudo -n sh -c "curl -fsSL https://tailscale.com/install.sh | sh"' >/dev/null 2>&1 + ssh -o BatchMode=yes "$SSH_TARGET" 'command -v tailscale' >/dev/null 2>&1 && ok "Tailscale 裝好了" || { bad "Tailscale 自動安裝失敗(多半是沒有免密 sudo)。SSH 進那台手動跑:curl -fsSL https://tailscale.com/install.sh | sudo sh,接著 tailscale up 登入你的 tailnet,再重跑本腳本。"; exit 1; } +fi # ---------- 2. 服务器上编译并启动 relay ---------- say "2/7 在那台編譯帳號伺服器(第一次約 3–10 分鐘)" @@ -83,7 +100,7 @@ ok "relay 在那台跑起来了(127.0.0.1:8780)" say "3/7 Tailscale HTTPS 網址(手機和電腦要能用 https 連到它)" SERVE=$(ssh -o BatchMode=yes "$SSH_TARGET" 'tailscale serve status 2>/dev/null || true') printf '%s\n' "$SERVE" | sed 's/^/ /' | head -12 -TS_HOST=$(ssh -o BatchMode=yes "$SSH_TARGET" 'tailscale status --json 2>/dev/null | python3 -c "import json,sys; print(json.load(sys.stdin)[\"Self\"][\"DNSName\"].rstrip(\".\"))" 2>/dev/null || true') +TS_HOST=$(ssh -o BatchMode=yes "$SSH_TARGET" 'tailscale status --json 2>/dev/null | sed -n '"'"'s/.*"DNSName": *"\([^"]*\)".*/\1/p'"'"' | head -1 | sed '"'"'s/\.$//'"'"'') [ -n "$TS_HOST" ] || { bad "那台沒有 Tailscale HTTPS 名稱。先到 Tailscale 管理頁(DNS › HTTPS Certificates)為 tailnet 開啟 HTTPS,再重跑。"; exit 1; } if ! printf '%s' "$SERVE" | grep -q "8443"; then ask "要在那台開 https://$TS_HOST:8443 → 127.0.0.1:8780 嗎?[Y/n] "