voltagegpu-cli 1.3.1__py3-none-any.whl → 2.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- volt/__init__.py +1 -1
- volt/cli/cmd_account.py +4 -1
- volt/cli/vgpu.py +485 -0
- volt/mcp/__init__.py +1 -0
- volt/mcp/server.py +344 -0
- volt/sdk/attest.py +296 -0
- volt/sdk/catalog.py +163 -0
- volt/sdk/client.py +240 -236
- volt/sdk/config.py +2 -1
- volt/sdk/fleet.py +220 -0
- volt/sdk/models.py +336 -332
- volt/sdk/remote.py +140 -0
- volt/sdk/scripts/snp-proof-in-the-vm.sh +216 -0
- volt/sdk/scripts/two-proofs-in-the-vm.sh +51 -0
- volt/sdk/ssh.py +157 -157
- volt/sdk/ttl.py +104 -0
- {voltagegpu_cli-1.3.1.dist-info → voltagegpu_cli-2.0.1.dist-info}/METADATA +105 -5
- voltagegpu_cli-2.0.1.dist-info/RECORD +32 -0
- {voltagegpu_cli-1.3.1.dist-info → voltagegpu_cli-2.0.1.dist-info}/entry_points.txt +1 -0
- voltagegpu_cli-1.3.1.dist-info/RECORD +0 -22
- {voltagegpu_cli-1.3.1.dist-info → voltagegpu_cli-2.0.1.dist-info}/WHEEL +0 -0
- {voltagegpu_cli-1.3.1.dist-info → voltagegpu_cli-2.0.1.dist-info}/licenses/LICENSE +0 -0
volt/__init__.py
CHANGED
volt/cli/cmd_account.py
CHANGED
|
@@ -107,7 +107,10 @@ def balance_cmd(state: AppState, as_json: bool) -> None:
|
|
|
107
107
|
if as_json:
|
|
108
108
|
echo_json(bal.to_dict())
|
|
109
109
|
return
|
|
110
|
-
|
|
110
|
+
if bal.balance is None:
|
|
111
|
+
info(f"Balance hidden: {esc(bal.balance_hidden or 'this key cannot read the account balance')}")
|
|
112
|
+
else:
|
|
113
|
+
console.print(f"Balance [bold green]{money(bal.balance)}[/bold green] {esc(bal.currency)}")
|
|
111
114
|
if bal.active_pods is not None:
|
|
112
115
|
info(f"Running {bal.active_pods} pod{'s' if bal.active_pods != 1 else ''}")
|
|
113
116
|
hint(f"Top up: {esc(bal.top_up_url)}")
|
volt/cli/vgpu.py
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
"""`vgpu`: the short command for scripts and AI agents (next to `voltagegpu`).
|
|
2
|
+
|
|
3
|
+
vgpu ls [--kind vm|cpu|standard|baremetal|sandbox] [--gpu H100] [--json]
|
|
4
|
+
vgpu up TIER --name N --ttl 1h -y [--json]
|
|
5
|
+
vgpu ps | exec N "cmd" [-d] | scp N remote local | rm N -y | attest N | whoami
|
|
6
|
+
|
|
7
|
+
The key is read from VOLT_API_KEY or VOLTAGEGPU_API_KEY (or the file written by
|
|
8
|
+
`voltagegpu login`) and never printed. Every machine gets a deadline (--ttl).
|
|
9
|
+
Exit codes are a contract, see EXIT_CODES; `exec` returns the remote command's
|
|
10
|
+
own code (255 when SSH itself fails).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import json
|
|
16
|
+
import os
|
|
17
|
+
import secrets
|
|
18
|
+
import shlex
|
|
19
|
+
import subprocess
|
|
20
|
+
import sys
|
|
21
|
+
import time
|
|
22
|
+
from datetime import datetime, timezone
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import Any, Dict, List, Mapping, Optional, Tuple
|
|
25
|
+
|
|
26
|
+
import click
|
|
27
|
+
|
|
28
|
+
from .. import __version__
|
|
29
|
+
from ..sdk import attest
|
|
30
|
+
from ..sdk.catalog import filter_offers, list_offers
|
|
31
|
+
from ..sdk.config import API_KEYS_PAGE
|
|
32
|
+
from ..sdk.errors import ApiError, ConfigError, scrub
|
|
33
|
+
from ..sdk.fleet import Instance, NameStore, check_name, deploy, list_instances, release, resolve
|
|
34
|
+
from ..sdk.remote import RemotePathError, detached_command, exec_argv, scp_argv
|
|
35
|
+
from ..sdk.ssh import find_ssh_binary
|
|
36
|
+
from ..sdk.ttl import KINDS, TtlError, parse_ttl, plan_ttl
|
|
37
|
+
from .main import configure_stream
|
|
38
|
+
from .state import AppState
|
|
39
|
+
|
|
40
|
+
EXIT_CODES = {
|
|
41
|
+
0: "ok",
|
|
42
|
+
1: "error",
|
|
43
|
+
2: "usage",
|
|
44
|
+
3: "api key missing, malformed or refused",
|
|
45
|
+
4: "api key scope missing",
|
|
46
|
+
5: "not found",
|
|
47
|
+
6: "balance or api key budget exceeded (402)",
|
|
48
|
+
7: "unavailable, sold out or over --max-price",
|
|
49
|
+
8: "attestation failed",
|
|
50
|
+
9: "attestation incomplete or local tool missing",
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
# Seams for the tests (no ssh binary, no waiting).
|
|
54
|
+
_sleep = time.sleep
|
|
55
|
+
_run = subprocess.run
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _tool(name: str) -> Optional[str]:
|
|
59
|
+
return find_ssh_binary(name=name)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class Fail(Exception):
|
|
63
|
+
def __init__(self, exit_code: int, message: str, code: Optional[str] = None, extra: Optional[Mapping[str, Any]] = None) -> None:
|
|
64
|
+
super().__init__(message)
|
|
65
|
+
self.exit_code = exit_code
|
|
66
|
+
self.message = message
|
|
67
|
+
self.code = code or {2: "USAGE", 5: "NOT_FOUND", 7: "UNAVAILABLE", 8: "ATTESTATION_FAILED", 9: "TOOL_MISSING"}.get(exit_code, "ERROR")
|
|
68
|
+
self.extra = dict(extra or {})
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def api_fail(exc: ApiError) -> Fail:
|
|
72
|
+
payload = exc.payload or {}
|
|
73
|
+
raw_code = payload.get("error") if isinstance(payload.get("error"), str) else None
|
|
74
|
+
code = raw_code if raw_code and raw_code.isupper() and " " not in raw_code else str(payload.get("code") or f"HTTP_{exc.status_code or 0}")
|
|
75
|
+
status = exc.status_code
|
|
76
|
+
exit_code = {401: 3, 403: 4, 404: 5, 402: 6, 409: 7, 410: 7, 429: 7, 503: 7, 400: 2, 422: 2}.get(status, 1)
|
|
77
|
+
if status == 403 and code not in ("API_KEY_SCOPE_MISSING", "API_KEY_ACCOUNT_RESOURCE"):
|
|
78
|
+
exit_code = 7 # offer closed or account not eligible: not a scope problem
|
|
79
|
+
extra = {k: payload[k] for k in ("window", "budget_usd", "spent_usd", "requested_usd", "charged", "required_scope") if k in payload}
|
|
80
|
+
message = exc.message
|
|
81
|
+
if code == "API_KEY_BUDGET_EXCEEDED":
|
|
82
|
+
message = f"Stop: {exc.message} Nothing was charged. Ask the account owner to raise the budget; do not retry."
|
|
83
|
+
elif exit_code == 3:
|
|
84
|
+
message = f"The API refused the key (HTTP {status}). Check VOLT_API_KEY; keys are created at {API_KEYS_PAGE}."
|
|
85
|
+
return Fail(exit_code, message, code, {"http_status": status, **extra})
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def ssh_key_needed(exc: ApiError, kind: str, given: bool, how: str) -> Optional[Fail]:
|
|
89
|
+
"""The 400 of a standard GPU when the account has several SSH keys and none was named.
|
|
90
|
+
|
|
91
|
+
``how`` names the way to give one (``--ssh-key-id`` for the CLI, ``ssh_key_ids`` for MCP).
|
|
92
|
+
The API answers it before any charge; its text is the only marker (no code in the body).
|
|
93
|
+
"""
|
|
94
|
+
if kind != "standard" or given or exc.status_code != 400 or "more than one ssh key" not in exc.message.lower():
|
|
95
|
+
return None
|
|
96
|
+
return Fail(2, f"This account has several SSH keys: pass {how} with one key id (the account owner lists them with "
|
|
97
|
+
"`voltagegpu ssh-keys list`). Nothing was charged.", "SSH_KEY_ID_REQUIRED", {"http_status": 400})
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def _cents(values: Any) -> Any:
|
|
101
|
+
"""Budgets and spend shown to a human, to the cent (a float sum prints 0.18999999999999995)."""
|
|
102
|
+
if not isinstance(values, Mapping):
|
|
103
|
+
return values
|
|
104
|
+
return {k: round(v, 2) if isinstance(v, (int, float)) and not isinstance(v, bool) else v for k, v in values.items()}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def _emit(as_json: bool, data: Any, human: str) -> None:
|
|
108
|
+
click.echo(json.dumps(data, indent=2, default=str) if as_json else human)
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _state(ctx: click.Context) -> AppState:
|
|
112
|
+
return ctx.ensure_object(AppState)
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _confirm(yes: bool, question: str) -> None:
|
|
116
|
+
if yes:
|
|
117
|
+
return
|
|
118
|
+
if not sys.stdin.isatty():
|
|
119
|
+
raise Fail(2, f"{question} Not a terminal: pass -y to confirm.")
|
|
120
|
+
if not click.confirm(question, default=False):
|
|
121
|
+
raise click.Abort()
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _find(ctx: click.Context, ref: str) -> Instance:
|
|
125
|
+
return find_instance(_state(ctx).client(), ref)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _reachable(inst: Instance):
|
|
129
|
+
target = inst.target
|
|
130
|
+
if target is None:
|
|
131
|
+
raise Fail(7, f"{inst.name or inst.id} is not reachable over SSH yet (status {inst.status}). Wait, then run `vgpu ps`.")
|
|
132
|
+
return target
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def _need(tool: str) -> str:
|
|
136
|
+
path = _tool(tool)
|
|
137
|
+
if not path:
|
|
138
|
+
raise Fail(9, f"`{tool}` was not found on PATH (OpenSSH client). Install it, then retry.")
|
|
139
|
+
return path
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def _identity(identity: Optional[str]) -> Optional[str]:
|
|
143
|
+
return identity or os.environ.get("VGPU_SSH_KEY") or None
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _money(value: Optional[float]) -> str:
|
|
147
|
+
return f"${value:.2f}/h" if isinstance(value, (int, float)) else "?"
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
@click.group(context_settings={"help_option_names": ["-h", "--help"], "max_content_width": 110})
|
|
151
|
+
@click.version_option(__version__, "-V", "--version", prog_name="vgpu")
|
|
152
|
+
@click.option("--base-url", envvar="VOLT_BASE_URL", hidden=True)
|
|
153
|
+
@click.pass_context
|
|
154
|
+
def vgpu(ctx: click.Context, base_url: Optional[str]) -> None:
|
|
155
|
+
"""VoltageGPU for scripts and AI agents: Confidential VMs, Confidential CPU, standard GPUs, dedicated machines, sandboxes.
|
|
156
|
+
|
|
157
|
+
\b
|
|
158
|
+
Always give --ttl, release with `vgpu rm` when done, stop on exit code 6
|
|
159
|
+
(budget or balance), and call a machine confidential only after `vgpu attest`
|
|
160
|
+
returns VERIFIED. Exit codes: 0 ok, 1 error, 2 usage, 3 key, 4 scope,
|
|
161
|
+
5 not found, 6 budget/balance (402), 7 unavailable, 8 attestation failed,
|
|
162
|
+
9 attestation incomplete or tool missing.
|
|
163
|
+
"""
|
|
164
|
+
state = ctx.ensure_object(AppState)
|
|
165
|
+
state.base_url = base_url
|
|
166
|
+
ctx.call_on_close(state.close)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
@vgpu.command("ls")
|
|
170
|
+
@click.option("--kind", type=click.Choice(KINDS), help="Only this kind of machine.")
|
|
171
|
+
@click.option("--gpu", help="GPU model contains this text (H100, a5000...).")
|
|
172
|
+
@click.option("--max-price", type=float, help="Only offers at or under this price per hour (USD).")
|
|
173
|
+
@click.option("--all", "all_", is_flag=True, help="Include sold-out offers.")
|
|
174
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
175
|
+
@click.pass_context
|
|
176
|
+
def ls_cmd(ctx: click.Context, kind: Optional[str], gpu: Optional[str], max_price: Optional[float], all_: bool, as_json: bool) -> None:
|
|
177
|
+
"""Offers with the price charged, cheapest first."""
|
|
178
|
+
offers, errors = list_offers(_state(ctx).client(), (kind,) if kind else KINDS)
|
|
179
|
+
offers = filter_offers(offers, gpu=gpu, max_price=max_price, include_sold_out=all_)
|
|
180
|
+
for err in errors:
|
|
181
|
+
click.echo(f"warning: {scrub(err)}", err=True)
|
|
182
|
+
lines = [f"{'KIND':<10} {'TIER':<38} {'PRICE':>10} {'AVAIL':>5} NAME"]
|
|
183
|
+
lines += [f"{o.kind:<10} {o.tier:<38} {_money(o.price_per_hour):>10} {'' if o.available is None else o.available:>5} "
|
|
184
|
+
f"{o.name}{' [' + o.tee + ']' if o.tee else ''}" for o in offers]
|
|
185
|
+
_emit(as_json, [o.to_dict() for o in offers], "\n".join(lines) if offers else "No offer matches.")
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _wait_ready_client(client: Any, pod_id: str, timeout: int) -> Optional[Instance]:
|
|
189
|
+
for _ in range(max(1, timeout // 10)):
|
|
190
|
+
inst = resolve(list_instances(client), pod_id)
|
|
191
|
+
if inst is not None and inst.target is not None:
|
|
192
|
+
return inst
|
|
193
|
+
_sleep(10)
|
|
194
|
+
return None
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
@vgpu.command("up")
|
|
198
|
+
@click.argument("tier")
|
|
199
|
+
@click.option("--name", required=True, help="Your name for the machine (lowercase, digits, hyphens).")
|
|
200
|
+
@click.option("--ttl", required=True, help="Deadline: 45m, 1h, 6h, 2d. Mandatory: the machine stops by itself then.")
|
|
201
|
+
@click.option("--kind", type=click.Choice(KINDS), help="Look the tier up in this kind only.")
|
|
202
|
+
@click.option("--image", help="Sandbox image (ubuntu-24.04 by default).")
|
|
203
|
+
@click.option("--max-price", type=float, help="Refuse if the hourly price is above this (USD).")
|
|
204
|
+
@click.option("-y", "--yes", is_flag=True, help="Do not ask for confirmation (required when not in a terminal).")
|
|
205
|
+
@click.option("--wait/--no-wait", default=True, help="Wait until SSH answers (default).")
|
|
206
|
+
@click.option("--timeout", default=900, show_default=True, help="Seconds to wait for SSH.")
|
|
207
|
+
@click.option("--ssh-key-id", "ssh_key_ids", multiple=True,
|
|
208
|
+
help="SSH key id to bind: one for a standard GPU (needed when the account has several keys); "
|
|
209
|
+
"repeatable for dedicated machines and sandboxes.")
|
|
210
|
+
@click.option("--show-secret", is_flag=True, help="Print the sudo password of a Confidential VM (shown once, never stored).")
|
|
211
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
212
|
+
@click.pass_context
|
|
213
|
+
def up_cmd(ctx: click.Context, tier: str, name: str, ttl: str, kind: Optional[str], image: Optional[str], max_price: Optional[float],
|
|
214
|
+
yes: bool, wait: bool, timeout: int, ssh_key_ids: tuple, show_secret: bool, as_json: bool) -> None:
|
|
215
|
+
"""Start a machine. Charges the first hour now; the unused part comes back when released."""
|
|
216
|
+
check_name(name)
|
|
217
|
+
seconds = parse_ttl(ttl)
|
|
218
|
+
client = _state(ctx).client()
|
|
219
|
+
offers, _ = list_offers(client, (kind,) if kind else KINDS)
|
|
220
|
+
offer = next((o for o in offers if o.tier == tier), None)
|
|
221
|
+
if offer is None:
|
|
222
|
+
raise Fail(5, f"Unknown tier {tier!r}. Run `vgpu ls` for the current offers.")
|
|
223
|
+
if not offer.in_stock:
|
|
224
|
+
raise Fail(7, f"{tier} is sold out right now. Run `vgpu ls` for what is available.", "SOLD_OUT")
|
|
225
|
+
if max_price is not None and (offer.price_per_hour is None or offer.price_per_hour > max_price):
|
|
226
|
+
raise Fail(7, f"{tier} costs {_money(offer.price_per_hour)}, above --max-price {max_price}.", "OVER_MAX_PRICE")
|
|
227
|
+
plan = plan_ttl(offer.kind, seconds)
|
|
228
|
+
names = NameStore()
|
|
229
|
+
known = names.load().get(name)
|
|
230
|
+
if known and resolve(list_instances(client), known.get("id", "")):
|
|
231
|
+
raise Fail(2, f"A running machine is already named {name!r}. Pick another --name or `vgpu rm {name} -y`.")
|
|
232
|
+
_confirm(yes, f"Start {offer.kind} {tier} at {_money(offer.price_per_hour)} (first hour prepaid), stopping after {ttl}?")
|
|
233
|
+
try:
|
|
234
|
+
result = deploy(client, offer, name, plan, image=image, ssh_key_ids=list(ssh_key_ids) or None)
|
|
235
|
+
except ApiError as exc:
|
|
236
|
+
if exc.status_code == 0:
|
|
237
|
+
raise Fail(1, "The deploy request got no answer: a machine may have started and been charged. Run `vgpu ps` "
|
|
238
|
+
"before trying again; vgpu never retries a deploy by itself.", "NO_ANSWER") from None
|
|
239
|
+
needed = ssh_key_needed(exc, offer.kind, bool(ssh_key_ids), "--ssh-key-id")
|
|
240
|
+
if needed:
|
|
241
|
+
raise needed from None
|
|
242
|
+
raise
|
|
243
|
+
secret = result.pop("_sudo_password", None)
|
|
244
|
+
names.put(name, {"id": result["id"], "kind": result["kind"], "tier": tier, "created_at": datetime.now(timezone.utc).isoformat()})
|
|
245
|
+
ready = _wait_ready_client(client, result["id"], timeout) if wait else None
|
|
246
|
+
result.update({"ssh": ready.ssh if ready else None, "ssh_ready": bool(ready), "status": ready.status if ready else "STARTING"})
|
|
247
|
+
if show_secret and secret:
|
|
248
|
+
result["sudo_password"] = secret
|
|
249
|
+
human = (f"Started {name} ({result['id']}), {offer.kind} {tier}, {_money(result['hourly_price'])}, stops after {ttl}.\n"
|
|
250
|
+
+ (f"SSH: {result['ssh']}" if ready else "Not reachable yet: `vgpu ps` shows when SSH is ready."))
|
|
251
|
+
_emit(as_json, result, human)
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@vgpu.command("ps")
|
|
255
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
256
|
+
@click.pass_context
|
|
257
|
+
def ps_cmd(ctx: click.Context, as_json: bool) -> None:
|
|
258
|
+
"""Machines of this key (or of the account for a full key)."""
|
|
259
|
+
rows = list_instances(_state(ctx).client())
|
|
260
|
+
lines = [f"{'NAME':<20} {'ID':<38} {'KIND':<10} {'STATUS':<10} {'PRICE':>10} SSH"]
|
|
261
|
+
lines += [f"{(i.name or '-'):<20} {i.id:<38} {i.kind:<10} {i.status:<10} {_money(i.hourly_price):>10} "
|
|
262
|
+
f"{i.ssh if i.ssh_ready else '-'}" for i in rows]
|
|
263
|
+
_emit(as_json, [i.to_dict() for i in rows], "\n".join(lines) if rows else "No machine.")
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
@vgpu.command("exec", context_settings={"ignore_unknown_options": True})
|
|
267
|
+
@click.argument("ref")
|
|
268
|
+
@click.argument("command", nargs=-1, required=True, type=click.UNPROCESSED)
|
|
269
|
+
@click.option("-d", "--detach", is_flag=True, help="Start in the background; prints the PID and the log path.")
|
|
270
|
+
@click.option("-i", "--identity", help="Private key file (default: VGPU_SSH_KEY, then your ssh defaults).")
|
|
271
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
272
|
+
@click.pass_context
|
|
273
|
+
def exec_cmd(ctx: click.Context, ref: str, command: tuple, detach: bool, identity: Optional[str], as_json: bool) -> None:
|
|
274
|
+
"""Run a command on the machine over SSH, with your local key."""
|
|
275
|
+
text = command[0] if len(command) == 1 else shlex.join(command)
|
|
276
|
+
inst = _find(ctx, ref)
|
|
277
|
+
target = _reachable(inst)
|
|
278
|
+
ssh = _need("ssh")
|
|
279
|
+
if not detach:
|
|
280
|
+
proc = _run(exec_argv(target, inst.id, text, identity=_identity(identity), ssh_bin=ssh))
|
|
281
|
+
ctx.exit(proc.returncode)
|
|
282
|
+
job = secrets.token_hex(4)
|
|
283
|
+
remote, log = detached_command(text, job)
|
|
284
|
+
proc = _run(exec_argv(target, inst.id, remote, identity=_identity(identity), ssh_bin=ssh), capture_output=True, timeout=120)
|
|
285
|
+
out = (proc.stdout or b"").decode("utf-8", "replace").strip().splitlines()
|
|
286
|
+
if proc.returncode != 0 or not out or not out[-1].strip().isdigit():
|
|
287
|
+
raise Fail(1, f"Could not start the background command (ssh exit {proc.returncode}).")
|
|
288
|
+
data = {"name": inst.name, "id": inst.id, "job": job, "pid": int(out[-1]), "log": log}
|
|
289
|
+
_emit(as_json, data, f"Started PID {data['pid']}, log {log}. Read it with: vgpu exec {ref} 'tail -n 50 {log}'")
|
|
290
|
+
|
|
291
|
+
|
|
292
|
+
@vgpu.command("scp")
|
|
293
|
+
@click.argument("ref")
|
|
294
|
+
@click.argument("source")
|
|
295
|
+
@click.argument("destination")
|
|
296
|
+
@click.option("--up", "upload", is_flag=True, help="Copy local SOURCE to remote DESTINATION (default: download).")
|
|
297
|
+
@click.option("-r", "--recursive", is_flag=True)
|
|
298
|
+
@click.option("-i", "--identity", help="Private key file (default: VGPU_SSH_KEY).")
|
|
299
|
+
@click.pass_context
|
|
300
|
+
def scp_cmd(ctx: click.Context, ref: str, source: str, destination: str, upload: bool, recursive: bool, identity: Optional[str]) -> None:
|
|
301
|
+
"""Copy files: `vgpu scp N /remote/path ./local` (or --up ./local /remote/path)."""
|
|
302
|
+
inst = _find(ctx, ref)
|
|
303
|
+
target = _reachable(inst)
|
|
304
|
+
argv = scp_argv(target, inst.id, source, destination, upload=upload, recursive=recursive,
|
|
305
|
+
identity=_identity(identity), scp_bin=_need("scp"))
|
|
306
|
+
ctx.exit(_run(argv).returncode)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
@vgpu.command("rm")
|
|
310
|
+
@click.argument("ref")
|
|
311
|
+
@click.option("-y", "--yes", is_flag=True)
|
|
312
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
313
|
+
@click.pass_context
|
|
314
|
+
def rm_cmd(ctx: click.Context, ref: str, yes: bool, as_json: bool) -> None:
|
|
315
|
+
"""Release a machine (the unused prepaid time of a standard pod is refunded)."""
|
|
316
|
+
inst = _find(ctx, ref)
|
|
317
|
+
_confirm(yes, f"Release {inst.name or inst.id} ({inst.kind})? Its disk is deleted.")
|
|
318
|
+
data = release(_state(ctx).client(), inst)
|
|
319
|
+
NameStore().remove_id(inst.id)
|
|
320
|
+
_emit(as_json, data, f"Released {inst.name or inst.id}.")
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _attest_failure(kind: str, challenge: str, check: str, detail: str) -> Tuple[Dict[str, Any], int]:
|
|
324
|
+
return {"verified": False, "status": "NOT_VERIFIED", "tee": attest.TEE[kind], "challenge": challenge, "failed": [check], "detail": detail}, 8
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def find_instance(client: Any, ref: str) -> Instance:
|
|
328
|
+
inst = resolve(list_instances(client), ref)
|
|
329
|
+
if inst is None:
|
|
330
|
+
raise Fail(5, f"No machine named or with id {ref!r} for this key. Run `vgpu ps`.")
|
|
331
|
+
return inst
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def attest_machine(client: Any, ref: str, *, out: Optional[str], offline: bool, identity: Optional[str],
|
|
335
|
+
vmpl: int = 0) -> Tuple[Dict[str, Any], int]:
|
|
336
|
+
"""The whole `vgpu attest` (CLI and local MCP server): result and exit code (0, 8 or 9).
|
|
337
|
+
|
|
338
|
+
``vmpl``: Confidential CPU only, the VMPL level the SEV-SNP report is requested at (SNP_VMPL).
|
|
339
|
+
"""
|
|
340
|
+
inst = find_instance(client, ref)
|
|
341
|
+
try:
|
|
342
|
+
script = attest.load_script(inst.kind)
|
|
343
|
+
except attest.AttestError as exc:
|
|
344
|
+
raise Fail(2 if inst.kind not in attest.SCRIPTS else 9, str(exc)) from None
|
|
345
|
+
target = _reachable(inst)
|
|
346
|
+
ssh, scp = _need("ssh"), _need("scp")
|
|
347
|
+
challenge = attest.new_challenge(inst.kind)
|
|
348
|
+
folder = Path(out or f"vgpu-attest-{inst.id}-{int(time.time())}")
|
|
349
|
+
folder.mkdir(parents=True, exist_ok=True)
|
|
350
|
+
key = _identity(identity)
|
|
351
|
+
level = vmpl if inst.kind == "cpu" else None
|
|
352
|
+
proc = _run(exec_argv(target, inst.id, attest.remote_command(challenge, level), identity=key, ssh_bin=ssh),
|
|
353
|
+
input=script, capture_output=True, timeout=1800)
|
|
354
|
+
log = folder / "in-the-vm.log"
|
|
355
|
+
log.write_bytes((proc.stdout or b"") + (proc.stderr or b""))
|
|
356
|
+
if proc.returncode != 0 and inst.kind == "cpu" and proc.returncode in attest.SNP_SCRIPT_EXIT:
|
|
357
|
+
name = attest.SNP_SCRIPT_EXIT[proc.returncode]
|
|
358
|
+
return _attest_failure(inst.kind, challenge, name,
|
|
359
|
+
f"the proof script refused the report ({name}, exit {proc.returncode}, SNP_VMPL={level}); see {log}")
|
|
360
|
+
if proc.returncode != 0:
|
|
361
|
+
return _attest_failure(inst.kind, challenge, "in_vm_script", f"the proof script exited with {proc.returncode}; see {log}")
|
|
362
|
+
archive = folder / Path(attest.ARCHIVES[inst.kind]).name
|
|
363
|
+
copy = _run(scp_argv(target, inst.id, attest.ARCHIVES[inst.kind], str(archive), identity=key, scp_bin=scp))
|
|
364
|
+
if copy.returncode != 0 or not archive.is_file():
|
|
365
|
+
return _attest_failure(inst.kind, challenge, "archive_copy", f"could not copy {attest.ARCHIVES[inst.kind]} (scp exit {copy.returncode})")
|
|
366
|
+
try:
|
|
367
|
+
extracted = attest.extract_archive(archive, folder / "proof")
|
|
368
|
+
if inst.kind == "vm":
|
|
369
|
+
result = attest.verify_gpu(extracted, challenge, offline=offline)
|
|
370
|
+
else:
|
|
371
|
+
result = attest.verify_cpu(extracted, challenge, snpguest=_tool("snpguest"), runner=_run, vmpl=vmpl)
|
|
372
|
+
except attest.ToolMissing as exc:
|
|
373
|
+
raise Fail(9, str(exc)) from None
|
|
374
|
+
except attest.AttestError as exc:
|
|
375
|
+
return _attest_failure(inst.kind, challenge, "archive", str(exc))
|
|
376
|
+
data = {"name": inst.name, "id": inst.id, **result.to_dict(), "folder": str(folder)}
|
|
377
|
+
return data, 0 if result.verified else 9 if result.status == "INCOMPLETE" else 8
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
@vgpu.command("attest")
|
|
381
|
+
@click.argument("ref")
|
|
382
|
+
@click.option("--offline", is_flag=True, help="GPU: use the collateral inside the bundle, no network call.")
|
|
383
|
+
@click.option("--out", type=click.Path(file_okay=False), help="Folder for the archive and the report.")
|
|
384
|
+
@click.option("-i", "--identity", help="Private key file (default: VGPU_SSH_KEY).")
|
|
385
|
+
@click.option("--vmpl", type=click.IntRange(0, 3), default=0, show_default=True,
|
|
386
|
+
help="Confidential CPU: VMPL level the SEV-SNP report is requested at (the script asks again at 1 if 0 is refused).")
|
|
387
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
388
|
+
@click.pass_context
|
|
389
|
+
def attest_cmd(ctx: click.Context, ref: str, offline: bool, out: Optional[str], identity: Optional[str], vmpl: int,
|
|
390
|
+
as_json: bool) -> None:
|
|
391
|
+
"""Prove the machine is a genuine TEE: challenge drawn here, proofs made in the VM, checked here."""
|
|
392
|
+
data, code = attest_machine(_state(ctx).client(), ref, out=out, offline=offline, identity=identity, vmpl=vmpl)
|
|
393
|
+
failed = data.get("failed") or []
|
|
394
|
+
# 2.0.1: a VM verdict covers the NVIDIA GPU too; say so when its checks passed.
|
|
395
|
+
gpu_checked = any(str(c.get("name", "")).startswith("nvidia.") and c.get("ok") for c in data.get("checks") or [])
|
|
396
|
+
tee = f"{data['tee']} + NVIDIA GPU" if data.get("verified") and gpu_checked else data["tee"]
|
|
397
|
+
human = f"{data['status']}: {tee}, challenge {data['challenge'][:16]}..." + (f" failed: {', '.join(failed)}" if failed else "")
|
|
398
|
+
if data.get("detail"):
|
|
399
|
+
human += f" ({data['detail']})"
|
|
400
|
+
if (data.get("facts") or {}).get("vmpl_fallback"):
|
|
401
|
+
human += " (VMPL 0 refused: the guest is not at VMPL 0; report requested at VMPL 1)"
|
|
402
|
+
_emit(as_json, data, human)
|
|
403
|
+
raise click.exceptions.Exit(code)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
@vgpu.command("whoami")
|
|
407
|
+
@click.option("--json", "as_json", is_flag=True)
|
|
408
|
+
@click.pass_context
|
|
409
|
+
def whoami_cmd(ctx: click.Context, as_json: bool) -> None:
|
|
410
|
+
"""The key's scopes, visibility, budgets and spend, and the balance (the key itself is never shown)."""
|
|
411
|
+
state = _state(ctx)
|
|
412
|
+
client = state.client()
|
|
413
|
+
try:
|
|
414
|
+
raw = client.call("GET", "/volt/whoami")
|
|
415
|
+
except ApiError as exc:
|
|
416
|
+
if exc.status_code != 404:
|
|
417
|
+
raise
|
|
418
|
+
bal = client.get_balance()
|
|
419
|
+
raw = {"balance": bal.balance, "balance_hidden": bal.balance_hidden, "key": {}}
|
|
420
|
+
key = raw.get("key") if isinstance(raw, Mapping) and isinstance(raw.get("key"), Mapping) else {}
|
|
421
|
+
data: Dict[str, Any] = {"key": {**key, "masked": client.config.masked_key}, "balance": raw.get("balance"),
|
|
422
|
+
"currency": raw.get("currency", "USD"), "base_url": client.config.base_url}
|
|
423
|
+
if raw.get("balance") is None:
|
|
424
|
+
data["balance_hidden"] = raw.get("balance_hidden") or "This key cannot read the account balance."
|
|
425
|
+
lines: List[str] = [f"key {data['key']['masked']} {key.get('name') or ''}".rstrip(),
|
|
426
|
+
f"scopes {', '.join(key.get('scopes') or []) or 'full access'}",
|
|
427
|
+
f"machines {key.get('pod_visibility') or 'account'}",
|
|
428
|
+
f"budgets {json.dumps(_cents(key.get('budgets_usd') or {}))} spent {json.dumps(_cents(key.get('spent_usd') or {}))}",
|
|
429
|
+
f"balance {data['balance']} {data['currency']}" if data["balance"] is not None
|
|
430
|
+
else f"balance hidden ({data['balance_hidden']})"]
|
|
431
|
+
_emit(as_json, data, "\n".join(lines))
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
@vgpu.command("mcp")
|
|
435
|
+
def mcp_cmd() -> None:
|
|
436
|
+
"""Local MCP server over stdio (ls, up, ps, exec, scp, rm, attest, inference, whoami) for AI agents."""
|
|
437
|
+
from ..mcp.server import run_stdio
|
|
438
|
+
|
|
439
|
+
run_stdio()
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
def _report(fail: Fail, as_json: bool) -> int:
|
|
443
|
+
message = scrub(fail.message)
|
|
444
|
+
if as_json:
|
|
445
|
+
click.echo(json.dumps({"error": {"code": fail.code, "message": message, "exit_code": fail.exit_code, **fail.extra}}, indent=2))
|
|
446
|
+
click.echo(f"Error: {message}", err=True)
|
|
447
|
+
return fail.exit_code
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
def main(argv: Optional[List[str]] = None) -> None:
|
|
451
|
+
configure_stream(sys.stdout)
|
|
452
|
+
configure_stream(sys.stderr)
|
|
453
|
+
args = list(sys.argv[1:] if argv is None else argv)
|
|
454
|
+
as_json = "--json" in args
|
|
455
|
+
debug = os.environ.get("VOLT_DEBUG") == "1"
|
|
456
|
+
try:
|
|
457
|
+
code = vgpu.main(args=args, prog_name="vgpu", standalone_mode=False)
|
|
458
|
+
sys.exit(code if isinstance(code, int) else 0)
|
|
459
|
+
except click.exceptions.Exit as exc:
|
|
460
|
+
sys.exit(exc.exit_code)
|
|
461
|
+
except click.exceptions.Abort:
|
|
462
|
+
click.echo("Aborted.", err=True)
|
|
463
|
+
sys.exit(1)
|
|
464
|
+
except click.ClickException as exc:
|
|
465
|
+
exc.show()
|
|
466
|
+
sys.exit(2 if isinstance(exc, click.UsageError) else exc.exit_code)
|
|
467
|
+
except Fail as fail:
|
|
468
|
+
sys.exit(_report(fail, as_json))
|
|
469
|
+
except ApiError as exc:
|
|
470
|
+
if debug:
|
|
471
|
+
raise
|
|
472
|
+
sys.exit(_report(api_fail(exc), as_json))
|
|
473
|
+
except ConfigError as exc:
|
|
474
|
+
sys.exit(_report(Fail(3, str(exc), "API_KEY_MISSING"), as_json))
|
|
475
|
+
except (TtlError, RemotePathError, ValueError) as exc:
|
|
476
|
+
sys.exit(_report(Fail(2, str(exc)), as_json))
|
|
477
|
+
except subprocess.TimeoutExpired:
|
|
478
|
+
sys.exit(_report(Fail(1, "The remote command timed out."), as_json))
|
|
479
|
+
except KeyboardInterrupt:
|
|
480
|
+
click.echo("Interrupted.", err=True)
|
|
481
|
+
sys.exit(130)
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+
if __name__ == "__main__":
|
|
485
|
+
main()
|
volt/mcp/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Local MCP server of the `vgpu` command (stdio)."""
|