Public: anyone can read this; the crew can change it.
small: every container, project and machine shows one of five states, each a colour with a word #18633
Merged 58m ago, live in production
scout/forge:scout/18617-small-every-container-project-and-machin → main 0 commits fork 4 threads · 4 unresolved
13 files
changed +307
−22
- docs/features/infra.md
- src/cloud/containers.ts
- src/cloud/tree.ts
- src/machines/five_states.ts
- src/tui/screens/cloudtree.ts
- src/tui/screens/infra.ts
- src/web/five_badge.ts
- src/web/pages/cloud_tree.ts
- src/web/pages/infra.ts
- src/web/style/components.ts
- test/cloud-containers.test.ts
- test/five-states.test.ts
- test/infra-page.test.ts
docs/features/infra.md +7
| @@ -129,3 +129,10 @@ Every machine that exists at its provider has an **At the cloud** button on /inf | ||
| 129 | 129 | |
| 130 | 130 | **Provider console (Hetzner):** a Hetzner machine on /infra has a Provider console button (`o` in the terminal's Infra). It asks Hetzner for the server's own console (`POST /v1/servers/{id}/actions/request_console`, through the audited fetch with the account's sealed key) and shows its `wss://` address and password once, on a page no cache keeps: good for about a minute, kept nowhere. The way in when a box's agent is silent and its firewall lets no SSH through. `src/cloud/provider_console.ts`. |
| 131 | 131 | |
| 132 | ## Five states (#18617) | |
| 133 | ||
| 134 | Every container, machine and project on Infrastructure (the running list, the cloud tree, a container's page) is one of five: **Running**, **Starting**, **Needs attention**, **Stopped**, **Can't tell**. Each is a colour with its word; the word is always there. `fiveOf`'s home is `src/machines/five_states.ts`: one table per raw state (Docker's, a container's health check, a machine row's), so the page and the terminal say the same. A raw state with no row is Can't tell, and says it does not know the word. | |
| 135 | ||
| 136 | - **Needs attention** says why in plain words and offers one button: Restart for a failing health check, Start for a dead container, Clear this row for a failed machine with nothing at its provider. A container that keeps restarting (a crash loop) needs attention too. A project offers its worst container's fix. On the cloud tree a game container's own power control is the fix, so the pane draws no second. In the terminal, `f` on the cloud tree does Restart/Start for a container, and `c` on the Infra machine list does Clear. | |
| 137 | - A project (a machine and its containers) is its worst container; a machine with none on record is Can't tell. | |
| 138 | - The terminal shows the same words in `src/tui/screens/infra.ts` and `src/tui/screens/cloudtree.ts`. |
src/cloud/containers.ts +5 −1
| @@ -14,6 +14,7 @@ import { nowWords } from "../games/gist.ts"; | ||
| 14 | 14 | import { heldOn, type Held } from "../games/hold.ts"; |
| 15 | 15 | import { boxNamed, listBoxes, logsOf, mayDo, roleOn, seesBox, servicesOf, type GameBox, type GameService } from "../games/store.ts"; |
| 16 | 16 | import { agoWords } from "../kernel/ago.ts"; |
| 17 | import { containerFive, fiveLine, type Told } from "../machines/five_states.ts"; | |
| 17 | 18 | |
| 18 | 19 | export interface Container { |
| 19 | 20 | /** Where it runs: the game box's name. */ |
| @@ -38,6 +39,8 @@ export interface Container { | ||
| 38 | 39 | /** Its own page, where it is changed. */ |
| 39 | 40 | readonly page: string; |
| 40 | 41 | readonly readAt: string; |
| 42 | /** One of the five states (`machines/five_states.ts`), with its reason and the one fix. */ | |
| 43 | readonly five: Told; | |
| 41 | 44 | } |
| 42 | 45 | |
| 43 | 46 | /** A box syncs every few seconds; past this its last word is no longer what runs now. */ |
| @@ -97,6 +100,7 @@ export function containersIn(db: Database, viewer: User | undefined, box: GameBo | ||
| 97 | 100 | image: owner ? (ps?.image ?? "") || UNKNOWN : "", |
| 98 | 101 | page: `/games/${box.name}/${s.name}`, |
| 99 | 102 | readAt: s.seenAt, |
| 103 | five: containerFive({ state: s.state, health: s.health, asleep: s.asleep, silent }), | |
| 100 | 104 | }; |
| 101 | 105 | }); |
| 102 | 106 | } |
| @@ -112,7 +116,7 @@ export const CONTAINER_COLUMNS = [ | ||
| 112 | 116 | |
| 113 | 117 | /** One cell as text; a value not known is an em dash, never 0. */ |
| 114 | 118 | export function containerCell(c: Container, field: string, now: number): string { |
| 115 | const text = field === "name" ? c.name : field === "state" ? c.state : field === "cpu" ? c.cpuLimit : field === "memory" ? c.memoryLimit : field === "started" ? (c.startedAt === "" ? "" : agoWords(c.startedAt, now)) : ""; | |
| 119 | const text = field === "name" ? c.name : field === "state" ? fiveLine(c.five) : field === "cpu" ? c.cpuLimit : field === "memory" ? c.memoryLimit : field === "started" ? (c.startedAt === "" ? "" : agoWords(c.startedAt, now)) : ""; | |
| 116 | 120 | return text === "" ? "—" : text; |
| 117 | 121 | } |
| 118 | 122 |
src/cloud/tree.ts +9
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | import type { Container, Inside, Logged } from "./containers.ts"; |
| 14 | import { fiveLine, projectFive, type Told } from "../machines/five_states.ts"; | |
| 14 | 15 | import type { Filed } from "./declared_in.ts"; |
| 15 | 16 | import { dollars } from "./prices.ts"; |
| 16 | 17 | import { PROJECT_CLOUDS, readEveryMs, type AccountRead, type CloudResource, type ProjectView } from "./projects.ts"; |
| @@ -622,9 +623,17 @@ export const CONTAINER_COST = "Its cost is inside its machine's."; | ||
| 622 | 623 | /** "3 containers", or "" for a machine with none known. */ |
| 623 | 624 | export const containersSay = (n: CloudNode): string => (n.children.length === 0 || n.level !== "resource" ? "" : many(n.children.length, "container")); |
| 624 | 625 | |
| 626 | /** | |
| 627 | * A machine's containers as one project: "Needs attention: 1 of 3 needs attention: ...", "" for | |
| 628 | * none known. | |
| 629 | */ | |
| 630 | export const projectSay = (n: CloudNode): string => (n.level !== "resource" || n.children.length === 0 ? "" : fiveLine(projectOf(n))); | |
| 631 | export const projectOf = (n: CloudNode): Told => projectFive(n.children.flatMap((c) => (c.container === undefined ? [] : [c.container.five]))); | |
| 632 | ||
| 625 | 633 | const containerFacts = (c: Container): Fact[] => |
| 626 | 634 | [ |
| 627 | 635 | { label: "Kind", value: "container" }, |
| 636 | { label: "Status", value: fiveLine(c.five) }, | |
| 628 | 637 | { label: "State", value: c.state }, |
| 629 | 638 | { label: "Health", value: c.health }, |
| 630 | 639 | { label: "CPU", value: c.cpu }, |
src/machines/five_states.ts added +115
| @@ -0,0 +1,115 @@ | ||
| 1 | /** | |
| 2 | * The five states (#18617, P16): every container, project and machine the forge shows is one of | |
| 3 | * Running, Starting, Needs attention, Stopped or Can't tell, a colour with a word. `fiveOf` is the | |
| 4 | * one function that maps what an agent or a provider reports to one of them; every page, tree row | |
| 5 | * and terminal line that shows a state asks it. Plain data in, plain data out: nothing here | |
| 6 | * touches a database or a request. | |
| 7 | * | |
| 8 | * Can't tell is the default: a raw state this file has no row for is that, and says so. | |
| 9 | */ | |
| 10 | ||
| 11 | export const FIVE = ["running", "starting", "attention", "stopped", "unknown"] as const; | |
| 12 | export type Five = (typeof FIVE)[number]; | |
| 13 | ||
| 14 | export const WORDS: Record<Five, string> = { running: "Running", starting: "Starting", attention: "Needs attention", stopped: "Stopped", unknown: "Can't tell" }; | |
| 15 | ||
| 16 | /** | |
| 17 | * The one button that fixes a state that needs attention: `act` is what the page posts or the | |
| 18 | * terminal asks. | |
| 19 | */ | |
| 20 | export interface Fix { | |
| 21 | readonly act: "restart" | "start" | "clear"; | |
| 22 | readonly label: string; | |
| 23 | } | |
| 24 | ||
| 25 | export interface Told { | |
| 26 | readonly state: Five; | |
| 27 | /** One of `WORDS`: the word that always goes with the colour. */ | |
| 28 | readonly word: string; | |
| 29 | /** Plain words for why, empty when the word says it all. */ | |
| 30 | readonly reason: string; | |
| 31 | /** Only a state that needs attention has one, and only when something can be done from here. */ | |
| 32 | readonly fix?: Fix; | |
| 33 | } | |
| 34 | ||
| 35 | const told = (state: Five, reason = "", fix?: Fix): Told => ({ state, word: WORDS[state], reason, ...(fix === undefined ? {} : { fix }) }); | |
| 36 | ||
| 37 | /** What Docker says of a container (`docker ps` states) and what the one row of each maps to. */ | |
| 38 | export const CONTAINER_STATES: Record<string, Five> = { | |
| 39 | running: "running", | |
| 40 | created: "starting", | |
| 41 | restarting: "attention", | |
| 42 | paused: "stopped", | |
| 43 | exited: "stopped", | |
| 44 | removing: "stopped", | |
| 45 | dead: "attention", | |
| 46 | }; | |
| 47 | /** A running container's health check: anything else (none, "") leaves it running. */ | |
| 48 | export const HEALTH_STATES: Record<string, Five> = { "": "running", none: "running", healthy: "running", starting: "starting", unhealthy: "attention" }; | |
| 49 | ||
| 50 | export interface ContainerSeen { | |
| 51 | readonly state: string; | |
| 52 | readonly health: string; | |
| 53 | /** Stopped by its sleep: it starts when someone joins. */ | |
| 54 | readonly asleep?: boolean; | |
| 55 | /** Its box has not synced lately, so what it last said is not what runs now. */ | |
| 56 | readonly silent?: boolean; | |
| 57 | } | |
| 58 | ||
| 59 | const UNHEALTHY: Fix = { act: "restart", label: "Restart" }; | |
| 60 | ||
| 61 | export function containerFive(c: ContainerSeen): Told { | |
| 62 | if (c.silent === true) return told("unknown", "its machine has not reported lately"); | |
| 63 | if (c.asleep === true) return told("stopped", "asleep, it wakes when someone joins"); | |
| 64 | const raw = c.state.toLowerCase(); | |
| 65 | const base = Object.hasOwn(CONTAINER_STATES, raw) ? CONTAINER_STATES[raw]! : undefined; | |
| 66 | if (base === undefined) return told("unknown", raw === "" ? "its machine has not said" : `its machine said "${raw.slice(0, 40)}", which this forge does not know`); | |
| 67 | if (raw === "running") { | |
| 68 | const h = c.health.toLowerCase(); | |
| 69 | const seen = Object.hasOwn(HEALTH_STATES, h) ? HEALTH_STATES[h]! : undefined; | |
| 70 | if (seen === undefined) return told("unknown", `its health check said "${h.slice(0, 40)}", which this forge does not know`); | |
| 71 | return seen === "attention" ? told("attention", "its health check is failing", UNHEALTHY) : told(seen, seen === "starting" ? "its health check is still starting" : ""); | |
| 72 | } | |
| 73 | if (raw === "restarting") return told("attention", "it keeps restarting, which is a crash loop", UNHEALTHY); | |
| 74 | if (base === "attention") return told("attention", "it died and was not restarted", { act: "start", label: "Start" }); | |
| 75 | if (raw === "paused") return told("stopped", "it is paused"); | |
| 76 | return told(base); | |
| 77 | } | |
| 78 | ||
| 79 | /** What a machine's row holds (`pinned_forge.state`). */ | |
| 80 | export const MACHINE_STATES: Record<string, Five> = { new: "starting", creating: "starting", running: "running", failed: "attention", retired: "stopped" }; | |
| 81 | ||
| 82 | export interface MachineSeen { | |
| 83 | readonly state: string; | |
| 84 | /** | |
| 85 | * Why a failed machine has nothing to end: "nothing exists at Hetzner", empty when something | |
| 86 | * does. | |
| 87 | */ | |
| 88 | readonly clear?: string; | |
| 89 | } | |
| 90 | ||
| 91 | export function machineFive(m: MachineSeen): Told { | |
| 92 | const raw = m.state.toLowerCase(); | |
| 93 | const five = Object.hasOwn(MACHINE_STATES, raw) ? MACHINE_STATES[raw]! : undefined; | |
| 94 | if (five === undefined) return told("unknown", raw === "" ? "its row says nothing" : `its row says "${raw.slice(0, 40)}", which this forge does not know`); | |
| 95 | if (five === "attention") return m.clear === undefined || m.clear === "" ? told("attention", "making it failed") : told("attention", m.clear, { act: "clear", label: "Clear this row" }); | |
| 96 | return told(five, five === "starting" ? "it is being made" : ""); | |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * A project (a game box and what it runs): the worst of its containers. Nothing running and nothing | |
| 101 | * known is Can't tell; every container stopped is Stopped. | |
| 102 | */ | |
| 103 | export function projectFive(members: readonly Told[]): Told { | |
| 104 | if (members.length === 0) return told("unknown", "it has no containers on record"); | |
| 105 | const n = (s: Five): number => members.filter((m) => m.state === s).length; | |
| 106 | const bad = members.find((m) => m.state === "attention"); | |
| 107 | if (bad !== undefined) return told("attention", `${n("attention")} of ${members.length} needs attention: ${bad.reason}`, bad.fix); | |
| 108 | if (n("unknown") > 0) return told("unknown", `${n("unknown")} of ${members.length} cannot be told`); | |
| 109 | if (n("starting") > 0) return told("starting", `${n("starting")} of ${members.length} still starting`); | |
| 110 | if (n("running") === 0) return told("stopped", "none of its containers is running"); | |
| 111 | return told("running", n("running") === members.length ? "" : `${n("running")} of ${members.length} containers running`); | |
| 112 | } | |
| 113 | ||
| 114 | /** One line for a terminal row or a title: "Needs attention: its health check is failing". */ | |
| 115 | export const fiveLine = (t: Told): string => (t.reason === "" ? t.word : `${t.word}: ${t.reason}`); |
src/tui/screens/cloudtree.ts +23 −3
| @@ -6,7 +6,7 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | import { cloudResources, readProjectsNow } from "../../actions/infra.ts"; |
| 9 | import { cloudCell, CLOUD_COLUMNS, FIRST_COLUMNS, firstCell, cloudName, CONTAINER_COST, containersSay, costLine, createdLabel, cutSays, factsOf, findingOf, isStale, largestCosts, listedOf, moneyMark, nodeAt, orderSays, readingOf, tabOf, tabsOf, unreadAccounts, unreadLine, unpricedSays, type CloudNode, type CloudView, type NodeTab } from "../../cloud/tree.ts"; | |
| 9 | import { cloudCell, CLOUD_COLUMNS, FIRST_COLUMNS, firstCell, cloudName, CONTAINER_COST, containersSay, costLine, createdLabel, projectSay, cutSays, factsOf, findingOf, isStale, largestCosts, listedOf, moneyMark, nodeAt, orderSays, readingOf, tabOf, tabsOf, unreadAccounts, unreadLine, unpricedSays, type CloudNode, type CloudView, type NodeTab } from "../../cloud/tree.ts"; | |
| 10 | 10 | import { noFileSays, placeSays } from "../../cloud/declared_in.ts"; |
| 11 | 11 | import { filesByNode } from "../../cloud/declared_read.ts"; |
| 12 | 12 | import { CONTAINER_COLUMNS, containerCell, logsOfContainer } from "../../cloud/containers.ts"; |
| @@ -19,6 +19,8 @@ import { style } from "../term/style.ts"; | ||
| 19 | 19 | import { truncate, wrap } from "../term/width.ts"; |
| 20 | 20 | import { fill, len, split } from "../ui/layout.ts"; |
| 21 | 21 | import { tableLines } from "../ui/list.ts"; |
| 22 | import { askPower, refusesNow } from "../../games/power.ts"; | |
| 23 | import { boxNamed } from "../../games/store.ts"; | |
| 22 | 24 | import { askEnd } from "./cloud_end.ts"; |
| 23 | 25 | import { accountScreen } from "./cloud_account.ts"; |
| 24 | 26 | import { Filter, ListState, Scroll, ago, drawLines, hitList, openOnWeb } from "./common.ts"; |
| @@ -70,9 +72,9 @@ export function cloudLines(v: CloudView, n: CloudNode, width = 80, tab: NodeTab | ||
| 70 | 72 | if (tab === "file") return [...head, "", ...fileLines(v, n, unset)]; |
| 71 | 73 | if (tab === "logs") return [...head, "", ...(v.logs === undefined || v.logs.lines.length === 0 ? [v.logs?.says ?? "Its lines are not here."] : v.logs.lines)]; |
| 72 | 74 | const facts = factsOf(v.whole, n).map((f) => `${f.label.padEnd(12)} ${f.value}`); |
| 73 | if (n.container !== undefined) return [...head, "", ...facts, read]; | |
| 75 | if (n.container !== undefined) return [...head, "", ...facts, ...(n.container.five.fix === undefined ? [] : [`${n.container.five.reason}: press f to ${n.container.five.fix.label.toLowerCase()} it`]), read]; | |
| 74 | 76 | if (n.resource !== undefined) |
| 75 | return [...head, "", ...facts, `${createdLabel(n.resource).padEnd(12)} ${n.resource.createdAt.slice(0, 10) || "not known"}`, read, ...(n.children.length === 0 ? [] : ["", containersSay(n), ...tableLines(CONTAINER_COLUMNS.map((k) => ({ field: { name: k.field, type: "text" }, label: k.label })), v.q.line, n.children.map((c) => CONTAINER_COLUMNS.map((k) => containerCell(c.container!, k.field, v.now))), width)])]; | |
| 77 | return [...head, "", ...(n.children.length === 0 ? [] : [`${"Status".padEnd(12)} ${projectSay(n)}`]), ...facts, `${createdLabel(n.resource).padEnd(12)} ${n.resource.createdAt.slice(0, 10) || "not known"}`, read, ...(n.children.length === 0 ? [] : ["", containersSay(n), ...tableLines(CONTAINER_COLUMNS.map((k) => ({ field: { name: k.field, type: "text" }, label: k.label })), v.q.line, n.children.map((c) => CONTAINER_COLUMNS.map((k) => containerCell(c.container!, k.field, v.now))), width)])]; | |
| 76 | 78 | // The first page is the page's own table: resource, kind, where, and a cost in words. |
| 77 | 79 | const first = n.path.length === 0; |
| 78 | 80 | const columns = first ? FIRST_COLUMNS : CLOUD_COLUMNS; |
| @@ -166,6 +168,7 @@ export function cloudTreeScreen(f: Session, ctx: Ctx): Screen { | ||
| 166 | 168 | binding(["a"], "check the account: what was not read, and the fix"), |
| 167 | 169 | binding(["w"], "open on the web"), |
| 168 | 170 | binding(["x", "X"], "plan a stop (x) or a delete (X) of the machine or volume"), |
| 171 | binding(["f"], "fix a container that needs attention: restart or start it"), | |
| 169 | 172 | binding(["P"], "read every account now"), |
| 170 | 173 | ], |
| 171 | 174 | render(area, s) { |
| @@ -284,6 +287,23 @@ export function cloudTreeScreen(f: Session, ctx: Ctx): Screen { | ||
| 284 | 287 | ctx.push(accountScreen(f, ctx, account[0]!, account[1]!)); |
| 285 | 288 | return true; |
| 286 | 289 | } |
| 290 | if (e.key === "f") { | |
| 291 | const at = (focus === "pane" && opened !== undefined ? nodeAt(v.root, opened) : undefined) ?? n; | |
| 292 | const c = at.container; | |
| 293 | const act = c?.five.fix?.act; | |
| 294 | if (c === undefined || (act !== "restart" && act !== "start")) return false; | |
| 295 | const box = boxNamed(f.app.db, c.box); | |
| 296 | if (box === undefined) return false; | |
| 297 | void askPower(f.app, f.user, box, c.name, act) | |
| 298 | .then((asked) => { | |
| 299 | note = !asked.ok ? `${act} ${box.name}/${c.name}: not asked, ${refusesNow(box, act) ? `${box.name} is held.` : asked.error}` : `${act} ${box.name}/${c.name}: asked.`; | |
| 300 | }) | |
| 301 | .catch(() => { | |
| 302 | note = `${act} ${box.name}/${c.name}: not asked, the forge could not check the box. Try again.`; | |
| 303 | }) | |
| 304 | .finally(() => ctx.invalidate()); | |
| 305 | return true; | |
| 306 | } | |
| 287 | 307 | if (e.key === "x" || e.key === "X") { |
| 288 | 308 | const at = (focus === "pane" && opened !== undefined ? nodeAt(v.root, opened) : undefined) ?? n; |
| 289 | 309 | if (at.resource === undefined) return false; |
src/tui/screens/infra.ts +2 −1
| @@ -18,6 +18,7 @@ import { infraCode, type CodeFile } from "../../cloud/infra_read.ts"; | ||
| 18 | 18 | import { buildOf, composeChoices, providerConsole, databaseChoices, infraOf, cloudResources, nameFor, planCompose, planDatabase, planRecord, recordAccounts, startBuild, type Compose, type Planned, type Running } from "../../actions/infra.ts"; |
| 19 | 19 | import { deleteSnapshot, endWithSnapshot, restoredName, restoreSnapshot } from "../../actions/infra_snapshot.ts"; |
| 20 | 20 | import { planMinecraft } from "../../actions/minecraft.ts"; |
| 21 | import { fiveLine, machineFive } from "../../machines/five_states.ts"; | |
| 21 | 22 | import { LIFETIME_DAYS, clearPinned, deployWords, endsWords, keepPinned, retirePinned, retireWords } from "../../actions/pinned.ts"; |
| 22 | 23 | import type { SnapshotView } from "../../actions/infra_snapshot.ts"; |
| 23 | 24 | import { machineView } from "../../actions/machine_view.ts"; |
| @@ -447,7 +448,7 @@ function infraWithCompose(f: Session, ctx: Ctx): { screen: Screen; compose: (par | ||
| 447 | 448 | ...v.value.groups.flatMap((g) => |
| 448 | 449 | g.machines.map((m) => ({ |
| 449 | 450 | title: `${m.name} · ${month(m.monthlyCents)}`, |
| 450 | secondary: `${g.title} · ${m.where} · ${m.clear !== "" ? m.clear : m.endsAt === null ? "no end" : `ends ${m.ends}`}${m.state === "running" ? "" : ` · ${m.state}`}${m.agentBack === undefined ? "" : ` · on its old agent: ${m.agentBack.length > 80 ? `${m.agentBack.slice(0, 79)}…` : m.agentBack}`}`, | |
| 451 | secondary: `${g.title} · ${m.where} · ${fiveLine(machineFive(m))}${m.clear !== "" ? ` (press c to clear this row)` : ` · ${m.endsAt === null ? "no end" : `ends ${m.ends}`}`}${m.agentBack === undefined ? "" : ` · on its old agent: ${m.agentBack.length > 80 ? `${m.agentBack.slice(0, 79)}…` : m.agentBack}`}`, | |
| 451 | 452 | meta: m.account, |
| 452 | 453 | })), |
| 453 | 454 | ), |
src/web/five_badge.ts added +32
| @@ -0,0 +1,32 @@ | ||
| 1 | /** | |
| 2 | * One of the five states as the page draws it (#18617): a badge whose word is always there, so it | |
| 3 | * reads in black and white, and for Needs attention its reason in plain words and the one button | |
| 4 | * that fixes it. The mapping is `machines/five_states.ts`; this only draws it. | |
| 5 | */ | |
| 6 | ||
| 7 | import type { Fix, Five, Told } from "../machines/five_states.ts"; | |
| 8 | import { html, type Html } from "./html.ts"; | |
| 9 | import type { IconName } from "./icons.ts"; | |
| 10 | import { btn, statusBadge, type Status } from "./ui.ts"; | |
| 11 | ||
| 12 | const LOOK: Record<Five, { status: Status; icon: IconName }> = { | |
| 13 | running: { status: "ok", icon: "play" }, | |
| 14 | starting: { status: "running", icon: "loader" }, | |
| 15 | attention: { status: "failed", icon: "alert" }, | |
| 16 | stopped: { status: "cut", icon: "square" }, | |
| 17 | unknown: { status: "held", icon: "help" }, | |
| 18 | }; | |
| 19 | ||
| 20 | /** The badge alone: colour, icon and the word. */ | |
| 21 | export const fiveBadge = (t: Told): Html => html`<span data-five="${t.state}">${statusBadge(LOOK[t.state].status, t.word, LOOK[t.state].icon)}</span>`; | |
| 22 | ||
| 23 | /** Where a fix posts: the page that draws it says, since a container's and a machine's differ. */ | |
| 24 | export type FixTo = (fix: Fix) => { readonly action: string; readonly confirm: boolean; readonly back?: string } | undefined; | |
| 25 | ||
| 26 | /** The badge, and a state that needs attention says why and offers its one fix. */ | |
| 27 | export function fiveBlock(t: Told, to: FixTo, who: string): Html { | |
| 28 | const at = t.fix === undefined ? undefined : to(t.fix); | |
| 29 | return html`<span class="row-fields" data-five-block="${t.state}">${fiveBadge(t)}${t.reason === "" ? null : html`<span data-five-reason>${t.reason}</span>`}${t.fix === undefined || at === undefined | |
| 30 | ? null | |
| 31 | : html`<form method="${at.confirm ? "get" : "post"}" action="${at.confirm ? "/-/confirm" : at.action}">${at.confirm ? html`<input type="hidden" name="to" value="${at.action}" />` : null}${at.back === undefined ? null : html`<input type="hidden" name="back" value="${at.back}" />`}${btn(t.fix.label, { size: "sm", variant: "ghost", label: `${t.fix.label} ${who}` })}</form>`}</span>`; | |
| 32 | } |
src/web/pages/cloud_tree.ts +19 −2
| @@ -34,6 +34,7 @@ import { | ||
| 34 | 34 | moneyWords, |
| 35 | 35 | linkedFacts, |
| 36 | 36 | containersSay, |
| 37 | projectOf, | |
| 37 | 38 | CONTAINER_COST, |
| 38 | 39 | accountName, |
| 39 | 40 | INCOMPLETE, |
| @@ -54,6 +55,8 @@ import { | ||
| 54 | 55 | } from "../../cloud/tree.ts"; |
| 55 | 56 | import { CONTAINER_COLUMNS, containerCell } from "../../cloud/containers.ts"; |
| 56 | 57 | import { dollars } from "../../cloud/prices.ts"; |
| 58 | import type { Fix } from "../../machines/five_states.ts"; | |
| 59 | import { fiveBadge, fiveBlock } from "../five_badge.ts"; | |
| 57 | 60 | import { emptySection } from "../empty_words.ts"; |
| 58 | 61 | import { html, raw, type Html } from "../html.ts"; |
| 59 | 62 | import { icon } from "../icons.ts"; |
| @@ -112,7 +115,7 @@ function level(v: CloudView, n: CloudNode, current: readonly string[], openAll: | ||
| 112 | 115 | const href = cloudHref(c.path, v.q); |
| 113 | 116 | const at = c.path.every((p, i) => current[i] === p); |
| 114 | 117 | const here = at && current.length === c.path.length; |
| 115 | const kind = c.container !== undefined ? c.container.state : c.resource === undefined ? "" : `${c.resource.kind}${c.held === undefined ? "" : " · held"}${unused(c.resource) ? " · unattached" : ""}${c.children.length === 0 ? "" : ` · ${containersSay(c)}`}`; | |
| 118 | const kind = c.container !== undefined ? c.container.five.word : c.resource === undefined ? "" : `${c.resource.kind}${c.held === undefined ? "" : " · held"}${unused(c.resource) ? " · unattached" : ""}${c.children.length === 0 ? "" : ` · ${containersSay(c)} · ${projectOf(c).word}`}`; | |
| 116 | 119 | if (kind !== "" && c.children.length === 0) |
| 117 | 120 | return html`<li><a class="ex-file" data-ex-file data-nav tabindex="-1" href="${href}" ${here ? raw('aria-current="page"') : null}><span class="truncate">${c.name}</span><span class="ex-kind">${kind}</span>${cost(c)}</a></li>`; |
| 118 | 121 | const open = at || (openAll && c.resource === undefined) || c.level === "cloud" || (c.level === "account" && c.children.length <= OPEN_ACCOUNT); |
| @@ -287,6 +290,19 @@ function billLine(n: CloudNode): Html { | ||
| 287 | 290 | return html`<p class="muted small" data-bill>${now === undefined ? null : html`${monthName(now.from)} so far ${dollars(now.cents)} · `}${taxSays(last ?? now) === "" ? null : html`${taxSays(last ?? now)} · `}Before credits · Bill read ${time(n.read!.bill!.readAt)}</p>`; |
| 288 | 291 | } |
| 289 | 292 | |
| 293 | /** | |
| 294 | * The state of a container or project and its one fix. A container whose pane already draws the | |
| 295 | * power control gets the reason only; a project offers its worst container's fix, posted to that | |
| 296 | * container's route. | |
| 297 | */ | |
| 298 | function fiveHead(v: CloudView, n: CloudNode, powered: boolean): Html | null { | |
| 299 | const route = (c: CloudNode, f: Fix) => ({ action: `/infra/container/${c.container!.box}/${c.name}/${f.act}`, confirm: false, back: cloudHref(n.path, v.q) }); | |
| 300 | if (n.container !== undefined) return fiveBlock(n.container.five, (f) => (powered ? undefined : route(n, f)), n.name); | |
| 301 | if (n.children.length === 0) return null; | |
| 302 | const bad = n.children.find((c) => c.container?.five.state === "attention" && c.container.five.fix !== undefined); | |
| 303 | return fiveBlock(projectOf(n), (f) => (bad === undefined ? undefined : route(bad, f)), n.name); | |
| 304 | } | |
| 305 | ||
| 290 | 306 | /** What is open beside the tree: a branch's costliest resources, or one resource's facts or file. */ |
| 291 | 307 | export function cloudPane(v: CloudView, n: CloudNode, tab: NodeTab = tabOf(n, null), game?: { readonly power: Html | null; readonly logs: Html }, endActs: readonly { act: string; href: string }[] = []): Html { |
| 292 | 308 | const r = n.resource; |
| @@ -307,6 +323,7 @@ export function cloudPane(v: CloudView, n: CloudNode, tab: NodeTab = tabOf(n, nu | ||
| 307 | 323 | const head = html`${trail(v, n)} |
| 308 | 324 | <div class="cloud-head"><h2>${n.name}</h2>${game?.power ?? null}</div> |
| 309 | 325 | ${sum(v, n)} |
| 326 | ${fiveHead(v, n, game?.power != null)} | |
| 310 | 327 | ${has.length === 0 ? null : tabs(has.map((t) => ({ label: t.label, href: cloudHref(n.path, v.q, t.id), current: t.id === tab })), `${n.name}'s tabs`)}`; |
| 311 | 328 | if (tab === "logs") return html`<section id="code-pane" data-path="${n.name}" data-cloud-pane="resource">${head}${game?.logs ?? logsTab(v)}</section>`; |
| 312 | 329 | if (tab === "file") return html`<section id="code-pane" data-path="${n.name}" data-cloud-pane="resource">${head}${fileTab(v, n)}</section>`; |
| @@ -321,7 +338,7 @@ export function cloudPane(v: CloudView, n: CloudNode, tab: NodeTab = tabOf(n, nu | ||
| 321 | 338 | </dl> |
| 322 | 339 | ${n.children.length === 0 ? null : html`<h2>${containersSay(n)}</h2><div class="table-scroll" role="region" tabindex="0" aria-label="Containers"><table class="table" data-dense> |
| 323 | 340 | <thead><tr>${CONTAINER_COLUMNS.map((k) => html`<th scope="col">${k.label}</th>`)}</tr></thead> |
| 324 | <tbody>${n.children.map((c) => html`<tr>${CONTAINER_COLUMNS.map((k) => html`<td>${k.field === "name" ? html`<a href="${cloudHref(c.path, v.q)}">${c.name}</a>` : containerCell(c.container!, k.field, v.now)}</td>`)}</tr>`)}</tbody> | |
| 341 | <tbody>${n.children.map((c) => html`<tr>${CONTAINER_COLUMNS.map((k) => html`<td>${k.field === "name" ? html`<a href="${cloudHref(c.path, v.q)}">${c.name}</a>` : k.field === "state" ? fiveBadge(c.container!.five) : containerCell(c.container!, k.field, v.now)}</td>`)}</tr>`)}</tbody> | |
| 325 | 342 | </table></div>`} |
| 326 | 343 | <div class="form-actions">${endActs.map((e) => btn(e.act === "stop" ? "Stop…" : "Delete…", { href: e.href, variant: e.act === "delete" ? "danger" : "ghost", label: `Plan to ${e.act} ${n.name}` }))}${btn("Ask about this", { href: `/talk?about=${encodeURIComponent(`cloud:${n.path.join("/")}`)}` })}</div> |
| 327 | 344 | </section>`; |
src/web/pages/infra.ts +5 −9
| @@ -26,6 +26,8 @@ import { CLOUD_TREE_CSS, cloudLayout, cloudTotal } from "./cloud_tree.ts"; | ||
| 26 | 26 | import type { CloudView } from "../../cloud/tree.ts"; |
| 27 | 27 | import { html, raw, type Html } from "../html.ts"; |
| 28 | 28 | import { icon } from "../icons.ts"; |
| 29 | import { fiveBlock } from "../five_badge.ts"; | |
| 30 | import { machineFive } from "../../machines/five_states.ts"; | |
| 29 | 31 | import { btn, crumbs, disclosure, field as textField, row, rowList, selectField } from "../ui.ts"; |
| 30 | 32 | import { DNS_NAME_PATTERN } from "../dns_name_pattern.ts"; |
| 31 | 33 | |
| @@ -171,24 +173,18 @@ const machineActions = (r: Running): Html => | ||
| 171 | 173 | <input type="hidden" name="to" value="/infra/${r.name}/end" /> |
| 172 | 174 | ${btn("End now", { variant: "ghost", size: "sm", label: `End ${r.name} now` })} |
| 173 | 175 | </form>` |
| 174 | : r.clear === "" | |
| 175 | ? null | |
| 176 | : html`<form method="get" action="/-/confirm"> | |
| 177 | <input type="hidden" name="to" value="/infra/${r.name}/clear" /> | |
| 178 | ${btn("Clear this row", { variant: "ghost", size: "sm", label: `Clear ${r.name}: nothing exists at its provider` })} | |
| 179 | </form>`} | |
| 176 | : null} | |
| 180 | 177 | </div>`; |
| 181 | 178 | |
| 182 | 179 | function machineRow(r: Running, cloud: { label: string; value: string } | undefined): Html { |
| 180 | const five = machineFive(r); | |
| 183 | 181 | const ends = r.endsAt === null ? "No end" : `Ends ${r.ends}`; |
| 184 | 182 | return row({ |
| 185 | 183 | // What its provider says of it, as a fact: "DigitalOcean — running · 4 vCPU 8 GB · …". |
| 186 | 184 | ...(cloud === undefined ? {} : { facts: [cloud] }), |
| 187 | 185 | id: `machine-${r.name}`, |
| 188 | 186 | title: html`<span class="mono">${r.name}</span>`, |
| 189 | meta: html`${r.clear !== "" | |
| 190 | ? html`<span data-where>${r.where} · ${r.state}</span><span>${r.clear}</span>` | |
| 191 | : html`<span data-where>${r.where}${r.state === "running" ? null : html` · ${r.state}`}</span><span data-ends>${ends}</span>`}${r.agentBack === undefined ? null : html`<span data-agent-back>On its old agent: ${r.agentBack}</span>`}`, | |
| 187 | meta: html`<span data-where>${r.where}</span>${fiveBlock(five, (f) => (f.act === "clear" ? { action: `/infra/${r.name}/clear`, confirm: true } : undefined), r.name)}${r.clear !== "" ? null : html`<span data-ends>${ends}</span>`}${r.agentBack === undefined ? null : html`<span data-agent-back>On its old agent: ${r.agentBack}</span>`}`, | |
| 192 | 188 | trail: html`<span>${money(r.monthlyCents)}<span class="muted"> a month</span></span>`, |
| 193 | 189 | after: machineActions(r), |
| 194 | 190 | data: { state: r.state }, |
src/web/style/components.ts +2
| @@ -605,6 +605,8 @@ span.ex-pic { display: inline-flex; align-items: center; justify-content: center | ||
| 605 | 605 | .row-children > li[data-disk] > .row-after { display: block; padding: 0 14px 10px 40px; overflow-wrap: anywhere; } |
| 606 | 606 | .row-children > li[data-disk] > .row-after > p { margin: 0; } |
| 607 | 607 | .archived-list .row-title, .archived-list .row-meta { white-space: normal; overflow-wrap: anywhere; } |
| 608 | .row-meta:has([data-five-block]) { white-space: normal; overflow: visible; overflow-wrap: anywhere; } | |
| 609 | .row-meta [data-five-block] { display: inline-flex; max-width: 100%; } | |
| 608 | 610 | .moved-from { margin: 8px 0 0; overflow-wrap: anywhere; } |
| 609 | 611 | @media (max-width: 640px) { |
| 610 | 612 | .archived-list .list > li.has-after > .row { flex-basis: 100%; } |
test/cloud-containers.test.ts +5 −5
| @@ -338,7 +338,7 @@ test("Restart and Stop from the container page: same origin, an operator's, queu | ||
| 338 | 338 | }); |
| 339 | 339 | |
| 340 | 340 | test("the terminal: the same rows and facts, by keys", async () => { |
| 341 | expect(cloudLines(view(), at(MACHINE)).join("\n")).toMatch(/3 containers\nName +State +CPU limit +Memory limit +Started\nfresh +running/); | |
| 341 | expect(cloudLines(view(), at(MACHINE)).join("\n")).toMatch(/3 containers\nName +State +CPU limit +Memory limit +Started\nfresh +Running/); | |
| 342 | 342 | const lines = cloudLines(view(), at(HUB)).join("\n"); |
| 343 | 343 | for (const [label, value] of Object.entries(facts(HUB))) expect(lines).toContain(`${label.padEnd(12)} ${value}`); |
| 344 | 344 | const terminal = headlessTerminal(160, 40); |
| @@ -380,12 +380,12 @@ test("a machine's containers are one table: name, state, CPU limit, memory limit | ||
| 380 | 380 | expect(page).toContain('role="region" tabindex="0" aria-label="Containers"'); |
| 381 | 381 | expect(page).toContain('<a href="/infra/-/hetzner/eu/games/machine:77/container:hub">hub</a>'); |
| 382 | 382 | expect(cells(page)).toEqual([ |
| 383 | ["hub", "running", "1.5 cores", "2.0 GiB", "26 hours ago"], | |
| 384 | ["survival", "exited", "—", "—", "—"], | |
| 383 | ["hub", "Running", "1.5 cores", "2.0 GiB", "26 hours ago"], | |
| 384 | ["survival", "Stopped", "—", "—", "—"], | |
| 385 | 385 | ]); |
| 386 | 386 | const text = cloudLines(view(), at(MACHINE), 100).join("\n"); |
| 387 | expect(text).toMatch(/hub +running +1\.5 cores +2\.0 GiB +26 hours ago/); | |
| 388 | expect(text).toMatch(/survival +exited +— +— +—/); | |
| 387 | expect(text).toMatch(/hub +Running +1\.5 cores +2\.0 GiB +26 hours ago/); | |
| 388 | expect(text).toMatch(/survival +Stopped +— +— +—/); | |
| 389 | 389 | }); |
| 390 | 390 | |
| 391 | 391 | test("a container's Logs tab shows its lines as text, live for its box's owner, says so when none, and is an admin's alone (#17953)", async () => { |
test/five-states.test.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | /** The five states (#18617): every raw state the engine can report, and the one it maps to. */ | |
| 2 | import { expect, test } from "bun:test"; | |
| 3 | import { CONTAINER_STATES, FIVE, HEALTH_STATES, MACHINE_STATES, WORDS, containerFive, fiveLine, machineFive, projectFive } from "../src/machines/five_states.ts"; | |
| 4 | ||
| 5 | test("every Docker state maps to one of the five, listed whole", () => { | |
| 6 | expect(CONTAINER_STATES).toEqual({ running: "running", created: "starting", restarting: "attention", paused: "stopped", exited: "stopped", removing: "stopped", dead: "attention" }); | |
| 7 | for (const [raw, five] of Object.entries(CONTAINER_STATES)) { | |
| 8 | // A running container's health decides; every other state is its own word. | |
| 9 | const t = containerFive({ state: raw, health: "" }); | |
| 10 | expect(t.state).toBe(five); | |
| 11 | expect(t.word).toBe(WORDS[five]); | |
| 12 | } | |
| 13 | }); | |
| 14 | ||
| 15 | test("a running container's health check: healthy and none run, starting starts, unhealthy needs a restart", () => { | |
| 16 | expect(HEALTH_STATES).toEqual({ "": "running", none: "running", healthy: "running", starting: "starting", unhealthy: "attention" }); | |
| 17 | for (const [h, five] of Object.entries(HEALTH_STATES)) expect(containerFive({ state: "running", health: h }).state).toBe(five); | |
| 18 | const sick = containerFive({ state: "running", health: "unhealthy" }); | |
| 19 | expect(sick.reason).toBe("its health check is failing"); | |
| 20 | expect(sick.fix).toEqual({ act: "restart", label: "Restart" }); | |
| 21 | expect(containerFive({ state: "dead", health: "" }).fix).toEqual({ act: "start", label: "Start" }); | |
| 22 | }); | |
| 23 | ||
| 24 | test("a sleeping container is stopped, a silent box is Can't tell, an unknown raw state is Can't tell and says so", () => { | |
| 25 | expect(containerFive({ state: "exited", health: "", asleep: true })).toMatchObject({ state: "stopped", reason: "asleep, it wakes when someone joins" }); | |
| 26 | expect(containerFive({ state: "running", health: "healthy", silent: true }).state).toBe("unknown"); | |
| 27 | expect(containerFive({ state: "", health: "" }).state).toBe("unknown"); | |
| 28 | const odd = containerFive({ state: "teleporting", health: "" }); | |
| 29 | expect(odd.state).toBe("unknown"); | |
| 30 | expect(odd.reason).toContain("does not know"); | |
| 31 | expect(containerFive({ state: "running", health: "weird" }).state).toBe("unknown"); | |
| 32 | // Names on the object prototype are not states. | |
| 33 | expect(containerFive({ state: "constructor", health: "" }).state).toBe("unknown"); | |
| 34 | expect(machineFive({ state: "toString" }).state).toBe("unknown"); | |
| 35 | }); | |
| 36 | ||
| 37 | test("every machine row state maps to one of the five; a failed row with nothing at its provider offers to clear it", () => { | |
| 38 | expect(MACHINE_STATES).toEqual({ new: "starting", creating: "starting", running: "running", failed: "attention", retired: "stopped" }); | |
| 39 | for (const [raw, five] of Object.entries(MACHINE_STATES)) expect(machineFive({ state: raw }).state).toBe(five); | |
| 40 | expect(machineFive({ state: "failed", clear: "nothing exists at Hetzner; clear this row" })).toMatchObject({ state: "attention", fix: { act: "clear", label: "Clear this row" } }); | |
| 41 | expect(machineFive({ state: "failed", clear: "" }).fix).toBeUndefined(); | |
| 42 | expect(machineFive({ state: "" }).state).toBe("unknown"); | |
| 43 | }); | |
| 44 | ||
| 45 | test("a project is the worst of its containers; none known is Can't tell", () => { | |
| 46 | const r = containerFive({ state: "running", health: "" }); | |
| 47 | const s = containerFive({ state: "exited", health: "" }); | |
| 48 | const a = containerFive({ state: "running", health: "unhealthy" }); | |
| 49 | const st = containerFive({ state: "created", health: "" }); | |
| 50 | expect(projectFive([]).state).toBe("unknown"); | |
| 51 | expect(projectFive([r, r]).state).toBe("running"); | |
| 52 | expect(projectFive([r, s]).state).toBe("running"); | |
| 53 | expect(projectFive([s, s]).state).toBe("stopped"); | |
| 54 | expect(projectFive([r, st]).state).toBe("starting"); | |
| 55 | expect(projectFive([r, a, st])).toMatchObject({ state: "attention", fix: { act: "restart" } }); | |
| 56 | }); | |
| 57 | ||
| 58 | test("colour is never alone: every state has a word, and a line always starts with it", () => { | |
| 59 | for (const f of FIVE) expect(WORDS[f]).not.toBe(""); | |
| 60 | expect(Object.values(WORDS)).toEqual(["Running", "Starting", "Needs attention", "Stopped", "Can't tell"]); | |
| 61 | expect(fiveLine(containerFive({ state: "running", health: "unhealthy" }))).toBe("Needs attention: its health check is failing"); | |
| 62 | expect(fiveLine(containerFive({ state: "running", health: "" }))).toBe("Running"); | |
| 63 | }); | |
| 64 | ||
| 65 | test("a crash loop needs attention and offers Restart (#18617)", () => { | |
| 66 | const t = containerFive({ state: "restarting", health: "" }); | |
| 67 | expect(t.state).toBe("attention"); | |
| 68 | expect(t.fix?.act).toBe("restart"); | |
| 69 | expect(projectFive([t]).fix?.act).toBe("restart"); | |
| 70 | }); | |
| 71 | ||
| 72 | test("a fix is a ghost button, so many failing rows draw no accent (#18617)", async () => { | |
| 73 | const { fiveBlock } = await import("../src/web/five_badge.ts"); | |
| 74 | const out = String(fiveBlock(containerFive({ state: "running", health: "unhealthy" }), () => ({ action: "/x", confirm: false }), "web")); | |
| 75 | expect(out).toContain("Restart"); | |
| 76 | expect(out).toContain(`data-variant="ghost"`); | |
| 77 | }); |
test/infra-page.test.ts +6 −1
| @@ -234,7 +234,7 @@ describe("infrastructure", () => { | ||
| 234 | 234 | const running = await (await get("/infra/live?part=running")).text(); |
| 235 | 235 | expect(running).toContain("Is nyc3 fast enough?"); |
| 236 | 236 | expect(running).toContain("shop-test"); |
| 237 | expect(running).toContain("<span data-where>New York · 1 vCPU, 2 GB</span><span data-ends>Ends"); | |
| 237 | expect(running).toContain("<span data-where>New York · 1 vCPU, 2 GB</span><span class=\"row-fields\" data-five-block=\"running\">"); | |
| 238 | 238 | }); |
| 239 | 239 | |
| 240 | 240 | test("the read every door shows: machines by purpose, meters, and what the watch found that the forge did not make", async () => { |
| @@ -485,6 +485,11 @@ describe("infrastructure", () => { | ||
| 485 | 485 | const running = await (await get("/infra/live?part=running")).text(); |
| 486 | 486 | expect(running).toContain("nothing exists at DigitalOcean; clear this row"); |
| 487 | 487 | expect(running).toContain('value="/infra/canary/clear"'); |
| 488 | // Colour is never alone: the word is on the badge, the reason beside it, and one button fixes | |
| 489 | // it. | |
| 490 | expect(running).toContain('data-five-block="attention"'); | |
| 491 | expect(running).toContain("Needs attention"); | |
| 492 | expect(running.match(/aria-label="Clear this row canary"/g)?.length).toBe(1); | |
| 488 | 493 | // A live machine is refused, a dead one planned; apply is the held action, never automatic. |
| 489 | 494 | const call = (args: Record<string, unknown>) => tool.run(mcpCtx(), admin, args) as Promise<any>; |
| 490 | 495 | expect((await call({ action: "plan", names: ["shop-test"] })).value.refusals[0].why).toContain("is not a failed row with nothing at its provider"); |
new above
offline
Doubled blank line before the heading; also the bullet says terminal
fdoes Restart/Start, which is only true on the cloud tree, say which screen.