workspace

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2026 License: BSD-3-Clause Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package workspace owns the per-user microVM that the loom-server shells + compile jobs run in. One VM per identity, persistent VMDK, brought up on first login + kept warm for the rest of the session.

Two backends share the same Provisioner interface :

  • Local : the dev-mode QEMU/TCG path. Boots an actual microVM via the standard weft-microvm-kernel + workspace image on the loom-server host (Mac with Apple-VZ- disabled QEMU is the canonical dev box). Slow cold boot (~10s) but the VM is reused across SPA reloads so the cost is paid once.

  • Agent : the prod path. Talks to a weft-agent on a hypervisor host and asks for a workspace VM to be placed there. The shell + compile traffic still flows through NATS (weft.exec.<vmID>.<sid>.{in,out}) — the loom-server is a thin router in both cases.

In dev mode (no WEFT_AGENT_URL + no WEFT_NATS_URL) the workspace falls back to the legacy local-pty path in api_shell.go : the user gets a host bash, a "dev mode no isolation" banner shows up in the SPA, compile uses the on-host pdflatex / go / pandoc.

Index

Constants

View Source
const QcowImageMediaType = "application/vnd.openweft.workspace.qcow2.image"

QcowImageMediaType is the OCI artifact layer mediatype the weft-loom-workspace bake publishes the qcow2 under.

Variables

This section is empty.

Functions

func EmbeddedNATSDisabled

func EmbeddedNATSDisabled() bool

EmbeddedNATSDisabled reports whether the operator opted out of auto-starting the embedded broker (e.g. they already run one).

func QEMUBackendSelected

func QEMUBackendSelected() bool

QEMUBackendSelected reports whether the operator picked the QEMU workspace backend via WEFT_LOOM_WORKSPACE_BACKEND=qemu. Devagent (in-process) is the default for unset/anything else.

func VMIDForIdentity

func VMIDForIdentity(id Identity) string

VMIDForIdentity is a stable hash of the user's subject so two runs of the loom-server land on the same VM directory (and the same NATS subjects, so JetStream replay of containers + mounts finds the previously-desired state). Lowercased hex prefix — short enough to read in a log line, deterministic enough to blame a user's VM in incident review.

Types

type DevAgentSpawner

type DevAgentSpawner interface {
	Spawn(nc *nats.Conn, vmID, workDir string) error
}

DevAgentSpawner is the callback the loom-server boot path injects to start an in-process mock of weft-microvm-agent. Lives in internal/devagent ; we keep it abstracted here so workspace stays import-free of devagent (which pulls in os/exec + pty).

type Identity

type Identity struct {
	Subject string
	Email   string
}

Identity is the auth-identity slice the workspace cares about. We keep a small purpose-fitted view rather than depending on the full auth.Identity so the package stays test-friendly.

type ImagePuller

type ImagePuller struct {
	// contains filtered or unexported fields
}

ImagePuller caches OCI artifact pulls under a per-host directory. One puller per loom-server lifetime ; concurrent ResolveBaseQcow2 calls for the same digest share the cached file.

func NewImagePuller

func NewImagePuller(cacheDir string) *ImagePuller

NewImagePuller builds the puller backed by cacheDir (auto-created on first pull). cacheDir == "" defaults to ~/.weft-loom/images/.

func (*ImagePuller) ResolveBaseQcow2

func (p *ImagePuller) ResolveBaseQcow2(ctx context.Context, ref string) (string, error)

ResolveBaseQcow2 returns the local path to the base qcow2 for ref. When ref is a local file path, returns it as-is. When ref looks like an OCI reference, pulls the artifact (or returns the cached path if the digest is already on disk).

type LocalProvisioner

type LocalProvisioner struct {
	NATSURL string // empty = disabled (host pty fallback)
	// QEMUBinary + KernelImage + WorkspaceImage are the inputs the
	// real QEMU boot will need ; left as fields here so the Ensure
	// impl can wire them via env vars without exposing them through
	// the loom-server's main flag surface.
	QEMUBinary     string
	KernelImage    string
	WorkspaceImage string
	// DevAgent boots an in-process mock of weft-microvm-agent when
	// non-nil. The loom-server boot path injects this when running
	// against the embedded broker so a SPA shell click lands on a
	// working host pty via the same NATS round-trip a real
	// production cluster would use.
	DevAgent DevAgentSpawner
}

