Skip to content
Back to search

vast-cli

Vast.ai python and cli api client

Safety scan

A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.

Checked today

Check it deeper on your machine

It has changed since its scan on 11 Nov 2026.

As a Claude Code plugin, install it and then ask Claude Code:

/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/vast-ai/vast-cli with scanskillsafety

Or from a terminal, clone it and run it:

git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/vast-ai/vast-cli'

It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.

Badge for your README
findsafeskills safety rating

Markdown

[![findsafeskills safety rating](/api/listings/vast-ai--vast-cli/badge.svg)](/listings/vast-ai--vast-cli)

HTML

<a href="/listings/vast-ai--vast-cli"><img src="/api/listings/vast-ai--vast-cli/badge.svg" alt="findsafeskills safety rating" /></a>

Install

i

Install instructions from the README

As written by the repo's author. findsafeskills has not verified these steps.

These steps run a script from the network that findsafeskills does not scan.

**Linux, macOS, or WSL** — install the CLI with no Python required:

curl -fsSL https://vast.ai/install.sh | bash

Update anytime with `vastai update`.

**Windows, or using the Python SDK** — install from PyPI instead:

pip install vastai

What was scanned

The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.

README.md · README · 112 lines · 1 finding marked

View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)

  1. # Vast.ai Python SDK & CLI
  2. [![PyPI version](https://badge.fury.io/py/vastai.svg)](https://badge.fury.io/py/vastai)
  3. Provides both the CLI and SDK for managing Vast.ai GPU cloud resources, plus a serverless client for endpoint inference.
  4. ## Install
  5. **Linux, macOS, or WSL** — install the CLI with no Python required:
  6. ```bash
  7. curl -fsSL https://vast.ai/install.sh | bash
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 11 on GitHub
  8. ```
  9. Update anytime with `vastai update`.
  10. **Windows, or using the Python SDK** — install from PyPI instead:
  11. ```bash
  12. pip install vastai
  13. ```
  14. ## Quickstart
  15. 1. Get your API key from [https://cloud.vast.ai/manage-keys/](https://cloud.vast.ai/manage-keys/)
  16. 2. Set your API key:
  17. ```bash
  18. vastai set api-key YOUR_API_KEY
  19. ```
  20. 3. Test a search:
  21. ```bash
  22. vastai search offers --limit 3
  23. ```
  24. You should see a short list of available GPU offers.
  25. ## CLI Usage
  26. The `vastai` command provides full access to the Vast.ai platform from your terminal:
  27. ```bash
  28. vastai search offers 'gpu_name=RTX_4090 num_gpus>=4'
  29. vastai create instance 12345 --image pytorch/pytorch --disk 32 --ssh --direct
  30. vastai show instances
  31. vastai stop instance 12345
  32. vastai destroy instance 12345
  33. ```
  34. Run `vastai --help` for a full list of commands. You can also use `--help` on any subcommand:
  35. ```bash
  36. vastai search offers --help
  37. vastai create instance --help
  38. ```
  39. ## SDK Usage
  40. ```python
  41. from vastai import VastAI
  42. vast = VastAI() # uses VAST_API_KEY env var, or pass api_key="..."
  43. vast.search_offers(query='gpu_name=RTX_4090 num_gpus>=4')
  44. vast.show_instances()
  45. vast.start_instance(id=12345)
  46. vast.stop_instance(id=12345)
  47. ```
  48. Use `help(vast.search_offers)` to view documentation for any method.
  49. ## Using the Serverless Client
  50. 1. Create the client
  51. ```python
  52. from vastai import Serverless
  53. serverless = Serverless() # or, Serverless("YOUR_API_KEY")
  54. ```
  55. 2. Get an endpoint
  56. ```python
  57. endpoint = await serverless.get_endpoint("my-endpoint")
  58. ```
  59. 3. Make a request
  60. ```python
  61. request_body = {
  62. "model": "Qwen/Qwen3-8B",
  63. "prompt" : "Who are you?",
  64. "max_tokens" : 100,
  65. "temperature" : 0.7
  66. }
  67. response = await serverless.request("/v1/completions", request_body)
  68. ```
  69. 4. Read the response
  70. ```python
  71. text = response["response"]["choices"][0]["text"]
  72. print(text)
  73. ```
  74. Find more examples in the `examples/` directory.
  75. ## AI Agents
  76. Vast.ai has a skill for AI coding agents (Claude Code, Cursor, Windsurf, Codex, etc.):
  77. ```bash
  78. npx skills add vast-ai/vast-cli
  79. ```
  80. This installs the Vast.ai skill so your agent can search offers, create instances, and manage GPU workflows directly. See [CLI SKILL.md](vastai/SKILL.md) or [SDK SKILL.md](vastai_sdk/SKILL.md) for the full reference.
  81. ## Contributing
  82. This [repository](https://github.com/vast-ai/vast-cli) is open source. If you find a bug, please [open an issue](https://github.com/vast-ai/vast-cli/issues). PRs are welcome.
vastai/cli/commands/billing.py · Bundled script · 769 lines · 1 finding marked
  1. """CLI commands for billing, invoices, earnings, and user account management."""
  2. import json
  3. import sys
  4. import time
  5. from copy import deepcopy
  6. from datetime import datetime, timezone
  7. from io import StringIO
  8. from vastai.cli.parser import argument
  9. from vastai.cli.display import (
  10. display_table, invoice_fields, user_fields, ipaddr_fields,
  11. scheduled_jobs_fields, deindent, print_or_page,
  12. )
  13. from vastai.api import billing as billing_api
  14. from vastai.api import auth as auth_api
  15. from vastai.api import teams as teams_api
  16. from vastai.cli.utils import get_parser as _get_parser, get_client # noqa: F401
  17. parser = _get_parser()
  18. # ---------------------------------------------------------------------------
  19. # Helper utilities
  20. # ---------------------------------------------------------------------------
  21. def convert_timestamp_to_date(unix_timestamp):
  22. utc_datetime = datetime.fromtimestamp(unix_timestamp, tz=timezone.utc)
  23. return utc_datetime.strftime("%Y-%m-%d")
  24. def to_timestamp_(val):
  25. """Convert a date string or int to a UNIX timestamp."""
  26. if isinstance(val, int):
  27. return val
  28. if isinstance(val, str):
  29. if val.isdigit():
  30. return int(val)
  31. return int(datetime.strptime(val + "+0000", '%Y-%m-%d%z').timestamp())
  32. raise ValueError("Invalid date format")
  33. def _sum(X, k):
  34. y = 0
  35. for x in X:
  36. a = float(x.get(k, 0))
  37. y += a
  38. return y
  39. def _select(X, k):
  40. Y = set()
  41. for x in X:
  42. v = x.get(k, None)
  43. if v is not None:
  44. Y.add(v)
  45. return Y
  46. def filter_invoice_items(args, rows):
  47. """Filter invoice items by date range and charge/credit type."""
  48. from datetime import date
  49. try:
  50. from dateutil import parser as dateutil_parser
  51. except ImportError:
  52. print("\nWARNING: Missing dateutil, can't parse time format")
  53. end_timestamp = 9999999999
  54. start_timestamp = 0
  55. start_date_txt = ""
  56. end_date_txt = ""
  57. if args.end_date:
  58. try:
  59. end_date = dateutil_parser.parse(str(args.end_date))
  60. end_date_txt = end_date.isoformat()
  61. if end_date.tzinfo is None:
  62. end_date = end_date.replace(tzinfo=timezone.utc)
  63. end_timestamp = end_date.timestamp()
  64. except ValueError:
  65. print("Warning: Invalid end date format! Ignoring end date!")
  66. if args.start_date:
  67. try:
  68. start_date = dateutil_parser.parse(str(args.start_date))
  69. start_date_txt = start_date.isoformat()
  70. if start_date.tzinfo is None:
  71. start_date = start_date.replace(tzinfo=timezone.utc)
  72. start_timestamp = start_date.timestamp()
  73. except ValueError:
  74. print("Warning: Invalid start date format! Ignoring start date!")
  75. selector_flag = ""
  76. if args.only_charges:
  77. type_txt = "Only showing charges."
  78. selector_flag = "only_charges"
  79. def type_filter_fn(row):
  80. return row["type"] == "charge"
  81. elif args.only_credits:
  82. type_txt = "Only showing credits."
  83. selector_flag = "only_credits"
  84. def type_filter_fn(row):
  85. return row["type"] == "payment"
  86. else:
  87. type_txt = ""
  88. def type_filter_fn(row):
  89. return True
  90. if args.end_date:
  91. if args.start_date:
  92. header_text = f'Invoice items after {start_date_txt} and before {end_date_txt}.'
  93. else:
  94. header_text = f'Invoice items before {end_date_txt}.'
  95. elif args.start_date:
  96. header_text = f'Invoice items after {start_date_txt}.'
  97. else:
  98. header_text = " "
  99. header_text = header_text + " " + type_txt
  100. rows = list(filter(
  101. lambda row: end_timestamp >= (row["timestamp"] or 0.0) >= start_timestamp
  102. and type_filter_fn(row) and float(row["amount"]) != 0,
  103. rows
  104. ))
  105. if start_date_txt:
  106. start_date_txt = "S:" + start_date_txt
  107. if end_date_txt:
  108. end_date_txt = "E:" + end_date_txt
  109. now = date.today()
  110. invoice_number = now.year * 12 + now.month - 1
  111. pdf_filename_fields = list(filter(
  112. lambda fld: fld != "",
  113. [str(invoice_number), start_date_txt, end_date_txt, selector_flag]
  114. ))
  115. filename = "invoice_" + "-".join(pdf_filename_fields) + ".pdf"
  116. return {"rows": rows, "header_text": header_text, "pdf_filename": filename}
  117. # ---------------------------------------------------------------------------
  118. # Invoices-v1 helpers (rich formatting)
  119. # ---------------------------------------------------------------------------
  120. charge_types = ['instance', 'volume', 'serverless', 'i', 'v', 's']
  121. invoice_types = {
  122. "transfers": "transfer",
  123. "stripe": "stripe_payments",
  124. "bitpay": "bitpay",
  125. "coinbase": "coinbase",
  126. "crypto.com": "crypto.com",
  127. "reserved": "instance_prepay",
  128. "payout_paypal": "paypal_manual",
  129. "payout_wise": "wise_manual",
  130. }
  131. def format_invoices_charges_results(args, results):
  132. indices_to_remove = []
  133. for i, item in enumerate(results):
  134. item['start'] = convert_timestamp_to_date(item['start']) if item['start'] else None
  135. item['end'] = convert_timestamp_to_date(item['end']) if item['end'] else None
  136. if item['amount'] == 0:
  137. indices_to_remove.append(i)
  138. elif args.invoices:
  139. if item['type'] not in {'transfer', 'payout'}:
  140. item['amount'] *= -1
  141. item['amount_str'] = f"${item['amount']:.2f}" if item['amount'] > 0 else f"-${abs(item['amount']):.2f}"
  142. else:
  143. item['amount'] = f"${item['amount']:.3f}"
  144. if args.charges:
  145. if item['type'] in {'instance', 'volume'} and not args.verbose:
  146. item['items'] = []
  147. if item['source'] and '-' in item['source']:
  148. item['type'], item['source'] = item['source'].capitalize().split('-')
  149. item['items'] = format_invoices_charges_results(args, item['items'])
  150. for i in reversed(indices_to_remove):
  151. del results[i]
  152. return results
  153. def rich_object_to_string(rich_obj, no_color=True):
  154. """Render a Rich object (Table or Tree) to a string."""
  155. from rich.console import Console
  156. buffer = StringIO()
  157. console = Console(record=True, file=buffer)
  158. console.print(rich_obj)
  159. return console.export_text(clear=True, styles=not no_color)
  160. def create_charges_tree(results, parent=None, title="Charges Breakdown"):
  161. """Build and return a Rich Tree from nested charge results."""
  162. from rich.text import Text
  163. from rich.tree import Tree
  164. from rich.panel import Panel
  165. if parent is None:
  166. root = Tree(Text(title, style="bold red"))
  167. create_charges_tree(results, root)
  168. return Panel(root, style="white on #000000", expand=False)
  169. top_level = (parent.label.plain == title)
  170. for item in results:
  171. end_date = f" -> {item['end']}" if item['start'] != item['end'] else ""
  172. label = Text.assemble(
  173. (item["type"], "bold cyan"),
  174. (f" {item['source']}" if item.get('source') else "", "gold1"), " -> ",
  175. (f"{item['amount']}", 'bold green1' if top_level else 'green1'),
  176. (f" -- {item['description']}", "bright_white" if top_level else "dim white"),
  177. (f" ({item['start']}{end_date})", "bold bright_white" if top_level else "white")
  178. )
  179. node = parent.add(label, guide_style="blue3")
  180. if item.get("items"):
  181. create_charges_tree(item["items"], node)
  182. return parent
  183. def create_rich_table_for_charges(args, results):
  184. """Build and return a Rich Table from charge results."""
  185. from rich.table import Table
  186. from rich.text import Text
  187. from rich import box
  188. from rich.padding import Padding
  189. table = Table(style="white", header_style="bold bright_yellow", box=box.DOUBLE_EDGE, row_styles=["on grey11", "none"])
  190. table.add_column(Text("Type", justify="center"), style="bold steel_blue1", justify="center")
  191. table.add_column(Text("ID", justify="center"), style="gold1", justify="center")
  192. table.add_column(Text("Amount", justify="center"), style="sea_green2", justify="right")
  193. table.add_column(Text("Start", justify="center"), style="bright_white", justify="center")
  194. table.add_column(Text("End", justify="center"), style="bright_white", justify="center")
  195. if not args.charge_type or 'serverless' in args.charge_type:
  196. table.add_column(Text("Endpoint", justify="center"), style="bright_red", justify="center")
  197. table.add_column(Text("Workergroup", justify="center"), style="orchid", justify="center")
  198. for item in results:
  199. row = [item['type'].capitalize(), item['source'], item['amount'], item['start'], item['end']]
  200. if not args.charge_type or 'serverless' in args.charge_type:
  201. row.append(str(item['metadata'].get('endpoint_id', '')))
  202. row.append(str(item['metadata'].get('workergroup_id', '')))
  203. table.add_row(*row)
  204. return Padding(table, (1, 2), style="on #000000", expand=False)
  205. def create_rich_table_for_invoices(results):
  206. """Build and return a Rich Table from invoice results."""
  207. from rich.table import Table
  208. from rich.text import Text
  209. from rich import box
  210. from rich.padding import Padding
  211. invoice_type_to_color = {
  212. "credit": "green1",
  213. "transfer": "gold1",
  214. "payout": "orchid",
  215. "reserved": "sky_blue1",
  216. "refund": "bright_red",
  217. }
  218. table = Table(style="white", header_style="bold bright_yellow", box=box.DOUBLE_EDGE, row_styles=["on grey11", "none"])
  219. table.add_column(Text("ID", justify="center"), style="bright_white", justify="center")
  220. table.add_column(Text("Created", justify="center"), style="yellow3", justify="center")
  221. table.add_column(Text("Paid", justify="center"), style="yellow3", justify="center")
  222. table.add_column(Text("Type", justify="center"), justify="center")
  223. table.add_column(Text("Result", justify="center"), justify="right")
  224. table.add_column(Text("Source", justify="center"), style="bright_cyan", justify="center")
  225. table.add_column(Text("Description", justify="center"), style="bright_white", justify="left")
  226. for item in results:
  227. table.add_row(
  228. str(item['metadata']['invoice_id']),
  229. item['start'],
  230. item['end'] if item['end'] else 'N/A',
  231. Text(item['type'].capitalize(), style=invoice_type_to_color.get(item['type'], "white")),
  232. Text(item['amount_str'], style="sea_green2" if item['amount'] > 0 else "bright_red"),
  233. item['source'].capitalize() if item['type'] != 'transfer' else item['source'],
  234. item['description'],
  235. )
  236. return Padding(table, (1, 2), style="on #000000", expand=False)
  237. # ---------------------------------------------------------------------------
  238. # show invoices (deprecated)
  239. # ---------------------------------------------------------------------------
  240. @parser.command(
  241. argument("-q", "--quiet", action="store_true", help="only display numeric ids"),
  242. argument("-s", "--start_date", help="start date and time for report. Many formats accepted (optional)", type=str),
  243. argument("-e", "--end_date", help="end date and time for report. Many formats accepted (optional)", type=str),
  244. argument("-c", "--only_charges", action="store_true", help="Show only charge items"),
  245. argument("-p", "--only_credits", action="store_true", help="Show only credit items"),
  246. argument("--instance_label", help="Filter charges on a particular instance label (useful for autoscaler groups)"),
  247. usage="(DEPRECATED) vastai show invoices [OPTIONS]",
  248. help="(DEPRECATED) Get billing history reports. Use `vastai show invoices-v1` instead.",
  249. )
  250. def show__invoices(args):
  251. """Show current payments and charges (deprecated)."""
  252. if not args.quiet:
  253. print("DEPRECATED: `vastai show invoices` will be removed in a future release. "
  254. "Use `vastai show invoices-v1` for the new paginated command.", file=sys.stderr)
  255. client = get_client(args)
  256. result = billing_api.show_invoices(client, start_date=args.start_date, end_date=args.end_date,
  257. only_charges=args.only_charges, only_credits=args.only_credits)
  258. rows = result["invoices"]
  259. current_charges = result["current"]
  260. invoice_filter_data = filter_invoice_items(args, rows)
  261. rows = invoice_filter_data["rows"]
  262. filter_header = invoice_filter_data["header_text"]
  263. if args.instance_label:
  264. contract_ids = _select(rows, 'instance_id')
  265. req_json = {
  266. "label": args.instance_label,
  267. "contract_ids": list(contract_ids)
  268. }
  269. if args.explain:
  270. print("request json: ")
  271. print(req_json)
  272. result2 = client.post("/contracts/fetch/", json_data=req_json)
  273. result2.raise_for_status()
  274. filtered_rows = result2.json()["contracts"]
  275. contract_ids = _select(filtered_rows, 'id')
  276. rows = [row for row in rows if row.get("instance_id") in contract_ids]
  277. if args.quiet:
  278. for row in rows:
  279. id = row.get("id", None)
  280. if id is not None:
  281. print(id)
  282. elif args.raw:
  283. return rows
  284. else:
  285. print(filter_header)
  286. display_table(rows, invoice_fields)
  287. print(f"Total: ${_sum(rows, 'amount')}")
  288. print("Current: ", current_charges)
  289. # ---------------------------------------------------------------------------
  290. # show invoices-v1 (advanced with pagination and rich formatting)
  291. # ---------------------------------------------------------------------------
  292. @parser.command(
  293. argument('-i', '--invoices', mutex_group='grp', action='store_true', required=True, help='Show invoices instead of charges'),
  294. argument('-it', '--invoice-type', choices=invoice_types.keys(), nargs='+', metavar='type',
  295. help=f'Filter which types of invoices to show: {{{", ".join(invoice_types.keys())}}}'),
  296. argument('-c', '--charges', mutex_group='grp', action='store_true', required=True, help='Show charges instead of invoices'),
  297. argument('-ct', '--charge-type', choices=charge_types, nargs='+', metavar='type',
  298. help='Filter which types of charges to show: {i|instance, v|volume, s|serverless}'),
  299. argument('-s', '--start-date', help='Start date (YYYY-MM-DD or timestamp)'),
  300. argument('-e', '--end-date', help='End date (YYYY-MM-DD or timestamp)'),
  301. argument('-l', '--limit', type=int, default=20, help='Number of results per page (default: 20, max: 100)'),
  302. argument('-t', '--next-token', help='Pagination token for next page'),
  303. argument('-f', '--format', choices=['table', 'tree'], default='table', help='Output format for charges (default: table)'),
  304. argument('-v', '--verbose', action='store_true', help='Include full Instance Charge details and Invoice Metadata (tree view only)'),
  305. argument('--latest-first', action='store_true', help='Sort by latest first'),
  306. usage="vastai show invoices-v1 [OPTIONS]",
  307. help="Get billing (invoices/charges) history reports with advanced filtering and pagination",
  308. epilog=deindent("""
  309. This command supports colored output and rich formatting if the 'rich' python module is installed!
  310. Examples:
  311. # Show the first 20 invoices in the last week (note: default window is a 7 day period ending today)
  312. vastai show invoices-v1 --invoices
  313. # Show the first 50 charges over a 7 day period starting from 2025-11-30 in tree format
  314. vastai show invoices-v1 --charges -s 2025-11-30 -f tree -l 50
  315. # Show the first 20 invoices of specific types for the month of November 2025
  316. vastai show invoices-v1 -i -it stripe bitpay transfers --start-date 2025-11-01 --end-date 2025-11-30
  317. # Show the first 20 charges for only volumes and serverless instances between two dates, including all details and metadata
  318. vastai show invoices-v1 -c --charge-type v s -s 2025-11-01 -e 2025-11-05 --format tree --verbose
  319. # Get the next page of paginated invoices, limit to 50 per page (note: type/date filters MUST match previous request for pagination to work)
  320. vastai show invoices-v1 --invoices --limit 50 --next-token eyJ2YWx1ZXMiOiB7ImlkIjogMjUwNzgyMzR9LCAib3NfcGFnZSI6IDB9
    • Dismissed after review: Contains a long base64-looking blob — not necessarily malicious, but worth inspecting what it decodes to
    • Line 380 on GitHub
  321. # Show the last 10 instance (only) charges over a 7 day period ending in 2025-12-25, sorted by latest charges first
  322. vastai show invoices-v1 --charges -ct instance --end-date 2025-12-25 -l 10 --latest-first
  323. """),
  324. )
  325. def show__invoices_v1(args):
  326. """Get billing (invoices/charges) history with advanced filtering and pagination."""
  327. output_lines = []
  328. try:
  329. from rich.prompt import Confirm
  330. has_rich = True
  331. except ImportError:
  332. output_lines.append("NOTE: To view results in color and table/tree format please install the 'rich' python module with 'pip install rich'\n")
  333. has_rich = False
  334. # Handle default start and end date values
  335. if not args.start_date and not args.end_date:
  336. args.end_date = int(time.time())
  337. if not args.start_date:
  338. args.start_date = args.end_date - 7 * 24 * 60 * 60
  339. elif not args.end_date:
  340. args.end_date = args.start_date + 7 * 24 * 60 * 60
  341. try:
  342. start_timestamp = to_timestamp_(args.start_date)
  343. end_timestamp = to_timestamp_(args.end_date)
  344. except Exception as e:
  345. print(f"Error parsing dates: {e}")
  346. print("Use format YYYY-MM-DD or UNIX timestamp")
  347. return
  348. if has_rich and not args.no_color and not args.raw:
  349. print("(use --no-color to disable colored output)\n")
  350. start_date = convert_timestamp_to_date(start_timestamp)
  351. end_date = convert_timestamp_to_date(end_timestamp)
  352. data_type = "Instance Charges" if args.charges else "Invoices"
  353. output_lines.append(f"Fetching {data_type} from {start_date} to {end_date}...")
  354. # Build SDK params
  355. params = {
  356. "charges": args.charges,
  357. "start_date": args.start_date,
  358. "end_date": args.end_date,
  359. "limit": min(args.limit, 100) if args.limit > 0 else 20,
  360. "latest_first": args.latest_first,
  361. "format": args.format if args.charges else "table",
  362. "charge_type": args.charge_type or [],
  363. "invoice_type": args.invoice_type or [],
  364. "next_token": args.next_token,
  365. }
  366. client = get_client(args)
  367. if args.raw:
  368. # Single-page raw response; dispatcher JSON-prints. Callers paginate
  369. # by re-running with --next-token.
  370. return billing_api.show_invoices_v1(client, params)
  371. found_results, found_count = [], 0
  372. looping = True
  373. while looping:
  374. response = billing_api.show_invoices_v1(client, params)
  375. found_results += response.get('results', [])
  376. found_count += response.get('count', 0)
  377. total = response.get('total', 0)
  378. next_token = response.get('next_token')
  379. if has_rich is False:
  380. output_lines.append("Raw response:\n" + json.dumps(response, indent=2))
  381. if next_token:
  382. print(f"Next page token: {next_token}\n")
  383. elif not found_results:
  384. output_lines.append("No results found")
  385. else:
  386. formatted_results = format_invoices_charges_results(args, deepcopy(found_results))
  387. if args.invoices:
  388. rich_obj = create_rich_table_for_invoices(formatted_results)
  389. elif args.format == 'tree':
  390. rich_obj = create_charges_tree(formatted_results)
  391. else:
  392. rich_obj = create_rich_table_for_charges(args, formatted_results)
  393. output_lines.append(rich_object_to_string(rich_obj, no_color=args.no_color))
  394. output_lines.append(f"Showing {found_count} of {total} results")
  395. if next_token:
  396. output_lines.append(f"Next page token: {next_token}\n")
  397. paging = print_or_page(args, '\n'.join(output_lines))
  398. if next_token and not paging:
  399. if has_rich:
  400. ans = Confirm.ask("Fetch next page?", show_default=False, default=False)
  401. else:
  402. ans = input("Fetch next page? (y/N): ").strip().lower() == 'y'
  403. if ans:
  404. params['next_token'] = next_token
  405. output_lines.clear()
  406. args.full = True
  407. else:
  408. looping = False
  409. else:
  410. looping = False
  411. # ---------------------------------------------------------------------------
  412. # show earnings
  413. # ---------------------------------------------------------------------------
  414. @parser.command(
  415. argument("-q", "--quiet", action="store_true", help="only display numeric ids"),
  416. argument("-s", "--start_date", help="start date and time for report. Many formats accepted", type=str),
  417. argument("-e", "--end_date", help="end date and time for report. Many formats accepted ", type=str),
  418. argument("-m", "--machine_id", help="Machine id (optional)", type=int),
  419. usage="vastai show earnings [OPTIONS]",
  420. help="Get machine earning history reports",
  421. )
  422. def show__earnings(args):
  423. """Show earnings history for a time range, optionally per machine."""
  424. client = get_client(args)
  425. rows = billing_api.show_earnings(client, start_date=args.start_date, end_date=args.end_date,
  426. machine_id=args.machine_id)
  427. if args.raw:
  428. return rows
  429. print(json.dumps(rows, indent=1, sort_keys=True))
  430. # ---------------------------------------------------------------------------
  431. # show deposit
  432. # ---------------------------------------------------------------------------
  433. @parser.command(
  434. argument("id", help="id of instance to get info for", type=int),
  435. usage="vastai show deposit ID [options]",
  436. help="Display reserve deposit info for an instance",
  437. )
  438. def show__deposit(args):
  439. """Show reserve deposit info for an instance."""
  440. client = get_client(args)
  441. result = billing_api.show_deposit(client, id=args.id)
  442. print(json.dumps(result, indent=1, sort_keys=True))
  443. # ---------------------------------------------------------------------------
  444. # show / set user
  445. # ---------------------------------------------------------------------------
  446. @parser.command(
  447. argument("-q", "--quiet", action="store_true", help="display information about user"),
  448. usage="vastai show user [OPTIONS]",
  449. help="Get current user data",
  450. epilog=deindent("""
  451. Shows stats for logged-in user. These include user balance, email, and ssh key. Does not show API key.
  452. """),
  453. )
  454. def show__user(args):
  455. """Show stats for logged-in user."""
  456. client = get_client(args)
  457. user_blob = billing_api.show_user(client)
  458. if args.raw:
  459. return user_blob
  460. else:
  461. display_table([user_blob], user_fields)
  462. @parser.command(
  463. argument("--file", help="file path for params in json format", type=str),
  464. usage="vastai set user --file FILE",
  465. help="Update user data from json file",
  466. epilog=deindent("""
  467. Available fields:
  468. Name Type Description
  469. ssh_key string
  470. paypal_email string
  471. wise_email string
  472. email string
  473. normalized_email string
  474. username string
  475. fullname string
  476. billaddress_line1 string
  477. billaddress_line2 string
  478. billaddress_city string
  479. billaddress_zip string
  480. billaddress_country string
  481. billaddress_taxinfo string
  482. balance_threshold_enabled string
  483. balance_threshold string
  484. autobill_threshold string
  485. phone_number string
  486. """),
  487. )
  488. def set__user(args):
  489. """Update user data from a json file."""
  490. params = None
  491. with open(args.file, 'r') as file:
  492. params = json.load(file)
  493. client = get_client(args)
  494. result = billing_api.set_user(client, params=params)
  495. print(f"{result}")
  496. # ---------------------------------------------------------------------------
  497. # show subaccounts / create subaccount
  498. # ---------------------------------------------------------------------------
  499. @parser.command(
  500. argument("-q", "--quiet", action="store_true", help="display subaccounts from current user"),
  501. usage="vastai show subaccounts [OPTIONS]",
  502. help="Get current subaccounts",
  503. )
  504. def show__subaccounts(args):
  505. """Show subaccounts."""
  506. client = get_client(args)
  507. rows = billing_api.show_subaccounts(client)
  508. if args.raw:
  509. return rows
  510. else:
  511. display_table(rows, user_fields)
  512. @parser.command(
  513. argument("--email", help="email address to use for login", type=str),
  514. argument("--username", help="username to use for login", type=str),
  515. argument("--password", help="password to use for login", type=str),
  516. argument("--type", help="host/client", type=str),
  517. usage="vastai create subaccount --email EMAIL --username USERNAME --password PASSWORD --type TYPE",
  518. help="Create a subaccount",
  519. epilog=deindent("""
  520. Creates a new account that is considered a child of your current account as defined via the API key.
  521. vastai create subaccount --email bob@gmail.com --username bob --password password --type host
  522. vastai create subaccount --email vast@gmail.com --username vast --password password --type host
  523. """),
  524. )
  525. def create__subaccount(args):
  526. """Create a subaccount under your current account."""
  527. host_only = False
  528. if args.type:
  529. host_only = args.type.lower() == "host"
  530. if getattr(args, 'explain', False):
  531. print("Request JSON would be: ")
  532. print({
  533. "email": args.email, "username": args.username,
  534. "password": args.password, "host_only": host_only, "parent_id": "me",
  535. })
  536. return
  537. client = get_client(args)
  538. try:
  539. rj = billing_api.create_subaccount(client, email=args.email, username=args.username,
  540. password=args.password, host_only=host_only)
  541. print(rj)
  542. except Exception as e:
  543. print(f"Failed with error: {e}")
  544. # ---------------------------------------------------------------------------
  545. # show ipaddrs
  546. # ---------------------------------------------------------------------------
  547. @parser.command(
  548. usage="vastai show ipaddrs [--api-key API_KEY] [--raw]",
  549. help="Display user's history of ip addresses",
  550. )
  551. def show__ipaddrs(args):
  552. """Show the history of ip address accesses."""
  553. client = get_client(args)
  554. rows = billing_api.show_ipaddrs(client)
  555. if args.raw:
  556. return rows
  557. else:
  558. display_table(rows, ipaddr_fields)
  559. # ---------------------------------------------------------------------------
  560. # transfer credit
  561. # ---------------------------------------------------------------------------
  562. @parser.command(
  563. argument("recipient_pos", nargs="?", default=None, metavar="RECIPIENT",
  564. help="email (or id) of recipient account (positional; use --recipient to name)"),
  565. argument("amount_pos", nargs="?", default=None, type=float, metavar="AMOUNT",
  566. help="dollars of credit to transfer (positional; use --amount to name)"),
  567. argument("-r", "--recipient", help="email (or id) of recipient account", type=str, default=None),
  568. argument("-a", "--amount", help="dollars of credit to transfer", type=float, default=None),
  569. argument("--skip", help="skip confirmation", action="store_true", default=False),
  570. usage="vastai transfer credit [--recipient EMAIL] [--amount DOLLARS] [RECIPIENT AMOUNT]",
  571. help="Transfer credits to another account",
  572. epilog=deindent("""
  573. Transfer credits to another account. This action is irreversible.
  574. Supports both named flags (recommended) and legacy positional arguments:
  575. vastai transfer credit --recipient user@example.com --amount 25.50
  576. vastai transfer credit -r user@example.com -a 25.50
  577. vastai transfer credit user@example.com 25.50
  578. """),
  579. )
  580. def transfer__credit(args):
  581. """Transfer credits to another account."""
  582. recipient = args.recipient if args.recipient is not None else args.recipient_pos
  583. amount = args.amount if args.amount is not None else args.amount_pos
  584. if recipient is None or amount is None:
  585. print("Error: both recipient and amount are required (as flags or positional args).")
  586. return
  587. if not args.skip:
  588. print(f"Transfer ${amount} credit to account {recipient}? This is irreversible.")
  589. ok = input("Continue? [y/n] ")
  590. if ok.strip().lower() != "y":
  591. return
  592. if args.explain:
  593. print("request json: ")
  594. print({"sender": "me", "recipient": recipient, "amount": amount})
  595. client = get_client(args)
  596. try:
  597. rj = teams_api.transfer_credit(client, recipient=recipient, amount=amount)
  598. if rj["success"]:
  599. print(f"Sent {amount} to {recipient}")
  600. else:
  601. print(rj["msg"])
  602. except Exception as e:
  603. print(f"Failed with error: {e}")
  604. # ---------------------------------------------------------------------------
  605. # scheduled jobs
  606. # ---------------------------------------------------------------------------
  607. def normalize_schedule_fields(job):
  608. """Mutates the job dict to replace None values with readable scheduling labels."""
  609. if job.get("day_of_the_week") is None:
  610. job["day_of_the_week"] = "Everyday"
  611. else:
  612. days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
  613. job["day_of_the_week"] = days[int(job["day_of_the_week"])]
  614. if job.get("hour_of_the_day") is None:
  615. job["hour_of_the_day"] = "Every hour"
  616. else:
  617. hour = int(job["hour_of_the_day"])
  618. suffix = "AM" if hour < 12 else "PM"
  619. hour_12 = hour % 12
  620. hour_12 = 12 if hour_12 == 0 else hour_12
  621. job["hour_of_the_day"] = f"{hour_12}_{suffix}"
  622. if job.get("min_of_the_hour") is None:
  623. job["min_of_the_hour"] = "Every minute"
  624. else:
  625. job["min_of_the_hour"] = f"{int(job['min_of_the_hour']):02d}"
  626. return job
  627. @parser.command(
  628. usage="vastai show scheduled-jobs [--api-key API_KEY] [--raw]",
  629. help="Display the list of scheduled jobs",
  630. )
  631. def show__scheduled_jobs(args):
  632. """Show the list of scheduled jobs for the account."""
  633. client = get_client(args)
  634. rows = auth_api.show_scheduled_jobs(client)
  635. if args.raw:
  636. return rows
  637. else:
  638. rows = [normalize_schedule_fields(job) for job in rows]
  639. display_table(rows, scheduled_jobs_fields)
  640. @parser.command(
  641. argument("id", help="id of scheduled job to remove", type=int),
  642. usage="vastai delete scheduled-job ID",
  643. help="Delete a scheduled job",
  644. )
  645. def delete__scheduled_job(args):
  646. """Delete a scheduled job."""
  647. client = get_client(args)
  648. result = auth_api.delete_scheduled_job(client, id=args.id)
  649. print(result)
vastai/cli/commands/offers.py · Bundled script · 629 lines · 1 finding marked
  1. """CLI commands for searching offers, templates, and benchmarks."""
  2. import json
  3. import sys
  4. import time
  5. from vastai.cli.parser import argument, hidden_aliases
  6. from vastai.cli.display import display_table, displayable_fields, displayable_fields_reserved, benchmarks_displayable_fields, deindent
  7. from vastai.api import offers as offers_api
  8. from vastai.cli.utils import get_parser as _get_parser, get_client # noqa: F401
  9. parser = _get_parser()
  10. # ---------------------------------------------------------------------------
  11. # search offers
  12. # ---------------------------------------------------------------------------
  13. @parser.command(
  14. argument("-t", "--type", default="on-demand", help="Show 'on-demand', 'reserved', or 'bid'(interruptible) pricing. default: on-demand"),
  15. argument("-i", "--interruptible", dest="type", const="bid", action="store_const", help="Alias for --type=bid"),
  16. argument("-b", "--bid", dest="type", const="bid", action="store_const", help="Alias for --type=bid"),
  17. argument("-r", "--reserved", dest="type", const="reserved", action="store_const", help="Alias for --type=reserved"),
  18. argument("-d", "--on-demand", dest="type", const="on-demand", action="store_const", help="Alias for --type=on-demand"),
  19. argument("-n", "--no-default", action="store_true", help="Disable default query"),
  20. argument("--new", action="store_true", help="New search exp"),
  21. argument("--limit", type=int, help=""),
  22. argument("--storage", type=float, default=5.0, help="Amount of storage to use for pricing, in GiB. default=5.0GiB"),
  23. argument("-o", "--order", type=str, help="Comma-separated list of fields to sort on. postfix field with - to sort desc. ex: -o 'num_gpus,total_flops-'. default='score-'", default='score-'),
  24. argument("query", help="Query to search for. default: 'external=false rentable=true verified=true cpu_arch in [amd64,arm64]', pass -n to ignore default", nargs="*", default=None),
  25. usage="vastai search offers [--help] [--api-key API_KEY] [--raw] <query>",
  26. help="Search for instance types using custom query",
  27. epilog=deindent("""
  28. Query syntax:
  29. query = comparison comparison...
  30. comparison = field op value
  31. field = <name of a field>
  32. op = one of: <, <=, ==, !=, >=, >, in, notin
  33. value = <bool, int, float, string> | 'any' | [value0, value1, ...]
  34. bool: True, False
  35. note: to pass '>' and '<' on the command line, make sure to use quotes
  36. note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
  37. Examples:
  38. # search for somewhat reliable single RTX 3090 instances
  39. vastai search offers 'reliability > 0.98 num_gpus=1 gpu_name=RTX_3090'
  40. # search for datacenter gpus with minimal compute_cap and total_flops
  41. vastai search offers 'compute_cap > 610 total_flops > 5 datacenter=True'
  42. # search for reliable 4 gpu offers in Taiwan or Sweden
  43. vastai search offers 'reliability>0.99 num_gpus=4 geolocation in [TW,SE]'
  44. # search for reliable RTX 3090 or 4090 gpus NOT in China or Vietnam
  45. vastai search offers 'reliability>0.99 gpu_name in ["RTX 4090", "RTX 3090"] geolocation notin [CN,VN]'
  46. # search for machines with nvidia drivers 535.86.05 or greater (and various other options)
  47. vastai search offers 'disk_space>146 duration>24 gpu_ram>10 cuda_vers>=12.1 direct_port_count>=2 driver_version >= 535.86.05'
  48. # search for reliable machines with at least 4 gpus, unverified, order by num_gpus
  49. vastai search offers 'reliability > 0.99 num_gpus>=4 verified=False' -o 'num_gpus-'
  50. # search for arm64 cpu architecture
  51. vastai search offers 'cpu_arch=arm64'
  52. Available fields:
  53. Name Type Description
  54. bw_nvlink float bandwidth NVLink
  55. compute_cap: int cuda compute capability*100 (ie: 650 for 6.5, 700 for 7.0)
  56. cpu_arch string host machine cpu architecture (e.g. amd64, arm64)
  57. cpu_cores: int # virtual cpus
  58. cpu_ghz: Float # cpu clock speed GHZ
  59. cpu_cores_effective: float # virtual cpus you get
  60. cpu_ram: float system RAM in gigabytes
  61. cuda_vers: float machine max supported cuda version (based on driver version)
  62. datacenter: bool show only datacenter offers
  63. direct_port_count int open ports on host's router
  64. disk_bw: float disk read bandwidth, in MB/s
  65. disk_space: float disk storage space, in GB
  66. dlperf: float DL-perf score (see FAQ for explanation)
  67. dlperf_usd: float DL-perf/$
  68. dph: float $/hour rental cost
  69. driver_version: string machine's nvidia/amd driver version as 3 digit string ex. "535.86.05,"
  70. duration: float max rental duration in days
  71. external: bool show external offers in addition to datacenter offers
  72. flops_usd: float TFLOPs/$
  73. geolocation: string Two letter country code. Works with operators =, !=, in, notin (e.g. geolocation not in ['XV','XZ'])
  74. gpu_arch string host machine gpu architecture (e.g. nvidia, amd)
  75. gpu_max_power float GPU power limit (watts)
  76. gpu_max_temp float GPU temp limit (C)
  77. gpu_mem_bw: float GPU memory bandwidth in GB/s
  78. gpu_name: string GPU model name (no quotes, replace spaces with underscores, ie: RTX_3090 rather than 'RTX 3090')
  79. gpu_ram: float per GPU RAM in GB
  80. gpu_total_ram: float total GPU RAM in GB
  81. gpu_frac: float Ratio of GPUs in the offer to gpus in the system
  82. gpu_display_active: bool True if the GPU has a display attached
  83. has_avx: bool CPU supports AVX instruction set.
  84. id: int instance unique ID
  85. inet_down: float internet download speed in Mb/s
  86. inet_down_cost: float internet download bandwidth cost in $/GB
  87. inet_up: float internet upload speed in Mb/s
  88. inet_up_cost: float internet upload bandwidth cost in $/GB
  89. machine_id int machine id of instance
  90. min_bid: float current minimum bid price in $/hr for interruptible
  91. num_gpus: int # of GPUs
  92. pci_gen: float PCIE generation
  93. pcie_bw: float PCIE bandwidth (CPU to GPU)
  94. reliability: float machine reliability score (see FAQ for explanation)
  95. rentable: bool is the instance currently rentable
  96. storage_cost: float storage cost in $/GB/month
  97. static_ip: bool is the IP addr static/stable
  98. total_flops: float total TFLOPs from all GPUs
  99. ubuntu_version string host machine ubuntu OS version
  100. verified: bool is the machine verified
  101. vms_enabled: bool is the machine a VM instance
  102. """),
  103. aliases=hidden_aliases(["search instances"]),
  104. )
  105. def search__offers(args):
  106. """Creates a query based on search parameters as in the examples above.
  107. :param argparse.Namespace args: should supply all the command-line options
  108. """
  109. from vastai.api.query import parse_query, offers_fields, offers_alias, offers_mult
  110. try:
  111. if args.no_default:
  112. query = {}
  113. else:
  114. query = {"verified": {"eq": True}, "external": {"eq": False}, "rentable": {"eq": True}}
  115. if args.query is not None:
  116. query = parse_query(args.query, query, offers_fields, offers_alias, offers_mult)
  117. if not args.no_default:
  118. # Request every cpu arch like the console (CLN-3461); explicit cpu_arch wins.
  119. query.setdefault("cpu_arch", {"in": ["amd64", "arm64"]})
  120. order = []
  121. for name in args.order.split(","):
  122. name = name.strip()
  123. if not name:
  124. continue
  125. direction = "asc"
  126. field = name
  127. if name.strip("-") != name:
  128. direction = "desc"
  129. field = name.strip("-")
  130. if name.strip("+") != name:
  131. direction = "asc"
  132. field = name.strip("+")
  133. if field in offers_alias:
  134. field = offers_alias[field]
  135. order.append([field, direction])
  136. query["order"] = order
  137. query["type"] = args.type
  138. if args.limit:
  139. query["limit"] = int(args.limit)
  140. query["allocated_storage"] = args.storage
  141. if query["type"] == 'interruptible':
  142. query["type"] = 'bid'
  143. except ValueError as e:
  144. print("Error: ", e)
  145. return 1
  146. json_blob = query
  147. client = get_client(args)
  148. if args.new:
  149. json_blob = {"select_cols": ['*'], "q": query}
  150. if args.explain:
  151. print("request json: ")
  152. print(json_blob)
  153. r = client.put("/search/asks/", json_data=json_blob)
  154. else:
  155. if args.explain:
  156. print("request json: ")
  157. print(json_blob)
  158. r = client.post("/bundles/", json_data=json_blob)
  159. r.raise_for_status()
  160. if r.headers.get('Content-Type') != 'application/json':
  161. print(f"invalid return Content-Type: {r.headers.get('Content-Type')}")
  162. return
  163. rows = r.json()["offers"]
  164. if args.raw:
  165. return rows
  166. else:
  167. if args.type == "reserved":
  168. display_table(rows, displayable_fields_reserved)
  169. else:
  170. display_table(rows, displayable_fields)
  171. # ---------------------------------------------------------------------------
  172. # search benchmarks
  173. # ---------------------------------------------------------------------------
  174. @parser.command(
  175. argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
  176. argument("-a", "--all", action="store_true", help="force fetching every page, even with --limit/--next-token (fetching everything is already the default otherwise)"),
  177. argument("-l", "--limit", type=int, default=None, help="max results per page; passing this switches to single-page mode instead of fetching everything"),
  178. argument("-t", "--next-token", dest="next_token", help="resume from a pagination token (implies single-page mode)"),
  179. usage="vastai search benchmarks [--help] [--api-key API_KEY] [--raw] [--limit N] [--next-token TOKEN] <query>",
  180. help="Search for benchmark results using custom query",
  181. epilog=deindent("""
  182. By default this fetches every page and prints one combined result. Passing
  183. --limit or --next-token switches to single-page mode, which prints a page
  184. and asks whether to fetch the next one (add --all to still fetch every page
  185. in that mode). The prompt is skipped when output is piped or --raw is used;
  186. those print the next page token so you can resume with --next-token.
  187. --raw prints a flat list when fetching every page, and the full response
  188. (benchmarks plus next_token) in single-page mode.
  189. Query syntax:
  190. query = comparison comparison...
  191. comparison = field op value
  192. field = <name of a field>
  193. op = one of: <, <=, ==, !=, >=, >, in, notin
  194. value = <bool, int, float, string> | 'any' | [value0, value1, ...]
  195. bool: True, False
  196. note: to pass '>' and '<' on the command line, make sure to use quotes
  197. note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
  198. Examples:
  199. # search for benchmarks with value > 100 on RTX 4090s for 2 specific machines
  200. vastai search benchmarks 'value > 100.0 gpu_name=RTX_4090 machine_id in [302,402]'
  201. # a page at a time, prompting between pages
  202. vastai search benchmarks 'gpu_name=RTX_5090' --limit 50
  203. Available fields:
  204. Name Type Description
  205. contract_id int ID of instance/contract reporting benchmark
  206. dph_base float gpu $/hr at the time of the benchmark
  207. gpu_name string GPU model benchmarked (e.g. RTX_4090)
  208. id int benchmark unique ID
  209. image string image used for benchmark
  210. last_update float date of benchmark
  211. launch_args string launch args of the benchmarked worker
  212. machine_id int id of machine benchmarked
  213. num_gpus int number of gpus used in benchmark
  214. template_hash string hash of the template benchmarked (formerly 'name')
  215. template_id int id of the template benchmarked
  216. type string benchmark type
  217. value float benchmark result (formerly 'score')
  218. """),
  219. aliases=hidden_aliases(["search benchmarks"]),
  220. )
  221. def search__benchmarks(args):
  222. """Creates a query based on search parameters as in the examples above.
  223. :param argparse.Namespace args: should supply all the command-line options
  224. """
  225. from vastai.api.query import parse_query, benchmarks_fields, fix_date_fields
  226. try:
  227. query = {}
  228. if args.query is not None:
  229. query = parse_query(args.query, query, benchmarks_fields)
  230. query = fix_date_fields(query, ['last_update'])
  231. except ValueError as e:
  232. print("Error: ", e)
  233. return 1
  234. client = get_client(args)
  235. # Fetch every page by default; --limit/--next-token opt into manual pagination.
  236. fetch_all = args.all or (args.limit is None and args.next_token is None)
  237. if fetch_all:
  238. rows = offers_api.search_benchmarks(
  239. client, query=query, limit=args.limit, after_token=args.next_token)
  240. if args.raw:
  241. # Flat list of every matching row -- the contract scripts have
  242. # always depended on for --raw. Single-page mode returns the
  243. # envelope instead, since only it has a token worth handing back.
  244. return rows
  245. display_table(rows, benchmarks_displayable_fields)
  246. return
  247. params = offers_api.benchmarks_query_args(
  248. query=query, limit=args.limit, after_token=args.next_token)
  249. page = 0
  250. while True:
  251. page += 1
  252. data = offers_api.search_benchmarks_v1(client, params)
  253. rows = data.get("benchmarks") or []
  254. next_token = data.get("next_token")
  255. if args.raw:
  256. return {"success": True, "benchmarks_found": len(rows),
  257. "benchmarks": rows, "next_token": next_token}
  258. display_table(rows, benchmarks_displayable_fields)
  259. if not next_token:
  260. return
  261. print(f"Next page token: {next_token}")
  262. # Never block on a prompt when piped or scripted.
  263. if not (sys.stdin.isatty() and sys.stdout.isatty()):
  264. return
  265. try:
  266. if input(f"Fetch next page? (page {page + 1}) (y/N): ").strip().lower() != "y":
  267. return
  268. except (EOFError, KeyboardInterrupt):
  269. return
  270. params["after_token"] = next_token
  271. # ---------------------------------------------------------------------------
  272. # search templates
  273. # ---------------------------------------------------------------------------
  274. @parser.command(
  275. argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
  276. usage="vastai search templates [--help] [--api-key API_KEY] [--raw] <query>",
  277. help="Search for template results using custom query",
  278. epilog=deindent("""
  279. Query syntax:
  280. query = comparison comparison...
  281. comparison = field op value
  282. field = <name of a field>
  283. op = one of: <, <=, ==, !=, >=, >, in, notin
  284. value = <bool, int, float, string> | 'any' | [value0, value1, ...]
  285. bool: True, False
  286. note: to pass '>' and '<' on the command line, make sure to use quotes
  287. note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
  288. Examples:
  289. # search for somewhat reliable single RTX 3090 instances, filter out any duplicates or offers that conflict with our existing stopped instances
  290. vastai search templates 'count_created > 100 creator_id in [38382,48982]'
  291. Available fields:
  292. Name Type Description
  293. creator_id int ID of creator
  294. created_at float time of initial template creation (UTC epoch timestamp)
  295. count_created int #instances created (popularity)
  296. default_tag string image default tag
  297. docker_login_repo string image docker repository
  298. id int template unique ID
  299. image string image used for template
  300. jup_direct bool supports jupyter direct
  301. hash_id string unique hash ID of template
  302. name string displayable name
  303. recent_create_date float last time of instance creation (UTC epoch timestamp)
  304. recommended_disk_space float min disk space required
  305. recommended bool is templated on our recommended list
  306. ssh_direct bool supports ssh direct
  307. tag string image tag
  308. use_ssh bool supports ssh (direct or proxy) """),
  309. aliases=hidden_aliases(["search templates"]),
  310. )
  311. def search__templates(args):
  312. """Creates a query based on search parameters as in the examples above.
  313. :param argparse.Namespace args: should supply all the command-line options
  314. """
  315. from vastai.api.query import parse_query, templates_fields, fix_date_fields
  316. try:
  317. query = {}
  318. if args.query is not None:
  319. query = parse_query(args.query, query, templates_fields)
  320. query = fix_date_fields(query, ['created_at', 'recent_create_date'])
  321. except ValueError as e:
  322. print("Error: ", e)
  323. return 1
  324. client = get_client(args)
  325. try:
  326. rows = offers_api.search_templates(client, query=query)
  327. print(json.dumps(rows, indent=1, sort_keys=True))
  328. except Exception as e:
  329. print(f"Error: {e}")
  330. # ---------------------------------------------------------------------------
  331. # search invoices
  332. # ---------------------------------------------------------------------------
  333. @parser.command(
  334. argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
  335. usage="vastai search invoices [--help] [--api-key API_KEY] [--raw] <query>",
  336. help="Search for invoices using custom query",
  337. epilog=deindent("""
  338. Query syntax:
  339. query = comparison comparison...
  340. comparison = field op value
  341. field = <name of a field>
  342. op = one of: <, <=, ==, !=, >=, >, in, notin
  343. value = <bool, int, float, string> | 'any' | [value0, value1, ...]
  344. bool: True, False
  345. note: to pass '>' and '<' on the command line, make sure to use quotes
  346. note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
  347. Examples:
  348. # search for somewhat reliable single RTX 3090 instances, filter out any duplicates or offers that conflict with our existing stopped instances
  349. vastai search invoices 'amount_cents>3000 '
  350. Available fields:
  351. Name Type Description
  352. id int,
  353. user_id int,
  354. when float, utc epoch timestamp of initial invoice creation
  355. paid_on float, actual payment date (utc epoch timestamp )
  356. payment_expected float, expected payment date (utc epoch timestamp )
  357. amount_cents int, amount of payment in cents
  358. is_credit bool, is a credit purchase
  359. is_delayed bool, is not yet paid
  360. balance_before float, balance before
  361. balance_after float, balance after
  362. original_amount int, original amount of payment
  363. event_id string,
  364. cut_amount int,
  365. cut_percent float,
  366. extra json,
  367. service string, type of payment
  368. stripe_charge json,
  369. stripe_refund json,
  370. stripe_payout json,
  371. error json,
  372. paypal_email string, email for paypal/wise payments
  373. transfer_group string,
  374. failed bool,
  375. refunded bool,
  376. is_check bool,
  377. """),
  378. aliases=hidden_aliases(["search invoices"]),
  379. )
  380. def search__invoices(args):
  381. """Search for invoices using custom query."""
  382. from vastai.api.query import parse_query, invoices_fields, fix_date_fields
  383. try:
  384. query = {}
  385. if args.query is not None:
  386. query = parse_query(args.query, query, invoices_fields)
  387. query = fix_date_fields(query, ['when', 'paid_on', 'payment_expected'])
  388. except ValueError as e:
  389. print("Error: ", e)
  390. return 1
  391. client = get_client(args)
  392. rows = offers_api.search_invoices(client, query=query)
  393. if args.raw:
  394. return rows
  395. print(json.dumps(rows, indent=1, sort_keys=True))
  396. # ---------------------------------------------------------------------------
  397. # create / update / delete template
  398. # ---------------------------------------------------------------------------
  399. @parser.command(
  400. argument("--name", help="name of the template", type=str),
  401. argument("--image", help="docker container image to launch", type=str),
  402. argument("--image_tag", help="docker image tag", type=str),
  403. argument("--href", help="link you want to provide", type=str),
  404. argument("--repo", help="link to repository", type=str),
  405. argument("--login", help="docker login arguments for private repo authentication, surround with ''", type=str),
  406. argument("--env", help="Contents of the 'Docker options' field", type=str),
  407. argument("--ssh", help="Launch as an ssh instance type", action="store_true"),
  408. argument("--jupyter", help="Launch as a jupyter instance instead of an ssh instance", action="store_true"),
  409. argument("--direct", help="Use (faster) direct connections for jupyter & ssh", action="store_true"),
  410. argument("--jupyter-dir", help="For runtype 'jupyter', directory in instance to use to launch jupyter", type=str),
  411. argument("--jupyter-lab", help="For runtype 'jupyter', Launch instance with jupyter lab", action="store_true"),
  412. argument("--onstart-cmd", help="contents of onstart script as single argument", type=str),
  413. argument("--search_params", help="search offers filters", type=str),
  414. argument("-n", "--no-default", action="store_true", help="Disable default search param query args"),
  415. argument("--disk_space", help="disk storage space, in GB", type=str),
  416. argument("--readme", help="readme string", type=str),
  417. argument("--hide-readme", help="hide the readme from users", action="store_true"),
  418. argument("--desc", help="description string", type=str),
  419. argument("--public", help="make template available to public", action="store_true"),
  420. usage="vastai create template",
  421. help="Create a new template",
  422. )
  423. def create__template(args):
  424. """Create a new template."""
  425. fields = offers_api.template_fields_from_flags(
  426. ssh=args.ssh, jupyter=args.jupyter, direct=args.direct,
  427. jupyter_lab=args.jupyter_lab, login=args.login,
  428. hide_readme=args.hide_readme, public=args.public,
  429. search_params=args.search_params, no_default=args.no_default,
  430. always_default=True,
  431. )
  432. extra_filters = fields["extra_filters"]
  433. if args.explain:
  434. print("request json: ")
  435. print({"name": args.name, "image": args.image, "extra_filters": extra_filters})
  436. client = get_client(args)
  437. try:
  438. rj = offers_api.create_template(
  439. client, name=args.name, image=args.image, image_tag=args.image_tag,
  440. href=args.href, repo=args.repo, env=args.env, onstart_cmd=args.onstart_cmd,
  441. jupyter_dir=args.jupyter_dir, disk_space=args.disk_space,
  442. readme=args.readme, desc=args.desc, **fields,
  443. )
  444. if rj.get("success"):
  445. print(f"New Template: {rj['template']}")
  446. else:
  447. print(rj.get('msg', rj))
  448. except Exception:
  449. print("The response is not valid JSON.")
  450. @parser.command(
  451. argument("HASH_ID", help="hash id of the template", type=str),
  452. argument("--name", help="name of the template", type=str),
  453. argument("--image", help="docker container image to launch", type=str),
  454. argument("--image_tag", help="docker image tag", type=str),
  455. argument("--href", help="link you want to provide", type=str),
  456. argument("--repo", help="link to repository", type=str),
  457. argument("--login", help="docker login arguments for private repo authentication, surround with ''", type=str),
  458. argument("--env", help="Contents of the 'Docker options' field", type=str),
  459. argument("--ssh", help="Launch as an ssh instance type", action="store_true"),
  460. argument("--jupyter", help="Launch as a jupyter instance instead of an ssh instance", action="store_true"),
  461. argument("--direct", help="Use (faster) direct connections for jupyter & ssh", action="store_true"),
  462. argument("--jupyter-dir", help="For runtype 'jupyter', directory in instance to use to launch jupyter", type=str),
  463. argument("--jupyter-lab", help="For runtype 'jupyter', Launch instance with jupyter lab", action="store_true"),
  464. argument("--onstart-cmd", help="contents of onstart script as single argument", type=str),
  465. argument("--search_params", help="search offers filters", type=str),
  466. argument("-n", "--no-default", action="store_true", help="Disable default search param query args"),
  467. argument("--disk_space", help="disk storage space, in GB", type=str),
  468. argument("--readme", help="readme string", type=str),
  469. argument("--hide-readme", help="hide the readme from users", action="store_true"),
  470. argument("--desc", help="description string", type=str),
  471. argument("--public", help="make template available to public", action="store_true"),
  472. usage="vastai update template HASH_ID",
  473. help="Update an existing template",
  474. epilog=deindent("""
  475. Edits create a new version of the template with a new hash id; the original stays
  476. valid for anyone already using it. Send the full template, not only the changed fields.
  477. Examples:
  478. vastai update template c81e7ab0e928a508510d1979346de10d --name "tgi-llama2-7B-quantized" --image "ghcr.io/huggingface/text-generation-inference:1.0.3" --env "-p 3000:3000 -e MODEL_ARGS='--model-id TheBloke/Llama-2-7B-chat-GPTQ --quantize gptq'" --onstart-cmd 'wget -O - https://raw.githubusercontent.com/vast-ai/vast-pyworker/main/scripts/launch_tgi.sh | bash' --search_params "gpu_ram>=23 num_gpus=1 gpu_name=RTX_3090 inet_down>128 direct_port_count>3 disk_space>=192 driver_version>=535086005 rented=False" --disk_space 8.0 --ssh --direct
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 560 on GitHub
  479. """),
  480. )
  481. def update__template(args):
  482. """Update an existing template."""
  483. fields = offers_api.template_fields_from_flags(
  484. ssh=args.ssh, jupyter=args.jupyter, direct=args.direct,
  485. jupyter_lab=args.jupyter_lab, login=args.login,
  486. hide_readme=args.hide_readme, public=args.public,
  487. search_params=args.search_params, no_default=args.no_default,
  488. always_default=True,
  489. )
  490. extra_filters = fields["extra_filters"]
  491. if args.explain:
  492. print("request json: ")
  493. print({"hash_id": args.HASH_ID, "name": args.name, "image": args.image})
  494. client = get_client(args)
  495. try:
  496. rj = offers_api.update_template(
  497. client, hash_id=args.HASH_ID, name=args.name, image=args.image,
  498. image_tag=args.image_tag, href=args.href, repo=args.repo, env=args.env,
  499. onstart_cmd=args.onstart_cmd, jupyter_dir=args.jupyter_dir,
  500. disk_space=args.disk_space, readme=args.readme, desc=args.desc,
  501. **fields,
  502. )
  503. if rj.get("success"):
  504. print(f"updated template: {json.dumps(rj['template'], indent=1)}")
  505. else:
  506. print(rj.get("msg", "template update failed"))
  507. return 1
  508. except Exception as e:
  509. print(_response_msg(getattr(e, "response", None)) or str(e))
  510. return 1
  511. def _response_msg(response):
  512. """Return the API's msg from an error response body, or None when there is none."""
  513. if response is None:
  514. return None
  515. try:
  516. body = response.json()
  517. except ValueError:
  518. return None
  519. return body.get("msg") if isinstance(body, dict) else None
  520. @parser.command(
  521. argument("--template-id", help="Template ID of Template to Delete", type=int),
  522. argument("--hash-id", help="Hash ID of Template to Delete", type=str),
  523. usage="vastai delete template [--template-id <id> | --hash-id <hash_id>]",
  524. help="Delete a Template",
  525. )
  526. def delete__template(args):
  527. """Delete a template."""
  528. if not args.hash_id and not args.template_id:
  529. print('ERROR: Must Specify either Template ID or Hash ID to delete a template')
  530. return
  531. if args.explain:
  532. print("request json: ")
  533. print({"hash_id": args.hash_id, "template_id": args.template_id})
  534. client = get_client(args)
  535. try:
  536. rj = offers_api.delete_template(client, hash_id=args.hash_id, template_id=args.template_id)
  537. print(rj.get('msg', rj))
  538. except Exception as e:
  539. print(f"Error: {e}")
vastai/cli/commands/update.py · Bundled script · 77 lines · 1 finding marked
  1. """`vastai update` — self-update for managed (curl | bash installer) installs.
  2. pip installs aren't managed by the installer; for those this command only
  3. prints the correct pip upgrade instructions and exits non-zero — it never
  4. shells out to pip, since the CLI cannot know which pip/venv owns its
  5. environment.
  6. """
  7. import sys
  8. from vastai.cli.parser import argument
  9. from vastai.cli.display import deindent
  10. from vastai.cli.util import VERSION
  11. from vastai.cli.selfupdate import (
  12. INSTALL_SH_HINT, PIP_UPGRADE_HINT, UpdateError,
  13. fetch_manifest, is_managed_install, is_newer, perform_update,
  14. )
  15. from vastai.cli.utils import get_parser as _get_parser
  16. parser = _get_parser()
  17. EXIT_STALE = 10 # `update --check` exit code when a newer version exists
  18. @parser.command(
  19. argument("--check", action="store_true", help="only check for a newer version; exit 0 if current, 10 if an update is available"),
  20. argument("--version", dest="target_version", metavar="VERSION", help="install a specific version instead of the latest (also how you pin or roll back)"),
  21. usage="vastai update [--check | --version VERSION]",
  22. help="Update the CLI to the latest version",
  23. epilog=deindent("""
  24. Updates a CLI installed with the managed installer
  25. (curl -fsSL https://vast.ai/install.sh | bash). The new version is
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 32 on GitHub
  26. installed into a fresh environment and verified before it is swapped
  27. in, so an interrupted update never leaves a broken CLI. To roll back a
  28. bad release, pin the previous one: vastai update --version 1.2.3
  29. For pip installs, update with: pip install --upgrade vastai
  30. """),
  31. )
  32. def update(args):
  33. try:
  34. if args.check:
  35. latest = fetch_manifest()["latest"]
  36. if is_newer(latest, VERSION):
  37. print(f"Update available: {latest} (you have {VERSION})")
  38. return EXIT_STALE
  39. print(f"vastai {VERSION} is up to date (latest: {latest})")
  40. return 0
  41. if not is_managed_install():
  42. print(
  43. "This CLI was not installed with the managed installer, so it "
  44. f"cannot self-update.\n Installed via pip? Run: {PIP_UPGRADE_HINT}\n"
  45. f" Or switch to the managed install: {INSTALL_SH_HINT}",
  46. file=sys.stderr,
  47. )
  48. return 1
  49. manifest = fetch_manifest()
  50. target = args.target_version or manifest["latest"]
  51. current = VERSION
  52. if target == current:
  53. latest = manifest["latest"]
  54. if args.target_version:
  55. print(f"vastai {current} is already installed (latest available: {latest}).")
  56. else:
  57. print(f"vastai {current} is up to date (latest: {latest}).")
  58. return 0
  59. print(f"Updating vastai {current} -> {target} ...")
  60. perform_update(target, manifest)
  61. print(f"Done. vastai is now {target}.")
  62. return 0
  63. except UpdateError as e:
  64. print(str(e), file=sys.stderr)
  65. if not args.check:
  66. print("The current install was left untouched.", file=sys.stderr)
  67. return 1
vastai/cli/selfupdate.py · Bundled script · 364 lines · 1 finding marked
  1. """Self-update engine for the managed (curl | bash) CLI install.
  2. Layout (docs/install-design.md, XDG Base Directory Specification): the
  3. install root defaults to $XDG_DATA_HOME/vastai (~/.local/share/vastai),
  4. override with $VASTAI_INSTALL_DIR — a single active venv at current/, with
  5. bin/vastai a fixed symlink into it. Updating rebuilds current/ in place
  6. (build temp → verify → swap); there is no version retention.
  7. "Managed install?" is detected structurally (``is_managed_install``): a managed
  8. CLI runs from <root>/current with a sibling <root>/bin/uv. That's ground
  9. truth — no on-disk marker to drift or hand-copy. pip installs fail the check
  10. and the updater never touches them.
  11. The passive nudge is throttled to one manifest GET and one notice per week,
  12. capped at 1s, and swallows every failure — the CLI must never get slower or
  13. noisier because the manifest endpoint is unreachable.
  14. """
  15. import json
  16. import os
  17. import re
  18. import shutil
  19. import subprocess
  20. import sys
  21. import time
  22. from pathlib import Path
  23. import requests
  24. from vastai.cli.util import DATA_HOME, DIRS, VERSION
  25. DEFAULT_MANIFEST_URL = "https://vast.ai/cli/manifest.json"
  26. INSTALL_SH_HINT = "curl -fsSL https://vast.ai/install.sh | bash"
    • Note: Pipes a downloaded script directly into a shell (curl | sh / wget | bash)
    • Line 33 on GitHub
  27. PIP_UPGRADE_HINT = "pip install --upgrade vastai"
  28. # Oldest version whose bundled selfupdate.py understands this layout
  29. # (<root>/current + <root>/bin/uv, XDG-based root). Versions before this pin
  30. # assumed ~/.vastai/env and would install fine but then see themselves as unmanaged,
  31. # permanently losing self-update until the install is rebuilt from scratch.
  32. MIN_DOWNGRADE_VERSION = "1.4.0"
  33. UPDATE_CHECK_FILE = os.path.join(DIRS['state'], "update_check.json")
  34. CHECK_INTERVAL_S = 7 * 24 * 60 * 60
  35. NUDGE_TIMEOUT_S = 1.0
  36. # Console scripts shipped in the wheel; each gets a swapped symlink in bin/.
  37. MANAGED_BINARIES = ("vastai", "serve-vast-deployment", "register-python-argcomplete")
  38. # Fixed, not XDG-overridable — every other XDG-aware CLI lands its symlink here too.
  39. LOCAL_BIN = Path.home() / ".local" / "bin"
  40. class UpdateError(Exception):
  41. """A self-update step failed; the current install is untouched."""
  42. class UninstallError(Exception):
  43. """A step of vastai uninstall failed; nothing further was attempted."""
  44. # ---------------------------------------------------------------------------
  45. # Install detection / manifest / versions
  46. # ---------------------------------------------------------------------------
  47. def install_root() -> Path:
  48. """Where the managed install lives.
  49. Prefers ground truth from where this interpreter is actually running: if
  50. we're already inside a managed install (``<root>/current`` next to
  51. ``<root>/bin/uv``), that's authoritative and immune to
  52. $VASTAI_INSTALL_DIR/$XDG_DATA_HOME differing between install time and
  53. now — e.g. a custom install dir that isn't set in the current shell would
  54. otherwise make a real managed install look unmanaged. Only when we're
  55. *not* currently running from one (a pip install checking
  56. ``is_managed_install``) do we fall back to computing where a managed
  57. install would live.
  58. """
  59. prefix = Path(sys.prefix).resolve()
  60. if prefix.name == "current" and (prefix.parent / "bin" / "uv").exists():
  61. return prefix.parent
  62. if os.environ.get("VASTAI_INSTALL_DIR"):
  63. return Path(os.environ["VASTAI_INSTALL_DIR"]).expanduser()
  64. return Path(DATA_HOME) / "vastai"
  65. def is_managed_install() -> bool:
  66. """True iff this CLI was installed by the managed installer.
  67. Detected from where the interpreter actually runs: a managed CLI lives in
  68. ``<root>/current`` next to ``<root>/bin/uv``. Ground truth, no marker file.
  69. pip installs run from elsewhere and fail the check.
  70. """
  71. try:
  72. root = install_root()
  73. return (
  74. Path(sys.prefix).resolve() == (root / "current").resolve()
  75. and (root / "bin" / "uv").exists()
  76. )
  77. except Exception:
  78. return False
  79. def fetch_manifest(timeout: float = 10.0) -> dict:
  80. url = os.environ.get("VASTAI_MANIFEST_URL") or DEFAULT_MANIFEST_URL
  81. try:
  82. r = requests.get(url, timeout=timeout)
  83. r.raise_for_status()
  84. data = r.json()
  85. except (requests.exceptions.RequestException, ValueError) as e:
  86. raise UpdateError(f"Could not fetch release manifest from {url}: {e}")
  87. if not isinstance(data, dict) or "latest" not in data:
  88. raise UpdateError(f"Malformed release manifest at {url}")
  89. if data.get("schema") != 1:
  90. raise UpdateError(
  91. f"Release manifest schema {data.get('schema')!r} is not supported "
  92. f"by this CLI version.\nRe-run the installer: {INSTALL_SH_HINT}"
  93. )
  94. return data
  95. def version_key(v: str):
  96. """Sort key tolerant of dev/rc (below release), post (above), +local (ignored)."""
  97. v = (v or "").strip().lstrip("v").split("+", 1)[0]
  98. nums, extra = [], 0
  99. for part in v.split("."):
  100. if part.isdigit():
  101. nums.append(int(part))
  102. continue
  103. m = re.match(r"\d+", part)
  104. if m:
  105. nums.append(int(m.group()))
  106. extra = 1 if part.startswith("post") else -1
  107. break
  108. while len(nums) < 3:
  109. nums.append(0)
  110. return tuple(nums), extra
  111. def is_newer(candidate: str, current: str) -> bool:
  112. return version_key(candidate) > version_key(current)
  113. # ---------------------------------------------------------------------------
  114. # Passive nudge
  115. # ---------------------------------------------------------------------------
  116. def _load_check_state() -> dict:
  117. try:
  118. with open(UPDATE_CHECK_FILE) as f:
  119. data = json.load(f)
  120. return data if isinstance(data, dict) else {}
  121. except (OSError, ValueError):
  122. return {}
  123. def _save_check_state(state: dict) -> None:
  124. try:
  125. with open(UPDATE_CHECK_FILE, "w") as f:
  126. json.dump(state, f)
  127. except OSError:
  128. pass
  129. def _stderr_is_tty() -> bool:
  130. try:
  131. return sys.stderr.isatty()
  132. except Exception:
  133. return False
  134. def notify_update(args=None) -> None:
  135. """Pre-command, best-effort upgrade nudge. Must never raise or block."""
  136. try:
  137. _notify_update(args)
  138. except Exception:
  139. pass
  140. def _notify_update(args) -> None:
  141. suppressed = (
  142. os.environ.get("VASTAI_NO_UPDATE_CHECK")
  143. or os.environ.get("CI")
  144. or getattr(args, "raw", False)
  145. or not _stderr_is_tty()
  146. # the updater itself doesn't need a nudge
  147. or getattr(getattr(args, "func", None), "__module__", "") == "vastai.cli.commands.update"
  148. )
  149. if suppressed:
  150. return
  151. now = time.time()
  152. state = _load_check_state()
  153. if now - state.get("checked_at", 0) > CHECK_INTERVAL_S:
  154. state["checked_at"] = now # stamped even on failure, so errors back off too
  155. try:
  156. state["latest"] = fetch_manifest(timeout=NUDGE_TIMEOUT_S).get("latest")
  157. except UpdateError:
  158. pass
  159. _save_check_state(state)
  160. latest = state.get("latest")
  161. if not latest or not is_newer(latest, VERSION):
  162. return
  163. if now - state.get("notified_at", 0) <= CHECK_INTERVAL_S:
  164. return
  165. hint = "vastai update" if is_managed_install() else PIP_UPGRADE_HINT
  166. arrow = "↑" if "utf" in (sys.stderr.encoding or "").lower() else "*"
  167. # Trailing blank line separates the nudge from the command output that follows it.
  168. print(
  169. f"{arrow} vastai {latest} is available (you have {VERSION}). Run `{hint}`.\n",
  170. file=sys.stderr,
  171. )
  172. state["notified_at"] = now
  173. _save_check_state(state)
  174. # ---------------------------------------------------------------------------
  175. # Update / rollback (managed installs only)
  176. # ---------------------------------------------------------------------------
  177. def _run(cmd, *, env=None):
  178. cmd = [str(c) for c in cmd]
  179. try:
  180. return subprocess.run(cmd, env=env, check=True, capture_output=True, text=True)
  181. except subprocess.CalledProcessError as e:
  182. # Surface the tool's own stderr — that's the real error (e.g. uv's
  183. # resolution failure), not a category we invent.
  184. detail = (e.stderr or e.stdout or "").strip()
  185. raise UpdateError(f"Command failed: {' '.join(cmd)}" + (f"\n{detail}" if detail else ""))
  186. except OSError as e:
  187. # Carry the actual OSError through (wrong path, missing binary, …)
  188. # rather than relabeling everything "Required tool not found".
  189. raise UpdateError(f"Could not run {' '.join(cmd)}: {e}")
  190. def _prune_uv_cache(uv: Path, env: dict) -> None:
  191. """Best-effort prune of uv's cache after a successful swap.
  192. Each update hardlinks the new env out of the cache, so the cache
  193. accumulates every version's wheels forever unless pruned. Prune (not
  194. clean): the cache is the user-global one, shared with any other uv on
  195. the machine. The update already succeeded — never fail because of this.
  196. """
  197. try:
  198. _run([uv, "cache", "prune", "--quiet"], env=env)
  199. except UpdateError:
  200. pass
  201. def _link_binaries(root: Path) -> None:
  202. """Point bin/<name> at the fixed current/bin/<name> for each shipped
  203. console script. The target path is constant across updates (always
  204. ``current/``), so this is not symlink *retargeting* — just (re)creating
  205. stable links."""
  206. for name in MANAGED_BINARIES:
  207. if not (root / "current" / "bin" / name).exists():
  208. continue
  209. link, tmp = root / "bin" / name, root / "bin" / f".{name}.tmp"
  210. if tmp.is_symlink() or tmp.exists():
  211. tmp.unlink()
  212. os.symlink(Path("..") / "current" / "bin" / name, tmp)
  213. os.replace(tmp, link)
  214. def wheel_spec(target: str, manifest: dict) -> str:
  215. """Requirement spec that installs ``target``.
  216. Latest installs the manifest's release wheel: hash-pinned bytes straight
  217. from the GitHub Release, no PyPI propagation window. Other targets are
  218. pins/rollbacks to long-published versions, where the PyPI pin is
  219. race-free and covers releases that predate wheel_url.
  220. """
  221. install = manifest.get("install") or {}
  222. url, sha = install.get("wheel_url"), install.get("wheel_sha256")
  223. if target == manifest.get("latest") and url and sha:
  224. return f"vastai @ {url}#sha256={sha}"
  225. return f"vastai=={target}"
  226. def perform_update(target: str, manifest: dict) -> None:
  227. """Install ``target`` into a fresh env, verify it, then swap it in.
  228. Single active install (the deno model): the new env is built in a temp dir
  229. and only swapped over the live one after it verifies, so an interrupted
  230. update can never leave a half-written or broken ``vastai``. There is no
  231. version retention — a re-pin or rollback is just ``update --version X``,
  232. which reinstalls. Wheel integrity comes from ``wheel_spec``; we
  233. additionally confirm the installed version matches ``target``.
  234. """
  235. if version_key(target) < version_key(MIN_DOWNGRADE_VERSION):
  236. raise UpdateError(
  237. f"Can't roll back to {target}: versions before {MIN_DOWNGRADE_VERSION} "
  238. "don't recognize this install's layout and would lose the ability "
  239. f"to self-update. The oldest version you can roll back to is {MIN_DOWNGRADE_VERSION}."
  240. )
  241. root = install_root()
  242. uv = root / "bin" / "uv"
  243. if not uv.exists():
  244. raise UpdateError(f"Bootstrap tool missing at {uv}.\nRe-run the installer: {INSTALL_SH_HINT}")
  245. python_pin = (manifest.get("install") or {}).get("python") or "3.12"
  246. env_dir, new_dir, old_dir = root / "current", root / ".current.new", root / ".current.old"
  247. shutil.rmtree(new_dir, ignore_errors=True)
  248. uv_env = dict(os.environ,
  249. UV_PYTHON_INSTALL_DIR=str(root / "python"),
  250. UV_PYTHON_PREFERENCE="only-managed")
  251. try:
  252. # --relocatable: shebangs must not hardcode the build path (env is renamed into place).
  253. _run([uv, "venv", new_dir, "--python", python_pin, "--relocatable", "--quiet"], env=uv_env)
  254. _run([uv, "pip", "install", "--python", new_dir / "bin" / "python",
  255. "--quiet", wheel_spec(target, manifest)], env=uv_env)
  256. installed = (_run([new_dir / "bin" / "vastai", "--version"]).stdout or "").strip()
  257. if target not in installed:
  258. raise UpdateError(
  259. f"Verification failed: new install reports version {installed!r}, expected {target}"
  260. )
  261. except UpdateError:
  262. shutil.rmtree(new_dir, ignore_errors=True)
  263. raise
  264. # Swap via two renames (~instant); a crash mid-build only dirties .current.new.
  265. shutil.rmtree(old_dir, ignore_errors=True)
  266. if env_dir.exists():
  267. os.replace(env_dir, old_dir)
  268. os.replace(new_dir, env_dir)
  269. _link_binaries(root)
  270. shutil.rmtree(old_dir, ignore_errors=True)
  271. _prune_uv_cache(uv, uv_env)
  272. # ---------------------------------------------------------------------------
  273. # Uninstall (managed installs only)
  274. # ---------------------------------------------------------------------------
  275. def perform_uninstall() -> Path:
  276. """Remove the managed install: the install root and its bin/ symlinks.
  277. Config, cache, and state (``vastai.cli.util.DIRS`` — each independently
  278. XDG-overridable) are left alone — the same guarantee documented for a
  279. manual ``rm -rf`` (docs/install-design.md §3). Safe to call while running
  280. from inside ``root``: like the update swap above, removing the directory
  281. doesn't disturb an already-running interpreter on POSIX.
  282. Raises ``UninstallError`` on any real failure (permissions, a symlink
  283. loop, …) instead of silently reporting success — callers should treat
  284. this as fallible, not a fire-and-forget best-effort cleanup.
  285. """
  286. root = install_root().resolve()
  287. for name in MANAGED_BINARIES:
  288. link = LOCAL_BIN / name
  289. try:
  290. if link.is_symlink() and root in link.resolve().parents:
  291. link.unlink()
  292. except OSError as e:
  293. raise UninstallError(f"Could not remove {link}: {e}")
  294. try:
  295. shutil.rmtree(root)
  296. except FileNotFoundError:
  297. pass # already gone — nothing left to remove
  298. except OSError as e:
  299. raise UninstallError(f"Could not remove {root}: {e}")
  300. return root

Ratings and comments

Sign in to rate this skill.

Sign in

Free to use. If it’s helped, pass it on.