雲端大腦換新版失敗時不會一直停著:上次換到一半大腦管理員被停掉(舊的已經停了),之後新的又一直建不起來,以前只會一直排「換新版」、不會叫醒它,現在換不成就先把停著的舊大腦叫醒;新的已經換上原名、還沒啟動就被中斷的,下次刪掉它、放回舊的再換一次(以前直接刪掉舊的,新的起不來就沒有退路);讀不到原本的登入(Docker 一時沒回答)就先不換,不再改領新的登入(會把正在跑的那個登出)。管理員每一輪核對每個大腦接著的網路:現在設定給 Docker、而且建立時就有 Docker 的才接到 Docker,從 ANYBOT_BRAINS_DOCKER 拿掉再更新立刻拿掉(不等換成新版),掉了的接回來,放回或叫醒的舊大腦不會拿到建立時沒有的 Docker。更新時 Caddyfile 還有別的轉到 relay 的寫法(一行的、http://relay:8080、容器名、matcher、to、{ 後面接註解)就不用暗號、照舊,並在完成畫面寫出來、說怎麼自己補;同一台 Caddy 代理別的服務(NAS、別的網站)或網域名稱有 relay- 照樣補上;已經有暗號那行的 Caddyfile 還有沒帶暗號、轉到 relay 的路也寫出來。FEATURES 和功能地圖 server 跟著改。
三輪獨立審查:第一輪沒有 P0/P1(提的 P2:Caddyfile 判斷太寬、叫醒時可能把收回的 Docker 還回去,都修了);第二輪一個 P1(叫醒、放回時照現在的設定接 Docker,會接給建立時沒有 Docker 的舊大腦),改成每輪核對網路;第三輪沒有 P0/P1,這輪沒再改程式。留下的 P2:核對網路失敗時沒寫紀錄;擁有者檔案不見又剛好拿不到帳號清單時,擁有者的 Docker 會短暫被拔掉;用變數或 IP 轉到 relay 的 Caddyfile 偵測不到。 已驗證: cargo test(904,含 featuremap::、layout_audit::;brains:: 10)、bash -n;Debian 10(mawk 1.3.3)上 13 種 Caddyfile 寫法的判斷;三個範本和 nobledesktop 現在的 Caddyfile 不會被誤提醒,多一個沒帶暗號的區塊會提醒;隔離測試機 test3:舊大腦停著、新的建不起來(ANYBOT_BRAIN_MEMORY=1k)→ 叫醒原本那個(同一個容器 ID、在線),改回來後換成新版;新的換上原名還沒啟動就中斷(手動造出 -old 和沒啟動過的容器)→ 放回原本那個、在線;手動拔掉擁有者的 Docker 網路 → 下一輪接回,手動把 Docker 網路接給沒有 Docker 的大腦 → 下一輪拿掉;舊版伺服器測試機:Caddyfile 多一個 reverse_proxy http://relay:8080 → 更新印出警告、暗號拿掉、Caddyfile 沒被動、網址照常,還原後暗號回來。 未驗證: 讀不到原本登入就不換(要讓 Docker 一時不回答,造不出來,只讀過程式);換上後新的啟動失敗、放回舊的那條路(沒在實機重現);nobledesktop 還沒換上這一版(它沒有管理員開的大腦,這次改的只影響那些和更新時的 Caddyfile 檢查)。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Uirha2uBHmbnuWBhNzjbJm
This commit is contained in:
parent
ace27374e6
commit
1605b38354
File diff suppressed because one or more lines are too long
|
|
@ -605,14 +605,20 @@ if [ -f .env ] && [ "$DRY" = 0 ]; then
|
|||
# [[:space:]] and would match nothing.)
|
||||
ws="[ $(printf '\t')]"
|
||||
block="^$ws*reverse_proxy$ws+relay:8080$ws*[{]$ws*\$"
|
||||
oneline="^$ws*reverse_proxy$ws+relay:8080$ws*\$"
|
||||
if [ "$secret" = 1 ] && ! grep -q 'X-AnyBot-Proxy' Caddyfile; then
|
||||
# Only where every way to the account server is a block the line can go in (a one-line
|
||||
# reverse_proxy, or none: the old way), each one got its line, and Caddy reads the file;
|
||||
# written back into the same file (caddy has that one file mounted), the one before put back
|
||||
# if that write fails half way.
|
||||
# The blocks the line goes in, and any other reverse_proxy or `to` naming the relay (one line,
|
||||
# a matcher, http://relay:8080, the container's name, a comment after the {) — what else this
|
||||
# Caddy forwards (a NAS, another site) doesn't count.
|
||||
blocks=0; others=0
|
||||
if [ -f Caddyfile ]; then
|
||||
blocks=$(grep -Ec "$block" Caddyfile || true)
|
||||
if [ "${blocks:-0}" -gt 0 ] && ! grep -Eq "$oneline" Caddyfile \
|
||||
others=$(grep -v "^$ws*#" Caddyfile | grep -E "^$ws*(reverse_proxy|to)$ws.*relay[:-]" | grep -Evc "$block" || true)
|
||||
fi
|
||||
if [ "$secret" = 1 ] && ! grep -q 'X-AnyBot-Proxy' Caddyfile; then
|
||||
# Only where every way to the account server is a block the line can go in (any other would
|
||||
# forward without it: the old way, and said so), each one got its line, and Caddy reads the
|
||||
# file; written back into the same file (caddy has that one file mounted), the one before put
|
||||
# back if that write fails half way.
|
||||
if [ "${blocks:-0}" -gt 0 ] && [ "${others:-0}" = 0 ] \
|
||||
&& awk -v re="$block" '{ print } $0 ~ re { print "\t\theader_up X-AnyBot-Proxy {$ANYBOT_PROXY_SECRET}" }' Caddyfile >Caddyfile.new \
|
||||
&& [ "$(grep -c 'X-AnyBot-Proxy' Caddyfile.new)" = "$blocks" ] \
|
||||
&& docker run --rm -e ANYBOT_PROXY_SECRET=check -v "$PWD/Caddyfile.new:/etc/caddy/Caddyfile:ro" caddy:2 caddy adapt --config /etc/caddy/Caddyfile --adapter caddyfile >/dev/null 2>&1 \
|
||||
|
|
@ -621,8 +627,15 @@ if [ -f .env ] && [ "$DRY" = 0 ]; then
|
|||
CADDY_PATCHED=1
|
||||
else
|
||||
secret=0
|
||||
CADDY_NOSECRET=1
|
||||
fi
|
||||
rm -f Caddyfile.new Caddyfile.before
|
||||
elif [ "$secret" = 1 ]; then
|
||||
# One that has the line: a way to the account server without it (added later by hand, or one
|
||||
# an older update let through) is said — the requests on it all come from Caddy as far as
|
||||
# the account server knows, and share one limit for sign-ups and sign-ins.
|
||||
heads=$(grep -v "^$ws*#" Caddyfile | grep -c 'X-AnyBot-Proxy' || true)
|
||||
if [ "${others:-0}" != 0 ] || [ "${blocks:-0}" -gt "${heads:-0}" ]; then CADDY_PARTIAL=1; fi
|
||||
fi
|
||||
if [ "$secret" = 1 ] && ! grep -q "^ANYBOT_PROXY_SECRET=['\"]\{0,1\}[0-9a-f]\{32,\}" .env; then
|
||||
(umask 077; { grep -v '^ANYBOT_PROXY_SECRET=' .env; printf "ANYBOT_PROXY_SECRET='%s'\n" "$(gen_key)"; } >.env.new) && mv .env.new .env
|
||||
|
|
@ -660,6 +673,14 @@ fi
|
|||
# ---------- 4. start ----------
|
||||
AT=3; header; card_open "啟動"
|
||||
[ "$START" = 1 ] && ok "用現有設定(.env)" || ok "寫好設定(.env、Caddyfile)"
|
||||
if [ "${CADDY_NOSECRET:-0}" = 1 ]; then
|
||||
warn "Caddyfile 沒辦法自動補上送暗號的那行(有別的轉到 relay 的寫法,或 Caddy 檢查沒過):帳號伺服器照舊相信請求自己說的來源,註冊、登入的次數限制擋不住偽造的。"
|
||||
note "要補:在每個轉到 relay:8080 的 reverse_proxy 區塊裡加 header_up X-AnyBot-Proxy {\$ANYBOT_PROXY_SECRET},再 make update-server"
|
||||
fi
|
||||
if [ "${CADDY_PARTIAL:-0}" = 1 ]; then
|
||||
warn "Caddyfile 裡有轉到 relay 的路沒帶暗號:走那條路的請求,帳號伺服器都當成從 Caddy 來(註冊、登入的次數限制大家共用)。"
|
||||
note "要補:那些 reverse_proxy 寫成區塊(reverse_proxy relay:8080 { … }),裡面加 header_up X-AnyBot-Proxy {\$ANYBOT_PROXY_SECRET},再 make update-server"
|
||||
fi
|
||||
if [ $DRY = 1 ]; then
|
||||
warn "試跑(--dry-run):沒有啟動。要啟動:make update-server"
|
||||
card_close; exit 0
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
- `server-install` 安裝:在 Linux 上貼一行 `install.sh … server`。沒有 Docker 就裝(設成開機自動跑),系統套件(含 `make`)、原始碼、建置、啟動都自己來,不問問題。剛加進 docker 群組的使用者用 `sg` 執行,寫出來的 `.env`、`Caddyfile`、`~/.docker` 是那個人的;之前用 sudo 留下的 root 檔案自己改回來,改不了說確切的指令。最後印出網址和邀請碼。
|
||||
- `server-address` 網址自己找:給了 `ANYBOT_SETUP_DOMAIN` 就用它(網域:自動申請憑證;IP:那個 IP);否則 Tailscale 有發憑證時用 https://它的名字;tailnet 不發憑證(headscale)時用 http://tailnet 位址(WireGuard 已經加密,AnyBot 只在 tailnet 上收 http;任何指到這台的名字也通;https 也在,手機通知用);都沒有就用 https://區網 IP(這台自己簽的憑證:每台電腦和手機先開 http://位址/anybot-ca.crt 信任一次)。位址在網路上是公開的(VPS)又沒給網域:停下來,請你給網域或裝 Tailscale,不把帳號用明文開在網路上。綁在某個位址時,讓系統可以先綁還沒出現的位址(`net.ipv4.ip_nonlocal_bind`),重開機時 tailnet 比 Docker 晚起來也綁得到。
|
||||
- `server-invite` 邀請碼:安裝時產生,註冊都要填,第一個人也要(免得先連到的人變成擁有者);在 `deploy/server/.env` 的 `ANYBOT_RELAY_INVITE`。沒填或填錯不能註冊。完成畫面提醒先自己註冊再給別人(第一個註冊的帳號是擁有者),而且只給信任的人:每個帳號的雲端大腦都能在這台伺服器的網路裡跑程式(連得到它的區網、tailnet)。
|
||||
- `server-brains` 每個帳號一個雲端大腦:大腦管理員看到新帳號就開一個(`anybot-brain-帳號`),有自己的資料;用帳號伺服器發的登入憑證,伺服器上不存密碼。只有擁有者(第一個註冊的帳號,管理員第一次看到就記住)和 `ANYBOT_BRAINS_DOCKER` 列的帳號,雲端大腦有 Docker,也就有自己的 Bot 電腦和它的家目錄——能動 Docker 等於能動整台伺服器(讀得到別人的大腦);其他帳號的雲端大腦沒有,用自己的電腦當手腳。大腦上可以跑帳號主人的指令(hook、連接器),所以每個大腦都不能偽造或偷聽網路封包(沒有 NET_RAW、不能提升權限),有記憶體、CPU 和程序數的上限,最多 `ANYBOT_BRAINS_MAX`(預設 20)個;管理員跟帳號伺服器講話走自己的網路,金鑰不經過大腦們的網路;帳號伺服器只相信 Caddy(帶著 `.env` 的 `ANYBOT_PROXY_SECRET`)說的請求來源,大腦直接連進去寫的不算。要停掉某個帳號的雲端大腦:`.env` 的 `ANYBOT_BRAINS_SKIP` 加它、`make update-server`,再 `docker stop` 它(管理員不再叫醒)。停掉的會叫醒;伺服器更新後(映像檔或開法變了)先讓它收尾再換成新版(同一份資料、同一個登入),新的建不起來就放回原本的;帳號伺服器不再認得它的登入時換一個新的;不刪任何大腦和資料。管理員第一次啟動以前就有的帳號(舊伺服器更新上來的)不開(`ANYBOT_BRAINS_ALL=1` 才開);不取代帳號裡不是它開的同名電腦,用帳號登入的電腦也不能取代它開的大腦。舊的單一帳號伺服器(`.env` 有 `ANYBOT_USER`)原本的大腦照舊。
|
||||
- `server-brains` 每個帳號一個雲端大腦:大腦管理員看到新帳號就開一個(`anybot-brain-帳號`),有自己的資料;用帳號伺服器發的登入憑證,伺服器上不存密碼。只有擁有者(第一個註冊的帳號,管理員第一次看到就記住)和 `ANYBOT_BRAINS_DOCKER` 列的帳號,雲端大腦有 Docker,也就有自己的 Bot 電腦和它的家目錄——能動 Docker 等於能動整台伺服器(讀得到別人的大腦);其他帳號的雲端大腦沒有,用自己的電腦當手腳;從 `ANYBOT_BRAINS_DOCKER` 拿掉再更新,它的 Docker 立刻拿掉(不等換成新版)。大腦上可以跑帳號主人的指令(hook、連接器),所以每個大腦都不能偽造或偷聽網路封包(沒有 NET_RAW、不能提升權限),有記憶體、CPU 和程序數的上限,最多 `ANYBOT_BRAINS_MAX`(預設 20)個;管理員跟帳號伺服器講話走自己的網路,金鑰不經過大腦們的網路;帳號伺服器只相信 Caddy(帶著 `.env` 的 `ANYBOT_PROXY_SECRET`)說的請求來源,大腦直接連進去寫的不算。要停掉某個帳號的雲端大腦:`.env` 的 `ANYBOT_BRAINS_SKIP` 加它、`make update-server`,再 `docker stop` 它(管理員不再叫醒)。停掉的會叫醒;伺服器更新後(映像檔或開法變了)先讓它收尾再換成新版(同一份資料、同一個登入),新的建不起來就放回原本的,原本的停著也叫醒;帳號伺服器不再認得它的登入時換一個新的;不刪任何大腦和資料。管理員第一次啟動以前就有的帳號(舊伺服器更新上來的)不開(`ANYBOT_BRAINS_ALL=1` 才開);不取代帳號裡不是它開的同名電腦,用帳號登入的電腦也不能取代它開的大腦。舊的單一帳號伺服器(`.env` 有 `ANYBOT_USER`)原本的大腦照舊。
|
||||
- `server-join` 註冊或登入後:伺服器會開雲端大腦(管理員最近一分鐘有在跑)時,電腦先等它上線(「正在等你的雲端大腦開好(第一次約半分鐘)…」,最多約一分半)再當它的螢幕,不把自己當大腦;這台原本的 Bot 搬過去,這台也是它的手腳。雲端大腦還沒有 Bot 時,電腦是歡迎畫面、手機是「還沒有 Bot」,照著建第一個。
|
||||
- `server-boot` 開機與更新:Docker 開機就跑(rootless 也打開 linger),帳號伺服器、大腦管理員和每個雲端大腦都跟著回來;檢查那一步寫「開機會自己啟動」,開不了說指令。更新用 `make update-server`;舊的伺服器更新時會補上大腦管理員的金鑰。
|
||||
|
||||
|
|
|
|||
127
src/brains.rs
127
src/brains.rs
|
|
@ -78,6 +78,8 @@ pub struct Existing {
|
|||
pub docker: bool,
|
||||
pub args: String,
|
||||
pub host: String,
|
||||
/// The networks it is on, joined with `,` (its way to Docker is one of them).
|
||||
pub networks: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
|
@ -289,6 +291,18 @@ fn tick(cfg: &Config, resting: &mut HashMap<String, (Instant, u32)>) -> Vec<Stri
|
|||
if counted(&existing, cfg) + waiting > cfg.max {
|
||||
errors.push(format!("雲端大腦已經 {} 個,到上限(ANYBOT_BRAINS_MAX={}):新的帳號先不開", counted(&existing, cfg), cfg.max));
|
||||
}
|
||||
// Docker as it is set now, every time: a brain on the way to it only when it gets Docker now
|
||||
// and was made with it — taken away at once (not when it is made again, which can keep
|
||||
// failing), given back when it was lost, never given to one made without it (one put back or
|
||||
// woken after a failed remake).
|
||||
if let Some(n) = &cfg.docker_network {
|
||||
for (u, e) in &existing {
|
||||
let want = e.docker && gets_docker(u, owner.as_deref(), &cfg.docker_users);
|
||||
if want != on_network(e, n) && docker(&["network", if want { "connect" } else { "disconnect" }, n, &e.name]).is_ok() {
|
||||
log::info!("AnyBot 大腦管理員:{u} 的雲端大腦{}", if want { "接回 Docker" } else { "拿掉 Docker" });
|
||||
}
|
||||
}
|
||||
}
|
||||
let actions = plan(&accounts, &existing, &image, owner.as_deref(), managed.as_ref(), cfg);
|
||||
let who = |a: &Action| match a {
|
||||
Action::Start(n, _) => n.clone(),
|
||||
|
|
@ -344,9 +358,46 @@ fn tick(cfg: &Config, resting: &mut HashMap<String, (Instant, u32)>) -> Vec<Stri
|
|||
/// first, so a turn under way is ended the way a stop ends it, its work saved.
|
||||
fn remake(cfg: &Config, user: &str, d: bool, fresh: bool) -> Result<(), String> {
|
||||
let name = container_name(user);
|
||||
let env = docker(&["inspect", "-f", "{{range .Config.Env}}{{println .}}{{end}}", &name]).unwrap_or_default();
|
||||
let val = |k: &str| env.lines().find_map(|l| l.strip_prefix(&format!("{k}=")).map(String::from)).filter(|v| !v.is_empty());
|
||||
let sign_in = if fresh { None } else { val("ANYBOT_HOST_ID").zip(val("ANYBOT_HOST_TOKEN")) };
|
||||
let r = remake_beside(cfg, user, d, fresh, &name);
|
||||
if r.is_err() {
|
||||
// Not made again: the brain there runs. One found stopped (the manager stopped half way
|
||||
// through an earlier remake, after the old one was stopped) was planned only to be made
|
||||
// again — not woken — and would stay stopped while every try fails.
|
||||
if docker(&["inspect", "-f", "{{.State.Running}}", &name]).is_ok_and(|s| s.trim() == "false") {
|
||||
docker_access(cfg, &name, d);
|
||||
if docker(&["start", &name]).is_ok() {
|
||||
log::info!("AnyBot 大腦管理員:{user} 原本的雲端大腦停著,先叫醒它");
|
||||
}
|
||||
}
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// Docker taken away before an old brain is put back or woken: it keeps the networks it had.
|
||||
/// (Never joined here — whether it was made with Docker isn't known here; the next tick joins
|
||||
/// the one that should be.)
|
||||
fn docker_access(cfg: &Config, name: &str, d: bool) {
|
||||
if let (false, Some(n)) = (d, &cfg.docker_network) {
|
||||
let _ = docker(&["network", "disconnect", n, name]);
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether it is on that network.
|
||||
pub fn on_network(e: &Existing, network: &str) -> bool {
|
||||
e.networks.split(',').any(|x| x.trim() == network)
|
||||
}
|
||||
|
||||
fn remake_beside(cfg: &Config, user: &str, d: bool, fresh: bool, name: &str) -> Result<(), String> {
|
||||
let name = name.to_string();
|
||||
// (Its sign-in read back — when Docker doesn't answer, nothing is done: a new sign-in would
|
||||
// sign the running brain out, and it would stay so if the rest then failed.)
|
||||
let sign_in = if fresh {
|
||||
None
|
||||
} else {
|
||||
let env = docker(&["inspect", "-f", "{{range .Config.Env}}{{println .}}{{end}}", &name]).map_err(|e| format!("讀不到 {user} 雲端大腦的登入,先不換:{e}"))?;
|
||||
let val = |k: &str| env.lines().find_map(|l| l.strip_prefix(&format!("{k}=")).map(String::from)).filter(|v| !v.is_empty());
|
||||
val("ANYBOT_HOST_ID").zip(val("ANYBOT_HOST_TOKEN"))
|
||||
};
|
||||
// A new sign-in, if one is needed, before the old brain is touched (a refusal touches nothing).
|
||||
let (id, token) = match sign_in {
|
||||
Some(s) => s,
|
||||
|
|
@ -391,11 +442,13 @@ fn remake(cfg: &Config, user: &str, d: bool, fresh: bool) -> Result<(), String>
|
|||
let back = |why: String| {
|
||||
let _ = docker(&["rm", "-f", &name]);
|
||||
let _ = docker(&["rename", &aside, &name]);
|
||||
docker_access(cfg, &name, d);
|
||||
let _ = docker(&["start", &name]);
|
||||
let _ = docker(&["rm", "-f", &new]);
|
||||
Err(format!("{user} 的新版雲端大腦開不起來,先用原本的:{why}"))
|
||||
};
|
||||
if let Err(e) = docker(&["rename", &name, &aside]) {
|
||||
docker_access(cfg, &name, d);
|
||||
let _ = docker(&["start", &name]);
|
||||
let _ = docker(&["rm", "-f", &new]);
|
||||
return Err(format!("{user} 的雲端大腦換不了:{e}"));
|
||||
|
|
@ -491,7 +544,7 @@ fn existing() -> Result<HashMap<String, Existing>, String> {
|
|||
if ids.is_empty() {
|
||||
return Ok(HashMap::new());
|
||||
}
|
||||
let mut args = vec!["inspect", "-f", "{{index .Config.Labels \"anybot.brain.user\"}}\t{{.Name}}\t{{.State.Running}}\t{{index .Config.Labels \"anybot.brain.layers\"}}\t{{index .Config.Labels \"anybot.brain.docker\"}}\t{{index .Config.Labels \"anybot.brain.args\"}}\t{{index .Config.Labels \"anybot.brain.host\"}}"];
|
||||
let mut args = vec!["inspect", "-f", "{{index .Config.Labels \"anybot.brain.user\"}}\t{{.Name}}\t{{.State.Running}}\t{{index .Config.Labels \"anybot.brain.layers\"}}\t{{index .Config.Labels \"anybot.brain.docker\"}}\t{{index .Config.Labels \"anybot.brain.args\"}}\t{{index .Config.Labels \"anybot.brain.host\"}}\t{{range $k, $v := .NetworkSettings.Networks}}{{$k}},{{end}}"];
|
||||
args.extend(ids);
|
||||
// (What each was made from is the label set then: a brain from before the label has none, and
|
||||
// is made again once.)
|
||||
|
|
@ -502,19 +555,54 @@ fn existing() -> Result<HashMap<String, Existing>, String> {
|
|||
let _ = docker(&["rm", "-f", &e.name]);
|
||||
}
|
||||
// One kept aside by a remake that was cut short (the manager stopped in the middle): put back
|
||||
// when there is no new one, gone when there is.
|
||||
// when there is no new one, or the new one under its name never started (not known to run —
|
||||
// made again instead); gone once the new one has run.
|
||||
let mut found = found;
|
||||
for (user, e) in aside {
|
||||
let name = container_name(&user);
|
||||
if found.contains_key(&user) {
|
||||
let _ = docker(&["rm", "-f", &e.name]);
|
||||
} else if docker(&["rename", &e.name, &name]).is_ok() {
|
||||
if let Some(now) = found.get(&user) {
|
||||
match aside_fate(docker(&["inspect", "-f", "{{.State.StartedAt}}", &now.name]).ok().as_deref()) {
|
||||
Aside::Gone => {
|
||||
let _ = docker(&["rm", "-f", &e.name]);
|
||||
continue;
|
||||
}
|
||||
Aside::Later => continue,
|
||||
Aside::Back => {
|
||||
if docker(&["rm", "-f", &now.name]).is_err() {
|
||||
continue;
|
||||
}
|
||||
found.remove(&user);
|
||||
}
|
||||
}
|
||||
}
|
||||
if docker(&["rename", &e.name, &name]).is_ok() {
|
||||
found.insert(user, Existing { name, ..e });
|
||||
}
|
||||
}
|
||||
Ok(found)
|
||||
}
|
||||
|
||||
/// What becomes of a brain kept aside (…-old) when one is under its name too.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Aside {
|
||||
/// The new one has run: the remake went through, the old one goes.
|
||||
Gone,
|
||||
/// The new one never started (the manager stopped right then): not known to run — the old one
|
||||
/// back, made again.
|
||||
Back,
|
||||
/// Docker didn't say: both left as they are until it does.
|
||||
Later,
|
||||
}
|
||||
|
||||
/// From when the one under its name started (`State.StartedAt`; Docker's zero time when never).
|
||||
pub fn aside_fate(started_at: Option<&str>) -> Aside {
|
||||
match started_at.map(str::trim) {
|
||||
None | Some("") => Aside::Later,
|
||||
Some(at) if at.starts_with("0001-") => Aside::Back,
|
||||
Some(_) => Aside::Gone,
|
||||
}
|
||||
}
|
||||
|
||||
/// The brains and the ones kept aside (named …-old) by a remake.
|
||||
pub fn parse_inspect_aside(out: &str) -> (Vec<(String, Existing)>, HashMap<String, Existing>) {
|
||||
let mut aside = vec![];
|
||||
|
|
@ -548,7 +636,7 @@ fn parse_inspect_lines(out: &str) -> Vec<(String, Existing)> {
|
|||
let f: Vec<&str> = l.split('\t').map(str::trim).collect();
|
||||
let (user, name) = (*f.first()?, f.get(1)?.trim_start_matches('/'));
|
||||
let at = |i: usize| f.get(i).copied().unwrap_or("").trim_start_matches("<no value>").to_string();
|
||||
(!user.is_empty() && !name.is_empty()).then(|| (user.to_string(), Existing { name: name.to_string(), running: f.get(2) == Some(&"true"), image: at(3), docker: f.get(4) == Some(&"1"), args: at(5), host: at(6) }))
|
||||
(!user.is_empty() && !name.is_empty()).then(|| (user.to_string(), Existing { name: name.to_string(), running: f.get(2) == Some(&"true"), image: at(3), docker: f.get(4) == Some(&"1"), args: at(5), host: at(6), networks: at(7) }))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
@ -625,7 +713,7 @@ mod tests {
|
|||
}
|
||||
|
||||
fn made(user: &str, docker: bool, image: &str, host: &str, c: &Config) -> Existing {
|
||||
Existing { name: container_name(user), running: true, image: image.into(), docker, args: args_hash(user, docker, c), host: host.into() }
|
||||
Existing { name: container_name(user), running: true, image: image.into(), docker, args: args_hash(user, docker, c), host: host.into(), networks: String::new() }
|
||||
}
|
||||
|
||||
/// Every account made since the manager came gets one brain: made once, started again when
|
||||
|
|
@ -751,6 +839,25 @@ mod tests {
|
|||
assert!(found.contains_key("bob") && !found.contains_key("amy"));
|
||||
}
|
||||
|
||||
/// The networks a brain is on are read back, and its way to Docker told apart.
|
||||
#[test]
|
||||
fn a_brains_networks_are_read_back() {
|
||||
let found = super::parse_inspect("amy\t/anybot-brain-amy-1\ttrue\tl1\t1\th\tid\tserver_default,server_docker-api,\nbob\t/anybot-brain-bob-2\ttrue\tl1\t0\th\tid\tserver_default,\n");
|
||||
assert!(super::on_network(&found["amy"], "server_docker-api"));
|
||||
assert!(!super::on_network(&found["bob"], "server_docker-api"));
|
||||
assert!(!super::on_network(&found["bob"], "server_docker"), "a name that only starts the same isn't it");
|
||||
}
|
||||
|
||||
/// A brain kept aside goes once the new one has run, and comes back when it never started.
|
||||
#[test]
|
||||
fn a_brain_kept_aside_comes_back_unless_the_new_one_ran() {
|
||||
use super::{aside_fate, Aside};
|
||||
assert_eq!(aside_fate(Some("2026-11-23T00:37:28.385270556Z\n")), Aside::Gone);
|
||||
assert_eq!(aside_fate(Some("0001-01-01T00:00:00Z\n")), Aside::Back);
|
||||
assert_eq!(aside_fate(None), Aside::Later, "Docker didn't answer");
|
||||
assert_eq!(aside_fate(Some("")), Aside::Later);
|
||||
}
|
||||
|
||||
/// An account that keeps failing is tried less and less often, never less than every half hour.
|
||||
#[test]
|
||||
fn an_account_that_keeps_failing_rests_longer() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue