refactor(commit): introduce Freezer class hierarchy across backends
Adds a Freezer ABC (backend/freeze.py) that encapsulates the
stop-commit-mark-preserved flow for all backends, following the same
pattern as BottleBackend. Each backend gets its own Freezer subclass:
DockerFreezer — docker commit
MacosContainerFreezer — container export + image rebuild; prompts
to stop if the container is running
SmolmachinesFreezer — smolvm pack create --from-vm
The base class owns write_committed_image, mark_preserved, and the
resume hint. Subclasses implement _freeze() and optionally override
_export_hint() for migration instructions.
Freezer.commit(agent, bottle) is the primary entry point for use
within a live launch context. Freezer.commit_slug(slug) is a
convenience wrapper for cmd_commit, which no longer branches on
backend names itself.
get_freezer(backend_name) is the factory, analogous to
get_bottle_backend(). CommitCancelled is raised by MacosContainerFreezer
when the user declines the stop prompt; cmd_commit catches it and
returns 0.
This commit is contained in:
@@ -11,49 +11,15 @@ snapshot instead of rebuilding from the Dockerfile.
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from ..backend import enumerate_active_agents
|
||||
from ..backend.docker.util import commit_container as docker_commit_container
|
||||
from ..backend.macos_container.util import commit_container as macos_commit_container
|
||||
from ..backend.macos_container.util import container_is_running as macos_container_is_running
|
||||
from ..backend.macos_container.util import stop_container as macos_stop_container
|
||||
from ..backend.smolmachines.smolvm import pack_create_from_vm
|
||||
from ..bottle_state import bottle_state_dir
|
||||
from ..bottle_state import mark_preserved, read_metadata, write_committed_image
|
||||
from ..log import die, info
|
||||
from ._common import PROG, read_tty_line
|
||||
from ..backend.freeze import CommitCancelled, get_freezer
|
||||
from ..bottle_state import read_metadata
|
||||
from ..log import die
|
||||
from ._common import PROG
|
||||
from . import tui
|
||||
|
||||
|
||||
_COMMITTED_IMAGE_PREFIX = "bot-bottle-committed-"
|
||||
_DOCKER_BACKENDS = {"docker", ""}
|
||||
_MACOS_CONTAINER_BACKEND = "macos-container"
|
||||
_SMOLMACHINES_BACKEND = "smolmachines"
|
||||
|
||||
|
||||
def _committed_image_tag(slug: str) -> str:
|
||||
return f"{_COMMITTED_IMAGE_PREFIX}{slug}:latest"
|
||||
|
||||
|
||||
def _agent_container_name(slug: str) -> str:
|
||||
return f"bot-bottle-{slug}"
|
||||
|
||||
|
||||
def _agent_machine_name(slug: str) -> str:
|
||||
return f"bot-bottle-{slug}"
|
||||
|
||||
|
||||
def _committed_smolmachine_output(slug: str) -> Path:
|
||||
return bottle_state_dir(slug) / "committed-smolmachine"
|
||||
|
||||
|
||||
def _committed_smolmachine_artifact(slug: str) -> Path:
|
||||
output = _committed_smolmachine_output(slug)
|
||||
return output.with_name(f"{output.name}.smolmachine")
|
||||
|
||||
|
||||
def cmd_commit(argv: list[str]) -> int:
|
||||
parser = argparse.ArgumentParser(prog=f"{PROG} commit", add_help=True)
|
||||
parser.add_argument(
|
||||
@@ -79,59 +45,9 @@ def cmd_commit(argv: list[str]) -> int:
|
||||
|
||||
metadata = read_metadata(slug)
|
||||
backend = metadata.backend if metadata else ""
|
||||
if backend in _DOCKER_BACKENDS:
|
||||
container = _agent_container_name(slug)
|
||||
image_tag = _committed_image_tag(slug)
|
||||
|
||||
docker_commit_container(container, image_tag)
|
||||
write_committed_image(slug, image_tag)
|
||||
mark_preserved(slug)
|
||||
info(f"to resume from this snapshot: ./cli.py resume {slug}")
|
||||
info(f"to export for migration: docker save {image_tag} -o {slug}.tar")
|
||||
try:
|
||||
get_freezer(backend).commit_slug(slug)
|
||||
except CommitCancelled:
|
||||
return 0
|
||||
|
||||
if backend == _MACOS_CONTAINER_BACKEND:
|
||||
container = _agent_container_name(slug)
|
||||
image_tag = _committed_image_tag(slug)
|
||||
|
||||
if macos_container_is_running(container):
|
||||
sys.stderr.write(
|
||||
f"bot-bottle: bottle {slug!r} is running; "
|
||||
"commit will stop it. Continue? [y/N] "
|
||||
)
|
||||
sys.stderr.flush()
|
||||
reply = read_tty_line().strip().lower()
|
||||
if reply not in ("y", "yes"):
|
||||
return 0
|
||||
macos_stop_container(container)
|
||||
|
||||
macos_commit_container(container, image_tag)
|
||||
write_committed_image(slug, image_tag)
|
||||
mark_preserved(slug)
|
||||
info(f"to resume from this snapshot: ./cli.py resume {slug}")
|
||||
info(
|
||||
f"to export for migration: "
|
||||
f"container image save {image_tag} -o {slug}.tar"
|
||||
)
|
||||
return 0
|
||||
|
||||
if backend == _SMOLMACHINES_BACKEND:
|
||||
machine = _agent_machine_name(slug)
|
||||
output = _committed_smolmachine_output(slug)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
pack_create_from_vm(machine, output)
|
||||
artifact = _committed_smolmachine_artifact(slug)
|
||||
write_committed_image(slug, str(artifact))
|
||||
mark_preserved(slug)
|
||||
info(f"to resume from this snapshot: ./cli.py resume {slug}")
|
||||
info(f"to export for migration: cp {artifact} {slug}.smolmachine")
|
||||
return 0
|
||||
|
||||
if backend:
|
||||
die(
|
||||
f"commit is only supported for docker, macos-container, and "
|
||||
f"smolmachines; "
|
||||
f"bottle {slug!r} uses {backend!r}"
|
||||
)
|
||||
die(f"commit cannot determine the backend for bottle {slug!r}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
Reference in New Issue
Block a user