-
Notifications
You must be signed in to change notification settings - Fork 3
feat: snapshot and resume example #740
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
Open
james-rl
wants to merge
3
commits into
main
Choose a base branch
from
james/snp-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/usr/bin/env -S npm run tsn -T | ||
|
|
||
| /** | ||
| --- | ||
| title: Devbox Snapshot and Resume | ||
| slug: devbox-snapshot-resume | ||
| 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. | ||
| workflow: | ||
| - Create a devbox | ||
| - Write a file to the devbox | ||
| - Create a disk snapshot | ||
| - Create a new devbox from the snapshot | ||
| - Modify the file on the original devbox | ||
| - Verify the clone has the original content | ||
| - Shutdown both devboxes and delete the snapshot | ||
| tags: | ||
| - devbox | ||
| - snapshot | ||
| - resume | ||
| - cleanup | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: yarn tsn -T examples/devbox-snapshot-resume.ts | ||
| test: yarn test:examples | ||
| --- | ||
| */ | ||
|
|
||
| import { RunloopSDK } from '@runloop/api-client'; | ||
| import { wrapRecipe, runAsCli } from './_harness'; | ||
| import type { RecipeContext, RecipeOutput } from './types'; | ||
|
|
||
| const FILE_PATH = '/home/user/welcome.txt'; | ||
| const ORIGINAL_CONTENT = 'hello world!'; | ||
| const MODIFIED_CONTENT = 'original devbox has changed the welcome message'; | ||
|
|
||
| export async function recipe(ctx: RecipeContext): Promise<RecipeOutput> { | ||
| const { cleanup } = ctx; | ||
|
|
||
| const sdk = new RunloopSDK({ | ||
| bearerToken: process.env['RUNLOOP_API_KEY'], | ||
| }); | ||
|
|
||
| // Create a devbox | ||
| const dbxOriginal = await sdk.devbox.create({ | ||
| name: 'dbx_original', | ||
| launch_parameters: { | ||
| resource_size_request: 'X_SMALL', | ||
| keep_alive_time_seconds: 60 * 5, | ||
| }, | ||
| }); | ||
| cleanup.add(`devbox:${dbxOriginal.id}`, () => sdk.devbox.fromId(dbxOriginal.id).shutdown()); | ||
|
|
||
| // Write a file to the original devbox | ||
| await dbxOriginal.file.write({ file_path: FILE_PATH, contents: ORIGINAL_CONTENT }); | ||
|
|
||
| // Read and display the file contents | ||
| const catOriginalBefore = await dbxOriginal.cmd.exec(`cat ${FILE_PATH}`); | ||
| const originalContentBefore = await catOriginalBefore.stdout(); | ||
|
|
||
| // Create a disk snapshot of the original devbox | ||
| const snapshot = await dbxOriginal.snapshotDisk({ | ||
| name: 'my-snapshot', | ||
| }); | ||
| cleanup.add(`snapshot:${snapshot.id}`, () => snapshot.delete()); | ||
|
|
||
| // Create a new devbox from the snapshot | ||
| const dbxClone = await sdk.devbox.createFromSnapshot(snapshot.id, { | ||
| name: 'dbx_clone', | ||
| launch_parameters: { | ||
| resource_size_request: 'X_SMALL', | ||
| keep_alive_time_seconds: 60 * 5, | ||
| }, | ||
| }); | ||
| cleanup.add(`devbox:${dbxClone.id}`, () => sdk.devbox.fromId(dbxClone.id).shutdown()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .addResource(x) we know which one it is with just the id xxx_yyy |
||
|
|
||
| // Modify the file on the original devbox | ||
| await dbxOriginal.file.write({ file_path: FILE_PATH, contents: MODIFIED_CONTENT }); | ||
|
|
||
| // Read the file contents from both devboxes | ||
| const catClone = await dbxClone.cmd.exec(`cat ${FILE_PATH}`); | ||
| const cloneContent = await catClone.stdout(); | ||
|
|
||
| const catOriginalAfter = await dbxOriginal.cmd.exec(`cat ${FILE_PATH}`); | ||
| const originalContentAfter = await catOriginalAfter.stdout(); | ||
|
|
||
| return { | ||
| resourcesCreated: [`devbox:${dbxOriginal.id}`, `snapshot:${snapshot.id}`, `devbox:${dbxClone.id}`], | ||
| checks: [ | ||
| { | ||
| name: 'original devbox file created successfully', | ||
| passed: catOriginalBefore.exitCode === 0 && originalContentBefore.trim() === ORIGINAL_CONTENT, | ||
| details: `content="${originalContentBefore.trim()}"`, | ||
| }, | ||
| { | ||
| name: 'snapshot created successfully', | ||
| passed: Boolean(snapshot.id), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. weird for an example maybe we lift this out of the example itself? |
||
| details: `snapshotId=${snapshot.id}`, | ||
| }, | ||
| { | ||
| name: 'clone devbox created from snapshot', | ||
| passed: Boolean(dbxClone.id), | ||
| details: `cloneId=${dbxClone.id}`, | ||
| }, | ||
| { | ||
| name: 'clone has original file content (before modification)', | ||
| passed: catClone.exitCode === 0 && cloneContent.trim() === ORIGINAL_CONTENT, | ||
| details: `cloneContent="${cloneContent.trim()}"`, | ||
| }, | ||
| { | ||
| name: 'original devbox has modified content', | ||
| passed: catOriginalAfter.exitCode === 0 && originalContentAfter.trim() === MODIFIED_CONTENT, | ||
| details: `originalContent="${originalContentAfter.trim()}"`, | ||
| }, | ||
| { | ||
| name: 'clone and original have divergent state', | ||
| passed: cloneContent.trim() !== originalContentAfter.trim(), | ||
| details: `clone="${cloneContent.trim()}" vs original="${originalContentAfter.trim()}"`, | ||
| }, | ||
| ], | ||
| }; | ||
| } | ||
|
|
||
| export const runDevboxSnapshotResumeExample = wrapRecipe({ recipe }); | ||
|
|
||
| if (require.main === module) { | ||
| void runAsCli(runDevboxSnapshotResumeExample); | ||
| } | ||
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.
catExecResult or something