|
| 1 | +""" |
| 2 | +Smoke-verify the Prefect example without a Prefect server: |
| 3 | + 1. `prefect-tenki` imports and registers the "tenki" worker type, and |
| 4 | + 2. `TenkiWorker.run()` drives the real per-flow-run infrastructure lifecycle |
| 5 | + against live Tenki — create microVM → run command → teardown — exit 0. |
| 6 | +
|
| 7 | +We deliberately do NOT execute a full flow run: the guest must reach the |
| 8 | +Prefect API, and a localhost `prefect server` is unreachable from inside the |
| 9 | +microVM. Driving run() directly is the upstream repo's own smoke pattern |
| 10 | +(tests/live_worker_smoke.py) and exercises everything Tenki-specific. |
| 11 | +
|
| 12 | +Token/workspace from env (CI) or ~/.config/tenki/config.yaml (local `tenki login`). |
| 13 | +""" |
| 14 | +import logging |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import tempfile |
| 18 | + |
| 19 | +# Keep ~/.prefect untouched — prefect writes its profile/db under PREFECT_HOME. |
| 20 | +os.environ.setdefault("PREFECT_HOME", tempfile.mkdtemp(prefix="prefect-tenki-verify-")) |
| 21 | + |
| 22 | +import anyio |
| 23 | +from tenki import AsyncClient |
| 24 | + |
| 25 | +from prefect_tenki import TenkiCredentials, TenkiWorker, TenkiWorkerJobConfiguration |
| 26 | + |
| 27 | +assert TenkiWorker.type == "tenki", TenkiWorker.type # the work-pool type name |
| 28 | + |
| 29 | + |
| 30 | +def cfg(key): |
| 31 | + try: |
| 32 | + with open(os.path.expanduser("~/.config/tenki/config.yaml")) as f: |
| 33 | + for line in f: |
| 34 | + if line.startswith(key + ":"): |
| 35 | + return line.split(":", 1)[1].strip() |
| 36 | + except Exception: |
| 37 | + pass |
| 38 | + return "" |
| 39 | + |
| 40 | + |
| 41 | +token = os.environ.get("TENKI_AUTH_TOKEN") or os.environ.get("TENKI_API_KEY") or cfg("auth_token") |
| 42 | +if not token: |
| 43 | + print("No token. Set TENKI_AUTH_TOKEN, or run `tenki login`.") |
| 44 | + sys.exit(1) |
| 45 | + |
| 46 | +# SDK auth gap: a bare `tenki login` session token is sent as Bearer (rejected); |
| 47 | +# prefix it `cookie:` so it goes as a cookie. A `tk_` API key works as-is. |
| 48 | +if not token.startswith(("tk_", "ory_st_", "cookie:")): |
| 49 | + token = f"cookie:{token}" |
| 50 | + |
| 51 | + |
| 52 | +async def main(): |
| 53 | + credentials = TenkiCredentials(api_key=token) |
| 54 | + |
| 55 | + workspace_id = os.environ.get("TENKI_WORKSPACE_ID") or cfg("current_workspace_id") |
| 56 | + if not workspace_id: |
| 57 | + async with AsyncClient(**credentials.get_client_options()) as client: |
| 58 | + workspaces = list((await client.who_am_i()).workspaces) |
| 59 | + if len(workspaces) != 1: |
| 60 | + raise RuntimeError(f"{len(workspaces)} workspaces accessible; set TENKI_WORKSPACE_ID.") |
| 61 | + workspace_id = workspaces[0].id |
| 62 | + |
| 63 | + # Same shape a `prefect worker start` builds per flow run, minus the |
| 64 | + # Prefect-prepared `prefect flow-run execute` command. |
| 65 | + configuration = TenkiWorkerJobConfiguration( |
| 66 | + name="prefect-tenki-verify", |
| 67 | + command="set -eu; test \"$(python3 -c 'print(6*7)')\" = \"42\"; echo ok", |
| 68 | + credentials=credentials, |
| 69 | + workspace_id=workspace_id, |
| 70 | + cpu_cores=1, |
| 71 | + memory_mb=1024, |
| 72 | + allow_inbound=False, |
| 73 | + allow_outbound=False, |
| 74 | + max_duration_seconds=120, |
| 75 | + create_timeout_seconds=180, |
| 76 | + command_timeout_seconds=60, |
| 77 | + stream_output=True, |
| 78 | + ) |
| 79 | + worker = TenkiWorker(work_pool_name="prefect-tenki-verify") |
| 80 | + result = await worker.run(object(), configuration) |
| 81 | + if result.status_code != 0: |
| 82 | + raise AssertionError(f"exit {result.status_code} (sandbox {result.identifier})") |
| 83 | + print("✓ prefect-tenki: TenkiWorker.run() → live microVM → flow command exit 0 → teardown") |
| 84 | + |
| 85 | + |
| 86 | +logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") |
| 87 | +try: |
| 88 | + anyio.run(main) |
| 89 | +except Exception as e: # noqa |
| 90 | + print(f"✗ {type(e).__name__}: {e}") |
| 91 | + sys.exit(1) |
0 commit comments