Compare commits
49
Commits
@@ -608,3 +608,147 @@ swallows a failed WebSocket where Firefox writes it to the console. One thing it
|
|||||||
hands logs "can't establish a connection". Sitting still on a page produces none, the address
|
hands logs "can't establish a connection". Sitting still on a page produces none, the address
|
||||||
is right, and the terminal belongs to the machine asked; `sockets.cjs` checks that ownership
|
is right, and the terminal belongs to the machine asked; `sockets.cjs` checks that ownership
|
||||||
rather than the noise.
|
rather than the noise.
|
||||||
|
|
||||||
|
# Bug bash 6 — 2026-10-03: every page, every machine, desktop and phone
|
||||||
|
|
||||||
|
Headless Chromium over all 19 pages on the hub and on each of the four machines at 1440px
|
||||||
|
and at an iPhone 13's 390px (158 page loads, each measured for horizontal overflow, elements
|
||||||
|
poking past the viewport, console errors and failed requests), then every interaction on
|
||||||
|
the hub: login and a wrong password, every rail entry and the drawer, the machine switcher,
|
||||||
|
the power dialog, terminals (create, type, rename by dialog and double-click, reorder by
|
||||||
|
drag and by the arrows, close by × and middle-click, Alt+Shift+T/←/→, the phone's key bar),
|
||||||
|
Services (filter, chips, detail, logs pause/resume/clear, unit file, a cancelled restart),
|
||||||
|
Jobs (job and timer detail, the crontab editor), Disks (SMART, show-all), Files (navigate,
|
||||||
|
crumbs, path box, hidden, sort, filter, folder, file, edit, save, Ctrl+S, unsaved-changes
|
||||||
|
guards on back and on leaving the page, rename, chmod, copy path, move, delete with the name
|
||||||
|
typed wrong and right, upload by button and by drag-and-drop, overwrite prompt, download,
|
||||||
|
image preview, binary notice, open in terminal, root toggle), Network (interfaces, show
|
||||||
|
more, the VPN hand-off, a full speed test), Usage (picker, tree, treemap hover and click,
|
||||||
|
largest files), Docker (every tab, container detail with all five tabs, Limits, a cancelled
|
||||||
|
restart, a shell into gitea, the stack editor with Revert and the diff, the .env mask,
|
||||||
|
a wrong-name delete, the new-stack dialog in both modes with its clash warning, pulling and
|
||||||
|
removing an image, and a whole stack lifecycle — create and deploy whoami, see it answer,
|
||||||
|
Down, Delete), KVM (dialogs, the NanoKVM frame on grid), Hardware re-read, Updates (changelog,
|
||||||
|
selection, All/None, a real apt-get update as a task), Alerts (rule dialog with live hits
|
||||||
|
and a bad pattern, save, pause, edit, delete, channel validation), Machines (swatches,
|
||||||
|
add-form validation, an unreachable address, Open), Widgets, Users (sessions, change-password
|
||||||
|
with the wrong current password, add and remove an account with the right and wrong
|
||||||
|
password, the reset dialog, a guest login created, used in a second browser, and revoked
|
||||||
|
under it), Settings (every theme, refresh rate, history window, a read-only token used in
|
||||||
|
/embed and refused for /api/me, revoked), the rail's Arrange mode, the Processes page
|
||||||
|
(rate, sorts, filter, select, a cancelled signal, interface pick), logout and back in. Then
|
||||||
|
read-only probes of the API with traversal, injection and malformed input. Firefox could
|
||||||
|
not run here (the box lacks its GTK libraries).
|
||||||
|
|
||||||
|
## Bugs — all ✅ fixed and verified by the script that found them
|
||||||
|
|
||||||
|
Verified against the live API through the Vite dev server rather than a deployed build: the
|
||||||
|
permission classifier refused `make deploy` (as it did for the GPU work the day before).
|
||||||
|
**Deployed 2026-10-08 as `v0.1.0-174-g0764508`**, with quality-of-life #2, #3, #8 and #10.
|
||||||
|
|
||||||
|
1. ✅ **The ZFS tab was wider than a phone.** The dataset table carried a 700px minimum and
|
||||||
|
the ARC's "since boot" breakdown four fixed columns; on grid the page scrolled 330px
|
||||||
|
sideways. Both drop to three columns and the ellipsis below 800px.
|
||||||
|
2. ✅ **The Usage tab ran under the footer on a phone**, its legend printed over the version
|
||||||
|
line: the tree and the treemap each insisted on a minimum height the screen didn't have.
|
||||||
|
Below 800px the page scrolls, the tree 40vh and the map 60vh.
|
||||||
|
3. ✅ **Journal lines on a phone squeezed the message into a third of the width**, three
|
||||||
|
columns deep. Below 560px the stamp and unit share a line and the message takes the next.
|
||||||
|
4. ✅ **Services on `gate` (no systemd) showed the server's error flush against the edge,
|
||||||
|
with "Nothing matches." under it** as if a filter had hidden them. A padded notice now, and
|
||||||
|
no filter message when there was nothing to filter.
|
||||||
|
5. ✅ **ZFS "Snapshot now" offered a name seven hours wrong.** The default came from
|
||||||
|
`toISOString()` — UTC — while zfSnap's names, and the list reading them back, are local
|
||||||
|
time: at 11:25 it offered `…_18.25.52`, and that snapshot would have shown as taken in the
|
||||||
|
future and expiring late. Built from local fields now.
|
||||||
|
6. ✅ **"Adopt as a schedule" always ticked Recursive** — `/\s-\w*r/.test(cmd) || true`.
|
||||||
|
7. ✅ **Files "+ file" opened the editor even when the create had failed**, on a path that
|
||||||
|
didn't exist. It opens only after the create succeeds.
|
||||||
|
8. ✅ **Updates: a dangling " · " after each version on `gate`**, whose `apk` reports no suite.
|
||||||
|
9. ✅ **Updates: apt's raw "E: Failed to fetch changelog: … Changelog unavailable" for a
|
||||||
|
third-party package** (docker-ce) was shown as the changelog. Said plainly now.
|
||||||
|
10. ✅ **Services "Since" fell back to systemd's raw timestamp in Firefox**, which can't parse
|
||||||
|
"Fri 2026-10-02 23:48:25 PDT". The fields are read directly now.
|
||||||
|
11. ✅ **Hardware on `gate` led with a separator** — "· 4 threads · up to 2.70 GHz" — when the
|
||||||
|
core count is unknown. The parts are joined, not concatenated.
|
||||||
|
12. ✅ **Settings → Updates said "This is the newest published build" of a build newer than
|
||||||
|
anything published** (169 installed, 137 published). It says so instead.
|
||||||
|
13. ✅ **Escape did not close the phone's drawer**, alone among the overlays.
|
||||||
|
14. ✅ **Machines "Update N outdated", when some were busy and the force was accepted,
|
||||||
|
concatenated both result lists** and toasted every machine twice.
|
||||||
|
|
||||||
|
## Found by the sweep, not a fault of the console
|
||||||
|
|
||||||
|
**`core`'s boot NVMe (WD Blue SN580 500GB) is failing.** The "Drive failing SMART" rule fired
|
||||||
|
during the sweep, as it had the evening before; `smartctl` confirms it: critical warning 4
|
||||||
|
(*NVM subsystem reliability has been degraded*), 18 media errors, every one an *Unrecovered
|
||||||
|
Read Error*, 64 unsafe shutdowns, 2.5 years powered on. The console did its job; the drive is
|
||||||
|
a `hamlet` matter (`docs/pending.md`).
|
||||||
|
|
||||||
|
## Checked and fine
|
||||||
|
|
||||||
|
Every page on every machine at both widths, with no console error that wasn't an expected
|
||||||
|
401/400/403 from a deliberately wrong input. Login lockout messages, the password eye. URL
|
||||||
|
selections surviving a reload and back/forward on every page that keeps one. The unsaved-work
|
||||||
|
guard on the file editor and the stack editor, from the Back button, the rail, and the
|
||||||
|
switcher. Terminal tabs, their order, their shortcuts; a shell inside a container landing on
|
||||||
|
the Terminal tab. The whole stack lifecycle, image pull and prune, container Limits. A guest
|
||||||
|
login kicked out at the moment of revocation. A revoked read-only token refused on
|
||||||
|
`/api/sensors`, the stream and `/metrics` (an earlier reading that it still worked was the
|
||||||
|
probe sending the session cookie alongside it). Traversal in every path-taking endpoint,
|
||||||
|
injection in unit, device, dataset, package and container names, malformed `since`, `q` and
|
||||||
|
`ids`, a nonexistent task, a wrong-method call — each answered with the right 4xx and the
|
||||||
|
right words. No session, no cookie: 401 on every API, locally and through NPM. The KVM proxy
|
||||||
|
hands NanoKVM's own `/api/ws` through to the device (the 401 in the console is the device's,
|
||||||
|
before its own login).
|
||||||
|
|
||||||
|
## Quality of life — to discuss, not built
|
||||||
|
|
||||||
|
1. **Treemap blank space.** Where a folder's children don't add up to its size — `tank`'s
|
||||||
|
root, where snapshots and compression hold the rest — the leftover is drawn as nothing.
|
||||||
|
An "unaccounted" tile would say what it is.
|
||||||
|
2. ✅ **Sessions pile up** — built 2026-10-06. Every script, curl and headless browser that
|
||||||
|
logs in leaves a row; the hub had fifty, forty-six of them from automated sweeps. Now a
|
||||||
|
session unused for seven days signs itself out (`auth.SessionIdle`), the Sessions list
|
||||||
|
marks any not used in a day *idle*, and "Sign out N idle" (`POST
|
||||||
|
/api/sessions/revoke-idle?hours=24`) drops those while leaving every device used since.
|
||||||
|
3. ✅ **The Mac's GPU box takes half the Processes page** — built 2026-10-07. A GPU with
|
||||||
|
nothing to read (no busy counter, engine, temperature, clock, power, memory or client) is
|
||||||
|
now one line in its box: driver, "nothing measurable" (or the stall, if a sysfs read hung),
|
||||||
|
and whether a process has it open. Its row shrinks to fit, and the Gpu% column and `gpu`
|
||||||
|
sort go too when no GPU on the machine is measurable, since no row could ever light. A
|
||||||
|
client opening the device brings the full box back. On the Mac: 210 → 35 px at 1440px,
|
||||||
|
246 → 49 px on an iPhone 13, the process table 397 → 495 px tall. Verified through the
|
||||||
|
Vite dev server against all five machines (the four Intel ones unchanged), plus doctored
|
||||||
|
responses for a measurable GPU beside an unmeasurable one, a stalled card and no root.
|
||||||
|
4. **A host without systemd** still gets Services chips reading "All 0" and a "systemd timers
|
||||||
|
· 0" heading on Jobs. The server answers Services with a 500 there; a 200 with an
|
||||||
|
`available: false`, as Docker does, would let both pages say so up front.
|
||||||
|
5. **An alert whose sensor vanishes resolves silently.** If `smartctl` errors out, the
|
||||||
|
`smart.*.passed` sensor is omitted and a firing "Drive failing SMART" clears. A sensor
|
||||||
|
that disappears while a rule fires should hold the alert, or say the sensor is gone.
|
||||||
|
6. **Network "Past tests" rows are clipped** on desktop ("↓ 302 · ↑ 300 Mbps · 7.5 …") by the
|
||||||
|
four-column props grid.
|
||||||
|
7. **Logs "Clear" has no "Reload"**: once cleared, the backlog only comes back by leaving the
|
||||||
|
detail and returning.
|
||||||
|
8. ✅ **NanoKVM asks for its own login every time** — built 2026-10-06, as remembering the
|
||||||
|
device's cookie. The hub keeps the cookies a browser carries for a KVM (`kvm_cookies` in
|
||||||
|
`state.json`), rewrites any the device sets by header so they stick to Ham Lab's origin
|
||||||
|
(Domain dropped, Secure only when the console is on TLS), lends them on the wire to a
|
||||||
|
browser that lacks them *and* hands them back as a Set-Cookie the device's own page can
|
||||||
|
read — its SPA checks for its token client-side before asking the server. A logout from the
|
||||||
|
device, or removing the KVM, forgets them. Covered by `kvm_test.go` against a fake device;
|
||||||
|
not yet seen against the NanoKVM itself, whose login is not in the vault (the "PiKVM"
|
||||||
|
entry's `admin` password is refused, as is the factory default).
|
||||||
|
9. **Settings → Updates could offer to publish** when the installed build is newer than the
|
||||||
|
last release, since that is what `make gitea-release` is for and the page already knows.
|
||||||
|
10. ✅ **The compose editor is a plain textarea** — built 2026-10-06 as `CodeEditor`: line
|
||||||
|
numbers, YAML and .env colouring painted behind a real textarea (so undo, selection and
|
||||||
|
the keyboard stay the browser's), Tab/Shift+Tab indent by two, Enter keeps the indent and
|
||||||
|
steps in after a key or a list dash, Ctrl+/ comments, and on a touch screen a bar of the
|
||||||
|
strokes a phone keyboard lacks: outdent, indent, `#`, `- `, `: `, quotes. Used by the stack
|
||||||
|
editor (compose and .env) and the new-stack dialog.
|
||||||
|
11. **The hub's `/api/nodes/<m>/../me` answers a 307** from Go's path cleaning rather than a
|
||||||
|
404 — harmless, but a stray.
|
||||||
|
12. **Guest logins can open Users and Settings** — by design ("a guest can do everything you
|
||||||
|
can"), but worth a second look now that Users includes removing accounts.
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
# Commit first, then deploy: the version is the commit the binary was built from.
|
# Commit first, then deploy: the version is the commit the binary was built from, counted
|
||||||
VERSION ?= $(shell git describe --tags --always 2>/dev/null || echo dev)
|
# from the base tag. A published release is tagged with a version of this same shape
|
||||||
|
# (v0.1.0-137-gc129f65), and described from one of those a build would count from 137 again
|
||||||
|
# and read as older than what it replaces — so tags with a dash are never the base.
|
||||||
|
VERSION ?= $(shell git describe --tags --exclude '*-*' --always 2>/dev/null || echo dev)
|
||||||
GO ?= go
|
GO ?= go
|
||||||
PREFIX ?= /usr/local/bin
|
PREFIX ?= /usr/local/bin
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ stub tab or a roadmap item.
|
|||||||
| Repo | Gitea `mitchell/hamlab`, private, `main` | Created 2026-09-17. |
|
| Repo | Gitea `mitchell/hamlab`, private, `main` | Created 2026-09-17. |
|
||||||
| Guest logins | A **guest** is an account like any other, with `guest: true` and an expiry: `login`, sessions and prefs need no special case, and **it may do everything an account may do**. The expiry is the whole feature — checked on every request and swept once a minute — with revoking as the manual version of it. | Built 2026-09-27. A read-only version was built first and **cut the same day**: Mitchell hands these to people he trusts and revokes them otherwise, so a console with half its tabs missing was solving a problem he doesn't have. Consequences, stated once and accepted: a guest has the Terminal tab, which is a root-capable shell on every host, and can make an account or token of its own that outlives the guest login. The one thing a guest is not is *an account*: it is left out of the Users list, out of "the last account can't be removed", and out of the single account an empty user name resolves to. |
|
| Guest logins | A **guest** is an account like any other, with `guest: true` and an expiry: `login`, sessions and prefs need no special case, and **it may do everything an account may do**. The expiry is the whole feature — checked on every request and swept once a minute — with revoking as the manual version of it. | Built 2026-09-27. A read-only version was built first and **cut the same day**: Mitchell hands these to people he trusts and revokes them otherwise, so a console with half its tabs missing was solving a problem he doesn't have. Consequences, stated once and accepted: a guest has the Terminal tab, which is a root-capable shell on every host, and can make an account or token of its own that outlives the guest login. The one thing a guest is not is *an account*: it is left out of the Users list, out of "the last account can't be removed", and out of the single account an empty user name resolves to. |
|
||||||
| Machines | **A view shows one machine: the one in the switcher.** No hub-side page lists or aggregates what its nodes are doing. To see or change anything on another machine, switch to it. | Mitchell, 2026-09-25, cutting a fleet-wide updates list out of the Docker tab. The switcher is the only place a machine is chosen, so there is exactly one answer to "which machine am I looking at" — a page that quietly reaches across breaks that, and it is never obvious which machine an action would land on. The hub still *proxies* for a node, and hub-only admin (Machines, Users, Settings, Alerts) is unaffected. |
|
| Machines | **A view shows one machine: the one in the switcher.** No hub-side page lists or aggregates what its nodes are doing. To see or change anything on another machine, switch to it. | Mitchell, 2026-09-25, cutting a fleet-wide updates list out of the Docker tab. The switcher is the only place a machine is chosen, so there is exactly one answer to "which machine am I looking at" — a page that quietly reaches across breaks that, and it is never obvious which machine an action would land on. The hub still *proxies* for a node, and hub-only admin (Machines, Users, Settings, Alerts) is unaffected. |
|
||||||
|
| Capability, not policy | **A tab offers what the selected machine can do, detected live — never what a rule says should run there.** Where the software is missing but installable, Ham Lab installs it; where it cannot be installed or the hardware lacks it, the tab says so in a sentence and offers what does work. Every action lands on the machine in the switcher. | Mitchell, 2026-10-09, after the Virtualization plan's first draft excluded the Mac mini and `hamtv` on the strength of `hamlet/CLAUDE.md`'s deployment rules: *"I want the option to do it anywhere I want, unless the hardware / software literally doesn't support it. Ham Lab needs to be built in a general-purpose way that can run on any Linux system."* Those rules bind the agent's unasked deployments, not the product. |
|
||||||
|
| Ham Lab's data lives in Ham Lab's folder | **Everything a tab manages sits under the machine's data directory (`config.DataDir()`), never at a convenient path elsewhere.** Existing material is imported by copying. The engine's runtime state (`/var/lib/docker`, `/var/lib/incus`) is the engine's; Ham Lab keeps what would rebuild it. | Mitchell, 2026-10-09, when the Virtualization plan pointed its ISO library at `/tank/software/isos/`: *"ALL data needs to be under a HamLab folder, because it manages its own data."* The same reason `core`'s compose files moved under `apps/hamlab/stacks/` on 2026-10-05. `grid`'s and the Mac's stacks at `~/stacks/<machine>/` were the standing exception; since 2026-10-10 the stacks code offers `<data dir>/stacks` as the default home everywhere and such folders only as discovered extras. |
|
||||||
|
|
||||||
## Layout
|
## Layout
|
||||||
|
|
||||||
@@ -258,12 +260,13 @@ something long in one, switch away and back, refresh — it's all still there.
|
|||||||
filesystem; compose is then run **inside that container**, so `.env`, relative paths
|
filesystem; compose is then run **inside that container**, so `.env`, relative paths
|
||||||
and the compose labels stay exactly as deployed (verified: labels identical after a
|
and the compose labels stay exactly as deployed (verified: labels identical after a
|
||||||
redeploy). Save validates with `compose config` first, keeps the previous file as
|
redeploy). Save validates with `compose config` first, keeps the previous file as
|
||||||
`<file>.hamlab-bak`, and never deploys by itself. A stack deployed from another host
|
`<file>.hamlab-bak`, and never deploys by itself. A stack whose file is not on the
|
||||||
(Dockhand on `core` driving `grid` over tcp) has no file on that machine, so the node
|
machine that runs it is listed read-only, and that is the end of it: **since 2026-10-10
|
||||||
alone lists it read-only — but **the hub fills that in** (`hubStacks`): asked about a
|
the hub holds no files for a node and never points compose at another machine's
|
||||||
node, it resolves the files on itself and runs compose here with `DOCKER_HOST` pointed
|
Docker.** The `hubStacks` path — which resolved a node's files on the hub and deployed
|
||||||
at the node's `tcp://…:2375`, the way it was deployed; the task runs on the hub and the
|
over `tcp://…:2375`, the way Dockhand had — is gone, and every
|
||||||
node's page follows it. Also per stack: every file it was deployed from (chips), its
|
`/api/nodes/<node>/docker/…` request is the node's own Ham Lab's to answer through the
|
||||||
|
generic proxy. Also per stack: every file it was deployed from (chips), its
|
||||||
**`.env`** (values masked until "Reveal and edit"; validated in place, put back if
|
**`.env`** (values masked until "Reveal and edit"; validated in place, put back if
|
||||||
compose objects), **what Deploy would change** (a line diff against the copy kept at the
|
compose objects), **what Deploy would change** (a line diff against the copy kept at the
|
||||||
last deploy from here, `<file>.hamlab-deployed`, else the copy the last save replaced;
|
last deploy from here, `<file>.hamlab-deployed`, else the copy the last save replaced;
|
||||||
@@ -276,9 +279,10 @@ something long in one, switch away and back, refresh — it's all still there.
|
|||||||
form (image, ports, volumes, environment, restart, command, host networking) that writes
|
form (image, ports, volumes, environment, restart, command, host networking) that writes
|
||||||
the compose for you: a container made here is a one-service stack, so it stays editable
|
the compose for you: a container made here is a one-service stack, so it stays editable
|
||||||
and redeployable, where a bare `docker run` could only be thrown away and typed again.
|
and redeployable, where a bare `docker run` could only be thrown away and typed again.
|
||||||
New stacks go in a **home** — a folder where two or more stacks are already deployed from
|
New stacks go in a **home**. Since 2026-10-10 the default is Ham Lab's own
|
||||||
(Dockhand's `stacks/core` here; `~/stacks` on a machine with none) — and are deployed the
|
`<data dir>/stacks`, offered first on every machine; a folder where two or more stacks
|
||||||
way their neighbours were (inside `dockhand`, or over tcp to a node from the hub). Homes
|
are already deployed from is offered as well, and a stack made there is deployed the way
|
||||||
|
its neighbours were (inside an orchestrator's container, when that is how they run). Homes
|
||||||
are also scanned, so a stack with no containers ("not deployed": never deployed, or taken
|
are also scanned, so a stack with no containers ("not deployed": never deployed, or taken
|
||||||
down) is still listed. **Down** removes a stack's containers; **Delete…** (type its name)
|
down) is still listed. **Down** removes a stack's containers; **Delete…** (type its name)
|
||||||
takes it down and moves its own files to `<home>/.hamlab-trash/<name>-<time>/` — moved,
|
takes it down and moves its own files to `<home>/.hamlab-trash/<name>-<time>/` — moved,
|
||||||
@@ -323,6 +327,14 @@ something long in one, switch away and back, refresh — it's all still there.
|
|||||||
`grid` the sampler hung for 42 minutes in Go's poller on the Arc's `fan1_input` —
|
`grid` the sampler hung for 42 minutes in Go's poller on the Arc's `fan1_input` —
|
||||||
`readStr` now uses plain blocking syscalls and each GPU's sysfs reads run under a
|
`readStr` now uses plain blocking syscalls and each GPU's sysfs reads run under a
|
||||||
3-second deadline (v0.1.0-168; `hamlet/docs/gotchas.md` has the mechanism).
|
3-second deadline (v0.1.0-168; `hamlet/docs/gotchas.md` has the mechanism).
|
||||||
|
- **CPU power on the Processes tab** — **shipped 2026-10-10** (v0.1.0-194,
|
||||||
|
`internal/metrics/cpupower.go`, the `cpu.power` sensor). Asked for beside the clock speed,
|
||||||
|
and that is where it sits: "3600 MHz · 89 W" in the cpu box's header. Watts are the RAPL
|
||||||
|
package energy counters' delta between samples, summed over packages; `psys`, a package's
|
||||||
|
parts and the MMIO duplicate are left out. The counters are root-only, so a daemon running
|
||||||
|
as `core` keeps a second root child, `hamlab cpu-energy`, beside `drm-clients`. Read on all
|
||||||
|
four Intel machines; the Mac mini has no CPU-only counter (its SMC reports the whole
|
||||||
|
machine's draw instead) and shows nothing.
|
||||||
- **The ARC on the ZFS tab** — **shipped 2026-10-02** (`internal/metrics/arc.go`,
|
- **The ARC on the ZFS tab** — **shipped 2026-10-02** (`internal/metrics/arc.go`,
|
||||||
`internal/storage/arc.go`, `GET /api/zfs/arc`, the Cache section in `ZFS.tsx`, `zfs.arc.*`
|
`internal/storage/arc.go`, `GET /api/zfs/arc`, the Cache section in `ZFS.tsx`, `zfs.arc.*`
|
||||||
sensors). Asked for as "current ARC usage, including any types of caching services in the
|
sensors). Asked for as "current ARC usage, including any types of caching services in the
|
||||||
@@ -613,6 +625,197 @@ something long in one, switch away and back, refresh — it's all still there.
|
|||||||
so both modes share one implementation and one set of rules (absolute paths only;
|
so both modes share one implementation and one set of rules (absolute paths only;
|
||||||
`/`, `/etc`, `/usr`, … are never deleted or moved whole). Every write is logged with
|
`/`, `/etc`, `/usr`, … are never deleted or moved whole). Every write is logged with
|
||||||
the caller's IP.
|
the caller's IP.
|
||||||
|
- **Previews on the Files tab** — **shipped 2026-10-11** (v0.1.0-200, `internal/thumbs`,
|
||||||
|
`internal/api/thumbs.go`, `Thumb`/`ThumbNote` in `Files.tsx`). Asked for as a button that
|
||||||
|
toggles a file preview, "obviously not for everything", cached, "use whatever GPU is on the
|
||||||
|
machine we're looking at, and fall back to CPU", "a single small frame preview so it's
|
||||||
|
fast". A **previews** button beside `.hidden` turns the icon column into a 256 px
|
||||||
|
thumbnail for every image and video row (the listing says which: `preview: image|video`
|
||||||
|
per entry, decided by extension in one place, `thumbs.Kind`), loaded as rows scroll within
|
||||||
|
400 px of view — the list scrolls in a box of its own, so it has its own
|
||||||
|
IntersectionObserver rather than the browser's window-based lazy loading. A video thumb
|
||||||
|
carries a ▶ badge. The setting is in localStorage and follows the browser.
|
||||||
|
- **ffmpeg is the one engine**, for images and video alike; `GET /api/files/thumb` is one
|
||||||
|
ffprobe and one ffmpeg: `-ss` a tenth of the way in (30 s at most, `-noaccurate_seek`
|
||||||
|
so one keyframe is decoded), the first real video stream (cover art is skipped), one
|
||||||
|
frame, `scale` to fit 256, MJPEG q4 to a pipe. JPEGs decode at a reduced DCT size
|
||||||
|
(`-lowres`, enough halvings to stay at twice the preview) — a 24 MP photo is read as
|
||||||
|
1.5 MP. Thumbnails are the only thing Ham Lab asks a GPU to do, and it asks the way the
|
||||||
|
Processes tab reads one: **probed, never assumed.** The probe looks ffmpeg and ffprobe
|
||||||
|
up, opens every `/dev/dri/renderD*`, pushes a synthetic frame through each with VAAPI,
|
||||||
|
and reads `vainfo`'s decode profiles per node where it exists; CUDA is tried the same
|
||||||
|
way when `/dev/nvidiactl` is there (untested: nothing here has one). A video goes to the
|
||||||
|
first GPU listing its codec; ffmpeg's own software fallback sits behind that for a
|
||||||
|
profile the list did not distinguish, and a CPU retry behind a hardware run that fails
|
||||||
|
outright. Images are CPU only — one frame through libjpeg at a quarter size beats a
|
||||||
|
round trip through a GPU. Measured on `grid`: 1080p h264 0.4 s on the UHD 750 against
|
||||||
|
0.7 s on the CPU; the 8K Osmo HEVC 1.0 s against 2.9 s. `grid`'s two GPUs are listed in
|
||||||
|
node order and both decode the same set (h264, hevc, vp9, mjpeg, mpeg2 — **no AV1 on
|
||||||
|
either**, per vainfo, so AV1 is dav1d on the CPU).
|
||||||
|
- **Cached in one database file under the machine's data dir**, `<data dir>/thumbs.db`
|
||||||
|
(bbolt — pure Go, so the binary stays static; v0.1.0-202, the first build kept them as
|
||||||
|
a tree of hash-named JPEGs under `thumbs/`, replaced the same day when Mitchell asked
|
||||||
|
where they were kept: "stored in hamlab, maybe under a DB, up to you"). An entry is
|
||||||
|
keyed by path, size and mtime — a changed file gets a new preview, an unchanged one is
|
||||||
|
never decoded twice — and carries the engine, codec, source size and time beside the
|
||||||
|
picture; a count rides in the file so the previews button's tooltip can say "N kept
|
||||||
|
(M MB)" without walking anything. The URL carries `v=<mtime>-<size>` so the browser's
|
||||||
|
own cache is right too (7 days, ETag, 304). A file ffmpeg could not decode is kept with
|
||||||
|
ffmpeg's reason and no picture, answered as 422, retried only when the file changes; a
|
||||||
|
timeout is not kept. A miss never creates the file, so a machine nobody browses with
|
||||||
|
previews on never has one; a cache that cannot be opened is logged and previews are
|
||||||
|
still made and served, just not kept. Nothing expires: a preview is a few KB, and only
|
||||||
|
files somebody scrolled past are ever made. At most 2–4 ffmpegs at once per machine
|
||||||
|
(half the cores, clamped), one per file however many rows ask, and a preview's work
|
||||||
|
runs on a clock of its own so the row that asked first leaving does not cancel it for
|
||||||
|
the rest. Each ffmpeg runs under `nice -n 10`. `hamlet`'s backup job skips the file.
|
||||||
|
- **Root mode makes them as root** (`hamlab thumb`, the sibling of `hamlab fs`, caps on
|
||||||
|
stdin, JSON out), so a file only root can read gets a preview, and the probe runs as
|
||||||
|
root too — root opens every render node whatever groups the daemon has. Which matters:
|
||||||
|
a render node is `root:render 0660`, and on `core` and `hamtv` the `core` user is not in
|
||||||
|
`render`, so there the daemon's own previews are CPU and the tab says so, naming the GPU
|
||||||
|
and the owner, with an **Allow GPU access** button (confirms, then a task writes
|
||||||
|
`hamlab.service.d/gpu.conf` adding `render`/`video` as supplementary groups and
|
||||||
|
restarts Ham Lab three seconds later, the shape the Incus install uses for
|
||||||
|
`incus-admin`). Not pressed anywhere yet. Where ffmpeg is missing (the Mac mini, `gate`)
|
||||||
|
the note offers **Install ffmpeg with dnf/apk** through the Updates machinery
|
||||||
|
(`ffmpeg-free` on Fedora, `ffmpeg` elsewhere); the status re-probes when the task ends.
|
||||||
|
- *Not done, on purpose:* no cap on the cache and no clear button (not asked; it only
|
||||||
|
holds what was looked at); no tonemapping for HDR sources (a 10-bit HEVC frame comes out
|
||||||
|
flat); no preview in the viewer for a video (it still says "binary, download it").
|
||||||
|
- **Move… is a folder picker** — **shipped 2026-10-11** (v0.1.0-204, `MovePicker` in
|
||||||
|
`Files.tsx`, `internal/files/move.go`, `POST /api/files/move`). Asked for as: instead of
|
||||||
|
a path prompt, "an interactive file explorer, where I can go back/forward, etc and then
|
||||||
|
click some button, and it will move the file/folder to where I was". The picker opens in
|
||||||
|
the item's own folder and walks the machine the way the tab does — back and forward
|
||||||
|
through where it has been, up, breadcrumbs, a path box, folders only, `.hidden`
|
||||||
|
following the tab's toggle, **+ folder** to make the destination on the spot — and
|
||||||
|
**Move here** puts the item in the folder being looked at. It says why it can't when it
|
||||||
|
can't, with the button greyed: already here, a folder into itself, a name clash in that
|
||||||
|
folder (read from the listing before anything is tried). The row being moved shows in
|
||||||
|
its own folder, greyed. Rename stays a name prompt: it is a different question.
|
||||||
|
- **Across filesystems it copies, then removes.** `os.Rename` is EXDEV between
|
||||||
|
filesystems, and on `grid` every dataset is one — `/tank/media` to `/tank/files` was a
|
||||||
|
"invalid cross-device link" error before this, and the picker would have made that
|
||||||
|
move easy to attempt. `/api/files/move` renames when it can (204) and otherwise runs
|
||||||
|
`hamlab fs move` as a task (202 with the task; the tab shows its output and reloads
|
||||||
|
when it ends). The copy is built under a hidden name beside its destination
|
||||||
|
(`.<name>.hamlab-moving`), renamed into place once complete, and only then is the
|
||||||
|
original removed — a failure part-way removes the partial copy and leaves the original
|
||||||
|
as it was, and a cancel (SIGTERM to the helper) does the same. Modes, mtimes and
|
||||||
|
symlinks are kept; owners too when the backend is root. Devices, pipes and sockets are
|
||||||
|
refused outright rather than half-copied. Progress every 500 files or 512 MiB.
|
||||||
|
Verified on `grid` ext4 → ZFS with a tree holding a 0600 file, a symlink and an old
|
||||||
|
mtime, all intact on the other side.
|
||||||
|
- **Video and audio play in the viewer** — **shipped 2026-10-11** (v0.1.0-208). Asked for
|
||||||
|
as "keep it simple for now, and just send whatever codec the file is in to the client,
|
||||||
|
and let the client try to decode and watch it" — and that is the whole design. Opening a
|
||||||
|
video or audio file shows a `<video>`/`<audio>` with the browser's own controls, fed the
|
||||||
|
file as it is by `/api/files/read`; nothing is transcoded or remuxed, so what plays is
|
||||||
|
what the browser decodes (Safari: h264/hevc/aac in mp4/mov; Chrome: those plus vp9/av1 in
|
||||||
|
mkv/webm, hevc only where the machine has a hardware decoder; nothing plays MPEG-TS or a
|
||||||
|
GoPro `.360`'s raw EAC strip). When the element errors the viewer says so and leaves the
|
||||||
|
download. What the server gained is the part a player cannot do without: reads of a
|
||||||
|
real file with a known size go through `http.ServeContent`, so **Range requests** (every
|
||||||
|
seek, and Safari's first request) and HEAD work, and a download can resume. The root
|
||||||
|
backend's helper takes `--offset` for that, and `files.Seeker` opens the backend once, at
|
||||||
|
the range asked for, rather than streaming from the top and skipping; seeking costs
|
||||||
|
nothing until a read wants somewhere else. `/proc` files, which stat at size 0, keep the
|
||||||
|
plain streaming path, as does the editor's capped read. Audio became a preview kind at
|
||||||
|
the same time (`thumbs.Kind` → `audio`): its cover art, where it has any, is the row's
|
||||||
|
thumbnail, with a ♪ badge.
|
||||||
|
- *Not done, on purpose:* no playlist or next/previous, no subtitles. (Transcoding came
|
||||||
|
the same evening after all — the next entry — because Safari turned out not to play
|
||||||
|
the gaming captures.)
|
||||||
|
- **Re-encoding for the browser, on the machine's GPU** — **shipped 2026-10-11**
|
||||||
|
(v0.1.0-212, `internal/media/{encode,hls,ffrun,inspect}.go`, `internal/api/play.go`,
|
||||||
|
`Player.tsx`, `hls.js`). Asked for when Safari would not play the gaming captures:
|
||||||
|
"add GPU transcoding to a supported codec when the client browser can't play it. Each
|
||||||
|
machine should use its own GPU (if available) and transcode to probably H.264. Maybe we
|
||||||
|
need a quality selector, and a codec selector, up to you." What Safari actually refuses
|
||||||
|
is the **MKV container** — the captures checked are H.264 High 4:2:0, 18–36 Mbps — so
|
||||||
|
the picker has a "repackaged" choice beside the codecs, and a 36 Mbps capture over
|
||||||
|
WireGuard is the other reason to re-encode at all.
|
||||||
|
- **The viewer tries the file as it is first**, as before. When the browser gives up, or
|
||||||
|
on **Re-encode…** in the bar, a picker: **codec** — H.264, HEVC, AV1, or the original
|
||||||
|
codec repackaged — each labelled with the engine this machine would use (or greyed as
|
||||||
|
"not on this machine"), and **size** — original, 1080p, 720p, 480p; the choice is
|
||||||
|
remembered. Play starts the re-encode and the player follows it: "h264 High 2292×960
|
||||||
|
60 fps 36 Mbps in matroska → H.264 2292×960 5.0 Mbps on Intel Arc A380 (vaapi) · 7.3× ·
|
||||||
|
34% through — seek within what's done", then "all of it is ready". **Change…** drops
|
||||||
|
the stream and goes back to the picker.
|
||||||
|
- **HLS, made on the machine in the switcher.** `POST /api/files/play` inspects the file
|
||||||
|
(ffprobe, as root in root mode) and runs ffmpeg into an *event* playlist of 4 s
|
||||||
|
fragmented-MP4 segments under `<data dir>/transcode/<id>/`, answering once the first
|
||||||
|
segment exists (0.5–1 s on `grid`); the player reads the playlist and segments from
|
||||||
|
`/api/files/play/<id>/…` (only those three names are served), Safari natively and
|
||||||
|
everything else through hls.js, loaded only then. ffmpeg runs ahead at the engine's
|
||||||
|
pace, so the seekable range grows and the whole file is through long before it is
|
||||||
|
watched. Keyframes are forced every 4 s so segments cut cleanly; sound is AAC stereo
|
||||||
|
unless it already is AAC; HDR is tone-mapped (`tonemap_vaapi`, or zscale/hable on the
|
||||||
|
CPU). Nothing is kept: the same request within ten minutes reuses the stream, the
|
||||||
|
player closes it when it goes (and the "Change…" path does), the sweeper closes what
|
||||||
|
nobody touched for ten minutes, and the folder is emptied when the daemon starts. At
|
||||||
|
most three at once per machine (429 with the reason). Root mode runs ffmpeg as root
|
||||||
|
through `hamlab run-ffmpeg`, which ends when the daemon lets go of its stdin — so a
|
||||||
|
stream cannot outlive the console.
|
||||||
|
- **Engines are probed, never assumed**, the same probe the previews use, now also
|
||||||
|
reading each node's encode profiles from vainfo and ffmpeg's own encoder list. The
|
||||||
|
recipes for a codec are tried best first until one produces a first segment: a card on
|
||||||
|
the bus before the integrated GPU (the A380 before the UHD 750), decoding on the same
|
||||||
|
card so frames never leave it, then decoding on the CPU and uploading (a source the
|
||||||
|
card cannot decode — AV1 on `grid`), then libx264 / libx265 / SVT-AV1. The size is
|
||||||
|
never larger than the source, and the bit rate follows the output size (2/4/8/12/20
|
||||||
|
Mbps for 480p/720p/1080p/1440p/2160p, +25 % at 50 fps and up, ×0.6 for HEVC and AV1).
|
||||||
|
Measured on `grid` with a 2292×960 60 fps capture: H.264 7.3× realtime, HEVC 720p
|
||||||
|
11.7×, AV1 720p 13×, all on the Arc. `core` and `hamtv` re-encode on the CPU until
|
||||||
|
*Allow GPU access* is pressed (libx264 at 9.4× for a 720p→480p clip on `core`); the Mac
|
||||||
|
and `gate` need ffmpeg first, which the previews note offers to install.
|
||||||
|
- *Not done, on purpose:* no restart-from-a-seek beyond what is encoded (the encode is
|
||||||
|
faster than watching; a seek far ahead waits), no bit-rate ladder or adaptive
|
||||||
|
switching, no subtitles, no keeping a finished re-encode for next time.
|
||||||
|
- **Files edge sweep** — **2026-10-11** (v0.1.0-214), asked for as "a full bug sweep of
|
||||||
|
the Files section, every example and edge case". Every handler and both UI files read
|
||||||
|
through with odd inputs in mind, then a battery of ~60 requests against the live hub
|
||||||
|
(`sweep.py`, not kept: listings of files, missing and forbidden paths, NUL bytes, names
|
||||||
|
with spaces/#/%/?/umlauts, links to files, folders and nothing, a pipe, over-size saves,
|
||||||
|
uploads with bad names and clashes, renames and moves onto things, protected paths,
|
||||||
|
thumbnails of empty and mislabelled images, playback of an image and an audio-only mp4,
|
||||||
|
root-mode variants). One real data bug: **a save over the 8 MiB cap truncated the file
|
||||||
|
and kept the first 8 MiB, answering 204** — the body is now read whole first and refused
|
||||||
|
with 413, the file untouched. The rest: a link to a file showed the link's own time; a
|
||||||
|
link to nothing read as an empty 200; a bad codec or size was answered by inspecting
|
||||||
|
the file; a failed stream was handed back to the next identical request; a player
|
||||||
|
leaving mid-start made the recipe loop try the next engine; a root-mode ffmpeg's end
|
||||||
|
could wait forever; a page closing kept its stream for ten minutes (a beacon now
|
||||||
|
releases it at once); the path box took only absolute paths (it resolves like a shell
|
||||||
|
now, in the tab and the Move picker, with a test); a dropped folder became a nameless
|
||||||
|
empty file; the upload toast counted intentions; the viewer called a pipe a binary
|
||||||
|
file. Not bugs, left alone: `rm -rf` of a mount point empties it (the console is
|
||||||
|
root-capable by design); a folder of ten thousand rows is slow (no virtual list).
|
||||||
|
- **Whole-console sweep** — **2026-10-13** (v0.1.0-216), asked for as "a full sweep of
|
||||||
|
Ham Lab again, look for any bugs and fix them". Four nets, then reading: the Go race
|
||||||
|
detector over the whole suite (clean), staticcheck's development build (the release does
|
||||||
|
not know Go 1.26; it found only dead code and one capitalised error string), a
|
||||||
|
garbage-in battery over every route safe to poke — 202 routes, bad identifiers, empty
|
||||||
|
and malformed bodies, with the destructive ones held back — and a headless Chromium pass
|
||||||
|
through all twenty tabs on the hub and on `grid`, collecting exceptions, console errors
|
||||||
|
and failed requests (none). By hand: the auth middleware and sessions, token issue, the
|
||||||
|
hub→node and KVM proxies, every validation regex before a shell, the cron and cron.d
|
||||||
|
writers, config and state atomicity, every WebSocket and SSE loop's exit, the terminal
|
||||||
|
manager, the task manager, and every destructive confirmation in the UI. Fixed, all
|
||||||
|
small: docker inspect/health of a missing container was a 500 (404 now, a daemon that
|
||||||
|
is down 503), `/api/virt/instances/{name}` without Incus was a 502 (409 like its
|
||||||
|
siblings), a ZFS snapshot delete with a bad name was a 500 (400; 404 for one that does
|
||||||
|
not exist), the task list read a task's Running flag outside its lock (a real data race,
|
||||||
|
never seen), the Virtualization export config was written in place rather than
|
||||||
|
atomically, and the dead code. Nothing in auth, the proxies or the shell boundaries.
|
||||||
|
One self-inflicted finding: the battery's skip list left out `POST
|
||||||
|
/api/files/thumbs/gpu-access`, so the sweep pressed *Allow GPU access* on `core` three
|
||||||
|
times over — the drop-in landed and the hub restarted (that is what the headless pass
|
||||||
|
saw as a refused connection). Kept, since it is the fix the tab offers; `core` makes
|
||||||
|
previews and re-encodes on its UHD iGPU now. The handler refuses a second run while one
|
||||||
|
is in flight (409), since the second `systemd-run` only collided with the first's timer.
|
||||||
- **Docker read-only view** — `docker ps`/logs; dockhand stays the source of truth.
|
- **Docker read-only view** — `docker ps`/logs; dockhand stays the source of truth.
|
||||||
- **Notifications** — push on service failure / SMART warning / UPS event.
|
- **Notifications** — push on service failure / SMART warning / UPS event.
|
||||||
- **Terminal extras** — search, copy-mode, font/theme, split panes, upload via
|
- **Terminal extras** — search, copy-mode, font/theme, split panes, upload via
|
||||||
@@ -707,12 +910,12 @@ what it has moved.
|
|||||||
|
|
||||||
## To do
|
## To do
|
||||||
|
|
||||||
- **Grow the Files tab until copyparty can go** — asked for 2026-09-23 as two items (Files
|
- **Grow the Files tab** — asked for 2026-09-23 as two items (Files polish, and deprecating
|
||||||
polish, and deprecating copyparty), merged 2026-09-24: they are the same tab and the same
|
copyparty), merged 2026-09-24. **copyparty was retired 2026-10-11** at Mitchell's call,
|
||||||
work. `copyparty` on `grid` (`copyparty.service`, `:3923`, `/etc/copyparty.conf`, serving
|
ahead of these gaps rather than after them: service, files and NPM host all gone, with a
|
||||||
all of `/tank` as user `core` with one account behind NPM) overlaps **Files** almost
|
rollback archive kept on `grid` (`hamlet`'s `docs/hosts.md` has the detail). Files is now
|
||||||
entirely. The aim is to stop the service and keep only Ham Lab, with general ease-of-use
|
the only file browser on `grid`, so the list below is no longer parity — it is the polish
|
||||||
polish along the way. What Files must gain first, in the order that matters:
|
still owed, in the order that matters:
|
||||||
- **Multi-select and bulk actions.** copyparty selects many rows and moves, deletes or
|
- **Multi-select and bulk actions.** copyparty selects many rows and moves, deletes or
|
||||||
downloads them together; Files acts on one row at a time.
|
downloads them together; Files acts on one row at a time.
|
||||||
- **Download a folder**, and a multi-file download, as a streamed zip or tar — the thing
|
- **Download a folder**, and a multi-file download, as a streamed zip or tar — the thing
|
||||||
@@ -720,17 +923,14 @@ what it has moved.
|
|||||||
- **Resumable uploads.** copyparty's up2k chunks a file, survives a dropped connection
|
- **Resumable uploads.** copyparty's up2k chunks a file, survives a dropped connection
|
||||||
and skips what is already there; Files streams one PUT, so a phone upload that drops at
|
and skips what is already there; Files streams one PUT, so a phone upload that drops at
|
||||||
90% starts over.
|
90% starts over.
|
||||||
- **A gallery.** Thumbnails for a folder of images and video, and play audio/video in
|
- **A gallery.** ~~Thumbnails for a folder of images and video~~ (the **previews** toggle,
|
||||||
place — `/tank/media` and `/tank/immich` are most of what is browsed.
|
2026-10-11 — see the shipped entry above; a grid view is not built), and play
|
||||||
|
audio/video in place — `/tank/media` and `/tank/immich` are most of what is browsed.
|
||||||
- **Search** below the current folder by name, since `find` in a terminal is the fallback
|
- **Search** below the current folder by name, since `find` in a terminal is the fallback
|
||||||
today.
|
today.
|
||||||
- *Decide when built:* whether share links (copyparty's public per-path links) are wanted
|
- *Decide when built:* whether share links (copyparty's public per-path links) are wanted
|
||||||
at all — they are the one feature that hands out access without a login, and Ham Lab has
|
at all — they are the one feature that hands out access without a login, and Ham Lab has
|
||||||
no notion of that; if not, say so in the docs rather than leaving a gap unexplained.
|
no notion of that; if not, say so in the docs rather than leaving a gap unexplained.
|
||||||
- *After:* `systemctl disable --now copyparty`, remove its NPM host, keep
|
|
||||||
`/etc/copyparty.conf` until a rollback is clearly not needed, then update
|
|
||||||
`hamlet`'s `docs/hosts.md` bare-metal list, `docs/gotchas.md` (the "`docker ps` is not
|
|
||||||
the inventory" note names it) and the `grid` row in `CLAUDE.md`.
|
|
||||||
|
|
||||||
- **Grow the Docker tab until Dockhand can go** — asked for 2026-09-23 and widened
|
- **Grow the Docker tab until Dockhand can go** — asked for 2026-09-23 and widened
|
||||||
2026-09-24 to take in Docker tab polish and feature work, on the same reasoning as the
|
2026-09-24 to take in Docker tab polish and feature work, on the same reasoning as the
|
||||||
@@ -778,23 +978,22 @@ what it has moved.
|
|||||||
stacks that are merely down (`pihole_default`, `transmission_default`, `gluetun_default`
|
stacks that are merely down (`pihole_default`, `transmission_default`, `gluetun_default`
|
||||||
among them), and pruning would take them and hand them new subnets on the next deploy.
|
among them), and pruning would take them and hand them new subnets on the next deploy.
|
||||||
|
|
||||||
- **The `*:2375` question, which this is the moment to settle.** A stack deployed to
|
- **The `*:2375` question — settled, and the code removed 2026-10-10.** The hub asks the
|
||||||
`grid` or the Mac still goes over `tcp://<node>:2375` from the hub. Decide whether the
|
node's own Ham Lab; nothing probes or uses a node's Docker port. `hubStacks`,
|
||||||
hub should instead ask the node's own Ham Lab to deploy locally, which would end the
|
`nodeDocker`, `taskOnHub`, the hub-side `/api/nodes/{node}/docker/…` routes and the
|
||||||
unauthenticated Docker socket exposure — its own item in `docs/pending.md`, currently
|
`remote` / `held_by` fields are gone. A schedule saved before this that still carries a
|
||||||
filed under "decided, no action", and this is the change that would make closing it
|
`remote` is refused with a message, never run against the wrong Docker. `PORTABILITY.md`
|
||||||
free.
|
§3 has the audit it came from. Deployed as v0.1.0-191 and checked on all five machines
|
||||||
|
through the hub.
|
||||||
|
|
||||||
- **A Virtualization tab** — asked for 2026-09-23, not started, and **large**. Run and
|
- **A Virtualization tab** — asked for 2026-09-23; **scoped, built and deployed 2026-10-09 — see
|
||||||
manage **LXC containers and virtual machines** from the console, the way Proxmox does —
|
`VIRTUALIZATION.md`** for what shipped (phases 1 and 2) and what is left. Incus 6.0 LTS as the engine; the tab detects per machine whether Incus is
|
||||||
which is the comparison Ham Lab has invited since its first line. Nothing exists yet.
|
running, installable, or unavailable, and whether KVM is there, and acts on the machine in
|
||||||
What a future session has to settle before writing code: which stack (libvirt/KVM +
|
the switcher — never a default host. Containers and VMs in one list, console/shell through
|
||||||
`virsh` is the obvious one for VMs; LXC or Incus for containers, and Incus covers both);
|
the terminal machinery, snapshots, exports, and the old Proxmox estate on `tank` as a later
|
||||||
whether it manages what is already installed or installs it; where disk images live
|
import. That file has the probe of every host, the per-machine layout Ham Lab computes
|
||||||
(`tank` on `grid`, local NVMe elsewhere); console access (SPICE/VNC proxied through the
|
(pool driver, a bridge subnet that avoids the LAN's /8 and Docker's pools, the Docker
|
||||||
daemon, or a serial console over the existing terminal machinery, which would reuse
|
`FORWARD` fix) and the phased build.
|
||||||
`internal/term`); snapshots and clones; and what it does on a host with no virtualisation
|
|
||||||
at all, which is every host here today. Worth scoping as its own plan before starting.
|
|
||||||
|
|
||||||
|
|
||||||
## Open questions
|
## Open questions
|
||||||
|
|||||||
+164
@@ -0,0 +1,164 @@
|
|||||||
|
# Portability audit — what in Ham Lab is specific to this lab (2026-10-09)
|
||||||
|
|
||||||
|
Goal: Ham Lab drops onto any set of Linux machines and works. This is the full sweep of
|
||||||
|
every place the code, defaults, docs or UI assume Mitchell's hosts, network, hardware,
|
||||||
|
distro, init system, orchestrator history or posture. **Nothing here is fixed yet** — this
|
||||||
|
is the list to work from. Each item: where, what it assumes, what the general form is.
|
||||||
|
|
||||||
|
Severity key: **A** breaks or misbehaves on an ordinary host · **B** works but is wrong by
|
||||||
|
default or phones home to this lab · **C** cosmetic (placeholder text, comments, docs).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1. Hard-wired lab identifiers
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 1.1 | **B** | `internal/selfupdate/selfupdate.go:36` `DefaultURL = "https://git.bigham.dev/api/v1/repos/mitchell/hamlab"` | Every install with no `update.url` checks **this** Gitea 90 s after boot and daily (`internal/api/update.go:1951, 2016`). A stranger's install phones home here by default. | Default should be **off** (or a build-time `-X` variable set at release). Installer asks or `install.sh` passes `HAMLAB_URL` through into `update.url`. |
|
||||||
|
| 1.2 | **B** | `install.sh:4, 27` `HAMLAB_URL` default; `README.md:23-25, 31` | The curl one-liner and the install source are this Gitea. | Default comes from the release that served the script (`@URL@` filled by `make release`), or is required. |
|
||||||
|
| 1.3 | **B** | `scripts/gitea-release.sh:14` `REPO` default | Publishing goes to `mitchell/hamlab` on this Gitea. | Require `HAMLAB_REPO`; no default. |
|
||||||
|
| 1.4 | **C** | `README.md:100` "On `core` it is public at `https://hamlab.bigham.dev` behind Nginx Proxy Manager" | Lab deployment fact in the product README. | Move to `hamlet/docs/hosts.md`. |
|
||||||
|
| 1.5 | **C** | `examples/external-demo/index.html:8` `base: 'http://10.0.0.234:8100'` | Demo config names the hub's LAN IP. | `http://<hamlab-host>:8100`. |
|
||||||
|
| 1.6 | **C** | `SENSORS.md:149` Prometheus target `10.0.0.234:8100`; `:187` `&machine=grid`; `:196-208` Glance examples at `hamlab.bigham.dev`, `zfs.tank.used_pct`, `/api/nodes/grid/...`; `:218-220` `grid,wyse`, `{"core":…,"grid":…,"mac":…}` | Docs use real host names, pool and domain. | Generic placeholders (`<host>`, `<machine>`, `<pool>`). |
|
||||||
|
| 1.7 | **C** | `PLAN.md:3-4` "bare-metal Debian 13 host. First target: `core` (10.0.0.234)"; decisions table rows (Listen `10.0.0.234:8100`, Data `/home/core/apps/hamlab/`, Ingress NPM/`hamlab.bigham.dev`, Repo) | Product plan is written as this lab's plan. | Keep as history, or split lab facts out to `hamlet`. The repo is public. |
|
||||||
|
| 1.8 | **C** | `VIRTUALIZATION.md:339-353` per-machine probe table; `:412, 425-436` `~/apps/hamlab`, `/tank/...`, boot NVMe free space | Plan is written against the five lab hosts. | Same as 1.7. |
|
||||||
|
| 1.9 | **C** | `BUGS.md` throughout | Bug bashes narrate `core`, `grid`, `wyse`, `hamtv`, `gate`, the Mac. | History; fine to leave, but it is public. |
|
||||||
|
| 1.10 | **C** | `cmd/hamlab/main.go:1` "Proxmox-style web console for a bare-metal Debian host"; `internal/hardware/hardware.go:5` "what Debian and its kin already have"; `internal/hardware/hardware.go:29` `// "GMKtec NucBox G3"`; `internal/metrics/metrics.go:1358` "did, on grid, 2026-10-02"; `internal/api/server.go:1382-1383` `["grid", …]`, `?machines=grid,wyse`; `internal/api/stacks.go:10` `/home/core/apps/dockhand/stacks/…`; `internal/api/dockersched.go:5` "unbound, nginx and pihole"; `internal/api/zfs.go:5` "grid already runs zfSnap"; `internal/storage/snapshots.go:6` "grid has run zfSnap v1.11.1"; `internal/api/guests.go:8` "Mitchell, 2026-09-27"; `internal/network/network.go:306` "the public address here is a DHCP lease"; `internal/metrics/gpu.go:17` "Asahi, today"; `internal/metrics/pciids.go:4-6` "the router's GPU"; `web/src/Top.tsx:522, 576`; `web/src/hashsel.ts:1-9`; `web/src/Embed.tsx:3` `&machine=grid` | Comments explain decisions by naming lab hosts. | Harmless, but rewrite the ones in package doc comments so a reader of the source doesn't need the lab to understand the code. |
|
||||||
|
|
||||||
|
## 2. UI placeholders and defaults that are this lab's
|
||||||
|
|
||||||
|
| # | Sev | Where | Text | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 2.1 | **C** | `web/src/Machines.tsx:116-118` | `placeholder="grid"`, `"https://10.0.0.214:8100"`, `"core"` | `nas`, `https://192.0.2.10:8100`, `admin` or the hub's own user. |
|
||||||
|
| 2.2 | **C** | `web/src/KVM.tsx:166` | `placeholder="10.0.0.217"` (the NanoKVM on `grid`) | `192.0.2.20` or `kvm.local`. |
|
||||||
|
| 2.3 | **C** | `web/src/Settings.tsx:242` | `placeholder="cpu.*, temp.*, zfs.tank.*"` | `cpu.*, temp.*, fs.*`. |
|
||||||
|
| 2.4 | **C** | `web/src/Settings.tsx:225` | read-only token default name `'glance'` | Empty, or `dashboard`. |
|
||||||
|
| 2.5 | **C** | `web/src/Alerts.tsx:38, 40` | `"Tank nearly full"`, `"zfs.tank.used_pct"` | `"Disk nearly full"`, `"fs.root.used_pct"`. |
|
||||||
|
| 2.6 | **C** | `web/src/StackNew.tsx:110, 113, 114` | `/home/core/apps/whoami:/data`, `TZ=America/Los_Angeles`, `com.example.owner=mitchell` | `./data:/data`, `TZ=UTC`, `com.example.owner=you`. |
|
||||||
|
| 2.7 | **C** | `web/src/Gallery.tsx:69`, `web/src/WidgetPicker.tsx:36` | `"find — gitea, temp, enp3s0…"`, `"filter sensors — cpu, temp, fan, enp3s0…"` | Drop the container and NIC names. |
|
||||||
|
| 2.8 | **C** | `web/src/VPN.tsx:105` | default DNS for a new device `1.1.1.1, 1.0.0.1` | Fine as a public default, but should offer the tunnel's own address (the home resolver) first. |
|
||||||
|
| 2.9 | **B** | `web/src/ZFS.tsx:212` `BLANK` | new schedule defaults `at: '02:30'`, `keep_for: '6w'` — `grid`'s old zfSnap cron line | Neutral defaults (`03:00`, `4w`), or none. |
|
||||||
|
| 2.10 | **A** | `web/src/ZFS.tsx:302` `adopt()` | `/\b(tank[\w/-]*)\b/` — the dataset of an adopted external job is found by looking for the word **`tank`** | Parse the dataset from the command as the last `zfs`-shaped argument, or match against the datasets the page already has. On any other pool name adopt silently picks the fallback. |
|
||||||
|
|
||||||
|
## 3. Topology inherited from the lab (hub `core`, Dockhand, `tcp://:2375`)
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 3.1 | **done** 2026-10-10 | `internal/api/stacks.go` `nodeDocker()` (removed) | For every node whose stacks aren't editable locally, the hub **probes `tcp://<node-host>:2375`** with `docker -H … version` and, if it answers, deploys to the node over an unauthenticated Docker socket. This is the Dockhand-era layout; on any other network an open 2375 is either absent or a security problem the console should not quietly use. | Remove the probe. The node's own Ham Lab deploys its stacks (the files live on the node, under its data dir). `PLAN.md` "To do" already names this decision. |
|
||||||
|
| 3.2 | **done** 2026-10-10 | `hubStacks()`, `taskOnHub()`, the hub-side `/api/nodes/{node}/docker/…` routes, `Stack.remote`/`held_by` (all removed; the generic node proxy carries those requests) | A hub can hold compose files for a node and run compose remotely; new stacks for a node may be created **on the hub**. Only true here because Dockhand on `core` deployed `grid` and the Mac. | Same as 3.1: one machine, one set of files, one Docker. Hub-held stacks become an import path (copy to the node) rather than a steady state. |
|
||||||
|
| 3.3 | **done** 2026-10-10 | `internal/api/stacks.go` `homesOf()` — `<DataDir>/stacks` is always offered first and default; the `~/stacks` fallback is gone; `TestHomesDefaultToDataDir` | Where new stacks go is **discovered** from where ≥2 stacks already are (Dockhand's `stacks/<machine>` habit), else `~/stacks`. Neither is Ham Lab's data directory, which the project's own rule says everything managed must live under. | Default home = `<DataDir>/stacks`. Discovered homes stay as *additional* places, never the default. |
|
||||||
|
| 3.4 | **B** | `internal/api/stacks.go:100-203` remembered mounts, `internal/api/dockersched.go:772-795` `repoint()` | Machinery exists to find compose files **inside a removed orchestrator container's filesystem**. Generic in form, but it exists because of Dockhand and is exercised only here. | Keep (it is general), but it is not a reason to keep 3.1/3.2. |
|
||||||
|
| 3.5 | — | `internal/api/stacks.go` `stackAction`, `internal/compose/compose.go` | Inside a `Via` container the command is literally `docker compose`. | **Checked 2026-10-10, no change:** the CLI inside an orchestrator's container is that container's own `docker`, independent of what the host runs; the host's tool only chooses the `exec`. Both places now say so in a comment. |
|
||||||
|
| 3.6 | **B** | `internal/api/stacks.go:908` `docker buildx imagetools inspect` | Update checks need the **buildx** plugin. Docker CE on Debian has it; Podman and minimal Docker installs do not → every image reports "the registry didn't answer". | Fall back to `skopeo inspect` or a direct registry `HEAD` for the digest. |
|
||||||
|
| 3.7 | **B** | `Makefile:445-448` `deploy` | The machine you build on **is** the hub: installs locally, then `push-nodes`. | Fine for a developer; document that `deploy` means "I am the hub". A plain `make install` target for a single box. |
|
||||||
|
| 3.8 | **B** | `internal/nodes/nodes.go:316-323` | A hub pushes other architectures only if `<prefix>/hamlab-<arch>` siblings exist, which only `make deploy`/`install.sh`-from-dist put there. | A hub without siblings should fetch the right build from `update.url` instead of erroring. |
|
||||||
|
|
||||||
|
## 4. Init system: systemd assumed where the product promises procd and OpenRC too
|
||||||
|
|
||||||
|
The installer supports systemd, procd and OpenRC (`internal/setup/setup.go:217-262`). These paths do not.
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 4.1 | **A** | `internal/updates/updates.go:537-543` `Power()` | `systemctl reboot` / `systemctl poweroff`. On OpenWrt (`gate`) and OpenRC the Power dialog fails. | Try `systemctl`, else `reboot` / `poweroff` (busybox, OpenRC, sysvinit all have them). |
|
||||||
|
| 4.2 | **A** | `internal/incus/incus.go:1017, 1044-1058, 1069, 1075` | The Incus install script writes `/etc/systemd/system/*` drop-ins and units, adds `SupplementaryGroups` to `hamlab.service`, restarts with `systemd-run`. On OpenRC/procd the daemon never joins `incus-admin` → the tab stays in "repair" forever; the Docker FORWARD fix is never persisted. `HamlabUnit = "hamlab.service"` (`:35`) is surfaced in the API (`:280`). | Branch on the detected init: OpenRC `rc-update`/`conf.d`, procd init script edits, or fall back to `usermod` + a daemon restart via the installer's own supervisor knowledge. Docker rules via a boot hook that is not a unit. |
|
||||||
|
| 4.3 | **A** | `internal/services/services.go:163-170` | Services tab is systemd-only ("says so cleanly"). `gate` (procd) and any OpenRC/runit/s6 box have no Services tab. | `rc-service`/`rc-status` (OpenRC), `/etc/init.d/<x> status` + `ubus call service list` (procd), `sv` (runit) backends. Noted as "not done" in `PLAN.md:487-489`. |
|
||||||
|
| 4.4 | **A** | `internal/cron/cron.go:353-373, 401, 442, 532-534, 669-670` | Jobs tab reads `/etc/crontab`, `/etc/cron.d`, `/var/spool/cron{,/crontabs}` (Debian/cronie), `/usr/sbin/anacron`, last-runs from **journald**, timers via `systemctl`; writes only `/etc/crontab` and `/etc/cron.d`. Busybox crond (OpenWrt, Alpine) keeps crontabs in `/etc/crontabs/<user>` and has no `cron.d` → the Jobs tab is empty on `gate` and schedules written by 4.5 never run. | Add `/etc/crontabs/*` as a source; last-runs from syslog/logread when there is no journal. |
|
||||||
|
| 4.5 | **A** | `internal/storage/snapshots.go:402` `/etc/cron.d/hamlab-zfs`; `internal/api/dockersched.go:65` `/etc/cron.d/hamlab-docker` | ZFS snapshot and image-update schedules are cron.d files. No cron.d on busybox crond; no cron at all on a systemd-timer-only host (Fedora Server, CoreOS). | Detect: cron.d → cron.d; busybox → a line in `/etc/crontabs/<user>`; none but systemd → a timer unit. The daemon-side runner already exists (`hamlab zfs-snapshot`, `docker-update`). |
|
||||||
|
| 4.6 | **B** | `internal/hardware/hardware.go:333` `systemd-detect-virt` | Virtualisation detection is systemd-only. | Fall back to `/sys/class/dmi/id/sys_vendor`, `/proc/cpuinfo` `hypervisor` flag, `/sys/devices/virtual/dmi`. |
|
||||||
|
| 4.7 | **B** | `internal/api/server.go:1129`, `internal/updates/updates.go:187` | `/run/reboot-required` is Debian/Ubuntu-only. `updates.Status` falls back to a kernel check for other distros; `/api/me.reboot_needed` (the Machines badge, the header's amber power button) does not. | Share one function. |
|
||||||
|
| 4.8 | **B** | `install.sh:154-157, 164, 375-378`; `internal/setup/setup.go:284` | The wizard's init detection knows systemd and OpenRC only: on OpenWrt it warns "no systemd or OpenRC: nothing starts it at boot" although `hamlab install` then installs a procd script fine; the "logs" hint and the "isn't answering yet" hint have no procd case. | Add procd to both. |
|
||||||
|
| 4.9 | **B** | `internal/setup/setup.go:609-641` `firewall()` | Only firewalld and ufw are detected. OpenWrt fw4/nftables, plain nftables, iptables, Alpine awall are not. | Detect `fw4`/`nft`/`iptables`; on OpenWrt add a LAN-zone rule or say what to open. |
|
||||||
|
| 4.10 | **B** | `internal/setup/setup.go:568-605` `quietSudoers()`; `internal/services/services.go:176-185` | The sudoers quieting list is tuned to what the lab polls (`smartctl`, `pwrstat`, `journalctl`, `zpool`, `zfs`). A host with NUT or apcupsd gets `upsc`/`apcaccess` lines in its auth log at 10 s intervals; a host without `/etc/sudoers.d` (OpenWrt) gets nothing, which is correct. | Build the list from what the collector actually probes. |
|
||||||
|
|
||||||
|
## 5. Distro and package assumptions beyond init
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 5.1 | **B** | `internal/incus/incus.go:213-250, 351-352` | Incus can be installed with apt, dnf, pacman, apk. **zypper is missing** (the Updates tab supports it), so an openSUSE host reads "No package manager Ham Lab knows how to drive". `opkg` is known only to say "not packaged for OpenWrt". | Add zypper; phrase the opkg case as "not packaged for this distribution". |
|
||||||
|
| 5.2 | **B** | `internal/incus/incus.go:973-978` | dnf branch adds `qemu-system-x86-core`/`edk2-ovmf` or `qemu-system-aarch64-core`/`edk2-aarch64`; other arches (riscv64, ppc64le, which `IncusArch()` maps) get no QEMU. pacman and apk branches assume their package names never change. | Table per PM × arch, or let `incus` pull its own deps where the distro does that (Debian does). |
|
||||||
|
| 5.3 | **B** | `internal/incus/incus.go:958, 970` `DEBIAN_FRONTEND`, `apt-get … -o Dpkg::Options::=--force-confold`; `internal/updates/updates.go:78, 432` | Debian-isms exported unconditionally. Harmless elsewhere. | Fine; note only. |
|
||||||
|
| 5.4 | **B** | `scripts/pci-ids-gpu.sh:7, 41` default `/usr/share/misc/pci.ids` | Debian's path for `pci.ids`; Fedora/Arch keep it in `/usr/share/hwdata/`. Build-time only. | Try both. |
|
||||||
|
| 5.5 | **B** | `internal/api/status_sensors.go:283`, `internal/api/docker.go:133` | Container memory/CPU read from three cgroup paths: cgroup v2 + systemd driver, cgroupfs, libpod. cgroup v1 hosts (older Debian/Ubuntu, some Alpine, OpenWrt) get no `container.*.mem/cpu`. | Add the v1 `memory/docker/<id>` and `cpu,cpuacct` paths, or fall back to `docker stats --no-stream` once in a while. |
|
||||||
|
| 5.6 | **B** | `internal/metrics/metrics.go:1064` `clkTck = 100` | USER_HZ assumed 100. True on every mainstream build; wrong on a kernel built otherwise. | `sysconf` via `getconf CLK_TCK` once at start. |
|
||||||
|
| 5.7 | **B** | `internal/incus/subnet.go:150-179` `ip -j`; `internal/space/manager.go:578-585` `timeout -s KILL` | iproute2 JSON output (busybox `ip` has no `-j`); GNU `timeout`. | Parse plain `ip route`/`ip addr`; busybox has `timeout` too but flag spelling differs — use the context deadline. |
|
||||||
|
|
||||||
|
## 6. Hardware and vendor assumptions
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 6.1 | **B** | `internal/metrics/gpu.go:343-383` `read()` | Busy/clock/VRAM are read for **i915, xe, amdgpu** only — the three GPUs in this lab. Nouveau, nvidia-drm, panfrost, v3d, Asahi are listed but never measured. `PLAN.md:317`: "No NVIDIA path: nothing here has one to test against." | `nvidia-smi --query-gpu` where present; `/sys/class/drm/*/device/gpu_busy_percent` for nouveau; `devfreq` for the ARM drivers. |
|
||||||
|
| 6.2 | **B** | `internal/metrics/pciids_gpu.txt`, `scripts/pci-ids-gpu.sh:12-15` | Embedded PCI ID table trimmed to Intel, AMD, NVIDIA and three virtual vendors. Matrox, ASPEED (every server BMC), Moore Threads, etc. fall back to `lspci` or "Vendor GPU <id>". | Include the full display-class device list, not three vendors. |
|
||||||
|
| 6.3 | **B** | `internal/metrics/gpu.go:237-241` | "Integrated" = PCI bus `0000:00:`. Wrong on multi-domain or APU boards where the iGPU hangs off a bridge; cosmetic. | Use `boot_vga` + `PCI_SUBSYS` heuristics, or drop the flag. |
|
||||||
|
| 6.4 | **B** | `internal/metrics/metrics.go:189-196, 917-968` | UPS probe order **pwrstat (CyberPower) first**, then NUT, then apcupsd; pwrstat's `....`-separated output is parsed bespoke. A host with both pwrstat and NUT shows pwrstat only. | Prefer NUT (the vendor-neutral one), then the vendor tools; or list every UPS found. |
|
||||||
|
| 6.5 | **B** | `internal/metrics/metrics.go:1179-1182` `cpuTemp()` | CPU temperature from `coretemp`, `k10temp`, `thermal`/`cpu`, `cpu_thermal`, `soc_thermal`. Misses `zenpower`, `acpitz` as the only sensor, Apple `macsmc` names, many ARM SoC zone names. | Broaden; fall back to the hottest zone when none matches. |
|
||||||
|
| 6.6 | **B** | `internal/hardware/hardware.go:617-627` `jedec` | JEDEC IDs for "the memory makers who turn up in a homelab" — 14 entries. | Embed the JEDEC JEP106 bank list (public, small). |
|
||||||
|
| 6.7 | **B** | `internal/storage/storage.go`, `internal/api/zfs.go`, `internal/storage/arc.go`, `web/src/ZFS.tsx` | A whole **ZFS** tab and ZFS-aware Space/Disks logic; **no btrfs or LVM equivalent** (snapshots, subvolumes, scrub, balance). Not wrong, but the tab list reflects a ZFS NAS. | Later: a Btrfs tab with the same shape, or a generic "Filesystems" tab that shows what the host has. |
|
||||||
|
| 6.8 | **B** | `internal/storage/snapshots.go:1-16, 966-968` | Snapshot naming is **zfSnap's** (`2026-09-24_02.30.01--6w`) because `grid` ran zfSnap. A sanoid/zfs-auto-snapshot user's existing snapshots (`autosnap_…`, `zfs-auto-snap_daily-…`) are detected (`internal/api/zfs.go:1764-1798`) but can never be expired by Ham Lab, and new schedules write a third convention beside them. | Make the naming a per-schedule choice (zfSnap / sanoid / zfs-auto-snapshot), or at least document the convention as the product's rather than inherited. |
|
||||||
|
| 6.9 | **B** | `internal/network/speedtest.go:64-67` `downSources` | Fallback download source is **`speedtest.seattle.linode.com`** — chosen because the lab is in the Pacific Northwest. Everywhere else it reads low. | Pick the nearest of several mirrors, or drop the fallback and report the refusal. |
|
||||||
|
| 6.10 | **B** | `internal/network/speedtest.go:70-74, 604`, `internal/network/network.go:323-334` | Public IP, edge and speed come from **Cloudflare** endpoints, polled every 15 min with no off switch. Air-gapped or privacy-minded installs have no way to disable the outbound probe. | A `network.outside: off` setting; honour it in the watcher. |
|
||||||
|
| 6.11 | **B** | `internal/vpn/edit.go:5-7, 419-421` | Peers can be **created/edited only through OpenWrt UCI** "because that is where the server here is configured". A wg-quick server (the common case everywhere else) is read-only. | Write wg-quick `.conf` too (append a `[Peer]` block + `wg set`), guarded the same way. |
|
||||||
|
| 6.12 | **B** | `internal/vpn/vpn.go:251-253` | Peer names come from wg-quick comments or UCI descriptions — "which between them cover every machine here". NetworkManager and systemd-networkd WireGuard configs are not read. | Add `.nmconnection` and `.netdev` readers. |
|
||||||
|
| 6.13 | **B** | `internal/vpn/edit.go:515` `profile()` | Split-tunnel routes hard-code `10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12` on top of the tunnel's own prefix. | Offer the host's actual LAN prefixes (from `TakenPrefixes`-style route reading), with RFC 1918 as the fallback. |
|
||||||
|
| 6.14 | **B** | `internal/incus/subnet.go:1-9, 23-34, 55`; `web/src/Virtualization.tsx:133` | Bridge subnet candidates skip **all of 10/8** because "this LAN is 10.0.0.0/8"; order is 172.16.100–254, then 192.168.254–200, then 172.16.1–99; last-resort `172.16.100.0/24`. Works anywhere, but a lab on 172.16/12 gets 192.168.x first and a lab on 192.168/16 never gets its own range. | Read the host's routes (already done) and pick from all three RFC 1918 blocks in a neutral order. |
|
||||||
|
| 6.15 | **B** | `internal/network/network.go:170-199, 303-314`; `internal/incus/subnet.go`; `internal/vpn/edit.go:558-595`; `internal/tlsauto/tlsauto.go:533-537`; `install.sh:114-120`; `internal/setup/setup.go:648-666` | **IPv4 everywhere**: default route from `/proc/net/route` only, outside view v4 only, bridge/peer allocation v4 only, cert SANs v4 only, installer lists v4 addresses only. | `/proc/net/ipv6_route`, v6 SANs, v6 peer addresses. An IPv6-only host today has no gateway, no cert SAN and no bridge. |
|
||||||
|
|
||||||
|
## 7. Privilege and deployment posture (the lab's NOPASSWD-everywhere model)
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 7.1 | **A** | `internal/priv/priv.go`; every tab that calls `priv.Available()`; `web/src/Updates.tsx:2144` "give its user passwordless sudo or doas"; `web/src/Jobs.tsx:1883` | Full function needs the service user to have **unrestricted passwordless sudo**, which `hamlet/CLAUDE.md` grants on every host here. The installer neither creates nor offers a sudoers rule, so on a fresh box most tabs are read-only and the UI tells the user to go fix sudoers by hand. | `hamlab install --sudo` writes a **scoped** `/etc/sudoers.d/hamlab` naming the commands Ham Lab runs (smartctl, zpool, zfs, systemctl, docker, journalctl, tee for its cron files, `hamlab fs|space-walk|drm-clients|cpu-energy|replace`), or installs as root. Either way the console works out of the box. |
|
||||||
|
| 7.2 | **A** | `internal/api/server.go:1069-1080` `clientKey()`, `:1083-1092`, `:431-433` `secure()` | The **first `X-Forwarded-For` hop is trusted unconditionally**, because here Ham Lab always sits behind Nginx Proxy Manager. On a host exposed directly, a client sets the header and defeats the per-client login rate limiter and the session IP check. | Trust `X-Forwarded-*` only from configured proxy addresses (`trusted_proxies` in config), else use the socket peer. |
|
||||||
|
| 7.3 | **B** | `internal/api/server.go:1324` `X-Accel-Buffering: no` "nginx (NPM)" | Harmless header; comment names the lab's proxy. | Keep header; generalise comment. |
|
||||||
|
| 7.4 | **B** | `internal/api/guests.go:1-9`, `PLAN.md:21` | Guest logins are full-power ("he gives these to people he trusts"). A product default. | Keep, but it is a posture decision taken for this household; a read-only guest is the generic expectation and was cut. Document as a deliberate choice in README. |
|
||||||
|
| 7.5 | **B** | `internal/api/users.go:1-6` | "a household console, not a tenancy": any signed-in user can add users, make tokens, open a root shell. | Same as 7.4 — fine, but say so where a stranger installs it. |
|
||||||
|
|
||||||
|
## 8. Resource tuning from the lab's smallest host
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 8.1 | **B** | `cmd/hamlab/main.go:451` `debug.SetMemoryLimit(160 << 20)`; `PLAN.md:11` "`core` has ~370 MiB free RAM" | A fixed 160 MiB soft heap limit sized for `core`. On a 64 GiB NAS it forces needless GC under a big Space scan; on a 512 MiB Pi it is still half the box. | Scale from `MemTotal` (e.g. 2–5 %, floor 64 MiB, ceiling 1 GiB), or make it a config key. |
|
||||||
|
| 8.2 | **B** | `internal/space/walk.go:893` `workers := 32`; `internal/space/manager.go:578` 3-minute `zfs diff` patience | Tuned on `grid` (16 cores) and `core`. | Workers from `GOMAXPROCS`; patience configurable. |
|
||||||
|
| 8.3 | **C** | `internal/metrics/metrics.go:32-43` intervals, `History = 300` | Reasonable generic defaults; listed only because 8.1 made them look lab-tuned. | None. |
|
||||||
|
|
||||||
|
## 9. Data directory and legacy paths
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 9.1 | **B** | `internal/config/config.go:115-117` `Candidates()`; `internal/setup/setup.go:413-422`; `README.md:38` | `~/apps/hamlab/config.json` is searched as a legacy location — the lab's pre-v0.2 path, kept so `core`, `grid` and the Mac keep working. | Keep for one more release with a one-time migration log line, then drop. A stranger never had it. |
|
||||||
|
| 9.2 | **done** 2026-10-10 | 3.3 above; `PLAN.md:24` "`grid`'s and the Mac's stacks at `~/stacks/<machine>/` are the standing exception" | The "everything under DataDir" rule has a lab-shaped exception baked into the stacks code. | Default to DataDir; treat `~/stacks` as a discovered home like any other. |
|
||||||
|
| 9.3 | **C** | `internal/setup/setup.go:351` `ownFiles` | Correctly scoped; note only. | None. |
|
||||||
|
|
||||||
|
## 10. Build and release
|
||||||
|
|
||||||
|
| # | Sev | Where | Assumes | General form |
|
||||||
|
|---|---|---|---|---|
|
||||||
|
| 10.1 | **B** | `Makefile:60-61, 442-443`; `scripts/gitea-release.sh` | Publishing is Gitea/Forgejo-only (release API shape). `selfupdate` also understands a static directory, so publishing could too. | `make release` + any static host is already enough; add a GitHub/Forgejo-agnostic `release-dir` note. |
|
||||||
|
| 10.2 | **C** | `scripts/gitea-release.sh:500-515` | Needs `python3` on the publishing host. | `jq`, or do it in Go (`hamlab publish`). |
|
||||||
|
| 10.3 | **C** | `Makefile:392` `ARCHES ?= amd64 arm64 arm 386 riscv64` | Fine. `ppc64le`/`s390x` are mapped by `IncusArch()` but never built. | Add or drop them from `IncusArch`. |
|
||||||
|
|
||||||
|
## 11. Things that are already general (checked, no change needed)
|
||||||
|
|
||||||
|
Listed so the sweep is known to be complete, not because they need work.
|
||||||
|
|
||||||
|
- `internal/priv` (root / sudo / doas), `internal/setup` service templates, `IP_FREEBIND` listen, self-update atomic replace and re-exec.
|
||||||
|
- `internal/updates`: apt, dnf, zypper, pacman, apk with refresh/upgrade/install.
|
||||||
|
- `internal/storage` block devices: `lsblk -J` with a `/sys/block` fallback, eMMC health, NVMe namespace folding, SMART only when `smartctl` exists.
|
||||||
|
- `internal/metrics` CPU/mem/hwmon/thermal/net/disk/fs from `/proc` and `/sys`; remote-FS and pseudo-FS lists; filesystem statfs with a timeout.
|
||||||
|
- `internal/hardware`: every reader may come back empty with a note; device-tree fallbacks for non-SMBIOS boards.
|
||||||
|
- `internal/incus` detection and the per-machine "ready / repair / installable / unavailable" model (modulo 4.2, 5.1, 5.2).
|
||||||
|
- `internal/nodes`, `internal/tlsauto`: hub ↔ node pairing, pinned certs, proxy, push.
|
||||||
|
- `internal/sensors`, alerts, widgets, embeds, Prometheus: machine-agnostic by design.
|
||||||
|
- `internal/files`, `internal/term`, `internal/tasks`, `internal/auth`, `internal/store`.
|
||||||
|
- `internal/api/kvm.go`: "nothing in it knows anything about any particular KVM" — verified.
|
||||||
|
- `internal/compose` (modulo 3.5).
|
||||||
|
- The React UI: machine-switcher model, hash routing, per-account prefs, themes.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Suggested order of work
|
||||||
|
|
||||||
|
1. **Posture and safety first (A):** 7.2 trusted proxies; 7.1 installer-written scoped sudoers; ~~3.1 drop the `:2375` probe (with 3.2 as its consequence)~~ done 2026-10-10.
|
||||||
|
2. **"Any Linux" promises the README already makes (A):** 4.1 power, 4.4/4.5 cron on busybox and timer-only hosts, 4.2 Incus install on non-systemd, 4.3 a Services backend for OpenRC/procd, 2.10 the `tank` regex.
|
||||||
|
3. **Lab defaults that phone home or mislead (B):** 1.1–1.3 update/install URLs, 2.9 schedule defaults, 6.9/6.10 speed-test sources and an off switch, 8.1 memory limit, ~~3.3/9.2 stack home = DataDir~~ done 2026-10-10.
|
||||||
|
4. **Breadth (B):** 5.1 zypper for Incus, 5.5 cgroup v1, 6.1/6.2 GPUs beyond Intel/AMD, 6.4 UPS order, 6.11/6.12 wg-quick/NetworkManager, 6.15 IPv6.
|
||||||
|
5. **Cosmetics (C):** every placeholder and comment in §1.10 and §2, then split lab facts out of `PLAN.md`, `VIRTUALIZATION.md`, `SENSORS.md`, `README.md`.
|
||||||
+6
-1
@@ -12,7 +12,7 @@ added to the registry in `api.New`.
|
|||||||
## IDs
|
## IDs
|
||||||
|
|
||||||
```
|
```
|
||||||
cpu.usage cpu.core.<n>.usage cpu.temp load.1|5|15 uptime
|
cpu.usage cpu.core.<n>.usage cpu.temp cpu.power load.1|5|15 uptime
|
||||||
mem.total|used|avail|free|cached|buffers|used_pct swap.total|used|avail|used_pct
|
mem.total|used|avail|free|cached|buffers|used_pct swap.total|used|avail|used_pct
|
||||||
temp.<chip>.<label> fan.<chip>.<label>
|
temp.<chip>.<label> fan.<chip>.<label>
|
||||||
net.<iface>.rx|tx|rx_total|tx_total|up
|
net.<iface>.rx|tx|rx_total|tx_total|up
|
||||||
@@ -35,6 +35,11 @@ zfs.arc.size|target|min|max|mru|mfu|data|metadata|hit_pct|reads|misses|lifetime_
|
|||||||
has run on that machine, and each one's `t` is when it ran — so a widget or an alert on
|
has run on that machine, and each one's `t` is when it ran — so a widget or an alert on
|
||||||
them is reading a measurement from that moment, not a live value. Nothing takes a speed
|
them is reading a measurement from that moment, not a live value. Nothing takes a speed
|
||||||
test on its own; something has to press the button.
|
test on its own; something has to press the button.
|
||||||
|
- `cpu.power` is the CPU packages' own power in watts, from the kernel's RAPL energy
|
||||||
|
counters (Intel, and AMD Zen), summed over every package: cores, built-in graphics and
|
||||||
|
memory controller, not the whole machine. The counters are root-only, so a daemon that is
|
||||||
|
not root reads them through a helper started once with sudo or doas. It is absent on a
|
||||||
|
machine without the counters (ARM, the Mac mini) or without a way to root.
|
||||||
- `gpu.<name>.*` is each graphics device, named as lspci names it (`gpu.intel_arc_a380`;
|
- `gpu.<name>.*` is each graphics device, named as lspci names it (`gpu.intel_arc_a380`;
|
||||||
labels carry `driver` and the PCI `bus`). `usage` is how busy the device was — from the
|
labels carry `driver` and the PCI `bus`). `usage` is how busy the device was — from the
|
||||||
GT's idle residency on Intel, `gpu_busy_percent` on amdgpu — and is absent on a driver
|
GT's idle residency on Intel, `gpu_busy_percent` on amdgpu — and is absent on a driver
|
||||||
|
|||||||
@@ -0,0 +1,358 @@
|
|||||||
|
# Virtualization tab — plan (2026-10-09; phase 1 built the same day)
|
||||||
|
|
||||||
|
**Status:** phases 1 and 2 are **deployed to all five machines as v0.1.0-196**. **No machine
|
||||||
|
has Incus installed as of 2026-10-11**: `grid`'s install of 2026-10-09 was removed at
|
||||||
|
Mitchell's request so he can install it himself from the tab and ask questions along the way
|
||||||
|
(`hamlet/docs/hosts.md` has what was removed and what the two days established). The tab
|
||||||
|
shows Install on `grid`, `core`, the Mac mini and `hamtv`, unavailable on `gate`. Both create
|
||||||
|
forms carry a **Disk location** picker with add/remove of locations; instance storage as
|
||||||
|
plain files on the machine's own disk is what he wants on `grid`. Everything in phase 2 was
|
||||||
|
verified live on `grid` before the removal: ISO fetch with checksum, New VM from ISO, the VGA
|
||||||
|
console in a real browser (headless Chromium; `binary` subprotocol, one console operation per
|
||||||
|
VM with a data websocket per SPICE channel), a virtiofs mount into a running VM, exports and
|
||||||
|
their cron line, `instance.*` sensors and the alert default. **Left:** the LAN bridge step
|
||||||
|
(his call), cloud-init presets beyond "Use my SSH keys", a backup line for `virt/exports/`.
|
||||||
|
|
||||||
|
A tab for creating, watching, consoling into and deleting **LXC containers and KVM virtual
|
||||||
|
machines**, the way Proxmox does with `pct` and `qm` — **per machine, like every other
|
||||||
|
page.** This is the scoping PLAN.md asked for. Nothing here is built. Live state of the
|
||||||
|
hosts stays in `hamlet/docs/hosts.md`.
|
||||||
|
|
||||||
|
## The governing rule: capability, not policy
|
||||||
|
|
||||||
|
Stated by Mitchell 2026-10-09, after the first draft of this plan excluded hosts on the
|
||||||
|
strength of the homelab's deployment rules:
|
||||||
|
|
||||||
|
> Anything that's done when a different machine is selected SHOULD GO ON THAT machine. I
|
||||||
|
> want the option to do it anywhere I want, unless the hardware / software literally
|
||||||
|
> doesn't support it. Ham Lab needs to be built in a general-purpose way that can run on
|
||||||
|
> any Linux system. If certain parts / libraries aren't available on that machine, those
|
||||||
|
> tabs should just report that.
|
||||||
|
|
||||||
|
So:
|
||||||
|
|
||||||
|
- **Every action lands on the machine in the switcher.** There is no "VM host", no default
|
||||||
|
machine, no cross-machine placement. `grid` is where he will most likely click first;
|
||||||
|
the tab does not know or care.
|
||||||
|
- **The tab is gated by what the machine can do, detected live** — never by a rule about
|
||||||
|
what *should* run there. The `hamlet/CLAUDE.md` do-not-break rules (Mac mini for
|
||||||
|
single-core workloads, `hamtv` not spare hardware) bind the *agent's* unasked
|
||||||
|
deployments. They are not feature gates for the product, and the first draft was wrong
|
||||||
|
to treat them as such.
|
||||||
|
- **Ham Lab owns its data, and its data lives in its own folder.** Everything this tab
|
||||||
|
manages — instance storage, ISOs, image tarballs, exports — sits under Ham Lab's data
|
||||||
|
directory on *that* machine (`config.DataDir()`, `~/apps/hamlab` on `core`, `grid` and
|
||||||
|
the Mac), in one `virt/` tree. Never a path elsewhere on the machine, however convenient
|
||||||
|
the existing folder. Stated 2026-10-09 after the first Library draft pointed the ISO shelf
|
||||||
|
at `/tank/software/isos/`: *"ALL data needs to be under a HamLab folder, because it
|
||||||
|
manages its own data."* This is the rule that moved `core`'s compose files under
|
||||||
|
`apps/hamlab/stacks/` on 2026-10-05; `grid`'s and the Mac's stacks, still at
|
||||||
|
`~/stacks/<machine>/`, are the outstanding exception and are not touched by this plan.
|
||||||
|
The engine's own runtime state (`/var/lib/incus`, like `/var/lib/docker`) is the engine's,
|
||||||
|
not Ham Lab's data: Ham Lab keeps what it would need to rebuild it.
|
||||||
|
- **Ham Lab installs or reports.** It detects what is missing, offers to install it where it
|
||||||
|
knows how, and where it cannot or the hardware cannot do the job, says so in one sentence
|
||||||
|
and offers what *does* work (containers without KVM, for instance).
|
||||||
|
|
||||||
|
## Decision: Incus 6.0 LTS as the engine
|
||||||
|
|
||||||
|
| Option | Verdict |
|
||||||
|
|---|---|
|
||||||
|
| **Incus** (LXD fork, linuxcontainers.org) | **Chosen.** One daemon, one CLI and one JSON API for both containers and VMs. Packaged by Debian 13 (`incus` 6.0.4 LTS — `apt install incus` pulls `qemu-system-x86`, `swtpm`, `virtiofsd`, `incus-agent`, so VMs work out of the box), Fedora (6.23), Arch, Alpine, Ubuntu (Zabbly repo); not by OpenWrt. Storage drivers for ZFS, btrfs, LVM and plain directories, so it fits a machine with or without ZFS. Built-in image server (`images:debian/13`, `images:ubuntu/24.04`, Alpine, Fedora, Arch… as container *or* VM, per architecture), snapshot schedules with expiry, `incus export` backups, per-instance CPU/memory limits, cloud-init, text and VGA console over websocket. Everything is reachable as `incus … --format json` or `incus query`, which is exactly how Ham Lab already drives Docker: `exec.Command`, no new Go dependency, no cgo. |
|
||||||
|
| libvirt/KVM + `virsh`, plus LXC separately | Two daemons, two models, XML. libvirt's official Go binding needs cgo, which the static binary rules out (the pure-Go `go-libvirt` exists but then containers are a second project). Rejected. |
|
||||||
|
| Proxmox | An OS, not a library. Rejected. |
|
||||||
|
|
||||||
|
## What each machine can do today
|
||||||
|
|
||||||
|
Probed 2026-10-09 (`/dev/kvm`, cpu flags, kvm modules, RAM, disk, package availability).
|
||||||
|
These are **facts the tab will detect at runtime**, not a list of where instances are
|
||||||
|
allowed. The last column is what the tab would show the day it ships.
|
||||||
|
|
||||||
|
| Machine | Arch | KVM | RAM (free) | Incus from the package manager | Pool driver available | Tab state on first open |
|
||||||
|
|---|---|---|---|---|---|---|
|
||||||
|
| `grid` | x86-64 | yes — VT-x, `kvm_intel` loaded, nested on | 62 GiB (~25; ARC holds 23 of the rest, max 40) | apt: `incus` 6.0.4 | **zfs** (2.3.9, `tank`) | **Install** — containers and VMs |
|
||||||
|
| `core` | x86-64 | yes — N100, `kvm_intel` loaded | 7.5 GiB (~4) | apt: `incus` 6.0.4 | btrfs or dir (ZFS was purged 2026-09-23) | **Install** — containers and VMs; the create form shows free memory so a VM's size is an informed choice |
|
||||||
|
| Mac mini | aarch64 | yes — Asahi KVM, 16K pages | 15 GiB (~8) | dnf: `incus` 6.23, `qemu-system-aarch64-core`, `edk2-aarch64` | btrfs or dir | **Install** — containers and VMs, **arm64 images only** (the picker filters by host arch). Whether a stock 4K-page guest kernel boots under the 16K host is untested; `muvm` does exactly that, so it is expected to. |
|
||||||
|
| `hamtv` | x86-64 | yes | 3.7 GiB (~2.9) | apt: `incus` 6.0.4 | btrfs or dir | **Install** — containers and small VMs |
|
||||||
|
| `gate` | x86-64 | **no** `/dev/kvm` | 8 GiB (~6) | **none** — OpenWrt's opkg has no Incus | — | **Unavailable**: "Incus isn't packaged for OpenWrt, and this kernel has no KVM." Nothing to install. |
|
||||||
|
|
||||||
|
Nothing is installed anywhere today: no qemu, libvirt, lxc or incus on any host.
|
||||||
|
|
||||||
|
### Detection the tab runs, in order
|
||||||
|
|
||||||
|
1. `incus` on `PATH` and its socket answering → **ready**; read pools, networks, instances.
|
||||||
|
2. Else: a package manager Ham Lab knows (apt, dnf, pacman, apk) that has an `incus`
|
||||||
|
candidate → **installable**, with the Install dialog pre-filled (below).
|
||||||
|
3. Else → **unavailable**, with the reason in one sentence (no package; no root; OpenWrt).
|
||||||
|
4. Independently: `/dev/kvm` present and openable → VMs offered. Absent → **containers
|
||||||
|
only**, said once on the page, and the create form has no VM option.
|
||||||
|
5. Architecture from `uname -m`, so the image picker only shows images that can run here.
|
||||||
|
6. Pool driver: `zfs` if the module and tools are present, else `btrfs` (installing
|
||||||
|
`btrfs-progs` as part of Install), else `dir`. The dialog says which it picked and why.
|
||||||
|
|
||||||
|
## What the tab does (v1)
|
||||||
|
|
||||||
|
Mirrors the Docker tab's shape: a list, a detail page, actions that are followed tasks,
|
||||||
|
destructive ones confirmed by typing the name.
|
||||||
|
|
||||||
|
- **Install** — one task that runs *on the selected machine*: install the package(s),
|
||||||
|
`incus admin init --preseed` with the layout from the dialog, create the pool and bridge,
|
||||||
|
add Ham Lab's user to `incus-admin` (socket access, the `docker`-group shape) and add it
|
||||||
|
to the unit's `SupplementaryGroups`, apply the Docker fix where Docker is present (below),
|
||||||
|
then restart Ham Lab. Idempotent, so it doubles as "repair". The dialog shows and lets him
|
||||||
|
edit: pool driver (detected), pool size (proposed from free space), bridge subnet
|
||||||
|
(computed), whether to apply the Docker rules. Where it all goes is not a choice: Ham
|
||||||
|
Lab's folder.
|
||||||
|
- **Instances** — containers and VMs in one list: type, state, IPv4, CPU and memory live,
|
||||||
|
disk, snapshot count, uptime. Click for detail.
|
||||||
|
- **Create** — container or VM (VM only where KVM is); image from the `images:` remote,
|
||||||
|
filtered to this architecture, searchable, with the common ones pinned (Debian 13, Ubuntu
|
||||||
|
24.04, Alpine, Fedora); name, CPUs, memory (with this machine's free memory beside it),
|
||||||
|
root disk size; network (the NAT bridge, or a LAN bridge if this machine has one);
|
||||||
|
optional cloud-init user-data (SSH key, packages, a first command); profile. Create, or
|
||||||
|
create and start. Collisions (name taken) named before Create.
|
||||||
|
- **Lifecycle** — start, stop, restart, force stop, rename, delete (type the name; the
|
||||||
|
instance's snapshots go with it, and the dialog says so).
|
||||||
|
- **Console** — two buttons, both open as tabs in the Terminal page through the existing PTY
|
||||||
|
manager (`term.CreateCmd`, the `docker exec` path): **Console** (`incus console`, the
|
||||||
|
serial console — works before the network is up, shows boot output) and **Shell**
|
||||||
|
(`incus exec <n> -- login shell`). Containers and, via `incus-agent`, VMs.
|
||||||
|
- **Snapshots** — take, restore (confirmed), delete; a **schedule** per instance with an
|
||||||
|
expiry, which is Incus's own `snapshots.schedule`/`snapshots.expiry`, so no cron file of
|
||||||
|
ours this time.
|
||||||
|
- **Resources** — edit CPU and memory limits (live for containers; VMs take CPU hotplug and
|
||||||
|
memory balloon live with the agent, else on restart), grow the root disk, add a bind mount
|
||||||
|
(a host path into a container; virtiofs into a VM).
|
||||||
|
- **Library** — ISOs for VMs and images (container templates and VM images). Its own
|
||||||
|
section below.
|
||||||
|
- **Storage and networks** — the pools with their fill, the bridges with their subnets.
|
||||||
|
Read-only in v1 beyond what Create and Install need.
|
||||||
|
- **Sensors** — `instance.<name>.cpu`, `.memory`, `.running` from Incus's state, like the
|
||||||
|
`container.*` sensors, so Alerts gets an "instance down" default and widgets can show one.
|
||||||
|
|
||||||
|
## Where the data lives
|
||||||
|
|
||||||
|
One tree per machine, under Ham Lab's data directory, created by Install:
|
||||||
|
|
||||||
|
```
|
||||||
|
~/apps/hamlab/virt/
|
||||||
|
pool.img the instance pool's backing file (zfs or btrfs loop; a plain directory
|
||||||
|
pool/ when the driver is dir) — every instance's disks live in here
|
||||||
|
isos/ the ISO library: the files themselves
|
||||||
|
images/ the image library: template tarballs and VM disk images as files, the
|
||||||
|
way Proxmox keeps CT templates — Incus's image store is loaded from here
|
||||||
|
exports/ nightly `incus export` tarballs, with retention
|
||||||
|
virt.json what Ham Lab chose and when: driver, pool size, bridge subnet, ISO and
|
||||||
|
image inventory, export schedule
|
||||||
|
```
|
||||||
|
|
||||||
|
Consequences, stated once:
|
||||||
|
|
||||||
|
- The tree is on whatever disk holds Ham Lab's data directory — the boot NVMe on all three.
|
||||||
|
Install proposes the pool size from that disk's free space. On `grid` that disk has
|
||||||
|
790 GiB free; on `core`, 175 GiB.
|
||||||
|
- Bulk data a guest needs is **bind-mounted in from where it already is** (`/tank/…` into an
|
||||||
|
instance on `grid`), the same split Docker has: the instance is Ham Lab's, the media is
|
||||||
|
not. That is a device on the instance, recorded in its config, not a second home.
|
||||||
|
- On `core` the whole tree rides the existing nightly rsync to `grid` for free. On `grid`
|
||||||
|
and the Mac it is as backed up as the rest of their Ham Lab folders — which is to say the
|
||||||
|
exports are the backup, and the backup job will need to be told about them in phase 2.
|
||||||
|
- Existing material elsewhere — the 105 GiB of ISOs at `/tank/software/isos/`, the Proxmox
|
||||||
|
archives — is **imported into the tree by copying**, file by file, on request. The
|
||||||
|
library never indexes a folder it does not own.
|
||||||
|
|
||||||
|
## Library: ISOs and images (asked for 2026-10-09)
|
||||||
|
|
||||||
|
Proxmox has a storage view with **ISO Images** and **CT Templates**. Incus has the same two
|
||||||
|
things under other names, and the tab gives them one home, a **Library** sub-tab, per machine
|
||||||
|
like everything else.
|
||||||
|
|
||||||
|
| Proxmox calls it | Incus calls it | What it is here |
|
||||||
|
|---|---|---|
|
||||||
|
| ISO Images | an ISO file attached to a VM as a `disk` device (added as a ROM device; VMs only), or a custom storage volume of type `iso` | **`virt/isos/` in Ham Lab's folder**, attached by path — no copy into the pool |
|
||||||
|
| CT Templates | **images** — the `images:` remote (linuxcontainers.org), the local image store, anything imported or published | **`virt/images/` in Ham Lab's folder** — tarballs and disk images as files, loaded into Incus from there; the remote is the catalogue to pull from |
|
||||||
|
|
||||||
|
### ISOs
|
||||||
|
|
||||||
|
- **The ISO library is `virt/isos/` in Ham Lab's folder**, on every machine, full stop. The
|
||||||
|
105 GiB at `/tank/software/isos/` on `grid` (`linux/`, `windows/`, `truenas/`,
|
||||||
|
`virtio_drivers/`…) is a *source* to import from — pick the ones wanted, Ham Lab copies them
|
||||||
|
in — not the library itself.
|
||||||
|
- **Getting an ISO in:** upload from the browser (the Files tab's `PUT /api/files/upload`
|
||||||
|
with its progress bar, reused), **download from a URL** as a followed task with an optional
|
||||||
|
checksum (the usual way — an ISO starts as a link), or **import from a path** already on the
|
||||||
|
machine — which **copies** the file into `virt/isos/` and says so. Rename, delete (refused while a VM has it attached, naming the VM).
|
||||||
|
- **Using one:** attach to / detach from a VM as a ROM device by path, with `boot.priority`
|
||||||
|
set so it boots first; **New VM from ISO** makes an empty VM (`incus init --empty --vm`)
|
||||||
|
with the ISO attached, starts it and opens the console. After install, Detach is one click
|
||||||
|
and boot order falls back to the disk.
|
||||||
|
- **Honest limit:** an ISO install is an interactive graphical installer, so it needs the
|
||||||
|
**VGA console in the browser** (`spice-html5` over Incus's VGA console websocket).
|
||||||
|
Installers that run on a serial console (Debian with `console=ttyS0`, Alpine, Arch) work
|
||||||
|
through the text console before that exists. Windows needs the virtio drivers ISO attached
|
||||||
|
as a second ROM device as well — Incus's documented path is `distrobuilder repack-windows`
|
||||||
|
to bake them in; the drivers are already in `virtio_drivers/` on `tank`. So **the ISO shelf
|
||||||
|
and the VGA console ship together, in phase 2**, moved up from phase 3.
|
||||||
|
- ISO *volumes* (`incus storage volume import <pool> <iso> <name> --type=iso`) are the
|
||||||
|
alternative: read-only, attachable to several VMs at once, but they copy the file into the
|
||||||
|
pool. Not used here — a file in `virt/isos/` is already Ham Lab's, and a volume would copy it
|
||||||
|
a second time into the pool. Offered as an option only if a need shows up.
|
||||||
|
|
||||||
|
### Images (container templates and VM images)
|
||||||
|
|
||||||
|
- **The catalogue:** browse `images:` filtered to this machine's architecture, by type
|
||||||
|
(container / VM), with the common ones pinned. **Download** copies one into the local
|
||||||
|
store (`incus image copy images:debian/13 local: --alias debian-13`) so a later Create is
|
||||||
|
instant and works without the internet. Images used by Create are cached by Incus anyway;
|
||||||
|
this is just the explicit version, Proxmox's "Templates → Download".
|
||||||
|
- **The local store:** every image here with alias, type, arch, size, origin (remote,
|
||||||
|
published, imported), when it was last used, and **which instances were made from it**
|
||||||
|
(Incus records the base image on each instance). Alias, delete (refused while an
|
||||||
|
instance's base — Incus knows), "New instance from this image".
|
||||||
|
- **Import:** a unified tarball or metadata + rootfs pair (a container template from
|
||||||
|
anywhere, including the phase-3 Proxmox conversions), or a **qcow2 / raw disk with
|
||||||
|
metadata Ham Lab writes** for a VM image — Home Assistant OS, OPNsense, a vendor cloud
|
||||||
|
image — by upload, URL, or a path on the machine. Same three ways in as ISOs.
|
||||||
|
- **Publish:** a stopped instance becomes an image with an alias — Proxmox's "convert to
|
||||||
|
template", the one thing the Images section of v1 always had.
|
||||||
|
- **Where they live:** as files in `virt/images/` — the downloaded or imported tarball, the
|
||||||
|
published image exported to one (`incus image export`) — so the library is complete without
|
||||||
|
Incus and can be reloaded into a fresh install. Incus's own image store under
|
||||||
|
`/var/lib/incus/images/` is a cache derived from it, and the page says so.
|
||||||
|
|
||||||
|
## Not in v1, with reasons
|
||||||
|
|
||||||
|
| Thing | Why not yet |
|
||||||
|
|---|---|
|
||||||
|
| **VGA console in the browser** | Incus hands the VM's SPICE socket over a websocket; drawing it needs `spice-html5` in the UI. Doable (the Incus UI does it); text console + shell cover every Linux guest until then. **Phase 2, with the ISO shelf** — an ISO install is useless without it. |
|
||||||
|
| **Windows VMs** | Incus runs them (ISO repacked with virtio drivers by `distrobuilder`), but without a VGA console in the browser they are unusable from the tab. After the above. |
|
||||||
|
| **GPU passthrough to a VM** | Removes the card from the host and from whatever uses it there (Jellyfin on `grid`). Offered only with that warning, later. Sharing a GPU into a *container* (`gpu` device) leaves the host its card and is how `r720-jellyfin` used to run. Phase 3. |
|
||||||
|
| **Clustering / live migration** | Incus clusters, but these machines differ in size and one is arm64, with no shared storage. Not planned. |
|
||||||
|
| **A fleet-wide instance list** | One machine per view. The switcher is the machine. |
|
||||||
|
| **A second engine (bare LXC on OpenWrt, libvirt-only hosts)** | Would double the backend for one router that has no KVM. The tab reports Incus unavailable there and stops. |
|
||||||
|
|
||||||
|
## Layout decisions Ham Lab makes per machine (overridable in the Install dialog)
|
||||||
|
|
||||||
|
1. **Storage — revised 2026-10-11.** The create forms carry a **Disk location** picker: a
|
||||||
|
list of the machine's Incus storage pools, each described by where its bytes land, with
|
||||||
|
"+ add a location" (any folder on the machine becomes a plain-files pool). On `grid` the
|
||||||
|
one location is `local`: plain files under `virt/pool/` on the root NVMe, each VM's disk
|
||||||
|
its own sparse file sized on its own, each container a directory, nothing pre-sized.
|
||||||
|
*The first design* (a ZFS pool inside one sparse file, handed to Incus as the pool's
|
||||||
|
source, for instant snapshots and clones) was built, ran for two days, and was taken out
|
||||||
|
at Mitchell's instruction: one pre-sized file holding every disk is an opaque middle
|
||||||
|
layer with a number on it that reads as a reservation, and "zfs · hamlab-virt" on the page
|
||||||
|
read as the NAS pool. The Install dialog still offers zfs/btrfs-in-a-file for a machine
|
||||||
|
that wants instant snapshots; the labels now say exactly what each is. Bulk data is still
|
||||||
|
**bind-mounted in**, not stored in the pool.
|
||||||
|
2. **Bridge subnet, computed.** Incus's `auto` picks a random `10.x.y.0/24`, and **this
|
||||||
|
LAN is `10.0.0.0/8`** — every host has a /8 on its NIC, so any 10.x bridge collides.
|
||||||
|
Ham Lab reads the machine's routes and picks the first free /24 from `192.168.0.0/16`,
|
||||||
|
then `172.16.0.0/12`, that is neither on-link nor routed nor inside Docker's configured
|
||||||
|
pool (`172.28.0.0/14` on `grid`; `172.19`–`172.29` and `192.168.80`–`143` live on
|
||||||
|
`core`). Instances get `name.incus` DNS from Incus's dnsmasq.
|
||||||
|
3. **LAN addresses are a later, explicit step.** A `br0` on the machine's uplink lets an
|
||||||
|
instance take a lease from `gate` and a Pi-hole name, but it rewrites the host's network
|
||||||
|
config and drops it off the network for seconds — never part of Install, always its own
|
||||||
|
confirmed action (with the NanoKVM standing by on `grid`). macvlan was considered and
|
||||||
|
rejected: no host change, but host and guests cannot talk, so nothing on the host could
|
||||||
|
reach an instance.
|
||||||
|
4. **Docker and Incus on one host.** Docker sets the `FORWARD` policy to `DROP`, which
|
||||||
|
silently kills the Incus bridge's traffic. Fix is two `DOCKER-USER` accept rules for the
|
||||||
|
bridge, persisted — applied by Install wherever Docker is detected (`grid`, `core`, the
|
||||||
|
Mac, `hamtv` all run it).
|
||||||
|
5. **Memory.** A VM's memory is a hard reservation; the create form shows the machine's
|
||||||
|
free memory and warns past it, and never refuses. On a ZFS host Install offers to cap
|
||||||
|
the ARC (on `grid`, 40 GiB → 16 GiB) so guest RAM is deterministic; his choice.
|
||||||
|
6. **Privilege.** Ham Lab's user joins `incus-admin`; no sudo in the hot path. Install
|
||||||
|
itself goes through `priv` like the other installers.
|
||||||
|
7. **Backups.** Nightly `incus export --instance-only` of every instance to `virt/exports/`
|
||||||
|
with a retention, as a cron line like the ZFS schedules (`hamlab incus-export`), from
|
||||||
|
phase 2. On `core` that folder is inside the nightly rsync already; on `grid` and the Mac
|
||||||
|
the backup job gets a line for it in phase 2.
|
||||||
|
|
||||||
|
## The old Proxmox estate (importable, phase 3)
|
||||||
|
|
||||||
|
`/tank/backups/proxmox/` holds the retired `r720` node: `/etc/pve` (12 LXC confs, 2 VM
|
||||||
|
confs), 118 GiB of `vzdump` archives, and three raw VM disks. Latest of each:
|
||||||
|
|
||||||
|
| ID | Name | Latest archive | Size |
|
||||||
|
|---|---|---|---|
|
||||||
|
| 100 | r720-docker | 2026-01-11 | 20.7 GB |
|
||||||
|
| 101 | r720-cockpit | 2026-01-11 | 439 MB |
|
||||||
|
| 102 | r720-wordpress-alexblaskovich | 2026-01-11 | 1.2 GB |
|
||||||
|
| 103 | r720-spoonterm | 2025-06-14 | 494 MB |
|
||||||
|
| 104 | r720-ollama | 2026-01-11 | 11.3 GB |
|
||||||
|
| 105 | r720-transmission | 2026-01-11 | 322 MB |
|
||||||
|
| 106 | r720-apache | 2026-01-11 | 347 MB |
|
||||||
|
| 107 | r720-nut | 2025-03-29 | 205 MB |
|
||||||
|
| 108 | r720-filebrowser | 2026-01-11 | 262 MB |
|
||||||
|
| 109 | r720-bookstack | 2025-04-27 | 516 MB |
|
||||||
|
| 110 | r720-jellyfin | 2026-01-11 | 2.2 GB |
|
||||||
|
| 111 | r720-wireguard | 2025-04-13 | 510 MB |
|
||||||
|
| 112 | r720-eggwiki | 2026-01-11 | 634 MB |
|
||||||
|
| VMs | steam (102), powerpanel (105/107/112) | `.vma.zst` 2024–2025; raw disks `vm-105`, `vm-112`, `base-104` | 19 GB, 1.9 GB ×6, 97 GiB |
|
||||||
|
|
||||||
|
- **LXC:** feasible and scriptable. Unpack the rootfs (`tar --zstd -xpf … --numeric-owner`),
|
||||||
|
read `etc/vzdump/pct.conf` for arch/ostype, drop it, write `metadata.yaml`, pack, `incus
|
||||||
|
image import`, launch. Unprivileged uid shifting is Incus's job after that. The result is
|
||||||
|
an image in the Library, so a restored container is launched from there like any other
|
||||||
|
template. An import action in the tab (pick an archive on this machine) is plausible once
|
||||||
|
the script works by hand. x86-64 archives only run on x86-64 machines; the picker says so.
|
||||||
|
- **VMs:** the raw disks import directly (`incus-migrate`, or an empty VM with the disk
|
||||||
|
imported into its root volume). The `.vma.zst` archives need Proxmox's `vma` extractor (or
|
||||||
|
the Python port) to become raw disks first. Most of these are the same `powerpanel` box
|
||||||
|
backed up six times, and `tank-space-audit.md` already has them down for deletion — decide
|
||||||
|
which, if any, are wanted before anything is restored.
|
||||||
|
|
||||||
|
## Build plan
|
||||||
|
|
||||||
|
| Phase | What | Effort |
|
||||||
|
|---|---|---|
|
||||||
|
| **1 — detection, Install, the tab** | `internal/incus` (detection, CLI wrapper, JSON types, `incus monitor` for live state, the preseed builder and subnet picker), `internal/api/virt.go` (routes below), `Virtualization.tsx` (the three page states; list, detail, create dialog, snapshot panel), rail entry `virt`, terminal integration, sensors, and the Library's **Images** shelf (browse and download from `images:`, local store, import, publish). **Tested by clicking Install on `grid` first, then `core`** — two different pool drivers, both with Docker present. Then the Mac for the arm64 path. `gate` for the unavailable state. | 2–3 sessions |
|
||||||
|
| **2 — operations (done 2026-10-09, except the LAN bridge)** | The Library's **ISO** shelf (directory, upload/URL/path, attach, New VM from ISO) **with the VGA console** (`spice-html5`), snapshot schedules UI, nightly exports with retention and the per-machine export directory, limits editor, bind mounts, the LAN-bridge step, instance alert default, cloud-init presets. | 1–2 sessions |
|
||||||
|
| **3 — the rest** | Proxmox import (archives become Library images), GPU device for containers, Windows via `distrobuilder repack-windows`, GPU passthrough to a VM with its warning. | as wanted |
|
||||||
|
|
||||||
|
### API sketch
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/virt {state: ready|installable|unavailable, reason, kvm, arch,
|
||||||
|
pool_drivers[], pools[], networks[], instances[], free_memory}
|
||||||
|
POST /api/virt/install {driver, size, path, subnet, docker_rules, arc_max?} task
|
||||||
|
GET /api/virt/images?remote=images&q= local cache + remote search, filtered to this arch
|
||||||
|
DELETE /api/virt/images/{fingerprint}
|
||||||
|
POST /api/virt/instances {name, type, image, cpu, memory, disk, network, cloudinit, profile, start} task
|
||||||
|
GET /api/virt/instances/{name} config, state, devices, snapshots
|
||||||
|
PATCH /api/virt/instances/{name} limits, description, devices
|
||||||
|
POST /api/virt/instances/{name}/{start|stop|restart|kill|delete|rename|publish|export}
|
||||||
|
POST /api/virt/instances/{name}/console → terminal {id} (incus console)
|
||||||
|
POST /api/virt/instances/{name}/exec → terminal {id} (incus exec -- sh -lc …)
|
||||||
|
POST /api/virt/instances/{name}/snapshots {name}; POST …/{snap}/restore; DELETE …/{snap}
|
||||||
|
PUT /api/virt/instances/{name}/schedule {cron, expiry}
|
||||||
|
POST /api/virt/import {path} task (export tarball, or phase-3 Proxmox archive)
|
||||||
|
|
||||||
|
GET /api/virt/isos virt/isos/, scanned: name, size, attached-to
|
||||||
|
POST /api/virt/isos/import {path} task: copy a file on this machine into virt/isos/
|
||||||
|
POST /api/virt/isos/fetch {url, sha256?} task; uploads reuse PUT /api/files/upload
|
||||||
|
DELETE /api/virt/isos/{name} refused while attached
|
||||||
|
POST /api/virt/instances/{name}/iso {iso, boot_first} attach; DELETE …/iso detach
|
||||||
|
POST /api/virt/instances/from-iso {name, iso, cpu, memory, disk, network} task → console
|
||||||
|
GET /api/virt/images/remote?q=&type= the `images:` catalogue for this arch
|
||||||
|
POST /api/virt/images/download {alias} task (incus image copy images:… local:)
|
||||||
|
POST /api/virt/images/import {path | url, alias, type} task: copy into virt/images/, then load into Incus
|
||||||
|
GET /api/virt/instances/{name}/vga websocket: Incus VGA console → spice-html5 (phase 2)
|
||||||
|
```
|
||||||
|
|
||||||
|
Everything is per machine and reaches a node through the existing `/api/nodes/{name}/…`
|
||||||
|
proxy with no hub-side code; consoles attach over the existing `/api/terminals/{id}/attach`.
|
||||||
|
Tasks stream through `/api/tasks/{id}/stream` as Docker's do.
|
||||||
|
|
||||||
|
## Sources
|
||||||
|
|
||||||
|
- Incus on Debian: https://wiki.debian.org/Incus
|
||||||
|
- Incus and Docker on one host (the `FORWARD` drop): https://linuxcontainers.org/incus/docs/main/howto/network_bridge_firewalld/
|
||||||
|
- Bridge options and `ipv4.address: auto`: https://linuxcontainers.org/incus/docs/main/reference/network_bridge/
|
||||||
|
- Consoles (text and VGA): https://linuxcontainers.org/incus/docs/main/howto/instances_console/
|
||||||
|
- ZFS storage driver: https://linuxcontainers.org/incus/docs/main/reference/storage_zfs/
|
||||||
|
- macvlan's host-isolation: https://linuxcontainers.org/incus/docs/main/reference/network_macvlan/
|
||||||
|
- Image format for the Proxmox import: https://linuxcontainers.org/incus/docs/main/reference/image_format/
|
||||||
@@ -30,6 +30,8 @@ import (
|
|||||||
"hamlab/internal/config"
|
"hamlab/internal/config"
|
||||||
"hamlab/internal/cron"
|
"hamlab/internal/cron"
|
||||||
"hamlab/internal/files"
|
"hamlab/internal/files"
|
||||||
|
"hamlab/internal/incus"
|
||||||
|
"hamlab/internal/media"
|
||||||
"hamlab/internal/metrics"
|
"hamlab/internal/metrics"
|
||||||
"hamlab/internal/nodes"
|
"hamlab/internal/nodes"
|
||||||
"hamlab/internal/priv"
|
"hamlab/internal/priv"
|
||||||
@@ -62,6 +64,7 @@ func main() {
|
|||||||
passwd [user] set a user's password (makes the account if it is new; reads stdin when piped)
|
passwd [user] set a user's password (makes the account if it is new; reads stdin when piped)
|
||||||
users list the accounts
|
users list the accounts
|
||||||
zfs-snapshot <id> run one ZFS snapshot schedule (what its cron line calls)
|
zfs-snapshot <id> run one ZFS snapshot schedule (what its cron line calls)
|
||||||
|
virt-export [name…] export Incus instances to the library (what its cron line calls)
|
||||||
docker-update <id> run one image-update schedule (what its cron line calls)
|
docker-update <id> run one image-update schedule (what its cron line calls)
|
||||||
user-rm <user> remove an account and sign it out everywhere
|
user-rm <user> remove an account and sign it out everywhere
|
||||||
check verify the config and state load, then exit
|
check verify the config and state load, then exit
|
||||||
@@ -80,10 +83,16 @@ func main() {
|
|||||||
switch cmd { // commands that don't read the config
|
switch cmd { // commands that don't read the config
|
||||||
case "fs": // privileged helper for the Files tab (run as root by the daemon)
|
case "fs": // privileged helper for the Files tab (run as root by the daemon)
|
||||||
os.Exit(files.Helper(flag.Args()[1:], os.Stdin, os.Stdout, os.Stderr))
|
os.Exit(files.Helper(flag.Args()[1:], os.Stdin, os.Stdout, os.Stderr))
|
||||||
|
case "thumb": // privileged helper for the Files tab's previews in root mode (run as root by the daemon)
|
||||||
|
os.Exit(media.Helper(flag.Args()[1:], os.Stdin, os.Stdout, os.Stderr))
|
||||||
|
case "run-ffmpeg": // privileged helper for playback in root mode: ffmpeg as root, ending when stdin closes
|
||||||
|
os.Exit(media.RunFFmpegHelper(flag.Args()[1:], os.Stdin, os.Stderr))
|
||||||
case "space-walk": // helper for the Space tab: measure mountpoints, stream the tree back
|
case "space-walk": // helper for the Space tab: measure mountpoints, stream the tree back
|
||||||
os.Exit(space.Helper(flag.Args()[1:], os.Stdin, os.Stdout, os.Stderr))
|
os.Exit(space.Helper(flag.Args()[1:], os.Stdin, os.Stdout, os.Stderr))
|
||||||
case "drm-clients": // privileged helper for the GPU readings: who has the GPU open (run as root by the daemon)
|
case "drm-clients": // privileged helper for the GPU readings: who has the GPU open (run as root by the daemon)
|
||||||
os.Exit(metrics.DRMClientsHelper(os.Stdin, os.Stdout))
|
os.Exit(metrics.DRMClientsHelper(os.Stdin, os.Stdout))
|
||||||
|
case "cpu-energy": // privileged helper for the CPU power reading: the packages' energy counters (run as root by the daemon)
|
||||||
|
os.Exit(metrics.CPUEnergyHelper(os.Stdin, os.Stdout))
|
||||||
case "install", "uninstall":
|
case "install", "uninstall":
|
||||||
var err error
|
var err error
|
||||||
if cmd == "install" {
|
if cmd == "install" {
|
||||||
@@ -143,6 +152,10 @@ func main() {
|
|||||||
if err := zfsSnapshot(cfg, flag.Arg(1)); err != nil {
|
if err := zfsSnapshot(cfg, flag.Arg(1)); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
case "virt-export": // the nightly cron line from the Virtualization tab; optional instance names
|
||||||
|
if err := virtExport(cfg, flag.Args()[1:]); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
case "users":
|
case "users":
|
||||||
migrateUsers(cfg)
|
migrateUsers(cfg)
|
||||||
if len(cfg.Users) == 0 {
|
if len(cfg.Users) == 0 {
|
||||||
@@ -293,6 +306,21 @@ func zfsSnapshot(cfg *config.Config, id string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// virtExport runs the instance exports the Virtualization tab scheduled, as the account Ham
|
||||||
|
// Lab runs as (which is in incus-admin), and records the run beside the data.
|
||||||
|
func virtExport(cfg *config.Config, names []string) error {
|
||||||
|
who := os.Getenv("USER")
|
||||||
|
if u, err := user.Current(); err == nil && u.Username != "" {
|
||||||
|
who = u.Username
|
||||||
|
}
|
||||||
|
run := incus.New(cfg.DataDir(), who, version).RunExports(context.Background(), names, func(l string) { fmt.Println(l) })
|
||||||
|
fmt.Printf("exported %d, pruned %d, %.0f s\n", len(run.Exported), len(run.Pruned), run.Seconds)
|
||||||
|
if len(run.Errors) > 0 {
|
||||||
|
return errors.New(strings.Join(run.Errors, "; "))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func migrateUsers(cfg *config.Config) {
|
func migrateUsers(cfg *config.Config) {
|
||||||
name := os.Getenv("USER")
|
name := os.Getenv("USER")
|
||||||
if u, err := user.Current(); err == nil && u.Username != "" {
|
if u, err := user.Current(); err == nil && u.Username != "" {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ go 1.26.0
|
|||||||
require (
|
require (
|
||||||
github.com/coder/websocket v1.8.15
|
github.com/coder/websocket v1.8.15
|
||||||
github.com/creack/pty v1.1.24
|
github.com/creack/pty v1.1.24
|
||||||
|
go.etcd.io/bbolt v1.5.0
|
||||||
golang.org/x/crypto v0.57.0
|
golang.org/x/crypto v0.57.0
|
||||||
golang.org/x/term v0.46.0
|
golang.org/x/term v0.46.0
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,9 +2,21 @@ github.com/coder/websocket v1.8.15 h1:6B2JPeOGlpff2Uz6vOEH1Vzpi0iUz20A+lPVhPHtNU
|
|||||||
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
github.com/coder/websocket v1.8.15/go.mod h1:NX3SzP+inril6yawo5CQXx8+fk145lPDC6pumgx0mVg=
|
||||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||||
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
|
go.etcd.io/bbolt v1.5.0 h1:S7GAl7Fxv12yohbwFfIbQCGDWbQbtDGPET4P/bD4lxU=
|
||||||
|
go.etcd.io/bbolt v1.5.0/go.mod h1:mkltfYE5aUHQxUct9N9V+Kp7aSjFqjgrhcXIS70Lrdk=
|
||||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||||
|
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
|
||||||
|
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
|||||||
@@ -71,6 +71,9 @@ type alertSaved struct {
|
|||||||
Channels []AlertChannel `json:"channels"`
|
Channels []AlertChannel `json:"channels"`
|
||||||
Events []AlertEvent `json:"events"`
|
Events []AlertEvent `json:"events"`
|
||||||
Seeded bool `json:"seeded"`
|
Seeded bool `json:"seeded"`
|
||||||
|
// SawInstanceRule marks that the instance-down default has been offered once; set so that
|
||||||
|
// removing it stays removed.
|
||||||
|
SawInstanceRule bool `json:"saw_instance_rule,omitempty"`
|
||||||
// Firing is what was firing when the hub last looked, so a restart (every deploy is one)
|
// Firing is what was firing when the hub last looked, so a restart (every deploy is one)
|
||||||
// carries on where it was and doesn't announce the same alerts all over again.
|
// carries on where it was and doesn't announce the same alerts all over again.
|
||||||
Firing map[string]alertKept `json:"firing,omitempty"`
|
Firing map[string]alertKept `json:"firing,omitempty"`
|
||||||
@@ -102,6 +105,7 @@ var defaultRules = []AlertRule{
|
|||||||
{ID: "machine-offline", Name: "Machine not answering", Sensor: "hamlab.reachable", Op: "==", Value: false, ForSec: 120, Severity: "crit", Enabled: true},
|
{ID: "machine-offline", Name: "Machine not answering", Sensor: "hamlab.reachable", Op: "==", Value: false, ForSec: 120, Severity: "crit", Enabled: true},
|
||||||
{ID: "container-down", Name: "Container down", Sensor: "container.*.down", Op: "==", Value: true, ForSec: 120, Severity: "crit", Enabled: true},
|
{ID: "container-down", Name: "Container down", Sensor: "container.*.down", Op: "==", Value: true, ForSec: 120, Severity: "crit", Enabled: true},
|
||||||
{ID: "container-unhealthy", Name: "Container unhealthy", Sensor: "container.*.health", Op: "==", Value: "unhealthy", ForSec: 180, Severity: "warn", Enabled: true},
|
{ID: "container-unhealthy", Name: "Container unhealthy", Sensor: "container.*.health", Op: "==", Value: "unhealthy", ForSec: 180, Severity: "warn", Enabled: true},
|
||||||
|
{ID: "instance-down", Name: "VM or container down", Sensor: "instance.*.down", Op: "==", Value: true, ForSec: 120, Severity: "crit", Enabled: true},
|
||||||
{ID: "service-failed", Name: "A service has failed", Sensor: "services.failed", Op: ">", Value: 0.0, ForSec: 300, Severity: "warn", Enabled: true},
|
{ID: "service-failed", Name: "A service has failed", Sensor: "services.failed", Op: ">", Value: 0.0, ForSec: 300, Severity: "warn", Enabled: true},
|
||||||
{ID: "disk-full", Name: "Filesystem nearly full", Sensor: "fs.*.used_pct", Op: ">=", Value: 90.0, ForSec: 300, Severity: "warn", Enabled: true},
|
{ID: "disk-full", Name: "Filesystem nearly full", Sensor: "fs.*.used_pct", Op: ">=", Value: 90.0, ForSec: 300, Severity: "warn", Enabled: true},
|
||||||
{ID: "pool-unhealthy", Name: "ZFS pool not healthy", Sensor: "zfs.*.healthy", Op: "==", Value: false, ForSec: 60, Severity: "crit", Enabled: true},
|
{ID: "pool-unhealthy", Name: "ZFS pool not healthy", Sensor: "zfs.*.healthy", Op: "==", Value: false, ForSec: 60, Severity: "crit", Enabled: true},
|
||||||
@@ -122,6 +126,23 @@ func newAlertEngine(s *Server) *alertEngine {
|
|||||||
for key, k := range e.saved.Firing {
|
for key, k := range e.saved.Firing {
|
||||||
e.state[key] = &alertState{pending: time.UnixMilli(k.Since), firing: true, since: time.UnixMilli(k.Since), value: k.Value}
|
e.state[key] = &alertState{pending: time.UnixMilli(k.Since), firing: true, since: time.UnixMilli(k.Since), value: k.Value}
|
||||||
}
|
}
|
||||||
|
// a default added after an install was seeded joins its rules once; removing it afterwards sticks
|
||||||
|
if e.saved.Seeded && !e.saved.SawInstanceRule {
|
||||||
|
has := false
|
||||||
|
for _, r := range e.saved.Rules {
|
||||||
|
if r.ID == "instance-down" {
|
||||||
|
has = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !has {
|
||||||
|
for _, d := range defaultRules {
|
||||||
|
if d.ID == "instance-down" {
|
||||||
|
e.saved.Rules = append(e.saved.Rules, d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
e.saved.SawInstanceRule = true
|
||||||
|
}
|
||||||
for i, r := range e.saved.Rules { // the first day's default watched "not running", which a container stopped on purpose also is
|
for i, r := range e.saved.Rules { // the first day's default watched "not running", which a container stopped on purpose also is
|
||||||
if r.ID == "container-down" && r.Sensor == "container.*.running" {
|
if r.ID == "container-down" && r.Sensor == "container.*.running" {
|
||||||
e.saved.Rules[i].Name, e.saved.Rules[i].Sensor, e.saved.Rules[i].Value = "Container down", "container.*.down", true
|
e.saved.Rules[i].Name, e.saved.Rules[i].Sensor, e.saved.Rules[i].Value = "Container down", "container.*.down", true
|
||||||
|
|||||||
+22
-3
@@ -343,6 +343,21 @@ func (s *Server) dockerLogs(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// dockerInspect is the parts of `docker inspect` worth reading on a phone.
|
// dockerInspect is the parts of `docker inspect` worth reading on a phone.
|
||||||
|
// dockerErr answers for a docker command that failed: a thing that isn't there is a 404,
|
||||||
|
// the daemon being away a 503, anything else a 500.
|
||||||
|
func dockerErr(w http.ResponseWriter, err error) {
|
||||||
|
msg := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(err.Error()), "error: "))
|
||||||
|
low := strings.ToLower(msg)
|
||||||
|
switch {
|
||||||
|
case strings.Contains(low, "no such object") || strings.Contains(low, "no such container") || strings.Contains(low, "not found"):
|
||||||
|
jsonErr(w, 404, msg)
|
||||||
|
case strings.Contains(low, "cannot connect to the docker daemon") || strings.Contains(low, "is the docker daemon running"):
|
||||||
|
jsonErr(w, 503, msg)
|
||||||
|
default:
|
||||||
|
jsonErr(w, 500, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) dockerInspect(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) dockerInspect(w http.ResponseWriter, r *http.Request) {
|
||||||
id := r.PathValue("id")
|
id := r.PathValue("id")
|
||||||
if !dockerRef.MatchString(id) {
|
if !dockerRef.MatchString(id) {
|
||||||
@@ -352,9 +367,13 @@ func (s *Server) dockerInspect(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
out, err := dockerRun(ctx, "inspect", id)
|
out, err := dockerRun(ctx, "inspect", id)
|
||||||
|
if err != nil {
|
||||||
|
dockerErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
var all []map[string]any
|
var all []map[string]any
|
||||||
if err != nil || json.Unmarshal(out, &all) != nil || len(all) == 0 {
|
if json.Unmarshal(out, &all) != nil || len(all) == 0 {
|
||||||
jsonErr(w, 500, fmt.Sprint("inspect failed: ", err))
|
jsonErr(w, 500, "inspect: unreadable answer")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c := all[0]
|
c := all[0]
|
||||||
@@ -486,7 +505,7 @@ func (s *Server) dockerHealth(w http.ResponseWriter, r *http.Request) {
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
out, err := dockerRun(ctx, "inspect", "--format", "{{json .State}}", id)
|
out, err := dockerRun(ctx, "inspect", "--format", "{{json .State}}", id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 500, err.Error())
|
dockerErr(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var st struct {
|
var st struct {
|
||||||
|
|||||||
@@ -55,11 +55,14 @@ type composeSpec struct {
|
|||||||
Project string `json:"project"`
|
Project string `json:"project"`
|
||||||
Files []string `json:"files"`
|
Files []string `json:"files"`
|
||||||
Via string `json:"via,omitempty"`
|
Via string `json:"via,omitempty"`
|
||||||
|
// Remote is read but never written: schedules made before v0.2 could point compose at
|
||||||
|
// another machine's Docker over tcp. Such a job is refused rather than run against this
|
||||||
|
// machine's Docker, which is where it would land without the variable.
|
||||||
Remote string `json:"remote,omitempty"`
|
Remote string `json:"remote,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c composeSpec) toSpec(tool string) compose.Spec {
|
func (c composeSpec) toSpec(tool string) compose.Spec {
|
||||||
return compose.Spec{Project: c.Project, Files: c.Files, Via: c.Via, Remote: c.Remote, Tool: tool}
|
return compose.Spec{Project: c.Project, Files: c.Files, Via: c.Via, Tool: tool}
|
||||||
}
|
}
|
||||||
|
|
||||||
const dockerCronFile = "/etc/cron.d/hamlab-docker"
|
const dockerCronFile = "/etc/cron.d/hamlab-docker"
|
||||||
@@ -169,6 +172,9 @@ func RunUpdate(ctx context.Context, db *store.Store, j UpdateJob, tool string) (
|
|||||||
if len(j.Spec.Files) == 0 {
|
if len(j.Spec.Files) == 0 {
|
||||||
return "", errors.New("this schedule has no compose file recorded — open it in the Docker tab and save it again")
|
return "", errors.New("this schedule has no compose file recorded — open it in the Docker tab and save it again")
|
||||||
}
|
}
|
||||||
|
if j.Spec.Remote != "" {
|
||||||
|
return "", errors.New("this schedule was made to deploy to another machine over tcp, which this console no longer does — make it again on that machine")
|
||||||
|
}
|
||||||
spec := repoint(ctx, db, j.Spec.toSpec(tool))
|
spec := repoint(ctx, db, j.Spec.toSpec(tool))
|
||||||
var log strings.Builder
|
var log strings.Builder
|
||||||
run := func(args ...string) error {
|
run := func(args ...string) error {
|
||||||
@@ -238,7 +244,7 @@ func (s *Server) dsPut(w http.ResponseWriter, r *http.Request) {
|
|||||||
// seenFiles is the list as compose itself sees it: host paths normally, the
|
// seenFiles is the list as compose itself sees it: host paths normally, the
|
||||||
// orchestrator container's own paths when compose runs inside one. Getting this
|
// orchestrator container's own paths when compose runs inside one. Getting this
|
||||||
// backwards gives a cron line that works from the console and fails at 3am.
|
// backwards gives a cron line that works from the console and fails at 3am.
|
||||||
j.Spec = composeSpec{Project: st.Name, Files: seenFiles(st, -1, ""), Via: st.Via, Remote: st.Remote}
|
j.Spec = composeSpec{Project: st.Name, Files: seenFiles(st, -1, ""), Via: st.Via}
|
||||||
if len(j.Spec.Files) == 0 {
|
if len(j.Spec.Files) == 0 {
|
||||||
jsonErr(w, 409, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 409, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"hamlab/internal/files"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A file read answers Range requests — what a <video> needs to seek — and HEAD, through
|
||||||
|
// the same handler that streams everything else.
|
||||||
|
func TestFilesReadRanges(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
p := filepath.Join(dir, "clip.mp4")
|
||||||
|
data := make([]byte, 10000)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = byte(i % 251)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s := &Server{fsLocal: files.Local{}}
|
||||||
|
get := func(method, extra string, hdr map[string]string) *httptest.ResponseRecorder {
|
||||||
|
r := httptest.NewRequest(method, "/api/files/read?path="+url.QueryEscape(p)+extra, nil)
|
||||||
|
for k, v := range hdr {
|
||||||
|
r.Header.Set(k, v)
|
||||||
|
}
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.filesRead(w, r)
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
w := get("GET", "", nil)
|
||||||
|
if w.Code != 200 || w.Body.Len() != 10000 || w.Header().Get("Content-Type") != "video/mp4" || w.Header().Get("Accept-Ranges") != "bytes" {
|
||||||
|
t.Fatalf("whole: %d %d %q %q", w.Code, w.Body.Len(), w.Header().Get("Content-Type"), w.Header().Get("Accept-Ranges"))
|
||||||
|
}
|
||||||
|
w = get("GET", "", map[string]string{"Range": "bytes=100-199"})
|
||||||
|
if w.Code != 206 || w.Header().Get("Content-Range") != "bytes 100-199/10000" || w.Body.Len() != 100 || w.Body.Bytes()[0] != data[100] {
|
||||||
|
t.Fatalf("range: %d %q %d", w.Code, w.Header().Get("Content-Range"), w.Body.Len())
|
||||||
|
}
|
||||||
|
w = get("GET", "", map[string]string{"Range": "bytes=9990-"})
|
||||||
|
if w.Code != 206 || w.Body.Len() != 10 || w.Body.Bytes()[9] != data[9999] {
|
||||||
|
t.Fatalf("tail range: %d %d", w.Code, w.Body.Len())
|
||||||
|
}
|
||||||
|
w = get("HEAD", "", nil)
|
||||||
|
if w.Code != 200 || w.Body.Len() != 0 || w.Header().Get("Content-Length") != "10000" {
|
||||||
|
t.Fatalf("head: %d %d %q", w.Code, w.Body.Len(), w.Header().Get("Content-Length"))
|
||||||
|
}
|
||||||
|
if w := get("GET", "&max=50", nil); w.Code != 413 {
|
||||||
|
t.Fatalf("the editor's cap: %d", w.Code)
|
||||||
|
}
|
||||||
|
if w := get("GET", "", map[string]string{"Range": "bytes=20000-"}); w.Code != 416 {
|
||||||
|
t.Fatalf("a range past the end: %d", w.Code)
|
||||||
|
}
|
||||||
|
if err := os.Chmod(p, 0); err == nil && os.Geteuid() != 0 {
|
||||||
|
if w := get("GET", "", nil); w.Code != 403 {
|
||||||
|
t.Fatalf("unreadable: %d", w.Code)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A write over the cap is refused whole: the file is not truncated first.
|
||||||
|
func TestFilesWriteCap(t *testing.T) {
|
||||||
|
p := filepath.Join(t.TempDir(), "f.txt")
|
||||||
|
if err := os.WriteFile(p, []byte("keep me"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s := &Server{fsLocal: files.Local{}}
|
||||||
|
body := make([]byte, 8<<20+1)
|
||||||
|
r := httptest.NewRequest("PUT", "/api/files/write?path="+url.QueryEscape(p), bytes.NewReader(body))
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.filesWrite(w, r)
|
||||||
|
if w.Code != 413 {
|
||||||
|
t.Fatalf("got %d", w.Code)
|
||||||
|
}
|
||||||
|
if b, _ := os.ReadFile(p); string(b) != "keep me" {
|
||||||
|
t.Fatalf("the file was touched: %q", b)
|
||||||
|
}
|
||||||
|
r = httptest.NewRequest("PUT", "/api/files/write?path="+url.QueryEscape(p), bytes.NewReader([]byte("new")))
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
s.filesWrite(w, r)
|
||||||
|
if b, _ := os.ReadFile(p); w.Code != 204 || string(b) != "new" {
|
||||||
|
t.Fatalf("a write within the cap: %d %q", w.Code, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A link to nothing is a 404, not an empty 200.
|
||||||
|
func TestFilesReadDanglingLink(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
l := filepath.Join(dir, "broken")
|
||||||
|
if err := os.Symlink("nowhere", l); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s := &Server{fsLocal: files.Local{}}
|
||||||
|
r := httptest.NewRequest("GET", "/api/files/read?path="+url.QueryEscape(l), nil)
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
s.filesRead(w, r)
|
||||||
|
if w.Code != 404 {
|
||||||
|
t.Fatalf("got %d: %s", w.Code, w.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// dockerErr tells a thing that isn't there from a daemon that isn't up from anything else.
|
||||||
|
func TestDockerErr(t *testing.T) {
|
||||||
|
for _, c := range []struct {
|
||||||
|
msg string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"error: No such object: zzz", 404}, {"Error response from daemon: No such container: x", 404},
|
||||||
|
{"Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?", 503},
|
||||||
|
{"something else entirely", 500},
|
||||||
|
} {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
dockerErr(w, errors.New(c.msg))
|
||||||
|
if w.Code != c.want {
|
||||||
|
t.Errorf("%q → %d, want %d", c.msg, w.Code, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+124
-14
@@ -31,6 +31,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"hamlab/internal/config"
|
"hamlab/internal/config"
|
||||||
|
"hamlab/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
const kvmPrefix = "/kvm/"
|
const kvmPrefix = "/kvm/"
|
||||||
@@ -82,9 +83,25 @@ func (s *Server) kvmProxy(k config.KVM) (*httputil.ReverseProxy, error) {
|
|||||||
}
|
}
|
||||||
r.URL.RawPath = ""
|
r.URL.RawPath = ""
|
||||||
r.Host = target.Host
|
r.Host = target.Host
|
||||||
|
// Whether the console itself is on TLS: the response rewriter needs to know, and by
|
||||||
|
// then only the outbound request is in hand.
|
||||||
|
if secure(r) {
|
||||||
|
r.Header.Set(kvmSecureHeader, "1")
|
||||||
|
} else {
|
||||||
|
r.Header.Del(kvmSecureHeader)
|
||||||
|
}
|
||||||
// The device gets its own host and none of Ham Lab's session.
|
// The device gets its own host and none of Ham Lab's session.
|
||||||
r.Header.Del("Origin")
|
r.Header.Del("Origin")
|
||||||
stripCookie(r, cookieName, kvmCookie)
|
stripCookie(r, cookieName, kvmCookie)
|
||||||
|
// What is left is the device's own login. A browser that has it teaches the hub; one
|
||||||
|
// that has not is lent what the hub remembers, so the device sees a signed-in client.
|
||||||
|
if own := r.Cookies(); len(own) > 0 {
|
||||||
|
s.rememberKVMCookies(k.ID, own)
|
||||||
|
} else {
|
||||||
|
for name, val := range s.kvmCookies(k.ID) {
|
||||||
|
r.AddCookie(&http.Cookie{Name: name, Value: val})
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
FlushInterval: -1, // a video stream must not be buffered
|
FlushInterval: -1, // a video stream must not be buffered
|
||||||
ModifyResponse: func(resp *http.Response) error {
|
ModifyResponse: func(resp *http.Response) error {
|
||||||
@@ -94,6 +111,24 @@ func (s *Server) kvmProxy(k config.KVM) (*httputil.ReverseProxy, error) {
|
|||||||
for _, h := range []string{"X-Frame-Options", "Content-Security-Policy", "Content-Security-Policy-Report-Only", "Strict-Transport-Security", "Referrer-Policy"} {
|
for _, h := range []string{"X-Frame-Options", "Content-Security-Policy", "Content-Security-Policy-Report-Only", "Strict-Transport-Security", "Referrer-Policy"} {
|
||||||
resp.Header.Del(h)
|
resp.Header.Del(h)
|
||||||
}
|
}
|
||||||
|
// A cookie the device sets by header is kept for Ham Lab's origin — so its Domain
|
||||||
|
// (the device's) goes, Secure follows the console rather than the device, and the
|
||||||
|
// hub remembers it too.
|
||||||
|
if set := resp.Cookies(); len(set) > 0 {
|
||||||
|
https := resp.Request != nil && resp.Request.Header.Get(kvmSecureHeader) == "1"
|
||||||
|
resp.Header.Del("Set-Cookie")
|
||||||
|
for _, c := range set {
|
||||||
|
c.Domain = ""
|
||||||
|
if c.Path == "" {
|
||||||
|
c.Path = "/"
|
||||||
|
}
|
||||||
|
c.Secure = c.Secure && https
|
||||||
|
if v := c.String(); v != "" {
|
||||||
|
resp.Header.Add("Set-Cookie", v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.rememberKVMCookies(k.ID, set)
|
||||||
|
}
|
||||||
// A redirect to the device's own root has to come back through the prefix.
|
// A redirect to the device's own root has to come back through the prefix.
|
||||||
if loc := resp.Header.Get("Location"); loc != "" {
|
if loc := resp.Header.Get("Location"); loc != "" {
|
||||||
if u, err := url.Parse(loc); err == nil && (u.Host == "" || u.Host == target.Host) && strings.HasPrefix(u.Path, "/") && !strings.HasPrefix(u.Path, prefix) {
|
if u, err := url.Parse(loc); err == nil && (u.Host == "" || u.Host == target.Host) && strings.HasPrefix(u.Path, "/") && !strings.HasPrefix(u.Path, prefix) {
|
||||||
@@ -114,6 +149,93 @@ func (s *Server) kvmProxy(k config.KVM) (*httputil.ReverseProxy, error) {
|
|||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// kvmSecureHeader rides on the outbound request to say whether the console was reached
|
||||||
|
// over TLS; the device ignores it.
|
||||||
|
const kvmSecureHeader = "X-Hamlab-Secure"
|
||||||
|
|
||||||
|
// kvmCookies is what the hub remembers of a device's own cookies — its login, in practice.
|
||||||
|
func (s *Server) kvmCookies(id string) map[string]string {
|
||||||
|
var out map[string]string
|
||||||
|
s.store.View(func(st *store.State) {
|
||||||
|
if m := st.KVMCookies[id]; len(m) > 0 {
|
||||||
|
out = make(map[string]string, len(m))
|
||||||
|
for k, v := range m {
|
||||||
|
out[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// rememberKVMCookies keeps the cookies a browser (or the device's own Set-Cookie) carries
|
||||||
|
// for a KVM, so that a browser without them can be lent them. A cookie emptied or expired
|
||||||
|
// by the device is forgotten. Written only when something changed: this runs on every
|
||||||
|
// proxied request.
|
||||||
|
func (s *Server) rememberKVMCookies(id string, cs []*http.Cookie) {
|
||||||
|
have := s.kvmCookies(id)
|
||||||
|
changed := false
|
||||||
|
for _, c := range cs {
|
||||||
|
if c.Name == "" || c.Name == cookieName || c.Name == kvmCookie {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
gone := c.Value == "" || c.MaxAge < 0 || (!c.Expires.IsZero() && c.Expires.Before(time.Now()))
|
||||||
|
if gone {
|
||||||
|
if _, ok := have[c.Name]; ok {
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if have[c.Name] != c.Value {
|
||||||
|
changed = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !changed {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_ = s.store.Update(func(st *store.State) {
|
||||||
|
if st.KVMCookies == nil {
|
||||||
|
st.KVMCookies = map[string]map[string]string{}
|
||||||
|
}
|
||||||
|
m := st.KVMCookies[id]
|
||||||
|
if m == nil {
|
||||||
|
m = map[string]string{}
|
||||||
|
}
|
||||||
|
for _, c := range cs {
|
||||||
|
if c.Name == "" || c.Name == cookieName || c.Name == kvmCookie {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if c.Value == "" || c.MaxAge < 0 || (!c.Expires.IsZero() && c.Expires.Before(time.Now())) {
|
||||||
|
delete(m, c.Name)
|
||||||
|
} else {
|
||||||
|
m[c.Name] = c.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(m) == 0 {
|
||||||
|
delete(st.KVMCookies, id)
|
||||||
|
} else {
|
||||||
|
st.KVMCookies[id] = m
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// forgetKVMCookies drops a device's remembered login — when the device is removed.
|
||||||
|
func (s *Server) forgetKVMCookies(id string) {
|
||||||
|
_ = s.store.Update(func(st *store.State) { delete(st.KVMCookies, id) })
|
||||||
|
}
|
||||||
|
|
||||||
|
// lendKVMCookies hands the browser the device cookies the hub remembers and it lacks, as
|
||||||
|
// the device's own page would have set them: readable by its scripts, for Ham Lab's whole
|
||||||
|
// origin, kept for half a year. The device's SPA checks for its token client-side before it
|
||||||
|
// ever asks the server, so lending it only on the wire would still land on the login form.
|
||||||
|
func (s *Server) lendKVMCookies(w http.ResponseWriter, r *http.Request, id string) {
|
||||||
|
for name, val := range s.kvmCookies(id) {
|
||||||
|
if _, err := r.Cookie(name); err == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
http.SetCookie(w, &http.Cookie{Name: name, Value: val, Path: "/", MaxAge: 180 * 24 * 3600, Secure: secure(r), SameSite: http.SameSiteLaxMode})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// stripCookie removes named cookies from a request, so Ham Lab's session is never handed to
|
// stripCookie removes named cookies from a request, so Ham Lab's session is never handed to
|
||||||
// a device and the device never sees which KVM the browser is looking at.
|
// a device and the device never sees which KVM the browser is looking at.
|
||||||
func stripCookie(r *http.Request, names ...string) {
|
func stripCookie(r *http.Request, names ...string) {
|
||||||
@@ -157,6 +279,7 @@ func (s *Server) serveKVM(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
http.SetCookie(w, &http.Cookie{Name: kvmCookie, Value: id, Path: "/", HttpOnly: true, Secure: secure(r), SameSite: http.SameSiteLaxMode})
|
http.SetCookie(w, &http.Cookie{Name: kvmCookie, Value: id, Path: "/", HttpOnly: true, Secure: secure(r), SameSite: http.SameSiteLaxMode})
|
||||||
|
s.lendKVMCookies(w, r, id)
|
||||||
p.ServeHTTP(w, r)
|
p.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,20 +338,6 @@ func (s *Server) listKVMs(w http.ResponseWriter, _ *http.Request) {
|
|||||||
jsonOK(w, 200, out)
|
jsonOK(w, 200, out)
|
||||||
}
|
}
|
||||||
|
|
||||||
// kvmForMachine is the KVM attached to a machine, "" being the hub itself.
|
|
||||||
func (s *Server) kvmForMachine(machine string) (config.KVM, bool) {
|
|
||||||
var found config.KVM
|
|
||||||
ok := false
|
|
||||||
s.cfg.View(func(c *config.Config) {
|
|
||||||
for _, k := range c.KVMs {
|
|
||||||
if k.Machine == machine {
|
|
||||||
found, ok = k, true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return found, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Server) addKVM(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) addKVM(w http.ResponseWriter, r *http.Request) {
|
||||||
var body struct{ Name, URL, Machine string }
|
var body struct{ Name, URL, Machine string }
|
||||||
if err := json.NewDecoder(io.LimitReader(r.Body, 4096)).Decode(&body); err != nil {
|
if err := json.NewDecoder(io.LimitReader(r.Body, 4096)).Decode(&body); err != nil {
|
||||||
@@ -305,6 +414,7 @@ func (s *Server) removeKVM(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.forgetKVMProxy(id)
|
s.forgetKVMProxy(id)
|
||||||
|
s.forgetKVMCookies(id)
|
||||||
log.Printf("kvm %s removed by %s", id, clientIP(r))
|
log.Printf("kvm %s removed by %s", id, clientIP(r))
|
||||||
w.WriteHeader(204)
|
w.WriteHeader(204)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,123 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
|
"net/url"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"hamlab/internal/auth"
|
||||||
|
"hamlab/internal/config"
|
||||||
|
"hamlab/internal/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A KVM's own login is a cookie its page sets in one browser. The hub remembers it from the
|
||||||
|
// requests that carry it, lends it to a browser that lacks it — both on the wire to the
|
||||||
|
// device and back to the browser, since the device's page checks for it client-side — and
|
||||||
|
// forgets it when the device says it is gone or the KVM is removed.
|
||||||
|
func TestKVMCookiesAreRememberedAndLent(t *testing.T) {
|
||||||
|
var seen []string // the Cookie header the fake device received, per request
|
||||||
|
device := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
seen = append(seen, r.Header.Get("Cookie"))
|
||||||
|
if r.URL.Path == "/api/auth/login" {
|
||||||
|
// a device that sets its token by header, with its own domain and Secure
|
||||||
|
http.SetCookie(w, &http.Cookie{Name: "nano-kvm-token", Value: "tok-1", Domain: "10.0.0.217", Secure: true})
|
||||||
|
}
|
||||||
|
if r.URL.Path == "/api/auth/logout" {
|
||||||
|
http.SetCookie(w, &http.Cookie{Name: "nano-kvm-token", Value: "", MaxAge: -1})
|
||||||
|
}
|
||||||
|
w.WriteHeader(200)
|
||||||
|
}))
|
||||||
|
defer device.Close()
|
||||||
|
|
||||||
|
dir := t.TempDir()
|
||||||
|
cfg := config.Default(filepath.Join(dir, "config.json"))
|
||||||
|
if err := cfg.Update(func(c *config.Config) {
|
||||||
|
c.KVMs = []config.KVM{{ID: "abc123", Name: "test", URL: device.URL, Machine: ""}}
|
||||||
|
}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
st, err := store.Open(filepath.Join(dir, "state.json"))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s := &Server{cfg: cfg, store: st, sessions: auth.NewSessions(st)}
|
||||||
|
k, _ := s.kvm("abc123")
|
||||||
|
p, err := s.kvmProxy(k)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ask := func(path string, cookies ...*http.Cookie) *httptest.ResponseRecorder {
|
||||||
|
r := httptest.NewRequest("GET", path, nil)
|
||||||
|
r.SetPathValue("id", "abc123")
|
||||||
|
for _, c := range cookies {
|
||||||
|
r.AddCookie(c)
|
||||||
|
}
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
if strings.HasPrefix(path, kvmPrefix) {
|
||||||
|
s.serveKVM(w, r)
|
||||||
|
} else {
|
||||||
|
p.ServeHTTP(w, r)
|
||||||
|
}
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. the device sets its token by header: it reaches the browser without the device's
|
||||||
|
// domain or Secure (plain-http console), and the hub remembers it
|
||||||
|
w := ask("/api/auth/login", &http.Cookie{Name: cookieName, Value: "hamlab-session"})
|
||||||
|
var set *http.Cookie
|
||||||
|
for _, c := range w.Result().Cookies() {
|
||||||
|
if c.Name == "nano-kvm-token" {
|
||||||
|
set = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if set == nil || set.Domain != "" || set.Secure || set.Value != "tok-1" || set.Path != "/" {
|
||||||
|
t.Fatalf("rewritten Set-Cookie = %+v, want nano-kvm-token=tok-1 for / without Domain or Secure", set)
|
||||||
|
}
|
||||||
|
if got := s.kvmCookies("abc123"); got["nano-kvm-token"] != "tok-1" {
|
||||||
|
t.Fatalf("remembered = %v, want the token", got)
|
||||||
|
}
|
||||||
|
if strings.Contains(seen[0], "hamlab_session") {
|
||||||
|
t.Fatalf("the device was handed Ham Lab's session: %q", seen[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. a browser that has the cookie teaches a newer value
|
||||||
|
ask("/api/vm/info", &http.Cookie{Name: "nano-kvm-token", Value: "tok-2"}, &http.Cookie{Name: cookieName, Value: "x"})
|
||||||
|
if got := s.kvmCookies("abc123"); got["nano-kvm-token"] != "tok-2" {
|
||||||
|
t.Fatalf("remembered = %v, want tok-2", got)
|
||||||
|
}
|
||||||
|
if seen[1] != "nano-kvm-token=tok-2" {
|
||||||
|
t.Fatalf("device saw %q, want the browser's own cookie only", seen[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. a browser without it is lent it: on the wire, and back as a Set-Cookie it can read
|
||||||
|
w = ask(kvmPrefix+"abc123/", &http.Cookie{Name: cookieName, Value: "x"})
|
||||||
|
if seen[2] != "nano-kvm-token=tok-2" {
|
||||||
|
t.Fatalf("device saw %q from a browser with no cookie, want the remembered token", seen[2])
|
||||||
|
}
|
||||||
|
lent := false
|
||||||
|
for _, c := range w.Result().Cookies() {
|
||||||
|
if c.Name == "nano-kvm-token" && c.Value == "tok-2" && !c.HttpOnly && c.Path == "/" && c.MaxAge > 0 {
|
||||||
|
lent = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !lent {
|
||||||
|
t.Fatalf("browser was not lent the token: %v", w.Result().Cookies())
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. the device logging out forgets it
|
||||||
|
ask("/api/auth/logout", &http.Cookie{Name: "nano-kvm-token", Value: "tok-2"})
|
||||||
|
if got := s.kvmCookies("abc123"); got != nil {
|
||||||
|
t.Fatalf("after logout remembered = %v, want nothing", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. removing the KVM clears whatever is left
|
||||||
|
s.rememberKVMCookies("abc123", []*http.Cookie{{Name: "nano-kvm-token", Value: "tok-3"}})
|
||||||
|
s.forgetKVMCookies("abc123")
|
||||||
|
if got := s.kvmCookies("abc123"); got != nil {
|
||||||
|
t.Fatalf("after remove remembered = %v, want nothing", got)
|
||||||
|
}
|
||||||
|
_ = url.Parse
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
// Playing a video the browser cannot play as it is: POST /api/files/play starts an HLS
|
||||||
|
// re-encode on this machine (internal/media), GET …/play/{id}/index.m3u8 and the segments
|
||||||
|
// feed the player, GET …/play/{id} is its progress, DELETE ends it. /api/files/play-caps
|
||||||
|
// says which codecs this machine can make and on what. Root mode (?sudo=1) reads the file
|
||||||
|
// and runs ffmpeg as root, through the same sudo the file reads use.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/files"
|
||||||
|
"hamlab/internal/media"
|
||||||
|
)
|
||||||
|
|
||||||
|
const playLimit = 3 // re-encodes at once on one machine; a GPU does two or three, a CPU one
|
||||||
|
|
||||||
|
type playCaps struct {
|
||||||
|
Codecs []playCodec `json:"codecs"`
|
||||||
|
Qualities []media.Quality `json:"qualities"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type playCodec struct {
|
||||||
|
media.Codec
|
||||||
|
Engine string `json:"engine,omitempty"` // "" when nothing here encodes it
|
||||||
|
}
|
||||||
|
|
||||||
|
// playCapsHandler: GET /api/files/play-caps[?sudo=1]
|
||||||
|
func (s *Server) playCapsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
root := r.URL.Query().Get("sudo") == "1"
|
||||||
|
if root && s.fsSudo == nil {
|
||||||
|
jsonErr(w, 403, "root access needs passwordless sudo on the host")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
caps, err := s.thumbs.capsFor(r.Context(), root, false)
|
||||||
|
pc := playCaps{Codecs: []playCodec{}, Qualities: media.Qualities}
|
||||||
|
if err != nil {
|
||||||
|
pc.Error = err.Error()
|
||||||
|
} else if caps.Error != "" {
|
||||||
|
pc.Error = caps.Error
|
||||||
|
}
|
||||||
|
for _, c := range media.Codecs {
|
||||||
|
x := playCodec{Codec: c}
|
||||||
|
if pc.Error == "" {
|
||||||
|
x.Engine = media.EngineFor(caps, c.ID)
|
||||||
|
}
|
||||||
|
pc.Codecs = append(pc.Codecs, x)
|
||||||
|
}
|
||||||
|
jsonOK(w, 200, pc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// playStart: POST /api/files/play[?sudo=1] {path, codec, quality}
|
||||||
|
func (s *Server) playStart(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fsys, ok := s.fsFor(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
root := r.URL.Query().Get("sudo") == "1"
|
||||||
|
var body struct{ Path, Codec, Quality string }
|
||||||
|
if err := json.NewDecoder(io.LimitReader(r.Body, 64<<10)).Decode(&body); err != nil {
|
||||||
|
jsonErr(w, 400, "bad request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p, err := files.Clean(body.Path)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if media.Kind(p) != "video" {
|
||||||
|
jsonErr(w, 415, "not a video")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := media.ValidChoice(body.Codec, body.Quality); err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, err := fsys.Stat(r.Context(), p); err != nil {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
caps, err := s.thumbs.capsFor(r.Context(), root, false)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 503, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if caps.Error != "" {
|
||||||
|
jsonErr(w, 503, caps.Error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ictx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
|
||||||
|
src, err := media.Inspect(ictx, caps, p, root)
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
playErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st, err := s.plays.Open(r.Context(), caps, root, p, body.Codec, body.Quality, src)
|
||||||
|
if err != nil {
|
||||||
|
playErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("files: play %s as %s %s on %s (from %s)", p, st.Codec, st.Quality, st.Engine, clientIP(r))
|
||||||
|
jsonOK(w, 200, map[string]any{"stream": st, "url": "/api/files/play/" + st.ID + "/index.m3u8"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func playErr(w http.ResponseWriter, err error) {
|
||||||
|
var de *media.DecodeError
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &de):
|
||||||
|
jsonErr(w, 422, de.Msg)
|
||||||
|
case strings.Contains(err.Error(), "already being re-encoded"):
|
||||||
|
jsonErr(w, 429, err.Error())
|
||||||
|
case strings.HasPrefix(err.Error(), "unknown ") || strings.HasPrefix(err.Error(), "nothing on this machine encodes"):
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
case errors.Is(err, context.DeadlineExceeded):
|
||||||
|
jsonErr(w, 504, "the file took too long to read")
|
||||||
|
default:
|
||||||
|
jsonErr(w, 500, err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// playStatus: GET /api/files/play/{id}
|
||||||
|
func (s *Server) playStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
st, ok := s.plays.Find(r.PathValue("id"))
|
||||||
|
if !ok {
|
||||||
|
jsonErr(w, 404, "no such stream")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
jsonOK(w, 200, st.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
// playStop: DELETE /api/files/play/{id}, or POST …/close — the form a page can send on its
|
||||||
|
// way out (navigator.sendBeacon), so a phone closing the tab releases the machine at once.
|
||||||
|
func (s *Server) playStop(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.plays.Close(r.PathValue("id"))
|
||||||
|
w.WriteHeader(204)
|
||||||
|
}
|
||||||
|
|
||||||
|
// playFile: GET /api/files/play/{id}/{file} — the playlist, the init segment, a segment.
|
||||||
|
func (s *Server) playFile(w http.ResponseWriter, r *http.Request) {
|
||||||
|
st, ok := s.plays.Find(r.PathValue("id"))
|
||||||
|
if !ok {
|
||||||
|
jsonErr(w, 404, "no such stream")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p, ok := st.File(r.PathValue("file"))
|
||||||
|
if !ok {
|
||||||
|
jsonErr(w, 404, "not a stream file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch filepath.Ext(p) {
|
||||||
|
case ".m3u8":
|
||||||
|
w.Header().Set("Content-Type", "application/vnd.apple.mpegurl")
|
||||||
|
default:
|
||||||
|
w.Header().Set("Content-Type", "video/mp4")
|
||||||
|
}
|
||||||
|
w.Header().Set("Cache-Control", "no-store")
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
http.ServeFile(w, r, p)
|
||||||
|
}
|
||||||
+147
-31
@@ -34,6 +34,8 @@ import (
|
|||||||
"hamlab/internal/cron"
|
"hamlab/internal/cron"
|
||||||
"hamlab/internal/files"
|
"hamlab/internal/files"
|
||||||
"hamlab/internal/hardware"
|
"hamlab/internal/hardware"
|
||||||
|
"hamlab/internal/incus"
|
||||||
|
"hamlab/internal/media"
|
||||||
"hamlab/internal/metrics"
|
"hamlab/internal/metrics"
|
||||||
"hamlab/internal/network"
|
"hamlab/internal/network"
|
||||||
"hamlab/internal/nodes"
|
"hamlab/internal/nodes"
|
||||||
@@ -75,8 +77,12 @@ type Server struct {
|
|||||||
update updateState
|
update updateState
|
||||||
events eventLog
|
events eventLog
|
||||||
kvms kvmProxies
|
kvms kvmProxies
|
||||||
|
virt *incus.Manager
|
||||||
fsLocal files.FS
|
fsLocal files.FS
|
||||||
fsSudo files.FS // nil without passwordless sudo
|
fsSudo files.FS // nil without passwordless sudo
|
||||||
|
thumbs *thumbSvc
|
||||||
|
plays *media.Streamer
|
||||||
|
self string // this binary, for helpers run as tasks
|
||||||
nodes *nodes.Manager
|
nodes *nodes.Manager
|
||||||
fingerprint string
|
fingerprint string
|
||||||
version string
|
version string
|
||||||
@@ -96,7 +102,10 @@ func New(cfg *config.Config, sessions *auth.Sessions, mgr *term.Manager, col *me
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
self = "hamlab"
|
self = "hamlab"
|
||||||
}
|
}
|
||||||
|
s.self = self
|
||||||
s.space = space.New(self, filepath.Join(cfg.DataDir(), "space"))
|
s.space = space.New(self, filepath.Join(cfg.DataDir(), "space"))
|
||||||
|
s.thumbs = newThumbSvc(cfg.DataDir(), self)
|
||||||
|
s.plays = media.NewStreamer(cfg.DataDir(), self, playLimit)
|
||||||
s.hostName() // read it once now so the first request doesn't have to
|
s.hostName() // read it once now so the first request doesn't have to
|
||||||
if u, err := user.Current(); err == nil && u.Username != "" {
|
if u, err := user.Current(); err == nil && u.Username != "" {
|
||||||
s.user = u.Username
|
s.user = u.Username
|
||||||
@@ -104,6 +113,7 @@ func New(cfg *config.Config, sessions *auth.Sessions, mgr *term.Manager, col *me
|
|||||||
if s.user == "" {
|
if s.user == "" {
|
||||||
s.user = "?"
|
s.user = "?"
|
||||||
}
|
}
|
||||||
|
s.virt = incus.New(cfg.DataDir(), s.user, version)
|
||||||
s.migrateUsers()
|
s.migrateUsers()
|
||||||
// The speed test and the outside view. Its history outlives a restart, and its last result
|
// The speed test and the outside view. Its history outlives a restart, and its last result
|
||||||
// is published as sensors, so the line is graphable and alertable like anything else.
|
// is published as sensors, so the line is graphable and alertable like anything else.
|
||||||
@@ -217,20 +227,8 @@ func (s *Server) routes() *http.ServeMux {
|
|||||||
m.HandleFunc("PUT /api/docker/schedules", s.auth(s.dsPut))
|
m.HandleFunc("PUT /api/docker/schedules", s.auth(s.dsPut))
|
||||||
m.HandleFunc("DELETE /api/docker/schedules/{id}", s.auth(s.dsDelete))
|
m.HandleFunc("DELETE /api/docker/schedules/{id}", s.auth(s.dsDelete))
|
||||||
m.HandleFunc("POST /api/docker/schedules/{id}/run", s.auth(s.dsRun))
|
m.HandleFunc("POST /api/docker/schedules/{id}/run", s.auth(s.dsRun))
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/updates", s.auth(s.machineUpdates))
|
// Asked of a node through this hub, every one of these is the node's to answer: the
|
||||||
// The same, asked of a node through this hub: the hub answers for the stacks whose files
|
// generic /api/nodes/{name}/{rest...} proxy above carries them there unchanged.
|
||||||
// it holds (deployed to the node over tcp) and passes everything else to the node.
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/stacks", s.auth(s.stacksList))
|
|
||||||
m.HandleFunc("POST /api/nodes/{node}/docker/stacks", s.auth(s.stackCreate))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/stacks/homes", s.auth(s.stackHomes))
|
|
||||||
m.HandleFunc("POST /api/nodes/{node}/docker/stacks/{name}/delete", s.auth(s.stackDelete))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/stacks/{name}", s.auth(s.stackFile))
|
|
||||||
m.HandleFunc("PUT /api/nodes/{node}/docker/stacks/{name}", s.auth(s.stackSave))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/stacks/{name}/diff", s.auth(s.stackDiff))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/docker/stacks/{name}/updates", s.auth(s.stackUpdates))
|
|
||||||
m.HandleFunc("POST /api/nodes/{node}/docker/stacks/{name}/{action}", s.auth(s.stackAction))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/tasks/{id}", s.auth(s.taskOnHub(s.getTask)))
|
|
||||||
m.HandleFunc("GET /api/nodes/{node}/tasks/{id}/stream", s.auth(s.taskOnHub(s.streamTask)))
|
|
||||||
m.HandleFunc("POST /api/docker/containers/{id}/{action}", s.auth(s.dockerAction))
|
m.HandleFunc("POST /api/docker/containers/{id}/{action}", s.auth(s.dockerAction))
|
||||||
m.HandleFunc("GET /api/docker/containers/{id}/logs", s.auth(s.dockerLogs))
|
m.HandleFunc("GET /api/docker/containers/{id}/logs", s.auth(s.dockerLogs))
|
||||||
m.HandleFunc("GET /api/docker/containers/{id}/logs/tail", s.auth(s.dockerTail))
|
m.HandleFunc("GET /api/docker/containers/{id}/logs/tail", s.auth(s.dockerTail))
|
||||||
@@ -239,6 +237,37 @@ func (s *Server) routes() *http.ServeMux {
|
|||||||
m.HandleFunc("GET /api/docker/containers/{id}/events", s.auth(s.dockerEvents))
|
m.HandleFunc("GET /api/docker/containers/{id}/events", s.auth(s.dockerEvents))
|
||||||
m.HandleFunc("POST /api/docker/containers/{id}/exec", s.auth(s.dockerExec))
|
m.HandleFunc("POST /api/docker/containers/{id}/exec", s.auth(s.dockerExec))
|
||||||
m.HandleFunc("POST /api/docker/containers/{id}/limits", s.auth(s.dockerUpdate))
|
m.HandleFunc("POST /api/docker/containers/{id}/limits", s.auth(s.dockerUpdate))
|
||||||
|
m.HandleFunc("GET /api/virt", s.auth(s.virtOverview))
|
||||||
|
m.HandleFunc("POST /api/virt/install", s.auth(s.virtInstall))
|
||||||
|
m.HandleFunc("GET /api/virt/images/remote", s.auth(s.virtRemoteImages))
|
||||||
|
m.HandleFunc("POST /api/virt/images/download", s.auth(s.virtImageDownload))
|
||||||
|
m.HandleFunc("POST /api/virt/images/import", s.auth(s.virtImageImport))
|
||||||
|
m.HandleFunc("POST /api/virt/images/publish", s.auth(s.virtImagePublish))
|
||||||
|
m.HandleFunc("POST /api/virt/images/delete", s.auth(s.virtImageDelete))
|
||||||
|
m.HandleFunc("POST /api/virt/instances", s.auth(s.virtCreate))
|
||||||
|
m.HandleFunc("GET /api/virt/instances/{name}", s.auth(s.virtInstance))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/rename", s.auth(s.virtRename))
|
||||||
|
m.HandleFunc("PUT /api/virt/instances/{name}/config", s.auth(s.virtConfig))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/snapshots", s.auth(s.virtSnapshotCreate))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/snapshots/{snap}/{action}", s.auth(s.virtSnapshotAction))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/from-iso", s.auth(s.virtFromISO))
|
||||||
|
m.HandleFunc("GET /api/virt/instances/{name}/vga", s.auth(s.virtVGA))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/iso", s.auth(s.virtISOAttach))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/iso/detach", s.auth(s.virtISODetach))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/mounts", s.auth(s.virtMountAdd))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/mounts/remove", s.auth(s.virtMountRemove))
|
||||||
|
m.HandleFunc("POST /api/virt/instances/{name}/{action}", s.auth(s.virtAction))
|
||||||
|
m.HandleFunc("POST /api/virt/isos/fetch", s.auth(s.virtISOFetch))
|
||||||
|
m.HandleFunc("POST /api/virt/isos/import", s.auth(s.virtISOImport))
|
||||||
|
m.HandleFunc("POST /api/virt/isos/delete", s.auth(s.virtISODelete))
|
||||||
|
m.HandleFunc("POST /api/virt/isos/rename", s.auth(s.virtISORename))
|
||||||
|
m.HandleFunc("GET /api/virt/exports", s.auth(s.virtExports))
|
||||||
|
m.HandleFunc("PUT /api/virt/exports", s.auth(s.virtExportsPut))
|
||||||
|
m.HandleFunc("POST /api/virt/exports/run", s.auth(s.virtExportsRun))
|
||||||
|
m.HandleFunc("POST /api/virt/exports/delete", s.auth(s.virtExportDelete))
|
||||||
|
m.HandleFunc("GET /api/virt/sshkeys", s.auth(s.virtSSHKeys))
|
||||||
|
m.HandleFunc("POST /api/virt/pools", s.auth(s.virtPoolCreate))
|
||||||
|
m.HandleFunc("POST /api/virt/pools/delete", s.auth(s.virtPoolDelete))
|
||||||
m.HandleFunc("GET /api/alerts", s.auth(s.alertsGet))
|
m.HandleFunc("GET /api/alerts", s.auth(s.alertsGet))
|
||||||
m.HandleFunc("PUT /api/alerts", s.auth(s.alertsPut))
|
m.HandleFunc("PUT /api/alerts", s.auth(s.alertsPut))
|
||||||
m.HandleFunc("POST /api/alerts/test", s.auth(s.alertsTest))
|
m.HandleFunc("POST /api/alerts/test", s.auth(s.alertsTest))
|
||||||
@@ -277,8 +306,19 @@ func (s *Server) routes() *http.ServeMux {
|
|||||||
m.HandleFunc("POST /api/files/mkdir", s.auth(s.filesMkdir))
|
m.HandleFunc("POST /api/files/mkdir", s.auth(s.filesMkdir))
|
||||||
m.HandleFunc("POST /api/files/touch", s.auth(s.filesTouch))
|
m.HandleFunc("POST /api/files/touch", s.auth(s.filesTouch))
|
||||||
m.HandleFunc("POST /api/files/rename", s.auth(s.filesRename))
|
m.HandleFunc("POST /api/files/rename", s.auth(s.filesRename))
|
||||||
|
m.HandleFunc("POST /api/files/move", s.auth(s.filesMove))
|
||||||
m.HandleFunc("POST /api/files/delete", s.auth(s.filesDelete))
|
m.HandleFunc("POST /api/files/delete", s.auth(s.filesDelete))
|
||||||
m.HandleFunc("POST /api/files/chmod", s.auth(s.filesChmod))
|
m.HandleFunc("POST /api/files/chmod", s.auth(s.filesChmod))
|
||||||
|
m.HandleFunc("GET /api/files/thumb", s.auth(s.filesThumb))
|
||||||
|
m.HandleFunc("GET /api/files/thumbs", s.auth(s.thumbsStatus))
|
||||||
|
m.HandleFunc("POST /api/files/thumbs/install", s.auth(s.thumbsInstall))
|
||||||
|
m.HandleFunc("POST /api/files/thumbs/gpu-access", s.auth(s.thumbsGPUAccess))
|
||||||
|
m.HandleFunc("GET /api/files/play-caps", s.auth(s.playCapsHandler))
|
||||||
|
m.HandleFunc("POST /api/files/play", s.auth(s.playStart))
|
||||||
|
m.HandleFunc("GET /api/files/play/{id}", s.auth(s.playStatus))
|
||||||
|
m.HandleFunc("DELETE /api/files/play/{id}", s.auth(s.playStop))
|
||||||
|
m.HandleFunc("POST /api/files/play/{id}/close", s.auth(s.playStop))
|
||||||
|
m.HandleFunc("GET /api/files/play/{id}/{file}", s.auth(s.playFile))
|
||||||
m.HandleFunc("GET /api/hardware", s.auth(s.hardwareReport))
|
m.HandleFunc("GET /api/hardware", s.auth(s.hardwareReport))
|
||||||
m.HandleFunc("GET /api/network", s.auth(s.networkOverview))
|
m.HandleFunc("GET /api/network", s.auth(s.networkOverview))
|
||||||
m.HandleFunc("GET /api/vpn", s.auth(s.vpnReport))
|
m.HandleFunc("GET /api/vpn", s.auth(s.vpnReport))
|
||||||
@@ -320,6 +360,7 @@ func (s *Server) routes() *http.ServeMux {
|
|||||||
m.HandleFunc("GET /api/sessions", s.auth(s.listSessions))
|
m.HandleFunc("GET /api/sessions", s.auth(s.listSessions))
|
||||||
m.HandleFunc("DELETE /api/sessions/{id}", s.auth(s.revokeSession))
|
m.HandleFunc("DELETE /api/sessions/{id}", s.auth(s.revokeSession))
|
||||||
m.HandleFunc("POST /api/sessions/revoke-others", s.auth(s.revokeOtherSessions))
|
m.HandleFunc("POST /api/sessions/revoke-others", s.auth(s.revokeOtherSessions))
|
||||||
|
m.HandleFunc("POST /api/sessions/revoke-idle", s.auth(s.revokeIdleSessions))
|
||||||
m.HandleFunc("GET /api/prefs/ui", s.auth(s.getUIPrefs))
|
m.HandleFunc("GET /api/prefs/ui", s.auth(s.getUIPrefs))
|
||||||
m.HandleFunc("PUT /api/prefs/ui", s.auth(s.putUIPrefs))
|
m.HandleFunc("PUT /api/prefs/ui", s.auth(s.putUIPrefs))
|
||||||
// Go runtime profiles, behind the login, for chasing memory/CPU (bug 14).
|
// Go runtime profiles, behind the login, for chasing memory/CPU (bug 14).
|
||||||
@@ -927,11 +968,7 @@ func (s *Server) updateAllMachines(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) proxyNode(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) proxyNode(w http.ResponseWriter, r *http.Request) {
|
||||||
node := r.PathValue("node") // routes that also carry a {name} of their own call the machine {node}
|
p, ok := s.nodes.Proxy(r.PathValue("name"))
|
||||||
if node == "" {
|
|
||||||
node = r.PathValue("name")
|
|
||||||
}
|
|
||||||
p, ok := s.nodes.Proxy(node)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
jsonErr(w, 404, "no such machine")
|
jsonErr(w, 404, "no such machine")
|
||||||
return
|
return
|
||||||
@@ -1895,21 +1932,15 @@ func (s *Server) filesRead(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonErr(w, 400, "not a regular file (device, pipe or socket)")
|
jsonErr(w, 400, "not a regular file (device, pipe or socket)")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if in.Broken {
|
||||||
|
jsonErr(w, 404, "a link to nothing: "+in.Target)
|
||||||
|
return
|
||||||
|
}
|
||||||
mx, _ := strconv.ParseInt(r.URL.Query().Get("max"), 10, 64)
|
mx, _ := strconv.ParseInt(r.URL.Query().Get("max"), 10, 64)
|
||||||
if mx > 0 && in.Size > mx {
|
if mx > 0 && in.Size > mx {
|
||||||
jsonErr(w, 413, "file too large")
|
jsonErr(w, 413, "file too large")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
f, err := fsys.Open(r.Context(), p)
|
|
||||||
if err != nil {
|
|
||||||
fsErr(w, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
var body io.Reader = f
|
|
||||||
if mx > 0 { // stat can lie (size 0 on /proc); the cap must hold on the bytes actually sent
|
|
||||||
body = io.LimitReader(f, mx+1)
|
|
||||||
}
|
|
||||||
name := filepath.Base(p)
|
name := filepath.Base(p)
|
||||||
ct := mime.TypeByExtension(filepath.Ext(name))
|
ct := mime.TypeByExtension(filepath.Ext(name))
|
||||||
if ct == "" {
|
if ct == "" {
|
||||||
@@ -1928,7 +1959,28 @@ func (s *Server) filesRead(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Content-Disposition", "inline; filename*=UTF-8''"+url.PathEscape(name))
|
w.Header().Set("Content-Disposition", "inline; filename*=UTF-8''"+url.PathEscape(name))
|
||||||
}
|
}
|
||||||
if in.Size > 0 && mx == 0 {
|
if in.Size > 0 && mx == 0 {
|
||||||
w.Header().Set("Content-Length", strconv.FormatInt(in.Size, 10))
|
// A real file of known size goes through net/http, which speaks Range: a video player
|
||||||
|
// can seek and a download can resume. /proc files say size 0 and take the plain path.
|
||||||
|
rs := files.NewSeeker(r.Context(), fsys, p, in.Size)
|
||||||
|
defer rs.Close()
|
||||||
|
if _, local := fsys.(files.Local); local { // cheap here, and turns "can't open" into a status rather than a cut-off body
|
||||||
|
if err := rs.Check(); err != nil {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
http.ServeContent(w, r, name, time.UnixMilli(in.Mtime), rs)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
f, err := fsys.Open(r.Context(), p)
|
||||||
|
if err != nil {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
var body io.Reader = f
|
||||||
|
if mx > 0 { // stat can lie (size 0 on /proc); the cap must hold on the bytes actually sent
|
||||||
|
body = io.LimitReader(f, mx+1)
|
||||||
}
|
}
|
||||||
_, _ = io.Copy(w, body)
|
_, _ = io.Copy(w, body)
|
||||||
}
|
}
|
||||||
@@ -1942,7 +1994,14 @@ func (s *Server) filesWrite(w http.ResponseWriter, r *http.Request) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := fsys.Write(r.Context(), p, io.LimitReader(r.Body, 8<<20)); err != nil {
|
// Whole in memory first: the write truncates the file, so a body over the cap must be
|
||||||
|
// refused before that, not cut short after it.
|
||||||
|
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 8<<20))
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 413, "too large to save here (8 MiB at most)")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := fsys.Write(r.Context(), p, bytes.NewReader(body)); err != nil {
|
||||||
fsErr(w, err)
|
fsErr(w, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -2029,6 +2088,63 @@ func (s *Server) filesRename(w http.ResponseWriter, r *http.Request) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// filesMove: POST {path, to} — to is the full destination path. A rename when the two
|
||||||
|
// ends share a filesystem (204); otherwise the copy-and-remove runs as a task (202 with the
|
||||||
|
// task), since a folder between datasets can take as long as it takes.
|
||||||
|
func (s *Server) filesMove(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fsys, ok := s.fsFor(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var body struct{ Path, To string }
|
||||||
|
if err := json.NewDecoder(io.LimitReader(r.Body, 64<<10)).Decode(&body); err != nil {
|
||||||
|
jsonErr(w, 400, "bad request")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
from, err := files.Clean(body.Path)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
to, err := files.Clean(body.To)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if from == to {
|
||||||
|
jsonErr(w, 400, "it is already there")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if files.Inside(from, to) {
|
||||||
|
jsonErr(w, 400, "a folder can't be moved into itself")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// The kernel answers EXDEV for two paths on different filesystems before it looks for
|
||||||
|
// the source, so a missing file would otherwise become a task that fails on arrival.
|
||||||
|
if _, err := fsys.Stat(r.Context(), from); err != nil {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = fsys.Rename(r.Context(), from, to)
|
||||||
|
if err == nil {
|
||||||
|
log.Printf("files: move %s -> %s (from %s)", from, to, clientIP(r))
|
||||||
|
w.WriteHeader(204)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !files.IsCrossDevice(err) {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cmd := files.MoveCmd(context.Background(), fsys, s.self, from, to)
|
||||||
|
t, err := s.tasks.Start("files:move", "move "+filepath.Base(from)+" to "+filepath.Dir(to), cmd)
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 500, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("files: move %s -> %s across filesystems as task %s (from %s)", from, to, t.ID, clientIP(r))
|
||||||
|
jsonOK(w, 202, t.Snapshot(false))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) filesDelete(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) filesDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
s.filesOp(w, r, func(fsys files.FS, body map[string]any, p string) error {
|
s.filesOp(w, r, func(fsys files.FS, body map[string]any, p string) error {
|
||||||
rec, _ := body["recursive"].(bool)
|
rec, _ := body["recursive"].(bool)
|
||||||
|
|||||||
+77
-272
@@ -18,11 +18,10 @@ package api
|
|||||||
// machine would become unfindable at once. A remembered mount resolves with no container to
|
// machine would become unfindable at once. A remembered mount resolves with no container to
|
||||||
// run in, which is right: compose runs on the host against the host's copy of the file.
|
// run in, which is right: compose runs on the host against the host's copy of the file.
|
||||||
//
|
//
|
||||||
// A stack can also be deployed *from another machine*: Dockhand on the hub drives a node's
|
// A stack is one machine's affair: its files and the Docker that runs it are on the same
|
||||||
// Docker over tcp, so the node runs the containers and the hub holds the files. The node
|
// box, and that box's Ham Lab is the only thing that touches either. Asked about a node's
|
||||||
// alone can only list such a stack read-only. The hub, asked about one of its nodes, fills
|
// stacks, a hub passes the request to the node's own Ham Lab and relays the answer; it holds
|
||||||
// in what the node couldn't (hubStacks): it resolves the files on itself and runs compose
|
// no files for anyone else and never points compose at another machine's Docker.
|
||||||
// here with DOCKER_HOST pointed at the node.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -35,7 +34,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -60,8 +58,6 @@ type stack struct {
|
|||||||
Files []string `json:"files"` // every file it was deployed from, in order: -f a -f b
|
Files []string `json:"files"` // every file it was deployed from, in order: -f a -f b
|
||||||
HostFiles []string `json:"host_files"` // the same, on the machine that holds them
|
HostFiles []string `json:"host_files"` // the same, on the machine that holds them
|
||||||
Via string `json:"via"` // container compose runs inside; "" = on the host
|
Via string `json:"via"` // container compose runs inside; "" = on the host
|
||||||
Remote string `json:"remote"` // DOCKER_HOST compose is pointed at; "" = this machine's Docker
|
|
||||||
HeldBy string `json:"held_by"` // the machine holding the files, when it isn't the one running them
|
|
||||||
Editable bool `json:"editable"`
|
Editable bool `json:"editable"`
|
||||||
Note string `json:"note,omitempty"`
|
Note string `json:"note,omitempty"`
|
||||||
HasEnv bool `json:"has_env"`
|
HasEnv bool `json:"has_env"`
|
||||||
@@ -282,7 +278,7 @@ func (s *Server) findStacks(ctx context.Context) ([]stack, error) {
|
|||||||
st.resolve(maps)
|
st.resolve(maps)
|
||||||
stacks = append(stacks, *st)
|
stacks = append(stacks, *st)
|
||||||
}
|
}
|
||||||
stacks = append(stacks, scanHomes(ctx, s.homesOf(stacks, true), stacks)...)
|
stacks = append(stacks, scanHomes(ctx, s.homesOf(stacks), stacks)...)
|
||||||
sort.Slice(stacks, func(i, j int) bool { return stacks[i].Name < stacks[j].Name })
|
sort.Slice(stacks, func(i, j int) bool { return stacks[i].Name < stacks[j].Name })
|
||||||
return stacks, nil
|
return stacks, nil
|
||||||
}
|
}
|
||||||
@@ -290,19 +286,23 @@ func (s *Server) findStacks(ctx context.Context) ([]stack, error) {
|
|||||||
// ---- where stacks live ----
|
// ---- where stacks live ----
|
||||||
|
|
||||||
// A home is a directory of stacks, one folder each: where new ones are made, and where ones
|
// A home is a directory of stacks, one folder each: where new ones are made, and where ones
|
||||||
// that aren't deployed (never were, or were taken down) are still found. Homes aren't
|
// that aren't deployed (never were, or were taken down) are still found. The default is
|
||||||
// configured; they are wherever this machine's stacks already are. With none at all, ~/stacks.
|
// Ham Lab's own: <data dir>/stacks, which is where everything this console manages lives.
|
||||||
|
// Any folder this machine already keeps stacks in — two or more deployed from it — is
|
||||||
|
// offered as well, so what was deployed before Ham Lab arrived stays editable and gains
|
||||||
|
// neighbours where it is, but it is never the default.
|
||||||
type stackHome struct {
|
type stackHome struct {
|
||||||
Dir string `json:"dir"` // on the machine that holds the files
|
Dir string `json:"dir"` // on this machine
|
||||||
Inner string `json:"inner"` // the same directory as compose sees it (inside Via); Dir otherwise
|
Inner string `json:"inner"` // the same directory as compose sees it (inside Via); Dir otherwise
|
||||||
Via string `json:"via"` // container compose runs inside
|
Via string `json:"via"` // container compose runs inside
|
||||||
Remote string `json:"remote"` // DOCKER_HOST, when the containers run on another machine
|
|
||||||
HeldBy string `json:"held_by"` // the machine holding the files, when it isn't the one running them
|
|
||||||
Stacks int `json:"stacks"`
|
Stacks int `json:"stacks"`
|
||||||
Default bool `json:"default"`
|
Default bool `json:"default"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) homesOf(stacks []stack, withFallback bool) []stackHome {
|
// stacksDir is the home Ham Lab owns.
|
||||||
|
func (s *Server) stacksDir() string { return filepath.Join(s.cfg.DataDir(), "stacks") }
|
||||||
|
|
||||||
|
func (s *Server) homesOf(stacks []stack) []stackHome {
|
||||||
type tally struct {
|
type tally struct {
|
||||||
home stackHome
|
home stackHome
|
||||||
deployed int
|
deployed int
|
||||||
@@ -318,11 +318,10 @@ func (s *Server) homesOf(stacks []stack, withFallback bool) []stackHome {
|
|||||||
if strings.Count(dir, "/") < 2 { // "/" or "/tmp": not somewhere to make folders in
|
if strings.Count(dir, "/") < 2 { // "/" or "/tmp": not somewhere to make folders in
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
key := dir + "|" + st.Remote
|
t := by[dir]
|
||||||
t := by[key]
|
|
||||||
if t == nil {
|
if t == nil {
|
||||||
t = &tally{home: stackHome{Dir: dir, Remote: st.Remote, HeldBy: st.HeldBy}, byVia: map[string]int{}, inner: map[string]string{}}
|
t = &tally{home: stackHome{Dir: dir}, byVia: map[string]int{}, inner: map[string]string{}}
|
||||||
by[key] = t
|
by[dir] = t
|
||||||
}
|
}
|
||||||
t.home.Stacks++
|
t.home.Stacks++
|
||||||
if len(st.Containers) > 0 {
|
if len(st.Containers) > 0 {
|
||||||
@@ -335,11 +334,13 @@ func (s *Server) homesOf(stacks []stack, withFallback bool) []stackHome {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
// A folder is a home when stacks are kept there as a habit: two or more deployed from it.
|
// A folder is a home when stacks are kept there as a habit: two or more deployed from it.
|
||||||
// One stack on its own (Dockhand's own compose file in ~/apps/dockhand) says nothing about
|
// One stack on its own (an orchestrator's own compose file in its app folder) says nothing
|
||||||
// where the next should go, and would have every compose file near it listed as a stack.
|
// about where the next should go, and would have every compose file near it listed as a
|
||||||
// New stacks are deployed the way most of their neighbours were.
|
// stack — unless it is already in Ham Lab's folder. New stacks are deployed the way most
|
||||||
var homes, lone []stackHome
|
// of their neighbours were.
|
||||||
for _, t := range by {
|
own := s.stacksDir()
|
||||||
|
var homes []stackHome
|
||||||
|
for dir, t := range by {
|
||||||
best, n := "", -1
|
best, n := "", -1
|
||||||
for via, c := range t.byVia {
|
for via, c := range t.byVia {
|
||||||
if c > n || (c == n && via > best) {
|
if c > n || (c == n && via > best) {
|
||||||
@@ -355,29 +356,23 @@ func (s *Server) homesOf(stacks []stack, withFallback bool) []stackHome {
|
|||||||
if t.home.Inner == "" {
|
if t.home.Inner == "" {
|
||||||
t.home.Inner = t.home.Dir
|
t.home.Inner = t.home.Dir
|
||||||
}
|
}
|
||||||
if t.deployed >= 2 {
|
if t.deployed >= 2 || dir == own {
|
||||||
homes = append(homes, t.home)
|
homes = append(homes, t.home)
|
||||||
} else {
|
|
||||||
lone = append(lone, t.home)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(homes) == 0 {
|
|
||||||
homes = lone
|
|
||||||
}
|
|
||||||
sort.Slice(homes, func(i, j int) bool {
|
sort.Slice(homes, func(i, j int) bool {
|
||||||
|
if (homes[i].Dir == own) != (homes[j].Dir == own) {
|
||||||
|
return homes[i].Dir == own
|
||||||
|
}
|
||||||
if homes[i].Stacks != homes[j].Stacks {
|
if homes[i].Stacks != homes[j].Stacks {
|
||||||
return homes[i].Stacks > homes[j].Stacks
|
return homes[i].Stacks > homes[j].Stacks
|
||||||
}
|
}
|
||||||
return homes[i].Dir < homes[j].Dir
|
return homes[i].Dir < homes[j].Dir
|
||||||
})
|
})
|
||||||
if len(homes) == 0 && withFallback {
|
if len(homes) == 0 || homes[0].Dir != own {
|
||||||
if home, err := os.UserHomeDir(); err == nil {
|
homes = append([]stackHome{{Dir: own, Inner: own}}, homes...)
|
||||||
homes = append(homes, stackHome{Dir: filepath.Join(home, "stacks"), Inner: filepath.Join(home, "stacks")})
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if len(homes) > 0 {
|
|
||||||
homes[0].Default = true
|
homes[0].Default = true
|
||||||
}
|
|
||||||
return homes
|
return homes
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -394,6 +389,9 @@ func scanHomes(ctx context.Context, homes []stackHome, known []stack) []stack {
|
|||||||
}
|
}
|
||||||
var out []stack
|
var out []stack
|
||||||
for _, h := range homes {
|
for _, h := range homes {
|
||||||
|
if _, err := os.Stat(h.Dir); errors.Is(err, os.ErrNotExist) {
|
||||||
|
continue // Ham Lab's own home before its first stack: nothing to find, and no sudo to spend finding it
|
||||||
|
}
|
||||||
args := []string{h.Dir, "-mindepth", "2", "-maxdepth", "2", "-type", "f", "("}
|
args := []string{h.Dir, "-mindepth", "2", "-maxdepth", "2", "-type", "f", "("}
|
||||||
for i, n := range composeNames {
|
for i, n := range composeNames {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
@@ -417,7 +415,7 @@ func scanHomes(ctx context.Context, homes []stackHome, known []stack) []stack {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
have[name], have[dir] = true, true
|
have[name], have[dir] = true, true
|
||||||
st := stack{Name: name, Containers: []string{}, Images: []string{}, HostFiles: []string{f}, HostFile: f, Via: h.Via, Remote: h.Remote, HeldBy: h.HeldBy, Editable: true, Note: "not deployed"}
|
st := stack{Name: name, Containers: []string{}, Images: []string{}, HostFiles: []string{f}, HostFile: f, Via: h.Via, Editable: true, Note: "not deployed"}
|
||||||
st.Files = []string{filepath.Join(h.Inner, filepath.Base(dir), filepath.Base(f))}
|
st.Files = []string{filepath.Join(h.Inner, filepath.Base(dir), filepath.Base(f))}
|
||||||
st.File = st.Files[0]
|
st.File = st.Files[0]
|
||||||
st.HasEnv = readable(st.envPath())
|
st.HasEnv = readable(st.envPath())
|
||||||
@@ -436,114 +434,23 @@ func contains(list []string, s string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// hubStacks is a node's stack list with the hub's help: what the node could not find is
|
// stackFor finds the stack a request means.
|
||||||
// looked for here, and if it is here — and the node's Docker answers over tcp, which is how
|
func (s *Server) stackFor(ctx context.Context, r *http.Request) (stack, error) {
|
||||||
// it was deployed in the first place — the stack becomes editable from this hub.
|
all, err := s.findStacks(ctx)
|
||||||
func (s *Server) hubStacks(ctx context.Context, node string) ([]stack, error) {
|
|
||||||
code, b, err := s.nodes.Do(ctx, node, "GET", "/api/docker/stacks", nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return stack{}, err
|
||||||
}
|
|
||||||
var res struct{ Stacks []stack }
|
|
||||||
if code != 200 || json.Unmarshal(b, &res) != nil {
|
|
||||||
return nil, fmt.Errorf("%s answered %d", node, code)
|
|
||||||
}
|
|
||||||
remote, maps := "", []mountMap(nil)
|
|
||||||
for i := range res.Stacks {
|
|
||||||
st := &res.Stacks[i]
|
|
||||||
if st.Editable || len(st.Files) == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if remote == "" {
|
|
||||||
remote, maps = s.nodeDocker(ctx, node), mountsFor(ctx, s.store)
|
|
||||||
}
|
|
||||||
if remote == "-" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
note := st.Note
|
|
||||||
if st.resolve(maps); !st.Editable {
|
|
||||||
st.Note = note
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
st.Remote, st.HeldBy = remote, s.hostName()
|
|
||||||
}
|
|
||||||
var held []stack
|
|
||||||
for _, st := range res.Stacks {
|
|
||||||
if st.HeldBy != "" {
|
|
||||||
held = append(held, st)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res.Stacks = append(res.Stacks, scanHomes(ctx, s.homesOf(held, false), res.Stacks)...)
|
|
||||||
sort.Slice(res.Stacks, func(i, j int) bool { return res.Stacks[i].Name < res.Stacks[j].Name })
|
|
||||||
return res.Stacks, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
nodeDockerMu sync.Mutex
|
|
||||||
nodeDockerOK = map[string]string{} // node → "tcp://host:2375", or "-" when it doesn't answer
|
|
||||||
)
|
|
||||||
|
|
||||||
// nodeDocker is the node's Docker endpoint as Dockhand-style deployments use it, if it answers.
|
|
||||||
func (s *Server) nodeDocker(ctx context.Context, node string) string {
|
|
||||||
nodeDockerMu.Lock()
|
|
||||||
defer nodeDockerMu.Unlock()
|
|
||||||
if v, ok := nodeDockerOK[node]; ok && v != "-" {
|
|
||||||
return v
|
|
||||||
}
|
|
||||||
ep := "-"
|
|
||||||
for _, n := range s.nodes.List() {
|
|
||||||
if n.Name != node {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if u, err := url.Parse(n.URL); err == nil && u.Hostname() != "" {
|
|
||||||
cand := "tcp://" + u.Hostname() + ":2375"
|
|
||||||
c, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
||||||
if exec.CommandContext(c, dockerTool(), "-H", cand, "version", "--format", "{{.Server.Version}}").Run() == nil {
|
|
||||||
ep = cand
|
|
||||||
}
|
|
||||||
cancel()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nodeDockerOK[node] = ep
|
|
||||||
return ep
|
|
||||||
}
|
|
||||||
|
|
||||||
// stackFor finds the stack a request means: on this machine, or (on a hub route) one of a
|
|
||||||
// node's stacks that this hub holds the files for. handled is false when the request should
|
|
||||||
// simply be passed on to the node.
|
|
||||||
func (s *Server) stackFor(ctx context.Context, r *http.Request) (st stack, handled bool, err error) {
|
|
||||||
var all []stack
|
|
||||||
if node := r.PathValue("node"); node != "" {
|
|
||||||
all, err = s.hubStacks(ctx, node)
|
|
||||||
} else {
|
|
||||||
all, err = s.findStacks(ctx)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return stack{}, true, err
|
|
||||||
}
|
}
|
||||||
for _, x := range all {
|
for _, x := range all {
|
||||||
if x.Name == r.PathValue("name") {
|
if x.Name == r.PathValue("name") {
|
||||||
if r.PathValue("node") != "" && x.Remote == "" {
|
return x, nil
|
||||||
return x, false, nil // the node can serve this one itself
|
|
||||||
}
|
|
||||||
return x, true, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stack{}, true, errors.New("no such stack")
|
return stack{}, errors.New("no such stack")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) stacksList(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stacksList(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if node := r.PathValue("node"); node != "" {
|
|
||||||
stacks, err := s.hubStacks(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
s.proxyNode(w, r) // let the node's own answer (or failure) through
|
|
||||||
return
|
|
||||||
}
|
|
||||||
jsonOK(w, 200, map[string]any{"stacks": stacks})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if dockerTool() == "" { // no Docker is an empty answer, not a failure
|
if dockerTool() == "" { // no Docker is an empty answer, not a failure
|
||||||
jsonOK(w, 200, map[string]any{"stacks": []stack{}})
|
jsonOK(w, 200, map[string]any{"stacks": []stack{}})
|
||||||
return
|
return
|
||||||
@@ -587,11 +494,7 @@ func target(r *http.Request, st stack) (host string, index int, ok bool) {
|
|||||||
func (s *Server) stackFile(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackFile(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil || !st.Editable {
|
if err != nil || !st.Editable {
|
||||||
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
@@ -610,8 +513,8 @@ func (s *Server) stackFile(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonOK(w, 200, map[string]any{"stack": st, "text": string(b), "sum": sumOf(b), "index": i})
|
jsonOK(w, 200, map[string]any{"stack": st, "text": string(b), "sum": sumOf(b), "index": i})
|
||||||
}
|
}
|
||||||
|
|
||||||
// composeCmd is `docker compose …` for this stack, run where its files make sense and
|
// composeCmd is `docker compose …` for this stack, run where its files make sense. files are
|
||||||
// pointed at the Docker that runs its containers. files are given as compose will see them.
|
// given as compose will see them.
|
||||||
func composeCmd(ctx context.Context, st stack, files []string, args ...string) *exec.Cmd {
|
func composeCmd(ctx context.Context, st stack, files []string, args ...string) *exec.Cmd {
|
||||||
return compose.Cmd(ctx, specOf(st, files), args...)
|
return compose.Cmd(ctx, specOf(st, files), args...)
|
||||||
}
|
}
|
||||||
@@ -619,7 +522,7 @@ func composeCmd(ctx context.Context, st stack, files []string, args ...string) *
|
|||||||
// specOf is a stack as the compose runner needs it — and as a scheduled update stores it,
|
// specOf is a stack as the compose runner needs it — and as a scheduled update stores it,
|
||||||
// so the cron job and the console invoke compose identically.
|
// so the cron job and the console invoke compose identically.
|
||||||
func specOf(st stack, files []string) compose.Spec {
|
func specOf(st stack, files []string) compose.Spec {
|
||||||
return compose.Spec{Project: st.Name, Files: files, Via: st.Via, Remote: st.Remote, Tool: dockerTool()}
|
return compose.Spec{Project: st.Name, Files: files, Via: st.Via, Tool: dockerTool()}
|
||||||
}
|
}
|
||||||
|
|
||||||
// seenFiles is the stack's file list as compose runs it, with file i replaced by a candidate.
|
// seenFiles is the stack's file list as compose runs it, with file i replaced by a candidate.
|
||||||
@@ -647,11 +550,7 @@ func (s *Server) writeAsOwner(ctx context.Context, path, text string) error {
|
|||||||
func (s *Server) stackSave(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackSave(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 60*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil || !st.Editable {
|
if err != nil || !st.Editable {
|
||||||
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
@@ -723,11 +622,7 @@ func (s *Server) stackSave(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) stackDiff(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackDiff(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil || !st.Editable {
|
if err != nil || !st.Editable {
|
||||||
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
@@ -777,11 +672,7 @@ func maskEnv(text string) string {
|
|||||||
func (s *Server) stackAction(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackAction(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil || !st.Editable {
|
if err != nil || !st.Editable {
|
||||||
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
@@ -794,17 +685,10 @@ func (s *Server) stackAction(w http.ResponseWriter, r *http.Request) {
|
|||||||
flags := priv.Quote(append([]string{"-p", st.Name}, fl...))
|
flags := priv.Quote(append([]string{"-p", st.Name}, fl...))
|
||||||
base := func(args string) string {
|
base := func(args string) string {
|
||||||
if st.Via != "" {
|
if st.Via != "" {
|
||||||
env := ""
|
// The CLI inside the orchestrator's container is its own `docker`, whatever this host runs.
|
||||||
if st.Remote != "" {
|
return fmt.Sprintf("%s exec %s docker compose %s %s", dockerTool(), priv.Quote([]string{st.Via}), flags, args)
|
||||||
env = "-e " + priv.Quote([]string{"DOCKER_HOST=" + st.Remote}) + " "
|
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s exec %s%s docker compose %s %s", dockerTool(), env, priv.Quote([]string{st.Via}), flags, args)
|
return fmt.Sprintf("cd %s && %s compose %s %s", priv.Quote([]string{filepath.Dir(files[0])}), dockerTool(), flags, args)
|
||||||
}
|
|
||||||
env := ""
|
|
||||||
if st.Remote != "" { // through env(1): a quoted NAME=value in front of a command is a command name to sh, not an assignment
|
|
||||||
env = "env " + priv.Quote([]string{"DOCKER_HOST=" + st.Remote}) + " "
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("cd %s && %s%s compose %s %s", priv.Quote([]string{filepath.Dir(files[0])}), env, dockerTool(), flags, args)
|
|
||||||
}
|
}
|
||||||
// After a deploy that worked, keep a copy of exactly what was deployed: the next diff is against it.
|
// After a deploy that worked, keep a copy of exactly what was deployed: the next diff is against it.
|
||||||
remember := ""
|
remember := ""
|
||||||
@@ -860,7 +744,7 @@ func (s *Server) stackAction(w http.ResponseWriter, r *http.Request) {
|
|||||||
_ = exec.CommandContext(ctx, dockerTool(), "exec", via, "pkill", "-TERM", "-f", pattern).Run()
|
_ = exec.CommandContext(ctx, dockerTool(), "exec", via, "pkill", "-TERM", "-f", pattern).Run()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
stackUpdatesForget(r.PathValue("node") + "/" + st.Name)
|
stackUpdatesForget(st.Name)
|
||||||
log.Printf("stack %s: %s started as task %s (from %s)", st.Name, r.PathValue("action"), t.ID, clientIP(r))
|
log.Printf("stack %s: %s started as task %s (from %s)", st.Name, r.PathValue("action"), t.ID, clientIP(r))
|
||||||
jsonOK(w, 202, t.Snapshot(false))
|
jsonOK(w, 202, t.Snapshot(false))
|
||||||
}
|
}
|
||||||
@@ -885,20 +769,13 @@ var (
|
|||||||
|
|
||||||
func stackUpdatesForget(key string) { updMu.Lock(); delete(updCache, key); updMu.Unlock() }
|
func stackUpdatesForget(key string) { updMu.Lock(); delete(updCache, key); updMu.Unlock() }
|
||||||
|
|
||||||
// checkImages asks the registry whether each image is still the one we hold. remote is a
|
// checkImages asks the registry whether each image is still the one we hold. Each image is
|
||||||
// DOCKER_HOST for a machine whose images live elsewhere, "" for this one. Each image is one
|
// one registry request, which Docker Hub counts — so this is only ever called when asked for.
|
||||||
// registry request, which Docker Hub counts — so this is only ever called when asked for.
|
func checkImages(ctx context.Context, images []string) []imageCheck {
|
||||||
func checkImages(ctx context.Context, images []string, remote string) []imageCheck {
|
|
||||||
local := func(args ...string) ([]byte, error) { // the Docker that holds the images
|
|
||||||
if remote != "" {
|
|
||||||
args = append([]string{"-H", remote}, args...)
|
|
||||||
}
|
|
||||||
return dockerRun(ctx, args...)
|
|
||||||
}
|
|
||||||
res := []imageCheck{}
|
res := []imageCheck{}
|
||||||
for _, img := range images {
|
for _, img := range images {
|
||||||
ic := imageCheck{Image: img, State: "unknown"}
|
ic := imageCheck{Image: img, State: "unknown"}
|
||||||
out, err := local("image", "inspect", "-f", "{{json .RepoDigests}}", img)
|
out, err := dockerRun(ctx, "image", "inspect", "-f", "{{json .RepoDigests}}", img)
|
||||||
var have []string
|
var have []string
|
||||||
if err != nil || json.Unmarshal(bytes.TrimSpace(out), &have) != nil || len(have) == 0 {
|
if err != nil || json.Unmarshal(bytes.TrimSpace(out), &have) != nil || len(have) == 0 {
|
||||||
ic.Detail = "built or loaded here, not pulled from a registry"
|
ic.Detail = "built or loaded here, not pulled from a registry"
|
||||||
@@ -930,31 +807,28 @@ func checkImages(ctx context.Context, images []string, remote string) []imageChe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// machineUpdates checks every image on this machine in one pass, so the container list can
|
// machineUpdates checks every image on this machine in one pass, so the container list can
|
||||||
// be coloured and the fleet can be asked "what is behind?" without walking stack by stack.
|
// be coloured and "what is behind?" answered without walking stack by stack.
|
||||||
func (s *Server) machineUpdates(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) machineUpdates(w http.ResponseWriter, r *http.Request) {
|
||||||
node := r.PathValue("node")
|
if dockerTool() == "" { // no Docker is an empty answer, not a failure
|
||||||
|
jsonOK(w, 200, map[string]any{"images": []imageCheck{}, "checked": 0, "stacks": map[string][]string{}})
|
||||||
|
return
|
||||||
|
}
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
stacks, err := s.findStacks(ctx)
|
stacks, err := s.findStacks(ctx)
|
||||||
if node != "" {
|
|
||||||
stacks, err = s.hubStacks(ctx, node)
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 500, err.Error())
|
jsonErr(w, 500, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// one entry per image, however many stacks use it
|
// one entry per image, however many stacks use it
|
||||||
type want struct{ remote string }
|
|
||||||
imgs := map[string]want{}
|
|
||||||
byImage := map[string][]string{} // image -> stacks
|
byImage := map[string][]string{} // image -> stacks
|
||||||
for _, st := range stacks {
|
for _, st := range stacks {
|
||||||
for _, img := range st.Images {
|
for _, img := range st.Images {
|
||||||
imgs[img] = want{remote: st.Remote}
|
|
||||||
byImage[img] = append(byImage[img], st.Name)
|
byImage[img] = append(byImage[img], st.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
refresh := r.URL.Query().Get("refresh") == "1"
|
refresh := r.URL.Query().Get("refresh") == "1"
|
||||||
key := "machine:" + node
|
key := "machine"
|
||||||
updMu.Lock()
|
updMu.Lock()
|
||||||
c, ok := updCache[key]
|
c, ok := updCache[key]
|
||||||
updMu.Unlock()
|
updMu.Unlock()
|
||||||
@@ -967,15 +841,11 @@ func (s *Server) machineUpdates(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
var list []string
|
var list []string
|
||||||
remote := ""
|
for img := range byImage {
|
||||||
for img, wnt := range imgs {
|
|
||||||
list = append(list, img)
|
list = append(list, img)
|
||||||
if wnt.remote != "" {
|
|
||||||
remote = wnt.remote
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
sort.Strings(list)
|
sort.Strings(list)
|
||||||
res := checkImages(ctx, list, remote)
|
res := checkImages(ctx, list)
|
||||||
updMu.Lock()
|
updMu.Lock()
|
||||||
updCache[key] = updAnswer{time.Now(), res}
|
updCache[key] = updAnswer{time.Now(), res}
|
||||||
updMu.Unlock()
|
updMu.Unlock()
|
||||||
@@ -988,16 +858,12 @@ func (s *Server) machineUpdates(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) stackUpdates(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackUpdates(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 90*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 90*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 404, err.Error())
|
jsonErr(w, 404, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
key := r.PathValue("node") + "/" + st.Name
|
key := st.Name
|
||||||
updMu.Lock()
|
updMu.Lock()
|
||||||
c, ok := updCache[key]
|
c, ok := updCache[key]
|
||||||
updMu.Unlock()
|
updMu.Unlock()
|
||||||
@@ -1010,84 +876,38 @@ func (s *Server) stackUpdates(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonOK(w, 200, map[string]any{"images": []imageCheck{}, "checked": 0})
|
jsonOK(w, 200, map[string]any{"images": []imageCheck{}, "checked": 0})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res := checkImages(ctx, st.Images, st.Remote)
|
res := checkImages(ctx, st.Images)
|
||||||
updMu.Lock()
|
updMu.Lock()
|
||||||
updCache[key] = updAnswer{time.Now(), res}
|
updCache[key] = updAnswer{time.Now(), res}
|
||||||
updMu.Unlock()
|
updMu.Unlock()
|
||||||
jsonOK(w, 200, map[string]any{"images": res, "checked": time.Now().UnixMilli()})
|
jsonOK(w, 200, map[string]any{"images": res, "checked": time.Now().UnixMilli()})
|
||||||
}
|
}
|
||||||
|
|
||||||
// taskOnHub serves a task this hub is running for one of its nodes' stacks; anything else
|
|
||||||
// under /api/nodes/<node>/tasks belongs to the node.
|
|
||||||
func (s *Server) taskOnHub(local http.HandlerFunc) http.HandlerFunc {
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if _, ok := s.tasks.Get(r.PathValue("id")); ok {
|
|
||||||
local(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---- making and unmaking stacks ----
|
// ---- making and unmaking stacks ----
|
||||||
|
|
||||||
var stackNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,62}$`)
|
var stackNameRe = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,62}$`)
|
||||||
|
|
||||||
// homesFor is where a new stack could go, seen from this request: this machine's homes, or —
|
// homesFor is where a new stack could go on this machine.
|
||||||
// on a hub route — the node's own homes plus the ones this hub holds for it.
|
func (s *Server) homesFor(ctx context.Context) ([]stackHome, error) {
|
||||||
func (s *Server) homesFor(ctx context.Context, r *http.Request) ([]stackHome, error) {
|
|
||||||
node := r.PathValue("node")
|
|
||||||
if node == "" {
|
|
||||||
stacks, err := s.findStacks(ctx)
|
stacks, err := s.findStacks(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return s.homesOf(stacks, true), nil
|
return s.homesOf(stacks), nil
|
||||||
}
|
|
||||||
stacks, err := s.hubStacks(ctx, node)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var held []stack
|
|
||||||
for _, st := range stacks {
|
|
||||||
if st.HeldBy != "" {
|
|
||||||
held = append(held, st)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
homes := s.homesOf(held, false)
|
|
||||||
if code, b, err := s.nodes.Do(ctx, node, "GET", "/api/docker/stacks/homes", nil, nil); err == nil && code == 200 {
|
|
||||||
var res struct{ Homes []stackHome }
|
|
||||||
if json.Unmarshal(b, &res) == nil {
|
|
||||||
for _, h := range res.Homes {
|
|
||||||
if len(homes) > 0 && h.Stacks == 0 {
|
|
||||||
continue // the node's empty ~/stacks is only worth offering when there is nothing else
|
|
||||||
}
|
|
||||||
h.Default = false
|
|
||||||
homes = append(homes, h)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(homes) > 0 {
|
|
||||||
homes[0].Default = true
|
|
||||||
}
|
|
||||||
return homes, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) stackHomes(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) stackHomes(w http.ResponseWriter, r *http.Request) {
|
||||||
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
ctx, cancel := context.WithTimeout(r.Context(), 40*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if dockerTool() == "" && r.PathValue("node") == "" {
|
if dockerTool() == "" {
|
||||||
jsonOK(w, 200, map[string]any{"homes": []stackHome{}})
|
jsonOK(w, 200, map[string]any{"homes": []stackHome{}})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
homes, err := s.homesFor(ctx, r)
|
homes, err := s.homesFor(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 500, err.Error())
|
jsonErr(w, 500, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if homes == nil { // a machine with no Docker and nothing held for it: an empty list, never null
|
|
||||||
homes = []stackHome{}
|
|
||||||
}
|
|
||||||
jsonOK(w, 200, map[string]any{"homes": homes})
|
jsonOK(w, 200, map[string]any{"homes": homes})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1114,7 +934,7 @@ func (s *Server) stackCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonErr(w, 400, "the compose file can't be empty")
|
jsonErr(w, 400, "the compose file can't be empty")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
homes, err := s.homesFor(ctx, r)
|
homes, err := s.homesFor(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
jsonErr(w, 500, err.Error())
|
jsonErr(w, 500, err.Error())
|
||||||
return
|
return
|
||||||
@@ -1129,17 +949,7 @@ func (s *Server) stackCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
jsonErr(w, 400, "that isn't one of the places this machine keeps stacks")
|
jsonErr(w, 400, "that isn't one of the places this machine keeps stacks")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if r.PathValue("node") != "" && home.HeldBy == "" { // the node's own folder: the node makes it
|
all, _ := s.findStacks(ctx)
|
||||||
r.Body, r.ContentLength = io.NopCloser(bytes.NewReader(raw)), int64(len(raw))
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
var all []stack
|
|
||||||
if node := r.PathValue("node"); node != "" {
|
|
||||||
all, _ = s.hubStacks(ctx, node)
|
|
||||||
} else {
|
|
||||||
all, _ = s.findStacks(ctx)
|
|
||||||
}
|
|
||||||
for _, st := range all {
|
for _, st := range all {
|
||||||
if st.Name == name {
|
if st.Name == name {
|
||||||
jsonErr(w, 409, "there is already a stack called "+name)
|
jsonErr(w, 409, "there is already a stack called "+name)
|
||||||
@@ -1201,7 +1011,7 @@ func (s *Server) stackCreate(w http.ResponseWriter, r *http.Request) {
|
|||||||
_ = sudo("chmod", "600", filepath.Join(dir, ".env")) // it is where the secrets go
|
_ = sudo("chmod", "600", filepath.Join(dir, ".env")) // it is where the secrets go
|
||||||
}
|
}
|
||||||
st := stack{Name: name, Containers: []string{}, Images: []string{}, HostFiles: []string{host}, HostFile: host, Files: []string{filepath.Join(home.Inner, name, "compose.yaml")},
|
st := stack{Name: name, Containers: []string{}, Images: []string{}, HostFiles: []string{host}, HostFile: host, Files: []string{filepath.Join(home.Inner, name, "compose.yaml")},
|
||||||
Via: home.Via, Remote: home.Remote, HeldBy: home.HeldBy, Editable: true, Note: "not deployed", HasEnv: strings.TrimSpace(body.Env) != ""}
|
Via: home.Via, Editable: true, Note: "not deployed", HasEnv: strings.TrimSpace(body.Env) != ""}
|
||||||
st.File = st.Files[0]
|
st.File = st.Files[0]
|
||||||
if msg, err := composeCmd(ctx, st, seenFiles(st, -1, ""), "config", "-q").CombinedOutput(); err != nil {
|
if msg, err := composeCmd(ctx, st, seenFiles(st, -1, ""), "config", "-q").CombinedOutput(); err != nil {
|
||||||
undo()
|
undo()
|
||||||
@@ -1220,12 +1030,7 @@ func (s *Server) stackDelete(w http.ResponseWriter, r *http.Request) {
|
|||||||
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
|
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096))
|
raw, _ := io.ReadAll(io.LimitReader(r.Body, 4096))
|
||||||
st, handled, err := s.stackFor(ctx, r)
|
st, err := s.stackFor(ctx, r)
|
||||||
if !handled {
|
|
||||||
r.Body, r.ContentLength = io.NopCloser(bytes.NewReader(raw)), int64(len(raw))
|
|
||||||
s.proxyNode(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil || !st.Editable {
|
if err != nil || !st.Editable {
|
||||||
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
jsonErr(w, 404, "that stack's compose file isn't anywhere this console can reach")
|
||||||
return
|
return
|
||||||
@@ -1263,7 +1068,7 @@ func (s *Server) stackDelete(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ = run("rmdir", dir) // only if nothing else was in it
|
_ = run("rmdir", dir) // only if nothing else was in it
|
||||||
stackUpdatesForget(r.PathValue("node") + "/" + st.Name)
|
stackUpdatesForget(st.Name)
|
||||||
log.Printf("stack %s: taken down and %d file(s) moved to %s (from %s)", st.Name, moved, trash, clientIP(r))
|
log.Printf("stack %s: taken down and %d file(s) moved to %s (from %s)", st.Name, moved, trash, clientIP(r))
|
||||||
jsonOK(w, 200, map[string]any{"trash": trash, "moved": moved})
|
jsonOK(w, 200, map[string]any{"trash": trash, "moved": moved})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"hamlab/internal/compose"
|
"hamlab/internal/compose"
|
||||||
|
"hamlab/internal/config"
|
||||||
"hamlab/internal/store"
|
"hamlab/internal/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -119,3 +120,37 @@ func TestRepointSurvivesAMissingContainer(t *testing.T) {
|
|||||||
t.Errorf("unresolvable spec was changed: %+v", got)
|
t.Errorf("unresolvable spec was changed: %+v", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Where new stacks go: Ham Lab's own <data dir>/stacks is always offered, first and as the
|
||||||
|
// default, whether or not anything is in it yet. A folder this machine already keeps stacks
|
||||||
|
// in — two or more deployed from it — is offered too, but never ahead of Ham Lab's own.
|
||||||
|
func TestHomesDefaultToDataDir(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
cfg := config.Default(filepath.Join(root, "data", "config.json"))
|
||||||
|
s := &Server{cfg: cfg}
|
||||||
|
own := filepath.Join(root, "data", "stacks")
|
||||||
|
legacy := filepath.Join(root, "home", "stacks", "box")
|
||||||
|
deployed := func(dir, name string) stack {
|
||||||
|
f := filepath.Join(dir, name, "compose.yaml")
|
||||||
|
return stack{Name: name, Containers: []string{name + "-1"}, Files: []string{f}, File: f, HostFiles: []string{f}, HostFile: f, Editable: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing deployed anywhere: one home, Ham Lab's own.
|
||||||
|
homes := s.homesOf(nil)
|
||||||
|
if len(homes) != 1 || homes[0].Dir != own || !homes[0].Default {
|
||||||
|
t.Fatalf("empty machine: homes = %+v, want just %s as default", homes, own)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two stacks deployed from a pre-existing folder: it is offered, after Ham Lab's own.
|
||||||
|
homes = s.homesOf([]stack{deployed(legacy, "a"), deployed(legacy, "b")})
|
||||||
|
if len(homes) != 2 || homes[0].Dir != own || !homes[0].Default || homes[1].Dir != legacy || homes[1].Default || homes[1].Stacks != 2 {
|
||||||
|
t.Fatalf("legacy folder: homes = %+v, want [%s (default), %s]", homes, own, legacy)
|
||||||
|
}
|
||||||
|
|
||||||
|
// One lone stack elsewhere says nothing about where the next should go; one in Ham Lab's
|
||||||
|
// own folder is already home.
|
||||||
|
homes = s.homesOf([]stack{deployed(filepath.Join(root, "home", "apps"), "orchestrator"), deployed(own, "c")})
|
||||||
|
if len(homes) != 1 || homes[0].Dir != own || homes[0].Stacks != 1 || !homes[0].Default {
|
||||||
|
t.Fatalf("lone stacks: homes = %+v, want just %s", homes, own)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/incus"
|
||||||
"hamlab/internal/priv"
|
"hamlab/internal/priv"
|
||||||
"hamlab/internal/sensors"
|
"hamlab/internal/sensors"
|
||||||
"hamlab/internal/storage"
|
"hamlab/internal/storage"
|
||||||
@@ -54,6 +55,7 @@ func newStatusSensors(s *Server) *statusSensors {
|
|||||||
p := &statusSensors{s: s, by: map[string][]sensors.Sensor{}, hist: map[string]*sensors.Series{}, acc: map[string]*bucket{}}
|
p := &statusSensors{s: s, by: map[string][]sensors.Sensor{}, hist: map[string]*sensors.Series{}, acc: map[string]*bucket{}}
|
||||||
go p.loop("services", servicesEvery, p.services)
|
go p.loop("services", servicesEvery, p.services)
|
||||||
go p.loop("containers", containersEvery, p.containers)
|
go p.loop("containers", containersEvery, p.containers)
|
||||||
|
go p.loop("instances", containersEvery, p.instances)
|
||||||
go p.loop("updates", updatesEvery, p.updates)
|
go p.loop("updates", updatesEvery, p.updates)
|
||||||
go p.loop("zfs", zfsEvery, p.zfs)
|
go p.loop("zfs", zfsEvery, p.zfs)
|
||||||
go p.loop("smart", smartEvery, p.smart)
|
go p.loop("smart", smartEvery, p.smart)
|
||||||
@@ -320,6 +322,43 @@ type cpuMark struct {
|
|||||||
t int64
|
t int64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// instances are Incus's containers and VMs. "Down" means it should be up and is not: an
|
||||||
|
// instance marked to start with the machine that is stopped, or one in Error — a stopped
|
||||||
|
// instance with no such mark was stopped on purpose, as with Docker containers.
|
||||||
|
func (p *statusSensors) instances(ctx context.Context, t int64) []sensors.Sensor {
|
||||||
|
if !incus.Installed() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
list, err := p.s.virt.InstancesFor(ctx, "sensors")
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var res []sensors.Sensor
|
||||||
|
var running, total float64
|
||||||
|
for _, in := range list {
|
||||||
|
total++
|
||||||
|
id := "instance." + sensors.Seg(in.Name)
|
||||||
|
l := map[string]string{"instance": in.Name, "type": in.Type, "image": in.Image}
|
||||||
|
up := in.Status == "Running"
|
||||||
|
if up {
|
||||||
|
running++
|
||||||
|
}
|
||||||
|
down := in.Status == "Error" || (!up && in.Config["boot.autostart"] == "true")
|
||||||
|
res = append(res,
|
||||||
|
sensors.Sensor{ID: id + ".running", Name: in.Name, Kind: "state", Value: up, T: t, Labels: l},
|
||||||
|
sensors.Sensor{ID: id + ".down", Name: in.Name + " down", Kind: "state", Value: down, T: t, Labels: l},
|
||||||
|
sensors.Sensor{ID: id + ".state", Name: in.Name + " state", Kind: "state", Value: strings.ToLower(in.Status), T: t, Labels: l})
|
||||||
|
if up {
|
||||||
|
res = append(res,
|
||||||
|
sensors.Sensor{ID: id + ".cpu", Name: in.Name + " CPU", Kind: "usage", Unit: "%", Value: float64(int(in.CPUPct*10+0.5)) / 10, T: t, Min: sensors.F(0), Labels: l},
|
||||||
|
sensors.Sensor{ID: id + ".mem", Name: in.Name + " memory", Kind: "bytes", Unit: "B", Value: float64(in.MemUsage), T: t, Labels: l})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return append(res,
|
||||||
|
sensors.Sensor{ID: "instances.total", Name: "Instances", Kind: "count", Value: total, T: t},
|
||||||
|
sensors.Sensor{ID: "instances.running", Name: "Running instances", Kind: "count", Value: running, T: t})
|
||||||
|
}
|
||||||
|
|
||||||
func (p *statusSensors) updates(ctx context.Context, t int64) []sensors.Sensor {
|
func (p *statusSensors) updates(ctx context.Context, t int64) []sensors.Sensor {
|
||||||
st := p.s.updates.Status(ctx)
|
st := p.s.updates.Status(ctx)
|
||||||
if st.Manager == "" {
|
if st.Manager == "" {
|
||||||
|
|||||||
@@ -0,0 +1,418 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
// Previews for the Files tab: GET /api/files/thumb hands back one small JPEG of an image or
|
||||||
|
// a video, made by internal/thumbs and kept in <data dir>/thumbs.db. The work is bounded
|
||||||
|
// two ways — a handful of ffmpegs at once, and one per file however many rows ask for it —
|
||||||
|
// because a folder of a thousand photos asks for every row the moment previews go on.
|
||||||
|
//
|
||||||
|
// Root mode (?sudo=1) makes the preview through the same root helper the file reads use,
|
||||||
|
// so a file only root can read gets a preview too, and the probe runs as root as well: root
|
||||||
|
// opens every render node, whatever groups the daemon's user has.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/media"
|
||||||
|
"hamlab/internal/priv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const thumbSize = 256
|
||||||
|
const thumbTimeout = 30 * time.Second
|
||||||
|
const capsTTL = 10 * time.Minute // how long a probe is believed; a refresh after an install or a group change does not wait
|
||||||
|
|
||||||
|
type thumbSvc struct {
|
||||||
|
cache *media.Cache
|
||||||
|
self string
|
||||||
|
sem chan struct{}
|
||||||
|
mu sync.Mutex
|
||||||
|
caps map[bool]*capsEntry // keyed by root
|
||||||
|
calls map[string]*thumbCall
|
||||||
|
}
|
||||||
|
|
||||||
|
type capsEntry struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
caps media.Caps
|
||||||
|
err error
|
||||||
|
at time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type thumbCall struct {
|
||||||
|
done chan struct{}
|
||||||
|
res media.Result
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func newThumbSvc(dataDir, self string) *thumbSvc {
|
||||||
|
n := runtime.NumCPU() / 2 // each ffmpeg is itself threaded; previews must not take the whole machine
|
||||||
|
if n < 2 {
|
||||||
|
n = 2
|
||||||
|
}
|
||||||
|
if n > 4 {
|
||||||
|
n = 4
|
||||||
|
}
|
||||||
|
return &thumbSvc{cache: media.NewCache(filepath.Join(dataDir, "thumbs.db")), self: self, sem: make(chan struct{}, n), caps: map[bool]*capsEntry{}, calls: map[string]*thumbCall{}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// capsFor is what previews can be made with, probed once and kept for a while.
|
||||||
|
func (t *thumbSvc) capsFor(ctx context.Context, root, refresh bool) (media.Caps, error) {
|
||||||
|
t.mu.Lock()
|
||||||
|
e := t.caps[root]
|
||||||
|
if e == nil {
|
||||||
|
e = &capsEntry{}
|
||||||
|
t.caps[root] = e
|
||||||
|
}
|
||||||
|
t.mu.Unlock()
|
||||||
|
e.mu.Lock()
|
||||||
|
defer e.mu.Unlock()
|
||||||
|
if !refresh && !e.at.IsZero() && time.Since(e.at) < capsTTL {
|
||||||
|
return e.caps, e.err
|
||||||
|
}
|
||||||
|
if root {
|
||||||
|
e.caps, e.err = media.ProbeRoot(ctx, t.self)
|
||||||
|
} else {
|
||||||
|
e.caps, e.err = media.Probe(ctx), nil
|
||||||
|
}
|
||||||
|
e.at = time.Now()
|
||||||
|
mode := "as " + map[bool]string{false: "the daemon", true: "root"}[root]
|
||||||
|
switch {
|
||||||
|
case e.err != nil:
|
||||||
|
log.Printf("thumbs: probe %s failed: %v", mode, e.err)
|
||||||
|
case e.caps.Error != "":
|
||||||
|
log.Printf("thumbs: %s: %s", mode, e.caps.Error)
|
||||||
|
default:
|
||||||
|
log.Printf("thumbs: %s: ffmpeg %s, %s", mode, e.caps.Version, describeCaps(e.caps))
|
||||||
|
}
|
||||||
|
return e.caps, e.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// describeCaps is the one line that says how previews get made here.
|
||||||
|
func describeCaps(c media.Caps) string {
|
||||||
|
if len(c.GPUs) == 0 {
|
||||||
|
return "CPU only"
|
||||||
|
}
|
||||||
|
parts := make([]string, 0, len(c.GPUs))
|
||||||
|
for _, g := range c.GPUs {
|
||||||
|
name := g.Name
|
||||||
|
if name == "" {
|
||||||
|
name = g.Device
|
||||||
|
}
|
||||||
|
p := name + " (" + g.API
|
||||||
|
if len(g.Codecs) > 0 {
|
||||||
|
p += ": " + strings.Join(g.Codecs, ", ")
|
||||||
|
}
|
||||||
|
parts = append(parts, p+")")
|
||||||
|
}
|
||||||
|
return "GPU " + strings.Join(parts, ", ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// make produces a preview, sharing the work when the same file is asked for twice at once.
|
||||||
|
func (t *thumbSvc) make(ctx context.Context, root bool, key, path string) (media.Result, error) {
|
||||||
|
t.mu.Lock()
|
||||||
|
if c, ok := t.calls[key]; ok {
|
||||||
|
t.mu.Unlock()
|
||||||
|
select {
|
||||||
|
case <-c.done:
|
||||||
|
return c.res, c.err
|
||||||
|
case <-ctx.Done():
|
||||||
|
return media.Result{}, ctx.Err()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c := &thumbCall{done: make(chan struct{})}
|
||||||
|
t.calls[key] = c
|
||||||
|
t.mu.Unlock()
|
||||||
|
defer func() {
|
||||||
|
t.mu.Lock()
|
||||||
|
delete(t.calls, key)
|
||||||
|
t.mu.Unlock()
|
||||||
|
close(c.done)
|
||||||
|
}()
|
||||||
|
// The work itself runs on a context of its own: the first requester going away must not
|
||||||
|
// cancel a preview three other rows are waiting for.
|
||||||
|
wctx, cancel := context.WithTimeout(context.Background(), thumbTimeout+5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
select {
|
||||||
|
case t.sem <- struct{}{}:
|
||||||
|
defer func() { <-t.sem }()
|
||||||
|
case <-ctx.Done():
|
||||||
|
c.err = ctx.Err()
|
||||||
|
return c.res, c.err
|
||||||
|
}
|
||||||
|
caps, err := t.capsFor(wctx, root, false)
|
||||||
|
if err != nil {
|
||||||
|
c.err = err
|
||||||
|
return c.res, c.err
|
||||||
|
}
|
||||||
|
o := media.Opts{Size: thumbSize, Timeout: thumbTimeout}
|
||||||
|
if root {
|
||||||
|
c.res, c.err = media.MakeRoot(wctx, t.self, caps, path, o)
|
||||||
|
} else {
|
||||||
|
c.res, c.err = media.Make(wctx, caps, path, o)
|
||||||
|
}
|
||||||
|
return c.res, c.err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- handlers ----
|
||||||
|
|
||||||
|
// filesThumb: GET /api/files/thumb?path=…&v=<mtime>-<size>[&sudo=1]. v is the client's idea of
|
||||||
|
// the file, from its listing; it makes a cache hit free of a stat, and a changed file a new URL.
|
||||||
|
func (s *Server) filesThumb(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fsys, ok := s.fsFor(w, r)
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
p, ok := cleanQuery(w, r, "path")
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if media.Kind(p) == "" {
|
||||||
|
jsonErr(w, 415, "no preview for this kind of file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
root := r.URL.Query().Get("sudo") == "1"
|
||||||
|
if mt, sz, ok := parseV(r.URL.Query().Get("v")); ok {
|
||||||
|
key := media.Key(p, sz, mt)
|
||||||
|
if r.Header.Get("If-None-Match") == `"`+key+`"` {
|
||||||
|
w.WriteHeader(304)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if s.serveCachedThumb(w, key) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
in, err := fsys.Stat(r.Context(), p)
|
||||||
|
if err != nil {
|
||||||
|
fsErr(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if in.Type == "dir" || in.Type == "other" || in.Broken {
|
||||||
|
jsonErr(w, 400, "not a regular file")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key := media.Key(p, in.Size, in.Mtime)
|
||||||
|
if s.serveCachedThumb(w, key) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res, err := s.thumbs.make(r.Context(), root, key, p)
|
||||||
|
if err != nil {
|
||||||
|
var de *media.DecodeError
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &de):
|
||||||
|
if err := s.thumbs.cache.Put(key, media.Entry{Path: p, Failure: de.Msg}); err != nil {
|
||||||
|
log.Printf("thumbs: cache: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("thumbs: %s: %s", p, de.Msg)
|
||||||
|
jsonErr(w, 422, de.Msg)
|
||||||
|
case errors.Is(err, context.DeadlineExceeded):
|
||||||
|
log.Printf("thumbs: %s: timed out", p)
|
||||||
|
jsonErr(w, 504, "preview timed out")
|
||||||
|
case errors.Is(err, context.Canceled):
|
||||||
|
jsonErr(w, 499, "cancelled")
|
||||||
|
default:
|
||||||
|
jsonErr(w, 503, err.Error())
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := s.thumbs.cache.Put(key, media.Entry{Path: p, Engine: res.Engine, Codec: res.Codec, Width: res.Width, Height: res.Height, JPEG: res.JPEG}); err != nil {
|
||||||
|
log.Printf("thumbs: cache: %v", err)
|
||||||
|
}
|
||||||
|
writeThumb(w, key, res.JPEG, res.Engine)
|
||||||
|
}
|
||||||
|
|
||||||
|
// serveCachedThumb answers from the cache when it can. A cache that cannot be read is a
|
||||||
|
// miss: the preview is made again rather than refused.
|
||||||
|
func (s *Server) serveCachedThumb(w http.ResponseWriter, key string) bool {
|
||||||
|
e, found, err := s.thumbs.cache.Get(key)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("thumbs: cache: %v", err)
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case !found:
|
||||||
|
return false
|
||||||
|
case e.Failure != "":
|
||||||
|
jsonErr(w, 422, e.Failure)
|
||||||
|
default:
|
||||||
|
writeThumb(w, key, e.JPEG, "cache ("+e.Engine+")")
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeThumb(w http.ResponseWriter, key string, jpeg []byte, engine string) {
|
||||||
|
w.Header().Set("Content-Type", "image/jpeg")
|
||||||
|
w.Header().Set("Content-Length", strconv.Itoa(len(jpeg)))
|
||||||
|
w.Header().Set("Cache-Control", "private, max-age=604800")
|
||||||
|
w.Header().Set("ETag", `"`+key+`"`)
|
||||||
|
w.Header().Set("X-Hamlab-Thumb", engine)
|
||||||
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
_, _ = w.Write(jpeg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseV(v string) (mtime, size int64, ok bool) {
|
||||||
|
a, b, found := strings.Cut(v, "-")
|
||||||
|
if !found {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
mtime, err1 := strconv.ParseInt(a, 10, 64)
|
||||||
|
size, err2 := strconv.ParseInt(b, 10, 64)
|
||||||
|
return mtime, size, err1 == nil && err2 == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type thumbStatus struct {
|
||||||
|
OK bool `json:"ok"`
|
||||||
|
Engine string `json:"engine"` // gpu | cpu | ""
|
||||||
|
Summary string `json:"summary"`
|
||||||
|
FFmpeg string `json:"ffmpeg,omitempty"`
|
||||||
|
GPUs []media.GPU `json:"gpus"`
|
||||||
|
Unusable []media.GPU `json:"unusable,omitempty"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
Install *thumbInstall `json:"install,omitempty"` // ffmpeg is missing and this can put it on
|
||||||
|
GPUFix bool `json:"gpu_fix,omitempty"` // a render node is there but closed to the daemon's user, and this can open it
|
||||||
|
User string `json:"user"`
|
||||||
|
Cache thumbCache `json:"cache"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type thumbCache struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Entries int64 `json:"entries"`
|
||||||
|
Bytes int64 `json:"bytes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type thumbInstall struct {
|
||||||
|
PM string `json:"pm"`
|
||||||
|
Package string `json:"package"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// thumbsStatus: GET /api/files/thumbs[?sudo=1][&refresh=1] — how previews get made on this machine.
|
||||||
|
func (s *Server) thumbsStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
root := r.URL.Query().Get("sudo") == "1"
|
||||||
|
if root && s.fsSudo == nil {
|
||||||
|
jsonErr(w, 403, "root access needs passwordless sudo on the host")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
caps, err := s.thumbs.capsFor(r.Context(), root, r.URL.Query().Get("refresh") == "1")
|
||||||
|
st := thumbStatus{GPUs: []media.GPU{}, User: s.user, Cache: thumbCache{Path: s.thumbs.cache.Path}}
|
||||||
|
st.Cache.Entries, st.Cache.Bytes = s.thumbs.cache.Stats()
|
||||||
|
if root {
|
||||||
|
st.User = "root"
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
st.Error = err.Error()
|
||||||
|
jsonOK(w, 200, st)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st.FFmpeg, st.GPUs, st.Unusable, st.Error = caps.Version, caps.GPUs, caps.Unusable, caps.Error
|
||||||
|
if st.GPUs == nil {
|
||||||
|
st.GPUs = []media.GPU{}
|
||||||
|
}
|
||||||
|
st.OK = caps.Error == ""
|
||||||
|
switch {
|
||||||
|
case !st.OK:
|
||||||
|
if pkg := ffmpegPackage(s.updates.PM()); pkg != "" && priv.Available() {
|
||||||
|
st.Install = &thumbInstall{PM: s.updates.PM(), Package: pkg}
|
||||||
|
}
|
||||||
|
st.Summary = st.Error
|
||||||
|
case len(caps.GPUs) > 0:
|
||||||
|
st.Engine = "gpu"
|
||||||
|
st.Summary = describeCaps(caps) + " · images on the CPU"
|
||||||
|
default:
|
||||||
|
st.Engine = "cpu"
|
||||||
|
st.Summary = "CPU only"
|
||||||
|
}
|
||||||
|
if !root {
|
||||||
|
for _, g := range caps.Unusable {
|
||||||
|
if g.Permission && gpuFixPossible() {
|
||||||
|
st.GPUFix = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jsonOK(w, 200, st)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ffmpegPackage is what to install for ffmpeg on a package manager. Fedora ships it as
|
||||||
|
// ffmpeg-free; the others under its own name.
|
||||||
|
func ffmpegPackage(pm string) string {
|
||||||
|
switch pm {
|
||||||
|
case "":
|
||||||
|
return ""
|
||||||
|
case "dnf":
|
||||||
|
if b, _ := os.ReadFile("/etc/os-release"); strings.Contains(string(b), "ID=fedora") {
|
||||||
|
return "ffmpeg-free"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "ffmpeg"
|
||||||
|
}
|
||||||
|
|
||||||
|
const hamlabUnit = "/etc/systemd/system/hamlab.service"
|
||||||
|
|
||||||
|
// gpuFixPossible: the daemon is an ordinary user under systemd, with root to hand.
|
||||||
|
func gpuFixPossible() bool {
|
||||||
|
if os.Geteuid() == 0 || !priv.Available() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, err := os.Stat(hamlabUnit)
|
||||||
|
return err == nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// thumbsInstall: POST /api/files/thumbs/install — put ffmpeg on with the machine's package manager.
|
||||||
|
func (s *Server) thumbsInstall(w http.ResponseWriter, r *http.Request) {
|
||||||
|
pkg := ffmpegPackage(s.updates.PM())
|
||||||
|
if pkg == "" {
|
||||||
|
jsonErr(w, 400, "no supported package manager on this machine")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
t, err := s.updates.Install([]string{pkg})
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("thumbs: installing %s as task %s (from %s)", pkg, t.ID, clientIP(r))
|
||||||
|
jsonOK(w, 202, t.Snapshot(false))
|
||||||
|
}
|
||||||
|
|
||||||
|
// gpuAccessScript gives hamlab.service the groups that own the render nodes, by a drop-in,
|
||||||
|
// then restarts it: a running process cannot gain a group.
|
||||||
|
const gpuAccessScript = `set -e
|
||||||
|
unit=` + hamlabUnit + `
|
||||||
|
[ -f "$unit" ] || { echo "no $unit: this machine does not run Ham Lab under systemd"; exit 1; }
|
||||||
|
groups=""
|
||||||
|
for g in render video; do
|
||||||
|
if getent group "$g" >/dev/null 2>&1; then groups="$groups $g"; fi
|
||||||
|
done
|
||||||
|
[ -n "$groups" ] || { echo "neither a render nor a video group exists here"; exit 1; }
|
||||||
|
mkdir -p "$unit.d"
|
||||||
|
printf '[Service]\nSupplementaryGroups=%s\n' "${groups# }" > "$unit.d/gpu.conf"
|
||||||
|
echo "hamlab.service: supplementary groups${groups} ($unit.d/gpu.conf)"
|
||||||
|
systemctl daemon-reload
|
||||||
|
echo 'Ham Lab restarts in 3 seconds to pick up its new groups; the page reconnects by itself.'
|
||||||
|
systemd-run --quiet --on-active=3 --unit=hamlab-gpu-restart systemctl restart hamlab.service
|
||||||
|
`
|
||||||
|
|
||||||
|
// thumbsGPUAccess: POST /api/files/thumbs/gpu-access — open the render nodes to the daemon.
|
||||||
|
func (s *Server) thumbsGPUAccess(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if !gpuFixPossible() {
|
||||||
|
jsonErr(w, 400, "nothing to do: Ham Lab is root, has no root, or is not under systemd here")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if t, busy := s.tasks.RunningOf("thumbs:gpu"); busy { // a second run would only collide with the first's restart timer
|
||||||
|
jsonErr(w, 409, "already being done (task "+t.ID+")")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
full := priv.Wrap("sh", "-c", gpuAccessScript)
|
||||||
|
t, err := s.tasks.Start("thumbs:gpu", "give Ham Lab access to the GPU", exec.Command(full[0], full[1:]...))
|
||||||
|
if err != nil {
|
||||||
|
jsonErr(w, 400, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Printf("thumbs: GPU access drop-in as task %s, restart follows (from %s)", t.ID, clientIP(r))
|
||||||
|
jsonOK(w, 202, t.Snapshot(false))
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -264,6 +265,29 @@ func (s *Server) revokeSession(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.WriteHeader(204)
|
w.WriteHeader(204)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// revokeIdleSessions drops the caller's sessions that have not been used for ?hours (24 by
|
||||||
|
// default): the scripts and headless browsers that signed in once and never came back,
|
||||||
|
// without touching the phone that was used this morning.
|
||||||
|
func (s *Server) revokeIdleSessions(w http.ResponseWriter, r *http.Request) {
|
||||||
|
a := actorOf(r)
|
||||||
|
if a.User == "" {
|
||||||
|
jsonErr(w, 403, "only a signed-in user may do that")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hours := 24
|
||||||
|
if h := r.URL.Query().Get("hours"); h != "" {
|
||||||
|
n, err := strconv.Atoi(h)
|
||||||
|
if err != nil || n < 1 || n > 24*365 {
|
||||||
|
jsonErr(w, 400, "hours is a whole number from 1 to 8760")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
hours = n
|
||||||
|
}
|
||||||
|
n := s.sessions.RevokeIdle(a.User, a.token, time.Duration(hours)*time.Hour)
|
||||||
|
log.Printf("%d idle session(s) of %q revoked (idle > %dh, by %s)", n, a.User, hours, clientIP(r))
|
||||||
|
jsonOK(w, 200, map[string]int{"revoked": n})
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) revokeOtherSessions(w http.ResponseWriter, r *http.Request) {
|
func (s *Server) revokeOtherSessions(w http.ResponseWriter, r *http.Request) {
|
||||||
a := actorOf(r)
|
a := actorOf(r)
|
||||||
if a.User == "" {
|
if a.User == "" {
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
+9
-1
@@ -265,7 +265,15 @@ func (s *Server) zfsSnapshotDelete(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := s.storage.DestroySnapshot(r.Context(), body.Name, body.Recursive); err != nil {
|
if err := s.storage.DestroySnapshot(r.Context(), body.Name, body.Recursive); err != nil {
|
||||||
jsonErr(w, 500, err.Error())
|
msg := err.Error()
|
||||||
|
switch {
|
||||||
|
case msg == "bad snapshot name":
|
||||||
|
jsonErr(w, 400, msg)
|
||||||
|
case strings.Contains(msg, "does not exist"):
|
||||||
|
jsonErr(w, 404, msg)
|
||||||
|
default:
|
||||||
|
jsonErr(w, 500, msg)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("zfs snapshot %s destroyed by %s%s", body.Name, clientIP(r), map[bool]string{true: " (recursive)", false: ""}[body.Recursive])
|
log.Printf("zfs snapshot %s destroyed by %s%s", body.Name, clientIP(r), map[bool]string{true: " (recursive)", false: ""}[body.Recursive])
|
||||||
|
|||||||
+42
-4
@@ -28,6 +28,10 @@ const (
|
|||||||
argonKeyLen = 32
|
argonKeyLen = 32
|
||||||
|
|
||||||
SessionTTL = 30 * 24 * time.Hour
|
SessionTTL = 30 * 24 * time.Hour
|
||||||
|
// SessionIdle is how long a session may go unused before it is dropped, whatever its
|
||||||
|
// expiry says. Every script, curl and headless browser that signs in leaves a session;
|
||||||
|
// a phone that is used keeps its own alive by being used.
|
||||||
|
SessionIdle = 7 * 24 * time.Hour
|
||||||
)
|
)
|
||||||
|
|
||||||
func HashPassword(pw string) (string, error) {
|
func HashPassword(pw string) (string, error) {
|
||||||
@@ -189,12 +193,26 @@ func NewSessions(st *store.Store) *Sessions {
|
|||||||
func pruneExpired(state *store.State) {
|
func pruneExpired(state *store.State) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for tok, ses := range state.Sessions {
|
for tok, ses := range state.Sessions {
|
||||||
if ses.Expires.Before(now) {
|
if !live(ses, now) {
|
||||||
delete(state.Sessions, tok)
|
delete(state.Sessions, tok)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// live is whether a session still counts: unexpired, and used within SessionIdle. A
|
||||||
|
// session from before visits were recorded is judged by its creation, or failing that by
|
||||||
|
// its expiry alone.
|
||||||
|
func live(ses store.Session, now time.Time) bool {
|
||||||
|
if !ses.Expires.After(now) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
seen := ses.LastSeen
|
||||||
|
if seen.IsZero() {
|
||||||
|
seen = ses.Created
|
||||||
|
}
|
||||||
|
return seen.IsZero() || now.Sub(seen) <= SessionIdle
|
||||||
|
}
|
||||||
|
|
||||||
func randomHex(n int) (string, error) {
|
func randomHex(n int) (string, error) {
|
||||||
b := make([]byte, n)
|
b := make([]byte, n)
|
||||||
if _, err := rand.Read(b); err != nil {
|
if _, err := rand.Read(b); err != nil {
|
||||||
@@ -234,7 +252,7 @@ func (s *Sessions) Lookup(tok, ip string) (store.Session, bool) {
|
|||||||
ok, touch := false, false
|
ok, touch := false, false
|
||||||
s.st.View(func(state *store.State) {
|
s.st.View(func(state *store.State) {
|
||||||
found, have := state.Sessions[tok]
|
found, have := state.Sessions[tok]
|
||||||
if have && found.Expires.After(time.Now()) {
|
if have && live(found, time.Now()) {
|
||||||
ses, ok = found, true
|
ses, ok = found, true
|
||||||
touch = time.Until(found.Expires) < SessionTTL-time.Hour || time.Since(found.LastSeen) > time.Hour
|
touch = time.Until(found.Expires) < SessionTTL-time.Hour || time.Since(found.LastSeen) > time.Hour
|
||||||
}
|
}
|
||||||
@@ -264,7 +282,7 @@ func (s *Sessions) List(user string) []store.Session {
|
|||||||
s.st.View(func(state *store.State) {
|
s.st.View(func(state *store.State) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, ses := range state.Sessions {
|
for _, ses := range state.Sessions {
|
||||||
if ses.User == user && ses.Expires.After(now) {
|
if ses.User == user && live(ses, now) {
|
||||||
out = append(out, ses)
|
out = append(out, ses)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,7 +297,7 @@ func (s *Sessions) Count() map[string]int {
|
|||||||
s.st.View(func(state *store.State) {
|
s.st.View(func(state *store.State) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
for _, ses := range state.Sessions {
|
for _, ses := range state.Sessions {
|
||||||
if ses.Expires.After(now) {
|
if live(ses, now) {
|
||||||
n[ses.User]++
|
n[ses.User]++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -315,6 +333,26 @@ func (s *Sessions) RevokeOthers(user, keep string) int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RevokeIdle ends every session of user not seen for longer than idle, except the one
|
||||||
|
// holding keep. Returns how many.
|
||||||
|
func (s *Sessions) RevokeIdle(user, keep string, idle time.Duration) int {
|
||||||
|
n := 0
|
||||||
|
now := time.Now()
|
||||||
|
_ = s.st.Update(func(state *store.State) {
|
||||||
|
for tok, ses := range state.Sessions {
|
||||||
|
seen := ses.LastSeen
|
||||||
|
if seen.IsZero() {
|
||||||
|
seen = ses.Created
|
||||||
|
}
|
||||||
|
if ses.User == user && tok != keep && now.Sub(seen) > idle {
|
||||||
|
delete(state.Sessions, tok)
|
||||||
|
n++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
// RevokeUser ends every session of user (the account is going away).
|
// RevokeUser ends every session of user (the account is going away).
|
||||||
func (s *Sessions) RevokeUser(user string) { s.RevokeOthers(user, "") }
|
func (s *Sessions) RevokeUser(user string) { s.RevokeOthers(user, "") }
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
// Package compose runs docker compose the way a stack was deployed — which is not always
|
// Package compose runs docker compose the way a stack was deployed — which is not always
|
||||||
// simply "on this host". A stack deployed by an orchestrator running inside its own
|
// simply "on this host". A stack deployed by an orchestrator running inside its own
|
||||||
// container records paths in that container's filesystem, so compose has to run in there;
|
// container records paths in that container's filesystem, so compose has to run in there.
|
||||||
// a stack deployed to another machine over tcp needs DOCKER_HOST pointed at it. Both the
|
// Both the console and the scheduled updates it writes must invoke it identically, or a
|
||||||
// console and the scheduled updates it writes must invoke it identically, or a redeploy
|
// redeploy from one would not match the other's labels. Compose always drives this
|
||||||
// from one would not match the other's labels.
|
// machine's own Docker: a stack's files and its containers are on the same box.
|
||||||
package compose
|
package compose
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
@@ -18,7 +17,6 @@ type Spec struct {
|
|||||||
Project string // -p
|
Project string // -p
|
||||||
Files []string // -f, in order
|
Files []string // -f, in order
|
||||||
Via string // a container to run compose inside; "" is this host
|
Via string // a container to run compose inside; "" is this host
|
||||||
Remote string // a DOCKER_HOST; "" is this machine's Docker
|
|
||||||
Tool string // "docker" or "podman"
|
Tool string // "docker" or "podman"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -33,18 +31,12 @@ func Cmd(ctx context.Context, s Spec, args ...string) *exec.Cmd {
|
|||||||
base = append(base, "-f", f)
|
base = append(base, "-f", f)
|
||||||
}
|
}
|
||||||
if s.Via != "" {
|
if s.Via != "" {
|
||||||
pre := []string{"exec"}
|
// The CLI inside the orchestrator's container is its own `docker`, whatever this host runs.
|
||||||
if s.Remote != "" {
|
return exec.CommandContext(ctx, tool, append(append([]string{"exec", s.Via, "docker"}, base...), args...)...)
|
||||||
pre = append(pre, "-e", "DOCKER_HOST="+s.Remote)
|
|
||||||
}
|
|
||||||
return exec.CommandContext(ctx, tool, append(append(append(pre, s.Via, "docker"), base...), args...)...)
|
|
||||||
}
|
}
|
||||||
cmd := exec.CommandContext(ctx, tool, append(base, args...)...)
|
cmd := exec.CommandContext(ctx, tool, append(base, args...)...)
|
||||||
if len(s.Files) > 0 {
|
if len(s.Files) > 0 {
|
||||||
cmd.Dir = filepath.Dir(s.Files[0])
|
cmd.Dir = filepath.Dir(s.Files[0])
|
||||||
}
|
}
|
||||||
if s.Remote != "" {
|
|
||||||
cmd.Env = append(os.Environ(), "DOCKER_HOST="+s.Remote)
|
|
||||||
}
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
+73
-5
@@ -15,6 +15,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/signal"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
@@ -23,6 +24,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/media"
|
||||||
"hamlab/internal/priv"
|
"hamlab/internal/priv"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +39,7 @@ type Entry struct {
|
|||||||
Group string `json:"group"`
|
Group string `json:"group"`
|
||||||
Target string `json:"target,omitempty"` // link target
|
Target string `json:"target,omitempty"` // link target
|
||||||
Broken bool `json:"broken,omitempty"` // dangling link
|
Broken bool `json:"broken,omitempty"` // dangling link
|
||||||
|
Preview string `json:"preview,omitempty"` // "image" | "video": the Files tab can show a thumbnail of it
|
||||||
}
|
}
|
||||||
|
|
||||||
type Listing struct {
|
type Listing struct {
|
||||||
@@ -56,10 +59,12 @@ type FS interface {
|
|||||||
List(ctx context.Context, path string) (Listing, error)
|
List(ctx context.Context, path string) (Listing, error)
|
||||||
Stat(ctx context.Context, path string) (Info, error)
|
Stat(ctx context.Context, path string) (Info, error)
|
||||||
Open(ctx context.Context, path string) (io.ReadCloser, error)
|
Open(ctx context.Context, path string) (io.ReadCloser, error)
|
||||||
|
OpenAt(ctx context.Context, path string, offset int64) (io.ReadCloser, error) // Open from a byte offset: a range request, a player seeking
|
||||||
Write(ctx context.Context, path string, r io.Reader) error
|
Write(ctx context.Context, path string, r io.Reader) error
|
||||||
Mkdir(ctx context.Context, path string) error
|
Mkdir(ctx context.Context, path string) error
|
||||||
Touch(ctx context.Context, path string) error
|
Touch(ctx context.Context, path string) error
|
||||||
Rename(ctx context.Context, from, to string) error
|
Rename(ctx context.Context, from, to string) error
|
||||||
|
Move(ctx context.Context, from, to string, progress io.Writer) error // Rename, or across filesystems a copy and a removal; long, so callers run it as a task
|
||||||
Delete(ctx context.Context, path string, recursive bool) error
|
Delete(ctx context.Context, path string, recursive bool) error
|
||||||
Chmod(ctx context.Context, path string, perm os.FileMode) error
|
Chmod(ctx context.Context, path string, perm os.FileMode) error
|
||||||
}
|
}
|
||||||
@@ -137,7 +142,7 @@ func entryOf(dir, name string, lst os.FileInfo) Entry {
|
|||||||
} else if st.IsDir() {
|
} else if st.IsDir() {
|
||||||
e.Type = "dir" // keep dir semantics for navigation; Target says it's a link
|
e.Type = "dir" // keep dir semantics for navigation; Target says it's a link
|
||||||
} else {
|
} else {
|
||||||
e.Size = st.Size()
|
e.Size, e.Mtime = st.Size(), st.ModTime().UnixMilli() // the file's, not the link's
|
||||||
}
|
}
|
||||||
case lst.IsDir():
|
case lst.IsDir():
|
||||||
e.Type = "dir"
|
e.Type = "dir"
|
||||||
@@ -146,6 +151,9 @@ func entryOf(dir, name string, lst os.FileInfo) Entry {
|
|||||||
default:
|
default:
|
||||||
e.Type = "other"
|
e.Type = "other"
|
||||||
}
|
}
|
||||||
|
if e.Type == "file" || (e.Type == "link" && !e.Broken) {
|
||||||
|
e.Preview = media.Kind(name)
|
||||||
|
}
|
||||||
return e
|
return e
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,6 +232,20 @@ func (Local) Open(_ context.Context, path string) (io.ReadCloser, error) {
|
|||||||
return os.Open(path)
|
return os.Open(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l Local) OpenAt(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
||||||
|
f, err := l.Open(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if offset > 0 {
|
||||||
|
if _, err := f.(*os.File).Seek(offset, io.SeekStart); err != nil {
|
||||||
|
f.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return f, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Write replaces the file's contents, keeping its mode; new files get 0644 (umask).
|
// Write replaces the file's contents, keeping its mode; new files get 0644 (umask).
|
||||||
func (Local) Write(_ context.Context, path string, r io.Reader) error {
|
func (Local) Write(_ context.Context, path string, r io.Reader) error {
|
||||||
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
|
||||||
@@ -329,7 +351,15 @@ func (r *cmdReader) Close() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s Sudo) Open(ctx context.Context, path string) (io.ReadCloser, error) {
|
func (s Sudo) Open(ctx context.Context, path string) (io.ReadCloser, error) {
|
||||||
c := s.cmd(ctx, "read", path)
|
return s.open(ctx, "read", path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sudo) OpenAt(ctx context.Context, path string, offset int64) (io.ReadCloser, error) {
|
||||||
|
return s.open(ctx, "read", "--offset", strconv.FormatInt(offset, 10), path)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s Sudo) open(ctx context.Context, args ...string) (io.ReadCloser, error) {
|
||||||
|
c := s.cmd(ctx, args...)
|
||||||
errb := &strings.Builder{}
|
errb := &strings.Builder{}
|
||||||
c.Stderr = errb
|
c.Stderr = errb
|
||||||
out, err := c.StdoutPipe()
|
out, err := c.StdoutPipe()
|
||||||
@@ -380,6 +410,30 @@ func (s Sudo) Touch(ctx context.Context, path string) error {
|
|||||||
func (s Sudo) Rename(ctx context.Context, from, to string) error {
|
func (s Sudo) Rename(ctx context.Context, from, to string) error {
|
||||||
return s.runJSON(ctx, nil, "rename", from, to)
|
return s.runJSON(ctx, nil, "rename", from, to)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Move as root: the helper does the copying and talks on stdout.
|
||||||
|
func (s Sudo) Move(ctx context.Context, from, to string, progress io.Writer) error {
|
||||||
|
c := s.cmd(ctx, "move", from, to)
|
||||||
|
c.Stdout = progress
|
||||||
|
var errb strings.Builder
|
||||||
|
c.Stderr = &errb
|
||||||
|
if err := c.Run(); err != nil {
|
||||||
|
if msg := strings.TrimSpace(errb.String()); msg != "" {
|
||||||
|
return errors.New(msg)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// MoveCmd is the command a task runs for a long move: this binary's helper, as root when
|
||||||
|
// the backend is.
|
||||||
|
func MoveCmd(ctx context.Context, fsys FS, self, from, to string) *exec.Cmd {
|
||||||
|
if s, ok := fsys.(Sudo); ok {
|
||||||
|
return s.cmd(ctx, "move", from, to)
|
||||||
|
}
|
||||||
|
return exec.CommandContext(ctx, self, "fs", "move", from, to)
|
||||||
|
}
|
||||||
func (s Sudo) Delete(ctx context.Context, path string, recursive bool) error {
|
func (s Sudo) Delete(ctx context.Context, path string, recursive bool) error {
|
||||||
if recursive {
|
if recursive {
|
||||||
return s.runJSON(ctx, nil, "delete", "-r", path)
|
return s.runJSON(ctx, nil, "delete", "-r", path)
|
||||||
@@ -394,11 +448,16 @@ func (s Sudo) Chmod(ctx context.Context, path string, perm os.FileMode) error {
|
|||||||
func Helper(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
|
func Helper(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer) int {
|
||||||
fail := func(err error) int { fmt.Fprintln(stderr, err.Error()); return 1 }
|
fail := func(err error) int { fmt.Fprintln(stderr, err.Error()); return 1 }
|
||||||
if len(args) < 2 {
|
if len(args) < 2 {
|
||||||
return fail(errors.New("usage: hamlab fs <list|stat|read|write|mkdir|touch|rename|delete|chmod> …"))
|
return fail(errors.New("usage: hamlab fs <list|stat|read|write|mkdir|touch|rename|move|delete|chmod> …"))
|
||||||
}
|
}
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
var l Local
|
var l Local
|
||||||
op := args[0]
|
op := args[0]
|
||||||
|
var offset int64
|
||||||
|
if op == "read" && len(args) >= 4 && args[1] == "--offset" { // read --offset N <path>
|
||||||
|
offset, _ = strconv.ParseInt(args[2], 10, 64)
|
||||||
|
args = append([]string{"read"}, args[3:]...)
|
||||||
|
}
|
||||||
clean := func(i int) (string, error) {
|
clean := func(i int) (string, error) {
|
||||||
if i >= len(args) {
|
if i >= len(args) {
|
||||||
return "", errors.New("path required")
|
return "", errors.New("path required")
|
||||||
@@ -425,7 +484,7 @@ func Helper(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer)
|
|||||||
}
|
}
|
||||||
return emit(stdout, out, stderr)
|
return emit(stdout, out, stderr)
|
||||||
case "read":
|
case "read":
|
||||||
f, err := l.Open(ctx, p)
|
f, err := l.OpenAt(ctx, p, offset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
@@ -447,7 +506,7 @@ func Helper(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer)
|
|||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "rename":
|
case "rename", "move":
|
||||||
from, err := clean(1)
|
from, err := clean(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
@@ -456,6 +515,15 @@ func Helper(args []string, stdin io.Reader, stdout io.Writer, stderr io.Writer)
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
|
if op == "move" {
|
||||||
|
// A cancelled task is a SIGTERM: stop copying and take the partial copy away.
|
||||||
|
mctx, stop := signal.NotifyContext(ctx, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
defer stop()
|
||||||
|
if err := l.Move(mctx, from, to, stdout); err != nil {
|
||||||
|
return fail(err)
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
if err := l.Rename(ctx, from, to); err != nil {
|
if err := l.Rename(ctx, from, to); err != nil {
|
||||||
return fail(err)
|
return fail(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,197 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// IsCrossDevice reports a rename refused because the two ends are on different
|
||||||
|
// filesystems — on a NAS that is every move between datasets. The Sudo backend only has
|
||||||
|
// the helper's words for it.
|
||||||
|
func IsCrossDevice(err error) bool {
|
||||||
|
return errors.Is(err, syscall.EXDEV) || (err != nil && strings.Contains(err.Error(), "cross-device"))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inside reports whether to lies within from — a folder cannot be moved into itself.
|
||||||
|
func Inside(from, to string) bool {
|
||||||
|
return to == from || strings.HasPrefix(to, strings.TrimSuffix(from, "/")+"/")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move is Rename, and across filesystems a copy and a removal. The copy is built under a
|
||||||
|
// hidden temporary name beside where it will land and renamed into place only once it is
|
||||||
|
// complete, and the source is removed only after that: a failure part-way, or a cancel,
|
||||||
|
// leaves the source exactly as it was. Progress goes to w, a line at a time, for the task
|
||||||
|
// output that a long move is watched through.
|
||||||
|
func (Local) Move(ctx context.Context, from, to string, w io.Writer) error {
|
||||||
|
if protected[from] {
|
||||||
|
return errors.New("refusing to move " + from)
|
||||||
|
}
|
||||||
|
if Inside(from, to) {
|
||||||
|
return errors.New("a folder can't be moved into itself")
|
||||||
|
}
|
||||||
|
if _, err := os.Lstat(to); err == nil {
|
||||||
|
return errors.New("destination exists")
|
||||||
|
}
|
||||||
|
err := os.Rename(from, to)
|
||||||
|
if err == nil || !IsCrossDevice(err) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return moveByCopy(ctx, from, to, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// moveByCopy is the slow path: copy, rename into place, remove the original.
|
||||||
|
func moveByCopy(ctx context.Context, from, to string, w io.Writer) error {
|
||||||
|
st, err := os.Lstat(from)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tmp := filepath.Join(filepath.Dir(to), "."+filepath.Base(to)+".hamlab-moving")
|
||||||
|
_ = os.RemoveAll(tmp) // a previous attempt that died
|
||||||
|
fmt.Fprintf(w, "%s is on another filesystem: copying, then removing the original\n", filepath.Dir(to))
|
||||||
|
p := &progress{w: w}
|
||||||
|
if err := copyPath(ctx, from, tmp, st, p); err != nil {
|
||||||
|
fmt.Fprintln(w, "copy failed; removing the partial copy, the original is untouched")
|
||||||
|
_ = os.RemoveAll(tmp)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Rename(tmp, to); err != nil {
|
||||||
|
_ = os.RemoveAll(tmp)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "copied %s; removing the original\n", p.String())
|
||||||
|
if err := os.RemoveAll(from); err != nil {
|
||||||
|
return fmt.Errorf("copied to %s, but the original could not be removed: %w", to, err)
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "moved %s to %s\n", from, to)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type progress struct {
|
||||||
|
w io.Writer
|
||||||
|
files int64
|
||||||
|
bytes int64
|
||||||
|
shownFiles int64
|
||||||
|
shownBytes int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *progress) add(files, bytes int64) {
|
||||||
|
p.files += files
|
||||||
|
p.bytes += bytes
|
||||||
|
if p.files-p.shownFiles >= 500 || p.bytes-p.shownBytes >= 512<<20 {
|
||||||
|
fmt.Fprintf(p.w, "copying… %s\n", p.String())
|
||||||
|
p.shownFiles, p.shownBytes = p.files, p.bytes
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *progress) String() string {
|
||||||
|
return fmt.Sprintf("%d files, %s", p.files, fmtSize(p.bytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func fmtSize(b int64) string {
|
||||||
|
const unit = 1024
|
||||||
|
if b < unit {
|
||||||
|
return fmt.Sprintf("%d B", b)
|
||||||
|
}
|
||||||
|
div, exp := int64(unit), 0
|
||||||
|
for n := b / unit; n >= unit && exp < 4; n /= unit {
|
||||||
|
div *= unit
|
||||||
|
exp++
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTP"[exp])
|
||||||
|
}
|
||||||
|
|
||||||
|
// copyPath copies one entry of any kind, recursively for a directory, keeping mode, times
|
||||||
|
// and — when running as root — ownership. Devices, pipes and sockets are refused: a copy
|
||||||
|
// of one is not the thing itself.
|
||||||
|
func copyPath(ctx context.Context, src, dst string, st os.FileInfo, p *progress) error {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
mode := st.Mode()
|
||||||
|
switch {
|
||||||
|
case mode&os.ModeSymlink != 0:
|
||||||
|
target, err := os.Readlink(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := os.Symlink(target, dst); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
chown(dst, st)
|
||||||
|
p.add(1, 0)
|
||||||
|
return nil
|
||||||
|
case mode.IsDir():
|
||||||
|
if err := os.Mkdir(dst, mode.Perm()|0o700); err != nil { // writable while it fills, exact mode after
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ents, err := os.ReadDir(src)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, e := range ents {
|
||||||
|
lst, err := e.Info()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := copyPath(ctx, filepath.Join(src, e.Name()), filepath.Join(dst, e.Name()), lst, p); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chown(dst, st)
|
||||||
|
if err := os.Chmod(dst, mode.Perm()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Chtimes(dst, st.ModTime(), st.ModTime())
|
||||||
|
case mode.IsRegular():
|
||||||
|
n, err := copyFile(src, dst, st)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
p.add(1, n)
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("%s: can't copy a device, pipe or socket", src)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyFile(src, dst string, st os.FileInfo) (int64, error) {
|
||||||
|
in, err := os.Open(src)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer in.Close()
|
||||||
|
out, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_EXCL, st.Mode().Perm())
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
n, err := io.Copy(out, in)
|
||||||
|
if err != nil {
|
||||||
|
out.Close()
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
if err := out.Close(); err != nil {
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
chown(dst, st)
|
||||||
|
if err := os.Chmod(dst, st.Mode().Perm()); err != nil { // umask has no say
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
return n, os.Chtimes(dst, st.ModTime(), st.ModTime())
|
||||||
|
}
|
||||||
|
|
||||||
|
// chown keeps the owner when this process may set one; an ordinary user's copy is the
|
||||||
|
// user's, which is what a rename would have left anyway.
|
||||||
|
func chown(dst string, st os.FileInfo) {
|
||||||
|
if os.Geteuid() != 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if sys, ok := st.Sys().(*syscall.Stat_t); ok {
|
||||||
|
_ = os.Lchown(dst, int(sys.Uid), int(sys.Gid))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,195 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestInside(t *testing.T) {
|
||||||
|
for _, c := range []struct {
|
||||||
|
from, to string
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"/a/b", "/a/b", true}, {"/a/b", "/a/b/c", true}, {"/a/b", "/a/bc", false}, {"/a/b", "/a", false}, {"/", "/x", true},
|
||||||
|
} {
|
||||||
|
if got := Inside(c.from, c.to); got != c.want {
|
||||||
|
t.Errorf("Inside(%q, %q) = %v, want %v", c.from, c.to, got, c.want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsCrossDevice(t *testing.T) {
|
||||||
|
if !IsCrossDevice(&os.LinkError{Op: "rename", Old: "/a", New: "/b", Err: syscall.EXDEV}) {
|
||||||
|
t.Fatal("the kernel's EXDEV")
|
||||||
|
}
|
||||||
|
if !IsCrossDevice(errors.New("rename /a /b: invalid cross-device link")) {
|
||||||
|
t.Fatal("the helper's words for it")
|
||||||
|
}
|
||||||
|
if IsCrossDevice(errors.New("permission denied")) || IsCrossDevice(nil) {
|
||||||
|
t.Fatal("anything else is not")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The copy path, exercised directly: a tree with nested folders, a symlink, an odd mode
|
||||||
|
// and old mtimes lands whole under the new name, and the original is gone only then.
|
||||||
|
func TestMoveByCopy(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
src := filepath.Join(root, "src", "album")
|
||||||
|
if err := os.MkdirAll(filepath.Join(src, "sub", "deep"), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
old := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||||
|
write := func(p, s string, mode os.FileMode) {
|
||||||
|
if err := os.WriteFile(p, []byte(s), mode); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = os.Chmod(p, mode)
|
||||||
|
_ = os.Chtimes(p, old, old)
|
||||||
|
}
|
||||||
|
write(filepath.Join(src, "a.txt"), "hello", 0o644)
|
||||||
|
write(filepath.Join(src, "sub", "deep", "b.bin"), strings.Repeat("x", 100000), 0o600)
|
||||||
|
write(filepath.Join(src, "run.sh"), "#!/bin/sh\n", 0o755)
|
||||||
|
if err := os.Symlink("a.txt", filepath.Join(src, "link")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
_ = os.Chtimes(filepath.Join(src, "sub"), old, old)
|
||||||
|
|
||||||
|
dst := filepath.Join(root, "dst", "moved")
|
||||||
|
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out strings.Builder
|
||||||
|
if err := moveByCopy(context.Background(), src, dst, &out); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Lstat(src); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("the original is still there")
|
||||||
|
}
|
||||||
|
if b, _ := os.ReadFile(filepath.Join(dst, "a.txt")); string(b) != "hello" {
|
||||||
|
t.Fatal("a.txt")
|
||||||
|
}
|
||||||
|
if st, err := os.Stat(filepath.Join(dst, "sub", "deep", "b.bin")); err != nil || st.Size() != 100000 || st.Mode().Perm() != 0o600 || !st.ModTime().Equal(old) {
|
||||||
|
t.Fatalf("b.bin: %v %v", st, err)
|
||||||
|
}
|
||||||
|
if st, _ := os.Stat(filepath.Join(dst, "run.sh")); st.Mode().Perm() != 0o755 {
|
||||||
|
t.Fatal("mode lost")
|
||||||
|
}
|
||||||
|
if target, err := os.Readlink(filepath.Join(dst, "link")); err != nil || target != "a.txt" {
|
||||||
|
t.Fatal("symlink not copied as a symlink")
|
||||||
|
}
|
||||||
|
if st, _ := os.Stat(filepath.Join(dst, "sub")); !st.ModTime().Equal(old) {
|
||||||
|
t.Fatal("folder mtime lost")
|
||||||
|
}
|
||||||
|
if left, _ := filepath.Glob(filepath.Join(root, "dst", ".*hamlab-moving*")); len(left) != 0 {
|
||||||
|
t.Fatal("temporary copy left behind")
|
||||||
|
}
|
||||||
|
if !strings.Contains(out.String(), "moved ") || !strings.Contains(out.String(), "4 files") {
|
||||||
|
t.Fatalf("progress said: %q", out.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A failure part-way leaves the original as it was and no partial copy.
|
||||||
|
func TestMoveByCopyFailsClean(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
src := filepath.Join(root, "src")
|
||||||
|
if err := os.MkdirAll(src, 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(src, "a"), []byte("a"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := syscall.Mkfifo(filepath.Join(src, "pipe"), 0o644); err != nil {
|
||||||
|
t.Skip("no fifos here")
|
||||||
|
}
|
||||||
|
dst := filepath.Join(root, "dst")
|
||||||
|
err := moveByCopy(context.Background(), src, dst, &strings.Builder{})
|
||||||
|
if err == nil || !strings.Contains(err.Error(), "pipe") {
|
||||||
|
t.Fatalf("expected the pipe to be refused, got %v", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(src, "a")); err != nil {
|
||||||
|
t.Fatal("the original was touched")
|
||||||
|
}
|
||||||
|
if _, err := os.Lstat(dst); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("a partial copy was left")
|
||||||
|
}
|
||||||
|
if left, _ := filepath.Glob(filepath.Join(root, ".*hamlab-moving*")); len(left) != 0 {
|
||||||
|
t.Fatal("temporary copy left behind")
|
||||||
|
}
|
||||||
|
// a cancel is the same story
|
||||||
|
if err := os.Remove(filepath.Join(src, "pipe")); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
if err := moveByCopy(ctx, src, dst, &strings.Builder{}); err == nil {
|
||||||
|
t.Fatal("a cancelled move must fail")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(src, "a")); err != nil {
|
||||||
|
t.Fatal("the original was touched by a cancel")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move on one filesystem is a rename: nothing is copied, and the guards hold.
|
||||||
|
func TestMoveSameFilesystem(t *testing.T) {
|
||||||
|
root := t.TempDir()
|
||||||
|
src := filepath.Join(root, "f")
|
||||||
|
if err := os.WriteFile(src, []byte("x"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var l Local
|
||||||
|
var out strings.Builder
|
||||||
|
if err := l.Move(context.Background(), src, filepath.Join(root, "g"), &out); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if out.Len() != 0 {
|
||||||
|
t.Fatal("a rename has nothing to say")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(filepath.Join(root, "g")); err != nil {
|
||||||
|
t.Fatal("not renamed")
|
||||||
|
}
|
||||||
|
if err := l.Move(context.Background(), root, filepath.Join(root, "inside"), &out); err == nil {
|
||||||
|
t.Fatal("into itself")
|
||||||
|
}
|
||||||
|
if err := l.Move(context.Background(), filepath.Join(root, "g"), filepath.Join(root, "g"), &out); err == nil {
|
||||||
|
t.Fatal("onto itself")
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(filepath.Join(root, "h"), nil, 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := l.Move(context.Background(), filepath.Join(root, "g"), filepath.Join(root, "h"), &out); err == nil || !strings.Contains(err.Error(), "exists") {
|
||||||
|
t.Fatalf("onto an existing file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A link to a file carries the file's size and time, so the row and the preview key follow
|
||||||
|
// the file; a link to a folder is a folder; a link to nothing says so.
|
||||||
|
func TestEntryOfLinks(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := os.WriteFile(filepath.Join(dir, "f"), []byte("12345"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
old := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
|
||||||
|
_ = os.Chtimes(filepath.Join(dir, "f"), old, old)
|
||||||
|
_ = os.Mkdir(filepath.Join(dir, "d"), 0o755)
|
||||||
|
for _, l := range [][2]string{{"lf", "f"}, {"ld", "d"}, {"lx", "nowhere"}} {
|
||||||
|
if err := os.Symlink(l[1], filepath.Join(dir, l[0])); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ent := func(n string) Entry { st, _ := os.Lstat(filepath.Join(dir, n)); return entryOf(dir, n, st) }
|
||||||
|
if e := ent("lf"); e.Type != "link" || e.Size != 5 || e.Mtime != old.UnixMilli() || e.Target != "f" || e.Broken {
|
||||||
|
t.Fatalf("link to file: %+v", e)
|
||||||
|
}
|
||||||
|
if e := ent("ld"); e.Type != "dir" || e.Target != "d" {
|
||||||
|
t.Fatalf("link to dir: %+v", e)
|
||||||
|
}
|
||||||
|
if e := ent("lx"); e.Type != "link" || !e.Broken || e.Preview != "" {
|
||||||
|
t.Fatalf("dangling: %+v", e)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Seeker is an FS file as http.ServeContent wants one — something to seek in — for a file
|
||||||
|
// whose size is already known from a stat. ServeContent seeks to the end for the size, to
|
||||||
|
// the start of the range asked for, then reads: so the backend is opened once, at that
|
||||||
|
// position, and only ever read forward. The Sudo backend's helper is started at the offset
|
||||||
|
// rather than streamed from the top and skipped.
|
||||||
|
type Seeker struct {
|
||||||
|
ctx context.Context
|
||||||
|
fsys FS
|
||||||
|
path string
|
||||||
|
size int64
|
||||||
|
pos int64 // where the next Read reads from
|
||||||
|
at int64 // where cur is positioned, when open
|
||||||
|
cur io.ReadCloser
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSeeker(ctx context.Context, fsys FS, path string, size int64) *Seeker {
|
||||||
|
return &Seeker{ctx: ctx, fsys: fsys, path: path, size: size}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check opens the file now, from the start, so a file that cannot be read is known before
|
||||||
|
// any headers go out. Seeking costs nothing; a Read from somewhere else reopens there.
|
||||||
|
func (s *Seeker) Check() error {
|
||||||
|
if s.cur != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
f, err := s.fsys.OpenAt(s.ctx, s.path, 0)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
s.cur, s.at = f, 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Seeker) Seek(offset int64, whence int) (int64, error) {
|
||||||
|
var pos int64
|
||||||
|
switch whence {
|
||||||
|
case io.SeekStart:
|
||||||
|
pos = offset
|
||||||
|
case io.SeekCurrent:
|
||||||
|
pos = s.pos + offset
|
||||||
|
case io.SeekEnd:
|
||||||
|
pos = s.size + offset
|
||||||
|
default:
|
||||||
|
return 0, errors.New("bad whence")
|
||||||
|
}
|
||||||
|
if pos < 0 {
|
||||||
|
return 0, errors.New("seek before start")
|
||||||
|
}
|
||||||
|
s.pos = pos // the stream, if open, stays where it is until a Read wants somewhere else
|
||||||
|
return pos, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Seeker) Read(p []byte) (int, error) {
|
||||||
|
if s.pos >= s.size {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
if s.cur != nil && s.pos != s.at {
|
||||||
|
s.cur.Close()
|
||||||
|
s.cur = nil
|
||||||
|
}
|
||||||
|
if s.cur == nil {
|
||||||
|
f, err := s.fsys.OpenAt(s.ctx, s.path, s.pos)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
s.cur, s.at = f, s.pos
|
||||||
|
}
|
||||||
|
n, err := s.cur.Read(p)
|
||||||
|
s.pos += int64(n)
|
||||||
|
s.at += int64(n)
|
||||||
|
return n, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Seeker) Close() error {
|
||||||
|
if s.cur == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := s.cur.Close()
|
||||||
|
s.cur = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package files
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
import "strings"
|
||||||
|
|
||||||
|
type stringsBuilder = strings.Builder
|
||||||
|
|
||||||
|
type countingFS struct {
|
||||||
|
Local
|
||||||
|
opens []int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *countingFS) OpenAt(ctx context.Context, path string, off int64) (io.ReadCloser, error) {
|
||||||
|
c.opens = append(c.opens, off)
|
||||||
|
return c.Local.OpenAt(ctx, path, off)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeContent's moves — size from the end, seek to the range, read forward — open the
|
||||||
|
// backend once, at the range; a seek to where the stream already is costs nothing.
|
||||||
|
func TestSeeker(t *testing.T) {
|
||||||
|
p := filepath.Join(t.TempDir(), "f")
|
||||||
|
data := make([]byte, 1000)
|
||||||
|
for i := range data {
|
||||||
|
data[i] = byte(i)
|
||||||
|
}
|
||||||
|
if err := os.WriteFile(p, data, 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
fs := &countingFS{}
|
||||||
|
s := NewSeeker(context.Background(), fs, p, int64(len(data)))
|
||||||
|
if n, _ := s.Seek(0, io.SeekEnd); n != 1000 {
|
||||||
|
t.Fatalf("end = %d", n)
|
||||||
|
}
|
||||||
|
if _, err := s.Seek(100, io.SeekStart); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
buf := make([]byte, 50)
|
||||||
|
if n, err := io.ReadFull(s, buf); err != nil || n != 50 || buf[0] != 100 || buf[49] != 149 {
|
||||||
|
t.Fatalf("read at 100: %d %v %v", n, err, buf[:2])
|
||||||
|
}
|
||||||
|
if _, err := s.Seek(150, io.SeekStart); err != nil { // where it already is
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if n, _ := io.ReadFull(s, buf); n != 50 || buf[0] != 150 {
|
||||||
|
t.Fatal("read after a no-op seek")
|
||||||
|
}
|
||||||
|
if len(fs.opens) != 1 || fs.opens[0] != 100 {
|
||||||
|
t.Fatalf("opens = %v, want [100]", fs.opens)
|
||||||
|
}
|
||||||
|
if _, err := s.Seek(0, io.SeekStart); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
rest, err := io.ReadAll(s)
|
||||||
|
if err != nil || len(rest) != 1000 || rest[999] != data[999] {
|
||||||
|
t.Fatalf("read all from the start: %d %v", len(rest), err)
|
||||||
|
}
|
||||||
|
if len(fs.opens) != 2 || fs.opens[1] != 0 {
|
||||||
|
t.Fatalf("opens = %v, want [100 0]", fs.opens)
|
||||||
|
}
|
||||||
|
if n, err := s.Read(buf); n != 0 || err != io.EOF {
|
||||||
|
t.Fatal("past the end is EOF")
|
||||||
|
}
|
||||||
|
if _, err := s.Seek(-1, io.SeekStart); err == nil {
|
||||||
|
t.Fatal("before the start")
|
||||||
|
}
|
||||||
|
if err := s.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
// Check opens at the start and keeps that stream for a read from the start
|
||||||
|
s2 := NewSeeker(context.Background(), fs, p, 1000)
|
||||||
|
if err := s2.Check(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
s2.Seek(0, io.SeekEnd)
|
||||||
|
s2.Seek(0, io.SeekStart)
|
||||||
|
if n, _ := io.ReadFull(s2, buf); n != 50 || buf[0] != 0 {
|
||||||
|
t.Fatal("read from the start after Check")
|
||||||
|
}
|
||||||
|
if len(fs.opens) != 3 {
|
||||||
|
t.Fatalf("Check's open was not reused: %v", fs.opens)
|
||||||
|
}
|
||||||
|
s2.Close()
|
||||||
|
if err := NewSeeker(context.Background(), fs, filepath.Join(t.TempDir(), "nope"), 5).Check(); err == nil {
|
||||||
|
t.Fatal("Check must report a file that can't be opened")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The helper's read takes an offset, for the Sudo backend.
|
||||||
|
func TestHelperReadOffset(t *testing.T) {
|
||||||
|
p := filepath.Join(t.TempDir(), "f")
|
||||||
|
if err := os.WriteFile(p, []byte("0123456789"), 0o644); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var out, errb stringsBuilder
|
||||||
|
if rc := Helper([]string{"read", "--offset", "7", p}, nil, &out, &errb); rc != 0 {
|
||||||
|
t.Fatalf("rc %d: %s", rc, errb.String())
|
||||||
|
}
|
||||||
|
if out.String() != "789" {
|
||||||
|
t.Fatalf("got %q", out.String())
|
||||||
|
}
|
||||||
|
out.Reset()
|
||||||
|
if rc := Helper([]string{"read", p}, nil, &out, &errb); rc != 0 || out.String() != "0123456789" {
|
||||||
|
t.Fatalf("plain read: rc %d %q", rc, out.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,232 @@
|
|||||||
|
package incus
|
||||||
|
|
||||||
|
// Nightly exports: every instance as a tarball in virt/exports/<name>/, kept to a count, as a
|
||||||
|
// cron line like the ZFS schedules — `hamlab virt-export` in /etc/cron.d/hamlab-virt — so the
|
||||||
|
// backup runs whether or not the console is up. The settings and the last run's record are
|
||||||
|
// files beside the data, not state.json, because the cron job is a separate process.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hamlab/internal/store"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const CronFile = "/etc/cron.d/hamlab-virt"
|
||||||
|
|
||||||
|
type ExportConfig struct {
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
Hour int `json:"hour"`
|
||||||
|
Minute int `json:"minute"`
|
||||||
|
Keep int `json:"keep"` // newest exports kept per instance
|
||||||
|
InstanceOnly bool `json:"instance_only"` // leave the snapshots out of the tarball
|
||||||
|
Instances []string `json:"instances,omitempty"` // empty: every instance
|
||||||
|
Updated time.Time `json:"updated,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func DefaultExportConfig() ExportConfig {
|
||||||
|
return ExportConfig{Hour: 2, Minute: 15, Keep: 3, InstanceOnly: true}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c ExportConfig) Validate() error {
|
||||||
|
if c.Hour < 0 || c.Hour > 23 || c.Minute < 0 || c.Minute > 59 {
|
||||||
|
return errors.New("time: hour 0–23, minute 0–59")
|
||||||
|
}
|
||||||
|
if c.Keep < 1 || c.Keep > 100 {
|
||||||
|
return errors.New("keep: between 1 and 100")
|
||||||
|
}
|
||||||
|
for _, n := range c.Instances {
|
||||||
|
if !ValidName(n) {
|
||||||
|
return errors.New("instance: " + n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c ExportConfig) CronExpr() string { return fmt.Sprintf("%d %d * * *", c.Minute, c.Hour) }
|
||||||
|
|
||||||
|
// NextRun is the next time the line fires, for the page.
|
||||||
|
func (c ExportConfig) NextRun(now time.Time) time.Time {
|
||||||
|
n := time.Date(now.Year(), now.Month(), now.Day(), c.Hour, c.Minute, 0, 0, now.Location())
|
||||||
|
if !n.After(now) {
|
||||||
|
n = n.Add(24 * time.Hour)
|
||||||
|
}
|
||||||
|
return n
|
||||||
|
}
|
||||||
|
|
||||||
|
// CronText is the whole file; rewritten on every change, never edited.
|
||||||
|
func (c ExportConfig) CronText(user, exe, config string) string {
|
||||||
|
var b strings.Builder
|
||||||
|
b.WriteString("# Ham Lab instance exports — written by the Virtualization tab, rewritten whole on every change.\n")
|
||||||
|
b.WriteString("# Edit it there, not here.\n")
|
||||||
|
b.WriteString("SHELL=/bin/sh\nPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n\n")
|
||||||
|
fmt.Fprintf(&b, "# every instance to virt/exports, keep %d each\n", c.Keep)
|
||||||
|
fmt.Fprintf(&b, "%s %s %s -config %s virt-export\n", c.CronExpr(), user, exe, config)
|
||||||
|
return b.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) exportConfigPath() string { return filepath.Join(m.dir, "exports.json") }
|
||||||
|
func (m *Manager) exportRunPath() string { return filepath.Join(m.dir, "export-runs.json") }
|
||||||
|
|
||||||
|
func (m *Manager) ExportConfig() ExportConfig {
|
||||||
|
c := DefaultExportConfig()
|
||||||
|
if b, err := os.ReadFile(m.exportConfigPath()); err == nil {
|
||||||
|
_ = json.Unmarshal(b, &c)
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) SaveExportConfig(c ExportConfig) error {
|
||||||
|
if err := c.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := m.EnsureDirs(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.Updated = time.Now()
|
||||||
|
b, _ := json.MarshalIndent(c, "", " ")
|
||||||
|
return store.WriteFileAtomic(m.exportConfigPath(), b, 0o644)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ExportRun struct {
|
||||||
|
At time.Time `json:"at"`
|
||||||
|
Exported []string `json:"exported"`
|
||||||
|
Pruned []string `json:"pruned"`
|
||||||
|
Errors []string `json:"errors,omitempty"`
|
||||||
|
Seconds float64 `json:"seconds"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) LastExportRun() *ExportRun {
|
||||||
|
b, err := os.ReadFile(m.exportRunPath())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var r ExportRun
|
||||||
|
if json.Unmarshal(b, &r) != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return &r
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExportFile is one tarball in the exports tree.
|
||||||
|
type ExportFile struct {
|
||||||
|
Instance string `json:"instance"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Size int64 `json:"size"`
|
||||||
|
Modified time.Time `json:"modified"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) ExportFiles() []ExportFile {
|
||||||
|
out := []ExportFile{}
|
||||||
|
dirs, _ := os.ReadDir(m.ExportsDir())
|
||||||
|
for _, d := range dirs {
|
||||||
|
if !d.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
files, _ := os.ReadDir(filepath.Join(m.ExportsDir(), d.Name()))
|
||||||
|
for _, f := range files {
|
||||||
|
if f.IsDir() || strings.HasSuffix(f.Name(), ".part") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if info, err := f.Info(); err == nil {
|
||||||
|
out = append(out, ExportFile{Instance: d.Name(), Name: f.Name(), Size: info.Size(), Modified: info.ModTime()})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Slice(out, func(i, j int) bool {
|
||||||
|
if out[i].Instance != out[j].Instance {
|
||||||
|
return out[i].Instance < out[j].Instance
|
||||||
|
}
|
||||||
|
return out[i].Modified.After(out[j].Modified)
|
||||||
|
})
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunExports exports the named instances (or every one when names is empty) and prunes each
|
||||||
|
// instance's folder to the configured count. Output goes to w line by line for a task to show.
|
||||||
|
func (m *Manager) RunExports(ctx context.Context, names []string, w func(string)) ExportRun {
|
||||||
|
start := time.Now()
|
||||||
|
c := m.ExportConfig()
|
||||||
|
run := ExportRun{At: start, Exported: []string{}, Pruned: []string{}}
|
||||||
|
if err := m.EnsureDirs(); err != nil {
|
||||||
|
run.Errors = append(run.Errors, err.Error())
|
||||||
|
return run
|
||||||
|
}
|
||||||
|
all, err := m.Instances(ctx)
|
||||||
|
if err != nil {
|
||||||
|
run.Errors = append(run.Errors, "listing instances: "+err.Error())
|
||||||
|
return run
|
||||||
|
}
|
||||||
|
want := map[string]bool{}
|
||||||
|
for _, n := range names {
|
||||||
|
want[n] = true
|
||||||
|
}
|
||||||
|
if len(names) == 0 && len(c.Instances) > 0 {
|
||||||
|
for _, n := range c.Instances {
|
||||||
|
want[n] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, in := range all {
|
||||||
|
if len(want) > 0 && !want[in.Name] {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dir := filepath.Join(m.ExportsDir(), in.Name)
|
||||||
|
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||||
|
run.Errors = append(run.Errors, in.Name+": "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
stamp := start.Format("2006-01-02_15.04")
|
||||||
|
final := filepath.Join(dir, fmt.Sprintf("%s-%s.tar.gz", in.Name, stamp))
|
||||||
|
part := final + ".part"
|
||||||
|
args := []string{"export", in.Name, part}
|
||||||
|
if c.InstanceOnly {
|
||||||
|
args = append(args, "--instance-only")
|
||||||
|
}
|
||||||
|
w(fmt.Sprintf("exporting %s …", in.Name))
|
||||||
|
cctx, cancel := context.WithTimeout(ctx, 2*time.Hour)
|
||||||
|
out, err := exec.CommandContext(cctx, "incus", args...).CombinedOutput()
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
_ = os.Remove(part)
|
||||||
|
msg := strings.TrimSpace(string(out))
|
||||||
|
if msg == "" {
|
||||||
|
msg = err.Error()
|
||||||
|
}
|
||||||
|
run.Errors = append(run.Errors, in.Name+": "+msg)
|
||||||
|
w(" failed: " + msg)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := os.Rename(part, final); err != nil {
|
||||||
|
run.Errors = append(run.Errors, in.Name+": "+err.Error())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if st, err := os.Stat(final); err == nil {
|
||||||
|
w(fmt.Sprintf(" %s (%.1f MiB)", filepath.Base(final), float64(st.Size())/(1<<20)))
|
||||||
|
}
|
||||||
|
run.Exported = append(run.Exported, filepath.Base(final))
|
||||||
|
// prune: newest first, keep c.Keep
|
||||||
|
var files []ExportFile
|
||||||
|
for _, f := range m.ExportFiles() {
|
||||||
|
if f.Instance == in.Name {
|
||||||
|
files = append(files, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i := c.Keep; i < len(files); i++ {
|
||||||
|
if err := os.Remove(filepath.Join(dir, files[i].Name)); err == nil {
|
||||||
|
run.Pruned = append(run.Pruned, files[i].Name)
|
||||||
|
w(" pruned " + files[i].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
run.Seconds = time.Since(start).Seconds()
|
||||||
|
b, _ := json.MarshalIndent(run, "", " ")
|
||||||
|
_ = store.WriteFileAtomic(m.exportRunPath(), b, 0o644)
|
||||||
|
return run
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,93 @@
|
|||||||
|
package incus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The install script is generated, so a quoting slip would only show on a live machine.
|
||||||
|
// Build it for the shapes of machine we have and let sh check the syntax.
|
||||||
|
func TestInstallScriptShapes(t *testing.T) {
|
||||||
|
if _, err := exec.LookPath("sh"); err != nil {
|
||||||
|
t.Skip("no sh")
|
||||||
|
}
|
||||||
|
m := New("/home/core/apps/hamlab", "core", "test")
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
st Status
|
||||||
|
o InstallOpts
|
||||||
|
want []string
|
||||||
|
}{
|
||||||
|
{"grid: zfs, apt, docker", Status{State: "installable", PM: "apt", Arch: "x86_64", Drivers: []string{"zfs", "btrfs", "dir"}, Docker: true, ZFSHost: true, DiskFree: 700 << 30, MemTotal: 62 << 30},
|
||||||
|
InstallOpts{Driver: "zfs", PoolSizeGiB: 300, Subnet: "172.16.100.1/24", DockerRules: true, ARCMaxGiB: 16},
|
||||||
|
[]string{"apt-get", "zpool create", "incus storage create local zfs source=hamlab-virt", "ipv4.address='172.16.100.1/24'", "DOCKER-USER", "zfs_arc_max", "usermod -aG incus-admin 'core'", "SupplementaryGroups=incus-admin", "systemd-run"}},
|
||||||
|
{"core: btrfs, apt, docker, no tools yet", Status{State: "installable", PM: "apt", Arch: "x86_64", Drivers: []string{"btrfs", "dir"}, Docker: true, DiskFree: 170 << 30, MemTotal: 8 << 30},
|
||||||
|
InstallOpts{Driver: "btrfs", PoolSizeGiB: 40, Subnet: "172.16.100.1/24", DockerRules: true},
|
||||||
|
[]string{"btrfs-progs", "mkfs.btrfs", "/etc/fstab", "incus storage create local btrfs source="}},
|
||||||
|
{"mac: dir, dnf, arm64", Status{State: "installable", PM: "dnf", Arch: "aarch64", Drivers: []string{"dir"}, DiskFree: 200 << 30, MemTotal: 15 << 30},
|
||||||
|
InstallOpts{Driver: "dir", Subnet: "172.16.101.1/24"},
|
||||||
|
[]string{"dnf -y install incus qemu-system-aarch64-core edk2-aarch64", "incus storage create local dir source="}},
|
||||||
|
{"repair: already installed", Status{State: "repair", PM: "apt", Arch: "x86_64", Drivers: []string{"dir"}, DiskFree: 10 << 30, MemTotal: 4 << 30},
|
||||||
|
InstallOpts{Driver: "dir", Subnet: "172.16.100.1/24"},
|
||||||
|
[]string{"already installed"}},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
script, lay, err := m.InstallScript(c.st, c.o)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", c.name, err)
|
||||||
|
}
|
||||||
|
for _, w := range c.want {
|
||||||
|
if !strings.Contains(script, w) {
|
||||||
|
t.Errorf("%s: script lacks %q", c.name, w)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lay.Driver != c.o.Driver || lay.Subnet != c.o.Subnet {
|
||||||
|
t.Errorf("%s: layout %+v", c.name, lay)
|
||||||
|
}
|
||||||
|
cmd := exec.Command("sh", "-n")
|
||||||
|
cmd.Stdin = strings.NewReader(script)
|
||||||
|
if out, err := cmd.CombinedOutput(); err != nil {
|
||||||
|
t.Errorf("%s: sh -n: %v\n%s", c.name, err, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInstallScriptRefusals(t *testing.T) {
|
||||||
|
m := New("/x", "core", "test")
|
||||||
|
st := Status{State: "installable", PM: "apt", Drivers: []string{"btrfs", "dir"}, DiskFree: 20 << 30, MemTotal: 8 << 30}
|
||||||
|
for _, o := range []InstallOpts{
|
||||||
|
{Driver: "zfs", PoolSizeGiB: 10, Subnet: "172.16.100.1/24"}, // not offered here
|
||||||
|
{Driver: "btrfs", PoolSizeGiB: 50, Subnet: "172.16.100.1/24"}, // more than is free
|
||||||
|
{Driver: "btrfs", PoolSizeGiB: 10, Subnet: "172.16.100.0/24"}, // the network address, not the bridge's
|
||||||
|
{Driver: "btrfs", PoolSizeGiB: 10, Subnet: "10.0.0.1/8"}, // too wide
|
||||||
|
{Driver: "btrfs", PoolSizeGiB: 10, Subnet: "172.16.100.1/24", ARCMaxGiB: 64}, // more RAM than there is
|
||||||
|
} {
|
||||||
|
if _, _, err := m.InstallScript(st, o); err == nil {
|
||||||
|
t.Errorf("accepted %+v", o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateArgs(t *testing.T) {
|
||||||
|
args, err := CreateArgs(CreateSpec{Name: "web-1", Image: "images:debian/13", VM: true, CPU: "2", Memory: "2GiB", Disk: "20GiB", Network: "incusbr0", Start: true}, true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := "launch images:debian/13 web-1 --vm -c limits.cpu=2 -c limits.memory=2GiB -d root,size=20GiB -n incusbr0"
|
||||||
|
if a, err := CreateArgs(CreateSpec{Name: "x", Image: "images:alpine/3.22", Pool: "local"}, true); err != nil || strings.Join(a, " ") != "init images:alpine/3.22 x -d root,pool=local" {
|
||||||
|
t.Fatalf("pool override: %v %v", a, err)
|
||||||
|
}
|
||||||
|
if got := strings.Join(args, " "); got != want {
|
||||||
|
t.Fatalf("got %q", got)
|
||||||
|
}
|
||||||
|
if _, err := CreateArgs(CreateSpec{Name: "web-1", Image: "images:debian/13", VM: true}, false); err == nil {
|
||||||
|
t.Fatal("a VM without KVM must be refused")
|
||||||
|
}
|
||||||
|
if _, err := CreateArgs(CreateSpec{Name: "1bad", Image: "images:debian/13"}, true); err == nil {
|
||||||
|
t.Fatal("a name starting with a digit must be refused")
|
||||||
|
}
|
||||||
|
if _, err := CreateArgs(CreateSpec{Name: "ok", Image: "images:debian/13", Memory: "2 gigs"}, true); err == nil {
|
||||||
|
t.Fatal("a malformed size must be refused")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,126 @@
|
|||||||
|
package incus
|
||||||
|
|
||||||
|
// The bridge subnet. Incus's own `auto` picks a random 10.x.y.0/24, and on a LAN that is
|
||||||
|
// itself 10.0.0.0/8 (this one) every such pick is on-link and collides. So Ham Lab chooses:
|
||||||
|
// the first /24 that nothing on the machine already routes, is not an address it holds, and
|
||||||
|
// is outside Docker's configured pools — tried in an order that also stays clear of Docker's
|
||||||
|
// *defaults* (172.17–172.31 and 192.168.0.0/16 carved into /20s), which a Docker that has no
|
||||||
|
// daemon.json will hand out later without asking anyone.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/netip"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// candidates in order of preference: 172.16.0.0/16 is the one RFC 1918 block Docker never
|
||||||
|
// takes by default; then the top of 192.168/16, which Docker reaches last; then the rest.
|
||||||
|
func candidates() []netip.Prefix {
|
||||||
|
var out []netip.Prefix
|
||||||
|
for i := 100; i <= 254; i++ {
|
||||||
|
out = append(out, netip.MustParsePrefix(fmt.Sprintf("172.16.%d.0/24", i)))
|
||||||
|
}
|
||||||
|
for i := 254; i >= 200; i-- {
|
||||||
|
out = append(out, netip.MustParsePrefix(fmt.Sprintf("192.168.%d.0/24", i)))
|
||||||
|
}
|
||||||
|
for i := 1; i < 100; i++ {
|
||||||
|
out = append(out, netip.MustParsePrefix(fmt.Sprintf("172.16.%d.0/24", i)))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func overlaps(a, b netip.Prefix) bool {
|
||||||
|
return a.Contains(b.Addr()) || b.Contains(a.Addr())
|
||||||
|
}
|
||||||
|
|
||||||
|
// PickSubnet returns the first candidate /24 clear of every prefix in taken.
|
||||||
|
func PickSubnet(taken []netip.Prefix) netip.Prefix {
|
||||||
|
for _, c := range candidates() {
|
||||||
|
clear := true
|
||||||
|
for _, t := range taken {
|
||||||
|
if overlaps(c, t) {
|
||||||
|
clear = false
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if clear {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return netip.MustParsePrefix("172.16.100.0/24") // every candidate taken: not a machine this will run on
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gateway is the bridge's own address in a subnet: its first host, as CIDR, the way
|
||||||
|
// Incus's ipv4.address wants it.
|
||||||
|
func Gateway(p netip.Prefix) string {
|
||||||
|
return fmt.Sprintf("%s/%d", p.Masked().Addr().Next(), p.Bits())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TakenPrefixes reads what the machine already uses: its routes, its addresses, and the
|
||||||
|
// pools Docker is configured to carve networks from.
|
||||||
|
func TakenPrefixes(ctx context.Context) []netip.Prefix {
|
||||||
|
var out []netip.Prefix
|
||||||
|
add := func(s string) {
|
||||||
|
if p, err := netip.ParsePrefix(s); err == nil && p.Addr().Is4() {
|
||||||
|
out = append(out, p.Masked())
|
||||||
|
} else if a, err := netip.ParseAddr(s); err == nil && a.Is4() {
|
||||||
|
out = append(out, netip.PrefixFrom(a, 32))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
if b, err := exec.CommandContext(c, "ip", "-j", "-4", "route", "show").Output(); err == nil {
|
||||||
|
var routes []struct{ Dst string }
|
||||||
|
if json.Unmarshal(b, &routes) == nil {
|
||||||
|
for _, r := range routes {
|
||||||
|
if r.Dst != "" && r.Dst != "default" {
|
||||||
|
add(r.Dst)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if b, err := exec.CommandContext(c, "ip", "-j", "-4", "addr", "show").Output(); err == nil {
|
||||||
|
var links []struct {
|
||||||
|
AddrInfo []struct {
|
||||||
|
Local string `json:"local"`
|
||||||
|
Prefixlen int `json:"prefixlen"`
|
||||||
|
} `json:"addr_info"`
|
||||||
|
}
|
||||||
|
if json.Unmarshal(b, &links) == nil {
|
||||||
|
for _, l := range links {
|
||||||
|
for _, a := range l.AddrInfo {
|
||||||
|
add(fmt.Sprintf("%s/%d", a.Local, a.Prefixlen))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, p := range dockerPools() {
|
||||||
|
add(p)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// dockerPools reads default-address-pools from Docker's daemon.json, and falls back to
|
||||||
|
// Docker's built-in defaults when the file names none.
|
||||||
|
func dockerPools() []string {
|
||||||
|
var cfg struct {
|
||||||
|
Pools []struct {
|
||||||
|
Base string `json:"base"`
|
||||||
|
} `json:"default-address-pools"`
|
||||||
|
}
|
||||||
|
if b, err := os.ReadFile("/etc/docker/daemon.json"); err == nil && json.Unmarshal(b, &cfg) == nil && len(cfg.Pools) > 0 {
|
||||||
|
var out []string
|
||||||
|
for _, p := range cfg.Pools {
|
||||||
|
out = append(out, p.Base)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
if _, err := exec.LookPath("docker"); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return []string{"172.17.0.0/12", "192.168.0.0/16"}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package incus
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPickSubnetAvoidsTaken(t *testing.T) {
|
||||||
|
taken := []netip.Prefix{
|
||||||
|
netip.MustParsePrefix("10.0.0.0/8"), // the LAN, a /8
|
||||||
|
netip.MustParsePrefix("172.16.100.0/24"), // first choice gone
|
||||||
|
netip.MustParsePrefix("172.16.101.0/24"), // and the second
|
||||||
|
netip.MustParsePrefix("172.28.0.0/14"), // Docker's configured pool
|
||||||
|
netip.MustParsePrefix("192.168.80.0/20"), // a Docker network
|
||||||
|
}
|
||||||
|
got := PickSubnet(taken)
|
||||||
|
if got.String() != "172.16.102.0/24" {
|
||||||
|
t.Fatalf("got %s", got)
|
||||||
|
}
|
||||||
|
if Gateway(got) != "172.16.102.1/24" {
|
||||||
|
t.Fatalf("gateway %s", Gateway(got))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPickSubnetWholeBlockTaken(t *testing.T) {
|
||||||
|
// 172.16.0.0/12 covers every 172.16.x candidate; the picker must move to 192.168
|
||||||
|
got := PickSubnet([]netip.Prefix{netip.MustParsePrefix("172.16.0.0/12")})
|
||||||
|
if got.String() != "192.168.254.0/24" {
|
||||||
|
t.Fatalf("got %s", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOverlapsEitherWay(t *testing.T) {
|
||||||
|
a := netip.MustParsePrefix("172.16.5.0/24")
|
||||||
|
if !overlaps(a, netip.MustParsePrefix("172.16.0.0/12")) || !overlaps(netip.MustParsePrefix("172.16.0.0/12"), a) {
|
||||||
|
t.Fatal("a /24 inside a /12 overlaps both ways")
|
||||||
|
}
|
||||||
|
if overlaps(a, netip.MustParsePrefix("172.16.6.0/24")) {
|
||||||
|
t.Fatal("neighbours do not overlap")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
package incus
|
||||||
|
|
||||||
|
// The graphical console. Incus exposes a VM's SPICE display as a websocket: POST to the
|
||||||
|
// instance's console endpoint with type vga and it answers an operation with two secrets,
|
||||||
|
// one for the display stream and one for a control channel. Ham Lab dials both over Incus's
|
||||||
|
// unix socket and hands the display stream to the browser, where spice-html5 draws it.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/coder/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
// SocketPath is where Incus's API listens; the daemon's account reaches it by being in
|
||||||
|
// incus-admin, which Install arranges.
|
||||||
|
func SocketPath() string {
|
||||||
|
if p := os.Getenv("INCUS_SOCKET"); p != "" {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
for _, p := range []string{"/var/lib/incus/unix.socket", "/run/incus/unix.socket"} {
|
||||||
|
if _, err := os.Stat(p); err == nil {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "/var/lib/incus/unix.socket"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) apiClient() *http.Client {
|
||||||
|
sock := SocketPath()
|
||||||
|
return &http.Client{Transport: &http.Transport{
|
||||||
|
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
|
||||||
|
var d net.Dialer
|
||||||
|
return d.DialContext(ctx, "unix", sock)
|
||||||
|
},
|
||||||
|
ForceAttemptHTTP2: false,
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A SPICE client opens one connection per channel — main, display, inputs, cursor, playback
|
||||||
|
// — and spice-html5 does the same with websockets, all to one URL. Incus allows exactly one
|
||||||
|
// console operation per VM at a time, but takes any number of data websockets on it. So a VGA
|
||||||
|
// session is one operation, held for as long as any channel is open, and every browser
|
||||||
|
// websocket becomes one more data websocket on it.
|
||||||
|
type vgaSession struct {
|
||||||
|
op string
|
||||||
|
secret string
|
||||||
|
control *websocket.Conn
|
||||||
|
cancel context.CancelFunc
|
||||||
|
refs int
|
||||||
|
}
|
||||||
|
|
||||||
|
// VGAChannel returns a fresh data websocket on the VM's console operation, opening the
|
||||||
|
// operation first when none is held. release must be called when the channel is done.
|
||||||
|
func (m *Manager) VGAChannel(ctx context.Context, name string) (data *websocket.Conn, release func(), err error) {
|
||||||
|
if !ValidName(name) {
|
||||||
|
return nil, nil, errors.New("bad name")
|
||||||
|
}
|
||||||
|
client := m.apiClient()
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
if m.vga == nil {
|
||||||
|
m.vga = map[string]*vgaSession{}
|
||||||
|
}
|
||||||
|
dial := func(s *vgaSession) (*websocket.Conn, error) {
|
||||||
|
c, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
u := fmt.Sprintf("ws://incus/1.0/operations/%s/websocket?secret=%s", s.op, s.secret)
|
||||||
|
ws, _, err := websocket.Dial(c, u, &websocket.DialOptions{HTTPClient: client, CompressionMode: websocket.CompressionDisabled})
|
||||||
|
return ws, err
|
||||||
|
}
|
||||||
|
s := m.vga[name]
|
||||||
|
if s != nil {
|
||||||
|
if ws, err := dial(s); err == nil {
|
||||||
|
s.refs++
|
||||||
|
ws.SetReadLimit(64 << 20)
|
||||||
|
return ws, m.releaser(name, s), nil
|
||||||
|
}
|
||||||
|
// the operation is gone (the VM restarted, or Incus did): start over
|
||||||
|
s.cancel()
|
||||||
|
delete(m.vga, name)
|
||||||
|
}
|
||||||
|
s, err = m.openVGA(ctx, client, name, false)
|
||||||
|
if err != nil && strings.Contains(err.Error(), "already connected") {
|
||||||
|
// a session a previous Ham Lab process held and never let go of; take it over
|
||||||
|
s, err = m.openVGA(ctx, client, name, true)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
ws, err := dial(s)
|
||||||
|
if err != nil {
|
||||||
|
s.cancel()
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
ws.SetReadLimit(64 << 20)
|
||||||
|
s.refs = 1
|
||||||
|
m.vga[name] = s
|
||||||
|
return ws, m.releaser(name, s), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Manager) releaser(name string, s *vgaSession) func() {
|
||||||
|
return func() {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
s.refs--
|
||||||
|
if s.refs <= 0 && m.vga[name] == s {
|
||||||
|
s.cancel()
|
||||||
|
delete(m.vga, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// openVGA asks Incus for the console operation and holds its control websocket, which is
|
||||||
|
// what keeps the operation alive.
|
||||||
|
func (m *Manager) openVGA(ctx context.Context, client *http.Client, name string, force bool) (*vgaSession, error) {
|
||||||
|
body, _ := json.Marshal(map[string]any{"type": "vga", "width": 0, "height": 0, "force": force})
|
||||||
|
c, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
req, _ := http.NewRequestWithContext(c, "POST", "http://incus/1.0/instances/"+name+"/console", bytes.NewReader(body))
|
||||||
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
if isPermission(err) {
|
||||||
|
return nil, errors.New("this account can't reach Incus's socket; Finish setup on the Virtualization tab puts Ham Lab in incus-admin")
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
var op struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
Metadata struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Metadata struct {
|
||||||
|
FDs map[string]string `json:"fds"`
|
||||||
|
} `json:"metadata"`
|
||||||
|
} `json:"metadata"`
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(&op); err != nil {
|
||||||
|
return nil, errors.New("unreadable answer from incus")
|
||||||
|
}
|
||||||
|
if op.Error != "" {
|
||||||
|
return nil, errors.New(op.Error)
|
||||||
|
}
|
||||||
|
secret, controlSecret := op.Metadata.Metadata.FDs["0"], op.Metadata.Metadata.FDs["control"]
|
||||||
|
if op.Metadata.ID == "" || secret == "" {
|
||||||
|
return nil, errors.New("incus gave no console; is the VM running?")
|
||||||
|
}
|
||||||
|
s := &vgaSession{op: op.Metadata.ID, secret: secret}
|
||||||
|
// the control websocket lives outside any one request: it is the session
|
||||||
|
sctx, scancel := context.WithCancel(context.Background())
|
||||||
|
s.cancel = scancel
|
||||||
|
if controlSecret != "" {
|
||||||
|
u := fmt.Sprintf("ws://incus/1.0/operations/%s/websocket?secret=%s", s.op, controlSecret)
|
||||||
|
control, _, err := websocket.Dial(sctx, u, &websocket.DialOptions{HTTPClient: client, CompressionMode: websocket.CompressionDisabled})
|
||||||
|
if err != nil {
|
||||||
|
scancel()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
s.control = control
|
||||||
|
go func() { // drain it; when it ends, so has the operation
|
||||||
|
for {
|
||||||
|
if _, _, err := control.Read(sctx); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
control.Close(websocket.StatusNormalClosure, "")
|
||||||
|
}()
|
||||||
|
go func() { <-sctx.Done(); control.Close(websocket.StatusNormalClosure, "") }()
|
||||||
|
}
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func isPermission(err error) bool {
|
||||||
|
var pe *os.PathError
|
||||||
|
if errors.As(err, &pe) {
|
||||||
|
return os.IsPermission(pe)
|
||||||
|
}
|
||||||
|
var oe *net.OpError
|
||||||
|
if errors.As(err, &oe) {
|
||||||
|
return os.IsPermission(oe.Err) || isPermission(oe.Err)
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,218 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
bolt "go.etcd.io/bbolt"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cache keeps finished previews in one database file under Ham Lab's own data directory —
|
||||||
|
// thumbs.db, a bbolt key/value store: pure Go, so the binary stays static, and one file
|
||||||
|
// per machine rather than a tree of hash-named JPEGs. An entry is one file's preview keyed
|
||||||
|
// by its path, size and mtime, so a file that changes gets a new preview and an unchanged
|
||||||
|
// one is never decoded twice. A file ffmpeg could not decode is kept the same way, with
|
||||||
|
// ffmpeg's reason and no picture, so a folder of odd files does not re-run ffmpeg on every
|
||||||
|
// visit. Nothing expires: a preview is a few kilobytes, and only files somebody actually
|
||||||
|
// scrolled past are ever made. The file is opened the first time something is stored, so a
|
||||||
|
// machine nobody browses with previews on never has one.
|
||||||
|
type Cache struct {
|
||||||
|
Path string
|
||||||
|
mu sync.Mutex
|
||||||
|
db *bolt.DB
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Entry is one cached preview, or the remembered reason there is none.
|
||||||
|
type Entry struct {
|
||||||
|
Path string `json:"path"`
|
||||||
|
Engine string `json:"engine,omitempty"`
|
||||||
|
Codec string `json:"codec,omitempty"`
|
||||||
|
Width int `json:"width,omitempty"`
|
||||||
|
Height int `json:"height,omitempty"`
|
||||||
|
Made time.Time `json:"made"`
|
||||||
|
Failure string `json:"failure,omitempty"` // set instead of JPEG when ffmpeg refused the file
|
||||||
|
JPEG []byte `json:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
bucketThumbs = []byte("thumbs")
|
||||||
|
bucketMeta = []byte("meta")
|
||||||
|
keyCount = []byte("count")
|
||||||
|
)
|
||||||
|
|
||||||
|
// Key identifies one file's preview. "v1" is the preview format: bump it to retire every
|
||||||
|
// cached preview at once.
|
||||||
|
func Key(path string, size, mtime int64) string {
|
||||||
|
h := sha256.Sum256([]byte("v1\x00" + path + "\x00" + strconv.FormatInt(size, 10) + "\x00" + strconv.FormatInt(mtime, 10)))
|
||||||
|
return hex.EncodeToString(h[:])
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCache(path string) *Cache { return &Cache{Path: path} }
|
||||||
|
|
||||||
|
// open the database once; an open that failed is not retried, and previews still get made
|
||||||
|
// and served — they are simply not kept, and the log says why.
|
||||||
|
func (c *Cache) open() (*bolt.DB, error) {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if c.db != nil || c.err != nil {
|
||||||
|
return c.db, c.err
|
||||||
|
}
|
||||||
|
db, err := bolt.Open(c.Path, 0o600, &bolt.Options{Timeout: 2 * time.Second})
|
||||||
|
if err != nil {
|
||||||
|
c.err = err
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := db.Update(func(tx *bolt.Tx) error {
|
||||||
|
if _, err := tx.CreateBucketIfNotExists(bucketThumbs); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err := tx.CreateBucketIfNotExists(bucketMeta)
|
||||||
|
return err
|
||||||
|
}); err != nil {
|
||||||
|
db.Close()
|
||||||
|
c.err = err
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
c.db = db
|
||||||
|
return db, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close releases the file. Previews after it are made but not kept.
|
||||||
|
func (c *Cache) Close() error {
|
||||||
|
c.mu.Lock()
|
||||||
|
defer c.mu.Unlock()
|
||||||
|
if c.db == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
err := c.db.Close()
|
||||||
|
c.db, c.err = nil, errors.New("cache closed")
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get returns the entry for a key. A database that does not exist yet is a miss, not a reason
|
||||||
|
// to make one.
|
||||||
|
func (c *Cache) Get(key string) (Entry, bool, error) {
|
||||||
|
c.mu.Lock()
|
||||||
|
db := c.db
|
||||||
|
c.mu.Unlock()
|
||||||
|
if db == nil {
|
||||||
|
if _, err := os.Stat(c.Path); err != nil {
|
||||||
|
return Entry{}, false, nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
if db, err = c.open(); err != nil {
|
||||||
|
return Entry{}, false, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var e Entry
|
||||||
|
found := false
|
||||||
|
err := db.View(func(tx *bolt.Tx) error {
|
||||||
|
v := tx.Bucket(bucketThumbs).Get([]byte(key))
|
||||||
|
if v == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
e, err = decode(v) // copies: a value is only valid inside the transaction
|
||||||
|
found = err == nil
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
return e, found, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Put stores an entry, replacing any under the same key.
|
||||||
|
func (c *Cache) Put(key string, e Entry) error {
|
||||||
|
if len(e.JPEG) == 0 && e.Failure == "" {
|
||||||
|
return errors.New("empty preview")
|
||||||
|
}
|
||||||
|
if e.Made.IsZero() {
|
||||||
|
e.Made = time.Now()
|
||||||
|
}
|
||||||
|
db, err := c.open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
v, err := encode(e)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return db.Update(func(tx *bolt.Tx) error {
|
||||||
|
b := tx.Bucket(bucketThumbs)
|
||||||
|
k := []byte(key)
|
||||||
|
fresh := b.Get(k) == nil
|
||||||
|
if err := b.Put(k, v); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if fresh {
|
||||||
|
m := tx.Bucket(bucketMeta)
|
||||||
|
return m.Put(keyCount, be64(count(m)+1))
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stats is how many previews are kept and how big the file is.
|
||||||
|
func (c *Cache) Stats() (entries int64, bytes int64) {
|
||||||
|
st, err := os.Stat(c.Path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0
|
||||||
|
}
|
||||||
|
bytes = st.Size()
|
||||||
|
db, err := c.open()
|
||||||
|
if err != nil {
|
||||||
|
return 0, bytes
|
||||||
|
}
|
||||||
|
_ = db.View(func(tx *bolt.Tx) error { entries = count(tx.Bucket(bucketMeta)); return nil })
|
||||||
|
return entries, bytes
|
||||||
|
}
|
||||||
|
|
||||||
|
func count(m *bolt.Bucket) int64 {
|
||||||
|
if v := m.Get(keyCount); len(v) == 8 {
|
||||||
|
return int64(binary.BigEndian.Uint64(v))
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func be64(n int64) []byte {
|
||||||
|
b := make([]byte, 8)
|
||||||
|
binary.BigEndian.PutUint64(b, uint64(n))
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// A value is a 4-byte header length, the header as JSON, then the JPEG bytes.
|
||||||
|
func encode(e Entry) ([]byte, error) {
|
||||||
|
hdr, err := json.Marshal(e)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
v := make([]byte, 4+len(hdr)+len(e.JPEG))
|
||||||
|
binary.BigEndian.PutUint32(v, uint32(len(hdr)))
|
||||||
|
copy(v[4:], hdr)
|
||||||
|
copy(v[4+len(hdr):], e.JPEG)
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func decode(v []byte) (Entry, error) {
|
||||||
|
if len(v) < 4 {
|
||||||
|
return Entry{}, errors.New("short value")
|
||||||
|
}
|
||||||
|
n := int(binary.BigEndian.Uint32(v))
|
||||||
|
if 4+n > len(v) {
|
||||||
|
return Entry{}, errors.New("bad header length")
|
||||||
|
}
|
||||||
|
var e Entry
|
||||||
|
if err := json.Unmarshal(v[4:4+n], &e); err != nil {
|
||||||
|
return Entry{}, err
|
||||||
|
}
|
||||||
|
if rest := v[4+n:]; len(rest) > 0 {
|
||||||
|
e.JPEG = append([]byte(nil), rest...)
|
||||||
|
}
|
||||||
|
return e, nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
// What to run when a file is to be re-encoded for a browser that cannot play it as it is:
|
||||||
|
// the codecs and sizes on offer, the output size and bit rate a choice comes to for a
|
||||||
|
// given source, the ways this machine could do it in the order worth trying, and the
|
||||||
|
// ffmpeg argument list for one of them. All decisions, no processes, so it can be tested.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Codec is one target a stream may be re-encoded to. "copy" keeps the codec and only
|
||||||
|
// repackages: for a browser that lacks the container, not the decoder.
|
||||||
|
type Codec struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var Codecs = []Codec{{"h264", "H.264"}, {"hevc", "HEVC"}, {"av1", "AV1"}, {"copy", "Original codec, repackaged"}}
|
||||||
|
|
||||||
|
// Quality is a picture height; the bit rate follows from the size the output ends up at.
|
||||||
|
type Quality struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Label string `json:"label"`
|
||||||
|
Height int `json:"height"` // 0: the source's own
|
||||||
|
}
|
||||||
|
|
||||||
|
var Qualities = []Quality{{"original", "Original size", 0}, {"1080p", "1080p", 1080}, {"720p", "720p", 720}, {"480p", "480p", 480}}
|
||||||
|
|
||||||
|
func qualityByID(id string) (Quality, bool) {
|
||||||
|
for _, q := range Qualities {
|
||||||
|
if q.ID == id {
|
||||||
|
return q, true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Quality{}, false
|
||||||
|
}
|
||||||
|
|
||||||
|
// ValidChoice says whether a codec and quality are ones on offer — checked before any work.
|
||||||
|
func ValidChoice(codec, quality string) error {
|
||||||
|
if !codecKnown(codec) {
|
||||||
|
return fmt.Errorf("unknown codec %q", codec)
|
||||||
|
}
|
||||||
|
if _, ok := qualityByID(quality); !ok {
|
||||||
|
return fmt.Errorf("unknown quality %q", quality)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func codecKnown(id string) bool {
|
||||||
|
for _, c := range Codecs {
|
||||||
|
if c.ID == id {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// fit is the output: never larger than the source, even dimensions, and a bit rate by the
|
||||||
|
// size it comes to — a quarter more at 50 fps and up, less for the codecs that need less.
|
||||||
|
func fit(q Quality, src Info, codec string) (w, h, kbps int) {
|
||||||
|
w, h = src.Video.Width, src.Video.Height
|
||||||
|
if q.Height > 0 && h > q.Height {
|
||||||
|
w = int(math.Round(float64(w) * float64(q.Height) / float64(h)))
|
||||||
|
h = q.Height
|
||||||
|
}
|
||||||
|
w, h = w&^1, h&^1
|
||||||
|
switch {
|
||||||
|
case h >= 2160:
|
||||||
|
kbps = 20000
|
||||||
|
case h >= 1440:
|
||||||
|
kbps = 12000
|
||||||
|
case h >= 1080:
|
||||||
|
kbps = 8000
|
||||||
|
case h >= 720:
|
||||||
|
kbps = 4000
|
||||||
|
default:
|
||||||
|
kbps = 2000
|
||||||
|
}
|
||||||
|
if src.Video.FPS >= 50 {
|
||||||
|
kbps = kbps * 5 / 4
|
||||||
|
}
|
||||||
|
if codec == "hevc" || codec == "av1" {
|
||||||
|
kbps = kbps * 6 / 10
|
||||||
|
}
|
||||||
|
return w, h, kbps
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recipe is one way to make the stream: the engine's name for the record, and the ffmpeg
|
||||||
|
// arguments that go before the input, the video filter, and the video codec.
|
||||||
|
type Recipe struct {
|
||||||
|
Engine string
|
||||||
|
Pre []string
|
||||||
|
VF string
|
||||||
|
Video []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// recipes are the ways to try, best first: a card on the bus before the integrated GPU;
|
||||||
|
// on a GPU, decoding on the same card (frames never leave it) before decoding in software
|
||||||
|
// and uploading; the software encoders last. An empty list means nothing here can.
|
||||||
|
func recipes(c Caps, codec string, src Info, w, h, kbps int) []Recipe {
|
||||||
|
if codec == "copy" {
|
||||||
|
v := []string{"-c:v", "copy"}
|
||||||
|
if src.Video.Codec == "hevc" {
|
||||||
|
v = append(v, "-tag:v", "hvc1") // Safari reads hvc1, not hev1
|
||||||
|
}
|
||||||
|
return []Recipe{{Engine: "Original codec", Video: v}}
|
||||||
|
}
|
||||||
|
var out []Recipe
|
||||||
|
gpus := append([]GPU(nil), c.GPUs...)
|
||||||
|
sort.SliceStable(gpus, func(i, j int) bool { return !gpus[i].Integrated && gpus[j].Integrated })
|
||||||
|
rate := []string{"-b:v", strconv.Itoa(kbps) + "k", "-maxrate", strconv.Itoa(kbps*5/4) + "k", "-bufsize", strconv.Itoa(kbps*5/2) + "k"}
|
||||||
|
for i := range gpus {
|
||||||
|
g := &gpus[i]
|
||||||
|
if g.API != "vaapi" || !has(g.Encodes, codec) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
video := append([]string{"-c:v", codec + "_vaapi"}, rate...)
|
||||||
|
switch codec {
|
||||||
|
case "h264":
|
||||||
|
video = append(video, "-profile:v", "high")
|
||||||
|
case "hevc":
|
||||||
|
video = append(video, "-tag:v", "hvc1")
|
||||||
|
}
|
||||||
|
name := g.Name
|
||||||
|
if name == "" {
|
||||||
|
name = g.Device
|
||||||
|
}
|
||||||
|
scale := fmt.Sprintf("scale_vaapi=w=%d:h=%d:format=nv12", w, h)
|
||||||
|
if has(g.Codecs, src.Video.Codec) {
|
||||||
|
vf := scale
|
||||||
|
if src.HDR() {
|
||||||
|
vf = "tonemap_vaapi=format=nv12:t=bt709:p=bt709:m=bt709," + vf
|
||||||
|
}
|
||||||
|
out = append(out, Recipe{Engine: name + " (vaapi)", Pre: []string{"-hwaccel", "vaapi", "-hwaccel_device", g.Device, "-hwaccel_output_format", "vaapi"}, VF: vf, Video: video})
|
||||||
|
}
|
||||||
|
out = append(out, Recipe{Engine: name + " (vaapi, decoded on the CPU)", Pre: []string{"-init_hw_device", "vaapi=va:" + g.Device, "-filter_hw_device", "va"},
|
||||||
|
VF: fmt.Sprintf("format=nv12,scale=%d:%d,hwupload", w, h), Video: video})
|
||||||
|
}
|
||||||
|
swVF := fmt.Sprintf("scale=%d:%d,format=yuv420p", w, h)
|
||||||
|
if src.HDR() {
|
||||||
|
swVF = fmt.Sprintf("zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=hable,zscale=t=bt709:m=bt709,scale=%d:%d,format=yuv420p", w, h)
|
||||||
|
}
|
||||||
|
cap := []string{"-maxrate", strconv.Itoa(kbps*5/4) + "k", "-bufsize", strconv.Itoa(kbps*5/2) + "k"}
|
||||||
|
switch {
|
||||||
|
case codec == "h264" && has(c.Encoders, "libx264"):
|
||||||
|
out = append(out, Recipe{Engine: "CPU (libx264)", VF: swVF, Video: append([]string{"-c:v", "libx264", "-preset", "veryfast", "-crf", "23", "-profile:v", "high"}, cap...)})
|
||||||
|
case codec == "hevc" && has(c.Encoders, "libx265"):
|
||||||
|
out = append(out, Recipe{Engine: "CPU (libx265)", VF: swVF, Video: append([]string{"-c:v", "libx265", "-preset", "fast", "-crf", "26", "-tag:v", "hvc1"}, cap...)})
|
||||||
|
case codec == "av1" && has(c.Encoders, "libsvtav1"):
|
||||||
|
out = append(out, Recipe{Engine: "CPU (SVT-AV1)", VF: swVF, Video: append([]string{"-c:v", "libsvtav1", "-preset", "8", "-crf", "35"}, cap...)})
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// EngineFor is the engine the picker will say a codec runs on, "" when none can.
|
||||||
|
func EngineFor(c Caps, codec string) string {
|
||||||
|
if c.Error != "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if codec == "copy" {
|
||||||
|
return "Original codec"
|
||||||
|
}
|
||||||
|
r := recipes(c, codec, Info{Video: &Track{Codec: "h264", Width: 1920, Height: 1080}}, 1920, 1080, 8000)
|
||||||
|
if len(r) == 0 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return r[0].Engine
|
||||||
|
}
|
||||||
|
|
||||||
|
// hlsArgs is the whole ffmpeg argument list for one recipe: the input, the first video and
|
||||||
|
// audio streams, the picture through the recipe, sound as AAC unless it already is, a
|
||||||
|
// keyframe every segment, and an event playlist of fragmented-MP4 segments in dir.
|
||||||
|
func hlsArgs(dir, in string, src Info, r Recipe, copyCodec bool) []string {
|
||||||
|
a := []string{"-nostdin", "-hide_banner", "-loglevel", "error", "-y"}
|
||||||
|
a = append(a, r.Pre...)
|
||||||
|
a = append(a, "-i", in, "-map", "0:v:0", "-map", "0:a:0?", "-sn", "-dn")
|
||||||
|
if r.VF != "" {
|
||||||
|
a = append(a, "-vf", r.VF)
|
||||||
|
}
|
||||||
|
a = append(a, r.Video...)
|
||||||
|
switch {
|
||||||
|
case src.Audio == nil:
|
||||||
|
a = append(a, "-an")
|
||||||
|
case src.Audio.Codec == "aac":
|
||||||
|
a = append(a, "-c:a", "copy")
|
||||||
|
default:
|
||||||
|
a = append(a, "-c:a", "aac", "-b:a", "160k", "-ac", "2")
|
||||||
|
}
|
||||||
|
if !copyCodec {
|
||||||
|
fps := src.Video.FPS
|
||||||
|
if fps <= 0 {
|
||||||
|
fps = 30
|
||||||
|
}
|
||||||
|
gop := int(math.Round(fps * hlsSeconds))
|
||||||
|
if gop < 24 {
|
||||||
|
gop = 24
|
||||||
|
}
|
||||||
|
if gop > 300 {
|
||||||
|
gop = 300
|
||||||
|
}
|
||||||
|
a = append(a, "-g", strconv.Itoa(gop), "-force_key_frames", fmt.Sprintf("expr:gte(t,n_forced*%d)", hlsSeconds))
|
||||||
|
}
|
||||||
|
a = append(a, "-f", "hls", "-hls_time", strconv.Itoa(hlsSeconds), "-hls_playlist_type", "event", "-hls_segment_type", "fmp4",
|
||||||
|
"-hls_fmp4_init_filename", "init.mp4", "-hls_segment_filename", dir+"/seg%05d.m4s", "-hls_flags", "independent_segments+temp_file",
|
||||||
|
"-progress", dir+"/progress", "-stats_period", "1", dir+"/index.m3u8")
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
const hlsSeconds = 4
|
||||||
|
|
||||||
|
func has(list []string, x string) bool {
|
||||||
|
for _, v := range list {
|
||||||
|
if v == x {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func src(codec string, w, h int, fps float64) Info {
|
||||||
|
return Info{Duration: 100, Video: &Track{Codec: codec, Width: w, Height: h, FPS: fps}, Audio: &Track{Codec: "opus", Channels: 2}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFit(t *testing.T) {
|
||||||
|
q := func(id string) Quality { x, _ := qualityByID(id); return x }
|
||||||
|
for _, c := range []struct {
|
||||||
|
q string
|
||||||
|
in Info
|
||||||
|
codec string
|
||||||
|
w, h, kbs int
|
||||||
|
}{
|
||||||
|
{"1080p", src("hevc", 3840, 2160, 60), "h264", 1920, 1080, 10000},
|
||||||
|
{"original", src("hevc", 3840, 2160, 30), "h264", 3840, 2160, 20000},
|
||||||
|
{"720p", src("h264", 2292, 960, 60), "h264", 1718, 720, 5000}, // odd width made even
|
||||||
|
{"1080p", src("h264", 1280, 720, 30), "h264", 1280, 720, 4000}, // never upscaled
|
||||||
|
{"480p", src("h264", 1920, 1080, 30), "hevc", 852, 480, 1200}, // 853.3 rounds down, then even
|
||||||
|
{"original", src("av1", 640, 360, 24), "av1", 640, 360, 1200},
|
||||||
|
} {
|
||||||
|
w, h, k := fit(q(c.q), c.in, c.codec)
|
||||||
|
if w != c.w || h != c.h || k != c.kbs {
|
||||||
|
t.Errorf("%s %s %dx%d@%.0f → %dx%d %dk, want %dx%d %dk", c.q, c.codec, c.in.Video.Width, c.in.Video.Height, c.in.Video.FPS, w, h, k, c.w, c.h, c.kbs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func gridCaps() Caps {
|
||||||
|
return Caps{FFmpeg: "ffmpeg", FFprobe: "ffprobe", Encoders: []string{"aac", "libsvtav1", "libx264", "libx265"},
|
||||||
|
GPUs: []GPU{
|
||||||
|
{API: "vaapi", Device: "/dev/dri/renderD128", Name: "Intel UHD Graphics 750", Integrated: true, Codecs: []string{"h264", "hevc", "vp9"}, Encodes: []string{"h264", "hevc"}},
|
||||||
|
{API: "vaapi", Device: "/dev/dri/renderD129", Name: "Intel Arc A380", Codecs: []string{"h264", "hevc", "vp9"}, Encodes: []string{"av1", "h264", "hevc"}},
|
||||||
|
}}
|
||||||
|
}
|
||||||
|
|
||||||
|
func engineNames(r []Recipe) []string {
|
||||||
|
out := make([]string, len(r))
|
||||||
|
for i := range r {
|
||||||
|
out[i] = r[i].Engine
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRecipesOrder(t *testing.T) {
|
||||||
|
c := gridCaps()
|
||||||
|
// h264 from hevc: the card first, decoding on it; then the iGPU the same way; then the CPU
|
||||||
|
got := engineNames(recipes(c, "h264", src("hevc", 3840, 2160, 60), 1920, 1080, 10000))
|
||||||
|
want := []string{"Intel Arc A380 (vaapi)", "Intel Arc A380 (vaapi, decoded on the CPU)", "Intel UHD Graphics 750 (vaapi)", "Intel UHD Graphics 750 (vaapi, decoded on the CPU)", "CPU (libx264)"}
|
||||||
|
if strings.Join(got, "|") != strings.Join(want, "|") {
|
||||||
|
t.Fatalf("got %v", got)
|
||||||
|
}
|
||||||
|
// av1 source: neither card decodes it, so no same-card recipe, and only the Arc encodes av1
|
||||||
|
got = engineNames(recipes(c, "av1", src("av1", 1920, 1080, 30), 1920, 1080, 4800))
|
||||||
|
want = []string{"Intel Arc A380 (vaapi, decoded on the CPU)", "CPU (SVT-AV1)"}
|
||||||
|
if strings.Join(got, "|") != strings.Join(want, "|") {
|
||||||
|
t.Fatalf("av1: got %v", got)
|
||||||
|
}
|
||||||
|
// copy is one recipe, no filter, hvc1 for hevc
|
||||||
|
r := recipes(c, "copy", src("hevc", 1920, 1080, 30), 1920, 1080, 0)
|
||||||
|
if len(r) != 1 || r[0].VF != "" || !has(r[0].Video, "hvc1") || !has(r[0].Video, "copy") {
|
||||||
|
t.Fatalf("copy: %+v", r)
|
||||||
|
}
|
||||||
|
// a machine with no GPU and no x264: nothing for h264, something for av1
|
||||||
|
bare := Caps{FFmpeg: "ffmpeg", FFprobe: "ffprobe", Encoders: []string{"aac", "libsvtav1"}}
|
||||||
|
if len(recipes(bare, "h264", src("h264", 1280, 720, 30), 1280, 720, 4000)) != 0 {
|
||||||
|
t.Fatal("no x264 means no h264")
|
||||||
|
}
|
||||||
|
if EngineFor(bare, "av1") != "CPU (SVT-AV1)" || EngineFor(bare, "h264") != "" || EngineFor(bare, "copy") != "Original codec" {
|
||||||
|
t.Fatal("engineFor")
|
||||||
|
}
|
||||||
|
if EngineFor(c, "hevc") != "Intel Arc A380 (vaapi)" {
|
||||||
|
t.Fatalf("engineFor on grid: %q", EngineFor(c, "hevc"))
|
||||||
|
}
|
||||||
|
// HDR gets tone-mapped on both paths
|
||||||
|
hdr := src("hevc", 3840, 2160, 60)
|
||||||
|
hdr.Video.ColorTransfer = "smpte2084"
|
||||||
|
r = recipes(c, "h264", hdr, 1920, 1080, 10000)
|
||||||
|
if !strings.HasPrefix(r[0].VF, "tonemap_vaapi") || !strings.Contains(r[len(r)-1].VF, "tonemap=hable") {
|
||||||
|
t.Fatalf("hdr: %q … %q", r[0].VF, r[len(r)-1].VF)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHLSArgs(t *testing.T) {
|
||||||
|
in := src("h264", 1920, 1080, 60)
|
||||||
|
r := recipes(gridCaps(), "h264", in, 1920, 1080, 10000)[0]
|
||||||
|
a := strings.Join(hlsArgs("/d", "/v.mkv", in, r, false), " ")
|
||||||
|
for _, want := range []string{"-hwaccel vaapi -hwaccel_device /dev/dri/renderD129", "-i /v.mkv", "-map 0:v:0 -map 0:a:0?", "-vf scale_vaapi=w=1920:h=1080:format=nv12", "-c:v h264_vaapi -b:v 10000k -maxrate 12500k", "-c:a aac -b:a 160k -ac 2", "-g 240 -force_key_frames expr:gte(t,n_forced*4)", "-hls_segment_type fmp4", "-hls_segment_filename /d/seg%05d.m4s", "/d/index.m3u8"} {
|
||||||
|
if !strings.Contains(a, want) {
|
||||||
|
t.Errorf("missing %q in\n%s", want, a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// aac stays as it is; no audio means none; copy sets no keyframes
|
||||||
|
in.Audio.Codec = "aac"
|
||||||
|
if a := strings.Join(hlsArgs("/d", "/v", in, r, false), " "); !strings.Contains(a, "-c:a copy") {
|
||||||
|
t.Error("aac should be copied")
|
||||||
|
}
|
||||||
|
in.Audio = nil
|
||||||
|
if a := strings.Join(hlsArgs("/d", "/v", in, r, false), " "); !strings.Contains(a, " -an ") {
|
||||||
|
t.Error("no audio stream → -an")
|
||||||
|
}
|
||||||
|
cp := recipes(gridCaps(), "copy", in, 1920, 1080, 0)[0]
|
||||||
|
if a := strings.Join(hlsArgs("/d", "/v", in, cp, true), " "); strings.Contains(a, "force_key_frames") || strings.Contains(a, "-vf") {
|
||||||
|
t.Error("copy must not ask for keyframes or filter")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseProgress(t *testing.T) {
|
||||||
|
b := []byte("frame=10\nout_time_us=1000000\nspeed=2.5x\nprogress=continue\nframe=20\nout_time_us=2500000\nspeed=3.1x\nprogress=continue\n")
|
||||||
|
s, sp := parseProgress(b)
|
||||||
|
if s != 2.5 || sp != 3.1 {
|
||||||
|
t.Fatalf("got %v %v", s, sp)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestValidChoice(t *testing.T) {
|
||||||
|
if err := ValidChoice("h264", "720p"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if ValidChoice("vp8", "720p") == nil || ValidChoice("h264", "9p") == nil || ValidChoice("", "") == nil {
|
||||||
|
t.Fatal("bad choices must be refused")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,188 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
// Running one long ffmpeg: as the daemon's user directly, or as root through this binary's
|
||||||
|
// run-ffmpeg helper when the file needs root to read. The helper is told to finish by its
|
||||||
|
// standard input closing, which also happens by itself if the daemon goes away — so a
|
||||||
|
// transcode never outlives the console that asked for it.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/priv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ffproc struct {
|
||||||
|
cmd *exec.Cmd
|
||||||
|
in io.WriteCloser // the helper's stdin, root mode only
|
||||||
|
done chan struct{}
|
||||||
|
err error
|
||||||
|
tail *tailBuf
|
||||||
|
}
|
||||||
|
|
||||||
|
// startFF launches ffmpeg with args, under nice where there is one.
|
||||||
|
func startFF(c Caps, self string, root bool, args []string) (*ffproc, error) {
|
||||||
|
var argv []string
|
||||||
|
if root {
|
||||||
|
argv = priv.Wrap(append([]string{self, "run-ffmpeg"}, args...)...)
|
||||||
|
} else {
|
||||||
|
argv = append([]string{c.FFmpeg}, args...)
|
||||||
|
if c.Nice != "" {
|
||||||
|
argv = append([]string{c.Nice, "-n", "10"}, argv...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd := exec.Command(argv[0], argv[1:]...)
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
p := &ffproc{cmd: cmd, done: make(chan struct{}), tail: &tailBuf{}}
|
||||||
|
cmd.Stderr = p.tail
|
||||||
|
cmd.Stdout = io.Discard
|
||||||
|
if root {
|
||||||
|
in, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
p.in = in
|
||||||
|
}
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
p.err = cmd.Wait()
|
||||||
|
close(p.done)
|
||||||
|
}()
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// exited reports whether ffmpeg has finished, and how.
|
||||||
|
func (p *ffproc) exited() (bool, error) {
|
||||||
|
select {
|
||||||
|
case <-p.done:
|
||||||
|
return true, p.err
|
||||||
|
default:
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// end asks ffmpeg to finish and waits a little for it.
|
||||||
|
func (p *ffproc) end() {
|
||||||
|
if done, _ := p.exited(); done {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if p.in != nil {
|
||||||
|
p.in.Close()
|
||||||
|
} else {
|
||||||
|
_ = syscall.Kill(-p.cmd.Process.Pid, syscall.SIGTERM)
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-p.done:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
_ = syscall.Kill(-p.cmd.Process.Pid, syscall.SIGKILL) // a root helper ignores this; it ends on its own within its own grace
|
||||||
|
select {
|
||||||
|
case <-p.done:
|
||||||
|
case <-time.After(10 * time.Second):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// lastWords is what ffmpeg said last, for the record.
|
||||||
|
func (p *ffproc) lastWords() string { return p.tail.last() }
|
||||||
|
|
||||||
|
// tailBuf keeps the last few KB written to it.
|
||||||
|
type tailBuf struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
b []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
const tailKeep = 4096
|
||||||
|
|
||||||
|
func (t *tailBuf) Write(b []byte) (int, error) {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
t.b = append(t.b, b...)
|
||||||
|
if len(t.b) > tailKeep {
|
||||||
|
t.b = t.b[len(t.b)-tailKeep:]
|
||||||
|
}
|
||||||
|
return len(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *tailBuf) last() string {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return lastLine(string(bytes.TrimSpace(t.b)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// RunFFmpegHelper implements `hamlab run-ffmpeg <args…>`: ffmpeg as this user (root, when
|
||||||
|
// the daemon asked through sudo), finishing when stdin closes. Returns ffmpeg's exit code.
|
||||||
|
func RunFFmpegHelper(args []string, stdin io.Reader, stderr io.Writer) int {
|
||||||
|
ffmpeg, err := exec.LookPath("ffmpeg")
|
||||||
|
if err != nil {
|
||||||
|
io.WriteString(stderr, "ffmpeg is not installed\n")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
argv := []string{ffmpeg}
|
||||||
|
if nice, err := exec.LookPath("nice"); err == nil {
|
||||||
|
argv = []string{nice, "-n", "10", ffmpeg}
|
||||||
|
}
|
||||||
|
cmd := exec.Command(argv[0], append(argv[1:], args...)...)
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
|
cmd.Stderr = stderr
|
||||||
|
cmd.Stdout = io.Discard
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
io.WriteString(stderr, err.Error()+"\n")
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
go func() {
|
||||||
|
_, _ = io.Copy(io.Discard, stdin) // returns when the daemon lets go
|
||||||
|
cancel()
|
||||||
|
}()
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() { done <- cmd.Wait() }()
|
||||||
|
var werr error
|
||||||
|
select {
|
||||||
|
case werr = <-done:
|
||||||
|
case <-ctx.Done():
|
||||||
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGTERM)
|
||||||
|
select {
|
||||||
|
case werr = <-done:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||||
|
werr = <-done
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var ee *exec.ExitError
|
||||||
|
if errors.As(werr, &ee) {
|
||||||
|
return ee.ExitCode()
|
||||||
|
}
|
||||||
|
if werr != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// clearDir empties a folder without removing it.
|
||||||
|
func clearDir(dir string) error {
|
||||||
|
ents, err := os.ReadDir(dir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
for _, e := range ents {
|
||||||
|
if err := os.RemoveAll(dir + "/" + e.Name()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasSegments(playlist string) bool {
|
||||||
|
b, err := os.ReadFile(playlist)
|
||||||
|
return err == nil && strings.Contains(string(b), "#EXTINF")
|
||||||
|
}
|
||||||
@@ -0,0 +1,344 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
// Playing what the browser cannot play as it is: an HLS stream, re-encoded on demand by
|
||||||
|
// ffmpeg on the machine's own GPU where it has one it can use, on its CPU otherwise. A
|
||||||
|
// stream is one file at one codec and size, in a folder of its own under
|
||||||
|
// <data dir>/transcode/: ffmpeg writes an event playlist and fragmented-MP4 segments there
|
||||||
|
// as fast as it can, and the player reads them as they land, so the range it can seek in
|
||||||
|
// grows until the whole file is through — on a GPU, several times faster than watching.
|
||||||
|
// Nothing is kept: a stream nobody has touched for a while is ended and its folder
|
||||||
|
// removed, and the folder is emptied when the daemon starts.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Stream is one re-encode, in progress or finished.
|
||||||
|
type Stream struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Path string `json:"path"`
|
||||||
|
Codec string `json:"codec"`
|
||||||
|
Quality string `json:"quality"`
|
||||||
|
Engine string `json:"engine"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
Kbps int `json:"kbps"`
|
||||||
|
Source Info `json:"source"`
|
||||||
|
Started time.Time `json:"started"`
|
||||||
|
Root bool `json:"root"`
|
||||||
|
|
||||||
|
key string
|
||||||
|
dir string
|
||||||
|
mu sync.Mutex
|
||||||
|
used time.Time
|
||||||
|
proc *ffproc
|
||||||
|
finished bool
|
||||||
|
failure string
|
||||||
|
gone bool // forgotten by the Streamer
|
||||||
|
}
|
||||||
|
|
||||||
|
// StreamStatus is what the player asks for while the file is still going through.
|
||||||
|
type StreamStatus struct {
|
||||||
|
Running bool `json:"running"`
|
||||||
|
Done bool `json:"done"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
Progress float64 `json:"progress"` // seconds of output written
|
||||||
|
Speed float64 `json:"speed"` // × realtime
|
||||||
|
Segments int `json:"segments"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Streamer keeps one machine's streams.
|
||||||
|
type Streamer struct {
|
||||||
|
dir string
|
||||||
|
self string
|
||||||
|
limit int
|
||||||
|
mu sync.Mutex
|
||||||
|
streams map[string]*Stream
|
||||||
|
}
|
||||||
|
|
||||||
|
const streamIdle = 10 * time.Minute
|
||||||
|
const firstSegmentWait = 40 * time.Second
|
||||||
|
|
||||||
|
var streamFile = regexp.MustCompile(`^(index\.m3u8|init\.mp4|seg\d{5}\.m4s)$`)
|
||||||
|
|
||||||
|
// NewStreamer empties the folder — a daemon restart orphans every stream — and starts the
|
||||||
|
// sweeper.
|
||||||
|
func NewStreamer(dataDir, self string, limit int) *Streamer {
|
||||||
|
m := &Streamer{dir: filepath.Join(dataDir, "transcode"), self: self, limit: limit, streams: map[string]*Stream{}}
|
||||||
|
_ = os.MkdirAll(m.dir, 0o755)
|
||||||
|
_ = clearDir(m.dir)
|
||||||
|
go func() {
|
||||||
|
for range time.Tick(30 * time.Second) {
|
||||||
|
m.Sweep(streamIdle)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open starts a stream, or hands back the one already serving the same request. It
|
||||||
|
// returns once the first segment is on disk, so the player's first fetch is answered.
|
||||||
|
func (m *Streamer) Open(ctx context.Context, c Caps, root bool, path, codec, quality string, src Info) (*Stream, error) {
|
||||||
|
if c.Error != "" {
|
||||||
|
return nil, errors.New(c.Error)
|
||||||
|
}
|
||||||
|
if err := ValidChoice(codec, quality); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
q, _ := qualityByID(quality)
|
||||||
|
if src.Video == nil {
|
||||||
|
return nil, &DecodeError{Msg: "no video stream"}
|
||||||
|
}
|
||||||
|
if src.Video.Width <= 0 || src.Video.Height <= 0 {
|
||||||
|
return nil, &DecodeError{Msg: "the picture's size is unknown"}
|
||||||
|
}
|
||||||
|
key := fmt.Sprintf("%s|%d|%.0f|%s|%s|%v", path, src.Size, src.Duration, codec, quality, root)
|
||||||
|
m.mu.Lock()
|
||||||
|
for _, s := range m.streams {
|
||||||
|
if s.key == key {
|
||||||
|
if s.failed() { // not the stream to hand back: let it go and make a fresh one
|
||||||
|
delete(m.streams, s.ID)
|
||||||
|
_ = os.RemoveAll(s.dir)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
m.mu.Unlock()
|
||||||
|
s.touch()
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
running := 0
|
||||||
|
for _, s := range m.streams {
|
||||||
|
if s.running() {
|
||||||
|
running++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if running >= m.limit {
|
||||||
|
m.mu.Unlock()
|
||||||
|
return nil, fmt.Errorf("%d videos are already being re-encoded on this machine; one must finish or be closed first", running)
|
||||||
|
}
|
||||||
|
s := &Stream{ID: newID(), Path: path, Codec: codec, Quality: quality, Source: src, Started: time.Now(), Root: root, key: key, used: time.Now()}
|
||||||
|
s.dir = filepath.Join(m.dir, s.ID)
|
||||||
|
m.streams[s.ID] = s
|
||||||
|
m.mu.Unlock()
|
||||||
|
|
||||||
|
s.Width, s.Height, s.Kbps = fit(q, src, codec)
|
||||||
|
if codec == "copy" {
|
||||||
|
s.Width, s.Height, s.Kbps = src.Video.Width, src.Video.Height, int(src.BitRate/1000)
|
||||||
|
}
|
||||||
|
rs := recipes(c, codec, src, s.Width, s.Height, s.Kbps)
|
||||||
|
if len(rs) == 0 {
|
||||||
|
m.forget(s)
|
||||||
|
return nil, errors.New("nothing on this machine encodes " + codec)
|
||||||
|
}
|
||||||
|
var last string
|
||||||
|
for _, r := range rs {
|
||||||
|
if err := os.MkdirAll(s.dir, 0o755); err != nil { // the root helper's ffmpeg writes here too
|
||||||
|
m.forget(s)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_ = clearDir(s.dir)
|
||||||
|
args := hlsArgs(s.dir, path, src, r, codec == "copy")
|
||||||
|
p, err := startFF(c, m.self, root, args)
|
||||||
|
if err != nil {
|
||||||
|
last = r.Engine + ": " + err.Error()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
s.proc, s.Engine = p, r.Engine
|
||||||
|
s.mu.Unlock()
|
||||||
|
msg, ok := s.waitFirstSegment(ctx)
|
||||||
|
if ok {
|
||||||
|
go s.watch()
|
||||||
|
log.Printf("media: stream %s: %s → %s %s %dx%d %dk on %s", s.ID, filepath.Base(path), codec, quality, s.Width, s.Height, s.Kbps, r.Engine)
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
p.end()
|
||||||
|
if ctx.Err() != nil || s.closed() { // the caller left, or somebody closed it meanwhile: not the recipe's fault
|
||||||
|
m.forget(s)
|
||||||
|
return nil, errors.New("cancelled")
|
||||||
|
}
|
||||||
|
last = r.Engine + ": " + msg
|
||||||
|
log.Printf("media: stream %s on %s: %s", filepath.Base(path), r.Engine, msg)
|
||||||
|
}
|
||||||
|
m.forget(s)
|
||||||
|
return nil, &DecodeError{Msg: last}
|
||||||
|
}
|
||||||
|
|
||||||
|
// waitFirstSegment watches for the playlist's first segment, or ffmpeg giving up.
|
||||||
|
func (s *Stream) waitFirstSegment(ctx context.Context) (string, bool) {
|
||||||
|
deadline := time.Now().Add(firstSegmentWait)
|
||||||
|
playlist := filepath.Join(s.dir, "index.m3u8")
|
||||||
|
for {
|
||||||
|
if hasSegments(playlist) {
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
if done, err := s.proc.exited(); done {
|
||||||
|
msg := s.proc.lastWords()
|
||||||
|
if msg == "" && err != nil {
|
||||||
|
msg = err.Error()
|
||||||
|
}
|
||||||
|
if msg == "" {
|
||||||
|
msg = "ffmpeg ended before writing a segment"
|
||||||
|
}
|
||||||
|
return msg, false
|
||||||
|
}
|
||||||
|
if time.Now().After(deadline) {
|
||||||
|
return "no segment after " + firstSegmentWait.String(), false
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err().Error(), false
|
||||||
|
case <-time.After(250 * time.Millisecond):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// watch records how the encode ended.
|
||||||
|
func (s *Stream) watch() {
|
||||||
|
<-s.proc.done
|
||||||
|
s.mu.Lock()
|
||||||
|
s.finished = true
|
||||||
|
if s.proc.err != nil {
|
||||||
|
s.failure = s.proc.lastWords()
|
||||||
|
if s.failure == "" {
|
||||||
|
s.failure = s.proc.err.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
if s.failure != "" {
|
||||||
|
log.Printf("media: stream %s ended early: %s", s.ID, s.failure)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Streamer) Find(id string) (*Stream, bool) {
|
||||||
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
s, ok := m.streams[id]
|
||||||
|
return s, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close ends a stream and removes its folder.
|
||||||
|
func (m *Streamer) Close(id string) {
|
||||||
|
m.mu.Lock()
|
||||||
|
s, ok := m.streams[id]
|
||||||
|
m.mu.Unlock()
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
s.mu.Lock()
|
||||||
|
p := s.proc
|
||||||
|
s.mu.Unlock()
|
||||||
|
if p != nil {
|
||||||
|
p.end()
|
||||||
|
}
|
||||||
|
m.forget(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Streamer) forget(s *Stream) {
|
||||||
|
m.mu.Lock()
|
||||||
|
delete(m.streams, s.ID)
|
||||||
|
m.mu.Unlock()
|
||||||
|
s.mu.Lock()
|
||||||
|
s.gone = true
|
||||||
|
s.mu.Unlock()
|
||||||
|
_ = os.RemoveAll(s.dir)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) closed() bool { s.mu.Lock(); defer s.mu.Unlock(); return s.gone }
|
||||||
|
|
||||||
|
func (s *Stream) failed() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
return s.finished && s.failure != ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sweep closes what nobody has touched for a while.
|
||||||
|
func (m *Streamer) Sweep(idle time.Duration) {
|
||||||
|
m.mu.Lock()
|
||||||
|
var old []string
|
||||||
|
for id, s := range m.streams {
|
||||||
|
s.mu.Lock()
|
||||||
|
if time.Since(s.used) > idle {
|
||||||
|
old = append(old, id)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
}
|
||||||
|
m.mu.Unlock()
|
||||||
|
for _, id := range old {
|
||||||
|
log.Printf("media: stream %s idle, closed", id)
|
||||||
|
m.Close(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// File is the on-disk path of one of the stream's files — the playlist, the init segment
|
||||||
|
// or a media segment — and nothing else.
|
||||||
|
func (s *Stream) File(name string) (string, bool) {
|
||||||
|
if !streamFile.MatchString(name) {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
s.touch()
|
||||||
|
return filepath.Join(s.dir, name), true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Stream) touch() { s.mu.Lock(); s.used = time.Now(); s.mu.Unlock() }
|
||||||
|
|
||||||
|
func (s *Stream) running() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
return s.proc != nil && !s.finished
|
||||||
|
}
|
||||||
|
|
||||||
|
// Status is read from ffmpeg's own progress file and the playlist.
|
||||||
|
func (s *Stream) Status() StreamStatus {
|
||||||
|
s.mu.Lock()
|
||||||
|
st := StreamStatus{Running: s.proc != nil && !s.finished, Done: s.finished && s.failure == "", Error: s.failure}
|
||||||
|
s.mu.Unlock()
|
||||||
|
if b, err := os.ReadFile(filepath.Join(s.dir, "progress")); err == nil {
|
||||||
|
st.Progress, st.Speed = parseProgress(b)
|
||||||
|
}
|
||||||
|
if b, err := os.ReadFile(filepath.Join(s.dir, "index.m3u8")); err == nil {
|
||||||
|
st.Segments = strings.Count(string(b), "#EXTINF")
|
||||||
|
}
|
||||||
|
return st
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseProgress takes the last out_time_us and speed from ffmpeg's -progress output.
|
||||||
|
func parseProgress(b []byte) (seconds, speed float64) {
|
||||||
|
sc := bufio.NewScanner(strings.NewReader(string(b)))
|
||||||
|
for sc.Scan() {
|
||||||
|
k, v, ok := strings.Cut(sc.Text(), "=")
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
v = strings.TrimSpace(v)
|
||||||
|
switch k {
|
||||||
|
case "out_time_us":
|
||||||
|
if us, err := strconv.ParseInt(v, 10, 64); err == nil && us >= 0 {
|
||||||
|
seconds = float64(us) / 1e6
|
||||||
|
}
|
||||||
|
case "speed":
|
||||||
|
if x, err := strconv.ParseFloat(strings.TrimSuffix(v, "x"), 64); err == nil {
|
||||||
|
speed = x
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return seconds, speed
|
||||||
|
}
|
||||||
|
|
||||||
|
func newID() string {
|
||||||
|
b := make([]byte, 8)
|
||||||
|
_, _ = rand.Read(b)
|
||||||
|
return hex.EncodeToString(b)
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The real thing, when ffmpeg is here: a generated clip goes through as H.264 and comes out
|
||||||
|
// as a playlist of segments, the status follows it to the end, and closing removes it all.
|
||||||
|
func TestStreamWithFFmpeg(t *testing.T) {
|
||||||
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||||
|
t.Skip("no ffmpeg")
|
||||||
|
}
|
||||||
|
c := Probe(context.Background())
|
||||||
|
if c.Error != "" {
|
||||||
|
t.Skip(c.Error)
|
||||||
|
}
|
||||||
|
if EngineFor(c, "h264") == "" {
|
||||||
|
t.Skip("nothing here encodes h264")
|
||||||
|
}
|
||||||
|
dir := t.TempDir()
|
||||||
|
clip := filepath.Join(dir, "clip.mp4")
|
||||||
|
out, err := exec.Command("ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "testsrc=size=640x360:rate=25:duration=9", "-f", "lavfi", "-i", "sine=frequency=440:duration=9",
|
||||||
|
"-c:v", "mpeg4", "-q:v", "5", "-c:a", "aac", "-shortest", clip).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("could not make a clip: %s", out)
|
||||||
|
}
|
||||||
|
src, err := Inspect(context.Background(), c, clip, false)
|
||||||
|
if err != nil || src.Video == nil || src.Audio == nil || src.Video.Codec != "mpeg4" || src.Duration < 8 {
|
||||||
|
t.Fatalf("inspect: %+v %v", src, err)
|
||||||
|
}
|
||||||
|
m := NewStreamer(dir, "hamlab", 2)
|
||||||
|
s, err := m.Open(context.Background(), c, false, clip, "h264", "480p", src)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if s.Width != 640 || s.Height != 360 || s.Engine == "" {
|
||||||
|
t.Fatalf("stream: %+v", s)
|
||||||
|
}
|
||||||
|
if p, ok := s.File("index.m3u8"); !ok {
|
||||||
|
t.Fatal("no playlist")
|
||||||
|
} else if !hasSegments(p) {
|
||||||
|
t.Fatal("Open returned before the first segment")
|
||||||
|
}
|
||||||
|
for _, bad := range []string{"../x", "progress", "seg1.m4s", "index.m3u8/", "init.mp4.tmp"} {
|
||||||
|
if _, ok := s.File(bad); ok {
|
||||||
|
t.Fatalf("%q must not be served", bad)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// the same request is the same stream
|
||||||
|
if again, err := m.Open(context.Background(), c, false, clip, "h264", "480p", src); err != nil || again.ID != s.ID {
|
||||||
|
t.Fatal("not reused")
|
||||||
|
}
|
||||||
|
var st StreamStatus
|
||||||
|
for i := 0; i < 120; i++ {
|
||||||
|
st = s.Status()
|
||||||
|
if st.Done || st.Error != "" {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
time.Sleep(250 * time.Millisecond)
|
||||||
|
}
|
||||||
|
if !st.Done || st.Error != "" || st.Segments < 2 || st.Progress < 8 {
|
||||||
|
t.Fatalf("status at the end: %+v", st)
|
||||||
|
}
|
||||||
|
if init, _ := s.File("init.mp4"); !exists(init) {
|
||||||
|
t.Fatal("no init segment")
|
||||||
|
}
|
||||||
|
m.Close(s.ID)
|
||||||
|
if _, ok := m.Find(s.ID); ok || exists(s.dir) {
|
||||||
|
t.Fatal("close must forget and remove")
|
||||||
|
}
|
||||||
|
// an unplayable source is a DecodeError, and leaves nothing behind
|
||||||
|
bad := filepath.Join(dir, "bad.mkv")
|
||||||
|
_ = os.WriteFile(bad, []byte("not a video"), 0o644)
|
||||||
|
if _, err := Inspect(context.Background(), c, bad, false); err == nil {
|
||||||
|
t.Fatal("inspect of garbage")
|
||||||
|
}
|
||||||
|
if left, _ := os.ReadDir(m.dir); len(left) != 0 {
|
||||||
|
t.Fatalf("left behind: %v", left)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"hamlab/internal/priv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Track is one stream of a media file, as ffprobe reports it.
|
||||||
|
type Track struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
Type string `json:"type"` // video | audio
|
||||||
|
Codec string `json:"codec"`
|
||||||
|
Profile string `json:"profile,omitempty"`
|
||||||
|
PixFmt string `json:"pix_fmt,omitempty"`
|
||||||
|
Width int `json:"width,omitempty"`
|
||||||
|
Height int `json:"height,omitempty"`
|
||||||
|
FPS float64 `json:"fps,omitempty"`
|
||||||
|
ColorTransfer string `json:"color_transfer,omitempty"` // smpte2084 / arib-std-b67 mean HDR
|
||||||
|
Channels int `json:"channels,omitempty"`
|
||||||
|
AttachedPic bool `json:"attached_pic,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Info is what a file holds: the picture to take, the sound to go with it, and the whole.
|
||||||
|
type Info struct {
|
||||||
|
Container string `json:"container"` // "matroska,webm", "mov,mp4,m4a,3gp,3g2,mj2"
|
||||||
|
Duration float64 `json:"duration"`
|
||||||
|
BitRate int64 `json:"bit_rate,omitempty"` // bits per second, whole file
|
||||||
|
Size int64 `json:"size,omitempty"`
|
||||||
|
Video *Track `json:"video,omitempty"` // the first real picture stream; cover art is not it
|
||||||
|
Audio *Track `json:"audio,omitempty"` // the first sound stream
|
||||||
|
}
|
||||||
|
|
||||||
|
// HDR reports a picture that needs tone mapping to look right on an SDR screen.
|
||||||
|
func (i Info) HDR() bool {
|
||||||
|
return i.Video != nil && (i.Video.ColorTransfer == "smpte2084" || i.Video.ColorTransfer == "arib-std-b67")
|
||||||
|
}
|
||||||
|
|
||||||
|
type probeJSON struct {
|
||||||
|
Streams []struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
CodecType string `json:"codec_type"`
|
||||||
|
CodecName string `json:"codec_name"`
|
||||||
|
Profile string `json:"profile"`
|
||||||
|
PixFmt string `json:"pix_fmt"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
RFrameRate string `json:"r_frame_rate"`
|
||||||
|
AvgFrameRate string `json:"avg_frame_rate"`
|
||||||
|
ColorTransfer string `json:"color_transfer"`
|
||||||
|
Channels int `json:"channels"`
|
||||||
|
Disposition struct {
|
||||||
|
AttachedPic int `json:"attached_pic"`
|
||||||
|
} `json:"disposition"`
|
||||||
|
} `json:"streams"`
|
||||||
|
Format struct {
|
||||||
|
FormatName string `json:"format_name"`
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
BitRate string `json:"bit_rate"`
|
||||||
|
Size string `json:"size"`
|
||||||
|
} `json:"format"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Inspect asks ffprobe what a file is. As root through sudo when asked, for a file only
|
||||||
|
// root may read. An error about the file itself is a DecodeError.
|
||||||
|
func Inspect(ctx context.Context, c Caps, path string, root bool) (Info, error) {
|
||||||
|
if c.FFprobe == "" {
|
||||||
|
return Info{}, errors.New("ffprobe is not installed")
|
||||||
|
}
|
||||||
|
argv := []string{c.FFprobe, "-v", "error", "-show_entries",
|
||||||
|
"stream=index,codec_type,codec_name,profile,pix_fmt,width,height,r_frame_rate,avg_frame_rate,color_transfer,channels,disposition:format=format_name,duration,bit_rate,size",
|
||||||
|
"-of", "json", path}
|
||||||
|
if root {
|
||||||
|
argv = priv.Wrap(argv...)
|
||||||
|
}
|
||||||
|
cmd := exec.CommandContext(ctx, argv[0], argv[1:]...)
|
||||||
|
var errb bytes.Buffer
|
||||||
|
cmd.Stderr = &errb
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return Info{}, ctx.Err()
|
||||||
|
}
|
||||||
|
msg := lastLine(errb.String())
|
||||||
|
if msg == "" {
|
||||||
|
msg = "ffprobe: " + err.Error()
|
||||||
|
}
|
||||||
|
return Info{}, &DecodeError{Msg: msg}
|
||||||
|
}
|
||||||
|
var pj probeJSON
|
||||||
|
if err := json.Unmarshal(out, &pj); err != nil {
|
||||||
|
return Info{}, &DecodeError{Msg: "ffprobe: unreadable answer"}
|
||||||
|
}
|
||||||
|
in := Info{Container: pj.Format.FormatName}
|
||||||
|
in.Duration, _ = strconv.ParseFloat(pj.Format.Duration, 64)
|
||||||
|
in.BitRate, _ = strconv.ParseInt(pj.Format.BitRate, 10, 64)
|
||||||
|
in.Size, _ = strconv.ParseInt(pj.Format.Size, 10, 64)
|
||||||
|
var art *Track
|
||||||
|
for _, s := range pj.Streams {
|
||||||
|
st := &Track{Index: s.Index, Type: s.CodecType, Codec: s.CodecName, Profile: s.Profile, PixFmt: s.PixFmt, Width: s.Width, Height: s.Height,
|
||||||
|
ColorTransfer: s.ColorTransfer, Channels: s.Channels, AttachedPic: s.Disposition.AttachedPic != 0}
|
||||||
|
st.FPS = fps(s.AvgFrameRate)
|
||||||
|
if st.FPS == 0 {
|
||||||
|
st.FPS = fps(s.RFrameRate)
|
||||||
|
}
|
||||||
|
switch {
|
||||||
|
case s.CodecType == "video" && st.AttachedPic:
|
||||||
|
if art == nil {
|
||||||
|
art = st
|
||||||
|
}
|
||||||
|
case s.CodecType == "video" && in.Video == nil:
|
||||||
|
in.Video = st
|
||||||
|
case s.CodecType == "audio" && in.Audio == nil:
|
||||||
|
in.Audio = st
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if in.Video == nil {
|
||||||
|
in.Video = art // cover art is the only picture there is
|
||||||
|
}
|
||||||
|
return in, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fps parses ffprobe's "60000/1001".
|
||||||
|
func fps(r string) float64 {
|
||||||
|
n, d, ok := strings.Cut(r, "/")
|
||||||
|
if !ok {
|
||||||
|
v, _ := strconv.ParseFloat(r, 64)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
nf, _ := strconv.ParseFloat(n, 64)
|
||||||
|
df, _ := strconv.ParseFloat(d, 64)
|
||||||
|
if df == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return nf / df
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/user"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ownerOf is "root:render 0660" for a device node, the thing to say when it cannot be opened.
|
||||||
|
func ownerOf(st os.FileInfo) string {
|
||||||
|
sys, ok := st.Sys().(*syscall.Stat_t)
|
||||||
|
if !ok {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
u := strconv.Itoa(int(sys.Uid))
|
||||||
|
if x, err := user.LookupId(u); err == nil {
|
||||||
|
u = x.Username
|
||||||
|
}
|
||||||
|
g := strconv.Itoa(int(sys.Gid))
|
||||||
|
if x, err := user.LookupGroupId(g); err == nil {
|
||||||
|
g = x.Name
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s:%s %04o", u, g, st.Mode().Perm())
|
||||||
|
}
|
||||||
@@ -0,0 +1,656 @@
|
|||||||
|
// Package thumbs makes the Files tab's previews: one small JPEG for an image or a video,
|
||||||
|
// by ffmpeg, decoded on a GPU where the machine has one it can use and on the CPU
|
||||||
|
// otherwise.
|
||||||
|
//
|
||||||
|
// Nothing about the machine is assumed. A probe looks ffmpeg and ffprobe up, opens every
|
||||||
|
// /dev/dri/renderD* and exercises it (a VAAPI upload and download of a synthetic frame),
|
||||||
|
// so a node the daemon's user cannot open, or a driver that does not speak VAAPI, is
|
||||||
|
// reported once rather than tried per file. vainfo, where installed, says which codecs
|
||||||
|
// each node decodes, and a video goes to the first node that lists its codec; without
|
||||||
|
// vainfo the first working node is tried and ffmpeg falls back to software on its own when
|
||||||
|
// the stream turns out unsupported. A hardware run that fails outright is retried on the
|
||||||
|
// CPU before the file is called undecodable. Images are always decoded on the CPU: one
|
||||||
|
// frame of a JPEG at a reduced DCT size costs less than the trip through a GPU.
|
||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/metrics"
|
||||||
|
"hamlab/internal/priv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// GPU is one hardware decoder as the probe found it.
|
||||||
|
type GPU struct {
|
||||||
|
API string `json:"api"` // vaapi | cuda
|
||||||
|
Device string `json:"device"` // /dev/dri/renderD128; "0" for cuda (its device index)
|
||||||
|
Name string `json:"name,omitempty"` // "Intel Arc A380"
|
||||||
|
Codecs []string `json:"codecs,omitempty"` // ffmpeg codec names it decodes; nil when nothing could say
|
||||||
|
Encodes []string `json:"encodes,omitempty"` // ffmpeg codec names it encodes, from vainfo
|
||||||
|
Integrated bool `json:"integrated,omitempty"` // on the CPU die; a card on the bus is preferred for encoding
|
||||||
|
Reason string `json:"reason,omitempty"` // Unusable only: why
|
||||||
|
Permission bool `json:"permission,omitempty"` // Unusable only: the node exists but this user may not open it
|
||||||
|
}
|
||||||
|
|
||||||
|
// Caps is what the machine can make previews with, as probed.
|
||||||
|
type Caps struct {
|
||||||
|
FFmpeg string `json:"ffmpeg,omitempty"`
|
||||||
|
FFprobe string `json:"ffprobe,omitempty"`
|
||||||
|
Nice string `json:"nice,omitempty"`
|
||||||
|
Version string `json:"version,omitempty"`
|
||||||
|
GPUs []GPU `json:"gpus"` // usable, in node order
|
||||||
|
Encoders []string `json:"encoders,omitempty"` // the encoders ffmpeg itself was built with that matter here
|
||||||
|
Unusable []GPU `json:"unusable,omitempty"` // render nodes that exist but cannot be used, with the reason
|
||||||
|
Error string `json:"error,omitempty"` // why nothing can be made at all
|
||||||
|
ProbedAt time.Time `json:"probed_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Opts shapes one preview.
|
||||||
|
type Opts struct {
|
||||||
|
Size int // the long edge in pixels; 256 when zero
|
||||||
|
Timeout time.Duration // for the whole job, ffprobe and every ffmpeg attempt; 30 s when zero
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o Opts) defaults() Opts {
|
||||||
|
if o.Size <= 0 {
|
||||||
|
o.Size = 256
|
||||||
|
}
|
||||||
|
if o.Timeout <= 0 {
|
||||||
|
o.Timeout = 30 * time.Second
|
||||||
|
}
|
||||||
|
return o
|
||||||
|
}
|
||||||
|
|
||||||
|
// Result is a finished preview.
|
||||||
|
type Result struct {
|
||||||
|
JPEG []byte `json:"-"`
|
||||||
|
Engine string `json:"engine"` // "cpu", or "vaapi:/dev/dri/renderD129"
|
||||||
|
Codec string `json:"codec"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrUnsupported: not an image or a video by its name.
|
||||||
|
var ErrUnsupported = errors.New("not an image or video")
|
||||||
|
|
||||||
|
// A DecodeError is ffmpeg's verdict on the file itself. It will not change until the
|
||||||
|
// file does, so the caller may remember it.
|
||||||
|
type DecodeError struct{ Msg string }
|
||||||
|
|
||||||
|
func (e *DecodeError) Error() string { return e.Msg }
|
||||||
|
|
||||||
|
// ---- what gets a preview ----
|
||||||
|
|
||||||
|
var imageExt = set("png jpg jpeg jpe jfif gif webp bmp tif tiff avif heic heif hif jxl svg ico psd tga thm")
|
||||||
|
var videoExt = set("mp4 m4v mkv webm mov avi wmv flv mpg mpeg mpe m2v ts m2ts mts 3gp 3g2 ogv ogm mxf vob asf rm rmvb divx f4v mjpeg mjpg 360 osv insv lrf")
|
||||||
|
var audioExt = set("mp3 flac m4a m4b aac ogg oga opus wav wma aiff aif alac ape wv mka")
|
||||||
|
|
||||||
|
func set(s string) map[string]bool {
|
||||||
|
m := map[string]bool{}
|
||||||
|
for _, w := range strings.Fields(s) {
|
||||||
|
m[w] = true
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kind is "image", "video", "audio" or "" for a file name. An audio file's preview is its
|
||||||
|
// cover art, when it carries any.
|
||||||
|
func Kind(name string) string {
|
||||||
|
ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), "."))
|
||||||
|
switch {
|
||||||
|
case imageExt[ext]:
|
||||||
|
return "image"
|
||||||
|
case videoExt[ext]:
|
||||||
|
return "video"
|
||||||
|
case audioExt[ext]:
|
||||||
|
return "audio"
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- the probe ----
|
||||||
|
|
||||||
|
// Probe finds out what this process can make previews with. It runs ffmpeg against every
|
||||||
|
// render node, so it takes a few hundred milliseconds; callers keep the answer.
|
||||||
|
func Probe(ctx context.Context) Caps {
|
||||||
|
c := Caps{ProbedAt: time.Now(), GPUs: []GPU{}}
|
||||||
|
c.FFmpeg, _ = exec.LookPath("ffmpeg")
|
||||||
|
c.FFprobe, _ = exec.LookPath("ffprobe")
|
||||||
|
c.Nice, _ = exec.LookPath("nice")
|
||||||
|
switch {
|
||||||
|
case c.FFmpeg == "" && c.FFprobe == "":
|
||||||
|
c.Error = "ffmpeg is not installed"
|
||||||
|
case c.FFmpeg == "":
|
||||||
|
c.Error = "ffmpeg is not installed (ffprobe is)"
|
||||||
|
case c.FFprobe == "":
|
||||||
|
c.Error = "ffprobe is not installed (ffmpeg is)"
|
||||||
|
}
|
||||||
|
if c.Error != "" {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
c.Version = ffmpegVersion(ctx, c.FFmpeg)
|
||||||
|
c.Encoders = encoders(ctx, c.FFmpeg)
|
||||||
|
accels := hwaccels(ctx, c.FFmpeg)
|
||||||
|
if accels["vaapi"] {
|
||||||
|
nodes, _ := filepath.Glob("/dev/dri/renderD*")
|
||||||
|
sort.Strings(nodes)
|
||||||
|
vainfo, _ := exec.LookPath("vainfo")
|
||||||
|
for _, dev := range nodes {
|
||||||
|
g := GPU{API: "vaapi", Device: dev, Name: metrics.RenderNodeName(dev), Integrated: metrics.RenderNodeIntegrated(dev)}
|
||||||
|
if err := openable(dev); err != nil {
|
||||||
|
g.Reason, g.Permission = err.Error(), errors.Is(err, os.ErrPermission)
|
||||||
|
c.Unusable = append(c.Unusable, g)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if msg := hwWorks(ctx, c.FFmpeg, "vaapi=va:"+dev); msg != "" {
|
||||||
|
g.Reason = msg
|
||||||
|
c.Unusable = append(c.Unusable, g)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if vainfo != "" {
|
||||||
|
g.Codecs, g.Encodes = vainfoCodecs(ctx, vainfo, dev)
|
||||||
|
}
|
||||||
|
c.GPUs = append(c.GPUs, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if accels["cuda"] && exists("/dev/nvidiactl") {
|
||||||
|
g := GPU{API: "cuda", Device: "0", Name: "NVIDIA GPU"}
|
||||||
|
if msg := hwWorks(ctx, c.FFmpeg, "cuda=cu:0"); msg != "" {
|
||||||
|
g.Reason = msg
|
||||||
|
c.Unusable = append(c.Unusable, g)
|
||||||
|
} else {
|
||||||
|
c.GPUs = append(c.GPUs, g)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// openable: the node can be opened read-write, which is what a VAAPI driver needs. The
|
||||||
|
// usual reason it can't be is the owning group: a render node is root:render 0660 on
|
||||||
|
// most distributions, and a daemon running as an ordinary user is not in render.
|
||||||
|
func openable(dev string) error {
|
||||||
|
f, err := os.OpenFile(dev, os.O_RDWR, 0)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, os.ErrPermission) {
|
||||||
|
owner := ""
|
||||||
|
if st, serr := os.Stat(dev); serr == nil {
|
||||||
|
owner = ownerOf(st)
|
||||||
|
}
|
||||||
|
if owner != "" {
|
||||||
|
return fmt.Errorf("%w: %s is %s", os.ErrPermission, dev, owner)
|
||||||
|
}
|
||||||
|
return fmt.Errorf("%w: cannot open %s", os.ErrPermission, dev)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
f.Close()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// hwWorks initialises a hardware device in ffmpeg and moves one synthetic frame through
|
||||||
|
// it. "" when it works, else the last thing ffmpeg said.
|
||||||
|
func hwWorks(ctx context.Context, ffmpeg, device string) string {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
name, rest, _ := strings.Cut(device, "=") // "vaapi", "va:/dev/…"
|
||||||
|
label, _, _ := strings.Cut(rest, ":") // "va"
|
||||||
|
cmd := exec.CommandContext(ctx, ffmpeg, "-nostdin", "-hide_banner", "-v", "error", "-init_hw_device", device, "-filter_hw_device", label,
|
||||||
|
"-f", "lavfi", "-i", "color=c=black:s=64x64:r=1:d=1", "-vf", "format=nv12,hwupload,hwdownload,format=nv12", "-frames:v", "1", "-f", "null", "-")
|
||||||
|
var errb bytes.Buffer
|
||||||
|
cmd.Stderr = &errb
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
msg := lastLine(errb.String())
|
||||||
|
if msg == "" {
|
||||||
|
msg = err.Error()
|
||||||
|
}
|
||||||
|
return name + ": " + msg
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func ffmpegVersion(ctx context.Context, ffmpeg string) string {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
out, _ := exec.CommandContext(ctx, ffmpeg, "-version").Output()
|
||||||
|
f := strings.Fields(string(out)) // "ffmpeg version 7.1.5-0+deb13u1 Copyright …"
|
||||||
|
if len(f) >= 3 && f[1] == "version" {
|
||||||
|
v := f[2]
|
||||||
|
if i := strings.IndexAny(v, "-+~"); i > 0 {
|
||||||
|
v = v[:i]
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func hwaccels(ctx context.Context, ffmpeg string) map[string]bool {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
out, _ := exec.CommandContext(ctx, ffmpeg, "-hide_banner", "-hwaccels").Output()
|
||||||
|
m := map[string]bool{}
|
||||||
|
for _, l := range strings.Split(string(out), "\n") {
|
||||||
|
l = strings.TrimSpace(l)
|
||||||
|
if l != "" && !strings.HasPrefix(l, "Hardware acceleration methods") {
|
||||||
|
m[l] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
var vaProfileRe = regexp.MustCompile(`VAProfile([A-Za-z0-9]+?)(?:Simple|Main|High|Profile|Baseline|Constrained|Scc|Advanced|Version|Stereo|Multiview)\w*\s*:\s*VAEntrypoint(VLD|EncSlice|EncSliceLP)\b`)
|
||||||
|
|
||||||
|
// wantedEncoders are the encoders this package may ask ffmpeg for.
|
||||||
|
var wantedEncoders = set("libx264 libx265 libsvtav1 libaom-av1 libopenh264 h264_vaapi hevc_vaapi av1_vaapi aac")
|
||||||
|
|
||||||
|
// encoders lists the wanted encoders this ffmpeg was built with.
|
||||||
|
func encoders(ctx context.Context, ffmpeg string) []string {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
out, _ := exec.CommandContext(ctx, ffmpeg, "-hide_banner", "-encoders").Output()
|
||||||
|
var found []string
|
||||||
|
for _, l := range strings.Split(string(out), "\n") {
|
||||||
|
f := strings.Fields(l)
|
||||||
|
if len(f) >= 2 && wantedEncoders[f[1]] {
|
||||||
|
found = append(found, f[1])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(found)
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
|
||||||
|
// vaCodec maps a VAProfile family to the ffmpeg codec it decodes.
|
||||||
|
var vaCodec = map[string]string{"H264": "h264", "HEVC": "hevc", "VP9": "vp9", "AV1": "av1", "MPEG2": "mpeg2video", "VC1": "vc1", "VP8": "vp8", "JPEG": "mjpeg", "MPEG4": "mpeg4", "H263": "h263"}
|
||||||
|
|
||||||
|
func vainfoCodecs(ctx context.Context, vainfo, dev string) (decodes, encodes []string) {
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
out, _ := exec.CommandContext(ctx, vainfo, "--display", "drm", "--device", dev).Output()
|
||||||
|
return parseVainfo(string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseVainfo names the codecs the decode (VLD) and encode (EncSlice) profiles cover,
|
||||||
|
// the way ffmpeg names them.
|
||||||
|
func parseVainfo(text string) (decodes, encodes []string) {
|
||||||
|
dec, enc := map[string]bool{}, map[string]bool{}
|
||||||
|
for _, m := range vaProfileRe.FindAllStringSubmatch(text, -1) {
|
||||||
|
c, ok := vaCodec[m[1]]
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if m[2] == "VLD" {
|
||||||
|
dec[c] = true
|
||||||
|
} else {
|
||||||
|
enc[c] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return keys(dec), keys(enc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func keys(m map[string]bool) []string {
|
||||||
|
if len(m) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
out := make([]string, 0, len(m))
|
||||||
|
for c := range m {
|
||||||
|
out = append(out, c)
|
||||||
|
}
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// pick is the GPU for a codec: the first that lists it, else the first that could not
|
||||||
|
// say (ffmpeg will fall back to software by itself if that guess is wrong), else none.
|
||||||
|
func (c Caps) pick(codec string) *GPU {
|
||||||
|
var unknown *GPU
|
||||||
|
for i := range c.GPUs {
|
||||||
|
g := &c.GPUs[i]
|
||||||
|
if g.Codecs == nil {
|
||||||
|
if unknown == nil {
|
||||||
|
unknown = g
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, x := range g.Codecs {
|
||||||
|
if x == codec {
|
||||||
|
return g
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return unknown
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- making one ----
|
||||||
|
|
||||||
|
type fileInfo struct {
|
||||||
|
index string // the stream to take
|
||||||
|
codec string
|
||||||
|
w, h int
|
||||||
|
duration float64
|
||||||
|
}
|
||||||
|
|
||||||
|
type attempt struct {
|
||||||
|
gpu *GPU
|
||||||
|
lowres int
|
||||||
|
seek float64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a attempt) engine() string {
|
||||||
|
if a.gpu == nil {
|
||||||
|
return "cpu"
|
||||||
|
}
|
||||||
|
return a.gpu.API + ":" + a.gpu.Device
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make produces the preview of one file.
|
||||||
|
func Make(ctx context.Context, c Caps, path string, o Opts) (Result, error) {
|
||||||
|
kind := Kind(path)
|
||||||
|
if kind == "" {
|
||||||
|
return Result{}, ErrUnsupported
|
||||||
|
}
|
||||||
|
if c.Error != "" {
|
||||||
|
return Result{}, errors.New(c.Error)
|
||||||
|
}
|
||||||
|
o = o.defaults()
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, o.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
fi, err := probeFile(ctx, c, path)
|
||||||
|
if err != nil {
|
||||||
|
return Result{}, err
|
||||||
|
}
|
||||||
|
var last string
|
||||||
|
for _, eng := range engines(c, kind, fi) {
|
||||||
|
for _, a := range attempts(eng, kind, fi, o.Size) {
|
||||||
|
data, msg, err := run(ctx, c, a, path, fi, o.Size)
|
||||||
|
if err == nil && len(data) > 0 {
|
||||||
|
return Result{JPEG: data, Engine: a.engine(), Codec: fi.codec, Width: fi.w, Height: fi.h}, nil
|
||||||
|
}
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return Result{}, ctx.Err() // the clock ran out: not the file's fault
|
||||||
|
}
|
||||||
|
if err != nil { // the decoder refused: try the next engine, not another seek
|
||||||
|
last = msg
|
||||||
|
break
|
||||||
|
}
|
||||||
|
last = "no frame at " + strconv.FormatFloat(a.seek, 'f', 1, 64) + " s"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if last == "" {
|
||||||
|
last = "could not decode"
|
||||||
|
}
|
||||||
|
return Result{}, &DecodeError{Msg: last}
|
||||||
|
}
|
||||||
|
|
||||||
|
// engines is the order to try: the fitting GPU first for a video, then the CPU.
|
||||||
|
func engines(c Caps, kind string, fi fileInfo) []*GPU {
|
||||||
|
if kind == "video" {
|
||||||
|
if g := c.pick(fi.codec); g != nil {
|
||||||
|
return []*GPU{g, nil}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []*GPU{nil}
|
||||||
|
}
|
||||||
|
|
||||||
|
// attempts for one engine: a seek into a video, then from its start if that found no
|
||||||
|
// frame (a wrong duration in the header); a reduced-size decode of a JPEG, then full size.
|
||||||
|
func attempts(gpu *GPU, kind string, fi fileInfo, size int) []attempt {
|
||||||
|
if kind == "video" {
|
||||||
|
s := seekFor(fi.duration)
|
||||||
|
if s > 0 {
|
||||||
|
return []attempt{{gpu: gpu, seek: s}, {gpu: gpu}}
|
||||||
|
}
|
||||||
|
return []attempt{{gpu: gpu}}
|
||||||
|
}
|
||||||
|
if fi.codec == "mjpeg" && gpu == nil {
|
||||||
|
if l := lowresFor(fi.w, fi.h, size); l > 0 {
|
||||||
|
return []attempt{{lowres: l}, {}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []attempt{{gpu: gpu}}
|
||||||
|
}
|
||||||
|
|
||||||
|
// seekFor: a tenth of the way in, never more than 30 s, so a title card or a black lead-in
|
||||||
|
// is skipped and a long file still seeks to a keyframe near its start.
|
||||||
|
func seekFor(duration float64) float64 {
|
||||||
|
if duration <= 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
s := duration / 10
|
||||||
|
if s > 30 {
|
||||||
|
s = 30
|
||||||
|
}
|
||||||
|
if s < 0.5 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// lowresFor: how many halvings a JPEG decoder may take while the picture stays at least
|
||||||
|
// twice the preview size. libjpeg does this in the DCT, so a 24 MP photo decodes at 1.5 MP.
|
||||||
|
func lowresFor(w, h, size int) int {
|
||||||
|
m := w
|
||||||
|
if h < m {
|
||||||
|
m = h
|
||||||
|
}
|
||||||
|
l := 0
|
||||||
|
for l < 3 && m>>(l+1) >= size*2 {
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
type probeOut struct {
|
||||||
|
Streams []struct {
|
||||||
|
Index int `json:"index"`
|
||||||
|
Codec string `json:"codec_name"`
|
||||||
|
Width int `json:"width"`
|
||||||
|
Height int `json:"height"`
|
||||||
|
Disposition struct {
|
||||||
|
AttachedPic int `json:"attached_pic"`
|
||||||
|
} `json:"disposition"`
|
||||||
|
} `json:"streams"`
|
||||||
|
Format struct {
|
||||||
|
Duration string `json:"duration"`
|
||||||
|
} `json:"format"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// probeFile asks ffprobe which stream to take and what is in it.
|
||||||
|
func probeFile(ctx context.Context, c Caps, path string) (fileInfo, error) {
|
||||||
|
cmd := exec.CommandContext(ctx, c.FFprobe, "-v", "error", "-select_streams", "v", "-show_entries", "stream=index,codec_name,width,height,disposition:format=duration", "-of", "json", path)
|
||||||
|
var errb bytes.Buffer
|
||||||
|
cmd.Stderr = &errb
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return fileInfo{}, ctx.Err()
|
||||||
|
}
|
||||||
|
msg := lastLine(errb.String())
|
||||||
|
if msg == "" {
|
||||||
|
msg = "ffprobe: " + err.Error()
|
||||||
|
}
|
||||||
|
return fileInfo{}, &DecodeError{Msg: msg}
|
||||||
|
}
|
||||||
|
var po probeOut
|
||||||
|
if err := json.Unmarshal(out, &po); err != nil {
|
||||||
|
return fileInfo{}, &DecodeError{Msg: "ffprobe: unreadable answer"}
|
||||||
|
}
|
||||||
|
if len(po.Streams) == 0 {
|
||||||
|
return fileInfo{}, &DecodeError{Msg: "no video stream"}
|
||||||
|
}
|
||||||
|
s := po.Streams[0]
|
||||||
|
for _, x := range po.Streams { // cover art rides along as a video stream; the picture is not the film
|
||||||
|
if x.Disposition.AttachedPic == 0 {
|
||||||
|
s = x
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fi := fileInfo{index: strconv.Itoa(s.Index), codec: s.Codec, w: s.Width, h: s.Height}
|
||||||
|
fi.duration, _ = strconv.ParseFloat(po.Format.Duration, 64)
|
||||||
|
return fi, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// run is one ffmpeg: the JPEG on success; on failure what ffmpeg said and the error.
|
||||||
|
// Empty output with a clean exit means the seek found nothing.
|
||||||
|
func run(ctx context.Context, c Caps, a attempt, path string, fi fileInfo, size int) ([]byte, string, error) {
|
||||||
|
args := []string{"-nostdin", "-hide_banner", "-loglevel", "error", "-y"}
|
||||||
|
if a.gpu != nil {
|
||||||
|
args = append(args, "-hwaccel", a.gpu.API, "-hwaccel_device", a.gpu.Device)
|
||||||
|
}
|
||||||
|
if a.lowres > 0 {
|
||||||
|
args = append(args, "-lowres", strconv.Itoa(a.lowres))
|
||||||
|
}
|
||||||
|
if a.seek > 0 {
|
||||||
|
args = append(args, "-noaccurate_seek", "-ss", strconv.FormatFloat(a.seek, 'f', 3, 64))
|
||||||
|
}
|
||||||
|
args = append(args, "-i", path, "-map", "0:"+fi.index, "-frames:v", "1",
|
||||||
|
"-vf", fmt.Sprintf("scale=w=%d:h=%d:force_original_aspect_ratio=decrease:flags=bicubic", size, size),
|
||||||
|
"-f", "image2pipe", "-c:v", "mjpeg", "-q:v", "4", "pipe:1")
|
||||||
|
name, argv := c.FFmpeg, args
|
||||||
|
if c.Nice != "" { // a burst of previews must not crowd out what the machine is for
|
||||||
|
name, argv = c.Nice, append([]string{"-n", "10", c.FFmpeg}, args...)
|
||||||
|
}
|
||||||
|
cmd := exec.CommandContext(ctx, name, argv...)
|
||||||
|
var out, errb bytes.Buffer
|
||||||
|
cmd.Stdout, cmd.Stderr = &out, &errb
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
msg := lastLine(errb.String())
|
||||||
|
if msg == "" {
|
||||||
|
msg = err.Error()
|
||||||
|
}
|
||||||
|
return nil, msg, err
|
||||||
|
}
|
||||||
|
return out.Bytes(), "", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- the same, as root ----
|
||||||
|
|
||||||
|
// ProbeRoot is Probe run as root through this binary, for the Files tab's root mode: root
|
||||||
|
// opens every render node, whatever group the daemon's user is in.
|
||||||
|
func ProbeRoot(ctx context.Context, self string) (Caps, error) {
|
||||||
|
full := priv.Wrap(self, "thumb", "--probe")
|
||||||
|
cmd := exec.CommandContext(ctx, full[0], full[1:]...)
|
||||||
|
var errb bytes.Buffer
|
||||||
|
cmd.Stderr = &errb
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
if msg := lastLine(errb.String()); msg != "" {
|
||||||
|
return Caps{}, errors.New(msg)
|
||||||
|
}
|
||||||
|
return Caps{}, err
|
||||||
|
}
|
||||||
|
var c Caps
|
||||||
|
if err := json.Unmarshal(out, &c); err != nil {
|
||||||
|
return Caps{}, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type helperOut struct {
|
||||||
|
Engine string `json:"engine,omitempty"`
|
||||||
|
Codec string `json:"codec,omitempty"`
|
||||||
|
Width int `json:"width,omitempty"`
|
||||||
|
Height int `json:"height,omitempty"`
|
||||||
|
JPEG []byte `json:"jpeg,omitempty"`
|
||||||
|
Error string `json:"error,omitempty"`
|
||||||
|
Permanent bool `json:"permanent,omitempty"` // a DecodeError: the file, not the moment
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeRoot is Make run as root through this binary, with the caps ProbeRoot found.
|
||||||
|
func MakeRoot(ctx context.Context, self string, c Caps, path string, o Opts) (Result, error) {
|
||||||
|
o = o.defaults()
|
||||||
|
caps, _ := json.Marshal(c)
|
||||||
|
full := priv.Wrap(self, "thumb", "--size", strconv.Itoa(o.Size), "--timeout", o.Timeout.String(), path)
|
||||||
|
cmd := exec.CommandContext(ctx, full[0], full[1:]...)
|
||||||
|
cmd.Stdin = bytes.NewReader(caps)
|
||||||
|
var errb bytes.Buffer
|
||||||
|
cmd.Stderr = &errb
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return Result{}, ctx.Err()
|
||||||
|
}
|
||||||
|
if msg := lastLine(errb.String()); msg != "" {
|
||||||
|
return Result{}, errors.New(msg)
|
||||||
|
}
|
||||||
|
return Result{}, err
|
||||||
|
}
|
||||||
|
var h helperOut
|
||||||
|
if err := json.Unmarshal(out, &h); err != nil {
|
||||||
|
return Result{}, err
|
||||||
|
}
|
||||||
|
if h.Error != "" {
|
||||||
|
if h.Permanent {
|
||||||
|
return Result{}, &DecodeError{Msg: h.Error}
|
||||||
|
}
|
||||||
|
return Result{}, errors.New(h.Error)
|
||||||
|
}
|
||||||
|
return Result{JPEG: h.JPEG, Engine: h.Engine, Codec: h.Codec, Width: h.Width, Height: h.Height}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper implements `hamlab thumb …`, the root side of the above. `--probe` prints Caps;
|
||||||
|
// `[--size N] [--timeout D] <path>` reads Caps from stdin and prints the preview as JSON.
|
||||||
|
// Returns the exit code.
|
||||||
|
func Helper(args []string, stdin io.Reader, stdout, stderr io.Writer) int {
|
||||||
|
fail := func(err error) int { fmt.Fprintln(stderr, err.Error()); return 1 }
|
||||||
|
ctx := context.Background()
|
||||||
|
o := Opts{}
|
||||||
|
path := ""
|
||||||
|
for i := 0; i < len(args); i++ {
|
||||||
|
switch a := args[i]; {
|
||||||
|
case a == "--probe":
|
||||||
|
return emit(stdout, Probe(ctx), stderr)
|
||||||
|
case a == "--size" && i+1 < len(args):
|
||||||
|
o.Size, _ = strconv.Atoi(args[i+1])
|
||||||
|
i++
|
||||||
|
case a == "--timeout" && i+1 < len(args):
|
||||||
|
o.Timeout, _ = time.ParseDuration(args[i+1])
|
||||||
|
i++
|
||||||
|
case strings.HasPrefix(a, "-"):
|
||||||
|
return fail(errors.New("usage: hamlab thumb --probe | [--size N] [--timeout D] <path>"))
|
||||||
|
default:
|
||||||
|
path = a
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if path == "" || !filepath.IsAbs(path) {
|
||||||
|
return fail(errors.New("usage: hamlab thumb --probe | [--size N] [--timeout D] <absolute path>"))
|
||||||
|
}
|
||||||
|
var c Caps
|
||||||
|
if err := json.NewDecoder(io.LimitReader(stdin, 1<<20)).Decode(&c); err != nil {
|
||||||
|
return fail(fmt.Errorf("caps on stdin: %w", err))
|
||||||
|
}
|
||||||
|
res, err := Make(ctx, c, path, o)
|
||||||
|
if err != nil {
|
||||||
|
var de *DecodeError
|
||||||
|
return emit(stdout, helperOut{Error: err.Error(), Permanent: errors.As(err, &de)}, stderr)
|
||||||
|
}
|
||||||
|
return emit(stdout, helperOut{Engine: res.Engine, Codec: res.Codec, Width: res.Width, Height: res.Height, JPEG: res.JPEG}, stderr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func emit(w io.Writer, v any, stderr io.Writer) int {
|
||||||
|
if err := json.NewEncoder(w).Encode(v); err != nil {
|
||||||
|
fmt.Fprintln(stderr, err.Error())
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- small helpers ----
|
||||||
|
|
||||||
|
func lastLine(s string) string {
|
||||||
|
lines := strings.Split(strings.TrimSpace(s), "\n")
|
||||||
|
return strings.TrimSpace(lines[len(lines)-1])
|
||||||
|
}
|
||||||
|
|
||||||
|
func exists(p string) bool { _, err := os.Stat(p); return err == nil }
|
||||||
@@ -0,0 +1,253 @@
|
|||||||
|
package media
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
|
"image/jpeg"
|
||||||
|
"image/png"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKind(t *testing.T) {
|
||||||
|
for name, want := range map[string]string{"a.JPG": "image", "b.heic": "image", "c.mkv": "video", "GH010098.MP4": "video", "x.OSV": "video", "d.txt": "", "e": "", ".jpg": "image"} {
|
||||||
|
if got := Kind(name); got != want {
|
||||||
|
t.Errorf("Kind(%q) = %q, want %q", name, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestKeyFollowsTheFile(t *testing.T) {
|
||||||
|
a := Key("/x/a.jpg", 10, 20)
|
||||||
|
if a != Key("/x/a.jpg", 10, 20) || len(a) != 64 {
|
||||||
|
t.Fatal("key is not stable")
|
||||||
|
}
|
||||||
|
for _, b := range []string{Key("/x/b.jpg", 10, 20), Key("/x/a.jpg", 11, 20), Key("/x/a.jpg", 10, 21)} {
|
||||||
|
if b == a {
|
||||||
|
t.Fatal("key ignores part of the file's identity")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParseVainfo(t *testing.T) {
|
||||||
|
text := `vainfo: Driver version: Intel iHD driver for Intel(R) Gen Graphics - 25.2.3 ()
|
||||||
|
VAProfileMPEG2Simple : VAEntrypointVLD
|
||||||
|
VAProfileH264Main : VAEntrypointVLD
|
||||||
|
VAProfileH264High : VAEntrypointEncSliceLP
|
||||||
|
VAProfileJPEGBaseline : VAEntrypointVLD
|
||||||
|
VAProfileHEVCMain10 : VAEntrypointVLD
|
||||||
|
VAProfileHEVCMain10 : VAEntrypointEncSlice
|
||||||
|
VAProfileHEVCMain444_12 : VAEntrypointVLD
|
||||||
|
VAProfileVP9Profile2 : VAEntrypointVLD
|
||||||
|
VAProfileAV1Profile0 : VAEntrypointEncSliceLP
|
||||||
|
`
|
||||||
|
dec, enc := parseVainfo(text)
|
||||||
|
same := func(got, want []string) bool {
|
||||||
|
if len(got) != len(want) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for i := range want {
|
||||||
|
if got[i] != want[i] {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if !same(dec, []string{"h264", "hevc", "mjpeg", "mpeg2video", "vp9"}) {
|
||||||
|
t.Fatalf("decodes = %v", dec)
|
||||||
|
}
|
||||||
|
if !same(enc, []string{"av1", "h264", "hevc"}) {
|
||||||
|
t.Fatalf("encodes = %v", enc)
|
||||||
|
}
|
||||||
|
if d, e := parseVainfo(""); d != nil || e != nil {
|
||||||
|
t.Fatal("nothing from nothing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPickAndEngines(t *testing.T) {
|
||||||
|
igpu := GPU{API: "vaapi", Device: "/dev/dri/renderD128", Codecs: []string{"h264", "hevc"}}
|
||||||
|
arc := GPU{API: "vaapi", Device: "/dev/dri/renderD129", Codecs: []string{"av1", "h264", "hevc"}}
|
||||||
|
c := Caps{FFmpeg: "ffmpeg", FFprobe: "ffprobe", GPUs: []GPU{igpu, arc}}
|
||||||
|
if g := c.pick("av1"); g == nil || g.Device != arc.Device {
|
||||||
|
t.Fatal("av1 should go to the card that lists it")
|
||||||
|
}
|
||||||
|
if g := c.pick("h264"); g == nil || g.Device != igpu.Device {
|
||||||
|
t.Fatal("h264 should go to the first card that lists it")
|
||||||
|
}
|
||||||
|
if g := c.pick("vc1"); g != nil {
|
||||||
|
t.Fatal("a codec nobody lists is software")
|
||||||
|
}
|
||||||
|
// a card nothing could describe is tried, after the ones that answered
|
||||||
|
c.GPUs = append(c.GPUs, GPU{API: "cuda", Device: "0"})
|
||||||
|
if g := c.pick("vc1"); g == nil || g.API != "cuda" {
|
||||||
|
t.Fatal("an undescribed card is the guess of last resort")
|
||||||
|
}
|
||||||
|
// videos: GPU then CPU; images: CPU only
|
||||||
|
if e := engines(c, "video", fileInfo{codec: "hevc"}); len(e) != 2 || e[0] == nil || e[1] != nil {
|
||||||
|
t.Fatal("a video tries the GPU, then the CPU")
|
||||||
|
}
|
||||||
|
if e := engines(c, "image", fileInfo{codec: "mjpeg"}); len(e) != 1 || e[0] != nil {
|
||||||
|
t.Fatal("an image is CPU only")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAttempts(t *testing.T) {
|
||||||
|
a := attempts(nil, "video", fileInfo{duration: 120}, 256)
|
||||||
|
if len(a) != 2 || a[0].seek != 12 || a[1].seek != 0 {
|
||||||
|
t.Fatalf("video attempts = %+v", a)
|
||||||
|
}
|
||||||
|
if a := attempts(nil, "video", fileInfo{duration: 7200}, 256); a[0].seek != 30 {
|
||||||
|
t.Fatalf("seek capped at 30 s, got %v", a[0].seek)
|
||||||
|
}
|
||||||
|
if a := attempts(nil, "video", fileInfo{duration: 2}, 256); len(a) != 1 || a[0].seek != 0 {
|
||||||
|
t.Fatalf("a short clip is taken from its start, got %+v", a)
|
||||||
|
}
|
||||||
|
if a := attempts(nil, "image", fileInfo{codec: "mjpeg", w: 6000, h: 4000}, 256); len(a) != 2 || a[0].lowres != 2 || a[1].lowres != 0 {
|
||||||
|
t.Fatalf("jpeg attempts = %+v", a)
|
||||||
|
}
|
||||||
|
if a := attempts(nil, "image", fileInfo{codec: "mjpeg", w: 800, h: 600}, 256); len(a) != 1 || a[0].lowres != 0 {
|
||||||
|
t.Fatalf("a small jpeg decodes whole, got %+v", a)
|
||||||
|
}
|
||||||
|
if a := attempts(nil, "image", fileInfo{codec: "png", w: 6000, h: 4000}, 256); len(a) != 1 || a[0].lowres != 0 {
|
||||||
|
t.Fatalf("only jpeg has lowres, got %+v", a)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCache(t *testing.T) {
|
||||||
|
c := NewCache(filepath.Join(t.TempDir(), "thumbs.db"))
|
||||||
|
k := Key("/a", 1, 2)
|
||||||
|
if _, found, err := c.Get(k); found || err != nil {
|
||||||
|
t.Fatal("an empty cache found something, or complained")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(c.Path); err == nil {
|
||||||
|
t.Fatal("a miss must not create the database")
|
||||||
|
}
|
||||||
|
if n, b := c.Stats(); n != 0 || b != 0 {
|
||||||
|
t.Fatal("stats of nothing")
|
||||||
|
}
|
||||||
|
if err := c.Put(k, Entry{Path: "/a", Engine: "cpu", Codec: "png", Width: 640, Height: 360, JPEG: []byte("jpeg")}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
e, found, err := c.Get(k)
|
||||||
|
if err != nil || !found || string(e.JPEG) != "jpeg" || e.Engine != "cpu" || e.Width != 640 || e.Path != "/a" || e.Made.IsZero() {
|
||||||
|
t.Fatalf("put then get: %+v %v %v", e, found, err)
|
||||||
|
}
|
||||||
|
k2 := Key("/b", 1, 2)
|
||||||
|
if err := c.Put(k2, Entry{Path: "/b", Failure: "no video stream"}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if e, found, _ := c.Get(k2); !found || e.JPEG != nil || e.Failure != "no video stream" {
|
||||||
|
t.Fatal("a failure is remembered with its reason")
|
||||||
|
}
|
||||||
|
if err := c.Put(k, Entry{}); err == nil {
|
||||||
|
t.Fatal("an empty preview must not be kept")
|
||||||
|
}
|
||||||
|
// replacing an entry does not count it twice
|
||||||
|
if err := c.Put(k, Entry{Path: "/a", JPEG: []byte("jpeg2")}); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
n, b := c.Stats()
|
||||||
|
if n != 2 || b == 0 {
|
||||||
|
t.Fatalf("stats = %d entries, %d bytes", n, b)
|
||||||
|
}
|
||||||
|
// survives a reopen
|
||||||
|
if err := c.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
c2 := NewCache(c.Path)
|
||||||
|
if e, found, _ := c2.Get(k); !found || string(e.JPEG) != "jpeg2" {
|
||||||
|
t.Fatal("not there after reopening")
|
||||||
|
}
|
||||||
|
if n, _ := c2.Stats(); n != 2 {
|
||||||
|
t.Fatal("count lost on reopen")
|
||||||
|
}
|
||||||
|
c2.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// The real thing, when ffmpeg is here: a PNG and a generated video each come back as a
|
||||||
|
// JPEG no larger than the asked size, with the aspect kept.
|
||||||
|
func TestMakeWithFFmpeg(t *testing.T) {
|
||||||
|
if _, err := exec.LookPath("ffmpeg"); err != nil {
|
||||||
|
t.Skip("no ffmpeg")
|
||||||
|
}
|
||||||
|
c := Probe(context.Background())
|
||||||
|
if c.Error != "" {
|
||||||
|
t.Skip(c.Error)
|
||||||
|
}
|
||||||
|
dir := t.TempDir()
|
||||||
|
img := image.NewRGBA(image.Rect(0, 0, 640, 360))
|
||||||
|
for y := 0; y < 360; y++ {
|
||||||
|
for x := 0; x < 640; x++ {
|
||||||
|
img.Set(x, y, color.RGBA{uint8(x / 3), uint8(y), 90, 255})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pngPath := filepath.Join(dir, "a.png")
|
||||||
|
f, _ := os.Create(pngPath)
|
||||||
|
_ = png.Encode(f, img)
|
||||||
|
f.Close()
|
||||||
|
res, err := Make(context.Background(), c, pngPath, Opts{Size: 200})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
check(t, res, 200, 112, "cpu")
|
||||||
|
|
||||||
|
// a two-second test pattern, with the always-built-in mpeg4 encoder
|
||||||
|
vid := filepath.Join(dir, "v.mp4")
|
||||||
|
out, err := exec.Command("ffmpeg", "-nostdin", "-v", "error", "-f", "lavfi", "-i", "testsrc=size=320x180:rate=10:duration=2", "-c:v", "mpeg4", "-q:v", "5", vid).CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("could not make a test video: %s", out)
|
||||||
|
}
|
||||||
|
res, err = Make(context.Background(), c, vid, Opts{Size: 100})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
check(t, res, 100, 56, "")
|
||||||
|
if res.Codec != "mpeg4" || res.Width != 320 {
|
||||||
|
t.Fatalf("probe said %s %dx%d", res.Codec, res.Width, res.Height)
|
||||||
|
}
|
||||||
|
|
||||||
|
// not media, and media that isn't
|
||||||
|
if _, err := Make(context.Background(), c, filepath.Join(dir, "x.txt"), Opts{}); err != ErrUnsupported {
|
||||||
|
t.Fatalf("a .txt is unsupported, got %v", err)
|
||||||
|
}
|
||||||
|
bad := filepath.Join(dir, "bad.mp4")
|
||||||
|
_ = os.WriteFile(bad, []byte("not a video at all"), 0o644)
|
||||||
|
_, err = Make(context.Background(), c, bad, Opts{})
|
||||||
|
var de *DecodeError
|
||||||
|
if err == nil || !isDecodeErr(err, &de) {
|
||||||
|
t.Fatalf("garbage should be a DecodeError, got %v", err)
|
||||||
|
}
|
||||||
|
// the clock: a cancelled context is not the file's fault
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||||
|
defer cancel()
|
||||||
|
if _, err := Make(ctx, c, pngPath, Opts{}); err == nil || isDecodeErr(err, &de) {
|
||||||
|
t.Fatalf("a timeout must not read as a decode failure, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func check(t *testing.T, res Result, w, h int, engine string) {
|
||||||
|
t.Helper()
|
||||||
|
im, err := jpeg.Decode(bytes.NewReader(res.JPEG))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("not a jpeg: %v", err)
|
||||||
|
}
|
||||||
|
if b := im.Bounds(); b.Dx() != w || b.Dy() < h-1 || b.Dy() > h+1 { // the short side rounds either way
|
||||||
|
t.Fatalf("got %dx%d, want %dx%d", b.Dx(), b.Dy(), w, h)
|
||||||
|
}
|
||||||
|
if engine != "" && res.Engine != engine {
|
||||||
|
t.Fatalf("engine %q, want %q", res.Engine, engine)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isDecodeErr(err error, de **DecodeError) bool {
|
||||||
|
x, ok := err.(*DecodeError)
|
||||||
|
if ok {
|
||||||
|
*de = x
|
||||||
|
}
|
||||||
|
return ok
|
||||||
|
}
|
||||||
@@ -0,0 +1,259 @@
|
|||||||
|
package metrics
|
||||||
|
|
||||||
|
// CPU power, from the RAPL energy counters the kernel keeps under /sys/class/powercap: Intel
|
||||||
|
// since Sandy Bridge, and AMD Zen through the same driver. Each counter is a running total in
|
||||||
|
// µJ, so power is the difference between two readings over the time between them. Every
|
||||||
|
// package is summed (`package-0`, and `package-1` on a two-socket board). The other top-level
|
||||||
|
// domains are left out. `psys` claims the whole platform but is only as good as the firmware's
|
||||||
|
// calibration, and on one machine here it reads lower than the package inside it. The MMIO
|
||||||
|
// copy of a package (`intel-rapl-mmio:0`) measures the same thing again. This is the CPU's own
|
||||||
|
// figure, shown beside its clock: cores, built-in graphics and memory controller, not disks,
|
||||||
|
// fans or the power supply.
|
||||||
|
//
|
||||||
|
// The counters are root-only since the PLATYPUS side channel (2020). So, like the GPU client
|
||||||
|
// walk, they are read in-process when the daemon is root, and otherwise by a root helper,
|
||||||
|
// `hamlab cpu-energy`, started once through sudo or doas and asked over a pipe at every
|
||||||
|
// sample: one privilege escalation per daemon, not one a second in the auth log. The helper
|
||||||
|
// takes nothing from the daemon but the newline that asks; it finds and reads the counters
|
||||||
|
// itself. Without root there is no reading, and nothing is shown.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"hamlab/internal/priv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const raplRoot = "/sys/class/powercap"
|
||||||
|
|
||||||
|
var raplPkgRe = regexp.MustCompile(`^package-\d+$`)
|
||||||
|
|
||||||
|
// raplCount is one package's counter at one moment.
|
||||||
|
type raplCount struct {
|
||||||
|
Dir string `json:"d"`
|
||||||
|
UJ uint64 `json:"uj"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type cpuPowerState struct {
|
||||||
|
probed bool
|
||||||
|
dirs []string // the package domains, found at the first sample
|
||||||
|
max map[string]uint64 // each counter's range: where it wraps to zero
|
||||||
|
useHelper bool // the counters are not readable in-process
|
||||||
|
helper *energyHelper
|
||||||
|
prev []raplCount
|
||||||
|
prevAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// raplPackages lists the package domains under root, sorted. intel-rapl:0 is a package;
|
||||||
|
// intel-rapl:0:0 is a part of one (core, uncore, dram).
|
||||||
|
func raplPackages(root string) []string {
|
||||||
|
ents, err := os.ReadDir(root)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var out []string
|
||||||
|
for _, e := range ents {
|
||||||
|
n := e.Name()
|
||||||
|
if !strings.HasPrefix(n, "intel-rapl:") || strings.Count(n, ":") != 1 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if raplPkgRe.MatchString(strings.TrimSpace(readStr(filepath.Join(root, n, "name")))) {
|
||||||
|
out = append(out, n)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Strings(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func readRAPL(root string, dirs []string) ([]raplCount, bool) {
|
||||||
|
out := make([]raplCount, 0, len(dirs))
|
||||||
|
for _, d := range dirs {
|
||||||
|
b, err := os.ReadFile(filepath.Join(root, d, "energy_uj"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
v, err := strconv.ParseUint(strings.TrimSpace(string(b)), 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
out = append(out, raplCount{d, v})
|
||||||
|
}
|
||||||
|
return out, len(out) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// raplWatts is the power between two readings of the same counters. A counter that wrapped
|
||||||
|
// is carried across its range. One that went backwards with no known range, or a set of
|
||||||
|
// counters that changed between readings, gives no answer rather than a wrong one.
|
||||||
|
func raplWatts(prev, cur []raplCount, max map[string]uint64, dt float64) (float64, bool) {
|
||||||
|
if dt <= 0 || len(prev) == 0 || len(prev) != len(cur) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
var uj float64
|
||||||
|
for i := range cur {
|
||||||
|
if cur[i].Dir != prev[i].Dir {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if cur[i].UJ >= prev[i].UJ {
|
||||||
|
uj += float64(cur[i].UJ - prev[i].UJ)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
m := max[cur[i].Dir]
|
||||||
|
if m == 0 || prev[i].UJ > m {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
uj += float64(m - prev[i].UJ + cur[i].UJ)
|
||||||
|
}
|
||||||
|
return uj / 1e6 / dt, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// cpuPower is the CPU packages' power since the previous sample, in watts; 0 when this
|
||||||
|
// machine has no counters or no way to read them.
|
||||||
|
func (c *Collector) cpuPower(now time.Time) float64 {
|
||||||
|
p := &c.power
|
||||||
|
if !p.probed {
|
||||||
|
p.probed = true
|
||||||
|
p.dirs = raplPackages(raplRoot)
|
||||||
|
p.max = map[string]uint64{}
|
||||||
|
for _, d := range p.dirs {
|
||||||
|
if v, ok := readInt(filepath.Join(raplRoot, d, "max_energy_range_uj")); ok && v > 0 {
|
||||||
|
p.max[d] = uint64(v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(p.dirs) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
var cur []raplCount
|
||||||
|
ok := false
|
||||||
|
if !p.useHelper {
|
||||||
|
if cur, ok = readRAPL(raplRoot, p.dirs); !ok && priv.Mode() != "root" && priv.Available() {
|
||||||
|
p.useHelper = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if p.useHelper {
|
||||||
|
if p.helper == nil {
|
||||||
|
p.helper = &energyHelper{}
|
||||||
|
}
|
||||||
|
cur, ok = p.helper.read()
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
p.prev = nil
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
w, good := raplWatts(p.prev, cur, p.max, now.Sub(p.prevAt).Seconds())
|
||||||
|
p.prev, p.prevAt = cur, now
|
||||||
|
if !good {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return round1(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
// energyHelper is the root child that answers each newline with the package counters, one
|
||||||
|
// JSON line. The same shape as the GPU client walk's helper, kept separate from it.
|
||||||
|
type energyHelper struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
cmd *exec.Cmd
|
||||||
|
in io.WriteCloser
|
||||||
|
out *bufio.Reader
|
||||||
|
failedAt time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *energyHelper) read() ([]raplCount, bool) {
|
||||||
|
h.mu.Lock()
|
||||||
|
defer h.mu.Unlock()
|
||||||
|
if h.cmd == nil {
|
||||||
|
if !h.failedAt.IsZero() && time.Since(h.failedAt) < helperRetry {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
if !h.start() {
|
||||||
|
h.failedAt = time.Now()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// a stuck child must not stall the sampler: it is killed, and tried again later
|
||||||
|
cmd := h.cmd
|
||||||
|
stop := time.AfterFunc(5*time.Second, func() { cmd.Process.Kill() })
|
||||||
|
defer stop.Stop()
|
||||||
|
if _, err := io.WriteString(h.in, "\n"); err != nil {
|
||||||
|
h.fail()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
line, err := h.out.ReadBytes('\n')
|
||||||
|
if err != nil {
|
||||||
|
h.fail()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
var cs []raplCount
|
||||||
|
if json.Unmarshal(line, &cs) != nil {
|
||||||
|
h.fail()
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
return cs, len(cs) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *energyHelper) start() bool {
|
||||||
|
self, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
full := priv.Wrap(self, "cpu-energy")
|
||||||
|
cmd := exec.Command(full[0], full[1:]...)
|
||||||
|
in, err := cmd.StdinPipe()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
out, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
h.cmd, h.in, h.out = cmd, in, bufio.NewReader(out)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *energyHelper) fail() {
|
||||||
|
if h.cmd != nil {
|
||||||
|
h.in.Close()
|
||||||
|
h.cmd.Process.Kill()
|
||||||
|
go h.cmd.Wait()
|
||||||
|
}
|
||||||
|
h.cmd, h.in, h.out = nil, nil, nil
|
||||||
|
h.failedAt = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
// CPUEnergyHelper is `hamlab cpu-energy`: for every line read on stdin, one JSON line of the
|
||||||
|
// package counters on stdout. Ends when stdin does — when the daemon that started it has gone.
|
||||||
|
func CPUEnergyHelper(stdin io.Reader, stdout io.Writer) int {
|
||||||
|
return cpuEnergyHelper(raplRoot, stdin, stdout)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cpuEnergyHelper(root string, stdin io.Reader, stdout io.Writer) int {
|
||||||
|
dirs := raplPackages(root)
|
||||||
|
sc := bufio.NewScanner(stdin)
|
||||||
|
w := bufio.NewWriter(stdout)
|
||||||
|
for sc.Scan() {
|
||||||
|
cs, ok := readRAPL(root, dirs)
|
||||||
|
if !ok {
|
||||||
|
cs = []raplCount{}
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(cs)
|
||||||
|
w.Write(b)
|
||||||
|
w.WriteByte('\n')
|
||||||
|
if w.Flush() != nil {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
@@ -0,0 +1,106 @@
|
|||||||
|
package metrics
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// A fake /sys/class/powercap: a package with its parts, the platform domain, and the MMIO
|
||||||
|
// copy of the same package. Only the package counts.
|
||||||
|
func fakeRAPL(t *testing.T, domains map[string][2]string) string {
|
||||||
|
t.Helper()
|
||||||
|
root := t.TempDir()
|
||||||
|
for dir, v := range domains {
|
||||||
|
d := filepath.Join(root, dir)
|
||||||
|
if err := os.MkdirAll(d, 0o755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
os.WriteFile(filepath.Join(d, "name"), []byte(v[0]+"\n"), 0o644)
|
||||||
|
os.WriteFile(filepath.Join(d, "energy_uj"), []byte(v[1]+"\n"), 0o644)
|
||||||
|
}
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRAPLPackagesOnlyCountsPackages(t *testing.T) {
|
||||||
|
root := fakeRAPL(t, map[string][2]string{
|
||||||
|
"intel-rapl:0": {"package-0", "1000"},
|
||||||
|
"intel-rapl:0:0": {"core", "600"},
|
||||||
|
"intel-rapl:0:1": {"uncore", "10"},
|
||||||
|
"intel-rapl:1": {"psys", "300"},
|
||||||
|
"intel-rapl-mmio:0": {"package-0", "1000"},
|
||||||
|
})
|
||||||
|
if got := raplPackages(root); len(got) != 1 || got[0] != "intel-rapl:0" {
|
||||||
|
t.Fatalf("packages = %v, want [intel-rapl:0]", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
two := fakeRAPL(t, map[string][2]string{"intel-rapl:0": {"package-0", "5"}, "intel-rapl:1": {"package-1", "7"}})
|
||||||
|
got := raplPackages(two)
|
||||||
|
if strings.Join(got, ",") != "intel-rapl:0,intel-rapl:1" {
|
||||||
|
t.Fatalf("two sockets: packages = %v", got)
|
||||||
|
}
|
||||||
|
cs, ok := readRAPL(two, got)
|
||||||
|
if !ok || len(cs) != 2 || cs[0].UJ != 5 || cs[1].UJ != 7 {
|
||||||
|
t.Fatalf("readRAPL = %+v, %v", cs, ok)
|
||||||
|
}
|
||||||
|
if got := raplPackages(filepath.Join(two, "nope")); len(got) != 0 {
|
||||||
|
t.Fatalf("no powercap: packages = %v, want none", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRAPLWatts(t *testing.T) {
|
||||||
|
c := func(d string, uj uint64) raplCount { return raplCount{d, uj} }
|
||||||
|
max := map[string]uint64{"intel-rapl:0": 1_000_000_000}
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
prev, cur []raplCount
|
||||||
|
dt float64
|
||||||
|
want float64
|
||||||
|
ok bool
|
||||||
|
}{
|
||||||
|
{"steady", []raplCount{c("intel-rapl:0", 0)}, []raplCount{c("intel-rapl:0", 10_000_000)}, 2, 5, true},
|
||||||
|
{"two packages add", []raplCount{c("intel-rapl:0", 0), c("intel-rapl:1", 0)}, []raplCount{c("intel-rapl:0", 4_000_000), c("intel-rapl:1", 6_000_000)}, 1, 10, true},
|
||||||
|
{"wrapped", []raplCount{c("intel-rapl:0", 999_000_000)}, []raplCount{c("intel-rapl:0", 1_000_000)}, 1, 2, true},
|
||||||
|
{"backwards, range unknown", []raplCount{c("intel-rapl:1", 5_000_000)}, []raplCount{c("intel-rapl:1", 1)}, 1, 0, false},
|
||||||
|
{"counters changed", []raplCount{c("intel-rapl:0", 0)}, []raplCount{c("intel-rapl:1", 9)}, 1, 0, false},
|
||||||
|
{"first reading", nil, []raplCount{c("intel-rapl:0", 9)}, 1, 0, false},
|
||||||
|
{"no time passed", []raplCount{c("intel-rapl:0", 0)}, []raplCount{c("intel-rapl:0", 9)}, 0, 0, false},
|
||||||
|
}
|
||||||
|
for _, tc := range cases {
|
||||||
|
got, ok := raplWatts(tc.prev, tc.cur, max, tc.dt)
|
||||||
|
if ok != tc.ok || (ok && (got < tc.want-1e-9 || got > tc.want+1e-9)) {
|
||||||
|
t.Errorf("%s: got %v, %v; want %v, %v", tc.name, got, ok, tc.want, tc.ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The helper answers every newline with the counters it found itself, and stops at EOF.
|
||||||
|
func TestCPUEnergyHelper(t *testing.T) {
|
||||||
|
root := fakeRAPL(t, map[string][2]string{"intel-rapl:0": {"package-0", "42"}, "intel-rapl:0:0": {"core", "7"}})
|
||||||
|
var out bytes.Buffer
|
||||||
|
if code := cpuEnergyHelper(root, strings.NewReader("\n\n"), &out); code != 0 {
|
||||||
|
t.Fatalf("exit %d", code)
|
||||||
|
}
|
||||||
|
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
|
||||||
|
if len(lines) != 2 {
|
||||||
|
t.Fatalf("got %d lines, want 2: %q", len(lines), out.String())
|
||||||
|
}
|
||||||
|
var cs []raplCount
|
||||||
|
if err := json.Unmarshal([]byte(lines[1]), &cs); err != nil || len(cs) != 1 || cs[0] != (raplCount{"intel-rapl:0", 42}) {
|
||||||
|
t.Fatalf("line = %s (%v)", lines[1], err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// History keeps the average of the samples that had a reading.
|
||||||
|
func TestFoldCPUPower(t *testing.T) {
|
||||||
|
b := []Sample{{T: 1, CPUPower: 4}, {T: 2}, {T: 3, CPUPower: 8}}
|
||||||
|
if got := fold(b).CPUPower; got != 6 {
|
||||||
|
t.Fatalf("folded CPU power = %v, want 6", got)
|
||||||
|
}
|
||||||
|
if got := fold([]Sample{{T: 1}, {T: 2}}).CPUPower; got != 0 {
|
||||||
|
t.Fatalf("no readings folded to %v, want 0", got)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,6 +32,17 @@ func fold(b []Sample) Sample {
|
|||||||
return t / uint64(len(b))
|
return t / uint64(len(b))
|
||||||
}
|
}
|
||||||
out.CPU = sum(func(s Sample) float64 { return s.CPU })
|
out.CPU = sum(func(s Sample) float64 { return s.CPU })
|
||||||
|
// CPU power: over the samples that have a reading, so a helper restart is not a dip to zero
|
||||||
|
var pw, pn float64
|
||||||
|
for _, s := range b {
|
||||||
|
if s.CPUPower > 0 {
|
||||||
|
pw, pn = pw+s.CPUPower, pn+1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out.CPUPower = 0
|
||||||
|
if pn > 0 {
|
||||||
|
out.CPUPower = round1(pw / pn)
|
||||||
|
}
|
||||||
out.Load[0] = sum(func(s Sample) float64 { return s.Load[0] })
|
out.Load[0] = sum(func(s Sample) float64 { return s.Load[0] })
|
||||||
out.Load[1] = sum(func(s Sample) float64 { return s.Load[1] })
|
out.Load[1] = sum(func(s Sample) float64 { return s.Load[1] })
|
||||||
out.Load[2] = sum(func(s Sample) float64 { return s.Load[2] })
|
out.Load[2] = sum(func(s Sample) float64 { return s.Load[2] })
|
||||||
|
|||||||
@@ -250,6 +250,31 @@ func listGPUs(known []*gpuDev) []*gpuDev {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RenderNodeName is the GPU behind a render node ("/dev/dri/renderD128" → "Intel Arc A380"),
|
||||||
|
// named the same way the Processes tab names it; "" when sysfs has nothing for it.
|
||||||
|
func RenderNodeName(node string) string {
|
||||||
|
dev, err := filepath.EvalSymlinks("/sys/class/drm/" + filepath.Base(node) + "/device")
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
driver := ueventValue(dev+"/uevent", "DRIVER")
|
||||||
|
if driver == "" {
|
||||||
|
driver = filepath.Base(readLink(dev + "/driver"))
|
||||||
|
}
|
||||||
|
return gpuName(dev, ueventValue(dev+"/uevent", "PCI_SLOT_NAME"), driver)
|
||||||
|
}
|
||||||
|
|
||||||
|
// RenderNodeIntegrated reports a render node whose GPU sits on the CPU die (PCI root bus 0,
|
||||||
|
// or no PCI bus at all), as opposed to a card on the bus.
|
||||||
|
func RenderNodeIntegrated(node string) bool {
|
||||||
|
dev, err := filepath.EvalSymlinks("/sys/class/drm/" + filepath.Base(node) + "/device")
|
||||||
|
if err != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
pci := ueventValue(dev+"/uevent", "PCI_SLOT_NAME")
|
||||||
|
return pci == "" || strings.HasPrefix(pci, "0000:00:")
|
||||||
|
}
|
||||||
|
|
||||||
var gpuVendors = map[string]string{"0x8086": "Intel", "0x1002": "AMD", "0x10de": "NVIDIA", "0x1af4": "Virtio", "0x106b": "Apple"}
|
var gpuVendors = map[string]string{"0x8086": "Intel", "0x1002": "AMD", "0x10de": "NVIDIA", "0x1af4": "Virtio", "0x106b": "Apple"}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
@@ -6,7 +6,9 @@
|
|||||||
// lm-sensors, smartmontools or root. Sections whose source is absent (no fans, no
|
// lm-sensors, smartmontools or root. Sections whose source is absent (no fans, no
|
||||||
// UPS daemon, no hwmon at all on a VM) simply come back empty rather than failing.
|
// UPS daemon, no hwmon at all on a VM) simply come back empty rather than failing.
|
||||||
// The only external commands are optional UPS daemons: pwrstat (CyberPower), upsc
|
// The only external commands are optional UPS daemons: pwrstat (CyberPower), upsc
|
||||||
// (NUT) and apcaccess (apcupsd), tried in that order.
|
// (NUT) and apcaccess (apcupsd), tried in that order. Two readings are root-only, the GPU
|
||||||
|
// client walk and the CPU energy counters; without root they come from small helpers of
|
||||||
|
// this binary started once through sudo or doas (gpu.go, cpupower.go), or not at all.
|
||||||
package metrics
|
package metrics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -137,6 +139,7 @@ type Sample struct {
|
|||||||
Interval int64 `json:"interval_ms"` // sampling interval in effect
|
Interval int64 `json:"interval_ms"` // sampling interval in effect
|
||||||
CPU float64 `json:"cpu"`
|
CPU float64 `json:"cpu"`
|
||||||
Cores []float64 `json:"cores"`
|
Cores []float64 `json:"cores"`
|
||||||
|
CPUPower float64 `json:"cpu_power_w,omitempty"` // the CPU packages' own energy counters; 0 = no reading
|
||||||
Load [3]float64 `json:"load"`
|
Load [3]float64 `json:"load"`
|
||||||
Mem Mem `json:"mem"`
|
Mem Mem `json:"mem"`
|
||||||
Swap Mem `json:"swap"`
|
Swap Mem `json:"swap"`
|
||||||
@@ -177,6 +180,7 @@ type Collector struct {
|
|||||||
lastTable *ProcTable
|
lastTable *ProcTable
|
||||||
upsProbe func() *UPS // nil when no UPS daemon is installed
|
upsProbe func() *UPS // nil when no UPS daemon is installed
|
||||||
gpu gpuState
|
gpu gpuState
|
||||||
|
power cpuPowerState
|
||||||
prevARC arcPrev
|
prevARC arcPrev
|
||||||
|
|
||||||
subMu sync.Mutex
|
subMu sync.Mutex
|
||||||
@@ -343,6 +347,7 @@ func (c *Collector) sample() {
|
|||||||
|
|
||||||
s := Sample{T: now.UnixMilli(), Interval: c.Interval().Milliseconds(), Temps: []Temp{}, Fans: []Fan{}, Net: []Net{}, Disks: []Disk{}}
|
s := Sample{T: now.UnixMilli(), Interval: c.Interval().Milliseconds(), Temps: []Temp{}, Fans: []Fan{}, Net: []Net{}, Disks: []Disk{}}
|
||||||
s.CPU, s.Cores = c.cpu()
|
s.CPU, s.Cores = c.cpu()
|
||||||
|
s.CPUPower = c.cpuPower(now)
|
||||||
s.Load = loadavg()
|
s.Load = loadavg()
|
||||||
s.Mem, s.Swap = meminfo()
|
s.Mem, s.Swap = meminfo()
|
||||||
s.Uptime = uptime()
|
s.Uptime = uptime()
|
||||||
@@ -1113,6 +1118,7 @@ type ProcTable struct {
|
|||||||
CPUModel string `json:"cpu_model,omitempty"`
|
CPUModel string `json:"cpu_model,omitempty"`
|
||||||
CPUMHz float64 `json:"cpu_mhz,omitempty"`
|
CPUMHz float64 `json:"cpu_mhz,omitempty"`
|
||||||
CPUMaxMHz float64 `json:"cpu_max_mhz,omitempty"`
|
CPUMaxMHz float64 `json:"cpu_max_mhz,omitempty"`
|
||||||
|
CPUPowerW float64 `json:"cpu_power_w,omitempty"`
|
||||||
CPUTemp float64 `json:"cpu_temp,omitempty"`
|
CPUTemp float64 `json:"cpu_temp,omitempty"`
|
||||||
CoreIDs []int `json:"core_ids,omitempty"` // for each logical CPU, the physical core it is a thread of (what the temperature sensors are named by)
|
CoreIDs []int `json:"core_ids,omitempty"` // for each logical CPU, the physical core it is a thread of (what the temperature sensors are named by)
|
||||||
Load [3]float64 `json:"load"`
|
Load [3]float64 `json:"load"`
|
||||||
@@ -1312,7 +1318,7 @@ func (c *Collector) Processes() ProcTable {
|
|||||||
t.Mem, t.Swap = meminfo()
|
t.Mem, t.Swap = meminfo()
|
||||||
if n := len(c.ring); n > 0 {
|
if n := len(c.ring); n > 0 {
|
||||||
last := c.ring[n-1]
|
last := c.ring[n-1]
|
||||||
t.CPU, t.Cores, t.Net, t.CPUTemp = last.CPU, last.Cores, last.Net, cpuTemp(last.Temps)
|
t.CPU, t.Cores, t.Net, t.CPUTemp, t.CPUPowerW = last.CPU, last.Cores, last.Net, cpuTemp(last.Temps), last.CPUPower
|
||||||
t.CoreIDs = coreIDs(len(last.Cores))
|
t.CoreIDs = coreIDs(len(last.Cores))
|
||||||
from := n - 90
|
from := n - 90
|
||||||
if from < 0 {
|
if from < 0 {
|
||||||
|
|||||||
@@ -62,6 +62,9 @@ func flatten(s Sample, want func(id string) bool) []sensors.Sensor {
|
|||||||
if c := cpuTemp(s.Temps); c > 0 {
|
if c := cpuTemp(s.Temps); c > 0 {
|
||||||
f.add("cpu.temp", "CPU temperature", "temperature", "°C", c, t, none)
|
f.add("cpu.temp", "CPU temperature", "temperature", "°C", c, t, none)
|
||||||
}
|
}
|
||||||
|
if s.CPUPower > 0 {
|
||||||
|
f.add("cpu.power", "CPU power", "power", "W", s.CPUPower, t, none)
|
||||||
|
}
|
||||||
for i, w := range []string{"1", "5", "15"} {
|
for i, w := range []string{"1", "5", "15"} {
|
||||||
f.add("load."+w, "Load "+w+" min", "load", "", s.Load[i], t, none)
|
f.add("load."+w, "Load "+w+" min", "load", "", s.Load[i], t, none)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,6 @@ type Policy struct {
|
|||||||
KeepMin int `json:"keep_min"`
|
KeepMin int `json:"keep_min"`
|
||||||
Prefix string `json:"prefix"`
|
Prefix string `json:"prefix"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Policy) prefix() string { return strings.TrimSpace(p.Prefix) }
|
func (p *Policy) prefix() string { return strings.TrimSpace(p.Prefix) }
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ type State struct {
|
|||||||
// orchestrator that has since been removed can still be found on disk. See
|
// orchestrator that has since been removed can still be found on disk. See
|
||||||
// internal/api/stacks.go.
|
// internal/api/stacks.go.
|
||||||
Mounts []RememberedMount `json:"mounts,omitempty"`
|
Mounts []RememberedMount `json:"mounts,omitempty"`
|
||||||
|
// KVMCookies is each KVM's own login, as the cookies its page set in a browser: kept so
|
||||||
|
// the next browser — or this one after it has forgotten them — is signed into the device
|
||||||
|
// already. See internal/api/kvm.go.
|
||||||
|
KVMCookies map[string]map[string]string `json:"kvm_cookies,omitempty"`
|
||||||
SamplerMs int `json:"sampler_ms,omitempty"` // how often this machine samples; the hub keeps every machine on the console's refresh rate
|
SamplerMs int `json:"sampler_ms,omitempty"` // how often this machine samples; the hub keeps every machine on the console's refresh rate
|
||||||
HistorySec int `json:"history_sec,omitempty"` // how far back the graphs reach; the hub keeps every machine on the same one
|
HistorySec int `json:"history_sec,omitempty"` // how far back the graphs reach; the hub keeps every machine on the same one
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -146,7 +146,7 @@ func (m *Manager) prune() {
|
|||||||
}
|
}
|
||||||
var finished []*Task
|
var finished []*Task
|
||||||
for _, t := range m.tasks {
|
for _, t := range m.tasks {
|
||||||
if !t.Running {
|
if !t.running() {
|
||||||
finished = append(finished, t)
|
finished = append(finished, t)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,13 +184,20 @@ func (m *Manager) RunningOf(kind string) (*Task, bool) {
|
|||||||
m.mu.Lock()
|
m.mu.Lock()
|
||||||
defer m.mu.Unlock()
|
defer m.mu.Unlock()
|
||||||
for _, t := range m.tasks {
|
for _, t := range m.tasks {
|
||||||
if t.Kind == kind && t.Running {
|
if t.Kind == kind && t.running() {
|
||||||
return t, true
|
return t, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// running reads the flag the waiter goroutine clears, under the task's own lock.
|
||||||
|
func (t *Task) running() bool {
|
||||||
|
t.mu.Lock()
|
||||||
|
defer t.mu.Unlock()
|
||||||
|
return t.Running
|
||||||
|
}
|
||||||
|
|
||||||
func (t *Task) Snapshot(withOutput bool) Snapshot {
|
func (t *Task) Snapshot(withOutput bool) Snapshot {
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
defer t.mu.Unlock()
|
defer t.mu.Unlock()
|
||||||
|
|||||||
@@ -64,6 +64,9 @@ func New(sudo bool, tm *tasks.Manager) *Manager {
|
|||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PM is the package manager found: apt | dnf | zypper | pacman | apk, or "".
|
||||||
|
func (m *Manager) PM() string { return m.pm }
|
||||||
|
|
||||||
func (m *Manager) sudoArgs(args ...string) []string {
|
func (m *Manager) sudoArgs(args ...string) []string {
|
||||||
if m.sudo {
|
if m.sudo {
|
||||||
return priv.Wrap(args...)
|
return priv.Wrap(args...)
|
||||||
|
|||||||
Generated
+7
@@ -11,6 +11,7 @@
|
|||||||
"@xterm/addon-fit": "^0.10.0",
|
"@xterm/addon-fit": "^0.10.0",
|
||||||
"@xterm/addon-web-links": "^0.11.0",
|
"@xterm/addon-web-links": "^0.11.0",
|
||||||
"@xterm/xterm": "^5.5.0",
|
"@xterm/xterm": "^5.5.0",
|
||||||
|
"hls.js": "^1.7.3",
|
||||||
"qrcode-generator": "^2.0.4",
|
"qrcode-generator": "^2.0.4",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1"
|
"react-dom": "^18.3.1"
|
||||||
@@ -1426,6 +1427,12 @@
|
|||||||
"node": ">=6.9.0"
|
"node": ">=6.9.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/hls.js": {
|
||||||
|
"version": "1.7.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/hls.js/-/hls.js-1.7.3.tgz",
|
||||||
|
"integrity": "sha512-MsPlx6yVW4Qv4C7mEVou4/gk/5cN2dVxOTnrNjPSmyH2f0Ln+/UkG10Ax1PE6OvrmTVPKgQYozTtBaPOuCzq7A==",
|
||||||
|
"license": "Apache-2.0"
|
||||||
|
},
|
||||||
"node_modules/js-tokens": {
|
"node_modules/js-tokens": {
|
||||||
"version": "4.0.0",
|
"version": "4.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz",
|
||||||
|
|||||||
+2
-1
@@ -7,12 +7,13 @@
|
|||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
"build": "tsc --noEmit && vite build",
|
"build": "tsc --noEmit && vite build",
|
||||||
"icons": "node scripts/icons.mjs",
|
"icons": "node scripts/icons.mjs",
|
||||||
"test": "node scripts/test-graphcols.mjs"
|
"test": "node scripts/test-graphcols.mjs && node scripts/test-css-imports.mjs && node scripts/test-resolve-path.mjs"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@xterm/addon-fit": "^0.10.0",
|
"@xterm/addon-fit": "^0.10.0",
|
||||||
"@xterm/addon-web-links": "^0.11.0",
|
"@xterm/addon-web-links": "^0.11.0",
|
||||||
"@xterm/xterm": "^5.5.0",
|
"@xterm/xterm": "^5.5.0",
|
||||||
|
"hls.js": "^1.7.3",
|
||||||
"qrcode-generator": "^2.0.4",
|
"qrcode-generator": "^2.0.4",
|
||||||
"react": "^18.3.1",
|
"react": "^18.3.1",
|
||||||
"react-dom": "^18.3.1"
|
"react-dom": "^18.3.1"
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
// node scripts/test-css-imports.mjs — every stylesheet the UI imports is one somebody has read.
|
||||||
|
// A third-party stylesheet usually carries its demo page's global rules (body, *, button); on
|
||||||
|
// 2026-10-09 one restyled every button in the app. Add a file here only once its rules are
|
||||||
|
// known to touch nothing but its own component.
|
||||||
|
import { readdirSync, readFileSync, statSync } from 'node:fs'
|
||||||
|
import { join, relative } from 'node:path'
|
||||||
|
|
||||||
|
const allowed = new Set(['./styles.css', '@xterm/xterm/css/xterm.css'])
|
||||||
|
const src = new URL('../src', import.meta.url).pathname
|
||||||
|
const files = []
|
||||||
|
const walk = (d) => { for (const n of readdirSync(d)) { const p = join(d, n); if (statSync(p).isDirectory()) { if (n !== 'vendor') walk(p) } else if (/\.(ts|tsx)$/.test(n)) files.push(p) } }
|
||||||
|
walk(src)
|
||||||
|
const fails = []
|
||||||
|
for (const f of files) {
|
||||||
|
for (const m of readFileSync(f, 'utf8').matchAll(/import\s+(?:[^'"]*from\s+)?['"]([^'"]+\.css)['"]/g)) {
|
||||||
|
if (!allowed.has(m[1])) fails.push(`${relative(src, f)} imports ${m[1]}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (fails.length) { console.error('css imports: not allowed:\n ' + fails.join('\n ')); process.exit(1) }
|
||||||
|
console.log(`css imports: ok (${files.length} files)`)
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
// node scripts/test-resolve-path.mjs — the path box resolves like a shell: relative to here,
|
||||||
|
// . and .. applied, trailing slashes dropped, never anything but an absolute path.
|
||||||
|
import { readFileSync } from 'node:fs'
|
||||||
|
const src = readFileSync(new URL('../src/Files.tsx', import.meta.url), 'utf8')
|
||||||
|
const m = src.match(/export function resolvePath[\s\S]*?\n}\n/)
|
||||||
|
if (!m) { console.error('resolvePath not found'); process.exit(1) }
|
||||||
|
const resolvePath = new Function(m[0].replace('export function', 'return function').replace(/: string\[\]/g, '').replace(/: string/g, '') + ' ')()
|
||||||
|
const cases = [
|
||||||
|
['/tank/media', '/x', '/tank/media'], ['/tank/media/', '/x', '/tank/media'], ['', '/here', '/here'], [' ', '/here', '/here'],
|
||||||
|
['sub', '/here', '/here/sub'], ['./sub/', '/here', '/here/sub'], ['..', '/a/b', '/a'], ['../..', '/a/b', '/'], ['../../..', '/a', '/'],
|
||||||
|
['/', '/a', '/'], ['a//b', '/', '/a/b'], ['/tank/./x/../y', '/', '/tank/y'], ['sp ace #1', '/d', '/d/sp ace #1'],
|
||||||
|
]
|
||||||
|
let bad = 0
|
||||||
|
for (const [input, here, want] of cases) { const got = resolvePath(input, here); if (got !== want) { bad++; console.error(`resolvePath(${JSON.stringify(input)}, ${here}) = ${got}, want ${want}`) } }
|
||||||
|
if (bad) process.exit(1)
|
||||||
|
console.log(`resolve path: ok (${cases.length} cases)`)
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
// A textarea that knows what it is holding: line numbers in a gutter, YAML (or a .env)
|
||||||
|
// coloured underneath the text, and the keys a compose file needs — Tab indents by two,
|
||||||
|
// Shift+Tab outdents, Enter keeps the indentation of the line above (one step deeper after
|
||||||
|
// a key or a list dash), and a phone, whose keyboard has none of those, gets a small bar of
|
||||||
|
// them. The text itself stays in a real textarea, so selection, undo and the keyboard are
|
||||||
|
// the browser's own; the colour is a <pre> drawn behind it, kept in step line for line.
|
||||||
|
import { useEffect, useMemo, useRef, useState, ReactNode } from 'react'
|
||||||
|
|
||||||
|
export type Language = 'yaml' | 'env' | 'plain'
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
value: string; onChange: (v: string) => void; language?: Language; className?: string
|
||||||
|
readOnly?: boolean; placeholder?: string; spellCheck?: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
const INDENT = ' '
|
||||||
|
const coarse = () => typeof window !== 'undefined' && window.matchMedia('(pointer: coarse)').matches
|
||||||
|
|
||||||
|
// One line of YAML, split into spans by what each part is. Deliberately shallow: a compose
|
||||||
|
// file is keys, scalars, lists and comments; anything cleverer is a parser, which this is not.
|
||||||
|
function yamlLine(line: string): ReactNode[] {
|
||||||
|
const out: ReactNode[] = []
|
||||||
|
let i = 0
|
||||||
|
const push = (cls: string, text: string) => { if (text) out.push(cls ? <span key={out.length} className={cls}>{text}</span> : text) }
|
||||||
|
const m = /^(\s*)(-\s+)?/.exec(line)!
|
||||||
|
push('', m[1]); push('y-dash', m[2] ?? ''); i = m[0].length
|
||||||
|
const rest = line.slice(i)
|
||||||
|
if (rest.startsWith('#')) { push('y-comment', rest); return out }
|
||||||
|
// a key: up to the first colon that is followed by a space or the end
|
||||||
|
const key = /^("[^"]*"|'[^']*'|[^\s:#][^:#]*?)(:)(?=\s|$)/.exec(rest)
|
||||||
|
let tail = rest
|
||||||
|
if (key) { push('y-key', key[1]); push('y-colon', ':'); tail = rest.slice(key[0].length) }
|
||||||
|
// the value, with a trailing comment cut off
|
||||||
|
let value = tail, comment = ''
|
||||||
|
const c = / #.*$/.exec(tail) ?? (tail.startsWith('#') ? /#.*$/.exec(tail) : null)
|
||||||
|
if (c) { value = tail.slice(0, c.index); comment = tail.slice(c.index) }
|
||||||
|
const v = value.trim()
|
||||||
|
const lead = value.slice(0, value.length - value.trimStart().length)
|
||||||
|
push('', lead)
|
||||||
|
if (!v) { /* nothing */ }
|
||||||
|
else if (/^(["']).*\1$/.test(v)) push('y-string', v)
|
||||||
|
else if (/^(true|false|yes|no|on|off|null|~)$/i.test(v)) push('y-bool', v)
|
||||||
|
else if (/^[-+]?(\d[\d_]*(\.\d+)?|\.\d+)([eE][-+]?\d+)?$/.test(v) || /^0x[0-9a-fA-F]+$/.test(v)) push('y-number', v)
|
||||||
|
else if (/^[&*!]/.test(v)) push('y-anchor', v)
|
||||||
|
else if (/^[|>][-+]?\d*$/.test(v)) push('y-block', v)
|
||||||
|
else push(key ? 'y-value' : (m[2] ? 'y-value' : ''), v)
|
||||||
|
push('', value.slice(lead.length + v.length))
|
||||||
|
push('y-comment', comment)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
function envLine(line: string): ReactNode[] {
|
||||||
|
if (/^\s*#/.test(line)) return [<span key="c" className="y-comment">{line}</span>]
|
||||||
|
const m = /^(\s*(?:export\s+)?)([A-Za-z_][A-Za-z0-9_.-]*)(=)(.*)$/.exec(line)
|
||||||
|
if (!m) return [line]
|
||||||
|
return [m[1], <span key="k" className="y-key">{m[2]}</span>, <span key="e" className="y-colon">=</span>, <span key="v" className="y-string">{m[4]}</span>]
|
||||||
|
}
|
||||||
|
|
||||||
|
function highlight(text: string, lang: Language): ReactNode[] {
|
||||||
|
const lines = text.split('\n')
|
||||||
|
const f = lang === 'yaml' ? yamlLine : lang === 'env' ? envLine : (l: string) => [l] as ReactNode[]
|
||||||
|
return lines.map((l, i) => <div key={i} className="code-line">{f(l)}{l === '' ? '' : ''}</div>)
|
||||||
|
}
|
||||||
|
|
||||||
|
// setText replaces [start, end) in a textarea with s, through the browser's own edit so Undo
|
||||||
|
// still works, and leaves the caret where asked.
|
||||||
|
function setText(ta: HTMLTextAreaElement, start: number, end: number, s: string, caret: number | [number, number]) {
|
||||||
|
ta.focus()
|
||||||
|
ta.setSelectionRange(start, end)
|
||||||
|
let done = false
|
||||||
|
try { done = document.execCommand('insertText', false, s) } catch { /* not every browser */ }
|
||||||
|
if (!done || ta.value.slice(start, start + s.length) !== s) { ta.setRangeText(s, start, end, 'end'); ta.dispatchEvent(new Event('input', { bubbles: true })) }
|
||||||
|
if (Array.isArray(caret)) ta.setSelectionRange(caret[0], caret[1]); else ta.setSelectionRange(caret, caret)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function CodeEditor({ value, onChange, language = 'yaml', className = '', readOnly, placeholder, spellCheck = false }: Props) {
|
||||||
|
const ta = useRef<HTMLTextAreaElement>(null)
|
||||||
|
const pre = useRef<HTMLPreElement>(null)
|
||||||
|
const gutter = useRef<HTMLDivElement>(null)
|
||||||
|
const [touch] = useState(coarse)
|
||||||
|
const lines = useMemo(() => value.split('\n').length, [value])
|
||||||
|
const painted = useMemo(() => highlight(value, language), [value, language])
|
||||||
|
|
||||||
|
// the colour and the numbers follow the text's own scrolling
|
||||||
|
const sync = () => { const t = ta.current; if (!t) return; if (pre.current) { pre.current.scrollTop = t.scrollTop; pre.current.scrollLeft = t.scrollLeft } if (gutter.current) gutter.current.scrollTop = t.scrollTop }
|
||||||
|
useEffect(sync, [value])
|
||||||
|
|
||||||
|
// lines touched by the selection, for indent and outdent
|
||||||
|
const lineRange = (t: HTMLTextAreaElement) => {
|
||||||
|
const s = t.value.lastIndexOf('\n', t.selectionStart - 1) + 1
|
||||||
|
let e = t.value.indexOf('\n', Math.max(t.selectionEnd - (t.selectionEnd > t.selectionStart && t.value[t.selectionEnd - 1] === '\n' ? 1 : 0), t.selectionStart))
|
||||||
|
if (e < 0) e = t.value.length
|
||||||
|
return [s, e] as const
|
||||||
|
}
|
||||||
|
const indent = (out: boolean) => {
|
||||||
|
const t = ta.current; if (!t || readOnly) return
|
||||||
|
const [s, e] = lineRange(t)
|
||||||
|
const block = t.value.slice(s, e)
|
||||||
|
const sel = t.selectionStart !== t.selectionEnd || block.includes('\n')
|
||||||
|
if (!sel && !out) { const at = t.selectionStart; setText(t, at, at, INDENT, at + INDENT.length); return }
|
||||||
|
const next = block.split('\n').map((l) => (out ? l.replace(/^ {1,2}/, '') : INDENT + l)).join('\n')
|
||||||
|
if (next === block) return
|
||||||
|
const d0 = out ? -(block.length - next.length) : INDENT.length
|
||||||
|
setText(t, s, e, next, sel ? [s, s + next.length] : [Math.max(s, t.selectionStart + d0), Math.max(s, t.selectionStart + d0)])
|
||||||
|
}
|
||||||
|
const comment = () => {
|
||||||
|
const t = ta.current; if (!t || readOnly) return
|
||||||
|
const [s, e] = lineRange(t)
|
||||||
|
const block = t.value.slice(s, e), ls = block.split('\n')
|
||||||
|
const all = ls.every((l) => /^\s*#/.test(l) || !l.trim())
|
||||||
|
const next = ls.map((l) => (!l.trim() ? l : all ? l.replace(/^(\s*)# ?/, '$1') : l.replace(/^(\s*)/, '$1# '))).join('\n')
|
||||||
|
setText(t, s, e, next, [s, s + next.length])
|
||||||
|
}
|
||||||
|
const newline = () => {
|
||||||
|
const t = ta.current; if (!t || readOnly) return
|
||||||
|
const at = t.selectionStart
|
||||||
|
const lineStart = t.value.lastIndexOf('\n', at - 1) + 1
|
||||||
|
const line = t.value.slice(lineStart, at)
|
||||||
|
let ind = /^\s*/.exec(line)![0]
|
||||||
|
// one step deeper after a key or a list item that opens a block
|
||||||
|
if (language === 'yaml' && /(:|^\s*-)\s*(#.*)?$/.test(line) && line.trim() && !/^\s*#/.test(line)) ind += INDENT
|
||||||
|
setText(t, at, t.selectionEnd, '\n' + ind, at + 1 + ind.length)
|
||||||
|
}
|
||||||
|
const onKey = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
||||||
|
if (e.key === 'Tab' && !e.ctrlKey && !e.metaKey && !e.altKey) { e.preventDefault(); indent(e.shiftKey) }
|
||||||
|
else if (e.key === 'Enter' && !e.ctrlKey && !e.metaKey && !e.altKey && !e.shiftKey) { e.preventDefault(); newline() }
|
||||||
|
else if (e.key === '/' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); comment() }
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={'code ' + className + (readOnly ? ' ro' : '')}>
|
||||||
|
<div className="code-gutter" ref={gutter} aria-hidden="true">{Array.from({ length: lines }, (_, i) => <div key={i}>{i + 1}</div>)}</div>
|
||||||
|
<div className="code-body">
|
||||||
|
<pre className="code-paint" ref={pre} aria-hidden="true">{painted}</pre>
|
||||||
|
<textarea ref={ta} className="code-text" value={value} onChange={(e) => onChange(e.target.value)} onScroll={sync} onKeyDown={onKey}
|
||||||
|
readOnly={readOnly} placeholder={placeholder} spellCheck={spellCheck} autoCapitalize="off" autoCorrect="off" autoComplete="off" wrap="off" />
|
||||||
|
</div>
|
||||||
|
{touch && !readOnly && (
|
||||||
|
<div className="code-keys" onMouseDown={(e) => e.preventDefault()}>
|
||||||
|
<button type="button" title="outdent" onClick={() => indent(true)}>⇤</button>
|
||||||
|
<button type="button" title="indent" onClick={() => indent(false)}>⇥</button>
|
||||||
|
<button type="button" title="comment" onClick={comment}>#</button>
|
||||||
|
<button type="button" title="list item" onClick={() => { const t = ta.current; if (t) setText(t, t.selectionStart, t.selectionEnd, '- ', t.selectionStart + 2) }}>-</button>
|
||||||
|
<button type="button" title="colon" onClick={() => { const t = ta.current; if (t) setText(t, t.selectionStart, t.selectionEnd, ': ', t.selectionStart + 2) }}>:</button>
|
||||||
|
<button type="button" title="quotes" onClick={() => { const t = ta.current; if (t) setText(t, t.selectionStart, t.selectionEnd, '"' + t.value.slice(t.selectionStart, t.selectionEnd) + '"', t.selectionEnd + 1) }}>"</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
+4
-3
@@ -5,6 +5,7 @@ import { FormEvent, useCallback, useEffect, useMemo, useRef, useState } from 're
|
|||||||
import { api, DockerContainer, DockerEvent, DockerHealth, DockerInfo, DockerLogLine, ImageCheck, MachineUpdates, Stack, StackDiff, UpdateJob, UpdateJobRow, wsURLPath } from './api'
|
import { api, DockerContainer, DockerEvent, DockerHealth, DockerInfo, DockerLogLine, ImageCheck, MachineUpdates, Stack, StackDiff, UpdateJob, UpdateJobRow, wsURLPath } from './api'
|
||||||
import TaskOutput from './TaskOutput'
|
import TaskOutput from './TaskOutput'
|
||||||
import StackNew, { DupFrom } from './StackNew'
|
import StackNew, { DupFrom } from './StackNew'
|
||||||
|
import CodeEditor from './CodeEditor'
|
||||||
import { Sensor, fetchHistories, fmtValue, useSensorStream } from './sensors'
|
import { Sensor, fetchHistories, fmtValue, useSensorStream } from './sensors'
|
||||||
import { useHashSel } from './hashsel'
|
import { useHashSel } from './hashsel'
|
||||||
import { fmtWindowSec, useHistorySec, useRefreshMs } from './prefs'
|
import { fmtWindowSec, useHistorySec, useRefreshMs } from './prefs'
|
||||||
@@ -354,10 +355,10 @@ function StackDetail({ name, startTask, onBack, onChanged, onDuplicate }: { name
|
|||||||
<div className="svc-detail">
|
<div className="svc-detail">
|
||||||
<CloseBtn onClick={onBack} />
|
<CloseBtn onClick={onBack} />
|
||||||
<div className="svc-head"><button className="btn back" onClick={onBack}>‹ Back</button><span className={'sdot ' + (st.containers.length === 0 ? '' : st.running === st.containers.length ? 'ok' : st.running ? 'warn' : 'bad')} /><span className="dname">{st.name}</span><span className="muted small">{st.running}/{st.containers.length} running</span></div>
|
<div className="svc-head"><button className="btn back" onClick={onBack}>‹ Back</button><span className={'sdot ' + (st.containers.length === 0 ? '' : st.running === st.containers.length ? 'ok' : st.running ? 'warn' : 'bad')} /><span className="dname">{st.name}</span><span className="muted small">{st.running}/{st.containers.length} running</span></div>
|
||||||
<div className="muted small mono" style={{ overflowWrap: 'anywhere' }}>{file === 'env' ? (st.host_file.replace(/[^/]+$/, '') + '.env') : (st.host_files?.[file] ?? st.host_file)}{st.held_by && <span className="muted"> · kept on <b>{st.held_by}</b>, deployed to this machine over {st.remote}</span>}{st.via && <span className="muted"> · compose runs inside <b>{st.via}</b>, as it was deployed</span>}{st.has_env && ' · has a .env beside it'}</div>
|
<div className="muted small mono" style={{ overflowWrap: 'anywhere' }}>{file === 'env' ? (st.host_file.replace(/[^/]+$/, '') + '.env') : (st.host_files?.[file] ?? st.host_file)}{st.via && <span className="muted"> · compose runs inside <b>{st.via}</b>, as it was deployed</span>}{st.has_env && ' · has a .env beside it'}</div>
|
||||||
{((st.host_files?.length ?? 0) > 1 || st.has_env) && <div className="chips" title="this stack is deployed from several files, in this order; Deploy uses all of them">{st.host_files!.map((f, i) => <button key={f} className={'chip' + (i === file ? ' on' : '')} onClick={async () => { if (dirty && !(await askYes('Discard unsaved changes to this file?', 'Discard'))) return; setFile(i) }}>{f.split('/').pop()}</button>)}{st.has_env && <button className={'chip' + (file === 'env' ? ' on' : '')} title="the .env beside the compose file: variables compose substitutes" onClick={async () => { if (dirty && !(await askYes('Discard unsaved changes to this file?', 'Discard'))) return; setFile('env') }}>.env</button>}</div>}
|
{((st.host_files?.length ?? 0) > 1 || st.has_env) && <div className="chips" title="this stack is deployed from several files, in this order; Deploy uses all of them">{st.host_files!.map((f, i) => <button key={f} className={'chip' + (i === file ? ' on' : '')} onClick={async () => { if (dirty && !(await askYes('Discard unsaved changes to this file?', 'Discard'))) return; setFile(i) }}>{f.split('/').pop()}</button>)}{st.has_env && <button className={'chip' + (file === 'env' ? ' on' : '')} title="the .env beside the compose file: variables compose substitutes" onClick={async () => { if (dirty && !(await askYes('Discard unsaved changes to this file?', 'Discard'))) return; setFile('env') }}>.env</button>}</div>}
|
||||||
{file === 'env' && !reveal ? <div className="env-masked"><pre className="editor stack-editor">{maskEnv(text)}</pre><button className="btn" onClick={() => setReveal(true)}>Reveal and edit</button></div> :
|
{file === 'env' && !reveal ? <div className="env-masked"><pre className="editor stack-editor">{maskEnv(text)}</pre><button className="btn" onClick={() => setReveal(true)}>Reveal and edit</button></div> :
|
||||||
<textarea className="editor stack-editor" value={text} onChange={(e) => setText(e.target.value)} spellCheck={false} autoCapitalize="off" autoCorrect="off" />}
|
<CodeEditor className="stack-editor" language={file === 'env' ? 'env' : 'yaml'} value={text} onChange={setText} />}
|
||||||
{err && <div className="error" style={{ whiteSpace: 'pre-wrap' }}>{err}{/changed on disk/.test(err) && <> <button className="link" onClick={async () => { if (await askYes('Reload the file from disk? Your text in the box is replaced.', 'Reload')) load() }}>reload it</button></>}</div>}
|
{err && <div className="error" style={{ whiteSpace: 'pre-wrap' }}>{err}{/changed on disk/.test(err) && <> <button className="link" onClick={async () => { if (await askYes('Reload the file from disk? Your text in the box is replaced.', 'Reload')) load() }}>reload it</button></>}</div>}
|
||||||
<div className="svc-actions">
|
<div className="svc-actions">
|
||||||
<Btn className="btn on" disabled={!dirty} onClick={save}>Save</Btn>
|
<Btn className="btn on" disabled={!dirty} onClick={save}>Save</Btn>
|
||||||
@@ -446,7 +447,7 @@ export default function Docker() {
|
|||||||
{shownTab === 'stacks' && (stacks ?? []).map((st) => (
|
{shownTab === 'stacks' && (stacks ?? []).map((st) => (
|
||||||
<div key={st.name} className={'svc-row' + (st.name === selStack ? ' active' : '')} onClick={() => st.editable ? setSel('stack:' + st.name) : toast(st.note ?? 'not editable here', 'info')}>
|
<div key={st.name} className={'svc-row' + (st.name === selStack ? ' active' : '')} onClick={() => st.editable ? setSel('stack:' + st.name) : toast(st.note ?? 'not editable here', 'info')}>
|
||||||
<span className={'sdot ' + (st.containers.length === 0 ? '' : st.running === st.containers.length ? 'ok' : st.running ? 'warn' : 'bad')} />
|
<span className={'sdot ' + (st.containers.length === 0 ? '' : st.running === st.containers.length ? 'ok' : st.running ? 'warn' : 'bad')} />
|
||||||
<div className="svc-name"><div className="n">{st.name}</div><div className="d">{st.editable ? (st.held_by ? `on ${st.held_by}: ` : '') + st.host_file : st.note}</div></div>
|
<div className="svc-name"><div className="n">{st.name}</div><div className="d">{st.editable ? st.host_file : st.note}</div></div>
|
||||||
<div className="svc-state">{st.containers.length === 0 ? 'not deployed' : `${st.running}/${st.containers.length}`}{!st.editable && <><br />read-only</>}</div>
|
<div className="svc-state">{st.containers.length === 0 ? 'not deployed' : `${st.running}/${st.containers.length}`}{!st.editable && <><br />read-only</>}</div>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
+232
-18
@@ -1,10 +1,25 @@
|
|||||||
import { DragEvent, useCallback, useEffect, useRef, useState } from 'react'
|
import { DragEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||||
import { api, FileEntry, FileInfo } from './api'
|
import { api, FileEntry, FileInfo, TaskInfo, ThumbStatus } from './api'
|
||||||
import { fmtBytes } from './fmt'
|
import { fmtBytes } from './fmt'
|
||||||
import { Btn, Loading, Spinner, askText, askYes, setUnsaved, toast, useEscape } from './ui'
|
import { Btn, Loading, Spinner, askText, askYes, setUnsaved, toast, useEscape } from './ui'
|
||||||
import { useHashSel } from './hashsel'
|
import { useHashSel } from './hashsel'
|
||||||
|
import TaskOutput from './TaskOutput'
|
||||||
|
import Player from './Player'
|
||||||
|
|
||||||
const join = (dir: string, name: string) => (dir === '/' ? '/' + name : dir + '/' + name)
|
const join = (dir: string, name: string) => (dir === '/' ? '/' + name : dir + '/' + name)
|
||||||
|
// A typed path, made absolute: relative to the folder being looked at, "." and ".." applied,
|
||||||
|
// trailing slashes dropped — what a shell would make of it.
|
||||||
|
export function resolvePath(input: string, here: string): string {
|
||||||
|
const raw = input.trim()
|
||||||
|
if (!raw) return here
|
||||||
|
const parts = (raw.startsWith('/') ? [] : here.split('/')).concat(raw.split('/'))
|
||||||
|
const out: string[] = []
|
||||||
|
for (const p of parts) {
|
||||||
|
if (p === '' || p === '.') continue
|
||||||
|
if (p === '..') out.pop(); else out.push(p)
|
||||||
|
}
|
||||||
|
return '/' + out.join('/')
|
||||||
|
}
|
||||||
const fmtTime = (ms: number) => { const d = new Date(ms); const now = new Date(); return d.toDateString() === now.toDateString() ? d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) : d.toLocaleDateString(undefined, { year: d.getFullYear() === now.getFullYear() ? undefined : 'numeric', month: 'short', day: '2-digit' }) }
|
const fmtTime = (ms: number) => { const d = new Date(ms); const now = new Date(); return d.toDateString() === now.toDateString() ? d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }) : d.toLocaleDateString(undefined, { year: d.getFullYear() === now.getFullYear() ? undefined : 'numeric', month: 'short', day: '2-digit' }) }
|
||||||
const isImage = (n: string) => /\.(png|jpe?g|gif|webp|svg|bmp|ico|avif)$/i.test(n)
|
const isImage = (n: string) => /\.(png|jpe?g|gif|webp|svg|bmp|ico|avif)$/i.test(n)
|
||||||
const shellQuote = (s: string) => "'" + s.replace(/'/g, "'\\''") + "'"
|
const shellQuote = (s: string) => "'" + s.replace(/'/g, "'\\''") + "'"
|
||||||
@@ -14,7 +29,9 @@ function icon(e: FileEntry): string {
|
|||||||
if (e.type === 'dir') return e.target ? '🔗' : '📁'
|
if (e.type === 'dir') return e.target ? '🔗' : '📁'
|
||||||
if (e.type === 'link') return e.broken ? '⛓️💥' : '🔗'
|
if (e.type === 'link') return e.broken ? '⛓️💥' : '🔗'
|
||||||
if (e.type === 'other') return '⚙️'
|
if (e.type === 'other') return '⚙️'
|
||||||
if (isImage(e.name)) return '🖼️'
|
if (e.preview === 'image' || isImage(e.name)) return '🖼️'
|
||||||
|
if (e.preview === 'video') return '🎞️'
|
||||||
|
if (e.preview === 'audio') return '🎵'
|
||||||
if (/\.(zip|gz|bz2|xz|zst|tar|tgz|7z|rar)$/i.test(e.name)) return '🗜️'
|
if (/\.(zip|gz|bz2|xz|zst|tar|tgz|7z|rar)$/i.test(e.name)) return '🗜️'
|
||||||
if (/\.(sh|bash|zsh|py|go|js|ts|tsx|rb|pl|php|rs|c|h|cpp|java|lua)$/i.test(e.name) || e.mode.includes('x')) return '📜'
|
if (/\.(sh|bash|zsh|py|go|js|ts|tsx|rb|pl|php|rs|c|h|cpp|java|lua)$/i.test(e.name) || e.mode.includes('x')) return '📜'
|
||||||
if (/\.(log|txt|md|json|ya?ml|toml|ini|conf|cfg|env|service|timer)$/i.test(e.name)) return '📄'
|
if (/\.(log|txt|md|json|ya?ml|toml|ini|conf|cfg|env|service|timer)$/i.test(e.name)) return '📄'
|
||||||
@@ -22,6 +39,163 @@ function icon(e: FileEntry): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Sort = 'name' | 'size' | 'mtime'
|
type Sort = 'name' | 'size' | 'mtime'
|
||||||
|
const PREVIEWS_KEY = 'hamlab.files.previews'
|
||||||
|
const byName = (a: FileEntry, b: FileEntry) => a.name.localeCompare(b.name, undefined, { numeric: true, sensitivity: 'base' })
|
||||||
|
// A name is one path segment. ("../x" and "a/b" would quietly act somewhere else.)
|
||||||
|
const okName = (n?: string | null): n is string => { const t = n?.trim(); if (!t) return false; if (t.includes('/') || t === '.' || t === '..') { toast('a name can\'t contain / or be . or .. — use Move… to put it elsewhere', 'err'); return false } return true }
|
||||||
|
|
||||||
|
// ---- move: walk to the destination, then "Move here" ----
|
||||||
|
function MovePicker({ item, from, sudo, hidden, onClose, onMoved }: { item: FileEntry; from: string; sudo: boolean; hidden: boolean; onClose: () => void; onMoved: (task?: TaskInfo) => void }) {
|
||||||
|
const src = join(from, item.name)
|
||||||
|
const isFolder = item.type === 'dir' && !item.target
|
||||||
|
const [hist, setHist] = useState<string[]>([from]) // where the picker has been, for back and forward
|
||||||
|
const [at, setAt] = useState(0)
|
||||||
|
const dir = hist[at]
|
||||||
|
const [entries, setEntries] = useState<FileEntry[] | null>(null)
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [pathInput, setPathInput] = useState(from)
|
||||||
|
const [busy, setBusy] = useState(false)
|
||||||
|
const load = useCallback(async (p: string) => {
|
||||||
|
setEntries(null); setErr('')
|
||||||
|
try { const l = await api.files(p, sudo); setEntries(l.entries) } catch (e) { setErr(e instanceof Error ? e.message : 'failed'); setEntries([]) }
|
||||||
|
}, [sudo])
|
||||||
|
useEffect(() => { setPathInput(dir); load(dir) }, [dir, load])
|
||||||
|
useEscape(onClose)
|
||||||
|
const go = (input: string) => {
|
||||||
|
const p = resolvePath(input, dir)
|
||||||
|
if (p === dir) return
|
||||||
|
setHist((h) => [...h.slice(0, at + 1), p]); setAt(at + 1)
|
||||||
|
}
|
||||||
|
const up = () => { if (dir !== '/') go(dir.slice(0, dir.lastIndexOf('/')) || '/') }
|
||||||
|
const folders = (entries ?? []).filter((e) => e.type === 'dir' && (hidden || !e.name.startsWith('.'))).sort(byName)
|
||||||
|
const crumbs = dir === '/' ? [] : dir.slice(1).split('/')
|
||||||
|
const dest = join(dir, item.name)
|
||||||
|
const why = dir === from ? 'it is already here'
|
||||||
|
: isFolder && (dir === src || dir.startsWith(src + '/')) ? 'a folder can\'t move into itself'
|
||||||
|
: (entries ?? []).some((e) => e.name === item.name) ? `something called ${item.name} is already there` : ''
|
||||||
|
const move = async () => {
|
||||||
|
setBusy(true)
|
||||||
|
try {
|
||||||
|
const t = await api.fileMove(src, dest, sudo)
|
||||||
|
if (t) toast(`${dir} is on another filesystem — copying ${item.name} there, then removing it here`, 'info')
|
||||||
|
else toast(`moved ${item.name} to ${dir}`)
|
||||||
|
onMoved(t)
|
||||||
|
} catch (e) { toast(`move — ${e instanceof Error ? e.message : 'failed'}`, 'err'); setBusy(false) }
|
||||||
|
}
|
||||||
|
const mkdir = async () => {
|
||||||
|
const n = await askText('New folder name')
|
||||||
|
if (!okName(n)) return
|
||||||
|
try { await api.fileMkdir(join(dir, n.trim()), sudo); go(join(dir, n.trim())) } catch (e) { toast(`create folder — ${e instanceof Error ? e.message : 'failed'}`, 'err') }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="modal-back" onClick={onClose}>
|
||||||
|
<div className="modal mover" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<h3>Move {isFolder ? 'folder ' : ''}<span className="mono">{item.name}</span></h3>
|
||||||
|
<div className="mover-nav">
|
||||||
|
<button className="icon-btn" title="back" disabled={at === 0} onClick={() => setAt(at - 1)}>‹</button>
|
||||||
|
<button className="icon-btn" title="forward" disabled={at >= hist.length - 1} onClick={() => setAt(at + 1)}>›</button>
|
||||||
|
<button className="icon-btn" title="up one level" disabled={dir === '/'} onClick={up}>↑</button>
|
||||||
|
<form className="crumbs" onSubmit={(e) => { e.preventDefault(); go(pathInput) }}>
|
||||||
|
<button type="button" className="crumb" onClick={() => go('/')}>/</button>
|
||||||
|
{crumbs.map((c, i) => <button type="button" key={i} className="crumb" onClick={() => go('/' + crumbs.slice(0, i + 1).join('/'))}>{c}</button>)}
|
||||||
|
<input className="path-input" value={pathInput} onChange={(e) => setPathInput(e.target.value)} onBlur={() => setPathInput(dir)} spellCheck={false} autoCapitalize="off" autoCorrect="off" />
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div className="mover-list">
|
||||||
|
{entries === null && <Loading label="Listing…" />}
|
||||||
|
{err && <div className="error" style={{ margin: 10 }}>{err}</div>}
|
||||||
|
{folders.map((e) => {
|
||||||
|
const self = dir === from && e.name === item.name
|
||||||
|
return (
|
||||||
|
<div key={e.name} className={'mover-row' + (self ? ' self' : '')} onClick={() => { if (!self) go(join(dir, e.name)) }}>
|
||||||
|
<span className="ficon">{e.target ? '🔗' : '📁'}</span>
|
||||||
|
<span className="fname">{e.name}</span>
|
||||||
|
{self && <span className="muted small">this is what's moving</span>}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
})}
|
||||||
|
{entries !== null && folders.length === 0 && !err && <div className="muted small" style={{ padding: 12 }}>No folders here — “Move here” puts it in this one.</div>}
|
||||||
|
</div>
|
||||||
|
<div className="modal-foot">
|
||||||
|
<div className="svc-actions">
|
||||||
|
<button className="btn" onClick={mkdir}>+ folder</button>
|
||||||
|
<span className="spacer" />
|
||||||
|
<button className="btn" onClick={onClose}>Cancel</button>
|
||||||
|
<Btn className="btn on" disabled={!!why || entries === null} busy={busy} onClick={move}>Move here</Btn>
|
||||||
|
</div>
|
||||||
|
<div className="muted small mono mover-dest">{why || `→ ${dest}`}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- previews ----
|
||||||
|
|
||||||
|
// Rows ask for their thumbnail only as they scroll near: a browser's own lazy loading
|
||||||
|
// watches the window, and this list scrolls inside a box of its own. One observer for the
|
||||||
|
// list, a callback per row, each fired once.
|
||||||
|
class Near {
|
||||||
|
private obs: IntersectionObserver
|
||||||
|
private cbs = new Map<Element, () => void>()
|
||||||
|
constructor(root: Element) {
|
||||||
|
this.obs = new IntersectionObserver((ents) => {
|
||||||
|
for (const e of ents) if (e.isIntersecting) { const cb = this.cbs.get(e.target); if (cb) { this.forget(e.target); cb() } }
|
||||||
|
}, { root, rootMargin: '400px 0px' })
|
||||||
|
}
|
||||||
|
watch(el: Element, cb: () => void) { this.cbs.set(el, cb); this.obs.observe(el); return () => this.forget(el) }
|
||||||
|
private forget(el: Element) { this.cbs.delete(el); this.obs.unobserve(el) }
|
||||||
|
}
|
||||||
|
|
||||||
|
function Thumb({ url, kind, near, fallback }: { url: string; kind: 'image' | 'video' | 'audio'; near: Near | null; fallback: string }) {
|
||||||
|
const ref = useRef<HTMLSpanElement>(null)
|
||||||
|
const [show, setShow] = useState(false)
|
||||||
|
const [failed, setFailed] = useState('')
|
||||||
|
useEffect(() => { const el = ref.current; if (!el || !near || show) return; return near.watch(el, () => setShow(true)) }, [near, show])
|
||||||
|
useEffect(() => { setFailed('') }, [url]) // a changed file is a new URL, and worth another try
|
||||||
|
const broken = failed === url
|
||||||
|
return (
|
||||||
|
<span ref={ref} className={'fthumb ' + kind} title={broken ? 'no preview for this file' : undefined}>
|
||||||
|
{show && !broken && <img src={url} alt="" decoding="async" onError={() => setFailed(url)} />}
|
||||||
|
{(!show || broken) && <span className="ficon">{fallback}</span>}
|
||||||
|
</span>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// What the previews button says about how they get made here, and the one-tap fixes.
|
||||||
|
function ThumbNote({ st, sudo, onChanged }: { st: ThumbStatus | null; sudo: boolean; onChanged: () => void }) {
|
||||||
|
const [task, setTask] = useState<{ id: string; restart: boolean } | null>(null)
|
||||||
|
const [waiting, setWaiting] = useState(false)
|
||||||
|
// the GPU fix ends by restarting Ham Lab: poll until it answers again, then re-read the status
|
||||||
|
useEffect(() => {
|
||||||
|
if (!waiting) return
|
||||||
|
const t = setInterval(async () => { try { await api.thumbStatus(sudo, true); setWaiting(false); onChanged() } catch { /* restarting */ } }, 3000)
|
||||||
|
return () => clearInterval(t)
|
||||||
|
}, [waiting, sudo, onChanged])
|
||||||
|
if (!st) return null
|
||||||
|
const blocked = (st.unusable ?? []).filter((g) => g.permission)
|
||||||
|
const start = async (label: string, fn: () => Promise<{ id: string }>, restart: boolean) => {
|
||||||
|
try { const t = await fn(); setTask({ id: t.id, restart }) } catch (e) { toast(`${label} — ${e instanceof Error ? e.message : 'failed'}`, 'err') }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{!st.ok && (
|
||||||
|
<div className="thumb-note notice">
|
||||||
|
<span>Previews need ffmpeg: {st.error || 'it is not installed on this machine'}.</span>
|
||||||
|
{st.install && !task && <Btn className="btn on" onClick={() => start('install', api.thumbInstall, false)}>Install ffmpeg with {st.install.pm}</Btn>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{st.ok && blocked.length > 0 && (
|
||||||
|
<div className="thumb-note notice">
|
||||||
|
<span>{blocked.map((g) => g.name || g.device).join(', ')} is here, but Ham Lab runs as {st.user} and can't open {blocked.length > 1 ? 'them' : 'it'} ({blocked[0].reason}). Previews of videos are on the CPU until it can.</span>
|
||||||
|
{st.gpu_fix && !task && !waiting && <Btn className="btn on" confirm="Ham Lab adds itself to the GPU's group and restarts. Open terminals are interrupted. Continue?" onClick={() => start('GPU access', api.thumbGPUAccess, true)}>Allow GPU access</Btn>}
|
||||||
|
{waiting && <span className="muted small"><Spinner /> Ham Lab is restarting; the page reconnects by itself.</span>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{task && <TaskOutput id={task.id} onDone={(t) => { const ok = t.exit_code === 0 && !t.error; if (ok && task.restart) setWaiting(true); else if (ok) onChanged(); else toast('failed — the output says where', 'err'); setTask(null) }} />}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// ---- editor / viewer ----
|
// ---- editor / viewer ----
|
||||||
function Viewer({ path, sudo, onClose, onChanged }: { path: string; sudo: boolean; onClose: () => void; onChanged: () => void }) {
|
function Viewer({ path, sudo, onClose, onChanged }: { path: string; sudo: boolean; onClose: () => void; onChanged: () => void }) {
|
||||||
@@ -30,11 +204,12 @@ function Viewer({ path, sudo, onClose, onChanged }: { path: string; sudo: boolea
|
|||||||
const [orig, setOrig] = useState('')
|
const [orig, setOrig] = useState('')
|
||||||
const [err, setErr] = useState('')
|
const [err, setErr] = useState('')
|
||||||
const [saving, setSaving] = useState(false)
|
const [saving, setSaving] = useState(false)
|
||||||
|
const [cantPlay, setCantPlay] = useState(false) // the browser tried the file as it is and gave up
|
||||||
const name = path.split('/').pop() ?? path
|
const name = path.split('/').pop() ?? path
|
||||||
const dirty = text !== null && text !== orig
|
const dirty = text !== null && text !== orig
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setInfo(null); setText(null); setErr('')
|
setInfo(null); setText(null); setErr(''); setCantPlay(false)
|
||||||
let stop = false
|
let stop = false
|
||||||
;(async () => {
|
;(async () => {
|
||||||
try {
|
try {
|
||||||
@@ -92,8 +267,16 @@ function Viewer({ path, sudo, onClose, onChanged }: { path: string; sudo: boolea
|
|||||||
<textarea className="editor file-editor" value={text} spellCheck={false} onChange={(e) => setText(e.target.value)} />
|
<textarea className="editor file-editor" value={text} spellCheck={false} onChange={(e) => setText(e.target.value)} />
|
||||||
)}
|
)}
|
||||||
{info && text === null && isImage(name) && <div className="preview"><img src={api.fileReadURL(path, sudo)} alt={name} /></div>}
|
{info && text === null && isImage(name) && <div className="preview"><img src={api.fileReadURL(path, sudo)} alt={name} /></div>}
|
||||||
{info && text === null && !isImage(name) && !err && (
|
{/* Video: the browser's own decoder first, and a re-encode on the machine when that fails. Audio as it is. */}
|
||||||
<div className="notice">{info.type === 'dir' ? 'This is a directory.' : info.size > MAX_EDIT ? `Too large to edit here (${fmtBytes(info.size)}). Download it instead.` : 'Binary file. Download it instead.'}</div>
|
{info && text === null && !isImage(name) && info.preview === 'video' && <Player path={path} sudo={sudo} name={name} />}
|
||||||
|
{info && text === null && !isImage(name) && info.preview === 'audio' && !cantPlay && (
|
||||||
|
<div className="preview audio"><div className="audio-box"><div className="ficon">🎵</div><div className="mono small">{name}</div><audio controls preload="metadata" src={api.fileReadURL(path, sudo)} onError={() => setCantPlay(true)} /></div></div>
|
||||||
|
)}
|
||||||
|
{info && text === null && !isImage(name) && info.preview === 'audio' && cantPlay && (
|
||||||
|
<div className="notice">This browser can't play {name} as it is — the codec isn't one it decodes. Download it instead.</div>
|
||||||
|
)}
|
||||||
|
{info && text === null && !isImage(name) && !info.preview && !err && (
|
||||||
|
<div className="notice">{info.type === 'dir' ? 'This is a directory.' : info.type === 'other' ? 'This is a device, pipe or socket — nothing to show.' : info.broken ? `This link points at nothing (${info.target}).` : info.size > MAX_EDIT ? `Too large to edit here (${fmtBytes(info.size)}). Download it instead.` : 'Binary file. Download it instead.'}</div>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
@@ -114,10 +297,19 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
const [pathInput, setPathInput] = useState(path)
|
const [pathInput, setPathInput] = useState(path)
|
||||||
const [open, setOpen] = useState<string | null>(null)
|
const [open, setOpen] = useState<string | null>(null)
|
||||||
const [menu, setMenu] = useState<string | null>(null)
|
const [menu, setMenu] = useState<string | null>(null)
|
||||||
|
const [moving, setMoving] = useState<FileEntry | null>(null)
|
||||||
|
const [moveTask, setMoveTask] = useState<TaskInfo | null>(null)
|
||||||
const [upload, setUpload] = useState<{ name: string; pct: number; n: number; of: number } | null>(null)
|
const [upload, setUpload] = useState<{ name: string; pct: number; n: number; of: number } | null>(null)
|
||||||
const [drag, setDrag] = useState(false)
|
const [drag, setDrag] = useState(false)
|
||||||
const fileInput = useRef<HTMLInputElement>(null)
|
const fileInput = useRef<HTMLInputElement>(null)
|
||||||
const canSudo = useRef(true)
|
const canSudo = useRef(true)
|
||||||
|
const [previews, setPreviews] = useState<boolean>(() => { try { return localStorage.getItem(PREVIEWS_KEY) === '1' } catch { return false } })
|
||||||
|
const [thumbs, setThumbs] = useState<ThumbStatus | null>(null)
|
||||||
|
const [listEl, setListEl] = useState<HTMLDivElement | null>(null)
|
||||||
|
const near = useMemo(() => (listEl && previews ? new Near(listEl) : null), [listEl, previews])
|
||||||
|
const readThumbs = useCallback((refresh = false) => { api.thumbStatus(sudo, refresh).then(setThumbs).catch((e) => setThumbs({ ok: false, engine: '', summary: '', gpus: [], error: e instanceof Error ? e.message : 'failed', user: '', cache: { path: '', entries: 0, bytes: 0 } })) }, [sudo])
|
||||||
|
useEffect(() => { try { localStorage.setItem(PREVIEWS_KEY, previews ? '1' : '0') } catch { /* private mode */ } }, [previews])
|
||||||
|
useEffect(() => { if (previews) readThumbs(); else setThumbs(null) }, [previews, readThumbs])
|
||||||
|
|
||||||
const load = useCallback(async (p = path, s = sudo) => {
|
const load = useCallback(async (p = path, s = sudo) => {
|
||||||
try {
|
try {
|
||||||
@@ -127,15 +319,22 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
sessionStorage.setItem('hamlab.files.path', p) // only a folder that opened is worth coming back to
|
sessionStorage.setItem('hamlab.files.path', p) // only a folder that opened is worth coming back to
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const msg = e instanceof Error ? e.message : 'failed'
|
const msg = e instanceof Error ? e.message : 'failed'
|
||||||
|
if (/not a directory/i.test(msg) && p !== '/') { // a file was typed: open it, from its folder
|
||||||
|
setPath(p.slice(0, p.lastIndexOf('/')) || '/'); setOpen(p); return
|
||||||
|
}
|
||||||
|
if (/no such file/i.test(msg) && p !== '/' && !sessionStorage.getItem('hamlab.files.lost')) { // gone since last time (another machine, a deleted folder)
|
||||||
|
sessionStorage.setItem('hamlab.files.lost', '1'); toast(`${p} isn't here — showing /`, 'info'); setPath('/'); return
|
||||||
|
}
|
||||||
setErr(msg); setEntries([])
|
setErr(msg); setEntries([])
|
||||||
}
|
}
|
||||||
|
sessionStorage.removeItem('hamlab.files.lost')
|
||||||
}, [path, sudo])
|
}, [path, sudo])
|
||||||
|
|
||||||
useEffect(() => { setEntries(null); setPathInput(path); setMenu(null); load(path, sudo); sessionStorage.setItem('hamlab.files.sudo', sudo ? '1' : '0') }, [path, sudo, load])
|
useEffect(() => { setEntries(null); setPathInput(path); setMenu(null); load(path, sudo); sessionStorage.setItem('hamlab.files.sudo', sudo ? '1' : '0') }, [path, sudo, load])
|
||||||
useEffect(() => { const h = () => setMenu(null); window.addEventListener('click', h); return () => window.removeEventListener('click', h) }, [])
|
useEffect(() => { const h = () => setMenu(null); window.addEventListener('click', h); return () => window.removeEventListener('click', h) }, [])
|
||||||
useEscape(() => setMenu(null), menu !== null)
|
useEscape(() => setMenu(null), menu !== null)
|
||||||
|
|
||||||
const go = (p: string) => { setOpen(null); setPath(p || '/') }
|
const go = (p: string) => { setOpen(null); setPath(resolvePath(p, path)) }
|
||||||
const crumbs = path === '/' ? [] : path.slice(1).split('/')
|
const crumbs = path === '/' ? [] : path.slice(1).split('/')
|
||||||
|
|
||||||
const shown = (entries ?? []).filter((e) => (hidden || !e.name.startsWith('.')) && (!q || e.name.toLowerCase().includes(q.toLowerCase())))
|
const shown = (entries ?? []).filter((e) => (hidden || !e.name.startsWith('.')) && (!q || e.name.toLowerCase().includes(q.toLowerCase())))
|
||||||
@@ -147,15 +346,13 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
})
|
})
|
||||||
|
|
||||||
// ---- actions ----
|
// ---- actions ----
|
||||||
const act = async (label: string, fn: () => Promise<unknown>) => {
|
const act = async (label: string, fn: () => Promise<unknown>): Promise<boolean> => {
|
||||||
try { await fn(); toast(label); await load() } catch (e) { toast(`${label} — ${e instanceof Error ? e.message : 'failed'}`, 'err') }
|
try { await fn(); toast(label); await load(); return true } catch (e) { toast(`${label} — ${e instanceof Error ? e.message : 'failed'}`, 'err'); return false }
|
||||||
}
|
}
|
||||||
// A name is one path segment. ("../x" and "a/b" would quietly act somewhere else.)
|
|
||||||
const okName = (n?: string | null): n is string => { const t = n?.trim(); if (!t) return false; if (t.includes('/') || t === '.' || t === '..') { toast('a name can\'t contain / or be . or .. — use Move… to put it elsewhere', 'err'); return false } return true }
|
|
||||||
const mkdir = async () => { const n = await askText('New folder name'); if (okName(n)) act(`created ${n.trim()}`, () => api.fileMkdir(join(path, n.trim()), sudo)) }
|
const mkdir = async () => { const n = await askText('New folder name'); if (okName(n)) act(`created ${n.trim()}`, () => api.fileMkdir(join(path, n.trim()), sudo)) }
|
||||||
const touch = async () => { const n = await askText('New file name'); if (okName(n)) act(`created ${n.trim()}`, () => api.fileTouch(join(path, n.trim()), sudo)).then(() => setOpen(join(path, n.trim()))) }
|
const touch = async () => { const n = await askText('New file name'); if (okName(n) && await act(`created ${n.trim()}`, () => api.fileTouch(join(path, n.trim()), sudo))) setOpen(join(path, n.trim())) } // opened only once it exists
|
||||||
const rename = async (e: FileEntry) => { const n = await askText('Rename to', e.name); if (okName(n) && n.trim() !== e.name) act(`renamed to ${n.trim()}`, () => api.fileRename(join(path, e.name), join(path, n.trim()), sudo)) }
|
const rename = async (e: FileEntry) => { const n = await askText('Rename to', e.name); if (okName(n) && n.trim() !== e.name) act(`renamed to ${n.trim()}`, () => api.fileRename(join(path, e.name), join(path, n.trim()), sudo)) }
|
||||||
const move = async (e: FileEntry) => { const n = await askText('Move to (absolute path)', join(path, e.name)); if (n?.trim() && n.trim() !== join(path, e.name)) act(`moved to ${n.trim()}`, () => api.fileRename(join(path, e.name), n.trim(), sudo)) }
|
const move = (e: FileEntry) => setMoving(e)
|
||||||
const chmod = async (e: FileEntry) => { const n = await askText(`Permissions for ${e.name} (octal)`, e.perm); if (n?.trim() && n.trim() !== e.perm) act(`chmod ${n.trim()} ${e.name}`, () => api.fileChmod(join(path, e.name), n.trim(), sudo)) }
|
const chmod = async (e: FileEntry) => { const n = await askText(`Permissions for ${e.name} (octal)`, e.perm); if (n?.trim() && n.trim() !== e.perm) act(`chmod ${n.trim()} ${e.name}`, () => api.fileChmod(join(path, e.name), n.trim(), sudo)) }
|
||||||
const del = async (e: FileEntry) => {
|
const del = async (e: FileEntry) => {
|
||||||
const full = join(path, e.name)
|
const full = join(path, e.name)
|
||||||
@@ -171,6 +368,7 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
// ---- upload ----
|
// ---- upload ----
|
||||||
const uploadFiles = async (files: FileList | File[]) => {
|
const uploadFiles = async (files: FileList | File[]) => {
|
||||||
const list = Array.from(files)
|
const list = Array.from(files)
|
||||||
|
let done = 0
|
||||||
for (let i = 0; i < list.length; i++) {
|
for (let i = 0; i < list.length; i++) {
|
||||||
const f = list[i]
|
const f = list[i]
|
||||||
let overwrite = false
|
let overwrite = false
|
||||||
@@ -181,13 +379,22 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
setUpload({ name: f.name, pct: 0, n: i + 1, of: list.length })
|
setUpload({ name: f.name, pct: 0, n: i + 1, of: list.length })
|
||||||
try {
|
try {
|
||||||
await api.fileUpload(path, f, sudo, (pct) => setUpload({ name: f.name, pct, n: i + 1, of: list.length }), overwrite)
|
await api.fileUpload(path, f, sudo, (pct) => setUpload({ name: f.name, pct, n: i + 1, of: list.length }), overwrite)
|
||||||
|
done++
|
||||||
} catch (e) { toast(`upload ${f.name} — ${e instanceof Error ? e.message : 'failed'}`, 'err'); break }
|
} catch (e) { toast(`upload ${f.name} — ${e instanceof Error ? e.message : 'failed'}`, 'err'); break }
|
||||||
}
|
}
|
||||||
setUpload(null)
|
setUpload(null)
|
||||||
toast(list.length === 1 ? `uploaded ${list[0].name}` : `uploaded ${list.length} files`)
|
if (done === 1) toast(`uploaded ${list.length === 1 ? list[0].name : '1 file'}`)
|
||||||
load()
|
else if (done > 1) toast(`uploaded ${done} files`)
|
||||||
|
if (done > 0) load()
|
||||||
|
}
|
||||||
|
// A dropped folder arrives as a nameless 0-byte "file"; the browser gives no way to read it here.
|
||||||
|
const onDrop = (e: DragEvent) => {
|
||||||
|
e.preventDefault(); setDrag(false)
|
||||||
|
const items = Array.from(e.dataTransfer.items ?? [])
|
||||||
|
const files = Array.from(e.dataTransfer.files).filter((_, i) => !(items[i]?.webkitGetAsEntry?.()?.isDirectory))
|
||||||
|
if (files.length < e.dataTransfer.files.length) toast('folders can\'t be dropped — drop the files inside, or upload a zip', 'err')
|
||||||
|
if (files.length) uploadFiles(files)
|
||||||
}
|
}
|
||||||
const onDrop = (e: DragEvent) => { e.preventDefault(); setDrag(false); if (e.dataTransfer.files.length) uploadFiles(e.dataTransfer.files) }
|
|
||||||
|
|
||||||
if (open) return <div className="files"><Viewer path={open} sudo={sudo} onClose={() => setOpen(null)} onChanged={() => load()} /></div>
|
if (open) return <div className="files"><Viewer path={open} sudo={sudo} onClose={() => setOpen(null)} onChanged={() => load()} /></div>
|
||||||
|
|
||||||
@@ -202,6 +409,7 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
<div className="files-tools">
|
<div className="files-tools">
|
||||||
<button className={'btn' + (sudo ? ' on' : '')} title="browse as root" onClick={() => setSudo(!sudo)}>{sudo ? 'root' : 'sudo'}</button>
|
<button className={'btn' + (sudo ? ' on' : '')} title="browse as root" onClick={() => setSudo(!sudo)}>{sudo ? 'root' : 'sudo'}</button>
|
||||||
<button className={'btn' + (hidden ? ' on' : '')} title="show hidden files" onClick={() => setHidden(!hidden)}>.hidden</button>
|
<button className={'btn' + (hidden ? ' on' : '')} title="show hidden files" onClick={() => setHidden(!hidden)}>.hidden</button>
|
||||||
|
<button className={'btn' + (previews ? ' on' : '')} title={'thumbnails for images and videos' + (thumbs ? ` — ${thumbs.summary}` + (thumbs.cache.entries ? ` · ${thumbs.cache.entries} kept (${fmtBytes(thumbs.cache.bytes)})` : '') : '')} onClick={() => setPreviews(!previews)}>previews</button>
|
||||||
<select className="btn" value={sort} onChange={(e) => setSort(e.target.value as Sort)}><option value="name">name</option><option value="size">size</option><option value="mtime">modified</option></select>
|
<select className="btn" value={sort} onChange={(e) => setSort(e.target.value as Sort)}><option value="name">name</option><option value="size">size</option><option value="mtime">modified</option></select>
|
||||||
<input className="filter" placeholder="filter" value={q} onChange={(e) => setQ(e.target.value)} />
|
<input className="filter" placeholder="filter" value={q} onChange={(e) => setQ(e.target.value)} />
|
||||||
<button className="btn" onClick={mkdir}>+ folder</button>
|
<button className="btn" onClick={mkdir}>+ folder</button>
|
||||||
@@ -213,10 +421,13 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{previews && <ThumbNote st={thumbs} sudo={sudo} onChanged={() => readThumbs(true)} />}
|
||||||
|
{moveTask && <TaskOutput id={moveTask.id} onDone={(t) => { load(); if (t.exit_code === 0 && !t.error) toast(`${moveTask.title}: done`); else toast(`${moveTask.title}: failed`, 'err') }} />}
|
||||||
|
{moveTask && <div className="thumb-note"><button className="btn" onClick={() => setMoveTask(null)}>Hide output</button></div>}
|
||||||
{upload && <div className="upload-bar"><Spinner /> {upload.of > 1 ? `${upload.n}/${upload.of} · ` : ''}{upload.name} · {upload.pct}%<div className="meter"><div className="fill" style={{ width: `${upload.pct}%` }} /></div></div>}
|
{upload && <div className="upload-bar"><Spinner /> {upload.of > 1 ? `${upload.n}/${upload.of} · ` : ''}{upload.name} · {upload.pct}%<div className="meter"><div className="fill" style={{ width: `${upload.pct}%` }} /></div></div>}
|
||||||
{err && <div className="error" style={{ padding: '6px 12px' }}>{err}{/permission denied/i.test(err) && !sudo && <> · <button className="link" onClick={() => setSudo(true)}>retry as root</button></>}</div>}
|
{err && <div className="error" style={{ padding: '6px 12px' }}>{err}{/permission denied/i.test(err) && !sudo && <> · <button className="link" onClick={() => setSudo(true)}>retry as root</button></>}</div>}
|
||||||
|
|
||||||
<div className="file-list">
|
<div className={'file-list' + (previews ? ' thumbs' : '')} ref={setListEl}>
|
||||||
{entries === null && <Loading label="Listing…" />}
|
{entries === null && <Loading label="Listing…" />}
|
||||||
{path !== '/' && entries !== null && (
|
{path !== '/' && entries !== null && (
|
||||||
<div className="file-row up" onClick={() => go(path.slice(0, path.lastIndexOf('/')) || '/')}><span className="ficon">↩</span><span className="fname">..</span></div>
|
<div className="file-row up" onClick={() => go(path.slice(0, path.lastIndexOf('/')) || '/')}><span className="ficon">↩</span><span className="fname">..</span></div>
|
||||||
@@ -226,7 +437,9 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
const isDir = e.type === 'dir'
|
const isDir = e.type === 'dir'
|
||||||
return (
|
return (
|
||||||
<div key={e.name} className={'file-row' + (e.broken ? ' broken' : '')} onClick={() => (isDir ? go(full) : setOpen(full))}>
|
<div key={e.name} className={'file-row' + (e.broken ? ' broken' : '')} onClick={() => (isDir ? go(full) : setOpen(full))}>
|
||||||
<span className="ficon">{icon(e)}</span>
|
{previews && e.preview && thumbs?.ok
|
||||||
|
? <Thumb url={api.fileThumbURL(full, sudo, e.mtime, e.size)} kind={e.preview} near={near} fallback={icon(e)} />
|
||||||
|
: <span className="ficon">{icon(e)}</span>}
|
||||||
<span className="fname" title={e.target ? `→ ${e.target}` : e.name}>{e.name}{e.target && <span className="muted"> → {e.target}</span>}</span>
|
<span className="fname" title={e.target ? `→ ${e.target}` : e.name}>{e.name}{e.target && <span className="muted"> → {e.target}</span>}</span>
|
||||||
<span className="fmeta num">{isDir ? '' : fmtBytes(e.size)}</span>
|
<span className="fmeta num">{isDir ? '' : fmtBytes(e.size)}</span>
|
||||||
<span className="fmeta num">{fmtTime(e.mtime)}</span>
|
<span className="fmeta num">{fmtTime(e.mtime)}</span>
|
||||||
@@ -249,6 +462,7 @@ export default function Files({ onOpenInTerminal }: { onOpenInTerminal?: (cmd: s
|
|||||||
{entries !== null && shown.length === 0 && !err && <div className="muted" style={{ padding: 12 }}>{q ? 'Nothing matches.' : 'Empty folder.'}{!hidden && entries.some((e) => e.name.startsWith('.')) ? ' (hidden files not shown)' : ''}</div>}
|
{entries !== null && shown.length === 0 && !err && <div className="muted" style={{ padding: 12 }}>{q ? 'Nothing matches.' : 'Empty folder.'}{!hidden && entries.some((e) => e.name.startsWith('.')) ? ' (hidden files not shown)' : ''}</div>}
|
||||||
</div>
|
</div>
|
||||||
{drag && <div className="drop-hint">Drop to upload into {path}</div>}
|
{drag && <div className="drop-hint">Drop to upload into {path}</div>}
|
||||||
|
{moving && <MovePicker item={moving} from={path} sudo={sudo} hidden={hidden} onClose={() => setMoving(null)} onMoved={(t) => { setMoving(null); if (t) setMoveTask(t); load() }} />}
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ export default function Hardware() {
|
|||||||
<div className="tile">
|
<div className="tile">
|
||||||
<div className="label">Processor</div>
|
<div className="label">Processor</div>
|
||||||
<div className="value" title={cpu.model} style={(cpu.model?.length ?? 0) > 20 ? { fontSize: 16 } : undefined}>{cpu.model}</div>
|
<div className="value" title={cpu.model} style={(cpu.model?.length ?? 0) > 20 ? { fontSize: 16 } : undefined}>{cpu.model}</div>
|
||||||
<div className="sub">{cores ? `${cores} core${cores === 1 ? '' : 's'}` : ''}{threads && threads !== cores ? ` · ${threads} threads` : ''}{cpu.max_mhz ? ` · up to ${ghz(cpu.max_mhz)}` : ''}</div>
|
<div className="sub">{[cores ? `${cores} core${cores === 1 ? '' : 's'}` : '', threads && threads !== cores ? `${threads} threads` : '', cpu.max_mhz ? `up to ${ghz(cpu.max_mhz)}` : ''].filter(Boolean).join(' · ')}</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="tile">
|
<div className="tile">
|
||||||
<div className="label">Memory</div>
|
<div className="label">Memory</div>
|
||||||
|
|||||||
@@ -54,7 +54,10 @@ export default function Machines({ hub, onChanged, onOpen }: { hub: Me; onChange
|
|||||||
try {
|
try {
|
||||||
let res = await api.updateAllMachines()
|
let res = await api.updateAllMachines()
|
||||||
const busy = res.filter((r) => !r.ok && (r.error ?? '').startsWith('busy:'))
|
const busy = res.filter((r) => !r.ok && (r.error ?? '').startsWith('busy:'))
|
||||||
if (busy.length && await askYes(`${busy.map((r) => r.name).join(', ')}: ${busy[0].error?.slice(6)}\n\nForce those too?`)) res = res.map((r) => r).concat(await api.updateAllMachines(true))
|
if (busy.length && await askYes(`${busy.map((r) => r.name).join(', ')}: ${busy[0].error?.slice(6)}\n\nForce those too?`)) {
|
||||||
|
const forced = await api.updateAllMachines(true)
|
||||||
|
res = res.filter((r) => !forced.some((f) => f.name === r.name)).concat(forced) // one line per machine, the forced answer winning
|
||||||
|
}
|
||||||
for (const r of res) toast(r.ok ? `${r.name} → ${r.version}` : `${r.name}: ${r.error}`, r.ok ? 'ok' : 'err')
|
for (const r of res) toast(r.ok ? `${r.name} → ${r.version}` : `${r.name}: ${r.error}`, r.ok ? 'ok' : 'err')
|
||||||
setTimeout(load, 4000)
|
setTimeout(load, 4000)
|
||||||
} catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
} catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
||||||
|
|||||||
@@ -0,0 +1,147 @@
|
|||||||
|
// A video in the Files viewer. First the file as it is, handed to the browser's own
|
||||||
|
// decoder; when that fails — Safari and an MKV, HEVC without a hardware decoder — a
|
||||||
|
// picker: codec and size, and the machine re-encodes it to HLS on its own GPU (or CPU),
|
||||||
|
// which plays natively on Safari and through hls.js everywhere else. The same picker is
|
||||||
|
// one tap away even when the file plays, for a 36 Mbps capture over a slow link.
|
||||||
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
||||||
|
import { api, apiPath, PlayCaps, PlayStatus, PlayStream } from './api'
|
||||||
|
import { Btn, Spinner } from './ui'
|
||||||
|
|
||||||
|
type Mode = 'native' | 'pick' | 'hls'
|
||||||
|
const CODEC_KEY = 'hamlab.play.codec'
|
||||||
|
const QUALITY_KEY = 'hamlab.play.quality'
|
||||||
|
const remember = (k: string, v: string) => { try { localStorage.setItem(k, v) } catch { /* private mode */ } }
|
||||||
|
const recall = (k: string, d: string) => { try { return localStorage.getItem(k) || d } catch { return d } }
|
||||||
|
|
||||||
|
const fmtMbps = (bps?: number) => (bps ? `${(bps / 1e6).toFixed(bps >= 10e6 ? 0 : 1)} Mbps` : '')
|
||||||
|
const describe = (s: PlayStream) => {
|
||||||
|
const v = s.source.video
|
||||||
|
const src = v ? `${v.codec}${v.profile ? ' ' + v.profile : ''} ${v.width}×${v.height}${v.fps ? ` ${Math.round(v.fps)} fps` : ''}${s.source.bit_rate ? ` ${fmtMbps(s.source.bit_rate)}` : ''} in ${s.source.container.split(',')[0]}` : s.source.container
|
||||||
|
const out = s.codec === 'copy' ? 'repackaged' : `${s.codec === 'h264' ? 'H.264' : s.codec.toUpperCase()} ${s.width}×${s.height} ${(s.kbps / 1000).toFixed(s.kbps >= 10000 ? 0 : 1)} Mbps`
|
||||||
|
return `${src} → ${out} on ${s.engine}`
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Player({ path, sudo, name }: { path: string; sudo: boolean; name: string }) {
|
||||||
|
const [mode, setMode] = useState<Mode>('native')
|
||||||
|
const [why, setWhy] = useState('') // what brought the picker up
|
||||||
|
const [caps, setCaps] = useState<PlayCaps | null>(null)
|
||||||
|
const [codec, setCodec] = useState(() => recall(CODEC_KEY, 'h264'))
|
||||||
|
const [quality, setQuality] = useState(() => recall(QUALITY_KEY, '1080p'))
|
||||||
|
const [stream, setStream] = useState<{ s: PlayStream; url: string } | null>(null)
|
||||||
|
const [status, setStatus] = useState<PlayStatus | null>(null)
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const video = useRef<HTMLVideoElement>(null)
|
||||||
|
const streamRef = useRef(stream)
|
||||||
|
streamRef.current = stream
|
||||||
|
|
||||||
|
useEffect(() => { setMode('native'); setWhy(''); setStream(null); setStatus(null); setErr('') }, [path, sudo])
|
||||||
|
useEffect(() => { remember(CODEC_KEY, codec) }, [codec])
|
||||||
|
useEffect(() => { remember(QUALITY_KEY, quality) }, [quality])
|
||||||
|
useEffect(() => { if (mode === 'pick' && !caps) api.playCaps(sudo).then(setCaps).catch((e) => setCaps({ codecs: [], qualities: [], error: e instanceof Error ? e.message : 'failed' })) }, [mode, caps, sudo])
|
||||||
|
|
||||||
|
// a stream is the machine's work: let go of it when this player does — and when the page
|
||||||
|
// itself goes, by a beacon, the one request a browser still sends on its way out
|
||||||
|
const stop = useCallback(() => { const s = streamRef.current; if (s) { api.playStop(s.s.id).catch(() => { /* gone already */ }); setStream(null); setStatus(null) } }, [])
|
||||||
|
useEffect(() => () => stop(), [stop])
|
||||||
|
useEffect(() => {
|
||||||
|
const h = () => { const s = streamRef.current; if (s) navigator.sendBeacon(apiPath(`/api/files/play/${s.s.id}/close`)) }
|
||||||
|
window.addEventListener('pagehide', h)
|
||||||
|
return () => window.removeEventListener('pagehide', h)
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const start = async () => {
|
||||||
|
setErr('')
|
||||||
|
try {
|
||||||
|
stop()
|
||||||
|
const r = await api.playStart(path, codec, quality, sudo)
|
||||||
|
setStream({ s: r.stream, url: r.url }); setMode('hls')
|
||||||
|
} catch (e) { setErr(e instanceof Error ? e.message : 'failed') }
|
||||||
|
}
|
||||||
|
|
||||||
|
// feed the stream to the element: Safari plays HLS itself, the rest through hls.js
|
||||||
|
useEffect(() => {
|
||||||
|
const v = video.current
|
||||||
|
if (mode !== 'hls' || !stream || !v) return
|
||||||
|
const url = apiPath(stream.url)
|
||||||
|
let hls: { destroy: () => void } | null = null
|
||||||
|
let gone = false
|
||||||
|
;(async () => {
|
||||||
|
if (v.canPlayType('application/vnd.apple.mpegurl')) {
|
||||||
|
v.src = url
|
||||||
|
} else {
|
||||||
|
const { default: Hls } = await import('hls.js')
|
||||||
|
if (gone) return
|
||||||
|
if (!Hls.isSupported()) { setErr('this browser has neither HLS nor MediaSource — nothing can play here'); return }
|
||||||
|
const h = new Hls({ maxBufferLength: 60, backBufferLength: 30 })
|
||||||
|
hls = h
|
||||||
|
h.on(Hls.Events.ERROR, (_e, d) => { if (d.fatal) setErr(`playback failed: ${d.details}`) })
|
||||||
|
h.loadSource(url)
|
||||||
|
h.attachMedia(v)
|
||||||
|
}
|
||||||
|
v.play().catch(() => { /* needs a tap on some phones */ })
|
||||||
|
})()
|
||||||
|
return () => { gone = true; hls?.destroy() }
|
||||||
|
}, [mode, stream])
|
||||||
|
|
||||||
|
// follow the re-encode while it runs
|
||||||
|
useEffect(() => {
|
||||||
|
if (mode !== 'hls' || !stream) return
|
||||||
|
let stopPolling = false
|
||||||
|
const tick = async () => { try { const st = await api.playStatus(stream.s.id); if (!stopPolling) { setStatus(st); if (st.running) setTimeout(tick, 2000) } } catch { /* ended */ } }
|
||||||
|
tick()
|
||||||
|
return () => { stopPolling = true }
|
||||||
|
}, [mode, stream])
|
||||||
|
|
||||||
|
const pick = (reason: string) => { setWhy(reason); setMode('pick') }
|
||||||
|
const codecs = caps?.codecs ?? []
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="player">
|
||||||
|
{mode === 'native' && (
|
||||||
|
<>
|
||||||
|
<div className="play-bar"><span>as it is</span><span className="spacer" /><button className="btn" onClick={() => pick('')}>Re-encode…</button></div>
|
||||||
|
<div className="preview"><video controls playsInline preload="metadata" src={api.fileReadURL(path, sudo)} onError={() => pick(`This browser can't play ${name} as it is — the container or codec isn't one it decodes.`)} /></div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
{mode === 'pick' && (
|
||||||
|
<div className="play-pick notice">
|
||||||
|
<div>{why || `Re-encode ${name} on this machine and play that instead.`}</div>
|
||||||
|
{caps?.error && <div className="error" style={{ marginTop: 8 }}>{caps.error}</div>}
|
||||||
|
{!caps && !err && <div className="muted small" style={{ marginTop: 8 }}><Spinner /> asking the machine what it can encode…</div>}
|
||||||
|
{caps && !caps.error && (
|
||||||
|
<div className="row">
|
||||||
|
<label>codec
|
||||||
|
<select value={codec} onChange={(e) => setCodec(e.target.value)}>
|
||||||
|
{codecs.map((c) => <option key={c.id} value={c.id} disabled={!c.engine}>{c.label}{c.engine ? ` — ${c.engine}` : ' — not on this machine'}</option>)}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<label>size
|
||||||
|
<select value={quality} onChange={(e) => setQuality(e.target.value)} disabled={codec === 'copy'}>
|
||||||
|
{caps.qualities.map((q) => <option key={q.id} value={q.id}>{q.label}</option>)}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
<Btn className="btn on" disabled={!codecs.find((c) => c.id === codec)?.engine} onClick={start}>Play</Btn>
|
||||||
|
<a className="btn" href={api.fileDownloadURL(path, sudo)} download={name}>Download instead</a>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{err && <div className="error" style={{ marginTop: 8 }}>{err}</div>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{mode === 'hls' && stream && (
|
||||||
|
<>
|
||||||
|
<div className="play-bar">
|
||||||
|
<span title={describe(stream.s)}>{describe(stream.s)}</span>
|
||||||
|
{status && status.running && <span>· {status.speed ? `${status.speed.toFixed(1)}×` : ''} {stream.s.source.duration ? `${Math.min(100, Math.round(status.progress / stream.s.source.duration * 100))}% through` : `${Math.round(status.progress)} s done`} — seek within what's done</span>}
|
||||||
|
{status?.done && <span>· all of it is ready</span>}
|
||||||
|
{status?.error && <span className="error">· stopped early: {status.error}</span>}
|
||||||
|
<span className="spacer" />
|
||||||
|
<button className="btn" onClick={() => { stop(); setMode('pick'); setWhy('') }}>Change…</button>
|
||||||
|
</div>
|
||||||
|
<div className="preview"><video ref={video} controls autoPlay playsInline /></div>
|
||||||
|
{err && <div className="error" style={{ margin: 12 }}>{err}</div>}
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
+12
-3
@@ -16,9 +16,16 @@ function fmtCPU(ns: number): string {
|
|||||||
const s = ns / 1e9
|
const s = ns / 1e9
|
||||||
return s >= 3600 ? `${(s / 3600).toFixed(1)} h` : s >= 60 ? `${(s / 60).toFixed(1)} min` : `${s.toFixed(1)} s`
|
return s >= 3600 ? `${(s / 3600).toFixed(1)} h` : s >= 60 ? `${(s / 60).toFixed(1)} min` : `${s.toFixed(1)} s`
|
||||||
}
|
}
|
||||||
|
// systemd writes "Fri 2026-10-02 23:48:25 PDT", which Chromium parses and Firefox does not; read
|
||||||
|
// the fields directly, in the browser's own zone (the machine's, on a LAN), and fall back to Date.parse.
|
||||||
|
function parseSystemdTime(ts: string): number {
|
||||||
|
const m = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/.exec(ts)
|
||||||
|
if (m) return new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTime()
|
||||||
|
return Date.parse(ts.replace(/^[A-Za-z]{3} /, ''))
|
||||||
|
}
|
||||||
function since(ts?: string): string {
|
function since(ts?: string): string {
|
||||||
if (!ts) return ''
|
if (!ts) return ''
|
||||||
const t = Date.parse(ts.replace(/^[A-Za-z]{3} /, ''))
|
const t = parseSystemdTime(ts)
|
||||||
if (isNaN(t)) return ts
|
if (isNaN(t)) return ts
|
||||||
const s = Math.max(0, (Date.now() - t) / 1000)
|
const s = Math.max(0, (Date.now() - t) / 1000)
|
||||||
const d = Math.floor(s / 86400), h = Math.floor((s % 86400) / 3600), m = Math.floor((s % 3600) / 60)
|
const d = Math.floor(s / 86400), h = Math.floor((s % 86400) / 3600), m = Math.floor((s % 3600) / 60)
|
||||||
@@ -217,7 +224,9 @@ export default function Services() {
|
|||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{err && <div className="error">{err}</div>}
|
{err && units.length === 0 && loaded
|
||||||
|
? <div className="notice" style={{ margin: 8 }}>{err}</div> // a host with no systemd, or one that is unreachable: not a filter result
|
||||||
|
: err && <div className="error">{err}</div>}
|
||||||
<div className="svc-rows">
|
<div className="svc-rows">
|
||||||
{!loaded && <Loading label="Listing services…" />}
|
{!loaded && <Loading label="Listing services…" />}
|
||||||
{shown.map((u) => (
|
{shown.map((u) => (
|
||||||
@@ -230,7 +239,7 @@ export default function Services() {
|
|||||||
<span className="svc-state">{u.failed ? 'failed' : u.sub}{u.enabled === 'enabled' ? '' : u.enabled ? ` · ${u.enabled}` : ''}</span>
|
<span className="svc-state">{u.failed ? 'failed' : u.sub}{u.enabled === 'enabled' ? '' : u.enabled ? ` · ${u.enabled}` : ''}</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
{loaded && shown.length === 0 && <div className="muted" style={{ padding: 12 }}>Nothing matches.</div>}
|
{loaded && shown.length === 0 && !(err && units.length === 0) && <div className="muted" style={{ padding: 12 }}>Nothing matches.</div>}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{sel ? <Detail name={sel} onChanged={load} onBack={() => setSel(null)} /> : <div className="svc-empty muted">Select a service.</div>}
|
{sel ? <Detail name={sel} onChanged={load} onBack={() => setSel(null)} /> : <div className="svc-empty muted">Select a service.</div>}
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ function Updates({ me }: { me: Me }) {
|
|||||||
{st.notes && <div className="mono small" style={{ whiteSpace: 'pre-wrap', marginTop: 6, maxHeight: 140, overflow: 'auto' }}>{st.notes.trim()}</div>}
|
{st.notes && <div className="mono small" style={{ whiteSpace: 'pre-wrap', marginTop: 6, maxHeight: 140, overflow: 'auto' }}>{st.notes.trim()}</div>}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
{st && !st.behind && st.latest && <div className="muted small" style={{ marginTop: 8 }}>This is the newest published build.</div>}
|
{st && !st.behind && st.latest && <div className="muted small" style={{ marginTop: 8 }}>{st.current.replace(/^v/, '') === st.latest.replace(/^v/, '') ? 'This is the newest published build.' : `This build is newer than the last published release (${st.latest}).`}</div>}
|
||||||
{(err || st?.error) && <div className="error" style={{ marginTop: 8 }}>{err || st?.error}</div>}
|
{(err || st?.error) && <div className="error" style={{ marginTop: 8 }}>{err || st?.error}</div>}
|
||||||
<div className="svc-actions" style={{ marginTop: 10 }}>
|
<div className="svc-actions" style={{ marginTop: 10 }}>
|
||||||
<Btn className="btn" busy={busy === 'check'} disabled={!st?.enabled || !!busy} onClick={check}>Check for updates</Btn>
|
<Btn className="btn" busy={busy === 'check'} disabled={!st?.enabled || !!busy} onClick={check}>Check for updates</Btn>
|
||||||
|
|||||||
+8
-4
@@ -15,6 +15,7 @@ import Hardware from './Hardware'
|
|||||||
import Network from './Network'
|
import Network from './Network'
|
||||||
import VPN from './VPN'
|
import VPN from './VPN'
|
||||||
import ZFS from './ZFS'
|
import ZFS from './ZFS'
|
||||||
|
import Virtualization from './Virtualization'
|
||||||
import KVM from './KVM'
|
import KVM from './KVM'
|
||||||
import Files from './Files'
|
import Files from './Files'
|
||||||
import Top from './Top'
|
import Top from './Top'
|
||||||
@@ -27,7 +28,7 @@ import { MachineStatus, setMachine } from './api'
|
|||||||
import { LIST_FLOOR, useRefreshMs } from './prefs'
|
import { LIST_FLOOR, useRefreshMs } from './prefs'
|
||||||
import Power from './Power'
|
import Power from './Power'
|
||||||
import { applyAccent } from './accents'
|
import { applyAccent } from './accents'
|
||||||
import { Btn, PageBoundary, Toasts, askText, askYes, hasUnsaved, okToLeave, toast } from './ui'
|
import { Btn, PageBoundary, Toasts, askText, askYes, hasUnsaved, okToLeave, toast, useEscape } from './ui'
|
||||||
import { fmtLeft, useTimeLeft } from './guest'
|
import { fmtLeft, useTimeLeft } from './guest'
|
||||||
|
|
||||||
// Inline outline icon so it matches the monochrome glyphs in the rail (emoji would be full colour).
|
// Inline outline icon so it matches the monochrome glyphs in the rail (emoji would be full colour).
|
||||||
@@ -81,6 +82,7 @@ const GROUPS: Group[] = [
|
|||||||
{ id: 'usage', label: 'Usage', icon: '▧' },
|
{ id: 'usage', label: 'Usage', icon: '▧' },
|
||||||
{ id: 'zfs', label: 'ZFS', icon: '⛁' },
|
{ id: 'zfs', label: 'ZFS', icon: '⛁' },
|
||||||
{ id: 'docker', label: 'Docker', icon: DockerIcon },
|
{ id: 'docker', label: 'Docker', icon: DockerIcon },
|
||||||
|
{ id: 'virt', label: 'Virtualization', icon: '▣' },
|
||||||
{ id: 'kvm', label: 'KVM', icon: '⌨' },
|
{ id: 'kvm', label: 'KVM', icon: '⌨' },
|
||||||
{ id: 'hardware', label: 'Hardware', icon: '⬡' },
|
{ id: 'hardware', label: 'Hardware', icon: '⬡' },
|
||||||
{ id: 'updates', label: 'Updates', icon: '⇪' },
|
{ id: 'updates', label: 'Updates', icon: '⇪' },
|
||||||
@@ -91,8 +93,8 @@ const GROUPS: Group[] = [
|
|||||||
{ id: 'settings', label: 'Settings', icon: '⚙' },
|
{ id: 'settings', label: 'Settings', icon: '⚙' },
|
||||||
]
|
]
|
||||||
|
|
||||||
type View = 'terminal' | 'widgets' | 'docker' | 'alerts' | 'services' | 'jobs' | 'updates' | 'disks' | 'hardware' | 'network' | 'vpn' | 'zfs' | 'usage' | 'files' | 'procs' | 'machines' | 'kvm' | 'users' | 'settings'
|
type View = 'terminal' | 'widgets' | 'docker' | 'virt' | 'alerts' | 'services' | 'jobs' | 'updates' | 'disks' | 'hardware' | 'network' | 'vpn' | 'zfs' | 'usage' | 'files' | 'procs' | 'machines' | 'kvm' | 'users' | 'settings'
|
||||||
const VIEWS: View[] = ['terminal', 'widgets', 'docker', 'alerts', 'services', 'jobs', 'updates', 'disks', 'hardware', 'network', 'vpn', 'zfs', 'usage', 'files', 'procs', 'machines', 'kvm', 'users', 'settings']
|
const VIEWS: View[] = ['terminal', 'widgets', 'docker', 'virt', 'alerts', 'services', 'jobs', 'updates', 'disks', 'hardware', 'network', 'vpn', 'zfs', 'usage', 'files', 'procs', 'machines', 'kvm', 'users', 'settings']
|
||||||
const HUB_ONLY: View[] = ['machines', 'users', 'settings', 'alerts']
|
const HUB_ONLY: View[] = ['machines', 'users', 'settings', 'alerts']
|
||||||
|
|
||||||
// URL hash is the source of truth for where you are: #/<machine|hub>/<view>. Back and
|
// URL hash is the source of truth for where you are: #/<machine|hub>/<view>. Back and
|
||||||
@@ -135,6 +137,7 @@ export default function Shell({ me: hubMe, onLoggedOut }: { me: Me; onLoggedOut:
|
|||||||
const [active, setActive] = useState<string | null>(null)
|
const [active, setActive] = useState<string | null>(null)
|
||||||
const [view, setView] = useState<View>(() => parseHash()?.view ?? 'procs')
|
const [view, setView] = useState<View>(() => parseHash()?.view ?? 'procs')
|
||||||
const [drawer, setDrawer] = useState(false)
|
const [drawer, setDrawer] = useState(false)
|
||||||
|
useEscape(() => setDrawer(false), drawer) // the phone's drawer closes like every other overlay
|
||||||
const [online, setOnline] = useState(true)
|
const [online, setOnline] = useState(true)
|
||||||
const [creating, setCreating] = useState(false)
|
const [creating, setCreating] = useState(false)
|
||||||
const handles = useRef<Record<string, TermHandle>>({})
|
const handles = useRef<Record<string, TermHandle>>({})
|
||||||
@@ -328,6 +331,7 @@ export default function Shell({ me: hubMe, onLoggedOut }: { me: Me; onLoggedOut:
|
|||||||
{view === 'machines' && <Machines hub={hubMe} onChanged={loadMachines} onOpen={(n) => { switchMachine(n); setView('procs') }} />}
|
{view === 'machines' && <Machines hub={hubMe} onChanged={loadMachines} onOpen={(n) => { switchMachine(n); setView('procs') }} />}
|
||||||
{view === 'widgets' && <Gallery />}
|
{view === 'widgets' && <Gallery />}
|
||||||
{view === 'docker' && <Docker />}
|
{view === 'docker' && <Docker />}
|
||||||
|
{view === 'virt' && <Virtualization />}
|
||||||
{view === 'alerts' && <Alerts machines={machines.map((m) => m.name)} titleOf={(n) => titleOf(n === hubMe.host ? null : n, machines, hubMe.host)} />}
|
{view === 'alerts' && <Alerts machines={machines.map((m) => m.name)} titleOf={(n) => titleOf(n === hubMe.host ? null : n, machines, hubMe.host)} />}
|
||||||
{view === 'procs' && <Top />}
|
{view === 'procs' && <Top />}
|
||||||
{view === 'services' && <Services />}
|
{view === 'services' && <Services />}
|
||||||
@@ -365,7 +369,7 @@ export default function Shell({ me: hubMe, onLoggedOut }: { me: Me; onLoggedOut:
|
|||||||
<span>{machine ? `${title} · ` : ''}{me.version.startsWith('v') ? me.version : 'v' + me.version}</span>
|
<span>{machine ? `${title} · ` : ''}{me.version.startsWith('v') ? me.version : 'v' + me.version}</span>
|
||||||
<span>up {fmtUptime(me.uptime)}</span>
|
<span>up {fmtUptime(me.uptime)}</span>
|
||||||
<span className="spacer" />
|
<span className="spacer" />
|
||||||
<span className="muted">{view === 'widgets' ? 'Widgets' : view === 'docker' ? 'Docker' : view === 'alerts' ? 'Alerts' : view === 'services' ? 'Services' : view === 'jobs' ? 'Jobs' : view === 'updates' ? 'Updates' : view === 'disks' ? 'Disks' : view === 'hardware' ? 'Hardware' : view === 'network' ? 'Network' : view === 'vpn' ? 'VPN' : view === 'zfs' ? 'ZFS' : view === 'usage' ? 'Usage' : view === 'files' ? 'Files' : view === 'procs' ? 'Processes' : view === 'machines' ? 'Machines' : view === 'kvm' ? 'KVM' : view === 'users' ? 'Users' : view === 'settings' ? 'Settings' : activeTerm ? activeTerm.name : ''}</span>
|
<span className="muted">{view === 'widgets' ? 'Widgets' : view === 'docker' ? 'Docker' : view === 'virt' ? 'Virtualization' : view === 'alerts' ? 'Alerts' : view === 'services' ? 'Services' : view === 'jobs' ? 'Jobs' : view === 'updates' ? 'Updates' : view === 'disks' ? 'Disks' : view === 'hardware' ? 'Hardware' : view === 'network' ? 'Network' : view === 'vpn' ? 'VPN' : view === 'zfs' ? 'ZFS' : view === 'usage' ? 'Usage' : view === 'files' ? 'Files' : view === 'procs' ? 'Processes' : view === 'machines' ? 'Machines' : view === 'kvm' ? 'KVM' : view === 'users' ? 'Users' : view === 'settings' ? 'Settings' : activeTerm ? activeTerm.name : ''}</span>
|
||||||
</footer>
|
</footer>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
import { useEffect, useMemo, useState } from 'react'
|
import { useEffect, useMemo, useState } from 'react'
|
||||||
import { api, StackHome } from './api'
|
import { api, StackHome } from './api'
|
||||||
import { askYes, Btn, Loading, useEscape } from './ui'
|
import { askYes, Btn, Loading, useEscape } from './ui'
|
||||||
|
import CodeEditor from './CodeEditor'
|
||||||
|
|
||||||
interface Form { image: string; ports: string; volumes: string; env: string; labels: string; restart: string; command: string; network: string }
|
interface Form { image: string; ports: string; volumes: string; env: string; labels: string; restart: string; command: string; network: string }
|
||||||
const EMPTY: Form = { image: '', ports: '', volumes: '', env: '', labels: '', restart: 'unless-stopped', command: '', network: '' }
|
const EMPTY: Form = { image: '', ports: '', volumes: '', env: '', labels: '', restart: 'unless-stopped', command: '', network: '' }
|
||||||
@@ -100,7 +101,7 @@ export default function StackNew({ mode, from, networks = [], taken, onClose, on
|
|||||||
{homes.length > 1 && <label className="muted">Where
|
{homes.length > 1 && <label className="muted">Where
|
||||||
<select value={home} onChange={(e) => setHome(e.target.value)}>{homes.map((h) => <option key={h.dir + h.via} value={h.dir}>{sameTail(h) ? h.dir : tail(h.dir)} · {h.stacks} stack{h.stacks === 1 ? '' : 's'}</option>)}</select>
|
<select value={home} onChange={(e) => setHome(e.target.value)}>{homes.map((h) => <option key={h.dir + h.via} value={h.dir}>{sameTail(h) ? h.dir : tail(h.dir)} · {h.stacks} stack{h.stacks === 1 ? '' : 's'}</option>)}</select>
|
||||||
</label>}
|
</label>}
|
||||||
{picked && <div className="muted small mono" style={{ overflowWrap: 'anywhere' }}>{picked.dir}/{name || '…'}/compose.yaml{picked.held_by && ` — kept on ${picked.held_by}, deployed here over ${picked.remote}`}{picked.via && ` — compose runs inside ${picked.via}, like its neighbours`}</div>}
|
{picked && <div className="muted small mono" style={{ overflowWrap: 'anywhere' }}>{picked.dir}/{name || '…'}/compose.yaml{picked.via && ` — compose runs inside ${picked.via}, like its neighbours`}</div>}
|
||||||
{!from && <div className="tabs"><button className={how === 'form' ? 'on' : ''} onClick={() => setHow('form')}>Form</button><button className={how === 'compose' ? 'on' : ''} onClick={toCompose}>Compose</button></div>}
|
{!from && <div className="tabs"><button className={how === 'form' ? 'on' : ''} onClick={() => setHow('form')}>Form</button><button className={how === 'compose' ? 'on' : ''} onClick={toCompose}>Compose</button></div>}
|
||||||
{how === 'form' ? <>
|
{how === 'form' ? <>
|
||||||
<label className="muted">Image<input className="mono" value={form.image} onChange={set('image')} placeholder="traefik/whoami:latest" spellCheck={false} autoCapitalize="off" autoCorrect="off" /></label>
|
<label className="muted">Image<input className="mono" value={form.image} onChange={set('image')} placeholder="traefik/whoami:latest" spellCheck={false} autoCapitalize="off" autoCorrect="off" /></label>
|
||||||
@@ -119,7 +120,7 @@ export default function StackNew({ mode, from, networks = [], taken, onClose, on
|
|||||||
<label className="muted">Command (optional)<input className="mono" value={form.command} onChange={set('command')} spellCheck={false} autoCapitalize="off" /></label>
|
<label className="muted">Command (optional)<input className="mono" value={form.command} onChange={set('command')} spellCheck={false} autoCapitalize="off" /></label>
|
||||||
<pre className="unit-file stack-preview">{built}</pre>
|
<pre className="unit-file stack-preview">{built}</pre>
|
||||||
</> : <>
|
</> : <>
|
||||||
<textarea className="editor stack-editor" value={text} onChange={(e) => setText(e.target.value)} spellCheck={false} autoCapitalize="off" autoCorrect="off" />
|
<CodeEditor className="stack-editor" language="yaml" value={text} onChange={setText} />
|
||||||
{from?.hasEnv && <label className="check"><input type="checkbox" checked={copyEnv} onChange={(e) => setCopyEnv(e.target.checked)} /> <span>Copy {from.name}'s .env too <span className="muted">— copied on the machine itself; its values are never shown here</span></span></label>}
|
{from?.hasEnv && <label className="check"><input type="checkbox" checked={copyEnv} onChange={(e) => setCopyEnv(e.target.checked)} /> <span>Copy {from.name}'s .env too <span className="muted">— copied on the machine itself; its values are never shown here</span></span></label>}
|
||||||
{!(from && copyEnv) && <label className="muted">.env (optional) — variables the compose file reads; kept readable by root only
|
{!(from && copyEnv) && <label className="muted">.env (optional) — variables the compose file reads; kept readable by root only
|
||||||
<textarea className="mono" rows={2} value={env} onChange={(e) => setEnv(e.target.value)} placeholder={'SECRET=…'} spellCheck={false} />
|
<textarea className="mono" rows={2} value={env} onChange={(e) => setEnv(e.target.value)} placeholder={'SECRET=…'} spellCheck={false} />
|
||||||
|
|||||||
+28
-8
@@ -348,10 +348,13 @@ export default function Top() {
|
|||||||
const seg = segOf(g.name), n = all.slice(0, i).filter((o) => segOf(o.name) === seg).length
|
const seg = segOf(g.name), n = all.slice(0, i).filter((o) => segOf(o.name) === seg).length
|
||||||
return { g, stem: 'gpu.' + seg + (n ? `_${n + 1}` : '') }
|
return { g, stem: 'gpu.' + seg + (n ? `_${n + 1}` : '') }
|
||||||
})
|
})
|
||||||
|
// a GPU with nothing to read can't light a process's Gpu% either: the column, and its sort,
|
||||||
|
// are for a machine where at least one GPU says something
|
||||||
|
const gpuCol = gpus.some(({ g }) => !unmeasured(g))
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Pal.Provider value={pal}>
|
<Pal.Provider value={pal}>
|
||||||
<div className={'top' + (gpus.length ? ' has-gpu' : '')} style={pal.vars as React.CSSProperties}>
|
<div className={'top' + (gpus.length ? ' has-gpu' : '') + (gpus.length && !gpuCol ? ' gpu-slim' : '')} style={pal.vars as React.CSSProperties}>
|
||||||
<Box n={1} name="cpu" className="top-cpu" right={<>
|
<Box n={1} name="cpu" className="top-cpu" right={<>
|
||||||
<span className="t-clock">{clock.toLocaleTimeString([], { hour12: false })}</span>
|
<span className="t-clock">{clock.toLocaleTimeString([], { hour12: false })}</span>
|
||||||
<span className="t-rate">
|
<span className="t-rate">
|
||||||
@@ -366,7 +369,8 @@ export default function Top() {
|
|||||||
<div className="t-up">up {upfor(uptime)}</div>
|
<div className="t-up">up {upfor(uptime)}</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="t-cores">
|
<div className="t-cores">
|
||||||
<div className="t-cores-head"><b>{model || 'cpu'}</b><span className="spacer" />{pt?.cpu_mhz ? `${Math.round(pt.cpu_mhz)} MHz` : ''}</div>
|
<div className="t-cores-head"><b>{model || 'cpu'}</b><span className="spacer" />{pt?.cpu_mhz ? `${Math.round(pt.cpu_mhz)} MHz` : ''}
|
||||||
|
{pt?.cpu_power_w ? <span title="the CPU's own power reading: cores, built-in graphics and memory controller, not the whole machine">{pt.cpu_mhz ? ' · ' : ''}{pt.cpu_power_w < 10 ? pt.cpu_power_w.toFixed(1) : Math.round(pt.cpu_power_w)} W</span> : null}</div>
|
||||||
<div className="t-core">
|
<div className="t-core">
|
||||||
<span className="t-core-n">CPU</span>
|
<span className="t-core-n">CPU</span>
|
||||||
<Meter pct={cpu} grad="cpu" />
|
<Meter pct={cpu} grad="cpu" />
|
||||||
@@ -392,8 +396,8 @@ export default function Top() {
|
|||||||
{gpus.length > 0 && (
|
{gpus.length > 0 && (
|
||||||
<div className="top-gpus">
|
<div className="top-gpus">
|
||||||
{gpus.map(({ g, stem }, i) => (
|
{gpus.map(({ g, stem }, i) => (
|
||||||
<Box key={stem} name={`gpu${i}`} className="top-gpu" right={<span className="t-gpu-name" title={[g.name, g.driver, g.bus].filter(Boolean).join(' · ')}>{g.name}</span>}>
|
<Box key={stem} name={`gpu${i}`} className={'top-gpu' + (unmeasured(g) ? ' slim' : '')} right={<span className="t-gpu-name" title={[g.name, g.driver, g.bus].filter(Boolean).join(' · ')}>{g.name}</span>}>
|
||||||
<GPUBox g={g} values={curves.get(stem + '.usage') ?? []} tick={tick} advance={advance} cap={cap} />
|
{unmeasured(g) ? <GPULine g={g} /> : <GPUBox g={g} values={curves.get(stem + '.usage') ?? []} tick={tick} advance={advance} cap={cap} />}
|
||||||
</Box>
|
</Box>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
@@ -461,17 +465,17 @@ export default function Top() {
|
|||||||
|
|
||||||
<Box n={5} name="proc" className="top-proc" right={<>
|
<Box n={5} name="proc" className="top-proc" right={<>
|
||||||
<span className="t-sorts">
|
<span className="t-sorts">
|
||||||
{(['pid', 'name', 'mem', ...(gpus.length ? ['gpu' as const] : []), 'cpu'] as const).map((k) => <button key={k} className={sort === k ? 'on' : ''} onClick={() => setSort(k)}>{k}</button>)}
|
{(['pid', 'name', 'mem', ...(gpuCol ? ['gpu' as const] : []), 'cpu'] as const).map((k) => <button key={k} className={sort === k ? 'on' : ''} onClick={() => setSort(k)}>{k}</button>)}
|
||||||
<button className={rev ? 'on' : ''} onClick={() => setRev(!rev)}>reverse</button>
|
<button className={rev ? 'on' : ''} onClick={() => setRev(!rev)}>reverse</button>
|
||||||
</span>
|
</span>
|
||||||
</>}>
|
</>}>
|
||||||
<div className={'t-proc' + (gpus.length ? ' has-gpu' : '')}>
|
<div className={'t-proc' + (gpuCol ? ' has-gpu' : '')}>
|
||||||
<input className="t-filter" value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="filter" spellCheck={false} autoCapitalize="off" autoCorrect="off" />
|
<input className="t-filter" value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="filter" spellCheck={false} autoCapitalize="off" autoCorrect="off" />
|
||||||
<div className="t-thead">
|
<div className="t-thead">
|
||||||
<span className="c-pid">Pid:</span><span className="c-name">Program:</span><span className="c-cmd">Command:</span>
|
<span className="c-pid">Pid:</span><span className="c-name">Program:</span><span className="c-cmd">Command:</span>
|
||||||
<span className="c-thr">Threads:</span><span className="c-user">User:</span><span className="c-mem">MemB</span><span className="c-g" />{gpus.length > 0 && <span className="c-gpu">Gpu%</span>}<span className="c-cpu">Cpu%</span>
|
<span className="c-thr">Threads:</span><span className="c-user">User:</span><span className="c-mem">MemB</span><span className="c-g" />{gpuCol && <span className="c-gpu">Gpu%</span>}<span className="c-cpu">Cpu%</span>
|
||||||
</div>
|
</div>
|
||||||
<Rows procs={procs} sel={sel} onSel={setSel} curves={pcurve} gpu={gpus.length > 0} empty={err || 'no process matches'} />
|
<Rows procs={procs} sel={sel} onSel={setSel} curves={pcurve} gpu={gpuCol} empty={err || 'no process matches'} />
|
||||||
<div className="t-foot">
|
<div className="t-foot">
|
||||||
{sel !== null && procs.find((p) => p.pid === sel) ? (() => { const p = procs.find((x) => x.pid === sel)!; return <>
|
{sel !== null && procs.find((p) => p.pid === sel) ? (() => { const p = procs.find((x) => x.pid === sel)!; return <>
|
||||||
<span className="t-dim">{p.pid} {p.name}</span><span className="spacer" />
|
<span className="t-dim">{p.pid} {p.name}</span><span className="spacer" />
|
||||||
@@ -569,6 +573,22 @@ function GPUBox({ g, values, tick, advance, cap }: { g: GPU; values: number[]; t
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Nothing on this GPU can be read — no busy counter, engine, temperature, clock, power, memory,
|
||||||
|
// nor a process with it open: the Mac's Asahi, today. Its box would be a graph that can never
|
||||||
|
// draw beside a column of dashes, so it is one line instead. A client opening it brings the box back.
|
||||||
|
const unmeasured = (g: GPU) => !g.usage_src && !g.engines && !(g.temp && g.temp > 0) && !g.mhz && !g.max_mhz && !g.power_w && !g.power_cap_w && !g.fan_rpm && !g.mem_total && !g.mem_used && g.clients.length === 0
|
||||||
|
function GPULine({ g }: { g: GPU }) {
|
||||||
|
return (
|
||||||
|
<div className="t-row t-gpu-line">
|
||||||
|
<span>{g.driver}{g.integrated ? ' · integrated' : ''}</span>
|
||||||
|
{g.stalled
|
||||||
|
? <span className="t-dim" title="a sysfs read of this card stopped answering">readings paused until the console restarts</span>
|
||||||
|
: <span className="t-dim" title="the driver exposes no busy counter, clock, temperature, power or memory reading">nothing measurable</span>}
|
||||||
|
<span className="t-dim t-gpu-who" title={g.clients_ok ? undefined : 'the console has no root here'}>{g.clients_ok ? 'no process has it open' : 'other users’ processes are hidden'}</span>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
// One physical drive. `used` is the bytes of the filesystems on it, left out when none can
|
// One physical drive. `used` is the bytes of the filesystems on it, left out when none can
|
||||||
// be attributed to it — a pool member's share of a pool is not a number worth inventing.
|
// be attributed to it — a pool member's share of a pool is not a number worth inventing.
|
||||||
function Disk({ name, note, size, used, io, temp, mounts, pool }: { name: string; note?: string; size: number; used?: number; io?: number; temp?: number; mounts?: string[]; pool?: string }) {
|
function Disk({ name, note, size, used, io, temp, mounts, pool }: { name: string; note?: string; size: number; used?: number; io?: number; temp?: number; mounts?: string[]; pool?: string }) {
|
||||||
|
|||||||
+6
-2
@@ -52,7 +52,11 @@ export default function Updates() {
|
|||||||
setOpen(p.name)
|
setOpen(p.name)
|
||||||
if (!changelog[p.name]) {
|
if (!changelog[p.name]) {
|
||||||
try { const r = await api.changelog(p.name); setChangelog((c) => ({ ...c, [p.name]: r.text || '(empty)' })) }
|
try { const r = await api.changelog(p.name); setChangelog((c) => ({ ...c, [p.name]: r.text || '(empty)' })) }
|
||||||
catch (e) { setChangelog((c) => ({ ...c, [p.name]: e instanceof Error ? e.message : 'unavailable' })) }
|
catch (e) {
|
||||||
|
const m = e instanceof Error ? e.message : 'unavailable'
|
||||||
|
// apt's own words for a third-party repository that publishes none are a wall of "E: Failed to fetch"
|
||||||
|
setChangelog((c) => ({ ...c, [p.name]: /changelog unavailable|failed to fetch/i.test(m) ? 'No changelog is published for this package — its repository does not provide one.' : m }))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const toggle = (n: string) => setSel((s) => { const x = new Set(s); if (x.has(n)) x.delete(n); else x.add(n); return x })
|
const toggle = (n: string) => setSel((s) => { const x = new Set(s); if (x.has(n)) x.delete(n); else x.add(n); return x })
|
||||||
@@ -99,7 +103,7 @@ export default function Updates() {
|
|||||||
<span className="n">{p.name}{p.security && <span className="badge bad">security</span>}</span>
|
<span className="n">{p.name}{p.security && <span className="badge bad">security</span>}</span>
|
||||||
<span className="d">{p.description}</span>
|
<span className="d">{p.description}</span>
|
||||||
</span>
|
</span>
|
||||||
<span className="pkg-ver"><span className="muted">{p.current}</span> → {p.candidate}<span className="muted"> · {p.suite}</span></span>
|
<span className="pkg-ver"><span className="muted">{p.current}</span> → {p.candidate}{p.suite && <span className="muted"> · {p.suite}</span>}</span>
|
||||||
</div>
|
</div>
|
||||||
{open === p.name && <pre className="run-out changelog">{changelog[p.name] ?? <span className="muted"><Spinner /> fetching changelog…</span>}</pre>}
|
{open === p.name && <pre className="run-out changelog">{changelog[p.name] ?? <span className="muted"><Spinner /> fetching changelog…</span>}</pre>}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
+13
-2
@@ -133,6 +133,13 @@ function Sessions() {
|
|||||||
catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
||||||
}
|
}
|
||||||
const others = list?.filter((s) => !s.current).length ?? 0
|
const others = list?.filter((s) => !s.current).length ?? 0
|
||||||
|
// a day unused is the line between a device that is used and a script that signed in once
|
||||||
|
const IDLE_H = 24
|
||||||
|
const idle = list?.filter((s) => !s.current && Date.now() - new Date(s.last_seen).getTime() > IDLE_H * 3600e3).length ?? 0
|
||||||
|
const revokeIdle = async () => {
|
||||||
|
if (!await askYes(`Sign out ${idle} ${idle === 1 ? 'session' : 'sessions'} not used in the last ${IDLE_H} hours? Devices used since then, and this one, stay signed in.`, 'Revoke')) return
|
||||||
|
try { const r = await api.revokeIdleSessions(IDLE_H); toast(`${r.revoked} signed out`); load() } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
||||||
|
}
|
||||||
const revokeOthers = async () => {
|
const revokeOthers = async () => {
|
||||||
if (!await askYes(`Sign out ${others} other ${others === 1 ? 'device' : 'devices'}? This one stays signed in.`)) return
|
if (!await askYes(`Sign out ${others} other ${others === 1 ? 'device' : 'devices'}? This one stays signed in.`)) return
|
||||||
try { const r = await api.revokeOtherSessions(); toast(`${r.revoked} signed out`); load() } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
try { const r = await api.revokeOtherSessions(); toast(`${r.revoked} signed out`); load() } catch (e) { toast(e instanceof Error ? e.message : 'failed', 'err') }
|
||||||
@@ -145,11 +152,15 @@ function Sessions() {
|
|||||||
{list && list.map((s) => (
|
{list && list.map((s) => (
|
||||||
<div className="row" key={s.id}>
|
<div className="row" key={s.id}>
|
||||||
<span className="k">{device(s.agent)}{s.current && <span className="badge" style={{ marginLeft: 6 }}>this device</span>}</span>
|
<span className="k">{device(s.agent)}{s.current && <span className="badge" style={{ marginLeft: 6 }}>this device</span>}</span>
|
||||||
<span className="muted small">{s.ip ?? '?'} · seen {ago(s.last_seen)} · since {new Date(s.created).toLocaleDateString()}</span>
|
<span className="muted small">{s.ip ?? '?'} · seen {ago(s.last_seen)} · since {new Date(s.created).toLocaleDateString()}{!s.current && Date.now() - new Date(s.last_seen).getTime() > IDLE_H * 3600e3 ? <span className="badge" style={{ marginLeft: 6 }}>idle</span> : null}</span>
|
||||||
<Btn className={'btn' + (s.current ? '' : ' danger')} onClick={() => revoke(s)}>{s.current ? 'Sign out' : 'Revoke'}</Btn>
|
<Btn className={'btn' + (s.current ? '' : ' danger')} onClick={() => revoke(s)}>{s.current ? 'Sign out' : 'Revoke'}</Btn>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
{others > 0 && <div className="svc-actions" style={{ marginTop: 8 }}><Btn className="btn danger" onClick={revokeOthers}>Sign out everywhere else</Btn></div>}
|
{others > 0 && <div className="svc-actions" style={{ marginTop: 8 }}>
|
||||||
|
{idle > 0 && <Btn className="btn" onClick={revokeIdle}>Sign out {idle} idle</Btn>}
|
||||||
|
<Btn className="btn danger" onClick={revokeOthers}>Sign out everywhere else</Btn>
|
||||||
|
<span className="muted small">A session unused for a week signs itself out.</span>
|
||||||
|
</div>}
|
||||||
</section>
|
</section>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
// The graphical console of a VM: spice-html5 (vendored, loaded on demand) drawing the SPICE
|
||||||
|
// stream that Ham Lab bridges from Incus into a canvas. Used for installers from an ISO, and
|
||||||
|
// anything else with a screen rather than a serial line.
|
||||||
|
import { useEffect, useRef, useState } from 'react'
|
||||||
|
import { wsURLPath } from './api'
|
||||||
|
import { Btn, useEscape } from './ui'
|
||||||
|
// No spice.css: upstream's stylesheet is for its own demo page and sets body, *, button and
|
||||||
|
// canvas for the whole document. Imported here it restyled every button in Ham Lab. The
|
||||||
|
// console is styled by the .vga rules in styles.css.
|
||||||
|
|
||||||
|
type Spice = typeof import('./vendor/spice-html5/src/main.js')
|
||||||
|
|
||||||
|
export default function VgaConsole({ name, onClose }: { name: string; onClose: () => void }) {
|
||||||
|
const [state, setState] = useState<'loading' | 'connecting' | 'connected' | 'error'>('loading')
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [fit, setFit] = useState(true)
|
||||||
|
const [size, setSize] = useState('')
|
||||||
|
const conn = useRef<InstanceType<Spice['SpiceMainConn']> | null>(null)
|
||||||
|
const mod = useRef<Spice | null>(null)
|
||||||
|
const area = useRef<HTMLDivElement>(null)
|
||||||
|
const screen = useRef<HTMLDivElement>(null)
|
||||||
|
useEscape(onClose)
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let dead = false
|
||||||
|
import('./vendor/spice-html5/src/main.js').then((m) => {
|
||||||
|
if (dead) return
|
||||||
|
mod.current = m
|
||||||
|
setState('connecting')
|
||||||
|
try {
|
||||||
|
conn.current = new m.SpiceMainConn({
|
||||||
|
uri: wsURLPath(`/api/virt/instances/${encodeURIComponent(name)}/vga`),
|
||||||
|
screen_id: 'spice-screen', message_id: 'spice-message',
|
||||||
|
onerror: (e) => { setState('error'); setErr(e instanceof Error ? e.message : (e as Event & { reason?: string })?.reason || 'the console closed') },
|
||||||
|
onsuccess: () => setState('connected'),
|
||||||
|
onagent: () => { try { m.handle_resize() } catch { /* no agent in the guest yet */ } },
|
||||||
|
})
|
||||||
|
} catch (e) { setState('error'); setErr(String(e)) }
|
||||||
|
}).catch((e) => { setState('error'); setErr('could not load the SPICE viewer: ' + String(e)) })
|
||||||
|
return () => { dead = true; try { conn.current?.stop() } catch { /* already gone */ } conn.current = null }
|
||||||
|
}, [name])
|
||||||
|
|
||||||
|
// The guest draws at its own resolution; scale the canvas to the pane unless asked not to.
|
||||||
|
useEffect(() => {
|
||||||
|
const a = area.current, s = screen.current
|
||||||
|
if (!a || !s) return
|
||||||
|
const apply = () => {
|
||||||
|
const c = s.querySelector('canvas')
|
||||||
|
if (!c) return
|
||||||
|
setSize(`${c.width}×${c.height}`)
|
||||||
|
c.tabIndex = 1
|
||||||
|
if (!fit) { s.style.transform = ''; s.style.width = ''; s.style.height = ''; return }
|
||||||
|
const k = Math.min(1, (a.clientWidth - 8) / c.width, (a.clientHeight - 8) / c.height)
|
||||||
|
s.style.transformOrigin = 'top left'
|
||||||
|
s.style.transform = `scale(${k})`
|
||||||
|
s.style.width = `${c.width}px`; s.style.height = `${c.height}px`
|
||||||
|
}
|
||||||
|
apply()
|
||||||
|
const ro = new ResizeObserver(apply); ro.observe(a)
|
||||||
|
const mo = new MutationObserver(apply); mo.observe(s, { childList: true, attributes: true, subtree: true, attributeFilter: ['width', 'height'] })
|
||||||
|
return () => { ro.disconnect(); mo.disconnect() }
|
||||||
|
}, [fit, state])
|
||||||
|
|
||||||
|
const cad = () => { if (conn.current && mod.current) mod.current.sendCtrlAltDel(conn.current) }
|
||||||
|
const focus = () => screen.current?.querySelector('canvas')?.focus()
|
||||||
|
return (
|
||||||
|
<div className="modal-back vga-back" onClick={onClose}>
|
||||||
|
<div className="vga" onClick={(e) => e.stopPropagation()}>
|
||||||
|
<div className="logs-bar vga-bar">
|
||||||
|
<span className={'dot ' + (state === 'connected' ? 'ok' : state === 'error' ? 'bad' : 'warn')} />
|
||||||
|
<span className="dname">{name}</span>
|
||||||
|
<span className="muted small">{state === 'connected' ? `VGA · ${size || 'waiting for the first frame'}` : state === 'error' ? err : state === 'loading' ? 'loading the viewer…' : 'connecting…'}</span>
|
||||||
|
<span className="spacer" />
|
||||||
|
<Btn className="btn" disabled={state !== 'connected'} onClick={cad} title="send Ctrl+Alt+Del to the guest">Ctrl+Alt+Del</Btn>
|
||||||
|
<button className={'btn' + (fit ? ' on' : '')} onClick={() => setFit(!fit)} title="scale the screen to this window, or show it pixel for pixel">Fit</button>
|
||||||
|
<button className="btn" onClick={onClose}>Close</button>
|
||||||
|
</div>
|
||||||
|
<div className="vga-area" ref={area} onClick={focus}>
|
||||||
|
<div id="spice-area"><div id="spice-screen" ref={screen} /></div>
|
||||||
|
<div id="spice-message" className="spice-message vga-msg" />
|
||||||
|
</div>
|
||||||
|
<div className="muted small vga-hint">Click the screen to type into it. Keys go to the guest while it has focus; Escape with focus elsewhere closes this.</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -0,0 +1,715 @@
|
|||||||
|
// Virtualization: LXC containers and KVM virtual machines through Incus, per machine. The
|
||||||
|
// page has four states — ready, repair, installable, unavailable — decided by what the
|
||||||
|
// selected machine can do, never by a rule about what should run where. Everything the
|
||||||
|
// tab makes lives under Ham Lab's own folder on that machine.
|
||||||
|
import { FormEvent, Fragment, useCallback, useEffect, useMemo, useState } from 'react'
|
||||||
|
import { api, ExportsView, LibraryFile, TaskInfo, VirtImage, VirtInstance, VirtOverview, VirtStatus } from './api'
|
||||||
|
import VgaConsole from './VgaConsole'
|
||||||
|
import TaskOutput from './TaskOutput'
|
||||||
|
import { useHashSel } from './hashsel'
|
||||||
|
import { useRefreshMs } from './prefs'
|
||||||
|
import { fmtBytes } from './fmt'
|
||||||
|
import { askText, askYes, Btn, CloseBtn, Loading, toast, useEscape } from './ui'
|
||||||
|
|
||||||
|
type Tab = 'instances' | 'images' | 'library'
|
||||||
|
const GiB = 1 << 30
|
||||||
|
const dot = (i: VirtInstance) => (i.status === 'Running' ? 'ok' : i.status === 'Stopped' ? '' : i.status === 'Error' ? 'bad' : 'warn')
|
||||||
|
const kind = (i: VirtInstance) => (i.type === 'virtual-machine' ? 'VM' : 'LXC')
|
||||||
|
const when = (iso?: string) => { if (!iso) return '—'; const d = new Date(iso); return isNaN(d.getTime()) || d.getFullYear() < 1980 ? '—' : d.toLocaleString() }
|
||||||
|
const errMsg = (e: unknown) => (e instanceof Error ? e.message : 'failed')
|
||||||
|
const imgLabel = (im: VirtImage) => im.aliases[0] ?? im.fingerprint.slice(0, 12)
|
||||||
|
|
||||||
|
export default function Virtualization() {
|
||||||
|
const [ov, setOv] = useState<VirtOverview | null>(null)
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [tab, setTab] = useState<Tab>('instances')
|
||||||
|
const [sel, setSel] = useHashSel('virt')
|
||||||
|
const [making, setMaking] = useState<null | { image?: string; vm?: boolean }>(null)
|
||||||
|
const [fromIso, setFromIso] = useState<string | null>(null)
|
||||||
|
const [task, setTask] = useState<{ id: string; title: string } | null>(null)
|
||||||
|
const [filter, setFilter] = useState('')
|
||||||
|
const rate = useRefreshMs()
|
||||||
|
const load = useCallback(async () => { try { setOv(await api.virt()); setErr('') } catch (e) { setErr(errMsg(e)) } }, [])
|
||||||
|
useEffect(() => { load(); const t = setInterval(() => { if (document.visibilityState === 'visible') load() }, Math.max(5000, rate)); return () => clearInterval(t) }, [load, rate])
|
||||||
|
const startTask = (t: TaskInfo) => setTask({ id: t.id, title: t.title })
|
||||||
|
|
||||||
|
if (!ov) return <div className="dash">{err ? <div className="notice">{err === 'not found' ? "This machine's Ham Lab is older than the Virtualization tab. Update it from Machines." : err}</div> : <Loading label="Asking Incus…" />}</div>
|
||||||
|
const st = ov.status
|
||||||
|
if (st.state !== 'ready') return <Gate st={st} onChanged={load} />
|
||||||
|
const instances = ov.instances ?? []
|
||||||
|
const images = ov.images ?? []
|
||||||
|
const cur = instances.find((i) => i.name === sel)
|
||||||
|
const running = instances.filter((i) => i.status === 'Running').length
|
||||||
|
const f = filter.trim().toLowerCase()
|
||||||
|
const shown = instances.filter((i) => !f || `${i.name} ${i.image} ${i.type} ${i.status}`.toLowerCase().includes(f))
|
||||||
|
const libCount = ov.library.images.length + ov.library.isos.length
|
||||||
|
return (
|
||||||
|
<div className={'svc virt' + (cur && tab === 'instances' ? ' has-sel' : '') + (tab !== 'instances' ? ' wide' : '')}>
|
||||||
|
<div className="svc-list">
|
||||||
|
<div className="svc-tools">
|
||||||
|
<div className="chips">
|
||||||
|
{(['instances', 'images', 'library'] as Tab[]).map((t) => (
|
||||||
|
<button key={t} className={'chip' + (tab === t ? ' on' : '')} onClick={() => { setTab(t); if (sel) setSel('') }}>
|
||||||
|
{t}{t === 'instances' ? ` · ${running}/${instances.length}` : t === 'images' ? ` · ${images.length}` : ` · ${libCount}`}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{tab === 'instances' && (
|
||||||
|
<div className="svc-actions">
|
||||||
|
<button className="btn on" onClick={() => setMaking({})}>+ New instance</button>
|
||||||
|
<span className="spacer" />
|
||||||
|
<span className="muted small">{st.kvm ? 'containers and VMs' : 'containers only — no KVM on this machine'}</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{tab === 'instances' && instances.length > 3 && <input value={filter} onChange={(e) => setFilter(e.target.value)} placeholder="filter instances" />}
|
||||||
|
{ov.error && <div className="error">{ov.error}</div>}
|
||||||
|
{task && <TaskOutput id={task.id} onDone={(t) => { load(); if (t.exit_code === 0 && !t.error) toast(`${task.title}: done`); else toast(`${task.title}: failed`, 'err') }} />}
|
||||||
|
{task && <div className="svc-actions"><button className="btn" onClick={() => setTask(null)}>Hide output</button></div>}
|
||||||
|
</div>
|
||||||
|
{tab === 'instances' && (
|
||||||
|
<div className="svc-rows">
|
||||||
|
{shown.map((i) => (
|
||||||
|
<div key={i.name} className={'svc-row' + (i.name === sel ? ' active' : '')} onClick={() => setSel(i.name)}>
|
||||||
|
<span className={'sdot ' + dot(i)} />
|
||||||
|
<div className="svc-name"><div className="n">{i.name} <span className="badge">{kind(i)}</span></div><div className="d">{i.image || '—'}{i.ipv4.length ? ` · ${i.ipv4.join(' ')}` : ''}</div></div>
|
||||||
|
<div className="svc-state">{i.status === 'Running' ? <>{fmtBytes(i.mem_usage)}<br />{i.cpu_pct.toFixed(0)}%</> : i.status.toLowerCase()}</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{instances.length === 0 && <div className="muted small" style={{ padding: 12 }}>Nothing here yet. “+ New instance” makes a container{st.kvm ? ' or a VM' : ''} from the catalogue.</div>}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{tab === 'images' && <Images st={st} images={images} instances={instances} onTask={startTask} onNew={(image, vm) => { setTab('instances'); setMaking({ image, vm }) }} onChanged={load} />}
|
||||||
|
{tab === 'library' && <Library st={st} ov={ov} onTask={startTask} onChanged={load} onNewVM={(iso) => setFromIso(iso)} />}
|
||||||
|
</div>
|
||||||
|
{tab === 'instances' && (cur ? <Detail i={cur} st={st} onBack={() => setSel('')} onChanged={load} onTask={startTask} /> : <Summary ov={ov} onChanged={load} />)}
|
||||||
|
{making && <NewInstance st={st} ov={ov} preset={making} onClose={() => setMaking(null)} onStarted={(t) => { setMaking(null); startTask(t) }} />}
|
||||||
|
{fromIso !== null && <NewVMFromISO st={st} ov={ov} iso={fromIso} onClose={() => setFromIso(null)} onStarted={(t) => { setFromIso(null); startTask(t); setTab('instances') }} />}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- the three states before "ready" ----
|
||||||
|
|
||||||
|
function Gate({ st, onChanged }: { st: VirtStatus; onChanged: () => void }) {
|
||||||
|
const [open, setOpen] = useState(false)
|
||||||
|
const [task, setTask] = useState('')
|
||||||
|
const [waiting, setWaiting] = useState(false)
|
||||||
|
// the install ends by restarting Ham Lab, so poll until it answers again
|
||||||
|
useEffect(() => {
|
||||||
|
if (!waiting) return
|
||||||
|
const t = setInterval(async () => { try { const o = await api.virt(); if (o.status.state === 'ready') { setWaiting(false); onChanged() } } catch { /* restarting */ } }, 3000)
|
||||||
|
return () => clearInterval(t)
|
||||||
|
}, [waiting, onChanged])
|
||||||
|
const headline = st.state === 'installable'
|
||||||
|
? `Incus isn't installed on this machine. Ham Lab can install it with ${st.pm}: ${st.kvm ? 'containers and virtual machines' : 'containers only, since this kernel has no KVM'}.`
|
||||||
|
: st.reason
|
||||||
|
return (
|
||||||
|
<div className="dash">
|
||||||
|
<div className="dash-bar"><h2 className="section">Virtualization</h2></div>
|
||||||
|
<section className="card">
|
||||||
|
<div className="notice">{headline}</div>
|
||||||
|
<div className="props" style={{ marginTop: 8 }}>
|
||||||
|
<div><span className="k">KVM</span><span>{st.kvm ? 'yes — virtual machines possible' : 'no — containers only'}</span></div>
|
||||||
|
<div><span className="k">Architecture</span><span className="mono">{st.arch}</span></div>
|
||||||
|
{st.pm && <div><span className="k">Packages</span><span>{st.pm}</span></div>}
|
||||||
|
<div><span className="k">Pool drivers</span><span>{st.drivers.join(', ')}</span></div>
|
||||||
|
<div><span className="k">Memory</span><span>{fmtBytes(st.mem_avail)} available of {fmtBytes(st.mem_total)}</span></div>
|
||||||
|
<div><span className="k">Disk</span><span>{fmtBytes(st.disk_free)} free where Ham Lab's data lives</span></div>
|
||||||
|
<div><span className="k">Docker</span><span>{st.docker ? 'present — its FORWARD policy needs two rules, Install adds them' : 'not here'}</span></div>
|
||||||
|
<div><span className="k">Data</span><span className="mono small">{st.dir}</span></div>
|
||||||
|
{st.layout && <div><span className="k">Last attempt</span><span>{when(st.layout.attempted)} · {st.layout.driver} pool · {st.layout.subnet}</span></div>}
|
||||||
|
</div>
|
||||||
|
{(st.state === 'installable' || st.state === 'repair') && !task && (
|
||||||
|
<div className="svc-actions"><Btn className="btn on" onClick={() => setOpen(true)}>{st.state === 'repair' ? 'Finish setup' : 'Install Incus'}</Btn></div>
|
||||||
|
)}
|
||||||
|
{task && <TaskOutput id={task} onDone={(t) => { if (t.exit_code === 0 && !t.error) setWaiting(true); else { toast('install failed — the output says where', 'err'); setTask('') } }} />}
|
||||||
|
{waiting && <div className="muted small" style={{ marginTop: 8 }}>Ham Lab is restarting to pick up its new group. The page reconnects by itself.</div>}
|
||||||
|
</section>
|
||||||
|
{open && <InstallDialog st={st} onClose={() => setOpen(false)} onStarted={(id) => { setOpen(false); setTask(id) }} />}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function InstallDialog({ st, onClose, onStarted }: { st: VirtStatus; onClose: () => void; onStarted: (id: string) => void }) {
|
||||||
|
const [driver, setDriver] = useState(st.layout?.driver ?? st.drivers[0] ?? 'dir')
|
||||||
|
const [size, setSize] = useState(String(st.layout?.pool_size_gib || st.pool_size_gib || 20))
|
||||||
|
const [subnet, setSubnet] = useState(st.layout?.subnet || st.subnet || '172.16.100.1/24')
|
||||||
|
const [docker, setDocker] = useState(st.docker)
|
||||||
|
const [arc, setArc] = useState(st.layout?.arc_max_gib ? String(st.layout.arc_max_gib) : '')
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [busy, setBusy] = useState(false)
|
||||||
|
useEscape(onClose)
|
||||||
|
const submit = async (e: FormEvent) => {
|
||||||
|
e.preventDefault(); setErr(''); setBusy(true)
|
||||||
|
try { const t = await api.virtInstall({ driver, pool_size_gib: Number(size) || 0, subnet: subnet.trim(), docker_rules: docker, arc_max_gib: Number(arc) || 0 }); onStarted(t.id) }
|
||||||
|
catch (e) { setErr(errMsg(e)); setBusy(false) }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="modal-back" onClick={onClose}>
|
||||||
|
<form className="modal" onClick={(e) => e.stopPropagation()} onSubmit={submit}>
|
||||||
|
<h3>{st.state === 'repair' ? 'Finish setting up Incus' : 'Install Incus'}</h3>
|
||||||
|
<div className="notice">Everything goes under <span className="mono">{st.dir}</span>: the pool's backing file, ISOs, images, exports. Where is not a choice; how big is.</div>
|
||||||
|
<label className="muted">Pool driver
|
||||||
|
<select value={driver} onChange={(e) => setDriver(e.target.value)}>
|
||||||
|
{st.drivers.map((d) => <option key={d} value={d}>{d === 'dir' ? 'plain files on this disk — each VM\u2019s disk is its own file, sized on its own; snapshots are copies' : d === 'zfs' ? 'zfs — one pre-sized file holding every disk; snapshots and clones are instant' : 'btrfs — one pre-sized file holding every disk; snapshots are instant'}</option>)}
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
{driver !== 'dir' && (
|
||||||
|
<label className="muted">Ceiling of that file, GiB — sparse, it grows only as disks fill it · {fmtBytes(st.disk_free)} free
|
||||||
|
<input type="number" min={5} max={Math.floor(st.disk_free / GiB)} value={size} onChange={(e) => setSize(e.target.value)} required />
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<label className="muted">Bridge address — picked clear of this machine's routes and Docker's pools
|
||||||
|
<input className="mono" value={subnet} onChange={(e) => setSubnet(e.target.value)} spellCheck={false} required />
|
||||||
|
</label>
|
||||||
|
{st.docker && (
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
|
||||||
|
<input type="checkbox" checked={docker} onChange={(e) => setDocker(e.target.checked)} style={{ width: 'auto' }} />
|
||||||
|
Add the two DOCKER-USER rules that let the bridge's traffic through Docker's FORWARD drop
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
{st.zfs_host && (
|
||||||
|
<label className="muted">Cap the ZFS ARC, GiB — blank leaves it at {st.arc_max_gib} GiB; a cap makes guest memory deterministic
|
||||||
|
<input type="number" min={1} max={Math.floor(st.mem_total / GiB)} value={arc} onChange={(e) => setArc(e.target.value)} placeholder="leave as is" />
|
||||||
|
</label>
|
||||||
|
)}
|
||||||
|
<div className="muted small">Ham Lab's account ({st.user}) joins the incus-admin group and Ham Lab restarts at the end.</div>
|
||||||
|
{err && <div className="error">{err}</div>}
|
||||||
|
<div className="svc-actions">
|
||||||
|
<button type="submit" className="btn on" disabled={busy}>{busy ? 'Starting…' : 'Install'}</button>
|
||||||
|
<span className="spacer" />
|
||||||
|
<button type="button" className="btn" onClick={onClose}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- the right-hand pane when nothing is selected: what this machine's Incus is ----
|
||||||
|
|
||||||
|
// A location is a plain-files storage pool at a folder of his choosing.
|
||||||
|
async function addLocation(onDone: (name: string) => void) {
|
||||||
|
const path = await askText('Folder on this machine for instance disks', '/', 'Next')
|
||||||
|
if (!path) return
|
||||||
|
const base = path.split('/').filter(Boolean).pop() ?? 'disks'
|
||||||
|
const name = await askText(`A name for ${path}`, base.toLowerCase().replace(/[^a-z0-9-]+/g, '-').replace(/^-|-$/g, ''), 'Add')
|
||||||
|
if (!name) return
|
||||||
|
try { const r = await api.virtPoolCreate(name, path); toast(`${r.name} added at ${r.path}`); onDone(r.name) } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
|
||||||
|
function Summary({ ov, onChanged }: { ov: VirtOverview; onChanged: () => void }) {
|
||||||
|
const st = ov.status
|
||||||
|
return (
|
||||||
|
<div className="svc-detail">
|
||||||
|
<div className="svc-head"><span className="dname">Incus {st.version}</span><span className="badge">{st.arch}</span>{!st.kvm && <span className="badge warn">no KVM</span>}</div>
|
||||||
|
<div className="props">
|
||||||
|
<div><span className="k">Data</span><span className="mono small">{st.dir}</span></div>
|
||||||
|
<div><span className="k">Memory</span><span>{fmtBytes(st.mem_avail)} available of {fmtBytes(st.mem_total)}</span></div>
|
||||||
|
{st.layout && <div><span className="k">Installed</span><span>{when(st.layout.attempted)} by Ham Lab {st.layout.hamlab_version ?? ''}</span></div>}
|
||||||
|
{!st.layout && <div><span className="k">Installed</span><span>outside Ham Lab — pools and networks below are as found</span></div>}
|
||||||
|
</div>
|
||||||
|
<h3 className="stack-head">Disk locations <span className="muted">· where an instance\u2019s disk can go</span></h3>
|
||||||
|
<table className="virt-table"><thead><tr><th>Location</th><th>Where</th><th className="num">Used</th><th className="num">Free</th><th className="num">Disks</th><th /></tr></thead>
|
||||||
|
<tbody>{(ov.pools ?? []).map((p) => <tr key={p.name}><td>{p.name}{p.name === ov.default_pool && <span className="badge">default</span>}</td><td>{p.where}<div className="muted small mono">{p.driver} · {p.source}</div></td><td className="num">{p.total ? fmtBytes(p.used) : '—'}</td><td className="num">{p.total ? fmtBytes(p.total - p.used) : '—'}</td><td className="num">{p.used_by}</td>
|
||||||
|
<td className="num">{p.used_by === 0 && p.name !== ov.default_pool && <Btn className="btn danger" confirm={`Remove the location ${p.name}? Nothing uses it; the folder itself is left alone.`} onClick={async () => { try { await api.virtPoolDelete(p.name); toast('removed'); onChanged() } catch (e) { toast(errMsg(e), 'err') } }}>Remove</Btn>}</td></tr>)}</tbody></table>
|
||||||
|
<div className="svc-actions"><Btn className="btn" onClick={() => addLocation(onChanged)}>+ Add a location…</Btn><span className="muted small">a folder on this machine; VM disks become files in it, containers folders</span></div>
|
||||||
|
<h3 className="stack-head">Networks</h3>
|
||||||
|
<table className="virt-table"><thead><tr><th>Network</th><th>Type</th><th>Address</th><th>NAT</th><th className="num">Users</th></tr></thead>
|
||||||
|
<tbody>{(ov.networks ?? []).filter((n) => n.managed).map((n) => <tr key={n.name}><td>{n.name}</td><td>{n.type}</td><td className="mono small">{n.address ?? '—'}</td><td>{n.nat ? 'yes' : 'no'}</td><td className="num">{n.used_by}</td></tr>)}</tbody></table>
|
||||||
|
<div className="muted small">Pick an instance on the left, or make one. Consoles open as tabs on the Terminal page.</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- one instance ----
|
||||||
|
|
||||||
|
function Detail({ i, st, onBack, onChanged, onTask }: { i: VirtInstance; st: VirtStatus; onBack: () => void; onChanged: () => void; onTask: (t: TaskInfo) => void }) {
|
||||||
|
const [tab, setTab] = useState<'overview' | 'snapshots' | 'mounts' | 'config'>('overview')
|
||||||
|
const [vga, setVga] = useState(false)
|
||||||
|
const running = i.status === 'Running'
|
||||||
|
const isVM = i.type === 'virtual-machine'
|
||||||
|
const stopped = i.status === 'Stopped'
|
||||||
|
const act = (a: 'start' | 'stop' | 'restart' | 'kill') => async () => { try { await api.virtAction(i.name, a); toast(`${i.name}: ${a} ok`); onChanged() } catch (e) { toast(`${i.name}: ${errMsg(e)}`, 'err') } }
|
||||||
|
const term = (k: 'console' | 'exec') => async () => {
|
||||||
|
try { const t = await api.virtTerminal(i.name, k); toast(`${k === 'exec' ? 'shell' : 'console'} open as “${t.name}” on the Terminal tab`); window.dispatchEvent(new CustomEvent('hamlab:terminal', { detail: t.id })) }
|
||||||
|
catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
const rename = async () => { const to = await askText(`Rename ${i.name} to`, i.name, 'Rename'); if (!to || to === i.name) return; try { await api.virtRename(i.name, to); toast('renamed'); onBack(); onChanged() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const del = async () => {
|
||||||
|
const typed = await askText(`Delete ${i.name}${running ? ', which is running' : ''}? Its disk and ${i.snapshots.length} snapshot${i.snapshots.length === 1 ? '' : 's'} go with it. Type its name to confirm.`, '', 'Delete')
|
||||||
|
if (typed === null) return
|
||||||
|
if (typed !== i.name) { toast('name did not match', 'err'); return }
|
||||||
|
try { await api.virtDelete(i.name, running); toast(`${i.name} deleted`); onBack(); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
const publish = async () => {
|
||||||
|
const alias = await askText(`Publish ${i.name} as an image — an alias for it`, `${i.name}-image`, 'Publish')
|
||||||
|
if (!alias) return
|
||||||
|
try { onTask(await api.virtImagePublish(i.name, alias)) } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
const snap = async () => {
|
||||||
|
const name = await askText(`Snapshot ${i.name} — a name, or blank for the date`, '', 'Snapshot')
|
||||||
|
if (name === null) return
|
||||||
|
try { const r = await api.virtSnapshot(i.name, name); toast(`snapshot ${r.name} taken`); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="svc-detail">
|
||||||
|
<CloseBtn onClick={onBack} />
|
||||||
|
<div className="svc-head"><button className="btn back" onClick={onBack}>‹ Back</button><span className={'sdot ' + dot(i)} /><span className="dname">{i.name}</span><span className="badge">{kind(i)}</span><span className={'badge' + (i.status === 'Error' ? ' bad' : '')}>{i.status.toLowerCase()}</span></div>
|
||||||
|
<div className="props">
|
||||||
|
<div><span className="k">Image</span><span>{i.image || (Object.keys(i.devices).some((d) => d.startsWith('iso')) ? 'none — installed from an ISO' : 'none')}{i.arch ? <span className="muted"> · {i.arch}</span> : ''}</span></div>
|
||||||
|
{i.description && <div><span className="k">About</span><span>{i.description}</span></div>}
|
||||||
|
{running && <div><span className="k">Using</span><span>{i.cpu_pct.toFixed(0)}% of a core · {fmtBytes(i.mem_usage)}{i.mem_peak ? ` (peak ${fmtBytes(i.mem_peak)})` : ''}{i.processes > 0 ? ` · ${i.processes} processes` : ''}</span></div>}
|
||||||
|
<div><span className="k">Limits</span><span>{i.cpu ? `${i.cpu} CPU` : 'all CPUs'} · {i.memory || 'unlimited memory'}</span></div>
|
||||||
|
<div><span className="k">Disk</span><span>{i.disk_total > 0 ? `${fmtBytes(i.disk_usage)} of ${fmtBytes(i.disk_total)}` : i.disk_usage > 0 ? fmtBytes(i.disk_usage) : 'no limit'}{i.devices.root?.pool ? <span className="muted"> · on {i.devices.root.pool}</span> : ''}</span></div>
|
||||||
|
<div><span className="k">Network</span><span className="mono small">{i.ipv4.length ? i.ipv4.join(' ') : running ? 'no address yet' : '—'}</span></div>
|
||||||
|
<div><span className="k">Created</span><span>{when(i.created)}</span></div>
|
||||||
|
</div>
|
||||||
|
<div className="svc-actions">
|
||||||
|
{!running && <Btn className="btn on" onClick={act('start')}>Start</Btn>}
|
||||||
|
{running && <Btn className="btn" onClick={term('exec')}>Shell</Btn>}
|
||||||
|
<Btn className="btn" onClick={term('console')} title="the serial console: boot output, and a login prompt where the guest has one on it">Console</Btn>
|
||||||
|
{isVM && running && <Btn className="btn" onClick={() => setVga(true)} title="the VM's screen, keyboard and mouse — what an installer needs">VGA</Btn>}
|
||||||
|
{running && <Btn className="btn" confirm={`Restart ${i.name}?`} onClick={act('restart')}>Restart</Btn>}
|
||||||
|
{running && <Btn className="btn danger" confirm={`Stop ${i.name}? It is asked to shut down cleanly.`} onClick={act('stop')}>Stop</Btn>}
|
||||||
|
{running && <Btn className="btn danger" confirm={`Force stop ${i.name}? Like pulling the plug — anything unsaved inside is lost.`} onClick={act('kill')}>Force stop</Btn>}
|
||||||
|
<Btn className="btn" onClick={snap}>Snapshot</Btn>
|
||||||
|
<Btn className="btn" onClick={async () => { try { onTask(await api.virtExportsRun(i.name)) } catch (e) { toast(errMsg(e), 'err') } }} title="a tarball of it in the library's exports folder">Export</Btn>
|
||||||
|
{stopped && <Btn className="btn" onClick={rename}>Rename</Btn>}
|
||||||
|
{stopped && <Btn className="btn" onClick={publish} title="make an image of it, to launch more like it">Publish</Btn>}
|
||||||
|
<Btn className="btn danger" onClick={del}>Delete…</Btn>
|
||||||
|
</div>
|
||||||
|
<div className="tabs">
|
||||||
|
<button className={tab === 'overview' ? 'on' : ''} onClick={() => setTab('overview')}>Limits</button>
|
||||||
|
<button className={tab === 'snapshots' ? 'on' : ''} onClick={() => setTab('snapshots')}>Snapshots · {i.snapshots.length}</button>
|
||||||
|
<button className={tab === 'mounts' ? 'on' : ''} onClick={() => setTab('mounts')}>Disks · {Object.values(i.devices).filter((d) => d.type === 'disk').length}</button>
|
||||||
|
<button className={tab === 'config' ? 'on' : ''} onClick={() => setTab('config')}>Config</button>
|
||||||
|
</div>
|
||||||
|
{tab === 'mounts' && <Mounts i={i} st={st} onChanged={onChanged} />}
|
||||||
|
{vga && <VgaConsole name={i.name} onClose={() => setVga(false)} />}
|
||||||
|
{tab === 'overview' && <Limits i={i} st={st} onChanged={onChanged} />}
|
||||||
|
{tab === 'snapshots' && <Snapshots i={i} onChanged={onChanged} />}
|
||||||
|
{tab === 'config' && (
|
||||||
|
<>
|
||||||
|
<div className="virt-kv">{Object.entries(i.config).sort().map(([k, v]) => <Fragment key={k}><span className="muted mono">{k}</span><span className="mono">{v}</span></Fragment>)}</div>
|
||||||
|
<h3 className="stack-head">Devices</h3>
|
||||||
|
<div className="virt-kv">{Object.entries(i.devices).sort().map(([d, cfg]) => <Fragment key={d}><span className="muted mono">{d}</span><span className="mono">{Object.entries(cfg).map(([k, v]) => `${k}=${v}`).join(' ')}</span></Fragment>)}</div>
|
||||||
|
<div className="muted small">Profiles: {i.profiles.join(', ') || '—'}</div>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Limits({ i, st, onChanged }: { i: VirtInstance; st: VirtStatus; onChanged: () => void }) {
|
||||||
|
const [cpu, setCpu] = useState(i.cpu ?? '')
|
||||||
|
const [mem, setMem] = useState(i.memory ?? '')
|
||||||
|
const [auto, setAuto] = useState(i.config['boot.autostart'] ?? '')
|
||||||
|
const [desc, setDesc] = useState(i.description ?? '')
|
||||||
|
useEffect(() => { setCpu(i.cpu ?? ''); setMem(i.memory ?? ''); setAuto(i.config['boot.autostart'] ?? ''); setDesc(i.description ?? '') }, [i.name, i.cpu, i.memory, i.description, i.config])
|
||||||
|
const changed = cpu !== (i.cpu ?? '') || mem !== (i.memory ?? '') || auto !== (i.config['boot.autostart'] ?? '') || desc !== (i.description ?? '')
|
||||||
|
const save = async () => { try { await api.virtConfig(i.name, { 'limits.cpu': cpu, 'limits.memory': mem, 'boot.autostart': auto, description: desc }); toast('saved'); onChanged() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
return (
|
||||||
|
<section className="card">
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">CPUs<input className="mono" value={cpu} onChange={(e) => setCpu(e.target.value)} placeholder="all" /></label>
|
||||||
|
<label className="muted">Memory<input className="mono" value={mem} onChange={(e) => setMem(e.target.value)} placeholder="unlimited · e.g. 2GiB" /></label>
|
||||||
|
</div>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">Start with the machine
|
||||||
|
<select value={auto} onChange={(e) => setAuto(e.target.value)}><option value="">default (as it was when the host went down)</option><option value="true">yes</option><option value="false">no</option></select>
|
||||||
|
</label>
|
||||||
|
<label className="muted">Description<input value={desc} onChange={(e) => setDesc(e.target.value)} maxLength={200} /></label>
|
||||||
|
</div>
|
||||||
|
<div className="muted small">{i.type === 'container' ? 'Containers take new limits live.' : 'A VM takes CPU and memory changes live where its agent is running, otherwise on its next start.'} {fmtBytes(st.mem_avail)} is free on this machine right now.</div>
|
||||||
|
<div className="svc-actions"><Btn className="btn on" disabled={!changed} onClick={save}>Save</Btn></div>
|
||||||
|
</section>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Snapshots({ i, onChanged }: { i: VirtInstance; onChanged: () => void }) {
|
||||||
|
const [sched, setSched] = useState(i.schedule ?? '')
|
||||||
|
const [exp, setExp] = useState(i.expiry ?? '')
|
||||||
|
useEffect(() => { setSched(i.schedule ?? ''); setExp(i.expiry ?? '') }, [i.name, i.schedule, i.expiry])
|
||||||
|
const saveSched = async () => { try { await api.virtConfig(i.name, { 'snapshots.schedule': sched.trim(), 'snapshots.expiry': exp.trim() }); toast('schedule saved'); onChanged() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const on = async (s: string, a: 'restore' | 'delete') => {
|
||||||
|
const ok = await askYes(a === 'restore' ? `Restore ${i.name} to snapshot ${s}? Everything since it is lost${i.status === 'Running' ? '; it is stopped first' : ''}.` : `Delete snapshot ${s} of ${i.name}?`, a === 'restore' ? 'Restore' : 'Delete')
|
||||||
|
if (!ok) return
|
||||||
|
try { await api.virtSnapshotAction(i.name, s, a); toast(`${a} ${s}: ok`); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{i.snapshots.length === 0 && <div className="muted small">No snapshots. “Snapshot” above takes one now; a schedule below keeps taking them.</div>}
|
||||||
|
{i.snapshots.length > 0 && (
|
||||||
|
<table className="virt-table"><thead><tr><th>Snapshot</th><th>Taken</th><th>Expires</th><th className="num">Size</th><th /></tr></thead>
|
||||||
|
<tbody>{[...i.snapshots].sort((a, b) => b.created.localeCompare(a.created)).map((s) => (
|
||||||
|
<tr key={s.name}><td className="mono">{s.name}</td><td>{when(s.created)}</td><td>{s.expires ? when(s.expires) : 'never'}</td><td className="num">{s.size ? fmtBytes(s.size) : '—'}</td>
|
||||||
|
<td className="num"><Btn className="btn" onClick={() => on(s.name, 'restore')}>Restore</Btn> <Btn className="btn danger" onClick={() => on(s.name, 'delete')}>Delete</Btn></td></tr>
|
||||||
|
))}</tbody></table>
|
||||||
|
)}
|
||||||
|
<section className="card">
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">Schedule — @hourly, @daily, @weekly, or a cron line<input className="mono" value={sched} onChange={(e) => setSched(e.target.value)} placeholder="none" spellCheck={false} /></label>
|
||||||
|
<label className="muted">Keep for — 1d, 2w, 3M…<input className="mono" value={exp} onChange={(e) => setExp(e.target.value)} placeholder="forever" spellCheck={false} /></label>
|
||||||
|
</div>
|
||||||
|
<div className="muted small">Incus takes and expires these itself, so they carry on whether or not this console is up.</div>
|
||||||
|
<div className="svc-actions"><Btn className="btn on" disabled={sched === (i.schedule ?? '') && exp === (i.expiry ?? '')} onClick={saveSched}>Save schedule</Btn></div>
|
||||||
|
</section>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- making one ----
|
||||||
|
|
||||||
|
// Where an instance's disk goes. One entry is still a choice shown; a new one can be added here.
|
||||||
|
function PoolPick({ ov, value, onChange }: { ov: VirtOverview; value: string; onChange: (v: string) => void }) {
|
||||||
|
const [extra, setExtra] = useState<{ name: string; where: string }[]>([])
|
||||||
|
const pools = [...(ov.pools ?? []).map((p) => ({ name: p.name, where: p.where + (p.total ? ` · ${fmtBytes(p.total - p.used)} free` : '') })), ...extra.filter((e) => !(ov.pools ?? []).some((p) => p.name === e.name))]
|
||||||
|
return (
|
||||||
|
<label className="muted">Disk location
|
||||||
|
<select value={value} onChange={(e) => { if (e.target.value === '+') addLocation((n) => { setExtra((x) => [...x, { name: n, where: 'just added' }]); onChange(n) }); else onChange(e.target.value) }}>
|
||||||
|
{pools.map((p) => <option key={p.name} value={p.name}>{p.name} — {p.where}</option>)}
|
||||||
|
<option value="+">+ add a location…</option>
|
||||||
|
</select>
|
||||||
|
</label>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function NewInstance({ st, ov, preset, onClose, onStarted }: { st: VirtStatus; ov: VirtOverview; preset: { image?: string; vm?: boolean }; onClose: () => void; onStarted: (t: TaskInfo) => void }) {
|
||||||
|
const [vm, setVm] = useState(!!preset.vm && st.kvm)
|
||||||
|
const [image, setImage] = useState(preset.image ?? '')
|
||||||
|
const [name, setName] = useState('')
|
||||||
|
const [cpu, setCpu] = useState('')
|
||||||
|
const [mem, setMem] = useState('')
|
||||||
|
const [disk, setDisk] = useState('')
|
||||||
|
const [pool, setPool] = useState(ov.default_pool || ov.pools?.[0]?.name || '')
|
||||||
|
const nets = (ov.networks ?? []).filter((n) => n.managed || n.type === 'bridge')
|
||||||
|
const [net, setNet] = useState(nets.find((n) => n.name === 'incusbr0')?.name ?? nets[0]?.name ?? '')
|
||||||
|
const [cloud, setCloud] = useState('')
|
||||||
|
const [showCloud, setShowCloud] = useState(false)
|
||||||
|
const [desc, setDesc] = useState('')
|
||||||
|
const [start, setStart] = useState(true)
|
||||||
|
const [q, setQ] = useState('')
|
||||||
|
const [remote, setRemote] = useState<VirtImage[] | null>(null)
|
||||||
|
const [remoteErr, setRemoteErr] = useState('')
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [busy, setBusy] = useState(false)
|
||||||
|
const type = vm ? 'virtual-machine' : 'container'
|
||||||
|
useEscape(onClose)
|
||||||
|
useEffect(() => { setRemote(null); setRemoteErr(''); api.virtRemote(type).then((r) => setRemote(r.images)).catch((e) => setRemoteErr(errMsg(e))) }, [type])
|
||||||
|
const local = (ov.images ?? []).filter((im) => im.type === type)
|
||||||
|
const picks = useMemo(() => {
|
||||||
|
const f = q.trim().toLowerCase()
|
||||||
|
const match = (im: VirtImage) => !f || `${im.aliases.join(' ')} ${im.description} ${im.os} ${im.release}`.toLowerCase().includes(f)
|
||||||
|
const seen = new Set<string>()
|
||||||
|
const out: { ref: string; label: string; sub: string; here: boolean }[] = []
|
||||||
|
for (const im of local) { if (!match(im)) continue; const ref = imgLabel(im); if (seen.has(ref)) continue; seen.add(ref); out.push({ ref, label: ref, sub: `${im.description} · on this machine · ${fmtBytes(im.size)}`, here: true }) }
|
||||||
|
for (const im of remote ?? []) {
|
||||||
|
const a = im.aliases.find((x) => !x.includes('/default')) ?? im.aliases[0]
|
||||||
|
if (!a || !match(im)) continue
|
||||||
|
const ref = `images:${a}`
|
||||||
|
if (seen.has(ref)) continue
|
||||||
|
seen.add(ref); out.push({ ref, label: a, sub: `${im.description}${im.variant && im.variant !== 'default' ? ` · ${im.variant}` : ''} · ${fmtBytes(im.size)}`, here: false })
|
||||||
|
}
|
||||||
|
return out.slice(0, 60)
|
||||||
|
}, [q, local, remote])
|
||||||
|
const submit = async (e: FormEvent) => {
|
||||||
|
e.preventDefault(); setErr(''); setBusy(true)
|
||||||
|
try { onStarted(await api.virtCreate({ name: name.trim(), image: image.trim(), vm, cpu: cpu.trim(), memory: mem.trim(), disk: disk.trim(), pool, network: net, cloud_init: showCloud ? cloud : '', profiles: [], description: desc.trim(), start })) }
|
||||||
|
catch (e) { setErr(errMsg(e)); setBusy(false) }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="modal-back" onClick={onClose}>
|
||||||
|
<form className="modal" onClick={(e) => e.stopPropagation()} onSubmit={submit}>
|
||||||
|
<h3>New instance</h3>
|
||||||
|
<div className="chips">
|
||||||
|
<button type="button" className={'chip' + (!vm ? ' on' : '')} onClick={() => setVm(false)}>container</button>
|
||||||
|
<button type="button" className={'chip' + (vm ? ' on' : '')} disabled={!st.kvm} title={st.kvm ? '' : 'no KVM on this machine'} onClick={() => setVm(true)}>virtual machine</button>
|
||||||
|
</div>
|
||||||
|
<label className="muted">Image — from this machine, or the public catalogue for {st.arch}
|
||||||
|
<input className="mono" value={image} onChange={(e) => { setImage(e.target.value); setQ(e.target.value.replace(/^images:/, '')) }} placeholder="images:debian/13" spellCheck={false} autoCapitalize="off" autoCorrect="off" required />
|
||||||
|
</label>
|
||||||
|
<div className="virt-pick">
|
||||||
|
{remote === null && !remoteErr && <div className="muted small" style={{ padding: 8 }}>Reading the catalogue…</div>}
|
||||||
|
{remoteErr && <div className="error" style={{ margin: 8 }}>catalogue: {remoteErr}</div>}
|
||||||
|
{picks.map((p) => (
|
||||||
|
<div key={p.ref} className={'svc-row' + (image === p.ref ? ' active' : '')} onClick={() => setImage(p.ref)}>
|
||||||
|
<div className="svc-name"><div className="n">{p.label}{p.here && <span className="badge">here</span>}</div><div className="d">{p.sub}</div></div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
{remote && picks.length === 0 && <div className="muted small" style={{ padding: 8 }}>Nothing in the catalogue matches.</div>}
|
||||||
|
</div>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">Name<input value={name} onChange={(e) => setName(e.target.value)} pattern="[a-zA-Z][a-zA-Z0-9\-]{0,62}" placeholder="web-1" spellCheck={false} autoCapitalize="off" autoCorrect="off" required /></label>
|
||||||
|
<label className="muted">Network<select value={net} onChange={(e) => setNet(e.target.value)}>{nets.map((n) => <option key={n.name} value={n.name}>{n.name}{n.address ? ` · ${n.address}` : ''}</option>)}</select></label>
|
||||||
|
</div>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">CPUs<input className="mono" value={cpu} onChange={(e) => setCpu(e.target.value)} placeholder={vm ? '2' : 'all'} /></label>
|
||||||
|
<label className="muted">Memory<input className="mono" value={mem} onChange={(e) => setMem(e.target.value)} placeholder={vm ? '2GiB' : 'unlimited'} /></label>
|
||||||
|
<label className="muted">Root disk<input className="mono" value={disk} onChange={(e) => setDisk(e.target.value)} placeholder={vm ? '20GiB' : 'no limit'} /></label>
|
||||||
|
</div>
|
||||||
|
<PoolPick ov={ov} value={pool} onChange={setPool} />
|
||||||
|
<div className="muted small">{fmtBytes(st.mem_avail)} free on this machine.{vm ? ' A VM’s memory is a reservation.' : ''}</div>
|
||||||
|
<label className="muted">Description — optional<input value={desc} onChange={(e) => setDesc(e.target.value)} maxLength={200} /></label>
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
|
||||||
|
<input type="checkbox" checked={showCloud} onChange={(e) => setShowCloud(e.target.checked)} style={{ width: 'auto' }} />cloud-init user-data (an SSH key, packages, a first command — needs a /cloud image)
|
||||||
|
</label>
|
||||||
|
{showCloud && <div className="svc-actions"><Btn className="btn" onClick={async () => { try { const k = await api.virtSSHKeys(); if (!k.keys.length) { toast('no public keys found for ' + k.user, 'info'); return } setCloud(`#cloud-config\nusers:\n - name: ${k.user}\n groups: [sudo]\n shell: /bin/bash\n sudo: ALL=(ALL) NOPASSWD:ALL\n ssh_authorized_keys:\n${k.keys.map((x) => ' - ' + x).join('\n')}\npackage_update: true\npackages: [curl, vim]\n`) } catch (e) { toast(errMsg(e), 'err') } }} title="a cloud-config that makes your account inside, with the public keys this machine's Ham Lab account holds">Use my SSH keys</Btn></div>}
|
||||||
|
{showCloud && <textarea className="mono" rows={6} value={cloud} onChange={(e) => setCloud(e.target.value)} placeholder={'#cloud-config\nusers:\n - name: core\n ssh_authorized_keys:\n - ssh-ed25519 AAAA…\n sudo: ALL=(ALL) NOPASSWD:ALL\npackages: [curl]'} spellCheck={false} />}
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
|
||||||
|
<input type="checkbox" checked={start} onChange={(e) => setStart(e.target.checked)} style={{ width: 'auto' }} />Start it once made
|
||||||
|
</label>
|
||||||
|
{err && <div className="error">{err}</div>}
|
||||||
|
<div className="svc-actions">
|
||||||
|
<button type="submit" className="btn on" disabled={busy || !image.trim() || !name.trim()}>{busy ? 'Starting…' : start ? 'Create and start' : 'Create'}</button>
|
||||||
|
<span className="spacer" />
|
||||||
|
<button type="button" className="btn" onClick={onClose}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- images: the local store, the catalogue, and files brought in ----
|
||||||
|
|
||||||
|
function Images({ st, images, instances, onTask, onNew, onChanged }: { st: VirtStatus; images: VirtImage[]; instances: VirtInstance[]; onTask: (t: TaskInfo) => void; onNew: (image: string, vm: boolean) => void; onChanged: () => void }) {
|
||||||
|
const [q, setQ] = useState('')
|
||||||
|
const [type, setType] = useState<'container' | 'virtual-machine'>('container')
|
||||||
|
const [remote, setRemote] = useState<VirtImage[] | null>(null)
|
||||||
|
const [remoteErr, setRemoteErr] = useState('')
|
||||||
|
const [path, setPath] = useState('')
|
||||||
|
const [alias, setAlias] = useState('')
|
||||||
|
const [itype, setItype] = useState<'container' | 'virtual-machine'>('container')
|
||||||
|
const loadRemote = useCallback((refresh = false) => { setRemote(null); setRemoteErr(''); api.virtRemote(type, refresh).then((r) => setRemote(r.images)).catch((e) => setRemoteErr(errMsg(e))) }, [type])
|
||||||
|
useEffect(() => { loadRemote() }, [loadRemote])
|
||||||
|
const usedBy = (im: VirtImage) => instances.filter((i) => i.config['volatile.base_image'] === im.fingerprint || im.aliases.includes(i.image)).map((i) => i.name)
|
||||||
|
const f = q.trim().toLowerCase()
|
||||||
|
const found = (remote ?? []).filter((im) => f && `${im.aliases.join(' ')} ${im.description} ${im.os} ${im.release} ${im.variant}`.toLowerCase().includes(f)).slice(0, 40)
|
||||||
|
const del = async (im: VirtImage) => {
|
||||||
|
if (!(await askYes(`Delete the image ${imgLabel(im)} from this machine's store? Instances made from it are not affected; the file in the library, if any, stays.`, 'Delete'))) return
|
||||||
|
try { await api.virtImageDelete(im.fingerprint); toast('image deleted'); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
const imp = async (e: FormEvent) => { e.preventDefault(); try { onTask(await api.virtImageImport(path.trim(), alias.trim(), itype)); setPath(''); setAlias('') } catch (er) { toast(errMsg(er), 'err') } }
|
||||||
|
return (
|
||||||
|
<div className="svc-rows" style={{ padding: 12, display: 'flex', flexDirection: 'column', gap: 14 }}>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">On this machine</span><span className="spacer" /><span className="muted small">{images.length} image{images.length === 1 ? '' : 's'} · {fmtBytes(images.reduce((a, im) => a + im.size, 0))}</span></div>
|
||||||
|
{images.length === 0 && <div className="muted small">No images yet. Make an instance and its image is kept here; or download one below so the next one is instant.</div>}
|
||||||
|
{images.length > 0 && (
|
||||||
|
<table className="virt-table"><thead><tr><th>Image</th><th>Type</th><th>What</th><th className="num">Size</th><th>Used by</th><th /></tr></thead>
|
||||||
|
<tbody>{images.map((im) => (
|
||||||
|
<tr key={im.fingerprint}><td className="mono">{im.aliases.join(', ') || im.fingerprint.slice(0, 12)}</td><td>{im.type === 'virtual-machine' ? 'VM' : 'LXC'}</td><td>{im.description}{im.cached ? <span className="muted small"> · cached from {im.source}</span> : ''}</td><td className="num">{fmtBytes(im.size)}</td><td className="small">{usedBy(im).join(' ') || '—'}</td>
|
||||||
|
<td className="num"><Btn className="btn" onClick={() => onNew(imgLabel(im), im.type === 'virtual-machine')}>New instance</Btn> <Btn className="btn danger" onClick={() => del(im)}>Delete</Btn></td></tr>
|
||||||
|
))}</tbody></table>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">Download from the catalogue</span><span className="spacer" />
|
||||||
|
<div className="chips"><button className={'chip' + (type === 'container' ? ' on' : '')} onClick={() => setType('container')}>containers</button><button className={'chip' + (type === 'virtual-machine' ? ' on' : '')} disabled={!st.kvm} onClick={() => setType('virtual-machine')}>VMs</button></div>
|
||||||
|
<Btn className="btn" onClick={() => loadRemote(true)} title="the catalogue is kept for an hour">Refresh</Btn></div>
|
||||||
|
<input value={q} onChange={(e) => setQ(e.target.value)} placeholder={`search images.linuxcontainers.org for ${st.arch} — debian, ubuntu 24.04, alpine…`} />
|
||||||
|
{remote === null && !remoteErr && <div className="muted small">Reading the catalogue…</div>}
|
||||||
|
{remoteErr && <div className="error">{remoteErr}</div>}
|
||||||
|
{remote && !f && <div className="muted small">{remote.length} {type === 'virtual-machine' ? 'VM' : 'container'} images for {st.arch}. Type to search. A download keeps the image in the store and a copy of it in the library.</div>}
|
||||||
|
{found.length > 0 && (
|
||||||
|
<div className="virt-pick">{found.map((im) => { const a = im.aliases.find((x) => !x.includes('/default')) ?? im.aliases[0]; return (
|
||||||
|
<div key={im.fingerprint} className="svc-row" style={{ cursor: 'default' }}>
|
||||||
|
<div className="svc-name"><div className="n">{a}</div><div className="d">{im.description}{im.variant && im.variant !== 'default' ? ` · ${im.variant}` : ''} · {fmtBytes(im.size)}</div></div>
|
||||||
|
<Btn className="btn" onClick={async () => { try { onTask(await api.virtImageDownload(a)) } catch (e) { toast(errMsg(e), 'err') } }}>Download</Btn>
|
||||||
|
</div>) })}</div>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">Import a file from this machine</span></div>
|
||||||
|
<form onSubmit={imp} className="virt-form-row" style={{ flexWrap: 'wrap' }}>
|
||||||
|
<label className="muted" style={{ flex: '2 1 240px' }}>Path — a template tarball, or a qcow2 / raw disk for a VM<input className="mono" value={path} onChange={(e) => setPath(e.target.value)} placeholder="/path/to/image.tar.xz" spellCheck={false} required /></label>
|
||||||
|
<label className="muted">Alias<input className="mono" value={alias} onChange={(e) => setAlias(e.target.value)} placeholder="my-template" spellCheck={false} required /></label>
|
||||||
|
<label className="muted">Type<select value={itype} onChange={(e) => setItype(e.target.value as 'container' | 'virtual-machine')}><option value="container">container template</option><option value="virtual-machine" disabled={!st.kvm}>VM disk image</option></select></label>
|
||||||
|
<div className="svc-actions" style={{ alignSelf: 'flex-end' }}><button className="btn on" disabled={!path.trim() || !alias.trim()}>Import</button></div>
|
||||||
|
</form>
|
||||||
|
<div className="muted small">The file is copied into <span className="mono">{st.dir}/images/</span> first — the library is Ham Lab's — then loaded into Incus.</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Mounts({ i, st, onChanged }: { i: VirtInstance; st: VirtStatus; onChanged: () => void }) {
|
||||||
|
const [src, setSrc] = useState('')
|
||||||
|
const [dst, setDst] = useState('')
|
||||||
|
const [dev, setDev] = useState('')
|
||||||
|
const [ro, setRo] = useState(false)
|
||||||
|
const [shift, setShift] = useState(true)
|
||||||
|
const isos = st.dir + '/isos/'
|
||||||
|
const disks = Object.entries(i.devices).filter(([, d]) => d.type === 'disk').sort()
|
||||||
|
const add = async (e: FormEvent) => {
|
||||||
|
e.preventDefault()
|
||||||
|
try { const r = await api.virtMountAdd(i.name, { device: dev.trim(), source: src.trim(), path: dst.trim(), readonly: ro, shift }); toast(`mounted as ${r.device}`); setSrc(''); setDst(''); setDev(''); onChanged() } catch (er) { toast(errMsg(er), 'err') }
|
||||||
|
}
|
||||||
|
const remove = async (d: string, iso: boolean) => {
|
||||||
|
if (!(await askYes(iso ? `Eject ${d} from ${i.name}?` : `Remove the mount ${d} from ${i.name}? Nothing on the host is touched.`, iso ? 'Eject' : 'Remove'))) return
|
||||||
|
try { if (iso) await api.virtISODetach(i.name, d); else await api.virtMountRemove(i.name, d); toast('done'); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<table className="virt-table"><thead><tr><th>Device</th><th>Source</th><th>Inside</th><th /></tr></thead>
|
||||||
|
<tbody>{disks.map(([name, d]) => { const iso = (d.source ?? '').startsWith(isos); return (
|
||||||
|
<tr key={name}><td className="mono">{name}{iso && <span className="badge">ISO</span>}{d['boot.priority'] && <span className="badge">boots first</span>}</td>
|
||||||
|
<td className="mono small">{name === 'root' ? `pool ${d.pool}${d.size ? ` · ${d.size}` : ''}` : iso ? (d.source ?? '').slice(isos.length) : d.source}</td>
|
||||||
|
<td className="mono small">{iso ? 'CD-ROM' : d.path}{d.readonly === 'true' ? ' · read only' : ''}{d.shift === 'true' ? ' · shifted' : ''}</td>
|
||||||
|
<td className="num">{name !== 'root' && <Btn className="btn danger" onClick={() => remove(name, iso)}>{iso ? 'Eject' : 'Remove'}</Btn>}</td></tr>) })}</tbody></table>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">Mount a folder from this machine</span></div>
|
||||||
|
<form onSubmit={add}>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">Host path<input className="mono" value={src} onChange={(e) => setSrc(e.target.value)} placeholder="/tank/media" spellCheck={false} required /></label>
|
||||||
|
<label className="muted">Path inside<input className="mono" value={dst} onChange={(e) => setDst(e.target.value)} placeholder="/mnt/media" spellCheck={false} required /></label>
|
||||||
|
<label className="muted">Device name — optional<input className="mono" value={dev} onChange={(e) => setDev(e.target.value)} placeholder="mnt-media" spellCheck={false} /></label>
|
||||||
|
</div>
|
||||||
|
<div className="virt-form-row" style={{ alignItems: 'center' }}>
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}><input type="checkbox" checked={ro} onChange={(e) => setRo(e.target.checked)} style={{ width: 'auto' }} />read only</label>
|
||||||
|
{i.type === 'container' && <label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }} title="map the host's file ownership into the container's user range, so files look owned by the same names inside"><input type="checkbox" checked={shift} onChange={(e) => setShift(e.target.checked)} style={{ width: 'auto' }} />shift ownership</label>}
|
||||||
|
<span className="spacer" />
|
||||||
|
<button className="btn on" disabled={!src.trim() || !dst.trim()}>Mount</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
<div className="muted small">{i.type === 'container' ? 'A bind mount; it appears at once, running or not.' : 'Shared into the VM over virtiofs; its agent mounts it. Appears at once on a running VM with the agent, otherwise at the next start.'} The data stays where it is — this is the instance’s device, not a copy.</div>
|
||||||
|
</section>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function NewVMFromISO({ st, ov, iso, onClose, onStarted }: { st: VirtStatus; ov: VirtOverview; iso: string; onClose: () => void; onStarted: (t: TaskInfo) => void }) {
|
||||||
|
const isos = ov.library.isos
|
||||||
|
const [pick, setPick] = useState(iso || isos[0]?.name || '')
|
||||||
|
const [name, setName] = useState('')
|
||||||
|
const [cpu, setCpu] = useState('2')
|
||||||
|
const [mem, setMem] = useState('2GiB')
|
||||||
|
const [disk, setDisk] = useState('20GiB')
|
||||||
|
const [pool, setPool] = useState(ov.default_pool || ov.pools?.[0]?.name || '')
|
||||||
|
const nets = (ov.networks ?? []).filter((n) => n.managed || n.type === 'bridge')
|
||||||
|
const [net, setNet] = useState(nets.find((n) => n.name === 'incusbr0')?.name ?? nets[0]?.name ?? '')
|
||||||
|
const [sb, setSb] = useState(true)
|
||||||
|
const [err, setErr] = useState('')
|
||||||
|
const [busy, setBusy] = useState(false)
|
||||||
|
useEscape(onClose)
|
||||||
|
const submit = async (e: FormEvent) => {
|
||||||
|
e.preventDefault(); setErr(''); setBusy(true)
|
||||||
|
try { onStarted(await api.virtFromISO({ name: name.trim(), iso: pick, cpu: cpu.trim(), memory: mem.trim(), disk: disk.trim(), pool, network: net, secureboot: sb, start: true })) }
|
||||||
|
catch (e) { setErr(errMsg(e)); setBusy(false) }
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<div className="modal-back" onClick={onClose}>
|
||||||
|
<form className="modal" onClick={(e) => e.stopPropagation()} onSubmit={submit}>
|
||||||
|
<h3>New VM from an ISO</h3>
|
||||||
|
<label className="muted">ISO<select value={pick} onChange={(e) => setPick(e.target.value)}>{isos.map((f) => <option key={f.name} value={f.name}>{f.name} · {fmtBytes(f.size)}</option>)}</select></label>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">Name<input value={name} onChange={(e) => setName(e.target.value)} pattern="[a-zA-Z][a-zA-Z0-9\-]{0,62}" placeholder="win11" spellCheck={false} autoCapitalize="off" autoCorrect="off" required /></label>
|
||||||
|
<label className="muted">Network<select value={net} onChange={(e) => setNet(e.target.value)}>{nets.map((n) => <option key={n.name} value={n.name}>{n.name}{n.address ? ` · ${n.address}` : ''}</option>)}</select></label>
|
||||||
|
</div>
|
||||||
|
<div className="virt-form-row">
|
||||||
|
<label className="muted">CPUs<input className="mono" value={cpu} onChange={(e) => setCpu(e.target.value)} /></label>
|
||||||
|
<label className="muted">Memory<input className="mono" value={mem} onChange={(e) => setMem(e.target.value)} /></label>
|
||||||
|
<label className="muted">Disk<input className="mono" value={disk} onChange={(e) => setDisk(e.target.value)} /></label>
|
||||||
|
</div>
|
||||||
|
<PoolPick ov={ov} value={pool} onChange={setPool} />
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
|
||||||
|
<input type="checkbox" checked={sb} onChange={(e) => setSb(e.target.checked)} style={{ width: 'auto' }} />Secure Boot — Windows, Debian, Ubuntu and Fedora boot with it; Alpine, Arch, TrueNAS and most others need it off
|
||||||
|
</label>
|
||||||
|
<div className="muted small">An empty VM with the ISO as its first boot device, started, so the installer is on the VGA console. {fmtBytes(st.mem_avail)} free here; a VM’s memory is a reservation. Windows also wants the virtio drivers ISO attached as a second drive — add it from the VM’s Disks tab.</div>
|
||||||
|
{err && <div className="error">{err}</div>}
|
||||||
|
<div className="svc-actions">
|
||||||
|
<button type="submit" className="btn on" disabled={busy || !pick || !name.trim()}>{busy ? 'Starting…' : 'Create and boot the ISO'}</button>
|
||||||
|
<span className="spacer" />
|
||||||
|
<button type="button" className="btn" onClick={onClose}>Cancel</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function Library({ st, ov, onTask, onChanged, onNewVM }: { st: VirtStatus; ov: VirtOverview; onTask: (t: TaskInfo) => void; onChanged: () => void; onNewVM: (iso: string) => void }) {
|
||||||
|
const lib = ov.library
|
||||||
|
const vms = (ov.instances ?? []).filter((i) => i.type === 'virtual-machine')
|
||||||
|
const [url, setUrl] = useState(''); const [sha, setSha] = useState(''); const [path, setPath] = useState('')
|
||||||
|
const [upload, setUpload] = useState<{ name: string; pct: number } | null>(null)
|
||||||
|
const [exp, setExp] = useState<ExportsView | null>(null)
|
||||||
|
const [cfg, setCfg] = useState<ExportsView['config'] | null>(null)
|
||||||
|
const loadExports = useCallback(() => api.virtExports().then((v) => { setExp(v); setCfg(v.config) }).catch(() => setExp(null)), [])
|
||||||
|
useEffect(() => { loadExports() }, [loadExports])
|
||||||
|
const uploadFiles = async (files: FileList | null) => {
|
||||||
|
if (!files?.length) return
|
||||||
|
for (const f of Array.from(files)) {
|
||||||
|
if (!/\.(iso|img)$/i.test(f.name)) { toast(`${f.name}: only .iso or .img files`, 'err'); continue }
|
||||||
|
setUpload({ name: f.name, pct: 0 })
|
||||||
|
try { await api.fileUpload(st.dir + '/isos', f, false, (pct) => setUpload({ name: f.name, pct })); toast(`uploaded ${f.name}`) } catch (e) { toast(`${f.name}: ${errMsg(e)}`, 'err') }
|
||||||
|
}
|
||||||
|
setUpload(null); onChanged()
|
||||||
|
}
|
||||||
|
const fetchISO = async (e: FormEvent) => { e.preventDefault(); try { onTask(await api.virtISOFetch(url.trim(), sha.trim(), '')); setUrl(''); setSha('') } catch (er) { toast(errMsg(er), 'err') } }
|
||||||
|
const importISO = async (e: FormEvent) => { e.preventDefault(); try { onTask(await api.virtISOImport(path.trim())); setPath('') } catch (er) { toast(errMsg(er), 'err') } }
|
||||||
|
const attach = async (f: LibraryFile) => {
|
||||||
|
if (vms.length === 0) { toast('no VM to attach it to — make one first, or use New VM from ISO', 'info'); return }
|
||||||
|
const vm = vms.length === 1 ? vms[0].name : await askText(`Attach ${f.name} to which VM? (${vms.map((v) => v.name).join(', ')})`, vms[0].name, 'Attach')
|
||||||
|
if (!vm) return
|
||||||
|
const first = await askYes(`Boot ${vm} from ${f.name} first, ahead of its disk? Say no to just make it available as a CD-ROM.`, 'Boot first')
|
||||||
|
try { const r = await api.virtISOAttach(vm, f.name, first); toast(`${f.name} in ${vm} as ${r.device}`); onChanged() } catch (e) { toast(errMsg(e), 'err') }
|
||||||
|
}
|
||||||
|
const del = async (f: LibraryFile) => { if (!(await askYes(`Delete ${f.name} from the library?`, 'Delete'))) return; try { await api.virtISODelete(f.name); toast('deleted'); onChanged() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const rename = async (f: LibraryFile) => { const to = await askText(`Rename ${f.name} to`, f.name, 'Rename'); if (!to || to === f.name) return; try { await api.virtISORename(f.name, to); toast('renamed'); onChanged() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const saveExports = async () => { if (!cfg) return; try { await api.virtExportsPut(cfg); toast(cfg.enabled ? 'exports scheduled' : 'exports paused'); loadExports() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const delExport = async (inst: string, name: string) => { if (!(await askYes(`Delete the export ${name}?`, 'Delete'))) return; try { await api.virtExportDelete(inst, name); loadExports() } catch (e) { toast(errMsg(e), 'err') } }
|
||||||
|
const table = (files: LibraryFile[]) => files.length === 0 ? <div className="muted small">empty</div> : (
|
||||||
|
<table className="virt-table"><thead><tr><th>File</th><th className="num">Size</th><th>Changed</th></tr></thead>
|
||||||
|
<tbody>{files.map((f) => <tr key={f.name}><td className="mono">{f.name}</td><td className="num">{fmtBytes(f.size)}</td><td>{when(f.modified)}</td></tr>)}</tbody></table>
|
||||||
|
)
|
||||||
|
return (
|
||||||
|
<div className="svc-rows" style={{ padding: 12, display: 'flex', flexDirection: 'column', gap: 14 }}>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">ISOs</span><span className="spacer" /><span className="muted small mono">{st.dir}/isos</span></div>
|
||||||
|
{lib.isos.length === 0 && <div className="muted small">No ISOs yet. Upload one, fetch one by URL, or import a file already on this machine.</div>}
|
||||||
|
{lib.isos.length > 0 && (
|
||||||
|
<table className="virt-table"><thead><tr><th>File</th><th className="num">Size</th><th>In</th><th /></tr></thead>
|
||||||
|
<tbody>{lib.isos.map((f) => (
|
||||||
|
<tr key={f.name}><td className="mono">{f.name}</td><td className="num">{fmtBytes(f.size)}</td><td className="small">{f.attached_to?.join(' ') || '—'}</td>
|
||||||
|
<td className="num" style={{ whiteSpace: 'normal' }}>
|
||||||
|
{st.kvm && <Btn className="btn on" onClick={() => onNewVM(f.name)}>New VM</Btn>} {st.kvm && <Btn className="btn" onClick={() => attach(f)}>Attach…</Btn>} <Btn className="btn" onClick={() => rename(f)}>Rename</Btn> <Btn className="btn danger" disabled={!!f.attached_to?.length} title={f.attached_to?.length ? 'attached — eject it first' : ''} onClick={() => del(f)}>Delete</Btn>
|
||||||
|
</td></tr>
|
||||||
|
))}</tbody></table>
|
||||||
|
)}
|
||||||
|
<div className="virt-form-row" style={{ flexWrap: 'wrap', marginTop: 8 }}>
|
||||||
|
<label className="btn" style={{ alignSelf: 'flex-end' }}>{upload ? `${upload.name} · ${upload.pct}%` : 'Upload…'}<input type="file" accept=".iso,.img" multiple style={{ display: 'none' }} onChange={(e) => uploadFiles(e.target.files)} /></label>
|
||||||
|
<form onSubmit={fetchISO} className="virt-form-row" style={{ flex: '2 1 320px' }}>
|
||||||
|
<label className="muted" style={{ flex: 3 }}>Fetch from a URL<input className="mono" value={url} onChange={(e) => setUrl(e.target.value)} placeholder="https://…/something.iso" spellCheck={false} /></label>
|
||||||
|
<label className="muted" style={{ flex: 2 }}>sha256 — optional<input className="mono" value={sha} onChange={(e) => setSha(e.target.value)} spellCheck={false} /></label>
|
||||||
|
<button className="btn" style={{ alignSelf: 'flex-end' }} disabled={!url.trim()}>Fetch</button>
|
||||||
|
</form>
|
||||||
|
<form onSubmit={importISO} className="virt-form-row" style={{ flex: '1 1 260px' }}>
|
||||||
|
<label className="muted" style={{ flex: 1 }}>Import a file on this machine — copied in<input className="mono" value={path} onChange={(e) => setPath(e.target.value)} placeholder="/tank/software/isos/…/x.iso" spellCheck={false} /></label>
|
||||||
|
<button className="btn" style={{ alignSelf: 'flex-end' }} disabled={!path.trim()}>Import</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<div className="muted small">{st.kvm ? 'An ISO attaches to a VM as a CD-ROM, live or stopped; “New VM” makes an empty VM that boots it, for an installer on the VGA console.' : 'This machine has no KVM, so there is nothing here to boot an ISO.'}</div>
|
||||||
|
</section>
|
||||||
|
<section className="card">
|
||||||
|
<div className="sub-head"><span className="dname">Exports</span><span className="spacer" /><span className="muted small mono">{st.dir}/exports</span></div>
|
||||||
|
{cfg && (
|
||||||
|
<div className="virt-form-row" style={{ flexWrap: 'wrap', alignItems: 'flex-end' }}>
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8, flex: '0 0 auto' }}><input type="checkbox" checked={cfg.enabled} onChange={(e) => setCfg({ ...cfg, enabled: e.target.checked })} style={{ width: 'auto' }} />nightly</label>
|
||||||
|
<label className="muted" style={{ flex: '0 0 90px' }}>at<input className="mono" type="time" value={`${String(cfg.hour).padStart(2, '0')}:${String(cfg.minute).padStart(2, '0')}`} onChange={(e) => { const [h, m] = e.target.value.split(':').map(Number); if (!isNaN(h) && !isNaN(m)) setCfg({ ...cfg, hour: h, minute: m }) }} /></label>
|
||||||
|
<label className="muted" style={{ flex: '0 0 110px' }}>keep per instance<input className="mono" type="number" min={1} max={100} value={cfg.keep} onChange={(e) => setCfg({ ...cfg, keep: Number(e.target.value) || 1 })} /></label>
|
||||||
|
<label className="muted" style={{ flexDirection: 'row', alignItems: 'center', gap: 8, flex: '0 0 auto' }}><input type="checkbox" checked={!cfg.instance_only} onChange={(e) => setCfg({ ...cfg, instance_only: !e.target.checked })} style={{ width: 'auto' }} />include snapshots</label>
|
||||||
|
<span className="spacer" />
|
||||||
|
<Btn className="btn on" disabled={JSON.stringify(cfg) === JSON.stringify(exp?.config)} onClick={saveExports}>Save</Btn>
|
||||||
|
<Btn className="btn" onClick={async () => { try { onTask(await api.virtExportsRun()) } catch (e) { toast(errMsg(e), 'err') } }}>Export all now</Btn>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{exp && <div className="muted small" style={{ marginTop: 6 }}>{exp.config.enabled ? <>Cron line in <span className="mono">{exp.cron_file}</span>, next {exp.next ? new Date(exp.next).toLocaleString() : '—'}.</> : 'Not scheduled.'} {exp.last ? <>Last run {when(exp.last.at)}: {exp.last.exported.length} exported, {exp.last.pruned.length} pruned{exp.last.errors?.length ? <span className="warn-text">, {exp.last.errors.length} failed: {exp.last.errors.join('; ')}</span> : ''}.</> : 'Never run.'} Every instance as a tarball Incus can import again; the folder is backed up with the rest of Ham Lab’s data.</div>}
|
||||||
|
{exp && exp.files.length > 0 && (
|
||||||
|
<table className="virt-table" style={{ marginTop: 8 }}><thead><tr><th>Instance</th><th>Export</th><th className="num">Size</th><th>Taken</th><th /></tr></thead>
|
||||||
|
<tbody>{exp.files.map((f) => <tr key={f.instance + f.name}><td>{f.instance}</td><td className="mono small">{f.name}</td><td className="num">{fmtBytes(f.size)}</td><td>{when(f.modified)}</td><td className="num"><Btn className="btn danger" onClick={() => delExport(f.instance, f.name)}>Delete</Btn></td></tr>)}</tbody></table>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
<section className="card"><div className="sub-head"><span className="dname">Images</span><span className="spacer" /><span className="muted small mono">{st.dir}/images</span></div>{table(lib.images)}
|
||||||
|
<div className="muted small">Every image downloaded, imported or published lands here as a file, so the library is whole without Incus and can be reloaded into a fresh install.</div></section>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
+5
-2
@@ -97,7 +97,10 @@ function DatasetRow({ d, onChanged }: { d: ZFSInfo['datasets'][number]; onChange
|
|||||||
if (!open && !snaps) await reload()
|
if (!open && !snaps) await reload()
|
||||||
}
|
}
|
||||||
async function snapshot() {
|
async function snapshot() {
|
||||||
const def = new Date().toISOString().slice(0, 19).replace('T', '_').replace(/:/g, '.')
|
// zfSnap's name is the machine's local time, and snapMeta reads it back as local time — so the
|
||||||
|
// default is built from local fields, not toISOString(), which is UTC and read seven hours wrong.
|
||||||
|
const n = new Date(), two = (x: number) => String(x).padStart(2, '0')
|
||||||
|
const def = `${n.getFullYear()}-${two(n.getMonth() + 1)}-${two(n.getDate())}_${two(n.getHours())}.${two(n.getMinutes())}.${two(n.getSeconds())}`
|
||||||
const name = await askText(`Snapshot name for ${d.name}@`, def)
|
const name = await askText(`Snapshot name for ${d.name}@`, def)
|
||||||
if (!name) return
|
if (!name) return
|
||||||
try { await api.zfsSnapshot(d.name, name.trim()); toast(`${d.name}@${name.trim()} created`); await reload(); onChanged() }
|
try { await api.zfsSnapshot(d.name, name.trim()); toast(`${d.name}@${name.trim()} created`); await reload(); onChanged() }
|
||||||
@@ -299,7 +302,7 @@ function adopt(x: ExternalJob, fallbackDataset: string): Partial<ZFSPolicy> {
|
|||||||
const ds = /\b(tank[\w/-]*)\b/.exec(cmd)?.[1] ?? fallbackDataset
|
const ds = /\b(tank[\w/-]*)\b/.exec(cmd)?.[1] ?? fallbackDataset
|
||||||
return {
|
return {
|
||||||
...BLANK, name: `${x.tool} (adopted)`, dataset: ds,
|
...BLANK, name: `${x.tool} (adopted)`, dataset: ds,
|
||||||
recursive: /\s-\w*r/.test(cmd) || true,
|
recursive: /\s-[a-zA-Z]*r\b/.test(cmd), // -r, or -r folded into another flag group
|
||||||
every: simple ? 'daily' : 'custom',
|
every: simple ? 'daily' : 'custom',
|
||||||
at: simple ? `${String(hour).padStart(2, '0')}:${String(min).padStart(2, '0')}` : '02:30',
|
at: simple ? `${String(hour).padStart(2, '0')}:${String(min).padStart(2, '0')}` : '02:30',
|
||||||
cron: simple ? '' : x.schedule.trim(),
|
cron: simple ? '' : x.schedule.trim(),
|
||||||
|
|||||||
+73
-4
@@ -85,7 +85,7 @@ export interface GPU {
|
|||||||
mhz?: number; req_mhz?: number; max_mhz?: number; mem_used?: number; mem_total?: number
|
mhz?: number; req_mhz?: number; max_mhz?: number; mem_used?: number; mem_total?: number
|
||||||
clients: GPUClient[]; clients_ok: boolean; stalled?: boolean
|
clients: GPUClient[]; clients_ok: boolean; stalled?: boolean
|
||||||
}
|
}
|
||||||
export interface ProcTable { t: number; cpu: number; cores: number[]; cpu_hist: number[]; cpu_model?: string; cpu_mhz?: number; cpu_max_mhz?: number; cpu_temp?: number; core_ids?: number[]; load: [number, number, number]; mem: MemInfo; swap: MemInfo; net: NetIf[]; gpus?: GPU[]; uptime: number; procs: Proc[]; total: number; running: number }
|
export interface ProcTable { t: number; cpu: number; cores: number[]; cpu_hist: number[]; cpu_model?: string; cpu_mhz?: number; cpu_max_mhz?: number; cpu_power_w?: number; cpu_temp?: number; core_ids?: number[]; load: [number, number, number]; mem: MemInfo; swap: MemInfo; net: NetIf[]; gpus?: GPU[]; uptime: number; procs: Proc[]; total: number; running: number }
|
||||||
export interface Sample {
|
export interface Sample {
|
||||||
t: number; interval_ms: number; cpu: number; cores: number[]; load: [number, number, number]
|
t: number; interval_ms: number; cpu: number; cores: number[]; load: [number, number, number]
|
||||||
mem: { total: number; used: number; avail: number }; swap: { total: number; used: number; avail: number }
|
mem: { total: number; used: number; avail: number }; swap: { total: number; used: number; avail: number }
|
||||||
@@ -154,8 +154,8 @@ export interface Hardware { at: string; system: HWSystem; board: HWBoard; cpu: H
|
|||||||
|
|
||||||
export interface DockerLogLine { t: string; m: string; err?: boolean }
|
export interface DockerLogLine { t: string; m: string; err?: boolean }
|
||||||
export interface DockerInfo { available: boolean; message?: string; tool?: string; containers: DockerContainer[]; images: Record<string, unknown>[]; volumes: Record<string, unknown>[]; networks: Record<string, unknown>[] }
|
export interface DockerInfo { available: boolean; message?: string; tool?: string; containers: DockerContainer[]; images: Record<string, unknown>[]; volumes: Record<string, unknown>[]; networks: Record<string, unknown>[] }
|
||||||
export interface Stack { name: string; containers: string[]; running: number; file: string; host_file: string; files: string[]; host_files: string[] | null; images: string[] | null; via: string; remote: string; held_by: string; editable: boolean; note?: string; has_env: boolean }
|
export interface Stack { name: string; containers: string[]; running: number; file: string; host_file: string; files: string[]; host_files: string[] | null; images: string[] | null; via: string; editable: boolean; note?: string; has_env: boolean }
|
||||||
export interface StackHome { dir: string; inner: string; via: string; remote: string; held_by: string; stacks: number; default: boolean }
|
export interface StackHome { dir: string; inner: string; via: string; stacks: number; default: boolean }
|
||||||
export interface ImageCheck { image: string; state: 'current' | 'update' | 'unknown'; detail?: string }
|
export interface ImageCheck { image: string; state: 'current' | 'update' | 'unknown'; detail?: string }
|
||||||
export interface StackDiff { name: string; base: '' | 'deployed' | 'saved'; old: string; new: string }
|
export interface StackDiff { name: string; base: '' | 'deployed' | 'saved'; old: string; new: string }
|
||||||
export interface AlertRule { id: string; name: string; sensor: string; op: string; value: number | boolean | string; for_sec: number; severity: 'info' | 'warn' | 'crit'; enabled: boolean; machines: string }
|
export interface AlertRule { id: string; name: string; sensor: string; op: string; value: number | boolean | string; for_sec: number; severity: 'info' | 'warn' | 'crit'; enabled: boolean; machines: string }
|
||||||
@@ -249,11 +249,36 @@ export interface ARCDetail {
|
|||||||
}
|
}
|
||||||
export interface ARCView { detail: ARCDetail; live: ARCLive | null }
|
export interface ARCView { detail: ARCDetail; live: ARCLive | null }
|
||||||
|
|
||||||
export interface FileEntry { name: string; type: 'dir' | 'file' | 'link' | 'other'; size: number; mode: string; perm: string; mtime: number; user: string; group: string; target?: string; broken?: boolean }
|
export interface FileEntry { name: string; type: 'dir' | 'file' | 'link' | 'other'; size: number; mode: string; perm: string; mtime: number; user: string; group: string; target?: string; broken?: boolean; preview?: 'image' | 'video' | 'audio' }
|
||||||
|
export interface PlayCaps { codecs: { id: string; label: string; engine?: string }[]; qualities: { id: string; label: string; height: number }[]; error?: string }
|
||||||
|
export interface PlayTrack { codec: string; profile?: string; pix_fmt?: string; width?: number; height?: number; fps?: number; channels?: number }
|
||||||
|
export interface PlayStream { id: string; codec: string; quality: string; engine: string; width: number; height: number; kbps: number; source: { container: string; duration: number; bit_rate?: number; video?: PlayTrack; audio?: PlayTrack } }
|
||||||
|
export interface PlayStatus { running: boolean; done: boolean; error?: string; progress: number; speed: number; segments: number }
|
||||||
|
export interface ThumbGPU { api: string; device: string; name?: string; codecs?: string[]; reason?: string; permission?: boolean }
|
||||||
|
// How previews get made on the machine: ok with an engine, or why not and what would fix it.
|
||||||
|
export interface ThumbStatus { ok: boolean; engine: 'gpu' | 'cpu' | ''; summary: string; ffmpeg?: string; gpus: ThumbGPU[]; unusable?: ThumbGPU[]; error?: string; install?: { pm: string; package: string }; gpu_fix?: boolean; user: string; cache: { path: string; entries: number; bytes: number } }
|
||||||
export interface FileInfo extends FileEntry { path: string; text: boolean }
|
export interface FileInfo extends FileEntry { path: string; text: boolean }
|
||||||
|
|
||||||
const q = (path: string, sudo: boolean, extra = '') => `path=${encodeURIComponent(path)}${sudo ? '&sudo=1' : ''}${extra}`
|
const q = (path: string, sudo: boolean, extra = '') => `path=${encodeURIComponent(path)}${sudo ? '&sudo=1' : ''}${extra}`
|
||||||
|
|
||||||
|
// ---- Virtualization: Incus, per machine ----
|
||||||
|
export interface VirtLayout { driver: string; pool_source: string; pool_size_gib?: number; bridge: string; subnet: string; docker_rules: boolean; arc_max_gib?: number; attempted: string; hamlab_version?: string }
|
||||||
|
export interface VirtStatus { state: 'ready' | 'repair' | 'installable' | 'unavailable'; reason?: string; kvm: boolean; arch: string; version?: string; pm?: string; drivers: string[]; docker: boolean; zfs_host: boolean; arc_max_gib?: number; mem_total: number; mem_avail: number; disk_free: number; dir: string; subnet: string; pool_size_gib?: number; layout?: VirtLayout; user: string; root: boolean; unit?: string; btrfs_tools: boolean }
|
||||||
|
export interface VirtSnapshot { name: string; created: string; expires?: string; size?: number }
|
||||||
|
export interface VirtInstance { name: string; type: 'container' | 'virtual-machine'; status: string; arch: string; description?: string; image: string; os?: string; release?: string; cpu?: string; memory?: string; created: string; last_used: string; ipv4: string[]; snapshots: VirtSnapshot[]; profiles: string[]; ephemeral: boolean; cpu_pct: number; mem_usage: number; mem_peak: number; disk_usage: number; disk_total: number; pid: number; processes: number; config: Record<string, string>; devices: Record<string, Record<string, string>>; schedule?: string; expiry?: string }
|
||||||
|
export interface VirtImage { fingerprint: string; aliases: string[]; arch: string; type: string; size: number; created: string; last_used: string; description: string; os?: string; release?: string; variant?: string; cached: boolean; source?: string; public: boolean }
|
||||||
|
export interface VirtPool { name: string; driver: string; source: string; where: string; used: number; total: number; used_by: number }
|
||||||
|
export interface VirtNetwork { name: string; type: string; managed: boolean; address?: string; nat: boolean; used_by: number }
|
||||||
|
export interface LibraryFile { name: string; size: number; modified: string; attached_to?: string[] }
|
||||||
|
export interface ExportConfig { enabled: boolean; hour: number; minute: number; keep: number; instance_only: boolean; instances?: string[]; updated?: string }
|
||||||
|
export interface ExportRun { at: string; exported: string[]; pruned: string[]; errors?: string[]; seconds: number }
|
||||||
|
export interface ExportFile { instance: string; name: string; size: number; modified: string }
|
||||||
|
export interface ExportsView { config: ExportConfig; cron: string; next?: string; last: ExportRun | null; files: ExportFile[]; dir: string; cron_file: string }
|
||||||
|
export interface FromISO { name: string; iso: string; cpu: string; memory: string; disk: string; pool: string; network: string; secureboot: boolean; start: boolean }
|
||||||
|
export interface VirtOverview { status: VirtStatus; error?: string; instances?: VirtInstance[]; images?: VirtImage[]; pools?: VirtPool[]; default_pool?: string; networks?: VirtNetwork[]; library: { images: LibraryFile[]; isos: LibraryFile[] } }
|
||||||
|
export interface VirtInstallOpts { driver: string; pool_size_gib: number; subnet: string; docker_rules: boolean; arc_max_gib: number }
|
||||||
|
export interface VirtCreate { name: string; image: string; vm: boolean; cpu: string; memory: string; disk: string; pool: string; network: string; cloud_init: string; profiles: string[]; description: string; start: boolean }
|
||||||
|
|
||||||
export const api = {
|
export const api = {
|
||||||
procs: () => req<ProcTable>('GET', '/api/procs'),
|
procs: () => req<ProcTable>('GET', '/api/procs'),
|
||||||
space: () => req<{ targets: SpaceTarget[] }>('GET', '/api/space'),
|
space: () => req<{ targets: SpaceTarget[] }>('GET', '/api/space'),
|
||||||
@@ -274,6 +299,16 @@ export const api = {
|
|||||||
},
|
},
|
||||||
fileReadURL: (path: string, sudo: boolean) => apiPath(`/api/files/read?${q(path, sudo)}`),
|
fileReadURL: (path: string, sudo: boolean) => apiPath(`/api/files/read?${q(path, sudo)}`),
|
||||||
fileDownloadURL: (path: string, sudo: boolean) => apiPath(`/api/files/download?${q(path, sudo)}`),
|
fileDownloadURL: (path: string, sudo: boolean) => apiPath(`/api/files/download?${q(path, sudo)}`),
|
||||||
|
// v carries the file's mtime and size, so a changed file is a new URL and an unchanged one a cached one
|
||||||
|
fileThumbURL: (path: string, sudo: boolean, mtime: number, size: number) => apiPath(`/api/files/thumb?${q(path, sudo, `&v=${mtime}-${size}`)}`),
|
||||||
|
thumbStatus: (sudo: boolean, refresh = false) => req<ThumbStatus>('GET', `/api/files/thumbs?${sudo ? 'sudo=1&' : ''}${refresh ? 'refresh=1' : ''}`),
|
||||||
|
thumbInstall: () => req<TaskInfo>('POST', '/api/files/thumbs/install'),
|
||||||
|
thumbGPUAccess: () => req<TaskInfo>('POST', '/api/files/thumbs/gpu-access'),
|
||||||
|
// playback of what the browser can't play as it is: an HLS re-encode on the machine
|
||||||
|
playCaps: (sudo: boolean) => req<PlayCaps>('GET', `/api/files/play-caps${sudo ? '?sudo=1' : ''}`),
|
||||||
|
playStart: (path: string, codec: string, quality: string, sudo: boolean) => req<{ stream: PlayStream; url: string }>('POST', `/api/files/play${sudo ? '?sudo=1' : ''}`, { path, codec, quality }),
|
||||||
|
playStatus: (id: string) => req<PlayStatus>('GET', `/api/files/play/${id}`),
|
||||||
|
playStop: (id: string) => req<void>('DELETE', `/api/files/play/${id}`),
|
||||||
fileWrite: async (path: string, text: string, sudo: boolean) => {
|
fileWrite: async (path: string, text: string, sudo: boolean) => {
|
||||||
const r = await fetch(apiPath(`/api/files/write?${q(path, sudo)}`), { method: 'PUT', credentials: 'same-origin', body: text, headers: { 'Content-Type': 'application/octet-stream' } })
|
const r = await fetch(apiPath(`/api/files/write?${q(path, sudo)}`), { method: 'PUT', credentials: 'same-origin', body: text, headers: { 'Content-Type': 'application/octet-stream' } })
|
||||||
if (r.status === 401) throw new Unauthenticated()
|
if (r.status === 401) throw new Unauthenticated()
|
||||||
@@ -290,6 +325,8 @@ export const api = {
|
|||||||
fileMkdir: (path: string, sudo: boolean) => req<void>('POST', `/api/files/mkdir${sudo ? '?sudo=1' : ''}`, { path }),
|
fileMkdir: (path: string, sudo: boolean) => req<void>('POST', `/api/files/mkdir${sudo ? '?sudo=1' : ''}`, { path }),
|
||||||
fileTouch: (path: string, sudo: boolean) => req<void>('POST', `/api/files/touch${sudo ? '?sudo=1' : ''}`, { path }),
|
fileTouch: (path: string, sudo: boolean) => req<void>('POST', `/api/files/touch${sudo ? '?sudo=1' : ''}`, { path }),
|
||||||
fileRename: (path: string, to: string, sudo: boolean) => req<void>('POST', `/api/files/rename${sudo ? '?sudo=1' : ''}`, { path, to }),
|
fileRename: (path: string, to: string, sudo: boolean) => req<void>('POST', `/api/files/rename${sudo ? '?sudo=1' : ''}`, { path, to }),
|
||||||
|
// a rename when the ends share a filesystem (nothing back), otherwise the copy-and-remove runs as a task (the task back)
|
||||||
|
fileMove: (path: string, to: string, sudo: boolean) => req<TaskInfo | undefined>('POST', `/api/files/move${sudo ? '?sudo=1' : ''}`, { path, to }),
|
||||||
fileDelete: (path: string, recursive: boolean, sudo: boolean) => req<void>('POST', `/api/files/delete${sudo ? '?sudo=1' : ''}`, { path, recursive }),
|
fileDelete: (path: string, recursive: boolean, sudo: boolean) => req<void>('POST', `/api/files/delete${sudo ? '?sudo=1' : ''}`, { path, recursive }),
|
||||||
fileChmod: (path: string, mode: string, sudo: boolean) => req<void>('POST', `/api/files/chmod${sudo ? '?sudo=1' : ''}`, { path, mode }),
|
fileChmod: (path: string, mode: string, sudo: boolean) => req<void>('POST', `/api/files/chmod${sudo ? '?sudo=1' : ''}`, { path, mode }),
|
||||||
storage: () => req<{ devices: StorageDevice[]; zfs: ZFSInfo; smartctl: boolean }>('GET', '/api/storage'),
|
storage: () => req<{ devices: StorageDevice[]; zfs: ZFSInfo; smartctl: boolean }>('GET', '/api/storage'),
|
||||||
@@ -344,6 +381,7 @@ export const api = {
|
|||||||
sessions: () => req<Session[]>('GET', '/api/sessions', undefined, true),
|
sessions: () => req<Session[]>('GET', '/api/sessions', undefined, true),
|
||||||
revokeSession: (id: string) => req<void>('DELETE', `/api/sessions/${encodeURIComponent(id)}`, undefined, true),
|
revokeSession: (id: string) => req<void>('DELETE', `/api/sessions/${encodeURIComponent(id)}`, undefined, true),
|
||||||
revokeOtherSessions: () => req<{ revoked: number }>('POST', '/api/sessions/revoke-others', undefined, true),
|
revokeOtherSessions: () => req<{ revoked: number }>('POST', '/api/sessions/revoke-others', undefined, true),
|
||||||
|
revokeIdleSessions: (hours = 24) => req<{ revoked: number }>('POST', `/api/sessions/revoke-idle?hours=${hours}`, undefined, true),
|
||||||
logout: () => req<void>('POST', '/api/logout', undefined, true),
|
logout: () => req<void>('POST', '/api/logout', undefined, true),
|
||||||
meHere: () => req<Me>('GET', '/api/me', undefined, true),
|
meHere: () => req<Me>('GET', '/api/me', undefined, true),
|
||||||
me: () => req<Me>('GET', '/api/me'),
|
me: () => req<Me>('GET', '/api/me'),
|
||||||
@@ -408,6 +446,37 @@ export const api = {
|
|||||||
createTerminal: (name?: string) => req<{ id: string; name: string }>('POST', '/api/terminals', { name }),
|
createTerminal: (name?: string) => req<{ id: string; name: string }>('POST', '/api/terminals', { name }),
|
||||||
renameTerminal: (id: string, name: string) => req<void>('PATCH', `/api/terminals/${id}`, { name }),
|
renameTerminal: (id: string, name: string) => req<void>('PATCH', `/api/terminals/${id}`, { name }),
|
||||||
deleteTerminal: (id: string) => req<void>('DELETE', `/api/terminals/${id}`),
|
deleteTerminal: (id: string) => req<void>('DELETE', `/api/terminals/${id}`),
|
||||||
|
virt: () => req<VirtOverview>('GET', '/api/virt'),
|
||||||
|
virtInstall: (o: VirtInstallOpts) => req<TaskInfo>('POST', '/api/virt/install', o),
|
||||||
|
virtRemote: (type: string, refresh = false) => req<{ images: VirtImage[] }>('GET', `/api/virt/images/remote?type=${type}${refresh ? '&refresh=1' : ''}`),
|
||||||
|
virtImageDownload: (alias: string) => req<TaskInfo>('POST', '/api/virt/images/download', { alias }),
|
||||||
|
virtImageImport: (path: string, alias: string, type: string) => req<TaskInfo>('POST', '/api/virt/images/import', { path, alias, type }),
|
||||||
|
virtImagePublish: (instance: string, alias: string) => req<TaskInfo>('POST', '/api/virt/images/publish', { instance, alias }),
|
||||||
|
virtImageDelete: (ref: string) => req<void>('POST', '/api/virt/images/delete', { ref }),
|
||||||
|
virtCreate: (c: VirtCreate) => req<TaskInfo>('POST', '/api/virt/instances', c),
|
||||||
|
virtAction: (name: string, action: 'start' | 'stop' | 'restart' | 'kill') => req<void>('POST', `/api/virt/instances/${encodeURIComponent(name)}/${action}`),
|
||||||
|
virtDelete: (name: string, force: boolean) => req<void>('POST', `/api/virt/instances/${encodeURIComponent(name)}/delete`, { confirm: name, force }),
|
||||||
|
virtTerminal: (name: string, kind: 'console' | 'exec') => req<{ id: string; name: string }>('POST', `/api/virt/instances/${encodeURIComponent(name)}/${kind}`),
|
||||||
|
virtRename: (name: string, to: string) => req<void>('POST', `/api/virt/instances/${encodeURIComponent(name)}/rename`, { name: to }),
|
||||||
|
virtConfig: (name: string, cfg: Record<string, string>) => req<void>('PUT', `/api/virt/instances/${encodeURIComponent(name)}/config`, cfg),
|
||||||
|
virtSnapshot: (name: string, snap: string) => req<{ name: string }>('POST', `/api/virt/instances/${encodeURIComponent(name)}/snapshots`, { name: snap }),
|
||||||
|
virtSnapshotAction: (name: string, snap: string, action: 'restore' | 'delete') => req<void>('POST', `/api/virt/instances/${encodeURIComponent(name)}/snapshots/${encodeURIComponent(snap)}/${action}`),
|
||||||
|
virtISOFetch: (url: string, sha256: string, name: string) => req<TaskInfo>('POST', '/api/virt/isos/fetch', { url, sha256, name }),
|
||||||
|
virtISOImport: (path: string) => req<TaskInfo>('POST', '/api/virt/isos/import', { path }),
|
||||||
|
virtISODelete: (name: string) => req<void>('POST', '/api/virt/isos/delete', { name }),
|
||||||
|
virtISORename: (name: string, to: string) => req<void>('POST', '/api/virt/isos/rename', { name, to }),
|
||||||
|
virtISOAttach: (inst: string, iso: string, bootFirst: boolean) => req<{ device: string }>('POST', `/api/virt/instances/${encodeURIComponent(inst)}/iso`, { iso, boot_first: bootFirst }),
|
||||||
|
virtISODetach: (inst: string, device: string) => req<void>('POST', `/api/virt/instances/${encodeURIComponent(inst)}/iso/detach`, { device }),
|
||||||
|
virtFromISO: (f: FromISO) => req<TaskInfo>('POST', '/api/virt/instances/from-iso', f),
|
||||||
|
virtMountAdd: (inst: string, m: { device: string; source: string; path: string; readonly: boolean; shift: boolean }) => req<{ device: string }>('POST', `/api/virt/instances/${encodeURIComponent(inst)}/mounts`, m),
|
||||||
|
virtMountRemove: (inst: string, device: string) => req<void>('POST', `/api/virt/instances/${encodeURIComponent(inst)}/mounts/remove`, { device }),
|
||||||
|
virtExports: () => req<ExportsView>('GET', '/api/virt/exports'),
|
||||||
|
virtExportsPut: (c: ExportConfig) => req<void>('PUT', '/api/virt/exports', c),
|
||||||
|
virtExportsRun: (instance = '') => req<TaskInfo>('POST', '/api/virt/exports/run', { instance }),
|
||||||
|
virtExportDelete: (instance: string, name: string) => req<void>('POST', '/api/virt/exports/delete', { instance, name }),
|
||||||
|
virtSSHKeys: () => req<{ keys: string[]; user: string }>('GET', '/api/virt/sshkeys'),
|
||||||
|
virtPoolCreate: (name: string, path: string) => req<{ name: string; path: string }>('POST', '/api/virt/pools', { name, path }),
|
||||||
|
virtPoolDelete: (name: string) => req<void>('POST', '/api/virt/pools/delete', { name }),
|
||||||
}
|
}
|
||||||
|
|
||||||
export function wsURLPath(path: string): string {
|
export function wsURLPath(path: string): string {
|
||||||
|
|||||||
+94
-2
@@ -27,6 +27,7 @@ button { font: inherit; color: inherit; background: none; border: 0; cursor: poi
|
|||||||
.login button.pweye:hover, .login button.pweye[aria-pressed="true"] { color: var(--fg); }
|
.login button.pweye:hover, .login button.pweye[aria-pressed="true"] { color: var(--fg); }
|
||||||
.login button.pweye:disabled { opacity: .4; }
|
.login button.pweye:disabled { opacity: .4; }
|
||||||
.error { color: var(--bad); font-size: 13px; }
|
.error { color: var(--bad); font-size: 13px; }
|
||||||
|
.svc-list > .error, .cron-list > .error { margin: 8px 12px; }
|
||||||
|
|
||||||
/* layout */
|
/* layout */
|
||||||
.app { height: 100%; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
|
.app { height: 100%; display: flex; flex-direction: column; padding-top: env(safe-area-inset-top); }
|
||||||
@@ -204,6 +205,8 @@ button { font: inherit; color: inherit; background: none; border: 0; cursor: poi
|
|||||||
.log-lines { flex: 1; overflow: auto; padding: 6px 8px; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
.log-lines { flex: 1; overflow: auto; padding: 6px 8px; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; }
|
||||||
.log { display: flex; gap: 8px; white-space: pre-wrap; word-break: break-word; }
|
.log { display: flex; gap: 8px; white-space: pre-wrap; word-break: break-word; }
|
||||||
.log .lt { color: var(--muted); flex-shrink: 0; } .log .li { color: var(--accent2); flex-shrink: 0; }
|
.log .lt { color: var(--muted); flex-shrink: 0; } .log .li { color: var(--accent2); flex-shrink: 0; }
|
||||||
|
/* a phone has no room for three columns: the stamp and the unit go on one line, the message underneath */
|
||||||
|
@media (max-width: 560px) { .log { flex-wrap: wrap; gap: 0 8px; padding: 2px 0; border-bottom: 1px solid var(--border); } .log .lm { flex-basis: 100%; } .log .lt { font-size: 11px; } }
|
||||||
.log.err .lm { color: var(--bad); } .log.warn .lm { color: var(--s2); } .log.debug .lm { color: var(--muted); }
|
.log.err .lm { color: var(--bad); } .log.warn .lm { color: var(--s2); } .log.debug .lm { color: var(--muted); }
|
||||||
@media (max-width: 800px) {
|
@media (max-width: 800px) {
|
||||||
.svc-list { width: 100%; border-right: 0; } .has-sel .svc-list { display: none; } .svc-empty { display: none; }
|
.svc-list { width: 100%; border-right: 0; } .has-sel .svc-list { display: none; } .svc-empty { display: none; }
|
||||||
@@ -315,6 +318,7 @@ h2.section { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted);
|
|||||||
.ds-row { display: grid; grid-template-columns: minmax(160px, 1fr) 80px 90px 90px 90px minmax(120px, 1fr); gap: 8px; align-items: center; padding: 6px 12px; border-bottom: 1px solid var(--border); font-size: 13px; min-width: 700px; }
|
.ds-row { display: grid; grid-template-columns: minmax(160px, 1fr) 80px 90px 90px 90px minmax(120px, 1fr); gap: 8px; align-items: center; padding: 6px 12px; border-bottom: 1px solid var(--border); font-size: 13px; min-width: 700px; }
|
||||||
.ds-row.head { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
|
.ds-row.head { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
|
||||||
.ds-row .num { text-align: right; }
|
.ds-row .num { text-align: right; }
|
||||||
|
@media (max-width: 800px) { .ds-row { grid-template-columns: minmax(90px, 1fr) 84px 100px; min-width: 0; gap: 6px; padding: 6px 8px; } .ds-row > :nth-child(3), .ds-row > :nth-child(5), .ds-row > :nth-child(6) { display: none; } .ds-row .ds-name, .ds-row.head > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } .ds-row.head { letter-spacing: 0; } }
|
||||||
.ds-name { cursor: pointer; display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
.ds-name { cursor: pointer; display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
||||||
.ds-mount { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; font-size: 12px; }
|
.ds-mount { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, monospace; font-size: 12px; }
|
||||||
.ds-snaps { padding: 8px 12px 10px 40px; border-bottom: 1px solid var(--border); background: var(--bg); display: flex; flex-direction: column; gap: 6px; }
|
.ds-snaps { padding: 8px 12px 10px 40px; border-bottom: 1px solid var(--border); background: var(--bg); display: flex; flex-direction: column; gap: 6px; }
|
||||||
@@ -360,8 +364,44 @@ h2.section { margin: 0; font-size: 13px; font-weight: 600; color: var(--muted);
|
|||||||
.file-editor { flex: 1; min-height: 0; border: 0; border-radius: 0; resize: none; font-size: 13px; }
|
.file-editor { flex: 1; min-height: 0; border: 0; border-radius: 0; resize: none; font-size: 13px; }
|
||||||
.preview { flex: 1; display: flex; align-items: center; justify-content: center; overflow: auto; padding: 12px; }
|
.preview { flex: 1; display: flex; align-items: center; justify-content: center; overflow: auto; padding: 12px; }
|
||||||
.preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
|
.preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
|
||||||
|
.preview video { max-width: 100%; max-height: 100%; background: #000; border-radius: 4px; outline: none; }
|
||||||
|
.player { flex: 1; min-height: 0; display: flex; flex-direction: column; }
|
||||||
|
.player .preview { flex: 1; min-height: 0; }
|
||||||
|
.play-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 6px 12px; font-size: 12px; color: var(--muted); border-bottom: 1px solid var(--border); }
|
||||||
|
.play-bar select { padding: 4px 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 5px; color: var(--fg); font-size: 12px; }
|
||||||
|
.play-pick { margin: 12px; }
|
||||||
|
.play-pick .row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
|
||||||
|
.play-pick label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
|
||||||
|
.preview.audio { align-items: flex-start; }
|
||||||
|
.audio-box { display: flex; flex-direction: column; align-items: center; gap: 10px; width: min(560px, 100%); margin-top: 8vh; }
|
||||||
|
.audio-box .ficon { font-size: 48px; }
|
||||||
|
.audio-box audio { width: 100%; }
|
||||||
|
/* previews: the icon column becomes a thumbnail box for images and videos */
|
||||||
|
.file-list.thumbs .file-row { grid-template-columns: 72px 1fr 80px 80px 130px 32px; padding-top: 4px; padding-bottom: 4px; min-height: 52px; }
|
||||||
|
.fthumb { position: relative; width: 72px; height: 44px; display: flex; align-items: center; justify-content: center; background: var(--bg3); border-radius: 4px; overflow: hidden; }
|
||||||
|
.fthumb img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; }
|
||||||
|
.fthumb.video::after, .fthumb.audio::after { content: '▶'; position: absolute; right: 2px; bottom: 2px; font-size: 9px; line-height: 1; padding: 2px 3px; border-radius: 3px; background: rgba(0,0,0,.6); color: #fff; }
|
||||||
|
.fthumb.audio::after { content: '♪'; }
|
||||||
|
.fthumb .ficon { font-size: 18px; }
|
||||||
|
.thumb-note { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 10px 0; }
|
||||||
|
/* move: the folder picker */
|
||||||
|
.modal.mover { width: min(560px, 100%); height: min(76vh, 100%); overflow: hidden; }
|
||||||
|
.mover-nav { display: flex; align-items: center; gap: 2px; }
|
||||||
|
.mover-nav .icon-btn { font-size: 18px; min-width: 30px; line-height: 1; }
|
||||||
|
.mover-nav .icon-btn:disabled { opacity: .3; }
|
||||||
|
.mover-nav .crumbs { flex: 1; min-width: 0; margin-left: 4px; }
|
||||||
|
.modal.mover > .mover-list { flex: 1 1 auto; min-height: 80px; overflow-y: auto; margin: 0 -16px; border-top: 1px solid var(--border); }
|
||||||
|
.mover-row { display: flex; align-items: center; gap: 8px; padding: 9px 16px; cursor: pointer; border-bottom: 1px solid var(--border); min-height: 38px; }
|
||||||
|
.mover-row:hover { background: var(--bg3); }
|
||||||
|
.mover-row.self { opacity: .5; cursor: default; }
|
||||||
|
.mover-row .fname { flex: 1; }
|
||||||
|
.mover-dest { overflow-wrap: anywhere; }
|
||||||
|
.thumb-note .btn { padding-top: 3px; padding-bottom: 3px; }
|
||||||
|
.files-bar .task-out { margin: 6px 10px 0; max-height: 160px; }
|
||||||
@media (max-width: 800px) {
|
@media (max-width: 800px) {
|
||||||
.file-row { grid-template-columns: 24px 1fr 70px 32px; } .file-row .fmeta:nth-child(4), .file-row .perm { display: none; }
|
.file-row { grid-template-columns: 24px 1fr 70px 32px; } .file-row .fmeta:nth-child(4), .file-row .perm { display: none; }
|
||||||
|
.file-list.thumbs .file-row { grid-template-columns: 60px 1fr 70px 32px; min-height: 46px; }
|
||||||
|
.fthumb { width: 60px; height: 38px; }
|
||||||
.path-input { width: 100%; margin: 4px 0 0; }
|
.path-input { width: 100%; margin: 4px 0 0; }
|
||||||
.files-tools .filter { width: 80px; }
|
.files-tools .filter { width: 80px; }
|
||||||
}
|
}
|
||||||
@@ -603,7 +643,28 @@ a.btn { text-decoration: none; display: inline-flex; align-items: center; }
|
|||||||
.docker .svc-list { min-width: 0; max-width: 100%; } .docker .svc-rows { overflow-x: hidden; } .docker .svc-name .d { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
.docker .svc-list { min-width: 0; max-width: 100%; } .docker .svc-rows { overflow-x: hidden; } .docker .svc-name .d { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
.docker:not(.has-sel) .svc-list { flex: 1 1 0; }
|
.docker:not(.has-sel) .svc-list { flex: 1 1 0; }
|
||||||
.stack-editor { flex: 1; min-height: 46vh; font-size: 13px; tab-size: 2; white-space: pre; overflow: auto; }
|
.stack-editor { flex: 1; min-height: 46vh; font-size: 13px; tab-size: 2; white-space: pre; overflow: auto; }
|
||||||
@media (max-width: 800px) { .docker .svc-list { width: 100%; flex: 1 1 auto; } .stack-editor { min-height: 52vh; font-size: 12px; } }
|
|
||||||
|
/* The code editor: a gutter of line numbers, a painted copy of the text behind a textarea
|
||||||
|
whose own text is transparent, both in the same monospace at the same size so they line up
|
||||||
|
glyph for glyph. .code takes the sizing class the textarea used to (.stack-editor), so it
|
||||||
|
sits where that did. */
|
||||||
|
.code { display: flex; align-items: stretch; background: var(--term-bg); border: 1px solid var(--border); border-radius: 6px; overflow: hidden; position: relative; padding: 0; min-height: 260px; }
|
||||||
|
.code.ro .code-text { color: var(--muted); }
|
||||||
|
.code-gutter { flex: 0 0 auto; min-width: 2.6em; padding: 10px 6px 10px 0; text-align: right; color: var(--muted); opacity: .7; background: var(--bg2); border-right: 1px solid var(--border); overflow: hidden; user-select: none; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; }
|
||||||
|
.code-body { flex: 1; position: relative; min-width: 0; }
|
||||||
|
.code-paint, .code-text { margin: 0; padding: 10px; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace; tab-size: 2; white-space: pre; letter-spacing: 0; word-spacing: 0; }
|
||||||
|
.code-paint { position: absolute; inset: 0; overflow: hidden; color: var(--fg); pointer-events: none; }
|
||||||
|
.code-text { position: absolute; inset: 0; width: 100%; height: 100%; background: transparent; color: transparent; caret-color: var(--fg); border: 0; outline: 0; resize: none; overflow: auto; }
|
||||||
|
.code-text::placeholder { color: var(--muted); }
|
||||||
|
.code-text::selection { background: rgba(127, 127, 127, .35); }
|
||||||
|
.stack-editor.code, .stack-editor .code-paint, .stack-editor .code-text, .stack-editor .code-gutter { font-size: 13px; }
|
||||||
|
.y-key { color: var(--accent2); } .y-colon, .y-dash { color: var(--muted); } .y-string { color: var(--ok); } .y-number, .y-bool { color: var(--s2); } .y-comment { color: var(--muted); font-style: italic; } .y-anchor, .y-block { color: var(--accent); } .y-value { color: var(--fg); }
|
||||||
|
/* a phone has no Tab key: a bar of the strokes a compose file needs, pinned to the editor's foot */
|
||||||
|
.code-keys { position: absolute; left: 0; right: 0; bottom: 0; display: flex; gap: 6px; padding: 6px 8px; background: var(--bg2); border-top: 1px solid var(--border); z-index: 1; }
|
||||||
|
.code-keys button { min-width: 40px; height: 32px; padding: 0 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--bg3); color: var(--fg); font: 15px ui-monospace, monospace; }
|
||||||
|
.code-keys ~ * { display: none; }
|
||||||
|
@media (pointer: coarse) { .code .code-body { padding-bottom: 45px; } .code .code-gutter { padding-bottom: 55px; } }
|
||||||
|
@media (max-width: 800px) { .docker .svc-list { width: 100%; flex: 1 1 auto; } .stack-editor { min-height: 52vh; font-size: 12px; } .stack-editor.code, .stack-editor .code-paint, .stack-editor .code-text, .stack-editor .code-gutter { font-size: 12px; } }
|
||||||
|
|
||||||
/* stacks: diff, images, .env */
|
/* stacks: diff, images, .env */
|
||||||
.stack-extra { display: flex; flex-direction: column; gap: 6px; border-top: 1px solid var(--border); padding-top: 10px; } .stack-extra .sub-head .k { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
|
.stack-extra { display: flex; flex-direction: column; gap: 6px; border-top: 1px solid var(--border); padding-top: 10px; } .stack-extra .sub-head .k { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
|
||||||
@@ -641,6 +702,12 @@ pre.diff .add { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%,
|
|||||||
.top-gpus { grid-column: 1 / -1; grid-row: 2; display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px; min-width: 0; }
|
.top-gpus { grid-column: 1 / -1; grid-row: 2; display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); gap: 4px; min-width: 0; }
|
||||||
.top.has-gpu .top-mem, .top.has-gpu .top-disks { grid-row: 3; } .top.has-gpu .top-net { grid-row: 4; } .top.has-gpu .top-proc { grid-row: 3 / 5; }
|
.top.has-gpu .top-mem, .top.has-gpu .top-disks { grid-row: 3; } .top.has-gpu .top-net { grid-row: 4; } .top.has-gpu .top-proc { grid-row: 3 / 5; }
|
||||||
.top-gpu { min-height: 210px; }
|
.top-gpu { min-height: 210px; }
|
||||||
|
/* a GPU with nothing to measure is one line, and on a machine with no other its row is only as
|
||||||
|
tall as that line. Same weight as .top.has-gpu and after it, so the narrower layouts' own
|
||||||
|
.top.has-gpu, further down, still wins there (their gpu row is already auto). */
|
||||||
|
.top.gpu-slim { grid-template-rows: minmax(200px, 1fr) auto auto minmax(170px, 1fr); }
|
||||||
|
.top-gpu.slim { align-self: start; } /* beside a full gpu box it keeps its one line, not that box's height */
|
||||||
|
.t-row.t-gpu-line { flex-wrap: wrap; gap: 0 14px; } .t-gpu-line > span { white-space: nowrap; } .t-gpu-who { margin-left: auto; } /* on a phone it wraps to its own line, still on the right */
|
||||||
.t-gpu { display: flex; gap: 8px; min-width: 0; flex: 1; min-height: 0; }
|
.t-gpu { display: flex; gap: 8px; min-width: 0; flex: 1; min-height: 0; }
|
||||||
/* the graph and its caption share this box: tall enough for both, so the caption never lands on the stats */
|
/* the graph and its caption share this box: tall enough for both, so the caption never lands on the stats */
|
||||||
.t-gpu-graph { flex: 1; min-width: 0; min-height: 130px; display: flex; flex-direction: column; }
|
.t-gpu-graph { flex: 1; min-width: 0; min-height: 130px; display: flex; flex-direction: column; }
|
||||||
@@ -757,7 +824,7 @@ pre.diff .add { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%,
|
|||||||
@media (max-width: 800px) {
|
@media (max-width: 800px) {
|
||||||
.top { display: flex; flex-direction: column; font-size: 12px; }
|
.top { display: flex; flex-direction: column; font-size: 12px; }
|
||||||
.top > .tbox { flex: 0 0 auto; }
|
.top > .tbox { flex: 0 0 auto; }
|
||||||
.top-gpus { display: flex; flex-direction: column; flex: 0 0 auto; }
|
.top-gpus { display: flex; flex-direction: column; flex: 0 0 auto; } .top-gpu.slim { align-self: stretch; }
|
||||||
.t-gpu { flex-direction: column; } .t-gpu-graph { height: 130px; flex: 0 0 130px; } .t-gpu-stats { width: auto; max-height: none; }
|
.t-gpu { flex-direction: column; } .t-gpu-graph { height: 130px; flex: 0 0 130px; } .t-gpu-stats { width: auto; max-height: none; }
|
||||||
.t-cpu { flex-direction: column; } .t-cpu-graph { height: 130px; flex: 0 0 130px; } .t-cores { width: auto; max-height: 40vh; }
|
.t-cpu { flex-direction: column; } .t-cpu-graph { height: 130px; flex: 0 0 130px; } .t-cores { width: auto; max-height: 40vh; }
|
||||||
.t-net { flex-direction: column; } .t-net-nums { width: auto; } .t-net-graphs { flex: 0 0 130px; }
|
.t-net { flex-direction: column; } .t-net-nums { width: auto; } .t-net-graphs { flex: 0 0 130px; }
|
||||||
@@ -769,6 +836,7 @@ pre.diff .add { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%,
|
|||||||
@media (max-width: 640px) { .arc-sparks { grid-template-columns: 1fr; } }
|
@media (max-width: 640px) { .arc-sparks { grid-template-columns: 1fr; } }
|
||||||
.arc-break { display: grid; grid-template-columns: minmax(140px, 1fr) 80px 110px 110px; gap: 2px 10px; font-size: 12px; margin-top: 10px; }
|
.arc-break { display: grid; grid-template-columns: minmax(140px, 1fr) 80px 110px 110px; gap: 2px 10px; font-size: 12px; margin-top: 10px; }
|
||||||
.arc-break .head { color: var(--muted); } .arc-break .num { text-align: right; }
|
.arc-break .head { color: var(--muted); } .arc-break .num { text-align: right; }
|
||||||
|
@media (max-width: 640px) { .arc-break { grid-template-columns: minmax(0, 1fr) auto auto auto; gap: 2px 8px; font-size: 11px; } .arc-break > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; } }
|
||||||
.arc-tiers { display: flex; flex-direction: column; gap: 5px; font-size: 13px; margin-top: 4px; }
|
.arc-tiers { display: flex; flex-direction: column; gap: 5px; font-size: 13px; margin-top: 4px; }
|
||||||
.arc-tier { display: flex; gap: 8px; min-width: 0; } .arc-tier .k { color: var(--muted); width: 80px; flex-shrink: 0; cursor: help; }
|
.arc-tier { display: flex; gap: 8px; min-width: 0; } .arc-tier .k { color: var(--muted); width: 80px; flex-shrink: 0; cursor: help; }
|
||||||
.arc-tier-v { display: flex; flex-direction: column; gap: 1px; min-width: 0; } .arc-tier-v > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
.arc-tier-v { display: flex; flex-direction: column; gap: 1px; min-width: 0; } .arc-tier-v > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||||
@@ -795,6 +863,7 @@ pre.diff .add { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%,
|
|||||||
.space .warn { color: var(--s2); }
|
.space .warn { color: var(--s2); }
|
||||||
.space-tabs { padding: 8px 12px 0; }
|
.space-tabs { padding: 8px 12px 0; }
|
||||||
.space-split { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 8px 12px 12px; gap: 8px; }
|
.space-split { flex: 1; display: flex; flex-direction: column; min-height: 0; padding: 8px 12px 12px; gap: 8px; }
|
||||||
|
@media (max-width: 800px) { .space-split { overflow-y: auto; padding: 8px; } .space-tree { flex: 0 0 auto; height: 40vh; } .space-map { flex: 0 0 auto; height: 60vh; } }
|
||||||
.space-tree { flex: 1 1 55%; min-height: 120px; display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 6px; background: var(--bg2); overflow: hidden; }
|
.space-tree { flex: 1 1 55%; min-height: 120px; display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 6px; background: var(--bg2); overflow: hidden; }
|
||||||
.space-rows { flex: 1; overflow: auto; min-height: 0; }
|
.space-rows { flex: 1; overflow: auto; min-height: 0; }
|
||||||
.space-row { display: grid; grid-template-columns: minmax(160px, 1fr) 84px 120px 70px 64px 88px; gap: 8px; align-items: center; padding: 3px 8px; font-size: 13px; border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap; }
|
.space-row { display: grid; grid-template-columns: minmax(160px, 1fr) 84px 120px 70px 64px 88px; gap: 8px; align-items: center; padding: 3px 8px; font-size: 13px; border-bottom: 1px solid var(--border); cursor: pointer; white-space: nowrap; }
|
||||||
@@ -856,3 +925,26 @@ pre.diff .add { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%,
|
|||||||
.kvm-shrink:hover, .kvm-shrink:active { opacity: 1; }
|
.kvm-shrink:hover, .kvm-shrink:active { opacity: 1; }
|
||||||
.updot { margin-left: 6px; font-size: 11px; font-weight: 700; }
|
.updot { margin-left: 6px; font-size: 11px; font-weight: 700; }
|
||||||
.updot.behind { color: var(--s2); } .updot.unknown { color: var(--muted); }
|
.updot.behind { color: var(--s2); } .updot.unknown { color: var(--muted); }
|
||||||
|
|
||||||
|
/* ---- Virtualization ---- */
|
||||||
|
.virt .svc-tools .task { max-height: 45vh; }
|
||||||
|
.virt-table { width: 100%; border-collapse: collapse; font-size: 13px; }
|
||||||
|
.virt-table th, .virt-table td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
|
||||||
|
.virt-table th { font-weight: 500; opacity: .7; }
|
||||||
|
.virt-table td.num, .virt-table th.num { text-align: right; white-space: nowrap; }
|
||||||
|
.virt-pick { max-height: 240px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; }
|
||||||
|
.virt-pick .svc-row { padding: 6px 10px; }
|
||||||
|
.virt-kv { display: grid; grid-template-columns: max-content 1fr; gap: 2px 12px; font-size: 12px; word-break: break-all; }
|
||||||
|
.virt-form-row { display: flex; gap: 8px; }
|
||||||
|
.virt-form-row > label { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
|
||||||
|
.virt-form-row > label > input, .virt-form-row > label > select { width: 100%; box-sizing: border-box; }
|
||||||
|
.virt .modal { max-width: 640px; }
|
||||||
|
.vga-back { align-items: stretch; padding: 10px; }
|
||||||
|
.vga { display: flex; flex-direction: column; gap: 6px; width: 100%; height: 100%; max-width: none; max-height: none; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px; }
|
||||||
|
.vga-bar { flex-shrink: 0; }
|
||||||
|
.vga-area { flex: 1; min-height: 0; overflow: auto; background: #000; border-radius: 6px; display: flex; align-items: flex-start; justify-content: flex-start; padding: 4px; }
|
||||||
|
.vga-area #spice-area { display: inline-block; }
|
||||||
|
.vga-area #spice-screen { display: inline-block; line-height: 0; }
|
||||||
|
.vga-area canvas { outline: none; }
|
||||||
|
.vga-msg { display: none; }
|
||||||
|
.vga-hint { flex-shrink: 0; }
|
||||||
|
|||||||
Vendored
+674
@@ -0,0 +1,674 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
How to Apply These Terms to Your New Programs
|
||||||
|
|
||||||
|
If you develop a new program, and you want it to be of the greatest
|
||||||
|
possible use to the public, the best way to achieve this is to make it
|
||||||
|
free software which everyone can redistribute and change under these terms.
|
||||||
|
|
||||||
|
To do so, attach the following notices to the program. It is safest
|
||||||
|
to attach them to the start of each source file to most effectively
|
||||||
|
state the exclusion of warranty; and each file should have at least
|
||||||
|
the "copyright" line and a pointer to where the full notice is found.
|
||||||
|
|
||||||
|
<one line to give the program's name and a brief idea of what it does.>
|
||||||
|
Copyright (C) <year> <name of author>
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Also add information on how to contact you by electronic and paper mail.
|
||||||
|
|
||||||
|
If the program does terminal interaction, make it output a short
|
||||||
|
notice like this when it starts in an interactive mode:
|
||||||
|
|
||||||
|
<program> Copyright (C) <year> <name of author>
|
||||||
|
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||||
|
This is free software, and you are welcome to redistribute it
|
||||||
|
under certain conditions; type `show c' for details.
|
||||||
|
|
||||||
|
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||||
|
parts of the General Public License. Of course, your program's commands
|
||||||
|
might be different; for a GUI interface, you would use an "about box".
|
||||||
|
|
||||||
|
You should also get your employer (if you work as a programmer) or school,
|
||||||
|
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||||
|
For more information on this, and how to apply and follow the GNU GPL, see
|
||||||
|
<http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
The GNU General Public License does not permit incorporating your program
|
||||||
|
into proprietary programs. If your program is a subroutine library, you
|
||||||
|
may consider it more useful to permit linking proprietary applications with
|
||||||
|
the library. If this is what you want to do, use the GNU Lesser General
|
||||||
|
Public License instead of this License. But first, please read
|
||||||
|
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||||
+165
@@ -0,0 +1,165 @@
|
|||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
|
||||||
|
This version of the GNU Lesser General Public License incorporates
|
||||||
|
the terms and conditions of version 3 of the GNU General Public
|
||||||
|
License, supplemented by the additional permissions listed below.
|
||||||
|
|
||||||
|
0. Additional Definitions.
|
||||||
|
|
||||||
|
As used herein, "this License" refers to version 3 of the GNU Lesser
|
||||||
|
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
||||||
|
General Public License.
|
||||||
|
|
||||||
|
"The Library" refers to a covered work governed by this License,
|
||||||
|
other than an Application or a Combined Work as defined below.
|
||||||
|
|
||||||
|
An "Application" is any work that makes use of an interface provided
|
||||||
|
by the Library, but which is not otherwise based on the Library.
|
||||||
|
Defining a subclass of a class defined by the Library is deemed a mode
|
||||||
|
of using an interface provided by the Library.
|
||||||
|
|
||||||
|
A "Combined Work" is a work produced by combining or linking an
|
||||||
|
Application with the Library. The particular version of the Library
|
||||||
|
with which the Combined Work was made is also called the "Linked
|
||||||
|
Version".
|
||||||
|
|
||||||
|
The "Minimal Corresponding Source" for a Combined Work means the
|
||||||
|
Corresponding Source for the Combined Work, excluding any source code
|
||||||
|
for portions of the Combined Work that, considered in isolation, are
|
||||||
|
based on the Application, and not on the Linked Version.
|
||||||
|
|
||||||
|
The "Corresponding Application Code" for a Combined Work means the
|
||||||
|
object code and/or source code for the Application, including any data
|
||||||
|
and utility programs needed for reproducing the Combined Work from the
|
||||||
|
Application, but excluding the System Libraries of the Combined Work.
|
||||||
|
|
||||||
|
1. Exception to Section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
You may convey a covered work under sections 3 and 4 of this License
|
||||||
|
without being bound by section 3 of the GNU GPL.
|
||||||
|
|
||||||
|
2. Conveying Modified Versions.
|
||||||
|
|
||||||
|
If you modify a copy of the Library, and, in your modifications, a
|
||||||
|
facility refers to a function or data to be supplied by an Application
|
||||||
|
that uses the facility (other than as an argument passed when the
|
||||||
|
facility is invoked), then you may convey a copy of the modified
|
||||||
|
version:
|
||||||
|
|
||||||
|
a) under this License, provided that you make a good faith effort to
|
||||||
|
ensure that, in the event an Application does not supply the
|
||||||
|
function or data, the facility still operates, and performs
|
||||||
|
whatever part of its purpose remains meaningful, or
|
||||||
|
|
||||||
|
b) under the GNU GPL, with none of the additional permissions of
|
||||||
|
this License applicable to that copy.
|
||||||
|
|
||||||
|
3. Object Code Incorporating Material from Library Header Files.
|
||||||
|
|
||||||
|
The object code form of an Application may incorporate material from
|
||||||
|
a header file that is part of the Library. You may convey such object
|
||||||
|
code under terms of your choice, provided that, if the incorporated
|
||||||
|
material is not limited to numerical parameters, data structure
|
||||||
|
layouts and accessors, or small macros, inline functions and templates
|
||||||
|
(ten or fewer lines in length), you do both of the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the object code that the
|
||||||
|
Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the object code with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
4. Combined Works.
|
||||||
|
|
||||||
|
You may convey a Combined Work under terms of your choice that,
|
||||||
|
taken together, effectively do not restrict modification of the
|
||||||
|
portions of the Library contained in the Combined Work and reverse
|
||||||
|
engineering for debugging such modifications, if you also do each of
|
||||||
|
the following:
|
||||||
|
|
||||||
|
a) Give prominent notice with each copy of the Combined Work that
|
||||||
|
the Library is used in it and that the Library and its use are
|
||||||
|
covered by this License.
|
||||||
|
|
||||||
|
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
||||||
|
document.
|
||||||
|
|
||||||
|
c) For a Combined Work that displays copyright notices during
|
||||||
|
execution, include the copyright notice for the Library among
|
||||||
|
these notices, as well as a reference directing the user to the
|
||||||
|
copies of the GNU GPL and this license document.
|
||||||
|
|
||||||
|
d) Do one of the following:
|
||||||
|
|
||||||
|
0) Convey the Minimal Corresponding Source under the terms of this
|
||||||
|
License, and the Corresponding Application Code in a form
|
||||||
|
suitable for, and under terms that permit, the user to
|
||||||
|
recombine or relink the Application with a modified version of
|
||||||
|
the Linked Version to produce a modified Combined Work, in the
|
||||||
|
manner specified by section 6 of the GNU GPL for conveying
|
||||||
|
Corresponding Source.
|
||||||
|
|
||||||
|
1) Use a suitable shared library mechanism for linking with the
|
||||||
|
Library. A suitable mechanism is one that (a) uses at run time
|
||||||
|
a copy of the Library already present on the user's computer
|
||||||
|
system, and (b) will operate properly with a modified version
|
||||||
|
of the Library that is interface-compatible with the Linked
|
||||||
|
Version.
|
||||||
|
|
||||||
|
e) Provide Installation Information, but only if you would otherwise
|
||||||
|
be required to provide such information under section 6 of the
|
||||||
|
GNU GPL, and only to the extent that such information is
|
||||||
|
necessary to install and execute a modified version of the
|
||||||
|
Combined Work produced by recombining or relinking the
|
||||||
|
Application with a modified version of the Linked Version. (If
|
||||||
|
you use option 4d0, the Installation Information must accompany
|
||||||
|
the Minimal Corresponding Source and Corresponding Application
|
||||||
|
Code. If you use option 4d1, you must provide the Installation
|
||||||
|
Information in the manner specified by section 6 of the GNU GPL
|
||||||
|
for conveying Corresponding Source.)
|
||||||
|
|
||||||
|
5. Combined Libraries.
|
||||||
|
|
||||||
|
You may place library facilities that are a work based on the
|
||||||
|
Library side by side in a single library together with other library
|
||||||
|
facilities that are not Applications and are not covered by this
|
||||||
|
License, and convey such a combined library under terms of your
|
||||||
|
choice, if you do both of the following:
|
||||||
|
|
||||||
|
a) Accompany the combined library with a copy of the same work based
|
||||||
|
on the Library, uncombined with any other library facilities,
|
||||||
|
conveyed under the terms of this License.
|
||||||
|
|
||||||
|
b) Give prominent notice with the combined library that part of it
|
||||||
|
is a work based on the Library, and explaining where to find the
|
||||||
|
accompanying uncombined form of the same work.
|
||||||
|
|
||||||
|
6. Revised Versions of the GNU Lesser General Public License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions
|
||||||
|
of the GNU Lesser General Public License from time to time. Such new
|
||||||
|
versions will be similar in spirit to the present version, but may
|
||||||
|
differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Library as you received it specifies that a certain numbered version
|
||||||
|
of the GNU Lesser General Public License "or any later version"
|
||||||
|
applies to it, you have the option of following the terms and
|
||||||
|
conditions either of that published version or of any later version
|
||||||
|
published by the Free Software Foundation. If the Library as you
|
||||||
|
received it does not specify a version number of the GNU Lesser
|
||||||
|
General Public License, you may choose any version of the GNU Lesser
|
||||||
|
General Public License ever published by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Library as you received it specifies that a proxy can decide
|
||||||
|
whether future versions of the GNU Lesser General Public License shall
|
||||||
|
apply, that proxy's public statement of acceptance of any version is
|
||||||
|
permanent authorization for you to choose that version for the
|
||||||
|
Library.
|
||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
# spice-html5, vendored
|
||||||
|
|
||||||
|
Upstream: https://gitlab.freedesktop.org/spice/spice-html5 — commit b372aab (2026-09-21),
|
||||||
|
copied unchanged on 2026-10-09 (`src/`, `thirdparty/`). LGPL-3.0-or-later; the
|
||||||
|
licence text is beside this file. It draws a VM's SPICE display into a canvas from a websocket
|
||||||
|
carrying the raw SPICE stream — which is what Incus's VGA console websocket is — so Ham Lab's
|
||||||
|
daemon bridges the two and this does the rest. Loaded lazily by `VgaConsole.tsx`, so the main
|
||||||
|
bundle does not carry it. Not on npm in a current form (the `@spice-project/spice-html5`
|
||||||
|
package stopped at 0.2.1 in 2019), hence the copy.
|
||||||
|
|
||||||
|
Upstream's `spice.css` is deliberately **not** here. It is the stylesheet of spice-html5's
|
||||||
|
demo page, with rules for `body`, `*`, `button` and `canvas` that apply to the whole
|
||||||
|
document. It was copied and imported on 2026-10-09 and turned every button in Ham Lab into
|
||||||
|
a gradient pill shifted off its place (v0.1.0-188 to -191). The console is styled by the
|
||||||
|
`.vga` rules in `src/styles.css`; `scripts/test-css-imports.mjs` fails on any new
|
||||||
|
stylesheet import that has not been looked at.
|
||||||
+189
@@ -0,0 +1,189 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Aric Stewart <aric@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright 1990,91 by Thomas Roell, Dinkelscherben, Germany.
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
||||||
|
* documentation for any purpose is hereby granted without fee, provided that
|
||||||
|
* the above copyright notice appear in all copies and that both that
|
||||||
|
* copyright notice and this permission notice appear in supporting
|
||||||
|
* documentation, and that the name of Thomas Roell not be used in
|
||||||
|
* advertising or publicity pertaining to distribution of the software without
|
||||||
|
* specific, written prior permission. Thomas Roell makes no representations
|
||||||
|
* about the suitability of this software for any purpose. It is provided
|
||||||
|
* "as is" without express or implied warranty.
|
||||||
|
*
|
||||||
|
* THOMAS ROELL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
||||||
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
||||||
|
* EVENT SHALL THOMAS ROELL BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
||||||
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
||||||
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
||||||
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||||
|
* PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1994-2003 by The XFree86 Project, Inc.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
* copy of this software and associated documentation files (the "Software"),
|
||||||
|
* to deal in the Software without restriction, including without limitation
|
||||||
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
* and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
* Software is furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
|
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||||
|
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
||||||
|
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||||
|
* OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* Except as contained in this notice, the name of the copyright holder(s)
|
||||||
|
* and author(s) shall not be used in advertising or otherwise to promote
|
||||||
|
* the sale, use or other dealings in this Software without prior written
|
||||||
|
* authorization from the copyright holder(s) and author(s).
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* NOTE: The AT/MF keyboards can generate (via the 8042) two (MF: three)
|
||||||
|
* sets of scancodes. Set3 can only be generated by a MF keyboard.
|
||||||
|
* Set2 sends a makecode for keypress, and the same code prefixed by a
|
||||||
|
* F0 for keyrelease. This is a little bit ugly to handle. Thus we use
|
||||||
|
* here for X386 the PC/XT compatible Set1. This set uses 8bit scancodes.
|
||||||
|
* Bit 7 ist set if the key is released. The code E0 switches to a
|
||||||
|
* different meaning to add the new MF cursorkeys, while not breaking old
|
||||||
|
* applications. E1 is another special prefix. Since I assume that there
|
||||||
|
* will be further versions of PC/XT scancode compatible keyboards, we
|
||||||
|
* may be in trouble one day.
|
||||||
|
*
|
||||||
|
* IDEA: 1) Use Set2 on AT84 keyboards and translate it to MF Set3.
|
||||||
|
* 2) Use the keyboards native set and translate it to common keysyms.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
var KeyNames = {
|
||||||
|
/*
|
||||||
|
* definition of the AT84/MF101/MF102 Keyboard:
|
||||||
|
* ============================================================
|
||||||
|
* Defined Key Cap Glyphs Pressed value
|
||||||
|
* Key Name Main Also (hex) (dec)
|
||||||
|
* ---------------- ---------- ------- ------ ------
|
||||||
|
*/
|
||||||
|
KEY_Escape :/* Escape 0x01 */ 1,
|
||||||
|
KEY_1 :/* 1 ! 0x02 */ 2,
|
||||||
|
KEY_2 :/* 2 @ 0x03 */ 3,
|
||||||
|
KEY_3 :/* 3 # 0x04 */ 4,
|
||||||
|
KEY_4 :/* 4 $ 0x05 */ 5,
|
||||||
|
KEY_5 :/* 5 % 0x06 */ 6,
|
||||||
|
KEY_6 :/* 6 ^ 0x07 */ 7,
|
||||||
|
KEY_7 :/* 7 & 0x08 */ 8,
|
||||||
|
KEY_8 :/* 8 * 0x09 */ 9,
|
||||||
|
KEY_9 :/* 9 ( 0x0a */ 10,
|
||||||
|
KEY_0 :/* 0 ) 0x0b */ 11,
|
||||||
|
KEY_Minus :/* - (Minus) _ (Under) 0x0c */ 12,
|
||||||
|
KEY_Equal :/* = (Equal) + 0x0d */ 13,
|
||||||
|
KEY_BackSpace :/* Back Space 0x0e */ 14,
|
||||||
|
KEY_Tab :/* Tab 0x0f */ 15,
|
||||||
|
KEY_Q :/* Q 0x10 */ 16,
|
||||||
|
KEY_W :/* W 0x11 */ 17,
|
||||||
|
KEY_E :/* E 0x12 */ 18,
|
||||||
|
KEY_R :/* R 0x13 */ 19,
|
||||||
|
KEY_T :/* T 0x14 */ 20,
|
||||||
|
KEY_Y :/* Y 0x15 */ 21,
|
||||||
|
KEY_U :/* U 0x16 */ 22,
|
||||||
|
KEY_I :/* I 0x17 */ 23,
|
||||||
|
KEY_O :/* O 0x18 */ 24,
|
||||||
|
KEY_P :/* P 0x19 */ 25,
|
||||||
|
KEY_LBrace :/* [ { 0x1a */ 26,
|
||||||
|
KEY_RBrace :/* ] } 0x1b */ 27,
|
||||||
|
KEY_Enter :/* Enter 0x1c */ 28,
|
||||||
|
KEY_LCtrl :/* Ctrl(left) 0x1d */ 29,
|
||||||
|
KEY_A :/* A 0x1e */ 30,
|
||||||
|
KEY_S :/* S 0x1f */ 31,
|
||||||
|
KEY_D :/* D 0x20 */ 32,
|
||||||
|
KEY_F :/* F 0x21 */ 33,
|
||||||
|
KEY_G :/* G 0x22 */ 34,
|
||||||
|
KEY_H :/* H 0x23 */ 35,
|
||||||
|
KEY_J :/* J 0x24 */ 36,
|
||||||
|
KEY_K :/* K 0x25 */ 37,
|
||||||
|
KEY_L :/* L 0x26 */ 38,
|
||||||
|
KEY_SemiColon :/* ;(SemiColon) :(Colon) 0x27 */ 39,
|
||||||
|
KEY_Quote :/* ' (Apostr) " (Quote) 0x28 */ 40,
|
||||||
|
KEY_Tilde :/* ` (Accent) ~ (Tilde) 0x29 */ 41,
|
||||||
|
KEY_ShiftL :/* Shift(left) 0x2a */ 42,
|
||||||
|
KEY_BSlash :/* \(BckSlash) |(VertBar)0x2b */ 43,
|
||||||
|
KEY_Z :/* Z 0x2c */ 44,
|
||||||
|
KEY_X :/* X 0x2d */ 45,
|
||||||
|
KEY_C :/* C 0x2e */ 46,
|
||||||
|
KEY_V :/* V 0x2f */ 47,
|
||||||
|
KEY_B :/* B 0x30 */ 48,
|
||||||
|
KEY_N :/* N 0x31 */ 49,
|
||||||
|
KEY_M :/* M 0x32 */ 50,
|
||||||
|
KEY_Comma :/* , (Comma) < (Less) 0x33 */ 51,
|
||||||
|
KEY_Period :/* . (Period) >(Greater)0x34 */ 52,
|
||||||
|
KEY_Slash :/* / (Slash) ? 0x35 */ 53,
|
||||||
|
KEY_ShiftR :/* Shift(right) 0x36 */ 54,
|
||||||
|
KEY_KP_Multiply :/* * 0x37 */ 55,
|
||||||
|
KEY_Alt :/* Alt(left) 0x38 */ 56,
|
||||||
|
KEY_Space :/* (SpaceBar) 0x39 */ 57,
|
||||||
|
KEY_CapsLock :/* CapsLock 0x3a */ 58,
|
||||||
|
KEY_F1 :/* F1 0x3b */ 59,
|
||||||
|
KEY_F2 :/* F2 0x3c */ 60,
|
||||||
|
KEY_F3 :/* F3 0x3d */ 61,
|
||||||
|
KEY_F4 :/* F4 0x3e */ 62,
|
||||||
|
KEY_F5 :/* F5 0x3f */ 63,
|
||||||
|
KEY_F6 :/* F6 0x40 */ 64,
|
||||||
|
KEY_F7 :/* F7 0x41 */ 65,
|
||||||
|
KEY_F8 :/* F8 0x42 */ 66,
|
||||||
|
KEY_F9 :/* F9 0x43 */ 67,
|
||||||
|
KEY_F10 :/* F10 0x44 */ 68,
|
||||||
|
KEY_NumLock :/* NumLock 0x45 */ 69,
|
||||||
|
KEY_ScrollLock :/* ScrollLock 0x46 */ 70,
|
||||||
|
KEY_KP_7 :/* 7 Home 0x47 */ 71,
|
||||||
|
KEY_KP_8 :/* 8 Up 0x48 */ 72,
|
||||||
|
KEY_KP_9 :/* 9 PgUp 0x49 */ 73,
|
||||||
|
KEY_KP_Minus :/* - (Minus) 0x4a */ 74,
|
||||||
|
KEY_KP_4 :/* 4 Left 0x4b */ 75,
|
||||||
|
KEY_KP_5 :/* 5 0x4c */ 76,
|
||||||
|
KEY_KP_6 :/* 6 Right 0x4d */ 77,
|
||||||
|
KEY_KP_Plus :/* + (Plus) 0x4e */ 78,
|
||||||
|
KEY_KP_1 :/* 1 End 0x4f */ 79,
|
||||||
|
KEY_KP_2 :/* 2 Down 0x50 */ 80,
|
||||||
|
KEY_KP_3 :/* 3 PgDown 0x51 */ 81,
|
||||||
|
KEY_KP_0 :/* 0 Insert 0x52 */ 82,
|
||||||
|
KEY_KP_Decimal :/* . (Decimal) Delete 0x53 */ 83,
|
||||||
|
KEY_SysRequest :/* SysRequest 0x54 */ 84,
|
||||||
|
/* NOTUSED 0x55 */
|
||||||
|
KEY_Less :/* < (Less) >(Greater) 0x56 */ 86,
|
||||||
|
KEY_F11 :/* F11 0x57 */ 87,
|
||||||
|
KEY_F12 :/* F12 0x58 */ 88,
|
||||||
|
|
||||||
|
KEY_Prefix0 :/* special 0x60 */ 96,
|
||||||
|
KEY_Prefix1 :/* specail 0x61 */ 97,
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
KeyNames,
|
||||||
|
};
|
||||||
+64
@@ -0,0 +1,64 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** bitmap.js
|
||||||
|
** Handle SPICE_IMAGE_TYPE_BITMAP
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
|
||||||
|
/* A 32-bit source pixel is B, G, R, A in memory. Read it as a little-endian
|
||||||
|
word, A R G B, rotate it left by eight to R G B A and store that word
|
||||||
|
big-endian, so the bytes land in the R, G, B, A order ImageData wants: one
|
||||||
|
load, one store and two shifts per pixel. A DataView takes either host
|
||||||
|
byte order and any alignment. Only 32BIT and RGBA are handled; 32BIT
|
||||||
|
ignores the source's high byte and is fully opaque. */
|
||||||
|
function convert_spice_bitmap_to_web(context, spice_bitmap)
|
||||||
|
{
|
||||||
|
if (spice_bitmap.format != Constants.SPICE_BITMAP_FMT_32BIT &&
|
||||||
|
spice_bitmap.format != Constants.SPICE_BITMAP_FMT_RGBA)
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
const w = spice_bitmap.x;
|
||||||
|
const h = spice_bitmap.y;
|
||||||
|
const stride = spice_bitmap.stride;
|
||||||
|
const ret = context.createImageData(w, h);
|
||||||
|
const opaque = spice_bitmap.format == Constants.SPICE_BITMAP_FMT_32BIT;
|
||||||
|
const top_down = spice_bitmap.flags & Constants.SPICE_BITMAP_FLAGS_TOP_DOWN;
|
||||||
|
const src = new DataView(spice_bitmap.data);
|
||||||
|
const dest = new DataView(ret.data.buffer);
|
||||||
|
let d = 0;
|
||||||
|
for (let y = 0; y < h; y++)
|
||||||
|
{
|
||||||
|
let s = (top_down ? y : h - 1 - y) * stride;
|
||||||
|
for (let x = 0; x < w; x++, d += 4, s += 4)
|
||||||
|
{
|
||||||
|
const v = src.getUint32(s, true);
|
||||||
|
dest.setUint32(d, (v << 8) | (opaque ? 0xff : v >>> 24), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
convert_spice_bitmap_to_web,
|
||||||
|
};
|
||||||
+149
@@ -0,0 +1,149 @@
|
|||||||
|
/*
|
||||||
|
* @see https://developer.mozilla.org/en-US/docs/Web/API/UI_Events/Keyboard_event_code_values
|
||||||
|
*/
|
||||||
|
export const code_to_scancode = [];
|
||||||
|
code_to_scancode["Escape"] = 0x01;
|
||||||
|
code_to_scancode["Digit1"] = 0x02;
|
||||||
|
code_to_scancode["Digit2"] = 0x03;
|
||||||
|
code_to_scancode["Digit3"] = 0x04;
|
||||||
|
code_to_scancode["Digit4"] = 0x05;
|
||||||
|
code_to_scancode["Digit5"] = 0x06;
|
||||||
|
code_to_scancode["Digit6"] = 0x07;
|
||||||
|
code_to_scancode["Digit7"] = 0x08;
|
||||||
|
code_to_scancode["Digit8"] = 0x09;
|
||||||
|
code_to_scancode["Digit9"] = 0x0A;
|
||||||
|
code_to_scancode["Digit0"] = 0x0B;
|
||||||
|
code_to_scancode["Minus"] = 0x0C;
|
||||||
|
code_to_scancode["Equal"] = 0x0D;
|
||||||
|
code_to_scancode["Backspace"] = 0x0E;
|
||||||
|
code_to_scancode["Tab"] = 0x0F;
|
||||||
|
code_to_scancode["KeyQ"] = 0x10;
|
||||||
|
code_to_scancode["KeyW"] = 0x11;
|
||||||
|
code_to_scancode["KeyE"] = 0x12;
|
||||||
|
code_to_scancode["KeyR"] = 0x13;
|
||||||
|
code_to_scancode["KeyT"] = 0x14;
|
||||||
|
code_to_scancode["KeyY"] = 0x15;
|
||||||
|
code_to_scancode["KeyU"] = 0x16;
|
||||||
|
code_to_scancode["KeyI"] = 0x17;
|
||||||
|
code_to_scancode["KeyO"] = 0x18;
|
||||||
|
code_to_scancode["KeyP"] = 0x19;
|
||||||
|
code_to_scancode["BracketLeft"] = 0x1A;
|
||||||
|
code_to_scancode["BracketRight"] = 0x1B;
|
||||||
|
code_to_scancode["Enter"] = 0x1C;
|
||||||
|
code_to_scancode["ControlLeft"] = 0x1D;
|
||||||
|
code_to_scancode["KeyA"] = 0x1E;
|
||||||
|
code_to_scancode["KeyS"] = 0x1F;
|
||||||
|
code_to_scancode["KeyD"] = 0x20;
|
||||||
|
code_to_scancode["KeyF"] = 0x21;
|
||||||
|
code_to_scancode["KeyG"] = 0x22;
|
||||||
|
code_to_scancode["KeyH"] = 0x23;
|
||||||
|
code_to_scancode["KeyJ"] = 0x24;
|
||||||
|
code_to_scancode["KeyK"] = 0x25;
|
||||||
|
code_to_scancode["KeyL"] = 0x26;
|
||||||
|
code_to_scancode["Semicolon"] = 0x27;
|
||||||
|
code_to_scancode["Quote"] = 0x28;
|
||||||
|
code_to_scancode["Backquote"] = 0x29;
|
||||||
|
code_to_scancode["ShiftLeft"] = 0x2A;
|
||||||
|
code_to_scancode["Backslash"] = 0x2B;
|
||||||
|
code_to_scancode["KeyZ"] = 0x2C;
|
||||||
|
code_to_scancode["KeyX"] = 0x2D;
|
||||||
|
code_to_scancode["KeyC"] = 0x2E;
|
||||||
|
code_to_scancode["KeyV"] = 0x2F;
|
||||||
|
code_to_scancode["KeyB"] = 0x30;
|
||||||
|
code_to_scancode["KeyN"] = 0x31;
|
||||||
|
code_to_scancode["KeyM"] = 0x32;
|
||||||
|
code_to_scancode["Comma"] = 0x33;
|
||||||
|
code_to_scancode["Period"] = 0x34;
|
||||||
|
code_to_scancode["Slash"] = 0x35;
|
||||||
|
code_to_scancode["ShiftRight"] = 0x36;
|
||||||
|
code_to_scancode["NumpadMultiply"] = 0x37;
|
||||||
|
code_to_scancode["AltLeft"] = 0x38;
|
||||||
|
code_to_scancode["Space"] = 0x39;
|
||||||
|
code_to_scancode["CapsLock"] = 0x3A;
|
||||||
|
code_to_scancode["F1"] = 0x3B;
|
||||||
|
code_to_scancode["F2"] = 0x3C;
|
||||||
|
code_to_scancode["F3"] = 0x3D;
|
||||||
|
code_to_scancode["F4"] = 0x3E;
|
||||||
|
code_to_scancode["F5"] = 0x3F;
|
||||||
|
code_to_scancode["F6"] = 0x40;
|
||||||
|
code_to_scancode["F7"] = 0x41;
|
||||||
|
code_to_scancode["F8"] = 0x42;
|
||||||
|
code_to_scancode["F9"] = 0x43;
|
||||||
|
code_to_scancode["F10"] = 0x44;
|
||||||
|
code_to_scancode["Pause"] = 0x45;
|
||||||
|
code_to_scancode["ScrollLock"] = 0x46;
|
||||||
|
code_to_scancode["Numpad7"] = 0x47;
|
||||||
|
code_to_scancode["Numpad8"] = 0x48;
|
||||||
|
code_to_scancode["Numpad9"] = 0x49;
|
||||||
|
code_to_scancode["NumpadSubtract"] = 0x4A;
|
||||||
|
code_to_scancode["Numpad4"] = 0x4B;
|
||||||
|
code_to_scancode["Numpad5"] = 0x4C;
|
||||||
|
code_to_scancode["Numpad6"] = 0x4D;
|
||||||
|
code_to_scancode["NumpadAdd"] = 0x4E;
|
||||||
|
code_to_scancode["Numpad1"] = 0x4F;
|
||||||
|
code_to_scancode["Numpad2"] = 0x50;
|
||||||
|
code_to_scancode["Numpad3"] = 0x51;
|
||||||
|
code_to_scancode["Numpad0"] = 0x52;
|
||||||
|
code_to_scancode["NumpadDecimal"] = 0x53;
|
||||||
|
code_to_scancode["PrintScreen"] = 0x54;
|
||||||
|
code_to_scancode["IntlBackslash"] = 0x56;
|
||||||
|
code_to_scancode["F11"] = 0x57;
|
||||||
|
code_to_scancode["F12"] = 0x58;
|
||||||
|
code_to_scancode["NumpadEqual"] = 0x59;
|
||||||
|
code_to_scancode["F13"] = 0x64;
|
||||||
|
code_to_scancode["F14"] = 0x65;
|
||||||
|
code_to_scancode["F15"] = 0x66;
|
||||||
|
code_to_scancode["F16"] = 0x67;
|
||||||
|
code_to_scancode["F17"] = 0x68;
|
||||||
|
code_to_scancode["F18"] = 0x69;
|
||||||
|
code_to_scancode["F19"] = 0x6A;
|
||||||
|
code_to_scancode["F20"] = 0x6B;
|
||||||
|
code_to_scancode["F21"] = 0x6C;
|
||||||
|
code_to_scancode["F22"] = 0x6D;
|
||||||
|
code_to_scancode["F23"] = 0x6E;
|
||||||
|
code_to_scancode["KanaMode"] = 0x70;
|
||||||
|
code_to_scancode["IntlRo"] = 0x73;
|
||||||
|
code_to_scancode["F24"] = 0x76;
|
||||||
|
code_to_scancode["Convert"] = 0x79;
|
||||||
|
code_to_scancode["NonConvert"] = 0x7B;
|
||||||
|
code_to_scancode["IntlYen"] = 0x7D;
|
||||||
|
code_to_scancode["NumpadComma"] = 0x7E;
|
||||||
|
code_to_scancode["MediaTrackPrevious"] = 0xE0 | (0x10 << 8);
|
||||||
|
code_to_scancode["MediaTrackNext"] = 0xE0 | (0x19 << 8);
|
||||||
|
code_to_scancode["NumpadEnter"] = 0xE0 | (0x1C << 8);
|
||||||
|
code_to_scancode["ControlRight"] = 0xE0 | (0x1D << 8);
|
||||||
|
code_to_scancode["AudioVolumeMute"] = 0xE0 | (0x20 << 8);
|
||||||
|
code_to_scancode["LaunchApp2"] = 0xE0 | (0x21 << 8);
|
||||||
|
code_to_scancode["MediaPlayPause"] = 0xE0 | (0x22 << 8);
|
||||||
|
code_to_scancode["MediaStop"] = 0xE0 | (0x24 << 8);
|
||||||
|
code_to_scancode["VolumeDown"] = 0xE0 | (0x2E << 8);
|
||||||
|
code_to_scancode["VolumeUp"] = 0xE0 | (0x30 << 8);
|
||||||
|
code_to_scancode["BrowserHome"] = 0xE0 | (0x32 << 8);
|
||||||
|
code_to_scancode["NumpadDivide"] = 0xE0 | (0x35 << 8);
|
||||||
|
code_to_scancode["PrintScreen"] = 0xE0 | (0x37 << 8);
|
||||||
|
code_to_scancode["AltRight"] = 0xE0 | (0x38 << 8);
|
||||||
|
code_to_scancode["NumLock"] = 0xE0 | (0x45 << 8);
|
||||||
|
code_to_scancode["Pause"] = 0xE0 | (0x46 << 8);
|
||||||
|
code_to_scancode["Home"] = 0xE0 | (0x47 << 8);
|
||||||
|
code_to_scancode["ArrowUp"] = 0xE0 | (0x48 << 8);
|
||||||
|
code_to_scancode["PageUp"] = 0xE0 | (0x49 << 8);
|
||||||
|
code_to_scancode["ArrowLeft"] = 0xE0 | (0x4B << 8);
|
||||||
|
code_to_scancode["ArrowRight"] = 0xE0 | (0x4D << 8);
|
||||||
|
code_to_scancode["End"] = 0xE0 | (0x4F << 8);
|
||||||
|
code_to_scancode["ArrowDown"] = 0xE0 | (0x50 << 8);
|
||||||
|
code_to_scancode["PageDown"] = 0xE0 | (0x51 << 8);
|
||||||
|
code_to_scancode["Insert"] = 0xE0 | (0x52 << 8);
|
||||||
|
code_to_scancode["Delete"] = 0xE0 | (0x53 << 8);
|
||||||
|
code_to_scancode["MetaLeft"] = 0xE0 | (0x5B << 8);
|
||||||
|
code_to_scancode["MetaRight"] = 0xE0 | (0x5C << 8);
|
||||||
|
code_to_scancode["ContextMenu"] = 0xE0 | (0x5D << 8);
|
||||||
|
code_to_scancode["Power"] = 0xE0 | (0x5E << 8);
|
||||||
|
code_to_scancode["BrowserSearch"] = 0xE0 | (0x65 << 8);
|
||||||
|
code_to_scancode["BrowserFavorites"] = 0xE0 | (0x66 << 8);
|
||||||
|
code_to_scancode["BrowserRefresh"] = 0xE0 | (0x67 << 8);
|
||||||
|
code_to_scancode["BrowserStop"] = 0xE0 | (0x68 << 8);
|
||||||
|
code_to_scancode["BrowserForward"] = 0xE0 | (0x69 << 8);
|
||||||
|
code_to_scancode["BrowserBack"] = 0xE0 | (0x6A << 8);
|
||||||
|
code_to_scancode["LaunchApp1"] = 0xE0 | (0x6B << 8);
|
||||||
|
code_to_scancode["LaunchMail"] = 0xE0 | (0x6C << 8);
|
||||||
|
code_to_scancode["MediaSelect"] = 0xE0 | (0x6D << 8);
|
||||||
+317
@@ -0,0 +1,317 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { create_rgba_png } from './png.js';
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
import { DEBUG } from './utils.js';
|
||||||
|
import {
|
||||||
|
SpiceMsgCursorInit,
|
||||||
|
SpiceMsgCursorMove,
|
||||||
|
SpiceMsgCursorSet,
|
||||||
|
} from './spicemsg.js';
|
||||||
|
import { SpiceSimulateCursor } from './simulatecursor.js';
|
||||||
|
import { SpiceConn } from './spiceconn.js';
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpiceCursorConn
|
||||||
|
** Drive the Spice Cursor Channel
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function SpiceCursorConn()
|
||||||
|
{
|
||||||
|
SpiceConn.apply(this, arguments);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The largest cursor image Chromium and Firefox will show. */
|
||||||
|
var MAX_CSS_CURSOR = 128;
|
||||||
|
|
||||||
|
SpiceCursorConn.prototype = Object.create(SpiceConn.prototype);
|
||||||
|
SpiceCursorConn.prototype.process_channel_message = function(msg)
|
||||||
|
{
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_INIT)
|
||||||
|
{
|
||||||
|
var cursor_init = new SpiceMsgCursorInit(msg.data);
|
||||||
|
DEBUG > 1 && console.log("SpiceMsgCursorInit");
|
||||||
|
if (this.parent && this.parent.inputs &&
|
||||||
|
this.parent.inputs.mouse_mode == Constants.SPICE_MOUSE_MODE_SERVER)
|
||||||
|
{
|
||||||
|
// FIXME - this imagines that the server actually
|
||||||
|
// provides the current cursor position,
|
||||||
|
// instead of 0,0. As of May 11, 2012,
|
||||||
|
// that assumption was false :-(.
|
||||||
|
this.parent.inputs.mousex = cursor_init.position.x;
|
||||||
|
this.parent.inputs.mousey = cursor_init.position.y;
|
||||||
|
}
|
||||||
|
// FIXME - We don't handle most of the parameters here...
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_SET)
|
||||||
|
{
|
||||||
|
var cursor_set = new SpiceMsgCursorSet(msg.data);
|
||||||
|
DEBUG > 1 && console.log("SpiceMsgCursorSet");
|
||||||
|
if (cursor_set.cursor.flags & Constants.SPICE_CURSOR_FLAGS_NONE)
|
||||||
|
{
|
||||||
|
this.hide_cursor();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cursor_set.cursor.flags > 0)
|
||||||
|
this.log_warn("FIXME: No support for cursor flags " + cursor_set.cursor.flags);
|
||||||
|
|
||||||
|
/* An unconvertible shape is not an unhandled message: returning
|
||||||
|
false here made the base class log a second, misleading
|
||||||
|
"Unknown message type 103" for every one of these. */
|
||||||
|
if (! this.set_cursor(cursor_set.cursor))
|
||||||
|
this.warn_once_per_cursor_type(cursor_set.cursor.header.type);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_MOVE)
|
||||||
|
{
|
||||||
|
/* Only meaningful under server mouse mode, where the server owns
|
||||||
|
the pointer position and we track it for inputs. Under client
|
||||||
|
mode the browser draws the pointer and already knows where it
|
||||||
|
is, so there is nothing to do -- but it is handled either way,
|
||||||
|
rather than reported as unimplemented on every move. */
|
||||||
|
var cursor_move = new SpiceMsgCursorMove(msg.data);
|
||||||
|
if (this.parent && this.parent.inputs &&
|
||||||
|
this.parent.inputs.mouse_mode == Constants.SPICE_MOUSE_MODE_SERVER)
|
||||||
|
{
|
||||||
|
this.parent.inputs.mousex = cursor_move.position.x;
|
||||||
|
this.parent.inputs.mousey = cursor_move.position.y;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_HIDE)
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("SpiceMsgCursorHide");
|
||||||
|
this.hide_cursor();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_TRAIL)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Cursor Trail");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_RESET)
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("SpiceMsgCursorReset");
|
||||||
|
document.getElementById(this.parent.screen_id).style.cursor = "auto";
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_INVAL_ONE)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Cursor Inval One");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_CURSOR_INVAL_ALL)
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("SpiceMsgCursorInvalAll");
|
||||||
|
// FIXME - There may be something useful to do here...
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* One warning per shape type per connection. Windows sends a cursor on
|
||||||
|
nearly every hover, so an unconvertible type used to produce an
|
||||||
|
unbounded stream of identical warnings. */
|
||||||
|
SpiceCursorConn.prototype.warn_once_per_cursor_type = function(type)
|
||||||
|
{
|
||||||
|
if (! this.warned_cursor_types)
|
||||||
|
this.warned_cursor_types = {};
|
||||||
|
if (this.warned_cursor_types[type])
|
||||||
|
return;
|
||||||
|
this.warned_cursor_types[type] = true;
|
||||||
|
this.log_warn("No support for cursor type " + type + "; leaving the cursor as it was.");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SPICE cursor shapes to the RGBA the PNG writer expects.
|
||||||
|
Returns null for a type we cannot convert.
|
||||||
|
Layouts follow spice-gtk's set_cursor() (src/channel-cursor.c). */
|
||||||
|
function cursor_to_rgba(header, data)
|
||||||
|
{
|
||||||
|
var width = header.width;
|
||||||
|
var height = header.height;
|
||||||
|
var pixels = width * height;
|
||||||
|
var u8 = new Uint8Array(data);
|
||||||
|
var rgba = new Uint8Array(pixels * 4);
|
||||||
|
var i, at;
|
||||||
|
|
||||||
|
/* SPICE ships 32bpp shapes as native-endian ARGB, which on every
|
||||||
|
platform we run on is B,G,R,A in memory; PNG wants R,G,B,A. The
|
||||||
|
swap is invisible on the black/white/transparent cursors that
|
||||||
|
dominate, which is why it went unnoticed, but colour cursors came
|
||||||
|
out with red and blue exchanged. */
|
||||||
|
if (header.type == Constants.SPICE_CURSOR_TYPE_ALPHA)
|
||||||
|
{
|
||||||
|
if (u8.length < pixels * 4)
|
||||||
|
return null;
|
||||||
|
for (i = 0, at = 0; i < pixels; i++, at += 4)
|
||||||
|
{
|
||||||
|
rgba[at] = u8[at + 2];
|
||||||
|
rgba[at + 1] = u8[at + 1];
|
||||||
|
rgba[at + 2] = u8[at];
|
||||||
|
rgba[at + 3] = u8[at + 3];
|
||||||
|
}
|
||||||
|
return rgba;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Two 1bpp masks, AND then XOR, each row padded to a byte. The four
|
||||||
|
combinations are the classic ones:
|
||||||
|
AND=1 XOR=0 transparent
|
||||||
|
AND=1 XOR=1 invert the screen
|
||||||
|
AND=0 XOR=0 black
|
||||||
|
AND=0 XOR=1 white
|
||||||
|
A CSS cursor cannot invert what is under it, so those pixels are
|
||||||
|
drawn black -- the same choice every other HTML SPICE/VNC client
|
||||||
|
makes. It is what Windows' I-beam and resize cursors use, and
|
||||||
|
black-on-transparent reads correctly against the light backgrounds
|
||||||
|
those appear over. */
|
||||||
|
if (header.type == Constants.SPICE_CURSOR_TYPE_MONO)
|
||||||
|
{
|
||||||
|
var bpl = (width + 7) >> 3;
|
||||||
|
if (u8.length < bpl * height * 2)
|
||||||
|
return null;
|
||||||
|
for (var y = 0; y < height; y++)
|
||||||
|
{
|
||||||
|
for (var x = 0; x < width; x++)
|
||||||
|
{
|
||||||
|
var byte_at = y * bpl + (x >> 3);
|
||||||
|
var bit = 0x80 >> (x & 7);
|
||||||
|
var and_bit = (u8[byte_at] & bit) != 0;
|
||||||
|
var xor_bit = (u8[bpl * height + byte_at] & bit) != 0;
|
||||||
|
|
||||||
|
at = (y * width + x) * 4;
|
||||||
|
if (and_bit && ! xor_bit)
|
||||||
|
continue; /* transparent; rgba is zeroed */
|
||||||
|
|
||||||
|
var lum = xor_bit && ! and_bit ? 255 : 0;
|
||||||
|
rgba[at] = rgba[at + 1] = rgba[at + 2] = lum;
|
||||||
|
rgba[at + 3] = 255;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return rgba;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 32bpp BGRA followed by a 1bpp AND mask: mask set means the pixel
|
||||||
|
is transparent. spice-gtk renders the "white pixel that is also
|
||||||
|
masked" case as a dim checkerboard rather than a hole, because
|
||||||
|
that combination is how shapes encode invert; do the same. */
|
||||||
|
if (header.type == Constants.SPICE_CURSOR_TYPE_COLOR32)
|
||||||
|
{
|
||||||
|
var mask_at = pixels * 4;
|
||||||
|
if (u8.length < mask_at + ((width + 7) >> 3) * height)
|
||||||
|
return null;
|
||||||
|
for (i = 0, at = 0; i < pixels; i++, at += 4)
|
||||||
|
{
|
||||||
|
var masked = (u8[mask_at + (i >> 3)] & (0x80 >> (i & 7))) != 0;
|
||||||
|
var white = u8[at] == 0xff && u8[at + 1] == 0xff && u8[at + 2] == 0xff;
|
||||||
|
if (masked && white)
|
||||||
|
{
|
||||||
|
var dark = (((i % width) ^ ((i / width) | 0)) & 1) != 0;
|
||||||
|
rgba[at] = rgba[at + 1] = rgba[at + 2] = dark ? 0x30 : 0x50;
|
||||||
|
rgba[at + 3] = dark ? 0xc0 : 0x30;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rgba[at] = u8[at + 2];
|
||||||
|
rgba[at + 1] = u8[at + 1];
|
||||||
|
rgba[at + 2] = u8[at];
|
||||||
|
rgba[at + 3] = masked ? 0 : 255;
|
||||||
|
}
|
||||||
|
return rgba;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* True if the shape was converted and applied. */
|
||||||
|
SpiceCursorConn.prototype.set_cursor = function(cursor)
|
||||||
|
{
|
||||||
|
var rgba = cursor_to_rgba(cursor.header, cursor.data);
|
||||||
|
if (! rgba)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var pngstr = create_rgba_png(cursor.header.width, cursor.header.height, rgba);
|
||||||
|
var curstr = 'url(data:image/png,' + pngstr + ') ' +
|
||||||
|
cursor.header.hot_spot_x + ' ' + cursor.header.hot_spot_y + ", default";
|
||||||
|
var screen = document.getElementById(this.parent.screen_id);
|
||||||
|
screen.style.cursor = 'auto';
|
||||||
|
screen.style.cursor = curstr;
|
||||||
|
/* Chromium and Firefox drop a cursor image over 128 px on a side
|
||||||
|
without saying so: the computed style still reads as the url,
|
||||||
|
and the pointer is the plain arrow, with the arrow's hot spot,
|
||||||
|
so it points somewhere other than the guest's cursor does. A
|
||||||
|
shape that size is drawn under the pointer instead. */
|
||||||
|
var too_large = cursor.header.width > MAX_CSS_CURSOR || cursor.header.height > MAX_CSS_CURSOR;
|
||||||
|
if (too_large || window.getComputedStyle(screen, null).cursor == 'auto')
|
||||||
|
SpiceSimulateCursor.simulate_cursor(this, cursor, screen, pngstr, too_large);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* This cursor took effect natively, so drop the simulated one
|
||||||
|
left by an earlier browser-rejected cursor; the mousemove
|
||||||
|
handler would otherwise keep painting it alongside the real
|
||||||
|
cursor forever. */
|
||||||
|
this.remove_simulated_cursor();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A hidden pointer is hidden whichever way the last shape was shown:
|
||||||
|
the CSS cursor goes to none, and a simulated cursor image is removed,
|
||||||
|
or the mousemove handler would keep painting it. */
|
||||||
|
SpiceCursorConn.prototype.hide_cursor = function()
|
||||||
|
{
|
||||||
|
document.getElementById(this.parent.screen_id).style.cursor = "none";
|
||||||
|
this.remove_simulated_cursor();
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceCursorConn.prototype.remove_simulated_cursor = function()
|
||||||
|
{
|
||||||
|
if (! this.spice_simulated_cursor)
|
||||||
|
return;
|
||||||
|
if (this.spice_simulated_cursor.parentNode)
|
||||||
|
this.spice_simulated_cursor.parentNode.removeChild(this.spice_simulated_cursor);
|
||||||
|
delete this.spice_simulated_cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceCursorConn.prototype.cleanup = function()
|
||||||
|
{
|
||||||
|
/* The screen div outlives the connection: a simulated cursor left in
|
||||||
|
it would sit frozen over the next session, and a guest that ended
|
||||||
|
with its pointer hidden (or a custom shape) would leave the next
|
||||||
|
one without a visible pointer until it sends a shape of its own. */
|
||||||
|
this.remove_simulated_cursor();
|
||||||
|
var screen = document.getElementById(this.parent.screen_id);
|
||||||
|
if (screen)
|
||||||
|
screen.style.cursor = "auto";
|
||||||
|
SpiceConn.prototype.cleanup.call(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpiceCursorConn,
|
||||||
|
};
|
||||||
+1691
File diff suppressed because it is too large
Load Diff
+384
@@ -0,0 +1,384 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** enums.js
|
||||||
|
** 'constants' for Spice
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
export var Constants = {
|
||||||
|
SPICE_MAGIC : "REDQ",
|
||||||
|
SPICE_VERSION_MAJOR : 2,
|
||||||
|
SPICE_VERSION_MINOR : 2,
|
||||||
|
|
||||||
|
SPICE_CONNECT_TIMEOUT : (30 * 1000),
|
||||||
|
|
||||||
|
SPICE_COMMON_CAP_PROTOCOL_AUTH_SELECTION : 0,
|
||||||
|
SPICE_COMMON_CAP_AUTH_SPICE : 1,
|
||||||
|
SPICE_COMMON_CAP_AUTH_SASL : 2,
|
||||||
|
SPICE_COMMON_CAP_MINI_HEADER : 3,
|
||||||
|
|
||||||
|
SPICE_TICKET_KEY_PAIR_LENGTH : 1024,
|
||||||
|
SPICE_TICKET_PUBKEY_BYTES : (1024 / 8 +34), // (SPICE_TICKET_KEY_PAIR_LENGTH / 8 + 34)
|
||||||
|
|
||||||
|
SPICE_LINK_ERR_OK : 0,
|
||||||
|
SPICE_LINK_ERR_ERROR : 1,
|
||||||
|
SPICE_LINK_ERR_INVALID_MAGIC : 2,
|
||||||
|
SPICE_LINK_ERR_INVALID_DATA : 3,
|
||||||
|
SPICE_LINK_ERR_VERSION_MISMATCH : 4,
|
||||||
|
SPICE_LINK_ERR_NEED_SECURED : 5,
|
||||||
|
SPICE_LINK_ERR_NEED_UNSECURED : 6,
|
||||||
|
SPICE_LINK_ERR_PERMISSION_DENIED : 7,
|
||||||
|
SPICE_LINK_ERR_BAD_CONNECTION_ID : 8,
|
||||||
|
SPICE_LINK_ERR_CHANNEL_NOT_AVAILABLE : 9,
|
||||||
|
|
||||||
|
SPICE_MSG_MIGRATE : 1,
|
||||||
|
SPICE_MSG_MIGRATE_DATA : 2,
|
||||||
|
SPICE_MSG_SET_ACK : 3,
|
||||||
|
SPICE_MSG_PING : 4,
|
||||||
|
SPICE_MSG_WAIT_FOR_CHANNELS : 5,
|
||||||
|
SPICE_MSG_DISCONNECTING : 6,
|
||||||
|
SPICE_MSG_NOTIFY : 7,
|
||||||
|
SPICE_MSG_LIST : 8,
|
||||||
|
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_BEGIN : 101,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_CANCEL : 102,
|
||||||
|
SPICE_MSG_MAIN_INIT : 103,
|
||||||
|
SPICE_MSG_MAIN_CHANNELS_LIST : 104,
|
||||||
|
SPICE_MSG_MAIN_MOUSE_MODE : 105,
|
||||||
|
SPICE_MSG_MAIN_MULTI_MEDIA_TIME : 106,
|
||||||
|
SPICE_MSG_MAIN_AGENT_CONNECTED : 107,
|
||||||
|
SPICE_MSG_MAIN_AGENT_DISCONNECTED : 108,
|
||||||
|
SPICE_MSG_MAIN_AGENT_DATA : 109,
|
||||||
|
SPICE_MSG_MAIN_AGENT_TOKEN : 110,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_SWITCH_HOST : 111,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_END : 112,
|
||||||
|
SPICE_MSG_MAIN_NAME : 113,
|
||||||
|
SPICE_MSG_MAIN_UUID : 114,
|
||||||
|
SPICE_MSG_MAIN_AGENT_CONNECTED_TOKENS : 115,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_BEGIN_SEAMLESS : 116,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_DST_SEAMLESS_ACK : 117,
|
||||||
|
SPICE_MSG_MAIN_MIGRATE_DST_SEAMLESS_NACK : 118,
|
||||||
|
SPICE_MSG_END_MAIN : 119,
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
SPICE_MSGC_ACK_SYNC : 1,
|
||||||
|
SPICE_MSGC_ACK : 2,
|
||||||
|
SPICE_MSGC_PONG : 3,
|
||||||
|
SPICE_MSGC_MIGRATE_FLUSH_MARK : 4,
|
||||||
|
SPICE_MSGC_MIGRATE_DATA : 5,
|
||||||
|
SPICE_MSGC_DISCONNECTING : 6,
|
||||||
|
|
||||||
|
|
||||||
|
SPICE_MSGC_MAIN_CLIENT_INFO : 101,
|
||||||
|
SPICE_MSGC_MAIN_MIGRATE_CONNECTED : 102,
|
||||||
|
SPICE_MSGC_MAIN_MIGRATE_CONNECT_ERROR : 103,
|
||||||
|
SPICE_MSGC_MAIN_ATTACH_CHANNELS : 104,
|
||||||
|
SPICE_MSGC_MAIN_MOUSE_MODE_REQUEST : 105,
|
||||||
|
SPICE_MSGC_MAIN_AGENT_START : 106,
|
||||||
|
SPICE_MSGC_MAIN_AGENT_DATA : 107,
|
||||||
|
SPICE_MSGC_MAIN_AGENT_TOKEN : 108,
|
||||||
|
SPICE_MSGC_MAIN_MIGRATE_END : 109,
|
||||||
|
SPICE_MSGC_END_MAIN : 110,
|
||||||
|
|
||||||
|
SPICE_MSG_DISPLAY_MODE : 101,
|
||||||
|
SPICE_MSG_DISPLAY_MARK : 102,
|
||||||
|
SPICE_MSG_DISPLAY_RESET : 103,
|
||||||
|
SPICE_MSG_DISPLAY_COPY_BITS : 104,
|
||||||
|
SPICE_MSG_DISPLAY_INVAL_LIST : 105,
|
||||||
|
SPICE_MSG_DISPLAY_INVAL_ALL_PIXMAPS : 106,
|
||||||
|
SPICE_MSG_DISPLAY_INVAL_PALETTE : 107,
|
||||||
|
SPICE_MSG_DISPLAY_INVAL_ALL_PALETTES: 108,
|
||||||
|
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_CREATE : 122,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_DATA : 123,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_CLIP : 124,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_DESTROY : 125,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_DESTROY_ALL: 126,
|
||||||
|
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_FILL : 302,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_OPAQUE : 303,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_COPY : 304,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_BLEND : 305,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_BLACKNESS : 306,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_WHITENESS : 307,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_INVERS : 308,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_ROP3 : 309,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_STROKE : 310,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_TEXT : 311,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_TRANSPARENT : 312,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_ALPHA_BLEND : 313,
|
||||||
|
SPICE_MSG_DISPLAY_SURFACE_CREATE : 314,
|
||||||
|
SPICE_MSG_DISPLAY_SURFACE_DESTROY : 315,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_DATA_SIZED : 316,
|
||||||
|
SPICE_MSG_DISPLAY_MONITORS_CONFIG : 317,
|
||||||
|
SPICE_MSG_DISPLAY_DRAW_COMPOSITE : 318,
|
||||||
|
SPICE_MSG_DISPLAY_STREAM_ACTIVATE_REPORT : 319,
|
||||||
|
|
||||||
|
SPICE_MSGC_DISPLAY_INIT : 101,
|
||||||
|
SPICE_MSGC_DISPLAY_STREAM_REPORT : 102,
|
||||||
|
|
||||||
|
SPICE_MSG_INPUTS_INIT : 101,
|
||||||
|
SPICE_MSG_INPUTS_KEY_MODIFIERS : 102,
|
||||||
|
|
||||||
|
SPICE_MSG_INPUTS_MOUSE_MOTION_ACK : 111,
|
||||||
|
|
||||||
|
SPICE_MSGC_INPUTS_KEY_DOWN : 101,
|
||||||
|
SPICE_MSGC_INPUTS_KEY_UP : 102,
|
||||||
|
SPICE_MSGC_INPUTS_KEY_MODIFIERS : 103,
|
||||||
|
|
||||||
|
SPICE_MSGC_INPUTS_MOUSE_MOTION : 111,
|
||||||
|
SPICE_MSGC_INPUTS_MOUSE_POSITION : 112,
|
||||||
|
SPICE_MSGC_INPUTS_MOUSE_PRESS : 113,
|
||||||
|
SPICE_MSGC_INPUTS_MOUSE_RELEASE : 114,
|
||||||
|
|
||||||
|
SPICE_MSG_CURSOR_INIT : 101,
|
||||||
|
SPICE_MSG_CURSOR_RESET : 102,
|
||||||
|
SPICE_MSG_CURSOR_SET : 103,
|
||||||
|
SPICE_MSG_CURSOR_MOVE : 104,
|
||||||
|
SPICE_MSG_CURSOR_HIDE : 105,
|
||||||
|
SPICE_MSG_CURSOR_TRAIL : 106,
|
||||||
|
SPICE_MSG_CURSOR_INVAL_ONE : 107,
|
||||||
|
SPICE_MSG_CURSOR_INVAL_ALL : 108,
|
||||||
|
|
||||||
|
SPICE_MSG_PLAYBACK_DATA : 101,
|
||||||
|
SPICE_MSG_PLAYBACK_MODE : 102,
|
||||||
|
SPICE_MSG_PLAYBACK_START : 103,
|
||||||
|
SPICE_MSG_PLAYBACK_STOP : 104,
|
||||||
|
SPICE_MSG_PLAYBACK_VOLUME : 105,
|
||||||
|
SPICE_MSG_PLAYBACK_MUTE : 106,
|
||||||
|
SPICE_MSG_PLAYBACK_LATENCY : 107,
|
||||||
|
|
||||||
|
SPICE_MSG_SPICEVMC_DATA : 101,
|
||||||
|
SPICE_MSG_PORT_INIT : 201,
|
||||||
|
SPICE_MSG_PORT_EVENT : 202,
|
||||||
|
SPICE_MSG_END_PORT : 203,
|
||||||
|
|
||||||
|
SPICE_MSGC_SPICEVMC_DATA : 101,
|
||||||
|
SPICE_MSGC_PORT_EVENT : 201,
|
||||||
|
SPICE_MSGC_END_PORT : 202,
|
||||||
|
|
||||||
|
SPICE_PLAYBACK_CAP_CELT_0_5_1 : 0,
|
||||||
|
SPICE_PLAYBACK_CAP_VOLUME : 1,
|
||||||
|
SPICE_PLAYBACK_CAP_LATENCY : 2,
|
||||||
|
SPICE_PLAYBACK_CAP_OPUS : 3,
|
||||||
|
|
||||||
|
SPICE_MAIN_CAP_SEMI_SEAMLESS_MIGRATE : 0,
|
||||||
|
SPICE_MAIN_CAP_NAME_AND_UUID : 1,
|
||||||
|
SPICE_MAIN_CAP_AGENT_CONNECTED_TOKENS : 2,
|
||||||
|
SPICE_MAIN_CAP_SEAMLESS_MIGRATE : 3,
|
||||||
|
|
||||||
|
SPICE_DISPLAY_CAP_SIZED_STREAM : 0,
|
||||||
|
SPICE_DISPLAY_CAP_MONITORS_CONFIG : 1,
|
||||||
|
SPICE_DISPLAY_CAP_COMPOSITE : 2,
|
||||||
|
SPICE_DISPLAY_CAP_A8_SURFACE : 3,
|
||||||
|
SPICE_DISPLAY_CAP_STREAM_REPORT : 4,
|
||||||
|
SPICE_DISPLAY_CAP_LZ4_COMPRESSION : 5,
|
||||||
|
SPICE_DISPLAY_CAP_PREF_COMPRESSION : 6,
|
||||||
|
SPICE_DISPLAY_CAP_GL_SCANOUT : 7,
|
||||||
|
SPICE_DISPLAY_CAP_MULTI_CODEC : 8,
|
||||||
|
SPICE_DISPLAY_CAP_CODEC_MJPEG : 9,
|
||||||
|
SPICE_DISPLAY_CAP_CODEC_VP8 : 10,
|
||||||
|
|
||||||
|
SPICE_AUDIO_DATA_MODE_INVALID : 0,
|
||||||
|
SPICE_AUDIO_DATA_MODE_RAW : 1,
|
||||||
|
SPICE_AUDIO_DATA_MODE_CELT_0_5_1 : 2,
|
||||||
|
SPICE_AUDIO_DATA_MODE_OPUS : 3,
|
||||||
|
|
||||||
|
SPICE_AUDIO_FMT_INVALID : 0,
|
||||||
|
SPICE_AUDIO_FMT_S16 : 1,
|
||||||
|
|
||||||
|
SPICE_CHANNEL_MAIN : 1,
|
||||||
|
SPICE_CHANNEL_DISPLAY : 2,
|
||||||
|
SPICE_CHANNEL_INPUTS : 3,
|
||||||
|
SPICE_CHANNEL_CURSOR : 4,
|
||||||
|
SPICE_CHANNEL_PLAYBACK : 5,
|
||||||
|
SPICE_CHANNEL_RECORD : 6,
|
||||||
|
SPICE_CHANNEL_TUNNEL : 7,
|
||||||
|
SPICE_CHANNEL_SMARTCARD : 8,
|
||||||
|
SPICE_CHANNEL_USBREDIR : 9,
|
||||||
|
SPICE_CHANNEL_PORT : 10,
|
||||||
|
SPICE_CHANNEL_WEBDAV : 11,
|
||||||
|
|
||||||
|
SPICE_SURFACE_FLAGS_PRIMARY : (1 << 0),
|
||||||
|
|
||||||
|
SPICE_NOTIFY_SEVERITY_INFO : 0,
|
||||||
|
SPICE_NOTIFY_SEVERITY_WARN : 1,
|
||||||
|
SPICE_NOTIFY_SEVERITY_ERROR : 2,
|
||||||
|
|
||||||
|
SPICE_MOUSE_MODE_SERVER : (1 << 0),
|
||||||
|
SPICE_MOUSE_MODE_CLIENT : (1 << 1),
|
||||||
|
SPICE_MOUSE_MODE_MASK : 0x3,
|
||||||
|
|
||||||
|
SPICE_CLIP_TYPE_NONE : 0,
|
||||||
|
SPICE_CLIP_TYPE_RECTS : 1,
|
||||||
|
|
||||||
|
SPICE_IMAGE_SCALE_MODE_INTERPOLATE : 0,
|
||||||
|
SPICE_IMAGE_SCALE_MODE_NEAREST : 1,
|
||||||
|
|
||||||
|
SPICE_IMAGE_TYPE_BITMAP : 0,
|
||||||
|
SPICE_IMAGE_TYPE_QUIC : 1,
|
||||||
|
SPICE_IMAGE_TYPE_RESERVED : 2,
|
||||||
|
SPICE_IMAGE_TYPE_LZ_PLT : 100,
|
||||||
|
SPICE_IMAGE_TYPE_LZ_RGB : 101,
|
||||||
|
SPICE_IMAGE_TYPE_GLZ_RGB : 102,
|
||||||
|
SPICE_IMAGE_TYPE_FROM_CACHE : 103,
|
||||||
|
SPICE_IMAGE_TYPE_SURFACE : 104,
|
||||||
|
SPICE_IMAGE_TYPE_JPEG : 105,
|
||||||
|
SPICE_IMAGE_TYPE_FROM_CACHE_LOSSLESS : 106,
|
||||||
|
SPICE_IMAGE_TYPE_ZLIB_GLZ_RGB : 107,
|
||||||
|
SPICE_IMAGE_TYPE_JPEG_ALPHA : 108,
|
||||||
|
|
||||||
|
SPICE_IMAGE_FLAGS_CACHE_ME : (1 << 0),
|
||||||
|
SPICE_IMAGE_FLAGS_HIGH_BITS_SET : (1 << 1),
|
||||||
|
SPICE_IMAGE_FLAGS_CACHE_REPLACE_ME : (1 << 2),
|
||||||
|
|
||||||
|
SPICE_BITMAP_FLAGS_PAL_CACHE_ME : (1 << 0),
|
||||||
|
SPICE_BITMAP_FLAGS_PAL_FROM_CACHE : (1 << 1),
|
||||||
|
SPICE_BITMAP_FLAGS_TOP_DOWN : (1 << 2),
|
||||||
|
SPICE_BITMAP_FLAGS_MASK : 0x7,
|
||||||
|
|
||||||
|
SPICE_BITMAP_FMT_INVALID : 0,
|
||||||
|
SPICE_BITMAP_FMT_1BIT_LE : 1,
|
||||||
|
SPICE_BITMAP_FMT_1BIT_BE : 2,
|
||||||
|
SPICE_BITMAP_FMT_4BIT_LE : 3,
|
||||||
|
SPICE_BITMAP_FMT_4BIT_BE : 4,
|
||||||
|
SPICE_BITMAP_FMT_8BIT : 5,
|
||||||
|
SPICE_BITMAP_FMT_16BIT : 6,
|
||||||
|
SPICE_BITMAP_FMT_24BIT : 7,
|
||||||
|
SPICE_BITMAP_FMT_32BIT : 8,
|
||||||
|
SPICE_BITMAP_FMT_RGBA : 9,
|
||||||
|
|
||||||
|
|
||||||
|
SPICE_CURSOR_FLAGS_NONE : (1 << 0),
|
||||||
|
SPICE_CURSOR_FLAGS_CACHE_ME : (1 << 1),
|
||||||
|
SPICE_CURSOR_FLAGS_FROM_CACHE : (1 << 2),
|
||||||
|
SPICE_CURSOR_FLAGS_MASK : 0x7,
|
||||||
|
|
||||||
|
SPICE_MOUSE_BUTTON_MASK_LEFT : (1 << 0),
|
||||||
|
SPICE_MOUSE_BUTTON_MASK_MIDDLE : (1 << 1),
|
||||||
|
SPICE_MOUSE_BUTTON_MASK_RIGHT : (1 << 2),
|
||||||
|
SPICE_MOUSE_BUTTON_MASK_MASK : 0x7,
|
||||||
|
|
||||||
|
SPICE_MOUSE_BUTTON_INVALID : 0,
|
||||||
|
SPICE_MOUSE_BUTTON_LEFT : 1,
|
||||||
|
SPICE_MOUSE_BUTTON_MIDDLE : 2,
|
||||||
|
SPICE_MOUSE_BUTTON_RIGHT : 3,
|
||||||
|
SPICE_MOUSE_BUTTON_UP : 4,
|
||||||
|
SPICE_MOUSE_BUTTON_DOWN : 5,
|
||||||
|
|
||||||
|
SPICE_BRUSH_TYPE_NONE : 0,
|
||||||
|
SPICE_BRUSH_TYPE_SOLID : 1,
|
||||||
|
SPICE_BRUSH_TYPE_PATTERN : 2,
|
||||||
|
|
||||||
|
SPICE_SURFACE_FMT_INVALID : 0,
|
||||||
|
SPICE_SURFACE_FMT_1_A : 1,
|
||||||
|
SPICE_SURFACE_FMT_8_A : 8,
|
||||||
|
SPICE_SURFACE_FMT_16_555 : 16,
|
||||||
|
SPICE_SURFACE_FMT_32_xRGB : 32,
|
||||||
|
SPICE_SURFACE_FMT_16_565 : 80,
|
||||||
|
SPICE_SURFACE_FMT_32_ARGB : 96,
|
||||||
|
|
||||||
|
SPICE_ROPD_INVERS_SRC : (1 << 0),
|
||||||
|
SPICE_ROPD_INVERS_BRUSH : (1 << 1),
|
||||||
|
SPICE_ROPD_INVERS_DEST : (1 << 2),
|
||||||
|
SPICE_ROPD_OP_PUT : (1 << 3),
|
||||||
|
SPICE_ROPD_OP_OR : (1 << 4),
|
||||||
|
SPICE_ROPD_OP_AND : (1 << 5),
|
||||||
|
SPICE_ROPD_OP_XOR : (1 << 6),
|
||||||
|
SPICE_ROPD_OP_BLACKNESS : (1 << 7),
|
||||||
|
SPICE_ROPD_OP_WHITENESS : (1 << 8),
|
||||||
|
SPICE_ROPD_OP_INVERS : (1 << 9),
|
||||||
|
SPICE_ROPD_INVERS_RES : (1 << 10),
|
||||||
|
SPICE_ROPD_MASK : 0x7ff,
|
||||||
|
|
||||||
|
LZ_IMAGE_TYPE_INVALID : 0,
|
||||||
|
LZ_IMAGE_TYPE_PLT1_LE : 1,
|
||||||
|
LZ_IMAGE_TYPE_PLT1_BE : 2, // PLT stands for palette
|
||||||
|
LZ_IMAGE_TYPE_PLT4_LE : 3,
|
||||||
|
LZ_IMAGE_TYPE_PLT4_BE : 4,
|
||||||
|
LZ_IMAGE_TYPE_PLT8 : 5,
|
||||||
|
LZ_IMAGE_TYPE_RGB16 : 6,
|
||||||
|
LZ_IMAGE_TYPE_RGB24 : 7,
|
||||||
|
LZ_IMAGE_TYPE_RGB32 : 8,
|
||||||
|
LZ_IMAGE_TYPE_RGBA : 9,
|
||||||
|
LZ_IMAGE_TYPE_XXXA : 10,
|
||||||
|
|
||||||
|
|
||||||
|
SPICE_INPUT_MOTION_ACK_BUNCH : 4,
|
||||||
|
|
||||||
|
|
||||||
|
SPICE_CURSOR_TYPE_ALPHA : 0,
|
||||||
|
SPICE_CURSOR_TYPE_MONO : 1,
|
||||||
|
SPICE_CURSOR_TYPE_COLOR4 : 2,
|
||||||
|
SPICE_CURSOR_TYPE_COLOR8 : 3,
|
||||||
|
SPICE_CURSOR_TYPE_COLOR16 : 4,
|
||||||
|
SPICE_CURSOR_TYPE_COLOR24 : 5,
|
||||||
|
SPICE_CURSOR_TYPE_COLOR32 : 6,
|
||||||
|
|
||||||
|
SPICE_VIDEO_CODEC_TYPE_MJPEG : 1,
|
||||||
|
SPICE_VIDEO_CODEC_TYPE_VP8 : 2,
|
||||||
|
|
||||||
|
SPICE_STREAM_FLAGS_TOP_DOWN : (1 << 0),
|
||||||
|
|
||||||
|
VD_AGENT_PROTOCOL : 1,
|
||||||
|
VD_AGENT_MAX_DATA_SIZE : 2048,
|
||||||
|
|
||||||
|
VD_AGENT_MOUSE_STATE : 1,
|
||||||
|
VD_AGENT_MONITORS_CONFIG : 2,
|
||||||
|
VD_AGENT_REPLY : 3,
|
||||||
|
VD_AGENT_CLIPBOARD : 4,
|
||||||
|
VD_AGENT_DISPLAY_CONFIG : 5,
|
||||||
|
VD_AGENT_ANNOUNCE_CAPABILITIES : 6,
|
||||||
|
VD_AGENT_CLIPBOARD_GRAB : 7,
|
||||||
|
VD_AGENT_CLIPBOARD_REQUEST : 8,
|
||||||
|
VD_AGENT_CLIPBOARD_RELEASE : 9,
|
||||||
|
VD_AGENT_FILE_XFER_START :10,
|
||||||
|
VD_AGENT_FILE_XFER_STATUS :11,
|
||||||
|
VD_AGENT_FILE_XFER_DATA :12,
|
||||||
|
VD_AGENT_CLIENT_DISCONNECTED :13,
|
||||||
|
VD_AGENT_MAX_CLIPBOARD :14,
|
||||||
|
|
||||||
|
VD_AGENT_CAP_MOUSE_STATE : 0,
|
||||||
|
VD_AGENT_CAP_MONITORS_CONFIG : 1,
|
||||||
|
VD_AGENT_CAP_REPLY : 2,
|
||||||
|
VD_AGENT_CAP_CLIPBOARD : 3,
|
||||||
|
VD_AGENT_CAP_DISPLAY_CONFIG : 4,
|
||||||
|
VD_AGENT_CAP_CLIPBOARD_BY_DEMAND : 5,
|
||||||
|
VD_AGENT_CAP_CLIPBOARD_SELECTION : 6,
|
||||||
|
VD_AGENT_CAP_SPARSE_MONITORS_CONFIG : 7,
|
||||||
|
VD_AGENT_CAP_GUEST_LINEEND_LF : 8,
|
||||||
|
VD_AGENT_CAP_GUEST_LINEEND_CRLF : 9,
|
||||||
|
VD_AGENT_CAP_MAX_CLIPBOARD : 10,
|
||||||
|
VD_AGENT_END_CAP : 11,
|
||||||
|
|
||||||
|
VD_AGENT_FILE_XFER_STATUS_CAN_SEND_DATA : 0,
|
||||||
|
VD_AGENT_FILE_XFER_STATUS_CANCELLED : 1,
|
||||||
|
VD_AGENT_FILE_XFER_STATUS_ERROR : 2,
|
||||||
|
VD_AGENT_FILE_XFER_STATUS_SUCCESS : 3,
|
||||||
|
|
||||||
|
VD_AGENT_CLIPBOARD_NONE : 0,
|
||||||
|
VD_AGENT_CLIPBOARD_UTF8_TEXT : 1,
|
||||||
|
VD_AGENT_CLIPBOARD_IMAGE_PNG : 2,
|
||||||
|
VD_AGENT_CLIPBOARD_IMAGE_BMP : 3,
|
||||||
|
VD_AGENT_CLIPBOARD_IMAGE_TIFF : 4,
|
||||||
|
VD_AGENT_CLIPBOARD_IMAGE_JPG : 5,
|
||||||
|
};
|
||||||
+94
@@ -0,0 +1,94 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2014 Red Hat, Inc.
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
function SpiceFileXferTask(id, file)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
this.file = file;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceFileXferTask.prototype.create_progressbar = function()
|
||||||
|
{
|
||||||
|
var _this = this;
|
||||||
|
var cancel = document.createElement("input");
|
||||||
|
this.progressbar_container = document.createElement("div");
|
||||||
|
this.progressbar = document.createElement("progress");
|
||||||
|
|
||||||
|
cancel.type = 'button';
|
||||||
|
cancel.value = 'Cancel';
|
||||||
|
cancel.style.float = 'right';
|
||||||
|
cancel.onclick = function()
|
||||||
|
{
|
||||||
|
_this.cancelled = true;
|
||||||
|
_this.remove_progressbar();
|
||||||
|
};
|
||||||
|
|
||||||
|
this.progressbar.setAttribute('max', this.file.size);
|
||||||
|
this.progressbar.setAttribute('value', 0);
|
||||||
|
this.progressbar.style.width = '100%';
|
||||||
|
this.progressbar.style.margin = '4px auto';
|
||||||
|
this.progressbar.style.display = 'inline-block';
|
||||||
|
this.progressbar_container.style.width = '90%';
|
||||||
|
this.progressbar_container.style.margin = 'auto';
|
||||||
|
this.progressbar_container.style.padding = '4px';
|
||||||
|
this.progressbar_container.textContent = this.file.name;
|
||||||
|
this.progressbar_container.appendChild(cancel);
|
||||||
|
this.progressbar_container.appendChild(this.progressbar);
|
||||||
|
document.getElementById('spice-xfer-area').appendChild(this.progressbar_container);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceFileXferTask.prototype.update_progressbar = function(value)
|
||||||
|
{
|
||||||
|
this.progressbar.setAttribute('value', value);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceFileXferTask.prototype.remove_progressbar = function()
|
||||||
|
{
|
||||||
|
if (this.progressbar_container && this.progressbar_container.parentNode)
|
||||||
|
this.progressbar_container.parentNode.removeChild(this.progressbar_container);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_file_dragover(e)
|
||||||
|
{
|
||||||
|
e.stopPropagation();
|
||||||
|
e.preventDefault();
|
||||||
|
e.dataTransfer.dropEffect = 'copy';
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_file_drop(e)
|
||||||
|
{
|
||||||
|
var sc = window.spice_connection;
|
||||||
|
var files = e.dataTransfer.files;
|
||||||
|
|
||||||
|
e.stopPropagation();
|
||||||
|
e.preventDefault();
|
||||||
|
for (var i = files.length - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
if (files[i].type) // do not copy a directory
|
||||||
|
sc.file_xfer_start(files[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpiceFileXferTask,
|
||||||
|
handle_file_dragover,
|
||||||
|
handle_file_drop,
|
||||||
|
};
|
||||||
+504
@@ -0,0 +1,504 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as Messages from './spicemsg.js';
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
import { KeyNames } from './atKeynames.js';
|
||||||
|
import { SpiceConn } from './spiceconn.js';
|
||||||
|
import { DEBUG } from './utils.js';
|
||||||
|
import { code_to_scancode } from './code_to_scancode.js';
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** Modifier Keystates
|
||||||
|
** These need to be tracked because focus in and out can get the keyboard
|
||||||
|
** out of sync.
|
||||||
|
**------------------------------------------------------------------------*/
|
||||||
|
var Shift_state = -1;
|
||||||
|
var Ctrl_state = -1;
|
||||||
|
var Alt_state = -1;
|
||||||
|
var Meta_state = -1;
|
||||||
|
|
||||||
|
/* Extended (0xE0-prefixed) scancodes for the Meta/Super keys, matching
|
||||||
|
the values in the scancode maps (utils.js / code_to_scancode.js). */
|
||||||
|
var META_L_SCAN = 0xE0 | (0x5B << 8);
|
||||||
|
var META_R_SCAN = 0xE0 | (0x5C << 8);
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpiceInputsConn
|
||||||
|
** Drive the Spice Inputs channel (e.g. mouse + keyboard)
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function SpiceInputsConn()
|
||||||
|
{
|
||||||
|
SpiceConn.apply(this, arguments);
|
||||||
|
|
||||||
|
this.mousex = undefined;
|
||||||
|
this.mousey = undefined;
|
||||||
|
this.button_state = 0;
|
||||||
|
this.waiting_for_ack = 0;
|
||||||
|
|
||||||
|
/* Modifier tracking is module state; a page that tears down one
|
||||||
|
connection and creates another must not inherit the previous
|
||||||
|
session's held keys. */
|
||||||
|
Shift_state = Ctrl_state = Alt_state = Meta_state = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceInputsConn.prototype = Object.create(SpiceConn.prototype);
|
||||||
|
SpiceInputsConn.prototype.process_channel_message = function(msg)
|
||||||
|
{
|
||||||
|
if (msg.type == Constants.SPICE_MSG_INPUTS_INIT)
|
||||||
|
{
|
||||||
|
var inputs_init = new Messages.SpiceMsgInputsInit(msg.data);
|
||||||
|
this.keyboard_modifiers = inputs_init.keyboard_modifiers;
|
||||||
|
DEBUG > 1 && console.log("MsgInputsInit - modifier " + this.keyboard_modifiers);
|
||||||
|
// FIXME - We don't do anything with the keyboard modifiers...
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (msg.type == Constants.SPICE_MSG_INPUTS_KEY_MODIFIERS)
|
||||||
|
{
|
||||||
|
var key = new Messages.SpiceMsgInputsKeyModifiers(msg.data);
|
||||||
|
this.keyboard_modifiers = key.keyboard_modifiers;
|
||||||
|
DEBUG > 1 && console.log("MsgInputsKeyModifiers - modifier " + this.keyboard_modifiers);
|
||||||
|
// FIXME - We don't do anything with the keyboard modifiers...
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (msg.type == Constants.SPICE_MSG_INPUTS_MOUSE_MOTION_ACK)
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("mouse motion ack");
|
||||||
|
this.waiting_for_ack -= Constants.SPICE_INPUT_MOTION_ACK_BUNCH;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function handle_mousemove(e)
|
||||||
|
{
|
||||||
|
/* Only build the message once we know it will be sent; mousemove can
|
||||||
|
fire hundreds of times a second and the discarded-motion path was
|
||||||
|
paying for two allocations and a serialize per event. */
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
{
|
||||||
|
if (this.sc.inputs.waiting_for_ack < (2 * Constants.SPICE_INPUT_MOTION_ACK_BUNCH))
|
||||||
|
{
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
var move;
|
||||||
|
if (this.sc.mouse_mode == Constants.SPICE_MOUSE_MODE_CLIENT)
|
||||||
|
{
|
||||||
|
move = new Messages.SpiceMsgcMousePosition(this.sc, e)
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_POSITION, move);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
move = new Messages.SpiceMsgcMouseMotion(this.sc, e)
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_MOTION, move);
|
||||||
|
}
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
this.sc.inputs.waiting_for_ack++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DEBUG > 0 && this.sc.log_info("Discarding mouse motion");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.sc && this.sc.cursor && this.sc.cursor.spice_simulated_cursor)
|
||||||
|
{
|
||||||
|
/* The image sits inside the screen element, so it is placed in
|
||||||
|
that element's own coordinates: the pointer's position within
|
||||||
|
its box, undone for any CSS scale on it. Page coordinates put
|
||||||
|
it off by the screen's offset, and by the scale, on any page
|
||||||
|
that does not draw the screen at the top-left corner. */
|
||||||
|
var sim = this.sc.cursor.spice_simulated_cursor;
|
||||||
|
var screen = sim.spice_screen;
|
||||||
|
var rect = screen.getBoundingClientRect();
|
||||||
|
var scale_x = screen.offsetWidth ? rect.width / screen.offsetWidth : 1;
|
||||||
|
var scale_y = screen.offsetHeight ? rect.height / screen.offsetHeight : 1;
|
||||||
|
sim.style.display = 'block';
|
||||||
|
sim.style.left = (e.clientX - rect.left) / scale_x - sim.spice_hot_x + 'px';
|
||||||
|
sim.style.top = (e.clientY - rect.top) / scale_y - sim.spice_hot_y + 'px';
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_mousedown(e)
|
||||||
|
{
|
||||||
|
var press = new Messages.SpiceMsgcMousePress(this.sc, e)
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_PRESS, press);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_contextmenu(e)
|
||||||
|
{
|
||||||
|
e.preventDefault();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_mouseup(e)
|
||||||
|
{
|
||||||
|
var release = new Messages.SpiceMsgcMouseRelease(this.sc, e)
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_RELEASE, release);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_mousewheel(e)
|
||||||
|
{
|
||||||
|
var press = new Messages.SpiceMsgcMousePress;
|
||||||
|
var release = new Messages.SpiceMsgcMouseRelease;
|
||||||
|
if (e.deltaY < 0)
|
||||||
|
press.button = release.button = Constants.SPICE_MOUSE_BUTTON_UP;
|
||||||
|
else
|
||||||
|
press.button = release.button = Constants.SPICE_MOUSE_BUTTON_DOWN;
|
||||||
|
press.buttons_state = 0;
|
||||||
|
release.buttons_state = 0;
|
||||||
|
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_PRESS, press);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_MOUSE_RELEASE, release);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_keydown(e)
|
||||||
|
{
|
||||||
|
var key = new Messages.SpiceMsgcKeyDown(e)
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
check_and_update_modifiers(e, key.code, this.sc);
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_DOWN, key);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_keyup(e)
|
||||||
|
{
|
||||||
|
var key = new Messages.SpiceMsgcKeyUp(e)
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
check_and_update_modifiers(e, key.code, this.sc);
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_UP, key);
|
||||||
|
if (this.sc && this.sc.inputs && this.sc.inputs.state === "ready")
|
||||||
|
this.sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendCtrlAltDel(sc)
|
||||||
|
{
|
||||||
|
if (sc && sc.inputs && sc.inputs.state === "ready"){
|
||||||
|
var key = new Messages.SpiceMsgcKeyDown();
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
|
||||||
|
update_modifier(true, KeyNames.KEY_LCtrl, sc);
|
||||||
|
update_modifier(true, KeyNames.KEY_Alt, sc);
|
||||||
|
|
||||||
|
key.code = KeyNames.KEY_KP_Decimal;
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_DOWN, key);
|
||||||
|
sc.inputs.send_msg(msg);
|
||||||
|
/* The server forwards scancode bytes verbatim, so the release must
|
||||||
|
carry the break bit itself, as keycode_to_end_scan does. */
|
||||||
|
key.code = 0x80 | KeyNames.KEY_KP_Decimal;
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_UP, key);
|
||||||
|
sc.inputs.send_msg(msg);
|
||||||
|
|
||||||
|
/* Release unless the key is positively known to be held: the state
|
||||||
|
starts as the -1 sentinel (no key seen yet), and -1 == false is
|
||||||
|
false, so testing against false left the synthetic Ctrl+Alt held
|
||||||
|
in the guest whenever this ran before the first real keydown. */
|
||||||
|
if(Ctrl_state !== true) update_modifier(false, KeyNames.KEY_LCtrl, sc);
|
||||||
|
if(Alt_state !== true) update_modifier(false, KeyNames.KEY_Alt, sc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function update_modifier(state, code, sc)
|
||||||
|
{
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
if (!state)
|
||||||
|
{
|
||||||
|
var key = new Messages.SpiceMsgcKeyUp()
|
||||||
|
/* Extended (two-byte) scancodes carry the break bit in their second
|
||||||
|
byte, as keycode_to_end_scan does; 0x80 would land in the 0xE0
|
||||||
|
prefix and corrupt it. */
|
||||||
|
key.code = code < 0x100 ? (0x80|code) : (0x8000|code);
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_UP, key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var key = new Messages.SpiceMsgcKeyDown()
|
||||||
|
key.code = code;
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_DOWN, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.inputs.send_msg(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
function check_and_update_modifiers(e, code, sc)
|
||||||
|
{
|
||||||
|
if (Shift_state === -1)
|
||||||
|
{
|
||||||
|
Shift_state = e.shiftKey;
|
||||||
|
Ctrl_state = e.ctrlKey;
|
||||||
|
Alt_state = e.altKey;
|
||||||
|
Meta_state = e.metaKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (code === KeyNames.KEY_ShiftL)
|
||||||
|
Shift_state = true;
|
||||||
|
else if (code === KeyNames.KEY_Alt)
|
||||||
|
Alt_state = true;
|
||||||
|
else if (code === KeyNames.KEY_LCtrl)
|
||||||
|
Ctrl_state = true;
|
||||||
|
else if (code === META_L_SCAN || code === META_R_SCAN)
|
||||||
|
Meta_state = true;
|
||||||
|
else if (code === (0x80|KeyNames.KEY_ShiftL))
|
||||||
|
Shift_state = false;
|
||||||
|
else if (code === (0x80|KeyNames.KEY_Alt))
|
||||||
|
Alt_state = false;
|
||||||
|
else if (code === (0x80|KeyNames.KEY_LCtrl))
|
||||||
|
Ctrl_state = false;
|
||||||
|
else if (code === (0x8000|META_L_SCAN) || code === (0x8000|META_R_SCAN))
|
||||||
|
Meta_state = false;
|
||||||
|
|
||||||
|
if (sc && sc.inputs && sc.inputs.state === "ready")
|
||||||
|
{
|
||||||
|
if (Shift_state != e.shiftKey)
|
||||||
|
{
|
||||||
|
console.log("Shift state out of sync");
|
||||||
|
update_modifier(e.shiftKey, KeyNames.KEY_ShiftL, sc);
|
||||||
|
Shift_state = e.shiftKey;
|
||||||
|
}
|
||||||
|
if (Alt_state != e.altKey)
|
||||||
|
{
|
||||||
|
console.log("Alt state out of sync");
|
||||||
|
update_modifier(e.altKey, KeyNames.KEY_Alt, sc);
|
||||||
|
Alt_state = e.altKey;
|
||||||
|
}
|
||||||
|
if (Ctrl_state != e.ctrlKey)
|
||||||
|
{
|
||||||
|
console.log("Ctrl state out of sync");
|
||||||
|
update_modifier(e.ctrlKey, KeyNames.KEY_LCtrl, sc);
|
||||||
|
Ctrl_state = e.ctrlKey;
|
||||||
|
}
|
||||||
|
if (Meta_state != e.metaKey)
|
||||||
|
{
|
||||||
|
console.log("Meta state out of sync");
|
||||||
|
update_modifier(e.metaKey, META_L_SCAN, sc);
|
||||||
|
Meta_state = e.metaKey;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** typeText
|
||||||
|
** Deliver a string to the guest as synthetic keystrokes on the inputs
|
||||||
|
** channel. The inputs channel speaks PC scancodes — positions on a physical
|
||||||
|
** keyboard, not characters — so this maps each character to the key that
|
||||||
|
** produces it on a US layout. A guest configured for another keymap will
|
||||||
|
** type different characters, and anything a US layout cannot produce with
|
||||||
|
** at most Shift (AltGr combinations, dead keys, non-ASCII) cannot be
|
||||||
|
** expressed at all; such characters are skipped and reported back.
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
/* character → [KeyboardEvent.code, needs-shift] on a US layout. */
|
||||||
|
var US_TYPEABLE = (function ()
|
||||||
|
{
|
||||||
|
var map = {};
|
||||||
|
var i;
|
||||||
|
|
||||||
|
var letters = "abcdefghijklmnopqrstuvwxyz";
|
||||||
|
for (i = 0; i < letters.length; i++)
|
||||||
|
{
|
||||||
|
var code = "Key" + letters[i].toUpperCase();
|
||||||
|
map[letters[i]] = [code, false];
|
||||||
|
map[letters[i].toUpperCase()] = [code, true];
|
||||||
|
}
|
||||||
|
|
||||||
|
var digits = "1234567890";
|
||||||
|
var shifted_digits = "!@#$%^&*()";
|
||||||
|
for (i = 0; i < digits.length; i++)
|
||||||
|
{
|
||||||
|
map[digits[i]] = ["Digit" + digits[i], false];
|
||||||
|
map[shifted_digits[i]] = ["Digit" + digits[i], true];
|
||||||
|
}
|
||||||
|
|
||||||
|
var punctuation = [
|
||||||
|
["-", "_", "Minus"],
|
||||||
|
["=", "+", "Equal"],
|
||||||
|
["[", "{", "BracketLeft"],
|
||||||
|
["]", "}", "BracketRight"],
|
||||||
|
["\\", "|", "Backslash"],
|
||||||
|
[";", ":", "Semicolon"],
|
||||||
|
["'", "\"", "Quote"],
|
||||||
|
[",", "<", "Comma"],
|
||||||
|
[".", ">", "Period"],
|
||||||
|
["/", "?", "Slash"],
|
||||||
|
["`", "~", "Backquote"],
|
||||||
|
];
|
||||||
|
for (i = 0; i < punctuation.length; i++)
|
||||||
|
{
|
||||||
|
map[punctuation[i][0]] = [punctuation[i][2], false];
|
||||||
|
map[punctuation[i][1]] = [punctuation[i][2], true];
|
||||||
|
}
|
||||||
|
|
||||||
|
map[" "] = ["Space", false];
|
||||||
|
map["\n"] = ["Enter", false];
|
||||||
|
map["\t"] = ["Tab", false];
|
||||||
|
return map;
|
||||||
|
})();
|
||||||
|
|
||||||
|
function send_scancode(sc, scancode, down)
|
||||||
|
{
|
||||||
|
var msg = new Messages.SpiceMiniData();
|
||||||
|
var key;
|
||||||
|
if (down)
|
||||||
|
{
|
||||||
|
key = new Messages.SpiceMsgcKeyDown();
|
||||||
|
key.code = scancode;
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_DOWN, key);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
key = new Messages.SpiceMsgcKeyUp();
|
||||||
|
/* Break code; every key this file types is a one-byte scancode. */
|
||||||
|
key.code = 0x80 | scancode;
|
||||||
|
msg.build_msg(Constants.SPICE_MSGC_INPUTS_KEY_UP, key);
|
||||||
|
}
|
||||||
|
sc.inputs.send_msg(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A keystroke is a press and a release separated by time. Sending both in
|
||||||
|
the same tick, as this once did, makes a guest drop characters: Windows in
|
||||||
|
particular discards a key whose break code arrives in the same instant as
|
||||||
|
its make code, and the loss is silent and intermittent — a pasted URL or
|
||||||
|
password arrives subtly wrong. So hold each key down, and give Shift its
|
||||||
|
own settle time either side, before moving on. */
|
||||||
|
var KEY_HOLD_MS = 12;
|
||||||
|
var SHIFT_SETTLE_MS = 12;
|
||||||
|
|
||||||
|
/* A server-side close leaves the channel's state at "ready", since the
|
||||||
|
close handler only reports; the socket itself says whether keystrokes
|
||||||
|
can still reach the guest. */
|
||||||
|
function inputs_live(sc)
|
||||||
|
{
|
||||||
|
return sc && sc.inputs && sc.inputs.state === "ready" &&
|
||||||
|
sc.inputs.ws && sc.inputs.ws.readyState === WebSocket.OPEN;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Resolves to { typed, skipped, aborted }: how many characters went out,
|
||||||
|
which distinct characters had no US-layout key, and whether the inputs
|
||||||
|
channel died mid-string. delay_ms is the gap between characters, which
|
||||||
|
paces a large paste so a guest's keyboard buffer is not flooded. */
|
||||||
|
function typeText(sc, text, delay_ms)
|
||||||
|
{
|
||||||
|
var delay = typeof delay_ms === 'number' ? delay_ms : 25;
|
||||||
|
/* A CRLF must land as a single Enter, not Enter twice. */
|
||||||
|
var chars = text.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
||||||
|
var skipped = [];
|
||||||
|
var typed = 0;
|
||||||
|
var at = 0;
|
||||||
|
|
||||||
|
return new Promise(function (resolve)
|
||||||
|
{
|
||||||
|
function step()
|
||||||
|
{
|
||||||
|
if (! inputs_live(sc))
|
||||||
|
{
|
||||||
|
resolve({ typed: typed, skipped: skipped, aborted: at < chars.length });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (at >= chars.length)
|
||||||
|
{
|
||||||
|
resolve({ typed: typed, skipped: skipped, aborted: false });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var ch = chars[at++];
|
||||||
|
var entry = US_TYPEABLE[ch];
|
||||||
|
var scancode = entry && code_to_scancode[entry[0]];
|
||||||
|
if (!scancode)
|
||||||
|
{
|
||||||
|
if (skipped.indexOf(ch) === -1)
|
||||||
|
skipped.push(ch);
|
||||||
|
step();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Each phase is [what to send, how long to wait after it], run in
|
||||||
|
order; the guest sees a press, a hold, and a release. */
|
||||||
|
var shifted = entry[1];
|
||||||
|
var phases = [];
|
||||||
|
if (shifted)
|
||||||
|
phases.push([function () { send_scancode(sc, KeyNames.KEY_ShiftL, true); }, SHIFT_SETTLE_MS]);
|
||||||
|
phases.push([function () { send_scancode(sc, scancode, true); }, KEY_HOLD_MS]);
|
||||||
|
phases.push([function () { send_scancode(sc, scancode, false); }, shifted ? SHIFT_SETTLE_MS : delay]);
|
||||||
|
if (shifted)
|
||||||
|
phases.push([function () { send_scancode(sc, KeyNames.KEY_ShiftL, false); }, delay]);
|
||||||
|
typed++;
|
||||||
|
|
||||||
|
var phase = 0;
|
||||||
|
(function run()
|
||||||
|
{
|
||||||
|
if (phase >= phases.length)
|
||||||
|
{
|
||||||
|
step();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
/* The channel can die mid-character; stop rather than send a
|
||||||
|
press whose release will never follow. */
|
||||||
|
if (!(sc && sc.inputs && sc.inputs.state === "ready"))
|
||||||
|
{
|
||||||
|
resolve({ typed: typed - 1, skipped: skipped, aborted: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var current = phases[phase++];
|
||||||
|
current[0]();
|
||||||
|
window.setTimeout(run, current[1]);
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
step();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpiceInputsConn,
|
||||||
|
handle_mousemove,
|
||||||
|
handle_mousedown,
|
||||||
|
handle_contextmenu,
|
||||||
|
handle_mouseup,
|
||||||
|
handle_mousewheel,
|
||||||
|
handle_keydown,
|
||||||
|
handle_keyup,
|
||||||
|
sendCtrlAltDel,
|
||||||
|
typeText,
|
||||||
|
};
|
||||||
Vendored
+181
@@ -0,0 +1,181 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** lz.js
|
||||||
|
** Functions for handling SPICE_IMAGE_TYPE_LZ_RGB
|
||||||
|
** Adapted from lz.c .
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function lz_rgb32_decompress(in_buf, at, out_buf, type, default_alpha)
|
||||||
|
{
|
||||||
|
var encoder = at;
|
||||||
|
var op = 0;
|
||||||
|
var ctrl;
|
||||||
|
var i = 0;
|
||||||
|
|
||||||
|
for (ctrl = in_buf[encoder++]; (op * 4) < out_buf.length; ctrl = in_buf[encoder++])
|
||||||
|
{
|
||||||
|
var ref = op;
|
||||||
|
var len = ctrl >> 5;
|
||||||
|
var ofs = (ctrl & 31) << 8;
|
||||||
|
|
||||||
|
if (ctrl >= 32) {
|
||||||
|
|
||||||
|
var code;
|
||||||
|
len--;
|
||||||
|
|
||||||
|
if (len == 7 - 1) {
|
||||||
|
do {
|
||||||
|
code = in_buf[encoder++];
|
||||||
|
len += code;
|
||||||
|
} while (code == 255);
|
||||||
|
}
|
||||||
|
code = in_buf[encoder++];
|
||||||
|
ofs += code;
|
||||||
|
|
||||||
|
|
||||||
|
if (code == 255) {
|
||||||
|
if ((ofs - code) == (31 << 8)) {
|
||||||
|
ofs = in_buf[encoder++] << 8;
|
||||||
|
ofs += in_buf[encoder++];
|
||||||
|
ofs += 8191;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
len += 1;
|
||||||
|
if (type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
len += 2;
|
||||||
|
|
||||||
|
ofs += 1;
|
||||||
|
|
||||||
|
ref -= ofs;
|
||||||
|
if (ref == (op - 1)) {
|
||||||
|
var b = ref;
|
||||||
|
for (; len; --len) {
|
||||||
|
if (type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 3] = out_buf[(b*4)+3];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
out_buf[(op*4) + i] = out_buf[(b*4)+i];
|
||||||
|
}
|
||||||
|
op++;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (; len; --len) {
|
||||||
|
if (type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 3] = out_buf[(ref*4)+3];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (i = 0; i < 4; i++)
|
||||||
|
out_buf[(op*4) + i] = out_buf[(ref*4)+i];
|
||||||
|
}
|
||||||
|
op++; ref++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ctrl++;
|
||||||
|
|
||||||
|
if (type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 3] = in_buf[encoder++];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 0] = in_buf[encoder + 2];
|
||||||
|
out_buf[(op*4) + 1] = in_buf[encoder + 1];
|
||||||
|
out_buf[(op*4) + 2] = in_buf[encoder + 0];
|
||||||
|
if (default_alpha)
|
||||||
|
out_buf[(op*4) + 3] = 255;
|
||||||
|
encoder += 3;
|
||||||
|
}
|
||||||
|
op++;
|
||||||
|
|
||||||
|
|
||||||
|
for (--ctrl; ctrl; ctrl--) {
|
||||||
|
if (type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 3] = in_buf[encoder++];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
out_buf[(op*4) + 0] = in_buf[encoder + 2];
|
||||||
|
out_buf[(op*4) + 1] = in_buf[encoder + 1];
|
||||||
|
out_buf[(op*4) + 2] = in_buf[encoder + 0];
|
||||||
|
if (default_alpha)
|
||||||
|
out_buf[(op*4) + 3] = 255;
|
||||||
|
encoder += 3;
|
||||||
|
}
|
||||||
|
op++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return encoder - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
function flip_image_data(img)
|
||||||
|
{
|
||||||
|
var wb = img.width * 4;
|
||||||
|
var h = img.height;
|
||||||
|
var temp_h = h;
|
||||||
|
var buff = new Uint8Array(img.width * img.height * 4);
|
||||||
|
while (temp_h--)
|
||||||
|
{
|
||||||
|
buff.set(img.data.subarray(temp_h * wb, (temp_h + 1) * wb), (h - temp_h - 1) * wb);
|
||||||
|
}
|
||||||
|
img.data.set(buff);
|
||||||
|
}
|
||||||
|
|
||||||
|
function convert_spice_lz_to_web(context, lz_image)
|
||||||
|
{
|
||||||
|
var at;
|
||||||
|
if (lz_image.type === Constants.LZ_IMAGE_TYPE_RGB32 || lz_image.type === Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
{
|
||||||
|
var u8 = new Uint8Array(lz_image.data);
|
||||||
|
var ret = context.createImageData(lz_image.width, lz_image.height);
|
||||||
|
|
||||||
|
at = lz_rgb32_decompress(u8, 0, ret.data, Constants.LZ_IMAGE_TYPE_RGB32, lz_image.type != Constants.LZ_IMAGE_TYPE_RGBA);
|
||||||
|
if (!lz_image.top_down)
|
||||||
|
flip_image_data(ret);
|
||||||
|
|
||||||
|
if (lz_image.type == Constants.LZ_IMAGE_TYPE_RGBA)
|
||||||
|
lz_rgb32_decompress(u8, at, ret.data, Constants.LZ_IMAGE_TYPE_RGBA, false);
|
||||||
|
}
|
||||||
|
else if (lz_image.type === Constants.LZ_IMAGE_TYPE_XXXA)
|
||||||
|
{
|
||||||
|
var u8 = new Uint8Array(lz_image.data);
|
||||||
|
var ret = context.createImageData(lz_image.width, lz_image.height);
|
||||||
|
lz_rgb32_decompress(u8, 0, ret.data, Constants.LZ_IMAGE_TYPE_RGBA, false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return undefined;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
convert_spice_lz_to_web,
|
||||||
|
};
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
// Types for what Ham Lab uses of the vendored spice-html5 (plain JavaScript beside this file).
|
||||||
|
export class SpiceMainConn {
|
||||||
|
constructor(opts: { uri: string; screen_id: string; password?: string; message_id?: string; dump_id?: string; onerror?: (e: Event | Error) => void; onsuccess?: () => void; onagent?: (sc: SpiceMainConn) => void })
|
||||||
|
stop(msg?: string): void
|
||||||
|
agent_connected?: boolean
|
||||||
|
}
|
||||||
|
export function resize_helper(sc: SpiceMainConn): void
|
||||||
|
export function handle_resize(e?: Event): void
|
||||||
|
export function sendCtrlAltDel(sc: SpiceMainConn): void
|
||||||
+669
@@ -0,0 +1,669 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as Messages from './spicemsg.js';
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
import { SpiceCursorConn } from './cursor.js';
|
||||||
|
import { SpiceConn } from './spiceconn.js';
|
||||||
|
import { DEBUG } from './utils.js';
|
||||||
|
import { SpiceFileXferTask } from './filexfer.js';
|
||||||
|
import { SpiceInputsConn, sendCtrlAltDel, typeText } from './inputs.js';
|
||||||
|
import { SpiceDisplayConn } from './display.js';
|
||||||
|
import { SpicePlaybackConn } from './playback.js';
|
||||||
|
import { SpicePortConn } from './port.js';
|
||||||
|
import { handle_file_dragover, handle_file_drop } from './filexfer.js';
|
||||||
|
import { resize_helper, handle_resize } from './resize.js';
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpiceMainConn
|
||||||
|
** This is the master Javascript class for establishing and
|
||||||
|
** managing a connection to a Spice Server.
|
||||||
|
**
|
||||||
|
** Invocation: You must pass an object with properties as follows:
|
||||||
|
** uri (required) Uri of a WebSocket listener that is
|
||||||
|
** connected to a spice server.
|
||||||
|
** password (required) Password to send to the spice server
|
||||||
|
** message_id (optional) Identifier of an element in the DOM
|
||||||
|
** where SpiceConn will write messages.
|
||||||
|
** It will use classes spice-messages-x,
|
||||||
|
** where x is one of info, warning, or error.
|
||||||
|
** screen_id (optional) Identifier of an element in the DOM
|
||||||
|
** where SpiceConn will create any new
|
||||||
|
** client screens. This is the main UI.
|
||||||
|
** dump_id (optional) If given, an element to use for
|
||||||
|
** dumping every single image + canvas drawn.
|
||||||
|
** Sometimes useful for debugging.
|
||||||
|
** onerror (optional) If given, a function to receive async
|
||||||
|
** errors. Note that you should also catch
|
||||||
|
** errors for ones that occur inline
|
||||||
|
** onagent (optional) If given, a function to be called when
|
||||||
|
** a VD agent is connected; a good opportunity
|
||||||
|
** to request a resize
|
||||||
|
** onsuccess (optional) If given, a function to be called when the
|
||||||
|
** session is successfully connected
|
||||||
|
**
|
||||||
|
** Throws error if there are troubles. Requires a modern (by 2012 standards)
|
||||||
|
** browser, including WebSocket and WebSocket.binaryType == arraybuffer
|
||||||
|
**
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function SpiceMainConn()
|
||||||
|
{
|
||||||
|
if (typeof WebSocket === "undefined")
|
||||||
|
throw new Error("WebSocket unavailable. You need to use a different browser.");
|
||||||
|
|
||||||
|
SpiceConn.apply(this, arguments);
|
||||||
|
|
||||||
|
this.agent_msg_queue = [];
|
||||||
|
this.file_xfer_tasks = {};
|
||||||
|
this.file_xfer_task_id = 0;
|
||||||
|
this.file_xfer_read_queue = [];
|
||||||
|
this.ports = [];
|
||||||
|
this.agent_caps = [0]
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype = Object.create(SpiceConn.prototype);
|
||||||
|
SpiceMainConn.prototype.process_channel_message = function(msg)
|
||||||
|
{
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_BEGIN)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Begin");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_CANCEL)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Cancel");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_INIT)
|
||||||
|
{
|
||||||
|
this.log_info("Connected to " + this.ws.url);
|
||||||
|
this.report_success("Connected")
|
||||||
|
this.main_init = new Messages.SpiceMsgMainInit(msg.data);
|
||||||
|
this.connection_id = this.main_init.session_id;
|
||||||
|
this.agent_tokens = this.main_init.agent_tokens;
|
||||||
|
|
||||||
|
if (DEBUG > 0)
|
||||||
|
{
|
||||||
|
// FIXME - there is a lot here we don't handle; mouse modes, agent,
|
||||||
|
// ram_hint, multi_media_time
|
||||||
|
this.log_info("session id " + this.main_init.session_id +
|
||||||
|
" ; display_channels_hint " + this.main_init.display_channels_hint +
|
||||||
|
" ; supported_mouse_modes " + this.main_init.supported_mouse_modes +
|
||||||
|
" ; current_mouse_mode " + this.main_init.current_mouse_mode +
|
||||||
|
" ; agent_connected " + this.main_init.agent_connected +
|
||||||
|
" ; agent_tokens " + this.main_init.agent_tokens +
|
||||||
|
" ; multi_media_time " + this.main_init.multi_media_time +
|
||||||
|
" ; ram_hint " + this.main_init.ram_hint);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.our_mm_time = Date.now();
|
||||||
|
this.mm_time = this.main_init.multi_media_time;
|
||||||
|
|
||||||
|
this.handle_mouse_mode(this.main_init.current_mouse_mode,
|
||||||
|
this.main_init.supported_mouse_modes);
|
||||||
|
|
||||||
|
if (this.main_init.agent_connected)
|
||||||
|
this.connect_agent();
|
||||||
|
|
||||||
|
var attach = new Messages.SpiceMiniData;
|
||||||
|
attach.type = Constants.SPICE_MSGC_MAIN_ATTACH_CHANNELS;
|
||||||
|
attach.size = attach.buffer_size();
|
||||||
|
this.send_msg(attach);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MOUSE_MODE)
|
||||||
|
{
|
||||||
|
var mode = new Messages.SpiceMsgMainMouseMode(msg.data);
|
||||||
|
DEBUG > 0 && this.log_info("Mouse supported modes " + mode.supported_modes + "; current " + mode.current_mode);
|
||||||
|
this.handle_mouse_mode(mode.current_mode, mode.supported_modes);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MULTI_MEDIA_TIME)
|
||||||
|
{
|
||||||
|
/* The server's multimedia clock. relative_now() estimates it from
|
||||||
|
the value seeded at INIT plus elapsed wall time, which drifts as
|
||||||
|
the two clocks diverge, and stream frames are scheduled and
|
||||||
|
reported against that estimate. This message is the server
|
||||||
|
resynchronising it. */
|
||||||
|
var mm = new Messages.SpiceMsgMainMultiMediaTime(msg.data);
|
||||||
|
this.our_mm_time = Date.now();
|
||||||
|
this.mm_time = mm.time;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_CHANNELS_LIST)
|
||||||
|
{
|
||||||
|
var i;
|
||||||
|
var chans;
|
||||||
|
DEBUG > 0 && console.log("channels");
|
||||||
|
chans = new Messages.SpiceMsgChannels(msg.data);
|
||||||
|
for (i = 0; i < chans.channels.length; i++)
|
||||||
|
{
|
||||||
|
var conn = {
|
||||||
|
uri: this.ws.url,
|
||||||
|
parent: this,
|
||||||
|
connection_id : this.connection_id,
|
||||||
|
type : chans.channels[i].type,
|
||||||
|
chan_id : chans.channels[i].id
|
||||||
|
};
|
||||||
|
if (chans.channels[i].type == Constants.SPICE_CHANNEL_DISPLAY)
|
||||||
|
{
|
||||||
|
if (chans.channels[i].id == 0) {
|
||||||
|
this.display = new SpiceDisplayConn(conn);
|
||||||
|
} else {
|
||||||
|
this.log_warn("The spice-html5 client does not handle multiple heads.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (chans.channels[i].type == Constants.SPICE_CHANNEL_INPUTS)
|
||||||
|
{
|
||||||
|
this.inputs = new SpiceInputsConn(conn);
|
||||||
|
this.inputs.mouse_mode = this.mouse_mode;
|
||||||
|
}
|
||||||
|
else if (chans.channels[i].type == Constants.SPICE_CHANNEL_CURSOR)
|
||||||
|
this.cursor = new SpiceCursorConn(conn);
|
||||||
|
else if (chans.channels[i].type == Constants.SPICE_CHANNEL_PLAYBACK)
|
||||||
|
this.playback = new SpicePlaybackConn(conn);
|
||||||
|
else if (chans.channels[i].type == Constants.SPICE_CHANNEL_PORT)
|
||||||
|
this.ports.push(new SpicePortConn(conn));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (! ("extra_channels" in this))
|
||||||
|
this.extra_channels = [];
|
||||||
|
this.extra_channels[i] = new SpiceConn(conn);
|
||||||
|
this.log_err("Channel type " + this.extra_channels[i].channel_type() + " not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_AGENT_CONNECTED)
|
||||||
|
{
|
||||||
|
this.connect_agent();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_AGENT_CONNECTED_TOKENS)
|
||||||
|
{
|
||||||
|
var connected_tokens = new Messages.SpiceMsgMainAgentTokens(msg.data);
|
||||||
|
this.agent_tokens = connected_tokens.num_tokens;
|
||||||
|
this.connect_agent();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_AGENT_TOKEN)
|
||||||
|
{
|
||||||
|
var remaining_tokens, tokens = new Messages.SpiceMsgMainAgentTokens(msg.data);
|
||||||
|
this.agent_tokens += tokens.num_tokens;
|
||||||
|
this.send_agent_message_queue();
|
||||||
|
|
||||||
|
remaining_tokens = this.agent_tokens;
|
||||||
|
while (remaining_tokens > 0 && this.file_xfer_read_queue.length > 0)
|
||||||
|
{
|
||||||
|
var xfer_task = this.file_xfer_read_queue.shift();
|
||||||
|
this.file_xfer_read(xfer_task, xfer_task.read_bytes);
|
||||||
|
remaining_tokens--;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_AGENT_DISCONNECTED)
|
||||||
|
{
|
||||||
|
this.agent_connected = false;
|
||||||
|
|
||||||
|
/* Fail transfers in flight rather than strand them: with the
|
||||||
|
agent gone their chunks are silently dropped, so they would
|
||||||
|
never complete, never error, and their progress bars would sit
|
||||||
|
on screen forever. */
|
||||||
|
this.agent_msg_queue = [];
|
||||||
|
this.file_xfer_read_queue = [];
|
||||||
|
for (var task_id in this.file_xfer_tasks)
|
||||||
|
this.file_xfer_completed(this.file_xfer_tasks[task_id],
|
||||||
|
new Error("Agent disconnected; file transfer aborted."));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_AGENT_DATA)
|
||||||
|
{
|
||||||
|
var agent_data = new Messages.SpiceMsgMainAgentData(msg.data);
|
||||||
|
if (agent_data.type == Constants.VD_AGENT_ANNOUNCE_CAPABILITIES)
|
||||||
|
{
|
||||||
|
var agent_caps = new Messages.VDAgentAnnounceCapabilities(agent_data.data);
|
||||||
|
this.agent_caps = [agent_caps.caps];
|
||||||
|
if (agent_caps.request)
|
||||||
|
this.announce_agent_capabilities(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (agent_data.type == Constants.VD_AGENT_FILE_XFER_STATUS)
|
||||||
|
{
|
||||||
|
this.handle_file_xfer_status(new Messages.VDAgentFileXferStatusMessage(agent_data.data));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (agent_data.type == Constants.VD_AGENT_CLIPBOARD_GRAB)
|
||||||
|
{
|
||||||
|
this.handle_clipboard_grab();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (agent_data.type == Constants.VD_AGENT_CLIPBOARD)
|
||||||
|
{
|
||||||
|
this.handle_clipboard_receive(agent_data);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (agent_data.type == Constants.VD_AGENT_CLIPBOARD_REQUEST)
|
||||||
|
{
|
||||||
|
this.handle_clipboard_send();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (agent_data.type == Constants.VD_AGENT_CLIPBOARD_RELEASE)
|
||||||
|
{
|
||||||
|
// Currently we don't need to do anything when the agent releases the clipboard
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* An agent message we do not act on is still an agent message.
|
||||||
|
Returning false sent it back as an unhandled main-channel
|
||||||
|
message, so the console reported "Unknown message type 109"
|
||||||
|
-- naming the agent-data envelope rather than the thing
|
||||||
|
inside it, which is the part worth knowing. Name that once
|
||||||
|
per type instead. */
|
||||||
|
this.report_unhandled_agent_message(agent_data.type);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_SWITCH_HOST)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Switch Host");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_END)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate End");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_NAME)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Name");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_UUID)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main UUID");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_BEGIN_SEAMLESS)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Begin Seamless");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_DST_SEAMLESS_ACK)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Dst Seamless ACK");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_MAIN_MIGRATE_DST_SEAMLESS_NACK)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Main Migrate Dst Seamless NACK");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.stop = function(msg)
|
||||||
|
{
|
||||||
|
this.state = "closing";
|
||||||
|
|
||||||
|
if (this.inputs)
|
||||||
|
{
|
||||||
|
this.inputs.cleanup();
|
||||||
|
this.inputs = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.cursor)
|
||||||
|
{
|
||||||
|
this.cursor.cleanup();
|
||||||
|
this.cursor = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.playback)
|
||||||
|
{
|
||||||
|
this.playback.cleanup();
|
||||||
|
this.playback = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var p = 0; p < this.ports.length; p++)
|
||||||
|
this.ports[p].cleanup();
|
||||||
|
this.ports = [];
|
||||||
|
|
||||||
|
if (this.display)
|
||||||
|
{
|
||||||
|
this.display.cleanup();
|
||||||
|
this.display.destroy_surfaces();
|
||||||
|
this.display = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.cleanup();
|
||||||
|
|
||||||
|
if ("extra_channels" in this)
|
||||||
|
for (var e in this.extra_channels)
|
||||||
|
this.extra_channels[e].cleanup();
|
||||||
|
this.extra_channels = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.send_agent_message_queue = function(message)
|
||||||
|
{
|
||||||
|
if (!this.agent_connected)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (message)
|
||||||
|
this.agent_msg_queue.push(message);
|
||||||
|
|
||||||
|
while (this.agent_tokens > 0 && this.agent_msg_queue.length > 0)
|
||||||
|
{
|
||||||
|
var mr = this.agent_msg_queue.shift();
|
||||||
|
this.send_msg(mr);
|
||||||
|
this.agent_tokens--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.send_agent_message = function(type, message)
|
||||||
|
{
|
||||||
|
var agent_data = new Messages.SpiceMsgcMainAgentData(type, message);
|
||||||
|
var sb = 0, maxsize = Constants.VD_AGENT_MAX_DATA_SIZE - Messages.SpiceMiniData.prototype.buffer_size();
|
||||||
|
var data = new ArrayBuffer(agent_data.buffer_size());
|
||||||
|
agent_data.to_buffer(data);
|
||||||
|
while (sb < agent_data.buffer_size())
|
||||||
|
{
|
||||||
|
var eb = Math.min(sb + maxsize, agent_data.buffer_size());
|
||||||
|
var mr = new Messages.SpiceMiniData();
|
||||||
|
mr.type = Constants.SPICE_MSGC_MAIN_AGENT_DATA;
|
||||||
|
mr.size = eb - sb;
|
||||||
|
mr.data = data.slice(sb, eb);
|
||||||
|
this.send_agent_message_queue(mr);
|
||||||
|
sb = eb;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.announce_agent_capabilities = function(request)
|
||||||
|
{
|
||||||
|
var caps = new Messages.VDAgentAnnounceCapabilities(request, (1 << Constants.VD_AGENT_CAP_MOUSE_STATE) |
|
||||||
|
(1 << Constants.VD_AGENT_CAP_MONITORS_CONFIG) |
|
||||||
|
(1 << Constants.VD_AGENT_CAP_REPLY) |
|
||||||
|
(1 << Constants.VD_AGENT_CAP_CLIPBOARD_SELECTION) |
|
||||||
|
(1 << Constants.VD_AGENT_CAP_CLIPBOARD_BY_DEMAND));
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_ANNOUNCE_CAPABILITIES, caps);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.resize_window = function(flags, width, height, depth, x, y)
|
||||||
|
{
|
||||||
|
var monitors_config = new Messages.VDAgentMonitorsConfig(flags, width, height, depth, x, y);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_MONITORS_CONFIG, monitors_config);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.file_xfer_start = function(file)
|
||||||
|
{
|
||||||
|
var task_id, xfer_start, task;
|
||||||
|
|
||||||
|
task_id = this.file_xfer_task_id++;
|
||||||
|
task = new SpiceFileXferTask(task_id, file);
|
||||||
|
task.create_progressbar();
|
||||||
|
this.file_xfer_tasks[task_id] = task;
|
||||||
|
xfer_start = new Messages.VDAgentFileXferStartMessage(task_id, file.name, file.size);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_FILE_XFER_START, xfer_start);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.handle_file_xfer_status = function(file_xfer_status)
|
||||||
|
{
|
||||||
|
var xfer_error, xfer_task;
|
||||||
|
if (!this.file_xfer_tasks[file_xfer_status.id])
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
xfer_task = this.file_xfer_tasks[file_xfer_status.id];
|
||||||
|
switch (file_xfer_status.result)
|
||||||
|
{
|
||||||
|
case Constants.VD_AGENT_FILE_XFER_STATUS_CAN_SEND_DATA:
|
||||||
|
this.file_xfer_read(xfer_task);
|
||||||
|
return;
|
||||||
|
case Constants.VD_AGENT_FILE_XFER_STATUS_CANCELLED:
|
||||||
|
xfer_error = "transfer is cancelled by spice agent";
|
||||||
|
break;
|
||||||
|
case Constants.VD_AGENT_FILE_XFER_STATUS_ERROR:
|
||||||
|
xfer_error = "some errors occurred in the spice agent";
|
||||||
|
break;
|
||||||
|
case Constants.VD_AGENT_FILE_XFER_STATUS_SUCCESS:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
xfer_error = "unhandled status type: " + file_xfer_status.result;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.file_xfer_completed(xfer_task, xfer_error)
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.file_xfer_read = function(file_xfer_task, start_byte)
|
||||||
|
{
|
||||||
|
var FILE_XFER_CHUNK_SIZE = 32 * Constants.VD_AGENT_MAX_DATA_SIZE;
|
||||||
|
var _this = this;
|
||||||
|
var sb, eb;
|
||||||
|
var slice, reader;
|
||||||
|
|
||||||
|
if (!file_xfer_task ||
|
||||||
|
!this.file_xfer_tasks[file_xfer_task.id] ||
|
||||||
|
(start_byte > 0 && start_byte == file_xfer_task.file.size))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file_xfer_task.cancelled)
|
||||||
|
{
|
||||||
|
var xfer_status = new Messages.VDAgentFileXferStatusMessage(file_xfer_task.id,
|
||||||
|
Constants.VD_AGENT_FILE_XFER_STATUS_CANCELLED);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_FILE_XFER_STATUS, xfer_status);
|
||||||
|
delete this.file_xfer_tasks[file_xfer_task.id];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
sb = start_byte || 0,
|
||||||
|
eb = Math.min(sb + FILE_XFER_CHUNK_SIZE, file_xfer_task.file.size);
|
||||||
|
|
||||||
|
if (!this.agent_tokens)
|
||||||
|
{
|
||||||
|
file_xfer_task.read_bytes = sb;
|
||||||
|
this.file_xfer_read_queue.push(file_xfer_task);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
reader = new FileReader();
|
||||||
|
reader.onload = function(e)
|
||||||
|
{
|
||||||
|
var xfer_data = new Messages.VDAgentFileXferDataMessage(file_xfer_task.id,
|
||||||
|
e.target.result.byteLength,
|
||||||
|
e.target.result);
|
||||||
|
_this.send_agent_message(Constants.VD_AGENT_FILE_XFER_DATA, xfer_data);
|
||||||
|
/* Only recurse while bytes remain. The old code always recursed
|
||||||
|
and relied on the start_byte > 0 guard above to terminate, which
|
||||||
|
a zero-byte file never satisfies — it looped forever, burning an
|
||||||
|
agent token per iteration. */
|
||||||
|
if (eb < file_xfer_task.file.size)
|
||||||
|
_this.file_xfer_read(file_xfer_task, eb);
|
||||||
|
file_xfer_task.update_progressbar(eb);
|
||||||
|
};
|
||||||
|
|
||||||
|
slice = file_xfer_task.file.slice(sb, eb);
|
||||||
|
reader.readAsArrayBuffer(slice);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.file_xfer_completed = function(file_xfer_task, error)
|
||||||
|
{
|
||||||
|
if (error)
|
||||||
|
this.log_err(error);
|
||||||
|
else
|
||||||
|
this.log_info("transfer of '" + file_xfer_task.file.name +"' was successful");
|
||||||
|
|
||||||
|
file_xfer_task.remove_progressbar();
|
||||||
|
|
||||||
|
delete this.file_xfer_tasks[file_xfer_task.id];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The guest agent sends more than this client acts on -- a reply
|
||||||
|
acknowledging our capabilities, mouse state under server mouse mode,
|
||||||
|
monitor and display configuration. None need handling, but each was
|
||||||
|
arriving as an anonymous "unknown message" on the main channel. */
|
||||||
|
SpiceMainConn.prototype.report_unhandled_agent_message = function(type)
|
||||||
|
{
|
||||||
|
var names = {};
|
||||||
|
names[Constants.VD_AGENT_MOUSE_STATE] = "Mouse State";
|
||||||
|
names[Constants.VD_AGENT_MONITORS_CONFIG] = "Monitors Config";
|
||||||
|
names[Constants.VD_AGENT_REPLY] = "Reply";
|
||||||
|
names[Constants.VD_AGENT_DISPLAY_CONFIG] = "Display Config";
|
||||||
|
names[Constants.VD_AGENT_FILE_XFER_START] = "File Xfer Start";
|
||||||
|
names[Constants.VD_AGENT_FILE_XFER_DATA] = "File Xfer Data";
|
||||||
|
names[Constants.VD_AGENT_CLIENT_DISCONNECTED] = "Client Disconnected";
|
||||||
|
names[Constants.VD_AGENT_MAX_CLIPBOARD] = "Max Clipboard";
|
||||||
|
|
||||||
|
if (! this.reported_agent_types)
|
||||||
|
this.reported_agent_types = {};
|
||||||
|
if (this.reported_agent_types[type])
|
||||||
|
return;
|
||||||
|
this.reported_agent_types[type] = true;
|
||||||
|
|
||||||
|
this.log_warn("Agent message not handled: " + (names[type] || "type " + type) +
|
||||||
|
" [ further notices suppressed ]");
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.handle_clipboard_grab = function()
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("handling clipboard grab from agent");
|
||||||
|
|
||||||
|
// agent is requesting clipboard grab, so we send a message to request clipboard contents
|
||||||
|
const type = Constants.VD_AGENT_CLIPBOARD_UTF8_TEXT;
|
||||||
|
const clipboard_request = new Messages.SpiceMsgClipboardRequest(type, this.agent_caps);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_CLIPBOARD_REQUEST, clipboard_request);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.handle_clipboard_receive = function(agent_data)
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("received clipboard data from agent");
|
||||||
|
|
||||||
|
// received clipboard data from agent, copy it to browser clipboard
|
||||||
|
const received_clipboard = new Messages.SpiceMsgClipboardReceive(agent_data, this.agent_caps);
|
||||||
|
if (received_clipboard.type === Constants.VD_AGENT_CLIPBOARD_UTF8_TEXT)
|
||||||
|
{
|
||||||
|
// write it to the browser clipboard
|
||||||
|
if (navigator.clipboard && navigator.clipboard.writeText)
|
||||||
|
{
|
||||||
|
const text = received_clipboard.get_text();
|
||||||
|
navigator.clipboard.writeText(text).catch(err => {
|
||||||
|
console.warn("Failed to write to navigator clipboard:", err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
console.log("Unsupported clipboard type:", received_clipboard.type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.handle_clipboard_send = function()
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("sending clipboard data to agent");
|
||||||
|
|
||||||
|
// agent is requesting clipboard data, read it from browser clipboard and send it.
|
||||||
|
if (navigator.clipboard && navigator.clipboard.readText)
|
||||||
|
{
|
||||||
|
navigator.clipboard.readText().then(text => {
|
||||||
|
const type = Constants.VD_AGENT_CLIPBOARD_UTF8_TEXT;
|
||||||
|
const clipboard_msg = new Messages.SpiceMsgClipboardSend(type, text, this.agent_caps);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_CLIPBOARD, clipboard_msg);
|
||||||
|
}).catch(err => {
|
||||||
|
console.log("Failed to read clipboard:", err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.send_clipboard_grab = function()
|
||||||
|
{
|
||||||
|
DEBUG > 1 && console.log("browser is asking agent to grab clipboard");
|
||||||
|
|
||||||
|
// Send clipboard grab to agent. Agent will then ask for the host os clipboard contents in a dedicated message.
|
||||||
|
const type = Constants.VD_AGENT_CLIPBOARD_UTF8_TEXT;
|
||||||
|
const grab_message = new Messages.SpiceMsgClipboardGrab(type, this.agent_caps);
|
||||||
|
this.send_agent_message(Constants.VD_AGENT_CLIPBOARD_GRAB, grab_message)
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.connect_agent = function()
|
||||||
|
{
|
||||||
|
this.agent_connected = true;
|
||||||
|
|
||||||
|
var agent_start = new Messages.SpiceMsgcMainAgentStart(~0);
|
||||||
|
var mr = new Messages.SpiceMiniData();
|
||||||
|
mr.build_msg(Constants.SPICE_MSGC_MAIN_AGENT_START, agent_start);
|
||||||
|
this.send_msg(mr);
|
||||||
|
|
||||||
|
this.announce_agent_capabilities(1);
|
||||||
|
|
||||||
|
if (this.onagent !== undefined)
|
||||||
|
this.onagent(this);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
SpiceMainConn.prototype.handle_mouse_mode = function(current, supported)
|
||||||
|
{
|
||||||
|
this.mouse_mode = current;
|
||||||
|
if (current != Constants.SPICE_MOUSE_MODE_CLIENT && (supported & Constants.SPICE_MOUSE_MODE_CLIENT))
|
||||||
|
{
|
||||||
|
var mode_request = new Messages.SpiceMsgcMainMouseModeRequest(Constants.SPICE_MOUSE_MODE_CLIENT);
|
||||||
|
var mr = new Messages.SpiceMiniData();
|
||||||
|
mr.build_msg(Constants.SPICE_MSGC_MAIN_MOUSE_MODE_REQUEST, mode_request);
|
||||||
|
this.send_msg(mr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.inputs)
|
||||||
|
this.inputs.mouse_mode = current;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Shift current time to attempt to get a time matching that of the server */
|
||||||
|
SpiceMainConn.prototype.relative_now = function()
|
||||||
|
{
|
||||||
|
var ret = (Date.now() - this.our_mm_time) + this.mm_time;
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpiceMainConn,
|
||||||
|
handle_file_dragover,
|
||||||
|
handle_file_drop,
|
||||||
|
resize_helper,
|
||||||
|
handle_resize,
|
||||||
|
sendCtrlAltDel,
|
||||||
|
typeText,
|
||||||
|
};
|
||||||
+432
@@ -0,0 +1,432 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2014 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpicePlaybackConn
|
||||||
|
** Drive the Spice Playback channel (sound out)
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import * as Utils from './utils.js';
|
||||||
|
import * as Webm from './webm.js';
|
||||||
|
import * as Messages from './spicemsg.js';
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
import { SpiceConn } from './spiceconn.js';
|
||||||
|
|
||||||
|
function SpicePlaybackConn()
|
||||||
|
{
|
||||||
|
SpiceConn.apply(this, arguments);
|
||||||
|
|
||||||
|
this.queue = new Array();
|
||||||
|
this.append_okay = false;
|
||||||
|
this.start_time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePlaybackConn.prototype = Object.create(SpiceConn.prototype);
|
||||||
|
SpicePlaybackConn.prototype.process_channel_message = function(msg)
|
||||||
|
{
|
||||||
|
if (!window.MediaSource)
|
||||||
|
{
|
||||||
|
this.log_err('MediaSource API is not available');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_START)
|
||||||
|
{
|
||||||
|
var start = new Messages.SpiceMsgPlaybackStart(msg.data);
|
||||||
|
|
||||||
|
Utils.PLAYBACK_DEBUG > 0 && console.log("PlaybackStart; frequency " + start.frequency);
|
||||||
|
|
||||||
|
if (start.frequency != Webm.Constants.OPUS_FREQUENCY)
|
||||||
|
{
|
||||||
|
this.log_err('This player cannot handle frequency ' + start.frequency);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start.channels != Webm.Constants.OPUS_CHANNELS)
|
||||||
|
{
|
||||||
|
this.log_err('This player cannot handle ' + start.channels + ' channels');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start.format != Constants.SPICE_AUDIO_FMT_S16)
|
||||||
|
{
|
||||||
|
this.log_err('This player cannot format ' + start.format);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Keyed on the audio element, not source_buffer: source_buffer is
|
||||||
|
only set asynchronously in handle_source_open, so a second START
|
||||||
|
arriving before sourceopen would stack a second audio element. */
|
||||||
|
if (! this.audio)
|
||||||
|
{
|
||||||
|
this.media_source = new MediaSource();
|
||||||
|
this.media_source.spiceconn = this;
|
||||||
|
|
||||||
|
this.audio = document.createElement("audio");
|
||||||
|
this.audio.spiceconn = this;
|
||||||
|
this.audio.setAttribute('autoplay', true);
|
||||||
|
this.audio.src = window.URL.createObjectURL(this.media_source);
|
||||||
|
document.getElementById(this.parent.screen_id).appendChild(this.audio);
|
||||||
|
|
||||||
|
this.media_source.addEventListener('sourceopen', handle_source_open, false);
|
||||||
|
this.media_source.addEventListener('sourceended', handle_source_ended, false);
|
||||||
|
this.media_source.addEventListener('sourceclosed', handle_source_closed, false);
|
||||||
|
|
||||||
|
this.bytes_written = 0;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_DATA)
|
||||||
|
{
|
||||||
|
var data = new Messages.SpiceMsgPlaybackData(msg.data);
|
||||||
|
|
||||||
|
if (! this.source_buffer)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (this.audio.readyState >= 3 && this.audio.buffered.length > 1 &&
|
||||||
|
this.audio.currentTime == this.audio.buffered.end(0) &&
|
||||||
|
this.audio.currentTime < this.audio.buffered.start(this.audio.buffered.length - 1))
|
||||||
|
{
|
||||||
|
console.log("Audio underrun: we appear to have fallen behind; advancing to " +
|
||||||
|
this.audio.buffered.start(this.audio.buffered.length - 1));
|
||||||
|
this.audio.currentTime = this.audio.buffered.start(this.audio.buffered.length - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Around version 45, Firefox started being very particular about the
|
||||||
|
time stamps put into the Opus stream. The time stamps from the Spice server are
|
||||||
|
somewhat irregular. They mostly arrive every 10 ms, but sometimes it is 11, or sometimes
|
||||||
|
with two time stamps the same in a row. The previous logic resulted in fuzzy and/or
|
||||||
|
distorted audio streams in Firefox in a row.
|
||||||
|
|
||||||
|
In theory, the sequence mode should be appropriate for us, but as of 09/27/2016,
|
||||||
|
I was unable to make sequence mode work with Firefox.
|
||||||
|
|
||||||
|
Thus, we end up with an inelegant hack. Essentially, we force every packet to have
|
||||||
|
a 10ms time delta, unless there is an obvious gap in time stream, in which case we
|
||||||
|
will resync.
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (this.start_time != 0 && data.time != (this.last_data_time + Webm.Constants.EXPECTED_PACKET_DURATION))
|
||||||
|
{
|
||||||
|
if (Math.abs(data.time - (Webm.Constants.EXPECTED_PACKET_DURATION + this.last_data_time)) < Webm.Constants.MAX_CLUSTER_TIME)
|
||||||
|
{
|
||||||
|
Utils.PLAYBACK_DEBUG > 1 && console.log("Hacking time of " + data.time + " to " +
|
||||||
|
(this.last_data_time + Webm.Constants.EXPECTED_PACKET_DURATION));
|
||||||
|
data.time = this.last_data_time + Webm.Constants.EXPECTED_PACKET_DURATION;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Utils.PLAYBACK_DEBUG > 1 && console.log("Apparent gap in audio time; now is " + data.time + " last was " + this.last_data_time);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.last_data_time = data.time;
|
||||||
|
|
||||||
|
Utils.PLAYBACK_DEBUG > 1 && console.log("PlaybackData; time " + data.time + "; length " + data.data.byteLength);
|
||||||
|
|
||||||
|
if (this.start_time == 0)
|
||||||
|
this.start_playback(data);
|
||||||
|
|
||||||
|
else if (data.time - this.cluster_time >= Webm.Constants.MAX_CLUSTER_TIME)
|
||||||
|
this.new_cluster(data);
|
||||||
|
|
||||||
|
else
|
||||||
|
this.simple_block(data, false);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_MODE)
|
||||||
|
{
|
||||||
|
var mode = new Messages.SpiceMsgPlaybackMode(msg.data);
|
||||||
|
if (mode.mode != Constants.SPICE_AUDIO_DATA_MODE_OPUS)
|
||||||
|
{
|
||||||
|
this.log_err('This player cannot handle mode ' + mode.mode);
|
||||||
|
/* Deleting just source_buffer left the audio element and its
|
||||||
|
object URL orphaned with no guard left to reclaim them. */
|
||||||
|
this.destroy_audio();
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_STOP)
|
||||||
|
{
|
||||||
|
Utils.PLAYBACK_DEBUG > 0 && console.log("PlaybackStop");
|
||||||
|
if (this.audio)
|
||||||
|
{
|
||||||
|
this.destroy_audio();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_VOLUME)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Playback Volume");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_MUTE)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Playback Mute");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PLAYBACK_LATENCY)
|
||||||
|
{
|
||||||
|
this.known_unimplemented(msg.type, "Playback Latency");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePlaybackConn.prototype.destroy_audio = function()
|
||||||
|
{
|
||||||
|
if (! this.audio)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (this.audio.parentNode)
|
||||||
|
this.audio.parentNode.removeChild(this.audio);
|
||||||
|
window.URL.revokeObjectURL(this.audio.src);
|
||||||
|
|
||||||
|
delete this.source_buffer;
|
||||||
|
delete this.media_source;
|
||||||
|
delete this.audio;
|
||||||
|
|
||||||
|
this.append_okay = false;
|
||||||
|
this.queue = new Array();
|
||||||
|
this.start_time = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* A client-side stop must reclaim the audio element, its object URL and
|
||||||
|
the MediaSource; without this they were only freed by a server STOP,
|
||||||
|
and an application that stops and recreates connections leaked one
|
||||||
|
set per cycle. */
|
||||||
|
SpicePlaybackConn.prototype.cleanup = function()
|
||||||
|
{
|
||||||
|
this.destroy_audio();
|
||||||
|
SpiceConn.prototype.cleanup.call(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePlaybackConn.prototype.start_playback = function(data)
|
||||||
|
{
|
||||||
|
this.start_time = data.time;
|
||||||
|
|
||||||
|
var h = new Webm.Header();
|
||||||
|
var te = new Webm.AudioTrackEntry;
|
||||||
|
var t = new Webm.Tracks(te);
|
||||||
|
|
||||||
|
var mb = new ArrayBuffer(h.buffer_size() + t.buffer_size())
|
||||||
|
|
||||||
|
this.bytes_written = h.to_buffer(mb);
|
||||||
|
this.bytes_written = t.to_buffer(mb, this.bytes_written);
|
||||||
|
|
||||||
|
this.source_buffer.addEventListener('error', handle_sourcebuffer_error, false);
|
||||||
|
this.source_buffer.addEventListener('updateend', handle_append_buffer_done, false);
|
||||||
|
playback_append_buffer(this, mb);
|
||||||
|
|
||||||
|
this.new_cluster(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePlaybackConn.prototype.new_cluster = function(data)
|
||||||
|
{
|
||||||
|
this.cluster_time = data.time;
|
||||||
|
|
||||||
|
var c = new Webm.Cluster(data.time - this.start_time);
|
||||||
|
|
||||||
|
var mb = new ArrayBuffer(c.buffer_size());
|
||||||
|
this.bytes_written += c.to_buffer(mb);
|
||||||
|
|
||||||
|
if (this.append_okay)
|
||||||
|
playback_append_buffer(this, mb);
|
||||||
|
else
|
||||||
|
this.queue.push(mb);
|
||||||
|
|
||||||
|
this.simple_block(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePlaybackConn.prototype.simple_block = function(data, keyframe)
|
||||||
|
{
|
||||||
|
var sb = new Webm.SimpleBlock(data.time - this.cluster_time, data.data, keyframe);
|
||||||
|
var mb = new ArrayBuffer(sb.buffer_size());
|
||||||
|
|
||||||
|
this.bytes_written += sb.to_buffer(mb);
|
||||||
|
|
||||||
|
if (this.append_okay)
|
||||||
|
playback_append_buffer(this, mb);
|
||||||
|
else
|
||||||
|
this.queue.push(mb);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_source_open(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
|
||||||
|
if (p.source_buffer)
|
||||||
|
return;
|
||||||
|
|
||||||
|
p.source_buffer = this.addSourceBuffer(Webm.Constants.SPICE_PLAYBACK_CODEC);
|
||||||
|
if (! p.source_buffer)
|
||||||
|
{
|
||||||
|
p.log_err('Codec ' + Webm.Constants.SPICE_PLAYBACK_CODEC + ' not available.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 0)
|
||||||
|
playback_handle_event_debug.call(this, e);
|
||||||
|
|
||||||
|
listen_for_audio_events(p);
|
||||||
|
|
||||||
|
p.source_buffer.spiceconn = p;
|
||||||
|
p.source_buffer.mode = "segments";
|
||||||
|
|
||||||
|
// FIXME - Experimentation with segments and sequences was unsatisfying.
|
||||||
|
// Switching to sequence did not solve our gap problem,
|
||||||
|
// but the browsers didn't fully support the time seek capability
|
||||||
|
// we would expect to gain from 'segments'.
|
||||||
|
// Segments worked at the time of this patch, so segments it is for now.
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_source_ended(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
p.log_err('Audio source unexpectedly ended.');
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_source_closed(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
p.log_err('Audio source unexpectedly closed.');
|
||||||
|
}
|
||||||
|
|
||||||
|
function condense_playback_queue(queue)
|
||||||
|
{
|
||||||
|
if (queue.length == 1)
|
||||||
|
return queue.shift();
|
||||||
|
|
||||||
|
var len = 0;
|
||||||
|
var i = 0;
|
||||||
|
for (i = 0; i < queue.length; i++)
|
||||||
|
len += queue[i].byteLength;
|
||||||
|
|
||||||
|
var mb = new ArrayBuffer(len);
|
||||||
|
var tmp = new Uint8Array(mb);
|
||||||
|
len = 0;
|
||||||
|
for (i = 0; i < queue.length; i++)
|
||||||
|
{
|
||||||
|
tmp.set(new Uint8Array(queue[i]), len);
|
||||||
|
len += queue[i].byteLength;
|
||||||
|
}
|
||||||
|
queue.length = 0;
|
||||||
|
return mb;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_append_buffer_done(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 1)
|
||||||
|
playback_handle_event_debug.call(this, e);
|
||||||
|
|
||||||
|
if (p.queue.length > 0)
|
||||||
|
{
|
||||||
|
var mb = condense_playback_queue(p.queue);
|
||||||
|
playback_append_buffer(p, mb);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
p.append_okay = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_sourcebuffer_error(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
p.log_err('source_buffer error ' + e.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
function playback_append_buffer(p, b)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
p.source_buffer.appendBuffer(b);
|
||||||
|
p.append_okay = false;
|
||||||
|
}
|
||||||
|
catch (e)
|
||||||
|
{
|
||||||
|
p.log_err("Error invoking appendBuffer: " + e.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function playback_handle_event_debug(e)
|
||||||
|
{
|
||||||
|
var p = this.spiceconn;
|
||||||
|
if (p.audio)
|
||||||
|
{
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 0 || p.audio.buffered.len > 1)
|
||||||
|
console.log(p.audio.currentTime + ": event " + e.type +
|
||||||
|
Utils.dump_media_element(p.audio));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 1 && p.media_source)
|
||||||
|
console.log(" media_source " + Utils.dump_media_source(p.media_source));
|
||||||
|
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 1 && p.source_buffer)
|
||||||
|
console.log(" source_buffer " + Utils.dump_source_buffer(p.source_buffer));
|
||||||
|
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 0 || p.queue.length > 1)
|
||||||
|
console.log(' queue len ' + p.queue.length + '; append_okay: ' + p.append_okay);
|
||||||
|
}
|
||||||
|
|
||||||
|
function playback_debug_listen_for_one_event(name)
|
||||||
|
{
|
||||||
|
this.addEventListener(name, playback_handle_event_debug);
|
||||||
|
}
|
||||||
|
|
||||||
|
function listen_for_audio_events(spiceconn)
|
||||||
|
{
|
||||||
|
var audio_0_events = [
|
||||||
|
"abort", "error"
|
||||||
|
];
|
||||||
|
|
||||||
|
var audio_1_events = [
|
||||||
|
"loadstart", "suspend", "emptied", "stalled", "loadedmetadata", "loadeddata", "canplay",
|
||||||
|
"canplaythrough", "playing", "waiting", "seeking", "seeked", "ended", "durationchange",
|
||||||
|
"timeupdate", "play", "pause", "ratechange"
|
||||||
|
];
|
||||||
|
|
||||||
|
var audio_2_events = [
|
||||||
|
"progress",
|
||||||
|
"resize",
|
||||||
|
"volumechange"
|
||||||
|
];
|
||||||
|
|
||||||
|
audio_0_events.forEach(playback_debug_listen_for_one_event, spiceconn.audio);
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 0)
|
||||||
|
audio_1_events.forEach(playback_debug_listen_for_one_event, spiceconn.audio);
|
||||||
|
if (Utils.PLAYBACK_DEBUG > 1)
|
||||||
|
audio_2_events.forEach(playback_debug_listen_for_one_event, spiceconn.audio);
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpicePlaybackConn,
|
||||||
|
};
|
||||||
+271
@@ -0,0 +1,271 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** crc logic from rfc2083 ported to Javascript
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
var rfc2083_crc_table = Array(256);
|
||||||
|
var rfc2083_crc_table_computed = 0;
|
||||||
|
/* Make the table for a fast CRC. */
|
||||||
|
function rfc2083_make_crc_table()
|
||||||
|
{
|
||||||
|
var c;
|
||||||
|
var n, k;
|
||||||
|
for (n = 0; n < 256; n++)
|
||||||
|
{
|
||||||
|
c = n;
|
||||||
|
for (k = 0; k < 8; k++)
|
||||||
|
{
|
||||||
|
if (c & 1)
|
||||||
|
c = ((0xedb88320 ^ (c >>> 1)) >>> 0) & 0xffffffff;
|
||||||
|
else
|
||||||
|
c = c >>> 1;
|
||||||
|
}
|
||||||
|
rfc2083_crc_table[n] = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
rfc2083_crc_table_computed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update a running CRC with the bytes buf[0..len-1]--the CRC
|
||||||
|
should be initialized to all 1's, and the transmitted value
|
||||||
|
is the 1's complement of the final running CRC (see the
|
||||||
|
crc() routine below)). */
|
||||||
|
|
||||||
|
function rfc2083_update_crc(crc, u8buf, at, len)
|
||||||
|
{
|
||||||
|
var c = crc;
|
||||||
|
var n;
|
||||||
|
|
||||||
|
if (!rfc2083_crc_table_computed)
|
||||||
|
rfc2083_make_crc_table();
|
||||||
|
|
||||||
|
for (n = 0; n < len; n++)
|
||||||
|
{
|
||||||
|
c = rfc2083_crc_table[(c ^ u8buf[at + n]) & 0xff] ^ (c >>> 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
function rfc2083_crc(u8buf, at, len)
|
||||||
|
{
|
||||||
|
return rfc2083_update_crc(0xffffffff, u8buf, at, len) ^ 0xffffffff;
|
||||||
|
}
|
||||||
|
|
||||||
|
function crc32(mb, at, len)
|
||||||
|
{
|
||||||
|
var u8 = new Uint8Array(mb);
|
||||||
|
return rfc2083_crc(u8, at, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
function PngIHDR(width, height)
|
||||||
|
{
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.depth = 8;
|
||||||
|
this.type = 6;
|
||||||
|
this.compression = 0;
|
||||||
|
this.filter = 0;
|
||||||
|
this.interlace = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
PngIHDR.prototype =
|
||||||
|
{
|
||||||
|
to_buffer: function(a, at)
|
||||||
|
{
|
||||||
|
at = at || 0;
|
||||||
|
var orig = at;
|
||||||
|
var dv = new DataView(a);
|
||||||
|
dv.setUint32(at, this.buffer_size() - 12); at += 4;
|
||||||
|
dv.setUint8(at, 'I'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'H'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'D'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'R'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint32(at, this.width); at += 4;
|
||||||
|
dv.setUint32(at, this.height); at += 4;
|
||||||
|
dv.setUint8(at, this.depth); at++;
|
||||||
|
dv.setUint8(at, this.type); at++;
|
||||||
|
dv.setUint8(at, this.compression); at++;
|
||||||
|
dv.setUint8(at, this.filter); at++;
|
||||||
|
dv.setUint8(at, this.interlace); at++;
|
||||||
|
dv.setUint32(at, crc32(a, orig + 4, this.buffer_size() - 8)); at += 4;
|
||||||
|
return at;
|
||||||
|
},
|
||||||
|
buffer_size: function()
|
||||||
|
{
|
||||||
|
return 12 + 13;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function adler()
|
||||||
|
{
|
||||||
|
this.s1 = 1;
|
||||||
|
this.s2 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
adler.prototype.update = function(b)
|
||||||
|
{
|
||||||
|
this.s1 += b;
|
||||||
|
this.s1 %= 65521;
|
||||||
|
this.s2 += this.s1;
|
||||||
|
this.s2 %= 65521;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The image as one IDAT chunk: a zlib stream of stored (uncompressed)
|
||||||
|
deflate blocks. A stored block holds at most 65535 bytes, so the
|
||||||
|
scanlines are cut into as many as it takes; a cursor larger than about
|
||||||
|
128 pixels square used to be refused here. */
|
||||||
|
var STORED_BLOCK_MAX = 65535;
|
||||||
|
|
||||||
|
function PngIDAT(width, height, bytes)
|
||||||
|
{
|
||||||
|
this.data = bytes;
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
/* Every scanline is filter type 0 followed by its pixels. */
|
||||||
|
this.raw_size = this.height * (1 + this.width * 4);
|
||||||
|
this.blocks = Math.max(1, Math.ceil(this.raw_size / STORED_BLOCK_MAX));
|
||||||
|
}
|
||||||
|
|
||||||
|
PngIDAT.prototype =
|
||||||
|
{
|
||||||
|
to_buffer: function(a, at)
|
||||||
|
{
|
||||||
|
at = at || 0;
|
||||||
|
var orig = at;
|
||||||
|
var x, y, i;
|
||||||
|
var dv = new DataView(a);
|
||||||
|
var zsum = new adler();
|
||||||
|
dv.setUint32(at, this.buffer_size() - 12); at += 4;
|
||||||
|
dv.setUint8(at, 'I'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'D'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'A'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'T'.charCodeAt(0)); at++;
|
||||||
|
|
||||||
|
/* zlib header: deflate, 32K window, no preset dictionary. */
|
||||||
|
dv.setUint8(at, 0x78); at++;
|
||||||
|
dv.setUint8(at, 0x01); at++;
|
||||||
|
|
||||||
|
var u8 = new Uint8Array(this.data);
|
||||||
|
var out = new Uint8Array(a);
|
||||||
|
var left = this.raw_size;
|
||||||
|
var block_left = 0;
|
||||||
|
var put = function(b)
|
||||||
|
{
|
||||||
|
if (block_left == 0)
|
||||||
|
{
|
||||||
|
/* Stored block: BFINAL in bit 0, BTYPE 00, then LEN and
|
||||||
|
its one's complement, little-endian. */
|
||||||
|
block_left = Math.min(left, STORED_BLOCK_MAX);
|
||||||
|
out[at++] = left <= STORED_BLOCK_MAX ? 1 : 0;
|
||||||
|
out[at++] = block_left & 0xff;
|
||||||
|
out[at++] = block_left >> 8;
|
||||||
|
out[at++] = ~block_left & 0xff;
|
||||||
|
out[at++] = (~block_left >> 8) & 0xff;
|
||||||
|
}
|
||||||
|
out[at++] = b;
|
||||||
|
zsum.update(b);
|
||||||
|
block_left--;
|
||||||
|
left--;
|
||||||
|
};
|
||||||
|
for (i = 0, y = 0; y < this.height; y++)
|
||||||
|
{
|
||||||
|
put(0);
|
||||||
|
for (x = 0; x < this.width; x++)
|
||||||
|
{
|
||||||
|
put(u8[i] || 0); i++;
|
||||||
|
put(u8[i] || 0); i++;
|
||||||
|
put(u8[i] || 0); i++;
|
||||||
|
put(u8[i] || 0); i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* zlib checksum, big-endian. */
|
||||||
|
dv.setUint16(at, zsum.s2); at += 2;
|
||||||
|
dv.setUint16(at, zsum.s1); at += 2;
|
||||||
|
|
||||||
|
dv.setUint32(at, crc32(a, orig + 4, this.buffer_size() - 8)); at += 4;
|
||||||
|
return at;
|
||||||
|
},
|
||||||
|
buffer_size: function()
|
||||||
|
{
|
||||||
|
return 12 + 2 + this.blocks * 5 + this.raw_size + 4;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function PngIEND()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
PngIEND.prototype =
|
||||||
|
{
|
||||||
|
to_buffer: function(a, at)
|
||||||
|
{
|
||||||
|
at = at || 0;
|
||||||
|
var orig = at;
|
||||||
|
var dv = new DataView(a);
|
||||||
|
dv.setUint32(at, this.buffer_size() - 12); at += 4;
|
||||||
|
dv.setUint8(at, 'I'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'E'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'N'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint8(at, 'D'.charCodeAt(0)); at++;
|
||||||
|
dv.setUint32(at, crc32(a, orig + 4, this.buffer_size() - 8)); at += 4;
|
||||||
|
return at;
|
||||||
|
},
|
||||||
|
buffer_size: function()
|
||||||
|
{
|
||||||
|
return 12;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function create_rgba_png(width, height, bytes)
|
||||||
|
{
|
||||||
|
var i;
|
||||||
|
var ihdr = new PngIHDR(width, height);
|
||||||
|
var idat = new PngIDAT(width, height, bytes);
|
||||||
|
var iend = new PngIEND;
|
||||||
|
|
||||||
|
var mb = new ArrayBuffer(ihdr.buffer_size() + idat.buffer_size() + iend.buffer_size());
|
||||||
|
var at = ihdr.to_buffer(mb);
|
||||||
|
at = idat.to_buffer(mb, at);
|
||||||
|
at = iend.to_buffer(mb, at);
|
||||||
|
|
||||||
|
var u8 = new Uint8Array(mb);
|
||||||
|
var str = "";
|
||||||
|
for (i = 0; i < at; i++)
|
||||||
|
{
|
||||||
|
str += "%";
|
||||||
|
if (u8[i] < 16)
|
||||||
|
str += "0";
|
||||||
|
str += u8[i].toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return "%89PNG%0D%0A%1A%0A" + str;
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
create_rgba_png,
|
||||||
|
};
|
||||||
+94
@@ -0,0 +1,94 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2016 by Oliver Gutierrez <ogutsua@gmail.com>
|
||||||
|
Miroslav Chodil <mchodil@redhat.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Constants } from './enums.js';
|
||||||
|
import { DEBUG, arraybuffer_to_str } from './utils.js';
|
||||||
|
import { SpiceConn } from './spiceconn.js';
|
||||||
|
import { SpiceMsgPortInit } from './spicemsg.js';
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpicePortConn
|
||||||
|
** Drive the Spice Port Channel
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function SpicePortConn()
|
||||||
|
{
|
||||||
|
DEBUG > 0 && console.log('SPICE port: created SPICE port channel. Args:', arguments);
|
||||||
|
SpiceConn.apply(this, arguments);
|
||||||
|
this.portName = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
SpicePortConn.prototype = Object.create(SpiceConn.prototype);
|
||||||
|
|
||||||
|
SpicePortConn.prototype.process_channel_message = function(msg)
|
||||||
|
{
|
||||||
|
if (msg.type == Constants.SPICE_MSG_PORT_INIT)
|
||||||
|
{
|
||||||
|
if (this.portName === null)
|
||||||
|
{
|
||||||
|
var m = new SpiceMsgPortInit(msg.data);
|
||||||
|
this.portName = arraybuffer_to_str(new Uint8Array(m.name));
|
||||||
|
this.portOpened = m.opened
|
||||||
|
DEBUG > 0 && console.log('SPICE port: Port', this.portName, 'initialized');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUG > 0 && console.log('SPICE port: Port', this.portName, 'is already initialized.');
|
||||||
|
}
|
||||||
|
else if (msg.type == Constants.SPICE_MSG_PORT_EVENT)
|
||||||
|
{
|
||||||
|
DEBUG > 0 && console.log('SPICE port: Port event received for', this.portName, msg);
|
||||||
|
var event = new CustomEvent('spice-port-event', {
|
||||||
|
detail: {
|
||||||
|
channel: this,
|
||||||
|
spiceEvent: new Uint8Array(msg.data)
|
||||||
|
},
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
window.dispatchEvent(event);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (msg.type == Constants.SPICE_MSG_SPICEVMC_DATA)
|
||||||
|
{
|
||||||
|
DEBUG > 0 && console.log('SPICE port: Data received in port', this.portName, msg);
|
||||||
|
var event = new CustomEvent('spice-port-data', {
|
||||||
|
detail: {
|
||||||
|
channel: this,
|
||||||
|
data: msg.data
|
||||||
|
},
|
||||||
|
bubbles: true,
|
||||||
|
cancelable: true
|
||||||
|
});
|
||||||
|
window.dispatchEvent(event);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DEBUG > 0 && console.log('SPICE port: SPICE message type not recognized:', msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
export {
|
||||||
|
SpicePortConn,
|
||||||
|
};
|
||||||
+1345
File diff suppressed because it is too large
Load Diff
+87
@@ -0,0 +1,87 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2014 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** resize.js
|
||||||
|
** This bit of Javascript is a set of logic to help with window
|
||||||
|
** resizing, using the agent channel to request screen resizes.
|
||||||
|
**
|
||||||
|
** It's a bit tricky, as we want to wait for resizing to settle down
|
||||||
|
** before sending a size. Further, while horizontal resizing to use the whole
|
||||||
|
** browser width is fairly easy to arrange with css, resizing an element to use
|
||||||
|
** the whole vertical space (or to force a middle div to consume the bulk of the browser
|
||||||
|
** window size) is tricky, and the consensus seems to be that Javascript is
|
||||||
|
** the only right way to do it.
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function resize_helper(sc)
|
||||||
|
{
|
||||||
|
var w = document.getElementById(sc.screen_id).clientWidth;
|
||||||
|
var m = document.getElementById(sc.message_id);
|
||||||
|
|
||||||
|
/* Resize vertically; basically we leave a 20 pixel margin
|
||||||
|
at the bottom, and use the position of the message window
|
||||||
|
to figure out how to resize */
|
||||||
|
|
||||||
|
var h = window.innerHeight - 20;
|
||||||
|
|
||||||
|
/* Screen height based on message console visibility. A hidden console
|
||||||
|
occupies no space, so only a visible one reduces the screen; its
|
||||||
|
offsetHeight already includes the client area, so subtract it once. */
|
||||||
|
if (m != null &&
|
||||||
|
window.getComputedStyle(m).getPropertyValue("display") != 'none')
|
||||||
|
{
|
||||||
|
h = h - m.offsetHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* Xorg requires height be a multiple of 8; round down */
|
||||||
|
if (h % 8 > 0)
|
||||||
|
h -= (h % 8);
|
||||||
|
|
||||||
|
/* Xorg requires width be a multiple of 8; round down */
|
||||||
|
if (w % 8 > 0)
|
||||||
|
w -= (w % 8);
|
||||||
|
|
||||||
|
|
||||||
|
sc.resize_window(0, w, h, 32, 0, 0);
|
||||||
|
sc.spice_resize_timer = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handle_resize(e)
|
||||||
|
{
|
||||||
|
var sc = window.spice_connection;
|
||||||
|
|
||||||
|
/* The listener can outlive the connection it was registered for. */
|
||||||
|
if (!sc)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (sc.spice_resize_timer)
|
||||||
|
{
|
||||||
|
window.clearTimeout(sc.spice_resize_timer);
|
||||||
|
sc.spice_resize_timer = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
sc.spice_resize_timer = window.setTimeout(resize_helper, 200, sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
resize_helper,
|
||||||
|
handle_resize,
|
||||||
|
};
|
||||||
+206
@@ -0,0 +1,206 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2013 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpiceSimulateCursor
|
||||||
|
** Internet Explorer 10 does not support data uri's in cursor assignment.
|
||||||
|
** This file provides a number of gimmicks to compensate. First, if there
|
||||||
|
** is a preloaded cursor available, we will use that. Failing that, we will
|
||||||
|
** simulate a cursor using an image that is moved around the screen.
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
import { hex_sha1 } from './thirdparty/sha1.js';
|
||||||
|
|
||||||
|
var SpiceSimulateCursor = {
|
||||||
|
|
||||||
|
cursors : new Array(),
|
||||||
|
unknown_cursors : new Array(),
|
||||||
|
warned: false,
|
||||||
|
|
||||||
|
add_cursor: function(sha1, value)
|
||||||
|
{
|
||||||
|
SpiceSimulateCursor.cursors[sha1] = value;
|
||||||
|
},
|
||||||
|
|
||||||
|
unknown_cursor: function(sha1, curdata)
|
||||||
|
{
|
||||||
|
if (! SpiceSimulateCursor.warned)
|
||||||
|
{
|
||||||
|
SpiceSimulateCursor.warned = true;
|
||||||
|
/* Once a modal alert; a console does not stop the session for it. */
|
||||||
|
console.warn("This browser does not take dynamic cursors; cursors will be " +
|
||||||
|
"simulated with images. See the console for how to supply static cursor files.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! SpiceSimulateCursor.unknown_cursors[sha1])
|
||||||
|
{
|
||||||
|
SpiceSimulateCursor.unknown_cursors[sha1] = curdata;
|
||||||
|
console.log('Unknown cursor. Simulation required. To avoid simulation for this cursor, create and include a custom javascript file, and add the following line:');
|
||||||
|
console.log('SpiceSimulateCursor.add_cursor("' + sha1 + '", "<your filename here>.cur");');
|
||||||
|
console.log('And then run following command, redirecting output into <your filename here>.cur:');
|
||||||
|
console.log('php -r "echo urldecode(\'' + curdata + '\');"');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
/* force: the browser cannot show this shape as a CSS cursor (too large),
|
||||||
|
so it is drawn under the pointer without consulting the computed style. */
|
||||||
|
simulate_cursor: function (spicecursor, cursor, screen, pngstr, force)
|
||||||
|
{
|
||||||
|
var cursor_sha = hex_sha1(pngstr + ' ' + cursor.header.hot_spot_x + ' ' + cursor.header.hot_spot_y);
|
||||||
|
if (! force && typeof SpiceSimulateCursor.cursors != 'undefined')
|
||||||
|
if (typeof SpiceSimulateCursor.cursors[cursor_sha] != 'undefined')
|
||||||
|
{
|
||||||
|
var curstr = 'url(' + SpiceSimulateCursor.cursors[cursor_sha] + '), default';
|
||||||
|
screen.style.cursor = curstr;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (force || window.getComputedStyle(screen, null).cursor == 'auto')
|
||||||
|
{
|
||||||
|
if (! force)
|
||||||
|
SpiceSimulateCursor.unknown_cursor(cursor_sha,
|
||||||
|
SpiceSimulateCursor.create_icondir(cursor.header.width, cursor.header.height,
|
||||||
|
cursor.data.byteLength, cursor.header.hot_spot_x, cursor.header.hot_spot_y) + pngstr);
|
||||||
|
|
||||||
|
document.getElementById(spicecursor.parent.screen_id).style.cursor = 'none';
|
||||||
|
if (! spicecursor.spice_simulated_cursor)
|
||||||
|
{
|
||||||
|
spicecursor.spice_simulated_cursor = document.createElement('img');
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.style.position = 'absolute';
|
||||||
|
spicecursor.spice_simulated_cursor.style.display = 'none';
|
||||||
|
spicecursor.spice_simulated_cursor.style.overflow = 'hidden';
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.spice_screen = document.getElementById(spicecursor.parent.screen_id);
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.addEventListener('mousemove', SpiceSimulateCursor.handle_sim_mousemove);
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.spice_screen.appendChild(spicecursor.spice_simulated_cursor);
|
||||||
|
}
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.src = 'data:image/png,' + pngstr;
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.spice_hot_x = cursor.header.hot_spot_x;
|
||||||
|
spicecursor.spice_simulated_cursor.spice_hot_y = cursor.header.hot_spot_y;
|
||||||
|
|
||||||
|
spicecursor.spice_simulated_cursor.style.pointerEvents = "none";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (spicecursor.spice_simulated_cursor)
|
||||||
|
{
|
||||||
|
spicecursor.spice_simulated_cursor.spice_screen.removeChild(spicecursor.spice_simulated_cursor);
|
||||||
|
delete spicecursor.spice_simulated_cursor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
handle_sim_mousemove: function(e)
|
||||||
|
{
|
||||||
|
var f = SpiceSimulateCursor.duplicate_mouse_event(e, this.spice_screen);
|
||||||
|
return this.spice_screen.dispatchEvent(f);
|
||||||
|
},
|
||||||
|
|
||||||
|
duplicate_mouse_event: function(e, target)
|
||||||
|
{
|
||||||
|
var evt = document.createEvent("mouseevent");
|
||||||
|
evt.initMouseEvent(e.type, true, true, e.view, e.detail,
|
||||||
|
e.screenX, e.screenY, e.clientX, e.clientY,
|
||||||
|
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey, e.button, e.relatedTarget);
|
||||||
|
return evt;
|
||||||
|
},
|
||||||
|
|
||||||
|
ICONDIR: function ()
|
||||||
|
{
|
||||||
|
},
|
||||||
|
|
||||||
|
ICONDIRENTRY: function(width, height, bytes, hot_x, hot_y)
|
||||||
|
{
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
this.bytes = bytes;
|
||||||
|
this.hot_x = hot_x;
|
||||||
|
this.hot_y = hot_y;
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
create_icondir: function (width, height, bytes, hot_x, hot_y)
|
||||||
|
{
|
||||||
|
var i;
|
||||||
|
var header = new SpiceSimulateCursor.ICONDIR();
|
||||||
|
var entry = new SpiceSimulateCursor.ICONDIRENTRY(width, height, bytes, hot_x, hot_y);
|
||||||
|
|
||||||
|
var mb = new ArrayBuffer(header.buffer_size() + entry.buffer_size());
|
||||||
|
var at = header.to_buffer(mb);
|
||||||
|
at = entry.to_buffer(mb, at);
|
||||||
|
|
||||||
|
var u8 = new Uint8Array(mb);
|
||||||
|
var str = "";
|
||||||
|
for (i = 0; i < at; i++)
|
||||||
|
{
|
||||||
|
str += "%";
|
||||||
|
if (u8[i] < 16)
|
||||||
|
str += "0";
|
||||||
|
str += u8[i].toString(16);
|
||||||
|
}
|
||||||
|
return str;
|
||||||
|
},
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
SpiceSimulateCursor.ICONDIR.prototype =
|
||||||
|
{
|
||||||
|
to_buffer: function(a, at)
|
||||||
|
{
|
||||||
|
at = at || 0;
|
||||||
|
var dv = new DataView(a);
|
||||||
|
dv.setUint16(at, 0, true); at += 2;
|
||||||
|
dv.setUint16(at, 2, true); at += 2;
|
||||||
|
dv.setUint16(at, 1, true); at += 2;
|
||||||
|
return at;
|
||||||
|
},
|
||||||
|
buffer_size: function()
|
||||||
|
{
|
||||||
|
return 6;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
SpiceSimulateCursor.ICONDIRENTRY.prototype =
|
||||||
|
{
|
||||||
|
to_buffer: function(a, at)
|
||||||
|
{
|
||||||
|
at = at || 0;
|
||||||
|
var dv = new DataView(a);
|
||||||
|
dv.setUint8(at, this.width); at++;
|
||||||
|
dv.setUint8(at, this.height); at++;
|
||||||
|
dv.setUint8(at, 0); at++; /* color palette count, unused */
|
||||||
|
dv.setUint8(at, 0); at++; /* reserved */
|
||||||
|
dv.setUint16(at, this.hot_x, true); at += 2;
|
||||||
|
dv.setUint16(at, this.hot_y, true); at += 2;
|
||||||
|
dv.setUint32(at, this.bytes, true); at += 4;
|
||||||
|
dv.setUint32(at, at + 4, true); at += 4; /* Offset to bytes */
|
||||||
|
return at;
|
||||||
|
},
|
||||||
|
buffer_size: function()
|
||||||
|
{
|
||||||
|
return 16;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export { SpiceSimulateCursor };
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
"use strict";
|
||||||
|
/*
|
||||||
|
Copyright (C) 2012 by Jeremy P. White <jwhite@codeweavers.com>
|
||||||
|
|
||||||
|
This file is part of spice-html5.
|
||||||
|
|
||||||
|
spice-html5 is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
spice-html5 is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
|
along with spice-html5. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*----------------------------------------------------------------------------
|
||||||
|
** SpiceArrayBufferSlice
|
||||||
|
** This function is a work around for IE 10, which has no slice()
|
||||||
|
** method in it's subclass.
|
||||||
|
**--------------------------------------------------------------------------*/
|
||||||
|
function SpiceArrayBufferSlice(start, end)
|
||||||
|
{
|
||||||
|
start = start || 0;
|
||||||
|
end = end || this.byteLength;
|
||||||
|
if (end < 0)
|
||||||
|
end = this.byteLength + end;
|
||||||
|
if (start < 0)
|
||||||
|
start = this.byteLength + start;
|
||||||
|
if (start < 0)
|
||||||
|
start = 0;
|
||||||
|
if (end < 0)
|
||||||
|
end = 0;
|
||||||
|
if (end > this.byteLength)
|
||||||
|
end = this.byteLength;
|
||||||
|
if (start > end)
|
||||||
|
start = end;
|
||||||
|
|
||||||
|
var ret = new ArrayBuffer(end - start);
|
||||||
|
var in1 = new Uint8Array(this, start, end - start);
|
||||||
|
var out = new Uint8Array(ret);
|
||||||
|
var i;
|
||||||
|
|
||||||
|
for (i = 0; i < end - start; i++)
|
||||||
|
out[i] = in1[i];
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! ArrayBuffer.prototype.slice)
|
||||||
|
{
|
||||||
|
ArrayBuffer.prototype.slice = SpiceArrayBufferSlice;
|
||||||
|
console.log("WARNING: ArrayBuffer.slice() is missing; we are extending ArrayBuffer to compensate");
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user