LocalProvisioner is the unified entry point for the two dev-mode modes. Picks QEMU when the env vars are wired, falls through to the host-pty mode otherwise.

func NewLocalFromEnv

func NewLocalFromEnv() *LocalProvisioner

NewLocalFromEnv reads the standard weft env vars and returns a LocalProvisioner ready for use. Same env-var conventions as the other weft control plane components — WEFT_NATS_URL is the broker the in-VM agent subscribes to, WEFT_QEMU is the binary path.

In dev mode (WEFT_NATS_URL unset, WEFT_LOOM_DISABLE_EMBEDDED_NATS unset), the constructor leaves NATSURL empty ; the loom-server boot path replaces it with the embedded broker's URL by calling SetNATSURL before Ensure runs. This keeps the env-var contract stable while the embedded path stays opt-in via the disable knob.

func (*LocalProvisioner) Ensure

func (p *LocalProvisioner) Ensure(ctx context.Context, ident Identity) (vm *VM, err error)

Ensure returns a workspace VM. When NATS is unset the result is a sentinel signalling "fall back to host pty" — see api_shell.go.

func (*LocalProvisioner) SetNATSURL

func (p *LocalProvisioner) SetNATSURL(url string)

SetNATSURL late-binds the broker URL after the embedded broker has started ; called by the loom-server boot path so Ensure picks up the embedded URL automatically.

type NATSPubSub

type NATSPubSub interface {
	Publish(subject string, data []byte) error
	Subscribe(subject string, cb func(subj string, data []byte)) (Unsubscriber, error)
}

NATSPubSub is the slice of nats.Conn the workspace uses : Publish + Subscribe. Kept abstract so the shell handler is testable with an in-memory stub without standing up a broker.

func NewNATSConn

func NewNATSConn(nc *nats.Conn) NATSPubSub

NewNATSConn wraps nc as a NATSPubSub. Caller retains ownership of nc — closing it after is fine, the handler's subscriptions fail cleanly through their cb (the cb is best-effort).

type Provisioner

type Provisioner interface {
	Ensure(ctx context.Context, ident Identity) (*VM, error)
}

Provisioner is the abstract VM lifecycle owner. Each impl handles boot + image refresh + teardown idempotently.

type QEMUProvisioner

type QEMUProvisioner struct {
	NATSURL      string
	GuestNATSURL string
	// GuestAuth, when set, returns a per-VM broker credential that is
	// embedded in the URL given to the guest, and a revoke func called
	// when the VM exits.
	GuestAuth      func(vmID string) (user, pass string, revoke func(), err error)
	QEMUBinary     string
	KernelImage    string
	WorkspaceImage string
	// ImagePuller resolves WEFT_WORKSPACE_BASE_QCOW2 — when it
	// looks like an OCI ref, the puller fetches the artifact
	// + caches it ; when it's a local path, returned as-is.
	// nil = local-only mode (used in tests).
	ImagePuller *ImagePuller
	// ProjectsRoot is the loom-server's storage root path on the
	// host. The boot mounts <ProjectsRoot>/<subject>/ at /workspace
	// inside the guest via virtio-9p so the shell sees the user's
	// actual project files. The shell + compile path always run
	// against this mount.
	ProjectsRoot string
	// ToolsPath is the host directory that holds unpacked OCI tool
	// rootfs (texlive / markdown / ...) + their pre-rendered crun
	// bundles. Mounted at /opt/tools/ inside the guest read-only.
	// One copy on the host serves every user VM ; the tool wrappers
	// `crun exec` against the bundle without pulling the 5 GiB
	// texlive image per-VM. Same wire (virtio-9p) as /workspace ;
	// in prod the same dir is backed by a weft-block volume with
	// per-tool R/O snapshots exposed via NBD.
	ToolsPath string
	Logger    *slog.Logger
}

QEMUProvisioner boots one workspace microVM per Identity.

