From d55be2e211698b148893ddf49c08b2e4ba1ab7f2 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 12:02:26 -0800 Subject: [PATCH] refactor(computer): thin disks' compose machinery in its own module, Windows' own in windows_disk thin_disk holds what composing a thin disk on its base does whatever the guest OS: the image store's registry, the base's and disk's names, pulling a base with its admin's key, booting it on a kept overlay, giving it its parts over ssh, trimming, shutting down, converting, packing and pushing. A Guest names what's the OS's own: its admin, the launcher's guest file, and the commands its shell runs to prepare, unpack, provision, clean up, compact and shut down. windows_disk keeps the evaluation ISO, the unattended install, the evaluation's expiry and the PowerShell provisioner, and declares Windows' Guest. No behaviour changes. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/computers/README.md | 42 +-- .../env/envs/computer/utils/composer.py | 9 +- .../env/envs/computer/utils/thin_disk.py | 315 ++++++++++++++++ .../env/envs/computer/utils/windows_disk.py | 354 +++--------------- tst/unit/env/envs/test_computer_windows.py | 36 +- tst/unit/env/envs/test_thin_disk.py | 88 +++++ 6 files changed, 498 insertions(+), 346 deletions(-) create mode 100644 src/agent_env/env/envs/computer/utils/thin_disk.py create mode 100644 tst/unit/env/envs/test_thin_disk.py diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index 671428765..c5e8ca415 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -169,27 +169,27 @@ are, under a tag of its recipe, so another machine composing on it pulls it rath no ISO. A compose installs a base afresh once fewer than 7 days of its evaluation are left, from the ISO Microsoft's link serves then. Name it as `windows-11`, or by its id, `@local/agentenv-framework/computers/windows-11`. -A Windows computer env is composed on the same machine (`env/envs/computer/utils/windows_disk.py`): the base booted on -an overlay kept in a folder of its own, each layer installed over ssh as `agentadmin` by a PowerShell script composing -writes, as its `[install.windows]` says (its files under `C:\agentenv\drivers\`, or an app's under -`C:\agentenv\apps\`, its downloads checked by sha256, its venv on the pinned Python, its PowerShell run scripts, -elevated, an app's install steps, elevated too, and its services), then shut down and -packed onto the base's image, so every computer composed on a base shares one copy of it, and pushed to the env's image -store under its key, pinned, as a Linux disk is; a compose of the same inputs on any machine reuses it, and a deploy -pulls it. Gate 3 probes it at full. At the session user's sign-in, -`agentenv-session`, run windowless by `pythonw`, reads the deploy's environment from the NoCloud seed, which the -launcher attaches as a removable USB drive, since Windows mounts a FAT filesystem with no partition table on removable -media alone, and starts each layer's services in that session, again whenever one exits. A Windows computer boots -with 4 vCPUs and 8 GB, on local Docker with KVM and on `modal_vm`; it has computer-exec and data/add, through the -protocol's Windows defaults, so the probe runs its fixture there as on Linux, with Python one-liners for the commands it -would give a shell. On `modal_vm` it runs in your own Modal workspace, under the evaluation's terms or a licence of your -own; whether they allow it there is yours to check. Composing leaves `C:\agentenv` the admins', as `/opt/agentenv` is -root's on Linux: every user, and every sandboxed process (Chrome's, as Program Files allows it), reads and runs -what's there, and the session user writes only the services' logs and each -app's `.agentenv` folder. An app's own server runs in the session as its services do, on a loopback port it writes -with a token to that folder, which only the session user, SYSTEM and Administrators reach; it launches the app in a -Job object, which ends whatever the app started. Composing signs in as `agentadmin`, so -an app's install step for the session user can't run on Windows, and gate 1 refuses an app that declares one there. +A Windows computer env is composed on the same machine as a thin disk (`env/envs/computer/utils/thin_disk.py`, with +what's Windows' own in `windows_disk.py`): the base booted on an overlay kept in a folder of its own, each layer +installed over ssh as `agentadmin` by a PowerShell script composing writes, as its `[install.windows]` says (its files +under `C:\agentenv\drivers\`, or an app's under `C:\agentenv\apps\`, its downloads checked by sha256, its +venv on the pinned Python, its PowerShell run scripts, elevated, an app's install steps, elevated too, and its +services), then shut down and packed onto the base's image, so every computer composed on a base shares one copy of it, +and pushed to the env's image store under its key, pinned, as a Linux disk is; a compose of the same inputs on any +machine reuses it, and a deploy pulls it. Gate 3 probes it at full. At the session user's sign-in, `agentenv-session`, +run windowless by `pythonw`, reads the deploy's environment from the NoCloud seed, which the launcher attaches as a +removable USB drive, since Windows mounts a FAT filesystem with no partition table on removable media alone, and starts +each layer's services in that session, again whenever one exits. A Windows computer boots with 4 vCPUs and 8 GB, on +local Docker with KVM and on `modal_vm`; it has computer-exec and data/add, through the protocol's Windows defaults, so +the probe runs its fixture there as on Linux, with Python one-liners for the commands it would give a shell. On +`modal_vm` it runs in your own Modal workspace, under the evaluation's terms or a licence of your own; whether they +allow it there is yours to check. Composing leaves `C:\agentenv` the admins', as `/opt/agentenv` is root's on Linux: +every user, and every sandboxed process (Chrome's, as Program Files allows it), reads and runs what's there, and the +session user writes only the services' logs and each app's `.agentenv` folder. An app's own server runs in the session +as its services do, on a loopback port it writes with a token to that folder, which only the session user, SYSTEM and +Administrators reach; it launches the app in a Job object, which ends whatever the app started. Composing signs in as +`agentadmin`, so an app's install step for the session user can't run on Windows, and gate 1 refuses an app that +declares one there. This bundle has no tasks of its own: other bundles pull its computers by alias, and `agent-env run computers` has nothing to run. diff --git a/src/agent_env/env/envs/computer/utils/composer.py b/src/agent_env/env/envs/computer/utils/composer.py index 5d8f6fc97..95fbc8e9a 100644 --- a/src/agent_env/env/envs/computer/utils/composer.py +++ b/src/agent_env/env/envs/computer/utils/composer.py @@ -46,6 +46,7 @@ macos_vm_bundle, packages, probing, + thin_disk, windows_disk, ) from agent_env.env.envs.computer.utils.composed import BUILT, REPOSITORY, Record, failed, now, stale @@ -394,7 +395,7 @@ def say(line: str) -> None: progress.say(f"{pair}: {line}") attempt.stage = "base" - registry = windows_disk.Registry(_image_store(env), REPOSITORY) + registry = thin_disk.Registry(_image_store(env), REPOSITORY) disk_gb, user = base.forms.disk.min_disk_gb, base.session.user made = windows_disk.base(_key(recipe=_digest(windows_disk.RECIPE), disk_gb=disk_gb, user=user, form="windows-base", format=FORMAT), @@ -407,14 +408,14 @@ def say(line: str) -> None: attempt.stage = "build" with holding_locks([f"windows-disk:{key}"], lambda: progress.at( pair, "waiting", "waiting for the compose of the same disk under way on this machine")): - if (pinned := registry.served(windows_disk.disk_names(key).tag)) is not None: + if (pinned := registry.served(thin_disk.disk_names(windows_disk.GUEST, key).tag)) is not None: progress.at(pair, "reusing", "reusing the disk of the same key, composed earlier") result = {"status": "unverified", "key": key, "image": pinned, "reused": True, "expires": expires, "built_at": now()} else: progress.at(pair, "composing", "composing on this machine's Docker") - pinned = windows_disk.compose(made, build, key, - {KEY_LABEL: key, windows_disk.EXPIRES_LABEL: expires}, say, registry) + pinned = thin_disk.compose(windows_disk.GUEST, made, build, key, + {KEY_LABEL: key, windows_disk.EXPIRES_LABEL: expires}, say, registry) result = {"status": "unverified", "key": key, "image": pinned, "reused": False, "builder": "LocalSandboxProvider", "expires": expires, "built_at": now()} return _probed(env, driver, "disk", arch, result, provider, progress) diff --git a/src/agent_env/env/envs/computer/utils/thin_disk.py b/src/agent_env/env/envs/computer/utils/thin_disk.py new file mode 100644 index 000000000..af6411221 --- /dev/null +++ b/src/agent_env/env/envs/computer/utils/thin_disk.py @@ -0,0 +1,315 @@ +"""Thin disks, made with this machine's Docker and KVM: a base packed as ``disk/base.img`` in a layer of its own, an +empty overlay of it as ``disk/disk.img`` and the guest file beside, and a composed disk a layer on its base. A compose +boots the base on an overlay kept on this machine, gives it its parts over ssh as the base's admin, by the key agent-env +made for that base, shuts it down and packs the overlay onto the base's image, so every disk composed on a base shares +one copy of it. What's its guest OS's own, how its admin is reached and what its shell runs, is a ``Guest``; Windows' +is in ``windows_disk``.""" + +from __future__ import annotations + +import asyncio +import contextlib +import io +import json +import shutil +import subprocess +import tarfile +import time +from collections.abc import Callable, Iterator +from dataclasses import dataclass +from datetime import datetime +from pathlib import Path, PurePosixPath +from typing import NamedTuple + +from agent_env.artifact.artifacts.computer import DISK_BASE_FILE, DISK_FILE, DISK_OWNER +from agent_env.artifact.artifacts.docker_image import docker_login, push_local_image +from agent_env.config.paths import cache_root +from agent_env.providers.sandbox_providers.host_images import present +from agent_env.providers.sandbox_providers.local_docker.cli import docker +from agent_env.providers.sandbox_providers.local_docker.disk import KVM, launcher, machine_for +from agent_env.store.image_store.image_contents import labels as image_labels +from agent_env.store.image_store.image_store import ImageStore +from agent_env.store.image_store.registry_api import pin_digest, pin_if_served +from agent_env.store.local_state import holding_locks +from agent_env.utils.docker.build import build_image + +KEY_LABEL = "dev.agentenv.key" +#: Where a base's image keeps the key to its admin, which composing a layer on it takes over ssh. The image is in the +#: user's own image store, and the guest's ssh is reached only on this machine's loopback while a layer is composed. +ADMIN_KEY = PurePosixPath("/disk/admin.key") +#: How long a compose may take. +COMPOSE_TIMEOUT_S = 3600 +#: How long a guest may take to power off once told. +SHUTDOWN_TIMEOUT_S = 300 +#: How long trimming a disk's free space may take. +COMPACT_TIMEOUT_S = 1800 +#: How often a compose looks for the base's ssh, and a shutdown for the guest's end. +BOOT_POLL_S, SHUTDOWN_POLL_S = 5, 2 +CPUS, MEMORY_MB = 4, 8192 + + +@dataclass(frozen=True) +class Guest: + """A thin disk's guest OS as composing on it needs it: its ``name``, which names its images, locks and work folders + and starts its errors; the launcher's guest file it boots as (``file``); the admin ``user`` its ssh takes; and what + its shell runs for a compose: ``prepare`` an empty folder for the build, ``unpack`` a tar on stdin into it, + ``provision`` from it, ``cleanup`` after, ``compact`` its free space, which may fail, and ``shutdown``.""" + + name: str + file: str + user: str + prepare: str + unpack: str + provision: str + cleanup: str + compact: str + shutdown: str + + +@dataclass(frozen=True) +class Registry: + """The env's image store as thin disks are kept in it: by tag in ``repository``, pinned by digest, as Linux + disks are.""" + + store: ImageStore + repository: str + + def served(self, tag: str) -> str | None: + """``tag``'s ref, pinned to the digest the store serves for it now; None when it serves none.""" + ref = self.store.image_ref(self.repository, tag) + return pin_if_served(ref, self.store.auth(ref)) + + def labels(self, pinned: str) -> dict[str, str]: + return image_labels(pinned, "amd64", self.store.auth(pinned)) + + def push(self, local: str, tag: str) -> str: + """``local`` pushed as ``tag``; its ref there, pinned.""" + ref = self.store.image_ref(self.repository, tag) + push_local_image(local, ref, self.store) + return pin_digest(ref, self.store.auth(ref)) + + def pull(self, pinned: str) -> None: + docker_login(self.store.auth(pinned)) + docker("pull", "-q", pinned) + + +class Names(NamedTuple): + """What a base or a disk is called: ``local`` in this machine's Docker, ``tag`` in the image store.""" + + local: str + tag: str + + +def base_names(guest: Guest, key: str) -> Names: + return Names(f"agentenv-{guest.name}-base:{short(key)}", f"{guest.name}-base-{short(key)}") + + +def disk_names(guest: Guest, key: str) -> Names: + """A disk's names, its tag in the image store being its key, as a Linux disk's is.""" + return Names(f"agentenv-{guest.name}:{short(key)}", key.removeprefix("sha256:")) + + +def short(key: str) -> str: + return key.removeprefix("sha256:")[:16] + + +@dataclass(frozen=True) +class Base: + """A base: its image's tag on this machine, the folder of its admin's key, when its OS's licence runs out, for one + whose does, and, for one the image store holds that this machine doesn't yet, its ref there, pulled when a layer is + composed on it.""" + + image: str + keys: Path + expires: datetime | None = None + pinned: str | None = None + + +def base_lock(guest: Guest, image: str) -> str: + return f"{guest.name}-base:{image}" + + +def here(guest: Guest, base_: Base, registry: Registry, say: Callable[[str], None]) -> None: + """``base_`` on this machine, with the key to its admin: pulled from the image store when it's the store's, under + the base's lock, so a compose of another disk on it waits, then finds it.""" + if base_.pinned is None: + return + with holding_locks([base_lock(guest, base_.image)], lambda: say("waiting for the pull of the same base under way")): + if present(base_.image) and (base_.keys / "id").exists(): + return + say(f"pulling its base from the image store, {base_.pinned}") + registry.pull(base_.pinned) + docker("tag", base_.pinned, base_.image) + take_admin_key(base_.image, base_.keys) + + +def take_admin_key(image: str, keys: Path) -> None: + """The key to the admin of the base ``image`` holds, written to ``keys``, as composing a layer on it needs.""" + shutil.rmtree(keys, ignore_errors=True) + keys.mkdir(parents=True) + container = docker("create", image, "none") + try: + docker("cp", f"{container}:{ADMIN_KEY}", str(keys / "id")) + finally: + subprocess.run(["docker", "rm", container], capture_output=True) + (keys / "id").chmod(0o600) + + +def compose(guest: Guest, base_: Base, build: Path, key: str, labels: dict[str, str], say: Callable[[str], None], + registry: Registry) -> str: + """The disk of ``key`` composed on ``base_``: the base booted on an overlay kept in a folder of this machine, + ``build`` copied in and provisioned from as ``guest``'s admin over ssh, then shut down, packed onto the base's image + with ``labels`` and pushed to the image store; its ref there, pinned. A base the store holds is pulled first.""" + image, tag = disk_names(guest, key) + here(guest, base_, registry, say) + with working(guest, f"disk-{short(key)}") as work: + out = work / "out" + out.mkdir() + name = f"agentenv-{guest.name}-compose-{short(key)}" + say(f"booting its base in {name}") + machine = asyncio.run(machine_for(base_.image)) + container = launched(name, {"VM_KEEP": "/out/disk.img"}, ["-v", f"{out}:/out", *machine.args], machine.image) + try: + port, deadline = ssh_port(container), time.monotonic() + COMPOSE_TIMEOUT_S + while ssh(port, base_.keys, guest.user, "echo ready", check=False, timeout=60).returncode != 0: + alive(guest, container, "booting", deadline) + time.sleep(BOOT_POLL_S) + say("installing its layers over ssh") + ssh(port, base_.keys, guest.user, guest.prepare) + ssh(port, base_.keys, guest.user, guest.unpack, input=tarred(build), timeout=600) + done = ssh(port, base_.keys, guest.user, guest.provision, check=False, timeout=COMPOSE_TIMEOUT_S) + if done.returncode != 0: + raise RuntimeError(f"provisioning exited {done.returncode}: {(done.stderr + done.stdout).strip()[-3000:]}") + ssh(port, base_.keys, guest.user, guest.cleanup) + compacted(guest, port, base_.keys, say) + shut_down(guest, container, port, base_.keys) + finally: + subprocess.run(["docker", "rm", "-f", container], capture_output=True) + converted(out, machine.image, backing=f"/{DISK_BASE_FILE}", args=machine.args) + subprocess.run(["docker", "run", "--rm", "-v", f"{out}:/out", "--entrypoint", "qemu-img", launcher(), "rebase", + "-u", "-b", Path(DISK_BASE_FILE).name, "-F", "qcow2", "/out/disk.img"], + check=True, capture_output=True) + (out / "guest").write_text(guest.file) + say("packing the disk") + pack(guest, out, image, labels, base_image=base_.image) + say("pushing the disk to the image store") + return registry.push(image, tag) + + +def ssh(port: int, keys: Path, user: str, command: str, *, check: bool = True, timeout: float = 120, + input: bytes | None = None) -> subprocess.CompletedProcess: + """``command`` run on the guest as ``user``, over ssh to ``port`` here with the base's key, its output decoded; + refused when it fails unless not ``check``, as one that took over ``timeout`` has.""" + argv = ["ssh", "-p", str(port), "-i", str(keys / "id"), "-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes", "-o", + "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-o", "LogLevel=ERROR", "-o", + "ConnectTimeout=10", f"{user}@127.0.0.1", command] + try: + done = subprocess.run(argv, input=input, capture_output=True, timeout=timeout) + except subprocess.TimeoutExpired: + done = subprocess.CompletedProcess(argv, 255, b"", f"it took over {timeout:.0f} s".encode()) + done = subprocess.CompletedProcess(argv, done.returncode, done.stdout.decode(errors="replace"), + done.stderr.decode(errors="replace")) + if check and done.returncode != 0: + raise RuntimeError(f"ssh {command[:200]!r} exited {done.returncode}: " + f"{(done.stderr + done.stdout).strip()[-2000:]}") + return done + + +def compacted(guest: Guest, port: int, keys: Path, say: Callable[[str], None]) -> None: + """What the guest keeps that a computer doesn't need removed and its free space trimmed (``guest.compact``), so its + disk's image, rewritten after it shuts down, keeps only what it uses.""" + say("trimming its free space") + done = ssh(port, keys, guest.user, guest.compact, check=False, timeout=COMPACT_TIMEOUT_S) + if done.returncode != 0: + say(f"trimming exited {done.returncode}, so its image keeps what it held: {(done.stderr + done.stdout)[-300:]}") + + +def converted(folder: Path, image: str, backing: str | None = None, args: tuple[str, ...] = ()) -> None: + """``folder``'s ``disk.img`` rewritten by ``qemu-img convert`` in ``image`` run with ``args``, which give it its + backing file, keeping only the clusters it uses: a base's whole, a layer's over ``backing``.""" + over = ["-B", backing, "-F", "qcow2"] if backing else [] + subprocess.run(["docker", "run", "--rm", "-v", f"{folder}:/out", *args, "--entrypoint", "qemu-img", image, "convert", + "-O", "qcow2", *over, "/out/disk.img", "/out/compact.img"], check=True, capture_output=True) + (folder / "compact.img").replace(folder / "disk.img") + + +def launched(name: str, variables: dict[str, str], mounts: list[str], image: str) -> str: + """A launcher container of ``image``, named ``name``, booting with ``variables`` and KVM, its guest's ssh published + on a port of this machine's loopback; any earlier one of the name removed first.""" + subprocess.run(["docker", "rm", "-f", name], capture_output=True) + environment = {"VM_PORTS": "22", "VM_CPUS": str(CPUS), "VM_MEMORY_MB": str(MEMORY_MB), "VM_REQUIRE_KVM": "1", + **variables} + return docker("run", "-d", "--name", name, "--device", KVM, "-p", "127.0.0.1::22", *mounts, + *(arg for key, value in environment.items() for arg in ("-e", f"{key}={value}")), image) + + +def ssh_port(container: str) -> int: + return int(docker("port", container, "22/tcp").splitlines()[0].rpartition(":")[2]) + + +def alive(guest: Guest, container: str, doing: str, deadline: float) -> None: + """Raise when ``container`` has stopped or ``deadline`` has passed, with the end of its output.""" + running = docker("inspect", "-f", "{{.State.Running}}", container) == "true" + if running and time.monotonic() < deadline: + return + tail = subprocess.run(["docker", "logs", "--tail", "20", container], capture_output=True, text=True) + why = "its QEMU exited" if not running else f"it was still {doing} after the time it's given" + raise RuntimeError(f"{guest.name}: {why}: {(tail.stdout + tail.stderr).strip()[-2000:]}") + + +def shut_down(guest: Guest, container: str, port: int, keys: Path) -> None: + """The guest powered off, and so its launcher's container exited, which keeps its overlay.""" + ssh(port, keys, guest.user, guest.shutdown, check=False, timeout=30) + deadline = time.monotonic() + SHUTDOWN_TIMEOUT_S + while docker("inspect", "-f", "{{.State.Running}}", container) == "true": + if time.monotonic() > deadline: + raise RuntimeError(f"{guest.name}: the guest didn't power off within {SHUTDOWN_TIMEOUT_S} s") + time.sleep(SHUTDOWN_POLL_S) + if (code := docker("inspect", "-f", "{{.State.ExitCode}}", container)) != "0": + raise RuntimeError(f"{guest.name}: its launcher exited {code} as the guest powered off") + + +def pack(guest: Guest, folder: Path, image: str, labels: dict[str, str], base_image: str | None) -> None: + """``image`` of ``folder``'s disk: a base's, ``base.img`` in a layer of its own under an empty overlay of it, with + the key to its admin, or a composed one's ``disk.img`` over ``base_image``; the guest file beside it.""" + owner = f"--chown={DISK_OWNER}:{DISK_OWNER}" + if base_image is None: + subprocess.run(["docker", "run", "--rm", "-v", f"{folder}:/out", "--entrypoint", "qemu-img", launcher(), "create", + "-q", "-f", "qcow2", "-b", Path(DISK_BASE_FILE).name, "-F", "qcow2", "/out/disk.img"], + check=True, capture_output=True) + (folder / "guest").write_text(guest.file) + dockerfile = (f"FROM scratch\nCOPY {owner} base.img /{DISK_BASE_FILE}\n" + f"COPY --chmod=600 {ADMIN_KEY.name} {ADMIN_KEY}\n" + f"COPY {owner} disk.img guest /{Path(DISK_FILE).parent}/\n") + else: + dockerfile = f"FROM {base_image}\nCOPY {owner} disk.img guest /{Path(DISK_FILE).parent}/\n" + for name in ("answers", "out"): + shutil.rmtree(folder / name, ignore_errors=True) + (folder / "Dockerfile").write_text(dockerfile) + build_image(folder / "Dockerfile", folder, image, platform="linux/amd64", labels=labels) + + +@contextlib.contextmanager +def working(guest: Guest, name: str) -> Iterator[Path]: + """A work folder of ``name`` in this machine's cache, emptied first of what a build killed in it left, and removed + after; its caller holds the lock of its build, so one build works in it at a time.""" + work = cache_root() / guest.name / "work" / name + shutil.rmtree(work, ignore_errors=True) + work.mkdir(parents=True) + try: + yield work + finally: + shutil.rmtree(work, ignore_errors=True) + + +def tarred(folder: Path) -> bytes: + """``folder``'s files as a tar, which the guest's own tar unpacks.""" + buffer = io.BytesIO() + with tarfile.open(fileobj=buffer, mode="w", format=tarfile.USTAR_FORMAT) as tar: + for path in sorted(folder.rglob("*")): + tar.add(path, arcname=path.relative_to(folder).as_posix(), recursive=False) + return buffer.getvalue() + + +def label(image: str, name: str) -> str: + return json.loads(docker("image", "inspect", "-f", "{{json .Config.Labels}}", image) or "{}").get(name, "") diff --git a/src/agent_env/env/envs/computer/utils/windows_disk.py b/src/agent_env/env/envs/computer/utils/windows_disk.py index 1721e20b1..25dc84b2c 100644 --- a/src/agent_env/env/envs/computer/utils/windows_disk.py +++ b/src/agent_env/env/envs/computer/utils/windows_disk.py @@ -1,47 +1,36 @@ -"""Windows computers' disks, made with this machine's Docker and KVM, which keeps them, as a Mac keeps the VM bundles -composed on it: Windows isn't agent-env's to publish. A base is Windows installed from Microsoft's evaluation media, -fetched once into this machine's cache, by the QEMU launcher with the answers and setup of ``computers/disk/windows``, -then packed as a thin disk's base: ``disk/base.img`` in a layer of its own, an empty overlay of it as ``disk/disk.img`` -and the guest file beside. A composed disk is a layer on its base: the base booted on an overlay kept on this machine, -given each part over ssh as the base's admin, by the key agent-env made for that base, then shut down and packed onto -the base's image, so every disk composed on a base shares one copy of it. An evaluation runs for 90 days from its -install, so a base records when it expires, and one with fewer than ``RENEW_DAYS`` left is installed afresh.""" +"""Windows computers' disks, thin disks (``thin_disk``) made with this machine's Docker and KVM, which keeps them, as a +Mac keeps the VM bundles composed on it: Windows isn't agent-env's to publish. A base is Windows installed from +Microsoft's evaluation media, fetched once into this machine's cache, by the QEMU launcher with the answers and setup of +``computers/disk/windows``; a composed disk's parts are installed by a PowerShell script, ``provision.ps1``, run as the +base's admin. An evaluation runs for 90 days from its install, so a base records when it expires, and one with fewer +than ``RENEW_DAYS`` left is installed afresh.""" from __future__ import annotations -import asyncio import base64 -import contextlib -import io import json import re import secrets import shlex import shutil import subprocess -import tarfile import time import urllib.request -from collections.abc import Callable, Iterator +from collections.abc import Callable from dataclasses import dataclass from datetime import datetime, timedelta, timezone -from pathlib import Path, PurePosixPath, PureWindowsPath -from typing import Any, NamedTuple +from pathlib import Path, PureWindowsPath +from typing import Any from agentenv_protocol.computer.app import ADDRESS -from agent_env.artifact.artifacts.computer import DISK_BASE_FILE, DISK_FILE, DISK_OWNER -from agent_env.artifact.artifacts.docker_image import docker_login, push_local_image from agent_env.config.paths import cache_root +from agent_env.env.envs.computer.utils import thin_disk from agent_env.env.envs.computer.utils.install import WindowsInstall +from agent_env.env.envs.computer.utils.thin_disk import Base, Registry from agent_env.providers.sandbox_providers.host_images import present -from agent_env.providers.sandbox_providers.local_docker.cli import docker -from agent_env.providers.sandbox_providers.local_docker.disk import KVM, launcher, machine_for -from agent_env.store.image_store.image_contents import labels as image_labels -from agent_env.store.image_store.image_store import ImageStore -from agent_env.store.image_store.registry_api import pin_digest, pin_if_served +from agent_env.providers.sandbox_providers.local_docker.disk import launcher from agent_env.store.local_state import holding_locks -from agent_env.utils.docker.build import build_image #: Microsoft's link to the Windows 11 Enterprise evaluation ISO, English (United States), x64. ISO_URL = "https://go.microsoft.com/fwlink/?linkid=2334167&clcid=0x409" @@ -55,23 +44,15 @@ BUILD = ROOT / "build" #: The fewest days of its evaluation a base may have left, as a deploy needs some. RENEW_DAYS = 7 -EXPIRES_LABEL, BUILD_LABEL, KEY_LABEL = "dev.agentenv.windows.expires", "dev.agentenv.windows.build", "dev.agentenv.key" -#: Where a base's image keeps the key to its admin, which composing a layer on it takes over ssh. The image is in the -#: user's own image store, and the guest's ssh is reached only on this machine's loopback while a layer is composed. -ADMIN_KEY = PurePosixPath("/disk/admin.key") -#: How long an install, and a compose, may take, and how long the session user's first sign-in is left to settle. -INSTALL_TIMEOUT_S, COMPOSE_TIMEOUT_S, SETTLE_S = 3600, 3600, 60 -#: How long a guest may take to power off once told. -SHUTDOWN_TIMEOUT_S = 300 -#: How long trimming a disk's free space may take. -COMPACT_TIMEOUT_S = 1800 -#: How often an install looks for setup's end, a compose for the base's ssh, and a shutdown for the guest's. -INSTALL_POLL_S, BOOT_POLL_S, SHUTDOWN_POLL_S = 15, 5, 2 +EXPIRES_LABEL, BUILD_LABEL = "dev.agentenv.windows.expires", "dev.agentenv.windows.build" +#: How long an install may take, and how long the session user's first sign-in is left to settle. +INSTALL_TIMEOUT_S, SETTLE_S = 3600, 60 +#: How often an install looks for setup's end. +INSTALL_POLL_S = 15 #: How many times a compose tries a layer's download, and how long it waits between, as the base's setup does: a #: server's errors can last minutes, as GitHub's release downloads' 504s have. A client error, 4xx, but a timeout or #: a rate limit, fails at once, as no retry mends a wrong URL. FETCH_TRIES, FETCH_PAUSE_S = 30, 10 -CPUS, MEMORY_MB = 4, 8192 #: The groups and the account the folders under ``ROOT`` grant access to, by their well-known SIDs, as icacls takes #: them. ADMINISTRATORS, SYSTEM, USERS = "*S-1-5-32-544", "*S-1-5-18", "*S-1-5-32-545" @@ -85,62 +66,22 @@ "once; it's installed for evaluation under Microsoft's terms and runs 90 days from each install") -@dataclass(frozen=True) -class Registry: - """The env's image store as Windows disks are kept in it: by tag in ``repository``, pinned by digest, as Linux - disks are.""" - - store: ImageStore - repository: str - - def served(self, tag: str) -> str | None: - """``tag``'s ref, pinned to the digest the store serves for it now; None when it serves none.""" - ref = self.store.image_ref(self.repository, tag) - return pin_if_served(ref, self.store.auth(ref)) - - def labels(self, pinned: str) -> dict[str, str]: - return image_labels(pinned, "amd64", self.store.auth(pinned)) - - def push(self, local: str, tag: str) -> str: - """``local`` pushed as ``tag``; its ref there, pinned.""" - ref = self.store.image_ref(self.repository, tag) - push_local_image(local, ref, self.store) - return pin_digest(ref, self.store.auth(ref)) - - def pull(self, pinned: str) -> None: - docker_login(self.store.auth(pinned)) - docker("pull", "-q", pinned) - - -class Names(NamedTuple): - """What a base or a disk is called: ``local`` in this machine's Docker, ``tag`` in the image store.""" - - local: str - tag: str - - -def base_names(key: str) -> Names: - return Names(f"agentenv-windows-base:{_short(key)}", f"windows-base-{_short(key)}") - - -def disk_names(key: str) -> Names: - """A disk's names, its tag in the image store being its key, as a Linux disk's is.""" - return Names(f"agentenv-windows:{_short(key)}", key.removeprefix("sha256:")) - - -def _short(key: str) -> str: - return key.removeprefix("sha256:")[:16] - +def powershell(script: str) -> str: + """The command that runs ``script`` in PowerShell, encoded, so cmd's quoting never touches it.""" + return "powershell -NoProfile -NonInteractive -EncodedCommand " + base64.b64encode( + script.encode("utf-16-le")).decode() -@dataclass(frozen=True) -class Base: - """A base: its image's tag on this machine, the folder of its admin's key, when its evaluation expires, and, for one - the image store holds that this machine doesn't yet, its ref there, pulled when a layer is composed on it.""" - image: str - keys: Path - expires: datetime - pinned: str | None = None +#: Windows as a thin disk's guest: its admin over ssh runs cmd, which a compose has unpack its build into ``BUILD`` and +#: run its ``provision.ps1``, and trims its free space with the recipe's ``compact.ps1``. +GUEST = thin_disk.Guest( + name="windows", file="windows", user=ADMIN, + prepare=f'(if exist "{BUILD}" rmdir /s /q "{BUILD}") & mkdir "{BUILD}"', + unpack=f'tar -xf - -C "{BUILD}"', + provision=f'powershell -NoProfile -ExecutionPolicy Bypass -File "{BUILD}\\provision.ps1"', + cleanup=f'rmdir /s /q "{BUILD}"', + compact=powershell((RECIPE / "compact.ps1").read_text()), + shutdown="shutdown /s /t 0") @dataclass(frozen=True) @@ -184,10 +125,11 @@ def base(key: str, iso: Callable[[], Path], disk_gb: int, user: str, say: Callab store holds, pulled, with the key to its admin it carries, when a layer is composed on it; else Windows installed from the ISO ``iso`` gives, and pushed there. One with fewer than ``RENEW_DAYS`` of its evaluation left is installed afresh. One install of a key runs at once on a machine, and another waits for it.""" - (image, tag), keys = base_names(key), cache_root() / "windows" / "bases" / _short(key) - with holding_locks([_base_lock(image)], lambda: say("waiting for the install of the same base under way")): + (image, tag), keys = thin_disk.base_names(GUEST, key), cache_root() / "windows" / "bases" / thin_disk.short(key) + with holding_locks([thin_disk.base_lock(GUEST, image)], + lambda: say("waiting for the install of the same base under way")): if present(image) and (keys / "id").exists(): - expires = datetime.fromisoformat(_label(image, EXPIRES_LABEL)) + expires = datetime.fromisoformat(thin_disk.label(image, EXPIRES_LABEL)) if not expiring(expires.isoformat()): if registry.served(tag) is None: say("pushing the base to the image store, which doesn't hold it") @@ -202,48 +144,18 @@ def base(key: str, iso: Callable[[], Path], disk_gb: int, user: str, say: Callab subprocess.run(["ssh-keygen", "-q", "-t", "ed25519", "-N", "", "-C", f"agentenv {image}", "-f", str(keys / "id")], check=True) media = iso() - with _working(f"base-{_short(key)}") as work: + with thin_disk.working(GUEST, f"base-{thin_disk.short(key)}") as work: facts = _installed(work, media, keys, disk_gb, user, say) say("packing the base") expires = datetime.now(timezone.utc) + timedelta(minutes=facts["grace_minutes"]) - shutil.copy(keys / "id", work / ADMIN_KEY.name) - _pack(work, image, {KEY_LABEL: key, EXPIRES_LABEL: expires.isoformat(timespec="seconds"), - BUILD_LABEL: facts["build"]}, base_image=None) + shutil.copy(keys / "id", work / thin_disk.ADMIN_KEY.name) + thin_disk.pack(GUEST, work, image, {thin_disk.KEY_LABEL: key, EXPIRES_LABEL: expires.isoformat( + timespec="seconds"), BUILD_LABEL: facts["build"]}, base_image=None) say("pushing the base to the image store") registry.push(image, tag) return Base(image, keys, expires) -def _here(base_: Base, registry: Registry, say: Callable[[str], None]) -> None: - """``base_`` on this machine, with the key to its admin: pulled from the image store when it's the store's, under - the base's lock, so a compose of another disk on it waits, then finds it.""" - if base_.pinned is None: - return - with holding_locks([_base_lock(base_.image)], lambda: say("waiting for the pull of the same base under way")): - if present(base_.image) and (base_.keys / "id").exists(): - return - say(f"pulling its base from the image store, {base_.pinned}") - registry.pull(base_.pinned) - docker("tag", base_.pinned, base_.image) - _take_admin_key(base_.image, base_.keys) - - -def _base_lock(image: str) -> str: - return f"windows-base:{image}" - - -def _take_admin_key(image: str, keys: Path) -> None: - """The key to the admin of the base ``image`` holds, written to ``keys``, as composing a layer on it needs.""" - shutil.rmtree(keys, ignore_errors=True) - keys.mkdir(parents=True) - container = docker("create", image, "none") - try: - docker("cp", f"{container}:{ADMIN_KEY}", str(keys / "id")) - finally: - subprocess.run(["docker", "rm", container], capture_output=True) - (keys / "id").chmod(0o600) - - def _installed(work: Path, iso: Path, keys: Path, disk_gb: int, user: str, say: Callable[[str], None]) -> dict: """Windows installed from ``iso`` onto ``work/disk.img`` by the launcher, answered as the recipe says: then booted until its session user has signed in and settled, and shut down; what it reports of itself.""" @@ -264,71 +176,28 @@ def _installed(work: Path, iso: Path, keys: Path, disk_gb: int, user: str, say: name = f"agentenv-windows-install-{keys.name}" say(f"installing Windows from {iso.name} under QEMU in {name}; this takes about 15 minutes") started = time.monotonic() - container = _launched(name, {"VM_KEEP": "/out/disk.img", "VM_DISK_GB": str(disk_gb), "VM_MEDIA": "/in/media.iso", - "VM_FILES": "/in/answers"}, - ["-v", f"{out}:/out", "-v", f"{iso}:/in/media.iso:ro", "-v", f"{answers}:/in/answers:ro"], - launcher()) + container = thin_disk.launched( + name, {"VM_KEEP": "/out/disk.img", "VM_DISK_GB": str(disk_gb), "VM_MEDIA": "/in/media.iso", + "VM_FILES": "/in/answers"}, + ["-v", f"{out}:/out", "-v", f"{iso}:/in/media.iso:ro", "-v", f"{answers}:/in/answers:ro"], launcher()) try: - port = _ssh_port(container) + port = thin_disk.ssh_port(container) deadline = started + INSTALL_TIMEOUT_S while not _signed_in(port, keys, user): - _alive(container, "installing", deadline) + thin_disk.alive(GUEST, container, "installing", deadline) time.sleep(INSTALL_POLL_S) say(f"installed and signed in as {user} after {time.monotonic() - started:.0f} s; settling") time.sleep(SETTLE_S) - facts = json.loads(ssh(port, keys, powershell(_FACTS)).stdout.strip().splitlines()[-1]) - _compacted(port, keys, say) - _shut_down(container, port, keys) + facts = json.loads(thin_disk.ssh(port, keys, ADMIN, powershell(_FACTS)).stdout.strip().splitlines()[-1]) + thin_disk.compacted(GUEST, port, keys, say) + thin_disk.shut_down(GUEST, container, port, keys) finally: subprocess.run(["docker", "rm", "-f", container], capture_output=True) - _converted(out, launcher()) + thin_disk.converted(out, launcher()) shutil.move(out / "disk.img", work / "base.img") return facts -def compose(base_: Base, build: Path, key: str, labels: dict[str, str], say: Callable[[str], None], - registry: Registry) -> str: - """The disk of ``key`` composed on ``base_``: the base booted on an overlay kept in a folder of this machine, - ``build`` copied in and its ``provision.ps1`` run as the admin over ssh, then shut down, packed onto the base's - image with ``labels`` and pushed to the image store; its ref there, pinned. A base the store holds is pulled - first.""" - image, tag = disk_names(key) - _here(base_, registry, say) - with _working(f"disk-{_short(key)}") as work: - out = work / "out" - out.mkdir() - name = f"agentenv-windows-compose-{_short(key)}" - say(f"booting its base in {name}") - machine = asyncio.run(machine_for(base_.image)) - container = _launched(name, {"VM_KEEP": "/out/disk.img"}, ["-v", f"{out}:/out", *machine.args], machine.image) - try: - port, deadline = _ssh_port(container), time.monotonic() + COMPOSE_TIMEOUT_S - while ssh(port, base_.keys, "echo ready", check=False, timeout=60).returncode != 0: - _alive(container, "booting", deadline) - time.sleep(BOOT_POLL_S) - say("installing its layers over ssh") - ssh(port, base_.keys, f'(if exist "{BUILD}" rmdir /s /q "{BUILD}") & mkdir "{BUILD}"') - ssh(port, base_.keys, f'tar -xf - -C "{BUILD}"', input=_tarred(build), timeout=600) - done = ssh(port, base_.keys, f'powershell -NoProfile -ExecutionPolicy Bypass -File "{BUILD}\\provision.ps1"', - check=False, timeout=COMPOSE_TIMEOUT_S) - if done.returncode != 0: - raise RuntimeError(f"provisioning exited {done.returncode}: {(done.stderr + done.stdout).strip()[-3000:]}") - ssh(port, base_.keys, f'rmdir /s /q "{BUILD}"') - _compacted(port, base_.keys, say) - _shut_down(container, port, base_.keys) - finally: - subprocess.run(["docker", "rm", "-f", container], capture_output=True) - _converted(out, machine.image, backing=f"/{DISK_BASE_FILE}", args=machine.args) - subprocess.run(["docker", "run", "--rm", "-v", f"{out}:/out", "--entrypoint", "qemu-img", launcher(), "rebase", - "-u", "-b", PureWindowsPath(DISK_BASE_FILE).name, "-F", "qcow2", "/out/disk.img"], - check=True, capture_output=True) - (out / "guest").write_text("windows") - say("packing the disk") - _pack(out, image, labels, base_image=base_.image) - say("pushing the disk to the image store") - return registry.push(image, tag) - - def expiring(expires: str | None) -> bool: """Whether an evaluation that ends at ``expires`` has fewer than ``RENEW_DAYS`` left, as a disk with none recorded is taken to.""" @@ -417,35 +286,12 @@ def argv(command: str, prefix: PureWindowsPath) -> list[str]: return words -def ssh(port: int, keys: Path, command: str, *, check: bool = True, timeout: float = 120, - input: bytes | None = None) -> subprocess.CompletedProcess: - """``command`` run by cmd on the guest as its admin, over ssh to ``port`` here with the base's key, its output - decoded; refused when it fails unless not ``check``, as one that took over ``timeout`` has.""" - argv = ["ssh", "-p", str(port), "-i", str(keys / "id"), "-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes", "-o", - "StrictHostKeyChecking=no", "-o", "UserKnownHostsFile=/dev/null", "-o", "LogLevel=ERROR", "-o", - "ConnectTimeout=10", f"{ADMIN}@127.0.0.1", command] - try: - done = subprocess.run(argv, input=input, capture_output=True, timeout=timeout) - except subprocess.TimeoutExpired: - done = subprocess.CompletedProcess(argv, 255, b"", f"it took over {timeout:.0f} s".encode()) - done = subprocess.CompletedProcess(argv, done.returncode, done.stdout.decode(errors="replace"), - done.stderr.decode(errors="replace")) - if check and done.returncode != 0: - raise RuntimeError(f"ssh {command[:200]!r} exited {done.returncode}: " - f"{(done.stderr + done.stdout).strip()[-2000:]}") - return done - - -def powershell(script: str) -> str: - """The command that runs ``script`` in PowerShell, encoded, so cmd's quoting never touches it.""" - return "powershell -NoProfile -NonInteractive -EncodedCommand " + base64.b64encode( - script.encode("utf-16-le")).decode() def _signed_in(port: int, keys: Path, user: str) -> bool: """Whether setup has finished and ``user`` has an active session on the console; refused, with setup's error, when setup failed.""" - done = ssh(port, keys, f'if exist "{ROOT}\\setup-failed" (type "{ROOT}\\setup-failed" & exit 3) else ' + done = thin_disk.ssh(port, keys, ADMIN, f'if exist "{ROOT}\\setup-failed" (type "{ROOT}\\setup-failed" & exit 3) else ' f'if exist "{ROOT}\\setup-done" (quser) else (echo setting-up)', check=False, timeout=60) if done.returncode == 3: raise RuntimeError(f"windows: setup failed in the guest: {done.stdout.strip()[-2000:]}") @@ -453,106 +299,6 @@ def _signed_in(port: int, keys: Path, user: str) -> bool: re.MULTILINE | re.IGNORECASE) is not None -def _compacted(port: int, keys: Path, say: Callable[[str], None]) -> None: - """What the guest keeps that a computer doesn't need removed and its free space trimmed (``compact.ps1``), so its - disk's image, rewritten after it shuts down, keeps only what Windows uses.""" - say("trimming its free space") - done = ssh(port, keys, powershell((RECIPE / "compact.ps1").read_text()), check=False, timeout=COMPACT_TIMEOUT_S) - if done.returncode != 0: - say(f"trimming exited {done.returncode}, so its image keeps what it held: {(done.stderr + done.stdout)[-300:]}") - - -def _converted(folder: Path, image: str, backing: str | None = None, args: tuple[str, ...] = ()) -> None: - """``folder``'s ``disk.img`` rewritten by ``qemu-img convert`` in ``image`` run with ``args``, which give it its - backing file, keeping only the clusters it uses: a base's whole, a layer's over ``backing``.""" - over = ["-B", backing, "-F", "qcow2"] if backing else [] - subprocess.run(["docker", "run", "--rm", "-v", f"{folder}:/out", *args, "--entrypoint", "qemu-img", image, "convert", - "-O", "qcow2", *over, "/out/disk.img", "/out/compact.img"], check=True, capture_output=True) - (folder / "compact.img").replace(folder / "disk.img") - - -def _launched(name: str, variables: dict[str, str], mounts: list[str], image: str) -> str: - """A launcher container of ``image``, named ``name``, booting with ``variables`` and KVM, its guest's ssh published - on a port of this machine's loopback; any earlier one of the name removed first.""" - subprocess.run(["docker", "rm", "-f", name], capture_output=True) - environment = {"VM_PORTS": "22", "VM_CPUS": str(CPUS), "VM_MEMORY_MB": str(MEMORY_MB), "VM_REQUIRE_KVM": "1", - **variables} - return docker("run", "-d", "--name", name, "--device", KVM, "-p", "127.0.0.1::22", *mounts, - *(arg for key, value in environment.items() for arg in ("-e", f"{key}={value}")), image) - - -def _ssh_port(container: str) -> int: - return int(docker("port", container, "22/tcp").splitlines()[0].rpartition(":")[2]) - - -def _alive(container: str, doing: str, deadline: float) -> None: - """Raise when ``container`` has stopped or ``deadline`` has passed, with the end of its output.""" - running = docker("inspect", "-f", "{{.State.Running}}", container) == "true" - if running and time.monotonic() < deadline: - return - tail = subprocess.run(["docker", "logs", "--tail", "20", container], capture_output=True, text=True) - why = "its QEMU exited" if not running else f"it was still {doing} after the time it's given" - raise RuntimeError(f"windows: {why}: {(tail.stdout + tail.stderr).strip()[-2000:]}") - - -def _shut_down(container: str, port: int, keys: Path) -> None: - """The guest powered off, and so its launcher's container exited, which keeps its overlay.""" - ssh(port, keys, "shutdown /s /t 0", check=False, timeout=30) - deadline = time.monotonic() + SHUTDOWN_TIMEOUT_S - while docker("inspect", "-f", "{{.State.Running}}", container) == "true": - if time.monotonic() > deadline: - raise RuntimeError(f"windows: the guest didn't power off within {SHUTDOWN_TIMEOUT_S} s") - time.sleep(SHUTDOWN_POLL_S) - if (code := docker("inspect", "-f", "{{.State.ExitCode}}", container)) != "0": - raise RuntimeError(f"windows: its launcher exited {code} as the guest powered off") - - -def _pack(folder: Path, image: str, labels: dict[str, str], base_image: str | None) -> None: - """``image`` of ``folder``'s disk: a base's, ``base.img`` in a layer of its own under an empty overlay of it, or a - composed one's ``disk.img`` over ``base_image``; the guest file beside it.""" - owner = f"--chown={DISK_OWNER}:{DISK_OWNER}" - if base_image is None: - subprocess.run(["docker", "run", "--rm", "-v", f"{folder}:/out", "--entrypoint", "qemu-img", launcher(), "create", - "-q", "-f", "qcow2", "-b", Path(DISK_BASE_FILE).name, "-F", "qcow2", "/out/disk.img"], - check=True, capture_output=True) - (folder / "guest").write_text("windows") - dockerfile = (f"FROM scratch\nCOPY {owner} base.img /{DISK_BASE_FILE}\n" - f"COPY --chmod=600 {ADMIN_KEY.name} {ADMIN_KEY}\n" - f"COPY {owner} disk.img guest /{Path(DISK_FILE).parent}/\n") - else: - dockerfile = f"FROM {base_image}\nCOPY {owner} disk.img guest /{Path(DISK_FILE).parent}/\n" - for name in ("answers", "out"): - shutil.rmtree(folder / name, ignore_errors=True) - (folder / "Dockerfile").write_text(dockerfile) - build_image(folder / "Dockerfile", folder, image, platform="linux/amd64", labels=labels) - - -@contextlib.contextmanager -def _working(name: str) -> Iterator[Path]: - """A work folder of ``name`` in this machine's cache, emptied first of what a build killed in it left, and removed - after; its caller holds the lock of its build, so one build works in it at a time.""" - work = cache_root() / "windows" / "work" / name - shutil.rmtree(work, ignore_errors=True) - work.mkdir(parents=True) - try: - yield work - finally: - shutil.rmtree(work, ignore_errors=True) - - -def _tarred(folder: Path) -> bytes: - """``folder``'s files as a tar, which Windows' own tar unpacks.""" - buffer = io.BytesIO() - with tarfile.open(fileobj=buffer, mode="w", format=tarfile.USTAR_FORMAT) as tar: - for path in sorted(folder.rglob("*")): - tar.add(path, arcname=path.relative_to(folder).as_posix(), recursive=False) - return buffer.getvalue() - - -def _label(image: str, name: str) -> str: - return json.loads(docker("image", "inspect", "-f", "{{json .Config.Labels}}", image) or "{}").get(name, "") - - def _written(path: PureWindowsPath, text: str) -> str: """The line that writes ``text`` to ``path`` as UTF-8 with no BOM, which PowerShell 5's Set-Content would add.""" return f"[IO.File]::WriteAllText('{path}', {_ps(text)})" diff --git a/tst/unit/env/envs/test_computer_windows.py b/tst/unit/env/envs/test_computer_windows.py index b359995a4..5f8118f10 100644 --- a/tst/unit/env/envs/test_computer_windows.py +++ b/tst/unit/env/envs/test_computer_windows.py @@ -27,7 +27,7 @@ from agent_env.env.envs.computer.app_env import ComputerAppEnv from agent_env.env.envs.computer.driver_env import ComputerDriverEnv from agent_env.env.envs.computer.env import ComputerEnv -from agent_env.env.envs.computer.utils import composer, gate, windows_disk +from agent_env.env.envs.computer.utils import composer, gate, thin_disk, windows_disk from agent_env.env.envs.computer.utils.composer import Composing from agent_env.env.envs.computer.utils.install import WindowsInstall, WindowsService from agent_env.providers.sandbox_providers.sandbox_provider import SandboxProvider @@ -68,7 +68,8 @@ def push(self, local, tag): class FakeDocker: - """windows_disk on one machine's Docker: bases installed and disks composed and pushed to the image store.""" + """windows_disk and thin_disk on one machine's Docker: bases installed and disks composed and pushed to the image + store.""" def __init__(self, tmp_path: Path, registry: FakeRegistry): self.tmp, self.registry, self.bases, self.installs, self.composed = tmp_path, registry, {}, [], [] @@ -77,12 +78,12 @@ def base(self, key, iso, disk_gb, user, say, registry): if key not in self.bases: self.installs.append((key, iso(), disk_gb, user)) self.bases[key] = datetime.now(timezone.utc) + timedelta(days=89) - return windows_disk.Base(f"agentenv-windows-base:{key[7:23]}", self.tmp / "keys", self.bases[key]) + return thin_disk.Base(f"agentenv-windows-base:{key[7:23]}", self.tmp / "keys", self.bases[key]) - def compose(self, base, build, key, labels, say, registry): + def compose(self, guest, base, build, key, labels, say, registry): self.composed.append((base.image, (build / "provision.ps1").read_bytes().decode(), sorted(p.relative_to(build).as_posix() for p in build.rglob("*") if p.is_file()), labels)) - return registry.push(*windows_disk.disk_names(key)) + return registry.push(*thin_disk.disk_names(guest, key)) @pytest.fixture @@ -92,8 +93,8 @@ def docker(local_stores, cli_routing, monkeypatch, tmp_path): iso.write_bytes(b"iso") monkeypatch.setattr(windows_disk, "media", lambda say: iso) monkeypatch.setattr(windows_disk, "base", fake.base) - monkeypatch.setattr(windows_disk, "compose", fake.compose) - monkeypatch.setattr(windows_disk, "Registry", lambda store, repository: fake.registry) + monkeypatch.setattr(thin_disk, "compose", fake.compose) + monkeypatch.setattr(thin_disk, "Registry", lambda store, repository: fake.registry) monkeypatch.setattr(composer, "get_config", lambda: SimpleNamespace( get_image_store_for=lambda stored_id: SimpleNamespace(ensure_repository=lambda repository: None))) monkeypatch.setattr(composer, "_probed", lambda env, driver, form, arch, result, provider, progress: { @@ -330,11 +331,11 @@ def test_a_base_is_reused_until_its_evaluation_nears_its_end_then_installed_afre expires = datetime.now(timezone.utc) + timedelta(days=days_left) installed = [] monkeypatch.setattr(windows_disk, "present", lambda image: True) - monkeypatch.setattr(windows_disk, "_label", lambda image, name: expires.isoformat(timespec="seconds")) + monkeypatch.setattr(thin_disk, "label", lambda image, name: expires.isoformat(timespec="seconds")) monkeypatch.setattr(windows_disk.subprocess, "run", lambda argv, **kwargs: (keys / "id").write_text("new")) monkeypatch.setattr(windows_disk, "_installed", lambda *args: installed.append(args) or { "grace_minutes": 90 * 24 * 60, "build": "26200.6584"}) - monkeypatch.setattr(windows_disk, "_pack", lambda folder, image, labels, base_image: None) + monkeypatch.setattr(thin_disk, "pack", lambda guest, folder, image, labels, base_image: None) registry = FakeRegistry() base = windows_disk.base(key, lambda: tmp_path / "windows.iso", 64, "user", lambda line: None, registry) @@ -352,7 +353,7 @@ def test_a_base_this_machine_holds_goes_to_an_image_store_lacking_it_once(monkey (keys / "id").write_text("key") expires = datetime.now(timezone.utc) + timedelta(days=30) monkeypatch.setattr(windows_disk, "present", lambda image: True) - monkeypatch.setattr(windows_disk, "_label", lambda image, name: expires.isoformat(timespec="seconds")) + monkeypatch.setattr(thin_disk, "label", lambda image, name: expires.isoformat(timespec="seconds")) registry, pushed = FakeRegistry(), [] push = registry.push registry.push = lambda local, tag: pushed.append(local) or push(local, tag) @@ -373,14 +374,15 @@ def pull(pinned): pulls.append(pinned) time.sleep(0.2) - monkeypatch.setattr(windows_disk, "present", lambda image: made.is_set()) - monkeypatch.setattr(windows_disk, "docker", lambda *args: None) - monkeypatch.setattr(windows_disk, "_take_admin_key", lambda image, at: (at.mkdir(), (at / "id").write_text("key"), + monkeypatch.setattr(thin_disk, "present", lambda image: made.is_set()) + monkeypatch.setattr(thin_disk, "docker", lambda *args: None) + monkeypatch.setattr(thin_disk, "take_admin_key", lambda image, at: (at.mkdir(), (at / "id").write_text("key"), made.set())) registry = SimpleNamespace(pull=pull) - base = windows_disk.Base("agentenv-windows-base:1", keys, datetime.now(timezone.utc), "registry.test/base@sha256:1") + base = thin_disk.Base("agentenv-windows-base:1", keys, datetime.now(timezone.utc), "registry.test/base@sha256:1") - composes = [threading.Thread(target=windows_disk._here, args=(base, registry, lambda line: None)) for _ in range(2)] + composes = [threading.Thread(target=thin_disk.here, args=(windows_disk.GUEST, base, registry, lambda line: None)) + for _ in range(2)] for compose in composes: compose.start() for compose in composes: @@ -428,9 +430,9 @@ def run(argv, **kwargs): (tmp_path / "compact.img").write_text("compacted") (tmp_path / "disk.img").write_text("as Windows left it") - monkeypatch.setattr(windows_disk.subprocess, "run", run) + monkeypatch.setattr(thin_disk.subprocess, "run", run) - windows_disk._converted(tmp_path, "machine:1", backing=backing, args=args) + thin_disk.converted(tmp_path, "machine:1", backing=backing, args=args) assert ran == [["docker", "run", "--rm", "-v", f"{tmp_path}:/out", *args, "--entrypoint", "qemu-img", "machine:1", "convert", "-O", "qcow2", *over, "/out/disk.img", "/out/compact.img"]] diff --git a/tst/unit/env/envs/test_thin_disk.py b/tst/unit/env/envs/test_thin_disk.py new file mode 100644 index 000000000..f179ef2d2 --- /dev/null +++ b/tst/unit/env/envs/test_thin_disk.py @@ -0,0 +1,88 @@ +import io +import subprocess +import tarfile +from types import SimpleNamespace + +import pytest + +from agent_env.env.envs.computer.utils import thin_disk + +GUEST = thin_disk.Guest(name="plan9", file="plan9-desktop", user="glenda", prepare="prepare", unpack="unpack", + provision="provision", cleanup="cleanup", compact="compact", shutdown="shutdown") + + +class FakeMachine: + """A compose's launcher, its guest over ssh and the image store, as composing a disk on a base uses them: each + command run as whom, and what each step left.""" + + def __init__(self, monkeypatch, tmp_path, provisioned=0): + self.ran, self.unpacked, self.packed, self.pushed, self.removed = [], None, None, [], [] + self.provisioned = provisioned + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path / "cache")) + + async def machine_for(image): + return SimpleNamespace(image="launcher:1", args=("--mount", f"type=image,source={image},target=/disk")) + + monkeypatch.setattr(thin_disk, "machine_for", machine_for) + monkeypatch.setattr(thin_disk, "launcher", lambda: "launcher:1") + monkeypatch.setattr(thin_disk, "launched", lambda name, variables, mounts, image: "container") + monkeypatch.setattr(thin_disk, "ssh_port", lambda container: 2222) + monkeypatch.setattr(thin_disk, "ssh", self.ssh) + monkeypatch.setattr(thin_disk, "docker", lambda *args: "false" if "{{.State.Running}}" in args else "0") + monkeypatch.setattr(thin_disk, "converted", lambda folder, image, backing=None, args=(): None) + monkeypatch.setattr(thin_disk.subprocess, "run", self.run) + monkeypatch.setattr(thin_disk, "pack", self.pack) + + def ssh(self, port, keys, user, command, *, check=True, timeout=120, input=None): + self.ran.append((user, command)) + if command == GUEST.unpack: + with tarfile.open(fileobj=io.BytesIO(input)) as tar: + self.unpacked = sorted(tar.getnames()) + code = self.provisioned if command == GUEST.provision else 0 + return subprocess.CompletedProcess([], code, "provision.sh: line 3: no such app\n" if code else "", "") + + def run(self, argv, **kwargs): + if argv[:3] == ["docker", "rm", "-f"]: + self.removed.append(argv[3]) + return subprocess.CompletedProcess(argv, 0) + + def pack(self, guest, folder, image, labels, base_image): + self.packed = (guest, (folder / "guest").read_text(), image, labels, base_image) + + def push(self, local, tag): + self.pushed.append((local, tag)) + return f"registry.test/disks:{tag}@sha256:1" + + +def test_a_disk_is_composed_by_the_guests_own_commands_as_its_admin_in_order(monkeypatch, tmp_path): + machine = FakeMachine(monkeypatch, tmp_path) + build = tmp_path / "build" + (build / "context" / "driver").mkdir(parents=True) + (build / "provision.sh").write_text("#!/bin/sh\n") + (build / "context" / "driver" / "driver.py").write_text("") + base = thin_disk.Base("agentenv-plan9-base:1", tmp_path / "keys") + key = "sha256:" + "ab" * 32 + + pinned = thin_disk.compose(GUEST, base, build, key, {"dev.agentenv.test": "1"}, lambda line: None, + SimpleNamespace(push=machine.push)) + + assert machine.ran == [("glenda", command) for command in ("echo ready", "prepare", "unpack", "provision", + "cleanup", "compact", "shutdown")] + assert machine.unpacked == ["context", "context/driver", "context/driver/driver.py", "provision.sh"] + assert machine.packed == (GUEST, "plan9-desktop", f"agentenv-plan9:{'ab' * 8}", {"dev.agentenv.test": "1"}, + "agentenv-plan9-base:1") + assert machine.pushed == [(f"agentenv-plan9:{'ab' * 8}", "ab" * 32)] and pinned.endswith("@sha256:1") + assert machine.removed == ["container"] + + +def test_a_provision_that_fails_stops_the_compose_with_its_output_and_removes_the_launcher(monkeypatch, tmp_path): + machine = FakeMachine(monkeypatch, tmp_path, provisioned=2) + build = tmp_path / "build" + build.mkdir() + + with pytest.raises(RuntimeError, match="provisioning exited 2: provision.sh: line 3: no such app"): + thin_disk.compose(GUEST, thin_disk.Base("agentenv-plan9-base:1", tmp_path / "keys"), build, "sha256:" + "cd" * 32, + {}, lambda line: None, SimpleNamespace(push=machine.push)) + + assert [command for _, command in machine.ran][-1] == "provision" and machine.removed == ["container"] + assert machine.packed is None and machine.pushed == []