migrate 加自動整備:沒鑰匙自動 ssh-copy-id(現場輸一次密碼)、缺 Rust 自動裝 rustup、缺 Tailscale 試免密 sudo 裝;ts.net 名稱改用無依賴的 sed 解析
This commit is contained in:
parent
6ef68eeb34
commit
0ac16f25b8
|
|
@ -38,12 +38,29 @@ say "1/7 連到大腦那台(SSH)"
|
||||||
ask "那台的 SSH(例:daniel@cloudbrain 或 daniel@100.x.y.z): "
|
ask "那台的 SSH(例:daniel@cloudbrain 或 daniel@100.x.y.z): "
|
||||||
read -r SSH_TARGET
|
read -r SSH_TARGET
|
||||||
[ -n "$SSH_TARGET" ] || { bad "要填。"; exit 1; }
|
[ -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' ' ')
|
SRV_OS=$(ssh -o BatchMode=yes "$SSH_TARGET" 'uname -s; uname -m' | tr '\n' ' ')
|
||||||
ok "那台系統:$SRV_OS"
|
ok "那台系統:$SRV_OS"
|
||||||
# 非互動 SSH 不讀 .bashrc:rustup 裝在 ~/.cargo/bin,手動補進 PATH。
|
# 非互動 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; }
|
if ssh -o BatchMode=yes "$SSH_TARGET" 'export PATH="$HOME/.cargo/bin:$PATH"; command -v cargo' >/dev/null 2>&1; then
|
||||||
ssh -o BatchMode=yes "$SSH_TARGET" 'command -v tailscale' >/dev/null 2>&1 && ok "那台有 Tailscale" || { bad "那台沒有 Tailscale(要它給 relay 一個 https 網址)。"; exit 1; }
|
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 ----------
|
# ---------- 2. 服务器上编译并启动 relay ----------
|
||||||
say "2/7 在那台編譯帳號伺服器(第一次約 3–10 分鐘)"
|
say "2/7 在那台編譯帳號伺服器(第一次約 3–10 分鐘)"
|
||||||
|
|
@ -83,7 +100,7 @@ ok "relay 在那台跑起来了(127.0.0.1:8780)"
|
||||||
say "3/7 Tailscale HTTPS 網址(手機和電腦要能用 https 連到它)"
|
say "3/7 Tailscale HTTPS 網址(手機和電腦要能用 https 連到它)"
|
||||||
SERVE=$(ssh -o BatchMode=yes "$SSH_TARGET" 'tailscale serve status 2>/dev/null || true')
|
SERVE=$(ssh -o BatchMode=yes "$SSH_TARGET" 'tailscale serve status 2>/dev/null || true')
|
||||||
printf '%s\n' "$SERVE" | sed 's/^/ /' | head -12
|
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; }
|
[ -n "$TS_HOST" ] || { bad "那台沒有 Tailscale HTTPS 名稱。先到 Tailscale 管理頁(DNS › HTTPS Certificates)為 tailnet 開啟 HTTPS,再重跑。"; exit 1; }
|
||||||
if ! printf '%s' "$SERVE" | grep -q "8443"; then
|
if ! printf '%s' "$SERVE" | grep -q "8443"; then
|
||||||
ask "要在那台開 https://$TS_HOST:8443 → 127.0.0.1:8780 嗎?[Y/n] "
|
ask "要在那台開 https://$TS_HOST:8443 → 127.0.0.1:8780 嗎?[Y/n] "
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue