-
Notifications
You must be signed in to change notification settings - Fork 2
docs: add snapshot, suspend & resume example #764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a957c8b
docs: add snapshot, suspend & resume example
james-rl e2ea0b8
add example -- you know, the point of this branch
james-rl 462ea90
addressed great pr feedback
james-rl 5c63629
removed unnecessary readme callout
james-rl d5b83c0
cleanup
james-rl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| #!/usr/bin/env -S uv run python | ||
| """ | ||
| --- | ||
| title: Devbox Snapshots (Suspend, Resume, Restore, Delete) | ||
| slug: devbox-snapshots | ||
| use_case: Upload a file to a devbox, preserve it across suspend and resume, create a disk snapshot, restore multiple devboxes from that snapshot, mutate each copy independently, and delete the snapshot when finished. | ||
| workflow: | ||
| - Create a source devbox | ||
| - Upload a file and mutate it into a shared baseline | ||
| - Suspend and resume the source devbox | ||
| - Create a disk snapshot from the resumed devbox | ||
| - Restore two additional devboxes from the same snapshot baseline | ||
| - Mutate the same file differently in each devbox to prove isolation | ||
| - Shutdown the devboxes and delete the snapshot | ||
| tags: | ||
| - devbox | ||
| - snapshot | ||
| - suspend | ||
| - resume | ||
| - files | ||
| - cleanup | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: uv run python -m examples.devbox_snapshots | ||
| test: uv run pytest -m smoketest tests/smoketests/examples/ | ||
| --- | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import tempfile | ||
| from pathlib import Path | ||
|
|
||
| from runloop_api_client import AsyncRunloopSDK | ||
| from runloop_api_client.lib.polling import PollingConfig | ||
|
|
||
| from ._harness import run_as_cli, unique_name, wrap_recipe | ||
| from .example_types import ExampleCheck, RecipeOutput, RecipeContext | ||
|
|
||
| FILE_PATH = "/tmp/snapshot-demo.txt" | ||
| POLLING_CONFIG = PollingConfig(timeout_seconds=120.0, interval_seconds=5.0) | ||
|
|
||
|
|
||
| async def recipe(ctx: RecipeContext) -> RecipeOutput: | ||
| """Demonstrate suspend/resume and shared snapshot restoration with isolated mutations.""" | ||
| cleanup = ctx.cleanup | ||
| sdk = AsyncRunloopSDK() | ||
|
|
||
| resources_created: list[str] = [] | ||
|
|
||
| uploaded_contents = "uploaded-from-local-file" | ||
| baseline_contents = "baseline-after-upload-and-mutation" | ||
| source_contents = "source-devbox-after-isolated-mutation" | ||
| clone_a_contents = "clone-a-after-isolated-mutation" | ||
| clone_b_contents = "clone-b-after-isolated-mutation" | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as tmp_file: | ||
| tmp_file.write(uploaded_contents) | ||
| local_file_path = Path(tmp_file.name) | ||
| cleanup.add("local-file:snapshot-demo", lambda: local_file_path.unlink(missing_ok=True)) | ||
|
|
||
| source_devbox = await sdk.devbox.create( | ||
|
james-rl marked this conversation as resolved.
|
||
| name=unique_name("snapshot-source"), | ||
| launch_parameters={ | ||
| "resource_size_request": "X_SMALL", | ||
| "keep_alive_time_seconds": 60 * 5, | ||
| }, | ||
| ) | ||
| cleanup.add(f"devbox:{source_devbox.id}", source_devbox.shutdown) | ||
| resources_created.append(f"devbox:{source_devbox.id}") | ||
|
|
||
| await source_devbox.file.upload(path=FILE_PATH, file=local_file_path) | ||
| uploaded_readback = await source_devbox.file.read(file_path=FILE_PATH) | ||
|
|
||
| await source_devbox.file.write(file_path=FILE_PATH, contents=baseline_contents) | ||
|
|
||
| await source_devbox.suspend() | ||
| suspended_info = await source_devbox.await_suspended(polling_config=POLLING_CONFIG) | ||
|
|
||
| resumed_info = await source_devbox.resume(polling_config=POLLING_CONFIG) | ||
| resumed_readback = await source_devbox.file.read(file_path=FILE_PATH) | ||
|
|
||
| snapshot = await source_devbox.snapshot_disk( | ||
| name=unique_name("snapshot-baseline"), | ||
| commit_message="Capture the shared baseline after suspend and resume.", | ||
| polling_config=POLLING_CONFIG, | ||
| ) | ||
| cleanup.add(f"snapshot:{snapshot.id}", snapshot.delete) | ||
| resources_created.append(f"snapshot:{snapshot.id}") | ||
|
|
||
| clone_a = await snapshot.create_devbox( | ||
|
james-rl marked this conversation as resolved.
|
||
| name=unique_name("snapshot-clone-a"), | ||
| launch_parameters={ | ||
| "resource_size_request": "X_SMALL", | ||
| "keep_alive_time_seconds": 60 * 5, | ||
| }, | ||
| ) | ||
| cleanup.add(f"devbox:{clone_a.id}", clone_a.shutdown) | ||
| resources_created.append(f"devbox:{clone_a.id}") | ||
|
|
||
| # clone_a used snapshot.create_devbox(); clone_b uses sdk.devbox.create_from_snapshot() | ||
| # to demonstrate both entry points for restoring a devbox from a snapshot. | ||
| clone_b = await sdk.devbox.create_from_snapshot( | ||
|
james-rl marked this conversation as resolved.
|
||
| snapshot.id, | ||
| name=unique_name("snapshot-clone-b"), | ||
| launch_parameters={ | ||
| "resource_size_request": "X_SMALL", | ||
| "keep_alive_time_seconds": 60 * 5, | ||
| }, | ||
| ) | ||
|
james-rl marked this conversation as resolved.
|
||
| cleanup.add(f"devbox:{clone_b.id}", clone_b.shutdown) | ||
| resources_created.append(f"devbox:{clone_b.id}") | ||
|
|
||
| clone_a_baseline_readback = await clone_a.file.read(file_path=FILE_PATH) | ||
| clone_b_baseline_readback = await clone_b.file.read(file_path=FILE_PATH) | ||
|
|
||
| await source_devbox.file.write(file_path=FILE_PATH, contents=source_contents) | ||
| await clone_a.file.write(file_path=FILE_PATH, contents=clone_a_contents) | ||
| await clone_b.file.write(file_path=FILE_PATH, contents=clone_b_contents) | ||
|
|
||
| source_isolated_readback = await source_devbox.file.read(file_path=FILE_PATH) | ||
| clone_a_isolated_readback = await clone_a.file.read(file_path=FILE_PATH) | ||
| clone_b_isolated_readback = await clone_b.file.read(file_path=FILE_PATH) | ||
|
|
||
| return RecipeOutput( | ||
| resources_created=resources_created, | ||
| checks=[ | ||
| ExampleCheck( | ||
| name="uploaded file is readable on the source devbox", | ||
| passed=uploaded_readback == uploaded_contents, | ||
| details=uploaded_readback, | ||
| ), | ||
| ExampleCheck( | ||
| name="suspend reaches the suspended state", | ||
| passed=suspended_info.status == "suspended", | ||
| details=f"status={suspended_info.status}", | ||
| ), | ||
| ExampleCheck( | ||
| name="resume preserves the baseline file contents", | ||
| passed=resumed_info.status == "running" and resumed_readback == baseline_contents, | ||
| details=f"status={resumed_info.status}, contents={resumed_readback}", | ||
| ), | ||
| ExampleCheck( | ||
| name="multiple devboxes can use the same snapshot baseline", | ||
| passed=( | ||
| clone_a_baseline_readback == baseline_contents and clone_b_baseline_readback == baseline_contents | ||
| ), | ||
| details=(f"clone_a={clone_a_baseline_readback}, clone_b={clone_b_baseline_readback}"), | ||
| ), | ||
| ExampleCheck( | ||
| name="devboxes diverge after isolated mutations", | ||
| passed=( | ||
| source_isolated_readback == source_contents | ||
| and clone_a_isolated_readback == clone_a_contents | ||
| and clone_b_isolated_readback == clone_b_contents | ||
| ), | ||
| details=( | ||
| "source=" | ||
| f"{source_isolated_readback}, " | ||
| f"clone_a={clone_a_isolated_readback}, " | ||
| f"clone_b={clone_b_isolated_readback}" | ||
| ), | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| run_devbox_snapshots_example = wrap_recipe(recipe) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_as_cli(run_devbox_snapshots_example) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looks good, but seems very similar to
devbox_snapshot_resume.py. could these two be consolidated into one example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the great feedback in this PR. I made changes to address the issues you highlighted. The cleanup code is a lot less hairy now.
In regards to your more general point: we could consolidate these demos but I feel like they're for slightly different use cases. I think it's better to keep them separate but this is mostly based on impressions of how I think agents and llms will process these docs.