:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(devbox): clone via server-side AMI capture (#69072)
Raúl Negrón-Otero26b34309fb8e
diff --git a/tools/hogli-commands/hogli_commands/devbox/cli.py b/tools/hogli-commands/hogli_commands/devbox/cli.pyindex 531852292432..b8f9054f26d0 100644--- a/tools/hogli-commands/hogli_commands/devbox/cli.py+++ b/tools/hogli-commands/hogli_commands/devbox/cli.py@@ -40,7 +40,6 @@ from .coder import (coder_installed,coder_reachable,coder_ssh_alias_configured,- create_clone_image,create_task,create_workspace,delete_user_secret,@@ -58,8 +57,10 @@ from .coder import (get_default_git_identity,get_shared_users,get_sharing_status,+ get_source_instance_id,get_username,get_workspace,+ get_workspace_disk_size,get_workspace_name,get_workspace_region,get_workspace_status,@@ -1433,9 +1434,9 @@ def devbox_clone(workspace: str | None, new_label: str, yes: bool, verbose: boolif template != DEFAULT_TEMPLATE:_fail(f"Clone supports the '{DEFAULT_TEMPLATE}' template only (this devbox uses '{template}').")+ # The clone lands in the source's region -- a per-clone AMI is region-scoped,+ # so the template images and boots within that same region.region = region_from_workspace_name(source_name)- if region != DEFAULT_REGION:- _fail("Clone is us-east-1 only for now (an AMI is region-scoped). EU clone is not wired yet.")status = get_workspace_status(ws)if status != "running":@@ -1458,10 +1459,23 @@ def devbox_clone(workspace: str | None, new_label: str, yes: bool, verbose: boolclick.echo("Cancelled.")return- click.echo("Capturing a clone image from the source devbox (this takes a few minutes)...")- ami = create_clone_image(source_name, owner=owner, source_label=source_name)- click.echo(f"Captured {ami}. Creating '{target_name}'...")- clone_workspace(source_name, target_name, base_ami=ami, template=template, verbose=verbose)+ disk_size = get_workspace_disk_size(ws)+ if disk_size is None:+ _fail("Could not determine the source devbox's disk size. Rebuild it on the latest template and retry.")++ source_instance_id = get_source_instance_id(source_name)+ click.echo(+ f"Cloning '{source_name}' ({source_instance_id}) -> '{target_name}'. "+ "The template captures its disk into a private image (a few minutes) and boots the clone from it."+ )+ clone_workspace(+ target_name,+ source_instance_id=source_instance_id,+ disk_size=disk_size,+ region=region,+ template=template,+ verbose=verbose,+ )click.echo("Cloned.")_print_connection_info(target_name)diff --git a/tools/hogli-commands/hogli_commands/devbox/coder.py b/tools/hogli-commands/hogli_commands/devbox/coder.pyindex 489e4e8d0cf8..a919f250f9f5 100644--- a/tools/hogli-commands/hogli_commands/devbox/coder.py+++ b/tools/hogli-commands/hogli_commands/devbox/coder.py@@ -68,16 +68,18 @@ AUTO_START_APP_PARAMETER = "auto_start_app"# parameters, so forwarding the region here breaks every resume instead of# suppressing a picker. Valid values match the template contract exactly.WORKSPACE_REGION_PARAMETER = "workspace_region"+DISK_SIZE_PARAMETER = "disk_size"REGIONS = ("us-east-1", "eu-central-1")DEFAULT_REGION = REGIONS[0]-# Immutable, create-time AMI override consumed by `devbox:clone`. A clone boots-# the new box from a per-clone image captured off the source box's root volume,-# so the duplicate comes up with that box's full disk state instead of a blank-# golden AMI. Empty everywhere else.-BASE_AMI_PARAMETER = "base_ami"-# Key of the workspace metadata item the template publishes the region back as.+# Immutable, create-time parameter consumed by `devbox:clone`: the EC2 instance+# of the source devbox. The template images that instance server-side and boots+# the new box from the capture, so the duplicate comes up with the source's full+# disk state instead of a blank golden AMI. Empty everywhere else.+CLONE_SOURCE_PARAMETER = "clone_source_instance_id"+# Keys of the workspace metadata items the template publishes back.REGION_METADATA_KEY = "region"+DISK_METADATA_KEY = "disk"# Workspace-name suffix per region. us-east-1 is the historical default and# carries no suffix, so existing workspace names stay unchanged. Non-default# regions append `-{suffix}` at the end of the name so that a single user can@@ -1094,24 +1096,44 @@ def get_workspace_status(workspace: dict[str, Any]) -> str:return workspace.get("latest_build", {}).get("status", "unknown")-def get_workspace_region(workspace: dict[str, Any]) -> str | None:- """Return the region a workspace lives in, or ``None`` when unknown.+def _workspace_metadata_value(workspace: dict[str, Any], key: str) -> str | None:+ """Return a ``coder_metadata`` item value the template publishes back.- The template publishes the region as a ``coder_metadata`` item (key- ``region``), which surfaces under ``latest_build.resources[].metadata[]``- in the ``coder list`` payload. Returns ``None`` for boxes created before- the metadata item existed so callers can render their own placeholder.+ Items surface under ``latest_build.resources[].metadata[]`` in the ``coder+ list`` payload. Returns ``None`` when the key is absent (e.g. boxes created+ before that item existed) so callers can decide on a fallback."""- resources = workspace.get("latest_build", {}).get("resources", [])- for resource in resources:+ for resource in workspace.get("latest_build", {}).get("resources", []):for item in resource.get("metadata", []):- if isinstance(item, dict) and item.get("key") == REGION_METADATA_KEY:+ if isinstance(item, dict) and item.get("key") == key:value = item.get("value")if isinstance(value, str) and value:return valuereturn None+def get_workspace_region(workspace: dict[str, Any]) -> str | None:+ """Return the region a workspace lives in, or ``None`` when unknown.++ The template publishes the region as a ``coder_metadata`` item (key+ ``region``). Returns ``None`` for boxes created before the metadata item+ existed so callers can render their own placeholder.+ """+ return _workspace_metadata_value(workspace, REGION_METADATA_KEY)+++def get_workspace_disk_size(workspace: dict[str, Any]) -> int | None:+ """Return a workspace's root disk size in GiB, or ``None`` when unknown.++ The template publishes it as a ``coder_metadata`` item (key ``disk``, value+ like ``"100 GiB"``). A clone must request at least the source's size or the+ instance fails to launch from the captured AMI (``InvalidBlockDeviceMapping``).+ """+ value = _workspace_metadata_value(workspace, DISK_METADATA_KEY)+ match = re.search(r"^(\d+)\s*GiB$", value.strip()) if value else None+ return int(match.group(1)) if match else None++def _list_template_presets(template: str) -> list[str]:"""Return preset names defined on the active version of ``template``, or [] on failure.@@ -1211,7 +1233,7 @@ def create_workspace(``resolve_template_preset``; pass ``NO_PRESET`` to opt out."""parameters: dict[str, str] = {- "disk_size": str(disk_size),+ DISK_SIZE_PARAMETER: str(disk_size),"repo": repo,WORKSPACE_REGION_PARAMETER: region,}@@ -1240,89 +1262,35 @@ def create_workspace(raise SystemExit(result.returncode)-# Imaging a tens-of-GiB root volume to an AMI takes a few minutes; allow ample-# headroom (includes a possible one-time AWS CLI install on older golden AMIs).-CLONE_IMAGE_TIMEOUT = 1800--# Tag values ride into the AWS CLI tag shorthand; keep them to a safe charset so-# neither the shorthand parser nor the IAM tag conditions can be subverted.-_CLONE_TAG_VALUE_RE = re.compile(r"^[A-Za-z0-9._-]+$")-_CLONE_READY_RE = re.compile(r"^CLONE_READY=(ami-[0-9a-f]+)$", re.MULTILINE)--# Runs ON the source devbox (over its ssh alias) and images the box's own root-# volume into a private, tagged AMI the clone boots from. The box authenticates-# with its instance profile, so no human AWS credentials are involved. The AWS-# CLI is installed on demand for golden AMIs that predate it. --no-reboot keeps-# this box running (the clone is driven from a live ssh session) at the cost of-# a crash-consistent (not application-consistent) image -- quiesce the dev stack-# first for the latter. __OWNER__/__SOURCE__ are substituted with shell-quoted,-# validated values before transport.-_CLONE_IMAGE_SCRIPT = """#!/usr/bin/env bash-set -euo pipefail--OWNER=__OWNER__-SOURCE=__SOURCE__--if ! command -v aws >/dev/null 2>&1; then- echo "clone: installing AWS CLI on the devbox (one-time)..." >&2- sudo apt-get update -qq >/dev/null 2>&1 || true- sudo apt-get install -y -qq unzip >/dev/null 2>&1 || true- curl -fsSL "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o /tmp/awscliv2.zip- (cd /tmp && unzip -q -o awscliv2.zip && sudo ./aws/install --update)- rm -rf /tmp/awscliv2.zip /tmp/aws-fi--TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 120")-IID=$(curl -sS -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id)-REGION=$(curl -sS -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)+# A metadata read over ssh is near-instant; keep the ceiling tight so a wedged+# box fails fast instead of hanging the clone.+INSTANCE_ID_TIMEOUT = 60-CREATED_AT=$(date -u +%Y-%m-%dT%H:%M:%SZ)-NAME="posthog-devbox-clone-${OWNER}-$(date +%s)"-TAGS="{Key=Coder_CloneArtifact,Value=true},{Key=Coder_CloneOwner,Value=${OWNER}},{Key=Coder_CloneSource,Value=${SOURCE}},{Key=Coder_CloneCreatedAt,Value=${CREATED_AT}}"+_INSTANCE_ID_RE = re.compile(r"\bi-[0-9a-f]{8,17}\b")-# Flush dirty pages so the crash-consistent snapshot is as clean as possible.-sync--AMI=$(aws ec2 create-image --region "$REGION" --instance-id "$IID" --no-reboot --name "$NAME" --description "devbox clone of ${SOURCE} for ${OWNER}" --tag-specifications "ResourceType=image,Tags=[${TAGS},{Key=Name,Value=${NAME}}]" "ResourceType=snapshot,Tags=[${TAGS}]" --query ImageId --output text)--echo "CLONE_AMI=${AMI}"-echo "clone: baking ${AMI}; waiting for it to become available..." >&2-aws ec2 wait image-available --region "$REGION" --image-ids "$AMI"-echo "CLONE_READY=${AMI}"+# Runs ON the source devbox (over its ssh alias) and prints the box's own EC2+# instance id from IMDSv2. A read-only metadata lookup -- no AWS credentials and+# no imaging permissions. The clone template captures the AMI from this id+# server-side, after verifying the instance carries the requester's owner tag.+_INSTANCE_ID_SCRIPT = """#!/usr/bin/env bash+set -euo pipefail+TOKEN=$(curl -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 60")+curl -sS -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/instance-id"""-def _parse_clone_ami(output: str) -> str | None:- """Pull the AMI id out of the remote clone script's CLONE_READY line."""- match = _CLONE_READY_RE.search(output)- return match.group(1) if match else None+def get_source_instance_id(workspace_name: str, *, timeout: int = INSTANCE_ID_TIMEOUT) -> str:+ """Return the source devbox's own EC2 instance id (``i-...``), read from IMDS.--def create_clone_image(- workspace_name: str,- *,- owner: str,- source_label: str,- timeout: int = CLONE_IMAGE_TIMEOUT,-) -> str:- """Capture the source devbox's root volume into a tagged AMI; return its id.-- Runs on the box itself over the ssh alias so the capture uses the- instance's own profile -- no human AWS credentials. Blocks until the image- is ``available`` (a fresh root-volume snapshot of tens of GiB takes a few- minutes), then returns the AMI id for :func:`clone_workspace` to boot from.- The image is private and reaped by the cloud-infra clone reaper.+ A read-only metadata lookup over the box's ssh alias -- hogli makes no AWS+ calls and needs no imaging permissions. The clone template captures the AMI+ from this id server-side, after verifying the instance carries the+ requester's ownership tag."""if not coder_ssh_alias_configured(workspace_name):_fail(f"ssh alias for '{workspace_name}' is not configured. {RUNTIME_SETUP_HINT}")- for value, what in ((owner, "owner"), (source_label, "source")):- if not _CLONE_TAG_VALUE_RE.match(value):- _fail(f"Refusing to clone: {what} '{value}' has characters unsafe for an AWS tag.")- script = _CLONE_IMAGE_SCRIPT.replace("__OWNER__", shlex.quote(owner)).replace(- "__SOURCE__", shlex.quote(source_label)- )- encoded = base64.b64encode(script.encode()).decode()+ encoded = base64.b64encode(_INSTANCE_ID_SCRIPT.encode()).decode()try:result = subprocess.run(["ssh", _ssh_host_alias(workspace_name), f"echo {encoded} | base64 -d | bash"],@@ -1331,43 +1299,48 @@ def create_clone_image(timeout=timeout,)except subprocess.TimeoutExpired:- _fail(f"Timed out after {timeout}s waiting for the clone image to build.")+ _fail(f"Timed out after {timeout}s reading the source devbox's instance id.")if result.returncode != 0:sys.stderr.write(result.stderr)- _fail("Failed to capture a clone image from the source devbox.")+ _fail("Failed to read the source devbox's instance id over ssh.")- ami = _parse_clone_ami(result.stdout)- if ami is None:+ match = _INSTANCE_ID_RE.search(result.stdout)+ if match is None:sys.stderr.write(result.stdout)sys.stderr.write(result.stderr)- _fail("Clone image build did not report an AMI id.")- return ami+ _fail("Could not determine the source devbox's EC2 instance id.")+ return match.group(0)def clone_workspace(- source: str,target: str,*,- base_ami: str,+ source_instance_id: str,+ disk_size: int,+ region: str,template: str = DEFAULT_TEMPLATE,verbose: bool = False,) -> None:- """Create ``target`` as a copy of ``source``, booting from the clone AMI.-- ``--copy-parameters-from`` carries the source's parameter values (disk- size, region, git identity, ...) so the duplicate matches it; the explicit- ``base_ami`` override points the new box at the captured image.-- Unlike create/update, this does NOT go through the param-retry shim: that- path silently drops a parameter the template does not declare, which for a- clone would boot the golden AMI and present a blank box as a successful- clone. If ``base_ami`` is rejected, the template predates the clone change- and we fail loudly instead.+ """Create ``target`` as a clone via server-side AMI capture.++ ``clone_source_instance_id`` tells the template which instance to image; the+ template verifies ownership and captures the AMI itself, so hogli passes+ only the id. ``disk_size`` must be at least the source's or the clone fails+ to launch from the captured snapshot; ``region`` must match the source's+ (the AMI is region-scoped). Every other parameter falls back to the+ template default (``--use-parameter-defaults``), which resolves git identity+ to the workspace owner -- the source's on-disk config rides in via the AMI.++ ``--copy-parameters-from`` is deliberately NOT used: Coder resolves a copied+ value ahead of an explicit ``--parameter``, so it silently overrides+ ``clone_source_instance_id`` back to the source's empty value and boots a+ blank golden box as a successful clone. This also skips the param-retry+ shim, which would drop ``clone_source_instance_id`` on a template that+ predates the clone change; that key is load-bearing, so we fail loudly."""# --preset none is required: newer Coder shows an interactive preset picker- # that --yes does not bypass, which would hang clone_workspace. The source's- # own parameters arrive via --copy-parameters-from, so no preset is wanted.+ # that --yes does not bypass, which would hang the build.args = _append_parameter_flags(["coder",@@ -1377,19 +1350,26 @@ def clone_workspace(template,"--preset",NO_PRESET,- "--copy-parameters-from",- source,+ "--use-parameter-defaults","--yes",],- {BASE_AMI_PARAMETER: base_ami},+ {+ CLONE_SOURCE_PARAMETER: source_instance_id,+ DISK_SIZE_PARAMETER: str(disk_size),+ WORKSPACE_REGION_PARAMETER: region,+ },)result = _run_build(args, verbose=verbose)if result.returncode != 0:- if _PARAM_NOT_PRESENT_RE.search(result.stdout or ""):+ match = _PARAM_NOT_PRESENT_RE.search(result.stdout or "")+ missing = match.group(1) if match else None+ if missing == CLONE_SOURCE_PARAMETER:_fail(- f"This Coder template does not accept '{BASE_AMI_PARAMETER}', so it cannot be "+ f"This Coder template does not accept '{CLONE_SOURCE_PARAMETER}', so it cannot be ""cloned. Deploy the cloud-infra devbox-clone template change first.")+ if missing is not None:+ _fail(f"This Coder template does not accept the '{missing}' parameter.")raise SystemExit(result.returncode)