|
| 1 | +#!/usr/bin/env -S uv run python |
| 2 | +""" |
| 3 | +--- |
| 4 | +title: Devbox Tunnel (HTTP Server Access) |
| 5 | +slug: devbox-tunnel |
| 6 | +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. |
| 7 | +workflow: |
| 8 | + - Create a devbox |
| 9 | + - Start an HTTP server inside the devbox |
| 10 | + - Enable a tunnel for external access |
| 11 | + - Make an HTTP request from the local machine through the tunnel |
| 12 | + - Validate the response |
| 13 | + - Shutdown the devbox |
| 14 | +tags: |
| 15 | + - devbox |
| 16 | + - tunnel |
| 17 | + - networking |
| 18 | + - http |
| 19 | + - async |
| 20 | +prerequisites: |
| 21 | + - RUNLOOP_API_KEY |
| 22 | +run: uv run python -m examples.devbox_tunnel |
| 23 | +test: uv run pytest -m smoketest tests/smoketests/examples/ |
| 24 | +--- |
| 25 | +""" |
| 26 | + |
| 27 | +from __future__ import annotations |
| 28 | + |
| 29 | +import asyncio |
| 30 | + |
| 31 | +import httpx |
| 32 | + |
| 33 | +from runloop_api_client import AsyncRunloopSDK |
| 34 | + |
| 35 | +from ._harness import run_as_cli, wrap_recipe |
| 36 | +from .example_types import ExampleCheck, RecipeOutput, RecipeContext |
| 37 | + |
| 38 | +HTTP_SERVER_PORT = 8080 |
| 39 | +SERVER_STARTUP_DELAY_S = 2 |
| 40 | + |
| 41 | + |
| 42 | +async def recipe(ctx: RecipeContext) -> RecipeOutput: |
| 43 | + """Create a devbox, start an HTTP server, enable a tunnel, and access it from the local machine.""" |
| 44 | + cleanup = ctx.cleanup |
| 45 | + |
| 46 | + sdk = AsyncRunloopSDK() |
| 47 | + |
| 48 | + devbox = await sdk.devbox.create( |
| 49 | + name="devbox-tunnel-example", |
| 50 | + launch_parameters={ |
| 51 | + "resource_size_request": "X_SMALL", |
| 52 | + "keep_alive_time_seconds": 60 * 10, |
| 53 | + }, |
| 54 | + ) |
| 55 | + cleanup.add(f"devbox:{devbox.id}", devbox.shutdown) |
| 56 | + |
| 57 | + # Start a simple HTTP server inside the devbox using Python's built-in http.server |
| 58 | + # We use exec_async because the server runs indefinitely until stopped |
| 59 | + server_execution = await devbox.cmd.exec_async(f"python3 -m http.server {HTTP_SERVER_PORT} --directory /tmp") |
| 60 | + |
| 61 | + # Give the server a moment to start |
| 62 | + await asyncio.sleep(SERVER_STARTUP_DELAY_S) |
| 63 | + |
| 64 | + # Enable a tunnel to expose the HTTP server |
| 65 | + # For authenticated tunnels, use auth_mode="authenticated" and include the auth_token |
| 66 | + # in your requests via the Authorization header: `Authorization: Bearer {tunnel.auth_token}` |
| 67 | + tunnel = await devbox.net.enable_tunnel(auth_mode="open") |
| 68 | + |
| 69 | + # Get the tunnel URL for the server port |
| 70 | + tunnel_url = await devbox.get_tunnel_url(HTTP_SERVER_PORT) |
| 71 | + |
| 72 | + # Make an HTTP request from the LOCAL MACHINE through the tunnel to the devbox |
| 73 | + # This demonstrates that the tunnel allows external access to the devbox service |
| 74 | + async with httpx.AsyncClient() as client: |
| 75 | + response = await client.get(tunnel_url) |
| 76 | + response_text = response.text |
| 77 | + |
| 78 | + # Stop the HTTP server |
| 79 | + await server_execution.kill() |
| 80 | + |
| 81 | + return RecipeOutput( |
| 82 | + resources_created=[f"devbox:{devbox.id}"], |
| 83 | + checks=[ |
| 84 | + ExampleCheck( |
| 85 | + name="tunnel was created successfully", |
| 86 | + passed=bool(tunnel.tunnel_key), |
| 87 | + details=f"tunnel_key={tunnel.tunnel_key}", |
| 88 | + ), |
| 89 | + ExampleCheck( |
| 90 | + name="tunnel URL was constructed correctly", |
| 91 | + passed=tunnel.tunnel_key in tunnel_url and str(HTTP_SERVER_PORT) in tunnel_url, |
| 92 | + details=tunnel_url, |
| 93 | + ), |
| 94 | + ExampleCheck( |
| 95 | + name="HTTP request through tunnel succeeded", |
| 96 | + passed=response.is_success, |
| 97 | + details=f"status={response.status_code}", |
| 98 | + ), |
| 99 | + ExampleCheck( |
| 100 | + name="response contains directory listing", |
| 101 | + passed="Directory listing" in response_text, |
| 102 | + details=response_text[:200], |
| 103 | + ), |
| 104 | + ], |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +run_devbox_tunnel_example = wrap_recipe(recipe) |
| 109 | + |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + run_as_cli(run_devbox_tunnel_example) |
0 commit comments