|
| 1 | +#!/usr/bin/env -S uv run python |
| 2 | +""" |
| 3 | +--- |
| 4 | +title: Devbox Snapshot and Resume |
| 5 | +slug: devbox-snapshot-resume |
| 6 | +use_case: Create a devbox, snapshot its disk, resume from the snapshot, and demonstrate that changes in the original devbox do not affect the clone. Uses the async SDK. |
| 7 | +workflow: |
| 8 | + - Create a devbox |
| 9 | + - Write a file to the devbox |
| 10 | + - Create a disk snapshot |
| 11 | + - Create a new devbox from the snapshot |
| 12 | + - Modify the file on the original devbox |
| 13 | + - Verify the clone has the original content |
| 14 | + - Shutdown both devboxes and delete the snapshot |
| 15 | +tags: |
| 16 | + - devbox |
| 17 | + - snapshot |
| 18 | + - resume |
| 19 | + - cleanup |
| 20 | + - async |
| 21 | +prerequisites: |
| 22 | + - RUNLOOP_API_KEY |
| 23 | +run: uv run python -m examples.devbox_snapshot_resume |
| 24 | +test: uv run pytest -m smoketest tests/smoketests/examples/ |
| 25 | +--- |
| 26 | +""" |
| 27 | + |
| 28 | +from __future__ import annotations |
| 29 | + |
| 30 | +from runloop_api_client import AsyncRunloopSDK |
| 31 | + |
| 32 | +from ._harness import run_as_cli, wrap_recipe |
| 33 | +from .example_types import ExampleCheck, RecipeOutput, RecipeContext |
| 34 | + |
| 35 | +FILE_PATH = "/home/user/welcome.txt" |
| 36 | +ORIGINAL_CONTENT = "hello world!" |
| 37 | +MODIFIED_CONTENT = "original devbox has changed the welcome message" |
| 38 | + |
| 39 | + |
| 40 | +async def recipe(ctx: RecipeContext) -> RecipeOutput: |
| 41 | + """Create a devbox, snapshot it, resume from snapshot, and verify state isolation.""" |
| 42 | + cleanup = ctx.cleanup |
| 43 | + |
| 44 | + sdk = AsyncRunloopSDK() |
| 45 | + |
| 46 | + # Create a devbox |
| 47 | + dbx_original = await sdk.devbox.create( |
| 48 | + name="dbx_original", |
| 49 | + launch_parameters={ |
| 50 | + "resource_size_request": "X_SMALL", |
| 51 | + "keep_alive_time_seconds": 60 * 5, |
| 52 | + }, |
| 53 | + ) |
| 54 | + cleanup.add(f"devbox:{dbx_original.id}", dbx_original.shutdown) |
| 55 | + |
| 56 | + # Write a file to the original devbox |
| 57 | + await dbx_original.file.write(file_path=FILE_PATH, contents=ORIGINAL_CONTENT) |
| 58 | + |
| 59 | + # Read and display the file contents |
| 60 | + cat_original_before = await dbx_original.cmd.exec(f"cat {FILE_PATH}") |
| 61 | + original_content_before = await cat_original_before.stdout() |
| 62 | + |
| 63 | + # Create a disk snapshot of the original devbox |
| 64 | + snapshot = await dbx_original.snapshot_disk(name="my-snapshot") |
| 65 | + cleanup.add(f"snapshot:{snapshot.id}", snapshot.delete) |
| 66 | + |
| 67 | + # Create a new devbox from the snapshot |
| 68 | + dbx_clone = await sdk.devbox.create_from_snapshot( |
| 69 | + snapshot.id, |
| 70 | + name="dbx_clone", |
| 71 | + launch_parameters={ |
| 72 | + "resource_size_request": "X_SMALL", |
| 73 | + "keep_alive_time_seconds": 60 * 5, |
| 74 | + }, |
| 75 | + ) |
| 76 | + cleanup.add(f"devbox:{dbx_clone.id}", dbx_clone.shutdown) |
| 77 | + |
| 78 | + # Modify the file on the original devbox |
| 79 | + await dbx_original.file.write(file_path=FILE_PATH, contents=MODIFIED_CONTENT) |
| 80 | + |
| 81 | + # Read the file contents from both devboxes |
| 82 | + cat_clone = await dbx_clone.cmd.exec(f"cat {FILE_PATH}") |
| 83 | + clone_content = await cat_clone.stdout() |
| 84 | + |
| 85 | + # now the original devbox has been modified but the clone has the original message |
| 86 | + cat_original_after = await dbx_original.cmd.exec(f"cat {FILE_PATH}") |
| 87 | + original_content_after = await cat_original_after.stdout() |
| 88 | + |
| 89 | + return RecipeOutput( |
| 90 | + resources_created=[ |
| 91 | + f"devbox:{dbx_original.id}", |
| 92 | + f"snapshot:{snapshot.id}", |
| 93 | + f"devbox:{dbx_clone.id}", |
| 94 | + ], |
| 95 | + checks=[ |
| 96 | + ExampleCheck( |
| 97 | + name="original devbox file created successfully", |
| 98 | + passed=cat_original_before.exit_code == 0 and original_content_before.strip() == ORIGINAL_CONTENT, |
| 99 | + details=f'content="{original_content_before.strip()}"', |
| 100 | + ), |
| 101 | + ExampleCheck( |
| 102 | + name="snapshot created successfully", |
| 103 | + passed=bool(snapshot.id), |
| 104 | + details=f"snapshotId={snapshot.id}", |
| 105 | + ), |
| 106 | + ExampleCheck( |
| 107 | + name="clone devbox created from snapshot", |
| 108 | + passed=bool(dbx_clone.id), |
| 109 | + details=f"cloneId={dbx_clone.id}", |
| 110 | + ), |
| 111 | + ExampleCheck( |
| 112 | + name="clone has original file content (before modification)", |
| 113 | + passed=cat_clone.exit_code == 0 and clone_content.strip() == ORIGINAL_CONTENT, |
| 114 | + details=f'cloneContent="{clone_content.strip()}"', |
| 115 | + ), |
| 116 | + ExampleCheck( |
| 117 | + name="original devbox has modified content", |
| 118 | + passed=cat_original_after.exit_code == 0 and original_content_after.strip() == MODIFIED_CONTENT, |
| 119 | + details=f'originalContent="{original_content_after.strip()}"', |
| 120 | + ), |
| 121 | + ExampleCheck( |
| 122 | + name="clone and original have divergent state", |
| 123 | + passed=clone_content.strip() != original_content_after.strip(), |
| 124 | + details=f'clone="{clone_content.strip()}" vs original="{original_content_after.strip()}"', |
| 125 | + ), |
| 126 | + ], |
| 127 | + ) |
| 128 | + |
| 129 | + |
| 130 | +run_devbox_snapshot_resume_example = wrap_recipe(recipe) |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + run_as_cli(run_devbox_snapshot_resume_example) |
0 commit comments