NATSURL is the URL the loom-server itself uses (typically 127.0.0.1:4222 for the embedded broker, or the cluster broker LB in prod). GuestNATSURL is the URL the VM's agent dials — usually the same in prod (a NATS LB reachable from both), but with the embedded broker in dev it's a different shape : QEMU's user-mode SLIRP maps `10.0.2.2` to the host's `127.0.0.1`, so the guest needs `nats://10.0.2.2:4222` even when the loom-server itself uses `nats://127.0.0.1:4222`.

func NewQEMUFromEnv

func NewQEMUFromEnv(logger *slog.Logger) *QEMUProvisioner

NewQEMUFromEnv builds a QEMUProvisioner from the env-var surface : WEFT_NATS_URL, WEFT_QEMU, WEFT_WORKSPACE_KERNEL_IMAGE, WEFT_WORKSPACE_IMAGE, WEFT_LOOM_STORAGE_ROOT, WEFT_LOOM_TOOLS_PATH. Mirrors NewLocalFromEnv so the operator only learns one set of variable names.

func (*QEMUProvisioner) Ensure

func (p *QEMUProvisioner) Ensure(ctx context.Context, ident Identity) (*VM, error)

Ensure boots (or returns a cached handle for) the user's workspace microVM. The current impl validates the host has the needed tools available + materialises the per-user directory ; the QEMU exec itself is the next sub-step (it spans the kernel + rootfs pull paths, which we land in a dedicated PR so this file stays reviewable).

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry caches Ensure results so concurrent SPA reloads don't each kick off a fresh boot. Per-identity result + a "dead" flag : when a provisioner-spawned VM exits (QEMU killed, process panic, host reboot), the next Ensure invalidates the cache + re-runs the provisioner. The provisioner signals death by closing the VM's Dead channel (added in V0.5.1).

func NewRegistry

func NewRegistry(p Provisioner) *Registry

NewRegistry wraps p in a per-identity memoiser.

func (*Registry) Close

func (r *Registry) Close(ctx context.Context)

Close shuts down every live workspace VM in the registry. Called by the loom-server's main on SIGTERM. Best-effort : a failed graceful shutdown still attempts the next VM.

func (*Registry) Ensure

func (r *Registry) Ensure(ctx context.Context, ident Identity) (*VM, error)

Ensure returns the user's workspace VM, booting it on first call. Subsequent calls return the cached result, unless the previous VM exited — in which case the cache is invalidated + a fresh boot triggered. The provisioner is responsible for signalling death via VM.Dead (closed by the boot goroutine when QEMU exits).

type Unsubscriber

type Unsubscriber interface {
	Unsubscribe() error
}

Unsubscriber lets the shell handler tear down its in-subject subscription when the SPA WS hangs up.

type VM

type VM struct {
	VMID    string
	NATSURL string
	Conn    NATSPubSub
	// WorkDir is where the in-VM agent's shell pty boots into. In
	// dev mode the in-process devagent uses this to pick the host
	// directory ; the real agent inside the workspace VM uses its
	// own /workspace mount and ignores this hint.
	WorkDir string
	// Ready signals when the VM has finished its first-boot
	// reconcile (CubeFS mount, weft-microvm-agent's subscribers
	// warm). Closed by the provisioner ; never reopened.
	Ready <-chan struct{}
	// Dead signals that the VM exited — QEMU killed, panicked,
	// host reboot. The Registry watches this to invalidate its
	// cache on the next Ensure so the user gets a fresh boot
	// rather than a stale handle. Provisioners that don't track
	// VM death (e.g. the no-op dev-local-pty sentinel) may leave
	// this nil.
	Dead <-chan struct{}
	// Health is "healthy" once the agent's heartbeat is recent. The
	// router uses this to decide whether to publish into the in
	// subject or surface a 503 to the SPA.
	Health func() string
	// Close releases any provisioner-owned resources (heartbeat sub,
	// nats.Conn) so loom-server shutdown doesn't leak them. Provisioners
	// that don't own such handles leave it nil.
	Close func()
}

VM bundles everything the shell + compile routers need to wire a session to the user's workspace. VMID is what gets baked into the NATS subjects ; NATSURL is the broker the agent inside the VM is subscribed on ; Conn is the live nats.Conn the loom-server uses to publish + subscribe in the user's behalf (nil when the provisioner is in host-pty mode).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL