-
Notifications
You must be signed in to change notification settings - Fork 2
feat: example for using tunnels #754
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 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cab71bc
feat: added tunnels example
james-rl 44ef1e3
lint
james-rl 767524d
updated registry
james-rl af4bc5f
lint
james-rl 82c0b7c
fixed linter issue
james-rl aa1ee9b
got rid of pointless keep alive
james-rl 212175a
merged
james-rl 05ed79f
merged
james-rl 39cd0c3
fmt
james-rl 65f0341
update examples
james-rl 33d8c33
updated tunnels example
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,115 @@ | ||
| #!/usr/bin/env -S uv run python | ||
| """ | ||
| --- | ||
| title: Devbox Tunnel (HTTP Server Access) | ||
| slug: devbox-tunnel | ||
| use_case: Create a devbox, start an HTTP server, enable a tunnel, and access the server from the local machine through the tunnel. Uses the async SDK. | ||
| workflow: | ||
| - Create a devbox | ||
| - Start an HTTP server inside the devbox | ||
| - Enable a tunnel for external access | ||
| - Make an HTTP request from the local machine through the tunnel | ||
| - Validate the response | ||
| - Shutdown the devbox | ||
| tags: | ||
| - devbox | ||
| - tunnel | ||
| - networking | ||
| - http | ||
| - async | ||
| prerequisites: | ||
| - RUNLOOP_API_KEY | ||
| run: uv run python -m examples.devbox_tunnel | ||
| test: uv run pytest -m smoketest tests/smoketests/examples/ | ||
| --- | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
|
|
||
| import httpx | ||
|
|
||
| from runloop_api_client import AsyncRunloopSDK | ||
|
|
||
| from ._harness import run_as_cli, wrap_recipe | ||
| from .example_types import ExampleCheck, RecipeOutput, RecipeContext | ||
|
|
||
| HTTP_SERVER_PORT = 8080 | ||
| SERVER_STARTUP_DELAY_S = 2 | ||
|
|
||
|
|
||
| async def recipe(ctx: RecipeContext) -> RecipeOutput: | ||
| """Create a devbox, start an HTTP server, enable a tunnel, and access it from the local machine.""" | ||
| cleanup = ctx.cleanup | ||
|
|
||
| sdk = AsyncRunloopSDK() | ||
|
|
||
| devbox = await sdk.devbox.create( | ||
| name="devbox-tunnel-example", | ||
| launch_parameters={ | ||
| "resource_size_request": "X_SMALL", | ||
| }, | ||
| ) | ||
| cleanup.add(f"devbox:{devbox.id}", devbox.shutdown) | ||
|
|
||
| # Start a simple HTTP server inside the devbox using Python's built-in http.server | ||
| # We use exec_async because the server runs indefinitely until stopped | ||
| server_execution = await devbox.cmd.exec_async(f"python3 -m http.server {HTTP_SERVER_PORT} --directory /tmp") | ||
|
|
||
| # Give the server a moment to start | ||
| await asyncio.sleep(SERVER_STARTUP_DELAY_S) | ||
|
|
||
| # Enable a tunnel to expose the HTTP server | ||
| # For authenticated tunnels, use auth_mode="authenticated" and include the auth_token | ||
| # in your requests via the Authorization header: `Authorization: Bearer {tunnel.auth_token}` | ||
| tunnel = await devbox.net.enable_tunnel(auth_mode="open") | ||
|
|
||
| # Get the tunnel URL for the server port | ||
| tunnel_url = await devbox.get_tunnel_url(HTTP_SERVER_PORT) | ||
| if tunnel_url is None: | ||
| raise RuntimeError("Failed to get tunnel URL after enabling tunnel") | ||
|
|
||
| # Make an HTTP request from the LOCAL MACHINE through the tunnel to the devbox | ||
| # This demonstrates that the tunnel allows external access to the devbox service | ||
| async with httpx.AsyncClient() as client: | ||
| response = await client.get(tunnel_url) | ||
| response_text = response.text | ||
|
|
||
| # Stop the HTTP server | ||
| await server_execution.kill() | ||
|
|
||
| return RecipeOutput( | ||
| resources_created=[f"devbox:{devbox.id}"], | ||
| checks=[ | ||
| ExampleCheck( | ||
| name="tunnel was created successfully", | ||
| passed=bool(tunnel.tunnel_key), | ||
| details=f"tunnel_key={tunnel.tunnel_key}", | ||
| ), | ||
| ExampleCheck( | ||
| name="tunnel URL was constructed correctly", | ||
| passed=bool( | ||
| tunnel.tunnel_key and tunnel.tunnel_key in tunnel_url and str(HTTP_SERVER_PORT) in tunnel_url | ||
| ), | ||
| details=tunnel_url, | ||
| ), | ||
| ExampleCheck( | ||
| name="HTTP request through tunnel succeeded", | ||
| passed=response.is_success, | ||
| details=f"status={response.status_code}", | ||
| ), | ||
| ExampleCheck( | ||
| name="response contains directory listing", | ||
| passed="Directory listing" in response_text, | ||
| details=response_text[:200], | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
|
|
||
| run_devbox_tunnel_example = wrap_recipe(recipe) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| run_as_cli(run_devbox_tunnel_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.
nit: tunnel is unused. Let's move this to devbox create