-
Notifications
You must be signed in to change notification settings - Fork 2
improve(docs): blueprint build with context added to examples #751
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 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c68469e
blueprint build with context added to examples
james-rl ae7d92c
switch to async sdk since that is perferred anyway
james-rl 46439d2
add comment for object timeout
james-rl 7225715
add comment for object timeout & change value to be more reasonable
james-rl c4751c6
shrink build context to 1h
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #!/usr/bin/env -S uv run python | ||
| """ | ||
| --- | ||
| title: Blueprint with Build Context | ||
| slug: blueprint-with-build-context | ||
| use_case: Create a blueprint using the object store to provide docker build context files, then verify files are copied into the image. | ||
| workflow: | ||
| - Create a temporary directory with sample application files | ||
| - Upload the directory to object storage as build context | ||
| - Create a blueprint with a Dockerfile that copies the context files | ||
| - Create a devbox from the blueprint | ||
| - Verify the files were copied into the image | ||
| - Shutdown devbox and delete blueprint and storage object | ||
| tags: | ||
| - blueprint | ||
| - object-store | ||
| - build-context | ||
| - devbox | ||
| - cleanup | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: uv run python -m examples.blueprint_with_build_context | ||
| test: uv run pytest -m smoketest tests/smoketests/examples/ | ||
| --- | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import tempfile | ||
| from pathlib import Path | ||
| from datetime import timedelta | ||
|
|
||
| from runloop_api_client import RunloopSDK | ||
| 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 | ||
|
|
||
| # building can take time: make sure to set a long blueprint build timeout | ||
| BLUEPRINT_POLL_TIMEOUT_S = 10 * 60 | ||
| BLUEPRINT_POLL_MAX_ATTEMPTS = 600 | ||
| ONE_WEEK = timedelta(weeks=1) | ||
|
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. this is probably excessive, right? |
||
|
|
||
|
|
||
| def recipe(ctx: RecipeContext) -> RecipeOutput: | ||
| """Create a blueprint with build context from object storage, then verify files in a devbox.""" | ||
| cleanup = ctx.cleanup | ||
|
|
||
| sdk = RunloopSDK() | ||
|
|
||
| # setup: create a temporary directory with sample application files to use as build context | ||
| with tempfile.TemporaryDirectory() as tmp_dir: | ||
| tmp_path = Path(tmp_dir) | ||
| (tmp_path / "app.py").write_text('print("Hello from app")') | ||
| (tmp_path / "config.txt").write_text("key=value") | ||
|
|
||
| # upload the build context to object storage | ||
| storage_obj = sdk.storage_object.upload_from_dir( | ||
| tmp_path, | ||
| name=unique_name("example-build-context"), | ||
| ttl=ONE_WEEK, | ||
| ) | ||
| cleanup.add(f"storage_object:{storage_obj.id}", storage_obj.delete) | ||
|
|
||
| # create a blueprint with the build context | ||
| blueprint = sdk.blueprint.create( | ||
| name=unique_name("example-blueprint-context"), | ||
| dockerfile="FROM ubuntu:22.04\nWORKDIR /app\nCOPY . .", | ||
| build_context=storage_obj.as_build_context(), | ||
| polling_config=PollingConfig( | ||
| timeout_seconds=BLUEPRINT_POLL_TIMEOUT_S, | ||
| max_attempts=BLUEPRINT_POLL_MAX_ATTEMPTS, | ||
| ), | ||
| ) | ||
| cleanup.add(f"blueprint:{blueprint.id}", blueprint.delete) | ||
|
|
||
| devbox = blueprint.create_devbox( | ||
| name=unique_name("example-devbox"), | ||
| launch_parameters={ | ||
| "resource_size_request": "X_SMALL", | ||
| "keep_alive_time_seconds": 60 * 5, | ||
| }, | ||
| ) | ||
| cleanup.add(f"devbox:{devbox.id}", devbox.shutdown) | ||
|
|
||
| app_result = devbox.cmd.exec("cat /app/app.py") | ||
| app_stdout = app_result.stdout() | ||
|
|
||
| config_result = devbox.cmd.exec("cat /app/config.txt") | ||
| config_stdout = config_result.stdout() | ||
|
|
||
| return RecipeOutput( | ||
| resources_created=[ | ||
| f"storage_object:{storage_obj.id}", | ||
| f"blueprint:{blueprint.id}", | ||
| f"devbox:{devbox.id}", | ||
| ], | ||
| checks=[ | ||
| ExampleCheck( | ||
| name="app.py exists and readable", | ||
| passed=app_result.exit_code == 0, | ||
| details=f"exitCode={app_result.exit_code}", | ||
| ), | ||
| ExampleCheck( | ||
| name="app.py contains expected content", | ||
| passed='print("Hello from app")' in app_stdout, | ||
| details=app_stdout.strip(), | ||
| ), | ||
| ExampleCheck( | ||
| name="config.txt exists and readable", | ||
| passed=config_result.exit_code == 0, | ||
| details=f"exitCode={config_result.exit_code}", | ||
| ), | ||
| ExampleCheck( | ||
| name="config.txt contains expected content", | ||
| passed="key=value" in config_stdout, | ||
| details=config_stdout.strip(), | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| run_blueprint_with_build_context_example = wrap_recipe(recipe) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_as_cli(run_blueprint_with_build_context_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
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.
forgot to check the other examples, but should we use
AsyncRunloopSDKinstead?