|
| 1 | +"""Use csghub-cli to manage sandboxes (create, get, start, stop, exec, upload, health). |
| 2 | +
|
| 3 | +Install the package so the ``csghub-cli`` entry point is available:: |
| 4 | +
|
| 5 | + pip install . |
| 6 | +
|
| 7 | +If ``csghub-cli`` is not on PATH, use:: |
| 8 | +
|
| 9 | + python -m pycsghub.cli sandbox --help |
| 10 | +
|
| 11 | +This file documents typical commands. Set RUN_EXAMPLES = True to execute the |
| 12 | +sample ``create`` + ``get`` flow via subprocess (requires a valid token and image). |
| 13 | +""" |
| 14 | + |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import os |
| 18 | +import subprocess |
| 19 | +import sys |
| 20 | + |
| 21 | +# token = "your access token" |
| 22 | +token = None |
| 23 | + |
| 24 | +endpoint = "https://hub.opencsg.com" |
| 25 | + |
| 26 | +# Set True to run the subprocess example at the bottom (optional). |
| 27 | +RUN_EXAMPLES = False |
| 28 | + |
| 29 | +# --------------------------------------------------------------------------- |
| 30 | +# Shell equivalents (run in terminal) |
| 31 | +# --------------------------------------------------------------------------- |
| 32 | +# |
| 33 | +# export CSGHUB_TOKEN="your access token" |
| 34 | +# export ENDPOINT="https://hub.opencsg.com" |
| 35 | +# |
| 36 | +# Create (image + name; port 0 means server default) |
| 37 | +# |
| 38 | +# csghub-cli sandbox create -i your-runner-image:tag -n my-sandbox \\ |
| 39 | +# -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 40 | +# |
| 41 | +# Full JSON body (volumes, many env vars): put SandboxCreateRequest JSON in spec.json |
| 42 | +# |
| 43 | +# csghub-cli sandbox create --spec /path/to/spec.json -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 44 | +# |
| 45 | +# Query / lifecycle |
| 46 | +# |
| 47 | +# csghub-cli sandbox get my-sandbox -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 48 | +# csghub-cli sandbox start my-sandbox -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 49 | +# csghub-cli sandbox stop my-sandbox -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 50 | +# |
| 51 | +# Runtime (if gateway differs from Hub, add --aigateway-url) |
| 52 | +# |
| 53 | +# csghub-cli sandbox exec my-sandbox "echo hello" -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 54 | +# csghub-cli sandbox upload my-sandbox ./local-file.txt -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 55 | +# csghub-cli sandbox health my-sandbox -e "$ENDPOINT" -k "$CSGHUB_TOKEN" |
| 56 | +# |
| 57 | + |
| 58 | + |
| 59 | +def _token_arg() -> list[str]: |
| 60 | + t = token or os.environ.get("CSGHUB_TOKEN") |
| 61 | + if not t: |
| 62 | + return [] |
| 63 | + return ["-k", t] |
| 64 | + |
| 65 | + |
| 66 | +def run_subprocess_example() -> None: |
| 67 | + """Minimal create + get using the same Python process (optional).""" |
| 68 | + base = [ |
| 69 | + sys.executable, |
| 70 | + "-m", |
| 71 | + "pycsghub.cli", |
| 72 | + "sandbox", |
| 73 | + "-e", |
| 74 | + endpoint, |
| 75 | + ] |
| 76 | + create = [ |
| 77 | + *base, |
| 78 | + "create", |
| 79 | + "-i", |
| 80 | + "your-runner-image:tag", |
| 81 | + "-n", |
| 82 | + "my-sandbox", |
| 83 | + *_token_arg(), |
| 84 | + ] |
| 85 | + get_cmd = [*base, "get", "my-sandbox", *_token_arg()] |
| 86 | + subprocess.run(create, check=True) |
| 87 | + subprocess.run(get_cmd, check=True) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + if RUN_EXAMPLES: |
| 92 | + run_subprocess_example() |
| 93 | + else: |
| 94 | + print(__doc__) |
0 commit comments