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.
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
Markdown
[](/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
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)
- # Vast.ai Python SDK & CLI
- [](https://badge.fury.io/py/vastai)
- Provides both the CLI and SDK for managing Vast.ai GPU cloud resources, plus a serverless client for endpoint inference.
- ## Install
- **Linux, macOS, or WSL** — install the CLI with no Python required:
- ```bash
- 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
- ```
- Update anytime with `vastai update`.
- **Windows, or using the Python SDK** — install from PyPI instead:
- ```bash
- pip install vastai
- ```
- ## Quickstart
- 1. Get your API key from [https://cloud.vast.ai/manage-keys/](https://cloud.vast.ai/manage-keys/)
- 2. Set your API key:
- ```bash
- vastai set api-key YOUR_API_KEY
- ```
- 3. Test a search:
- ```bash
- vastai search offers --limit 3
- ```
- You should see a short list of available GPU offers.
- ## CLI Usage
- The `vastai` command provides full access to the Vast.ai platform from your terminal:
- ```bash
- vastai search offers 'gpu_name=RTX_4090 num_gpus>=4'
- vastai create instance 12345 --image pytorch/pytorch --disk 32 --ssh --direct
- vastai show instances
- vastai stop instance 12345
- vastai destroy instance 12345
- ```
- Run `vastai --help` for a full list of commands. You can also use `--help` on any subcommand:
- ```bash
- vastai search offers --help
- vastai create instance --help
- ```
- ## SDK Usage
- ```python
- from vastai import VastAI
- vast = VastAI() # uses VAST_API_KEY env var, or pass api_key="..."
- vast.search_offers(query='gpu_name=RTX_4090 num_gpus>=4')
- vast.show_instances()
- vast.start_instance(id=12345)
- vast.stop_instance(id=12345)
- ```
- Use `help(vast.search_offers)` to view documentation for any method.
- ## Using the Serverless Client
- 1. Create the client
- ```python
- from vastai import Serverless
- serverless = Serverless() # or, Serverless("YOUR_API_KEY")
- ```
- 2. Get an endpoint
- ```python
- endpoint = await serverless.get_endpoint("my-endpoint")
- ```
- 3. Make a request
- ```python
- request_body = {
- "model": "Qwen/Qwen3-8B",
- "prompt" : "Who are you?",
- "max_tokens" : 100,
- "temperature" : 0.7
- }
- response = await serverless.request("/v1/completions", request_body)
- ```
- 4. Read the response
- ```python
- text = response["response"]["choices"][0]["text"]
- print(text)
- ```
- Find more examples in the `examples/` directory.
- ## AI Agents
- Vast.ai has a skill for AI coding agents (Claude Code, Cursor, Windsurf, Codex, etc.):
- ```bash
- npx skills add vast-ai/vast-cli
- ```
- 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.
- ## Contributing
- 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
- """CLI commands for billing, invoices, earnings, and user account management."""
- import json
- import sys
- import time
- from copy import deepcopy
- from datetime import datetime, timezone
- from io import StringIO
- from vastai.cli.parser import argument
- from vastai.cli.display import (
- display_table, invoice_fields, user_fields, ipaddr_fields,
- scheduled_jobs_fields, deindent, print_or_page,
- )
- from vastai.api import billing as billing_api
- from vastai.api import auth as auth_api
- from vastai.api import teams as teams_api
- from vastai.cli.utils import get_parser as _get_parser, get_client # noqa: F401
- parser = _get_parser()
- # ---------------------------------------------------------------------------
- # Helper utilities
- # ---------------------------------------------------------------------------
- def convert_timestamp_to_date(unix_timestamp):
- utc_datetime = datetime.fromtimestamp(unix_timestamp, tz=timezone.utc)
- return utc_datetime.strftime("%Y-%m-%d")
- def to_timestamp_(val):
- """Convert a date string or int to a UNIX timestamp."""
- if isinstance(val, int):
- return val
- if isinstance(val, str):
- if val.isdigit():
- return int(val)
- return int(datetime.strptime(val + "+0000", '%Y-%m-%d%z').timestamp())
- raise ValueError("Invalid date format")
- def _sum(X, k):
- y = 0
- for x in X:
- a = float(x.get(k, 0))
- y += a
- return y
- def _select(X, k):
- Y = set()
- for x in X:
- v = x.get(k, None)
- if v is not None:
- Y.add(v)
- return Y
- def filter_invoice_items(args, rows):
- """Filter invoice items by date range and charge/credit type."""
- from datetime import date
- try:
- from dateutil import parser as dateutil_parser
- except ImportError:
- print("\nWARNING: Missing dateutil, can't parse time format")
- end_timestamp = 9999999999
- start_timestamp = 0
- start_date_txt = ""
- end_date_txt = ""
- if args.end_date:
- try:
- end_date = dateutil_parser.parse(str(args.end_date))
- end_date_txt = end_date.isoformat()
- if end_date.tzinfo is None:
- end_date = end_date.replace(tzinfo=timezone.utc)
- end_timestamp = end_date.timestamp()
- except ValueError:
- print("Warning: Invalid end date format! Ignoring end date!")
- if args.start_date:
- try:
- start_date = dateutil_parser.parse(str(args.start_date))
- start_date_txt = start_date.isoformat()
- if start_date.tzinfo is None:
- start_date = start_date.replace(tzinfo=timezone.utc)
- start_timestamp = start_date.timestamp()
- except ValueError:
- print("Warning: Invalid start date format! Ignoring start date!")
- selector_flag = ""
- if args.only_charges:
- type_txt = "Only showing charges."
- selector_flag = "only_charges"
- def type_filter_fn(row):
- return row["type"] == "charge"
- elif args.only_credits:
- type_txt = "Only showing credits."
- selector_flag = "only_credits"
- def type_filter_fn(row):
- return row["type"] == "payment"
- else:
- type_txt = ""
- def type_filter_fn(row):
- return True
- if args.end_date:
- if args.start_date:
- header_text = f'Invoice items after {start_date_txt} and before {end_date_txt}.'
- else:
- header_text = f'Invoice items before {end_date_txt}.'
- elif args.start_date:
- header_text = f'Invoice items after {start_date_txt}.'
- else:
- header_text = " "
- header_text = header_text + " " + type_txt
- rows = list(filter(
- lambda row: end_timestamp >= (row["timestamp"] or 0.0) >= start_timestamp
- and type_filter_fn(row) and float(row["amount"]) != 0,
- rows
- ))
- if start_date_txt:
- start_date_txt = "S:" + start_date_txt
- if end_date_txt:
- end_date_txt = "E:" + end_date_txt
- now = date.today()
- invoice_number = now.year * 12 + now.month - 1
- pdf_filename_fields = list(filter(
- lambda fld: fld != "",
- [str(invoice_number), start_date_txt, end_date_txt, selector_flag]
- ))
- filename = "invoice_" + "-".join(pdf_filename_fields) + ".pdf"
- return {"rows": rows, "header_text": header_text, "pdf_filename": filename}
- # ---------------------------------------------------------------------------
- # Invoices-v1 helpers (rich formatting)
- # ---------------------------------------------------------------------------
- charge_types = ['instance', 'volume', 'serverless', 'i', 'v', 's']
- invoice_types = {
- "transfers": "transfer",
- "stripe": "stripe_payments",
- "bitpay": "bitpay",
- "coinbase": "coinbase",
- "crypto.com": "crypto.com",
- "reserved": "instance_prepay",
- "payout_paypal": "paypal_manual",
- "payout_wise": "wise_manual",
- }
- def format_invoices_charges_results(args, results):
- indices_to_remove = []
- for i, item in enumerate(results):
- item['start'] = convert_timestamp_to_date(item['start']) if item['start'] else None
- item['end'] = convert_timestamp_to_date(item['end']) if item['end'] else None
- if item['amount'] == 0:
- indices_to_remove.append(i)
- elif args.invoices:
- if item['type'] not in {'transfer', 'payout'}:
- item['amount'] *= -1
- item['amount_str'] = f"${item['amount']:.2f}" if item['amount'] > 0 else f"-${abs(item['amount']):.2f}"
- else:
- item['amount'] = f"${item['amount']:.3f}"
- if args.charges:
- if item['type'] in {'instance', 'volume'} and not args.verbose:
- item['items'] = []
- if item['source'] and '-' in item['source']:
- item['type'], item['source'] = item['source'].capitalize().split('-')
- item['items'] = format_invoices_charges_results(args, item['items'])
- for i in reversed(indices_to_remove):
- del results[i]
- return results
- def rich_object_to_string(rich_obj, no_color=True):
- """Render a Rich object (Table or Tree) to a string."""
- from rich.console import Console
- buffer = StringIO()
- console = Console(record=True, file=buffer)
- console.print(rich_obj)
- return console.export_text(clear=True, styles=not no_color)
- def create_charges_tree(results, parent=None, title="Charges Breakdown"):
- """Build and return a Rich Tree from nested charge results."""
- from rich.text import Text
- from rich.tree import Tree
- from rich.panel import Panel
- if parent is None:
- root = Tree(Text(title, style="bold red"))
- create_charges_tree(results, root)
- return Panel(root, style="white on #000000", expand=False)
- top_level = (parent.label.plain == title)
- for item in results:
- end_date = f" -> {item['end']}" if item['start'] != item['end'] else ""
- label = Text.assemble(
- (item["type"], "bold cyan"),
- (f" {item['source']}" if item.get('source') else "", "gold1"), " -> ",
- (f"{item['amount']}", 'bold green1' if top_level else 'green1'),
- (f" -- {item['description']}", "bright_white" if top_level else "dim white"),
- (f" ({item['start']}{end_date})", "bold bright_white" if top_level else "white")
- )
- node = parent.add(label, guide_style="blue3")
- if item.get("items"):
- create_charges_tree(item["items"], node)
- return parent
- def create_rich_table_for_charges(args, results):
- """Build and return a Rich Table from charge results."""
- from rich.table import Table
- from rich.text import Text
- from rich import box
- from rich.padding import Padding
- table = Table(style="white", header_style="bold bright_yellow", box=box.DOUBLE_EDGE, row_styles=["on grey11", "none"])
- table.add_column(Text("Type", justify="center"), style="bold steel_blue1", justify="center")
- table.add_column(Text("ID", justify="center"), style="gold1", justify="center")
- table.add_column(Text("Amount", justify="center"), style="sea_green2", justify="right")
- table.add_column(Text("Start", justify="center"), style="bright_white", justify="center")
- table.add_column(Text("End", justify="center"), style="bright_white", justify="center")
- if not args.charge_type or 'serverless' in args.charge_type:
- table.add_column(Text("Endpoint", justify="center"), style="bright_red", justify="center")
- table.add_column(Text("Workergroup", justify="center"), style="orchid", justify="center")
- for item in results:
- row = [item['type'].capitalize(), item['source'], item['amount'], item['start'], item['end']]
- if not args.charge_type or 'serverless' in args.charge_type:
- row.append(str(item['metadata'].get('endpoint_id', '')))
- row.append(str(item['metadata'].get('workergroup_id', '')))
- table.add_row(*row)
- return Padding(table, (1, 2), style="on #000000", expand=False)
- def create_rich_table_for_invoices(results):
- """Build and return a Rich Table from invoice results."""
- from rich.table import Table
- from rich.text import Text
- from rich import box
- from rich.padding import Padding
- invoice_type_to_color = {
- "credit": "green1",
- "transfer": "gold1",
- "payout": "orchid",
- "reserved": "sky_blue1",
- "refund": "bright_red",
- }
- table = Table(style="white", header_style="bold bright_yellow", box=box.DOUBLE_EDGE, row_styles=["on grey11", "none"])
- table.add_column(Text("ID", justify="center"), style="bright_white", justify="center")
- table.add_column(Text("Created", justify="center"), style="yellow3", justify="center")
- table.add_column(Text("Paid", justify="center"), style="yellow3", justify="center")
- table.add_column(Text("Type", justify="center"), justify="center")
- table.add_column(Text("Result", justify="center"), justify="right")
- table.add_column(Text("Source", justify="center"), style="bright_cyan", justify="center")
- table.add_column(Text("Description", justify="center"), style="bright_white", justify="left")
- for item in results:
- table.add_row(
- str(item['metadata']['invoice_id']),
- item['start'],
- item['end'] if item['end'] else 'N/A',
- Text(item['type'].capitalize(), style=invoice_type_to_color.get(item['type'], "white")),
- Text(item['amount_str'], style="sea_green2" if item['amount'] > 0 else "bright_red"),
- item['source'].capitalize() if item['type'] != 'transfer' else item['source'],
- item['description'],
- )
- return Padding(table, (1, 2), style="on #000000", expand=False)
- # ---------------------------------------------------------------------------
- # show invoices (deprecated)
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("-q", "--quiet", action="store_true", help="only display numeric ids"),
- argument("-s", "--start_date", help="start date and time for report. Many formats accepted (optional)", type=str),
- argument("-e", "--end_date", help="end date and time for report. Many formats accepted (optional)", type=str),
- argument("-c", "--only_charges", action="store_true", help="Show only charge items"),
- argument("-p", "--only_credits", action="store_true", help="Show only credit items"),
- argument("--instance_label", help="Filter charges on a particular instance label (useful for autoscaler groups)"),
- usage="(DEPRECATED) vastai show invoices [OPTIONS]",
- help="(DEPRECATED) Get billing history reports. Use `vastai show invoices-v1` instead.",
- )
- def show__invoices(args):
- """Show current payments and charges (deprecated)."""
- if not args.quiet:
- print("DEPRECATED: `vastai show invoices` will be removed in a future release. "
- "Use `vastai show invoices-v1` for the new paginated command.", file=sys.stderr)
- client = get_client(args)
- result = billing_api.show_invoices(client, start_date=args.start_date, end_date=args.end_date,
- only_charges=args.only_charges, only_credits=args.only_credits)
- rows = result["invoices"]
- current_charges = result["current"]
- invoice_filter_data = filter_invoice_items(args, rows)
- rows = invoice_filter_data["rows"]
- filter_header = invoice_filter_data["header_text"]
- if args.instance_label:
- contract_ids = _select(rows, 'instance_id')
- req_json = {
- "label": args.instance_label,
- "contract_ids": list(contract_ids)
- }
- if args.explain:
- print("request json: ")
- print(req_json)
- result2 = client.post("/contracts/fetch/", json_data=req_json)
- result2.raise_for_status()
- filtered_rows = result2.json()["contracts"]
- contract_ids = _select(filtered_rows, 'id')
- rows = [row for row in rows if row.get("instance_id") in contract_ids]
- if args.quiet:
- for row in rows:
- id = row.get("id", None)
- if id is not None:
- print(id)
- elif args.raw:
- return rows
- else:
- print(filter_header)
- display_table(rows, invoice_fields)
- print(f"Total: ${_sum(rows, 'amount')}")
- print("Current: ", current_charges)
- # ---------------------------------------------------------------------------
- # show invoices-v1 (advanced with pagination and rich formatting)
- # ---------------------------------------------------------------------------
- @parser.command(
- argument('-i', '--invoices', mutex_group='grp', action='store_true', required=True, help='Show invoices instead of charges'),
- argument('-it', '--invoice-type', choices=invoice_types.keys(), nargs='+', metavar='type',
- help=f'Filter which types of invoices to show: {{{", ".join(invoice_types.keys())}}}'),
- argument('-c', '--charges', mutex_group='grp', action='store_true', required=True, help='Show charges instead of invoices'),
- argument('-ct', '--charge-type', choices=charge_types, nargs='+', metavar='type',
- help='Filter which types of charges to show: {i|instance, v|volume, s|serverless}'),
- argument('-s', '--start-date', help='Start date (YYYY-MM-DD or timestamp)'),
- argument('-e', '--end-date', help='End date (YYYY-MM-DD or timestamp)'),
- argument('-l', '--limit', type=int, default=20, help='Number of results per page (default: 20, max: 100)'),
- argument('-t', '--next-token', help='Pagination token for next page'),
- argument('-f', '--format', choices=['table', 'tree'], default='table', help='Output format for charges (default: table)'),
- argument('-v', '--verbose', action='store_true', help='Include full Instance Charge details and Invoice Metadata (tree view only)'),
- argument('--latest-first', action='store_true', help='Sort by latest first'),
- usage="vastai show invoices-v1 [OPTIONS]",
- help="Get billing (invoices/charges) history reports with advanced filtering and pagination",
- epilog=deindent("""
- This command supports colored output and rich formatting if the 'rich' python module is installed!
- Examples:
- # Show the first 20 invoices in the last week (note: default window is a 7 day period ending today)
- vastai show invoices-v1 --invoices
- # Show the first 50 charges over a 7 day period starting from 2025-11-30 in tree format
- vastai show invoices-v1 --charges -s 2025-11-30 -f tree -l 50
- # Show the first 20 invoices of specific types for the month of November 2025
- vastai show invoices-v1 -i -it stripe bitpay transfers --start-date 2025-11-01 --end-date 2025-11-30
- # Show the first 20 charges for only volumes and serverless instances between two dates, including all details and metadata
- vastai show invoices-v1 -c --charge-type v s -s 2025-11-01 -e 2025-11-05 --format tree --verbose
- # Get the next page of paginated invoices, limit to 50 per page (note: type/date filters MUST match previous request for pagination to work)
- 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
- # Show the last 10 instance (only) charges over a 7 day period ending in 2025-12-25, sorted by latest charges first
- vastai show invoices-v1 --charges -ct instance --end-date 2025-12-25 -l 10 --latest-first
- """),
- )
- def show__invoices_v1(args):
- """Get billing (invoices/charges) history with advanced filtering and pagination."""
- output_lines = []
- try:
- from rich.prompt import Confirm
- has_rich = True
- except ImportError:
- output_lines.append("NOTE: To view results in color and table/tree format please install the 'rich' python module with 'pip install rich'\n")
- has_rich = False
- # Handle default start and end date values
- if not args.start_date and not args.end_date:
- args.end_date = int(time.time())
- if not args.start_date:
- args.start_date = args.end_date - 7 * 24 * 60 * 60
- elif not args.end_date:
- args.end_date = args.start_date + 7 * 24 * 60 * 60
- try:
- start_timestamp = to_timestamp_(args.start_date)
- end_timestamp = to_timestamp_(args.end_date)
- except Exception as e:
- print(f"Error parsing dates: {e}")
- print("Use format YYYY-MM-DD or UNIX timestamp")
- return
- if has_rich and not args.no_color and not args.raw:
- print("(use --no-color to disable colored output)\n")
- start_date = convert_timestamp_to_date(start_timestamp)
- end_date = convert_timestamp_to_date(end_timestamp)
- data_type = "Instance Charges" if args.charges else "Invoices"
- output_lines.append(f"Fetching {data_type} from {start_date} to {end_date}...")
- # Build SDK params
- params = {
- "charges": args.charges,
- "start_date": args.start_date,
- "end_date": args.end_date,
- "limit": min(args.limit, 100) if args.limit > 0 else 20,
- "latest_first": args.latest_first,
- "format": args.format if args.charges else "table",
- "charge_type": args.charge_type or [],
- "invoice_type": args.invoice_type or [],
- "next_token": args.next_token,
- }
- client = get_client(args)
- if args.raw:
- # Single-page raw response; dispatcher JSON-prints. Callers paginate
- # by re-running with --next-token.
- return billing_api.show_invoices_v1(client, params)
- found_results, found_count = [], 0
- looping = True
- while looping:
- response = billing_api.show_invoices_v1(client, params)
- found_results += response.get('results', [])
- found_count += response.get('count', 0)
- total = response.get('total', 0)
- next_token = response.get('next_token')
- if has_rich is False:
- output_lines.append("Raw response:\n" + json.dumps(response, indent=2))
- if next_token:
- print(f"Next page token: {next_token}\n")
- elif not found_results:
- output_lines.append("No results found")
- else:
- formatted_results = format_invoices_charges_results(args, deepcopy(found_results))
- if args.invoices:
- rich_obj = create_rich_table_for_invoices(formatted_results)
- elif args.format == 'tree':
- rich_obj = create_charges_tree(formatted_results)
- else:
- rich_obj = create_rich_table_for_charges(args, formatted_results)
- output_lines.append(rich_object_to_string(rich_obj, no_color=args.no_color))
- output_lines.append(f"Showing {found_count} of {total} results")
- if next_token:
- output_lines.append(f"Next page token: {next_token}\n")
- paging = print_or_page(args, '\n'.join(output_lines))
- if next_token and not paging:
- if has_rich:
- ans = Confirm.ask("Fetch next page?", show_default=False, default=False)
- else:
- ans = input("Fetch next page? (y/N): ").strip().lower() == 'y'
- if ans:
- params['next_token'] = next_token
- output_lines.clear()
- args.full = True
- else:
- looping = False
- else:
- looping = False
- # ---------------------------------------------------------------------------
- # show earnings
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("-q", "--quiet", action="store_true", help="only display numeric ids"),
- argument("-s", "--start_date", help="start date and time for report. Many formats accepted", type=str),
- argument("-e", "--end_date", help="end date and time for report. Many formats accepted ", type=str),
- argument("-m", "--machine_id", help="Machine id (optional)", type=int),
- usage="vastai show earnings [OPTIONS]",
- help="Get machine earning history reports",
- )
- def show__earnings(args):
- """Show earnings history for a time range, optionally per machine."""
- client = get_client(args)
- rows = billing_api.show_earnings(client, start_date=args.start_date, end_date=args.end_date,
- machine_id=args.machine_id)
- if args.raw:
- return rows
- print(json.dumps(rows, indent=1, sort_keys=True))
- # ---------------------------------------------------------------------------
- # show deposit
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("id", help="id of instance to get info for", type=int),
- usage="vastai show deposit ID [options]",
- help="Display reserve deposit info for an instance",
- )
- def show__deposit(args):
- """Show reserve deposit info for an instance."""
- client = get_client(args)
- result = billing_api.show_deposit(client, id=args.id)
- print(json.dumps(result, indent=1, sort_keys=True))
- # ---------------------------------------------------------------------------
- # show / set user
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("-q", "--quiet", action="store_true", help="display information about user"),
- usage="vastai show user [OPTIONS]",
- help="Get current user data",
- epilog=deindent("""
- Shows stats for logged-in user. These include user balance, email, and ssh key. Does not show API key.
- """),
- )
- def show__user(args):
- """Show stats for logged-in user."""
- client = get_client(args)
- user_blob = billing_api.show_user(client)
- if args.raw:
- return user_blob
- else:
- display_table([user_blob], user_fields)
- @parser.command(
- argument("--file", help="file path for params in json format", type=str),
- usage="vastai set user --file FILE",
- help="Update user data from json file",
- epilog=deindent("""
- Available fields:
- Name Type Description
- ssh_key string
- paypal_email string
- wise_email string
- email string
- normalized_email string
- username string
- fullname string
- billaddress_line1 string
- billaddress_line2 string
- billaddress_city string
- billaddress_zip string
- billaddress_country string
- billaddress_taxinfo string
- balance_threshold_enabled string
- balance_threshold string
- autobill_threshold string
- phone_number string
- """),
- )
- def set__user(args):
- """Update user data from a json file."""
- params = None
- with open(args.file, 'r') as file:
- params = json.load(file)
- client = get_client(args)
- result = billing_api.set_user(client, params=params)
- print(f"{result}")
- # ---------------------------------------------------------------------------
- # show subaccounts / create subaccount
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("-q", "--quiet", action="store_true", help="display subaccounts from current user"),
- usage="vastai show subaccounts [OPTIONS]",
- help="Get current subaccounts",
- )
- def show__subaccounts(args):
- """Show subaccounts."""
- client = get_client(args)
- rows = billing_api.show_subaccounts(client)
- if args.raw:
- return rows
- else:
- display_table(rows, user_fields)
- @parser.command(
- argument("--email", help="email address to use for login", type=str),
- argument("--username", help="username to use for login", type=str),
- argument("--password", help="password to use for login", type=str),
- argument("--type", help="host/client", type=str),
- usage="vastai create subaccount --email EMAIL --username USERNAME --password PASSWORD --type TYPE",
- help="Create a subaccount",
- epilog=deindent("""
- Creates a new account that is considered a child of your current account as defined via the API key.
- vastai create subaccount --email bob@gmail.com --username bob --password password --type host
- vastai create subaccount --email vast@gmail.com --username vast --password password --type host
- """),
- )
- def create__subaccount(args):
- """Create a subaccount under your current account."""
- host_only = False
- if args.type:
- host_only = args.type.lower() == "host"
- if getattr(args, 'explain', False):
- print("Request JSON would be: ")
- print({
- "email": args.email, "username": args.username,
- "password": args.password, "host_only": host_only, "parent_id": "me",
- })
- return
- client = get_client(args)
- try:
- rj = billing_api.create_subaccount(client, email=args.email, username=args.username,
- password=args.password, host_only=host_only)
- print(rj)
- except Exception as e:
- print(f"Failed with error: {e}")
- # ---------------------------------------------------------------------------
- # show ipaddrs
- # ---------------------------------------------------------------------------
- @parser.command(
- usage="vastai show ipaddrs [--api-key API_KEY] [--raw]",
- help="Display user's history of ip addresses",
- )
- def show__ipaddrs(args):
- """Show the history of ip address accesses."""
- client = get_client(args)
- rows = billing_api.show_ipaddrs(client)
- if args.raw:
- return rows
- else:
- display_table(rows, ipaddr_fields)
- # ---------------------------------------------------------------------------
- # transfer credit
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("recipient_pos", nargs="?", default=None, metavar="RECIPIENT",
- help="email (or id) of recipient account (positional; use --recipient to name)"),
- argument("amount_pos", nargs="?", default=None, type=float, metavar="AMOUNT",
- help="dollars of credit to transfer (positional; use --amount to name)"),
- argument("-r", "--recipient", help="email (or id) of recipient account", type=str, default=None),
- argument("-a", "--amount", help="dollars of credit to transfer", type=float, default=None),
- argument("--skip", help="skip confirmation", action="store_true", default=False),
- usage="vastai transfer credit [--recipient EMAIL] [--amount DOLLARS] [RECIPIENT AMOUNT]",
- help="Transfer credits to another account",
- epilog=deindent("""
- Transfer credits to another account. This action is irreversible.
- Supports both named flags (recommended) and legacy positional arguments:
- vastai transfer credit --recipient user@example.com --amount 25.50
- vastai transfer credit -r user@example.com -a 25.50
- vastai transfer credit user@example.com 25.50
- """),
- )
- def transfer__credit(args):
- """Transfer credits to another account."""
- recipient = args.recipient if args.recipient is not None else args.recipient_pos
- amount = args.amount if args.amount is not None else args.amount_pos
- if recipient is None or amount is None:
- print("Error: both recipient and amount are required (as flags or positional args).")
- return
- if not args.skip:
- print(f"Transfer ${amount} credit to account {recipient}? This is irreversible.")
- ok = input("Continue? [y/n] ")
- if ok.strip().lower() != "y":
- return
- if args.explain:
- print("request json: ")
- print({"sender": "me", "recipient": recipient, "amount": amount})
- client = get_client(args)
- try:
- rj = teams_api.transfer_credit(client, recipient=recipient, amount=amount)
- if rj["success"]:
- print(f"Sent {amount} to {recipient}")
- else:
- print(rj["msg"])
- except Exception as e:
- print(f"Failed with error: {e}")
- # ---------------------------------------------------------------------------
- # scheduled jobs
- # ---------------------------------------------------------------------------
- def normalize_schedule_fields(job):
- """Mutates the job dict to replace None values with readable scheduling labels."""
- if job.get("day_of_the_week") is None:
- job["day_of_the_week"] = "Everyday"
- else:
- days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
- job["day_of_the_week"] = days[int(job["day_of_the_week"])]
- if job.get("hour_of_the_day") is None:
- job["hour_of_the_day"] = "Every hour"
- else:
- hour = int(job["hour_of_the_day"])
- suffix = "AM" if hour < 12 else "PM"
- hour_12 = hour % 12
- hour_12 = 12 if hour_12 == 0 else hour_12
- job["hour_of_the_day"] = f"{hour_12}_{suffix}"
- if job.get("min_of_the_hour") is None:
- job["min_of_the_hour"] = "Every minute"
- else:
- job["min_of_the_hour"] = f"{int(job['min_of_the_hour']):02d}"
- return job
- @parser.command(
- usage="vastai show scheduled-jobs [--api-key API_KEY] [--raw]",
- help="Display the list of scheduled jobs",
- )
- def show__scheduled_jobs(args):
- """Show the list of scheduled jobs for the account."""
- client = get_client(args)
- rows = auth_api.show_scheduled_jobs(client)
- if args.raw:
- return rows
- else:
- rows = [normalize_schedule_fields(job) for job in rows]
- display_table(rows, scheduled_jobs_fields)
- @parser.command(
- argument("id", help="id of scheduled job to remove", type=int),
- usage="vastai delete scheduled-job ID",
- help="Delete a scheduled job",
- )
- def delete__scheduled_job(args):
- """Delete a scheduled job."""
- client = get_client(args)
- result = auth_api.delete_scheduled_job(client, id=args.id)
- print(result)
vastai/cli/commands/offers.py · Bundled script · 629 lines · 1 finding marked
- """CLI commands for searching offers, templates, and benchmarks."""
- import json
- import sys
- import time
- from vastai.cli.parser import argument, hidden_aliases
- from vastai.cli.display import display_table, displayable_fields, displayable_fields_reserved, benchmarks_displayable_fields, deindent
- from vastai.api import offers as offers_api
- from vastai.cli.utils import get_parser as _get_parser, get_client # noqa: F401
- parser = _get_parser()
- # ---------------------------------------------------------------------------
- # search offers
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("-t", "--type", default="on-demand", help="Show 'on-demand', 'reserved', or 'bid'(interruptible) pricing. default: on-demand"),
- argument("-i", "--interruptible", dest="type", const="bid", action="store_const", help="Alias for --type=bid"),
- argument("-b", "--bid", dest="type", const="bid", action="store_const", help="Alias for --type=bid"),
- argument("-r", "--reserved", dest="type", const="reserved", action="store_const", help="Alias for --type=reserved"),
- argument("-d", "--on-demand", dest="type", const="on-demand", action="store_const", help="Alias for --type=on-demand"),
- argument("-n", "--no-default", action="store_true", help="Disable default query"),
- argument("--new", action="store_true", help="New search exp"),
- argument("--limit", type=int, help=""),
- argument("--storage", type=float, default=5.0, help="Amount of storage to use for pricing, in GiB. default=5.0GiB"),
- 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-'),
- 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),
- usage="vastai search offers [--help] [--api-key API_KEY] [--raw] <query>",
- help="Search for instance types using custom query",
- epilog=deindent("""
- Query syntax:
- query = comparison comparison...
- comparison = field op value
- field = <name of a field>
- op = one of: <, <=, ==, !=, >=, >, in, notin
- value = <bool, int, float, string> | 'any' | [value0, value1, ...]
- bool: True, False
- note: to pass '>' and '<' on the command line, make sure to use quotes
- note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
- Examples:
- # search for somewhat reliable single RTX 3090 instances
- vastai search offers 'reliability > 0.98 num_gpus=1 gpu_name=RTX_3090'
- # search for datacenter gpus with minimal compute_cap and total_flops
- vastai search offers 'compute_cap > 610 total_flops > 5 datacenter=True'
- # search for reliable 4 gpu offers in Taiwan or Sweden
- vastai search offers 'reliability>0.99 num_gpus=4 geolocation in [TW,SE]'
- # search for reliable RTX 3090 or 4090 gpus NOT in China or Vietnam
- vastai search offers 'reliability>0.99 gpu_name in ["RTX 4090", "RTX 3090"] geolocation notin [CN,VN]'
- # search for machines with nvidia drivers 535.86.05 or greater (and various other options)
- vastai search offers 'disk_space>146 duration>24 gpu_ram>10 cuda_vers>=12.1 direct_port_count>=2 driver_version >= 535.86.05'
- # search for reliable machines with at least 4 gpus, unverified, order by num_gpus
- vastai search offers 'reliability > 0.99 num_gpus>=4 verified=False' -o 'num_gpus-'
- # search for arm64 cpu architecture
- vastai search offers 'cpu_arch=arm64'
- Available fields:
- Name Type Description
- bw_nvlink float bandwidth NVLink
- compute_cap: int cuda compute capability*100 (ie: 650 for 6.5, 700 for 7.0)
- cpu_arch string host machine cpu architecture (e.g. amd64, arm64)
- cpu_cores: int # virtual cpus
- cpu_ghz: Float # cpu clock speed GHZ
- cpu_cores_effective: float # virtual cpus you get
- cpu_ram: float system RAM in gigabytes
- cuda_vers: float machine max supported cuda version (based on driver version)
- datacenter: bool show only datacenter offers
- direct_port_count int open ports on host's router
- disk_bw: float disk read bandwidth, in MB/s
- disk_space: float disk storage space, in GB
- dlperf: float DL-perf score (see FAQ for explanation)
- dlperf_usd: float DL-perf/$
- dph: float $/hour rental cost
- driver_version: string machine's nvidia/amd driver version as 3 digit string ex. "535.86.05,"
- duration: float max rental duration in days
- external: bool show external offers in addition to datacenter offers
- flops_usd: float TFLOPs/$
- geolocation: string Two letter country code. Works with operators =, !=, in, notin (e.g. geolocation not in ['XV','XZ'])
- gpu_arch string host machine gpu architecture (e.g. nvidia, amd)
- gpu_max_power float GPU power limit (watts)
- gpu_max_temp float GPU temp limit (C)
- gpu_mem_bw: float GPU memory bandwidth in GB/s
- gpu_name: string GPU model name (no quotes, replace spaces with underscores, ie: RTX_3090 rather than 'RTX 3090')
- gpu_ram: float per GPU RAM in GB
- gpu_total_ram: float total GPU RAM in GB
- gpu_frac: float Ratio of GPUs in the offer to gpus in the system
- gpu_display_active: bool True if the GPU has a display attached
- has_avx: bool CPU supports AVX instruction set.
- id: int instance unique ID
- inet_down: float internet download speed in Mb/s
- inet_down_cost: float internet download bandwidth cost in $/GB
- inet_up: float internet upload speed in Mb/s
- inet_up_cost: float internet upload bandwidth cost in $/GB
- machine_id int machine id of instance
- min_bid: float current minimum bid price in $/hr for interruptible
- num_gpus: int # of GPUs
- pci_gen: float PCIE generation
- pcie_bw: float PCIE bandwidth (CPU to GPU)
- reliability: float machine reliability score (see FAQ for explanation)
- rentable: bool is the instance currently rentable
- storage_cost: float storage cost in $/GB/month
- static_ip: bool is the IP addr static/stable
- total_flops: float total TFLOPs from all GPUs
- ubuntu_version string host machine ubuntu OS version
- verified: bool is the machine verified
- vms_enabled: bool is the machine a VM instance
- """),
- aliases=hidden_aliases(["search instances"]),
- )
- def search__offers(args):
- """Creates a query based on search parameters as in the examples above.
- :param argparse.Namespace args: should supply all the command-line options
- """
- from vastai.api.query import parse_query, offers_fields, offers_alias, offers_mult
- try:
- if args.no_default:
- query = {}
- else:
- query = {"verified": {"eq": True}, "external": {"eq": False}, "rentable": {"eq": True}}
- if args.query is not None:
- query = parse_query(args.query, query, offers_fields, offers_alias, offers_mult)
- if not args.no_default:
- # Request every cpu arch like the console (CLN-3461); explicit cpu_arch wins.
- query.setdefault("cpu_arch", {"in": ["amd64", "arm64"]})
- order = []
- for name in args.order.split(","):
- name = name.strip()
- if not name:
- continue
- direction = "asc"
- field = name
- if name.strip("-") != name:
- direction = "desc"
- field = name.strip("-")
- if name.strip("+") != name:
- direction = "asc"
- field = name.strip("+")
- if field in offers_alias:
- field = offers_alias[field]
- order.append([field, direction])
- query["order"] = order
- query["type"] = args.type
- if args.limit:
- query["limit"] = int(args.limit)
- query["allocated_storage"] = args.storage
- if query["type"] == 'interruptible':
- query["type"] = 'bid'
- except ValueError as e:
- print("Error: ", e)
- return 1
- json_blob = query
- client = get_client(args)
- if args.new:
- json_blob = {"select_cols": ['*'], "q": query}
- if args.explain:
- print("request json: ")
- print(json_blob)
- r = client.put("/search/asks/", json_data=json_blob)
- else:
- if args.explain:
- print("request json: ")
- print(json_blob)
- r = client.post("/bundles/", json_data=json_blob)
- r.raise_for_status()
- if r.headers.get('Content-Type') != 'application/json':
- print(f"invalid return Content-Type: {r.headers.get('Content-Type')}")
- return
- rows = r.json()["offers"]
- if args.raw:
- return rows
- else:
- if args.type == "reserved":
- display_table(rows, displayable_fields_reserved)
- else:
- display_table(rows, displayable_fields)
- # ---------------------------------------------------------------------------
- # search benchmarks
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
- argument("-a", "--all", action="store_true", help="force fetching every page, even with --limit/--next-token (fetching everything is already the default otherwise)"),
- argument("-l", "--limit", type=int, default=None, help="max results per page; passing this switches to single-page mode instead of fetching everything"),
- argument("-t", "--next-token", dest="next_token", help="resume from a pagination token (implies single-page mode)"),
- usage="vastai search benchmarks [--help] [--api-key API_KEY] [--raw] [--limit N] [--next-token TOKEN] <query>",
- help="Search for benchmark results using custom query",
- epilog=deindent("""
- By default this fetches every page and prints one combined result. Passing
- --limit or --next-token switches to single-page mode, which prints a page
- and asks whether to fetch the next one (add --all to still fetch every page
- in that mode). The prompt is skipped when output is piped or --raw is used;
- those print the next page token so you can resume with --next-token.
- --raw prints a flat list when fetching every page, and the full response
- (benchmarks plus next_token) in single-page mode.
- Query syntax:
- query = comparison comparison...
- comparison = field op value
- field = <name of a field>
- op = one of: <, <=, ==, !=, >=, >, in, notin
- value = <bool, int, float, string> | 'any' | [value0, value1, ...]
- bool: True, False
- note: to pass '>' and '<' on the command line, make sure to use quotes
- note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
- Examples:
- # search for benchmarks with value > 100 on RTX 4090s for 2 specific machines
- vastai search benchmarks 'value > 100.0 gpu_name=RTX_4090 machine_id in [302,402]'
- # a page at a time, prompting between pages
- vastai search benchmarks 'gpu_name=RTX_5090' --limit 50
- Available fields:
- Name Type Description
- contract_id int ID of instance/contract reporting benchmark
- dph_base float gpu $/hr at the time of the benchmark
- gpu_name string GPU model benchmarked (e.g. RTX_4090)
- id int benchmark unique ID
- image string image used for benchmark
- last_update float date of benchmark
- launch_args string launch args of the benchmarked worker
- machine_id int id of machine benchmarked
- num_gpus int number of gpus used in benchmark
- template_hash string hash of the template benchmarked (formerly 'name')
- template_id int id of the template benchmarked
- type string benchmark type
- value float benchmark result (formerly 'score')
- """),
- aliases=hidden_aliases(["search benchmarks"]),
- )
- def search__benchmarks(args):
- """Creates a query based on search parameters as in the examples above.
- :param argparse.Namespace args: should supply all the command-line options
- """
- from vastai.api.query import parse_query, benchmarks_fields, fix_date_fields
- try:
- query = {}
- if args.query is not None:
- query = parse_query(args.query, query, benchmarks_fields)
- query = fix_date_fields(query, ['last_update'])
- except ValueError as e:
- print("Error: ", e)
- return 1
- client = get_client(args)
- # Fetch every page by default; --limit/--next-token opt into manual pagination.
- fetch_all = args.all or (args.limit is None and args.next_token is None)
- if fetch_all:
- rows = offers_api.search_benchmarks(
- client, query=query, limit=args.limit, after_token=args.next_token)
- if args.raw:
- # Flat list of every matching row -- the contract scripts have
- # always depended on for --raw. Single-page mode returns the
- # envelope instead, since only it has a token worth handing back.
- return rows
- display_table(rows, benchmarks_displayable_fields)
- return
- params = offers_api.benchmarks_query_args(
- query=query, limit=args.limit, after_token=args.next_token)
- page = 0
- while True:
- page += 1
- data = offers_api.search_benchmarks_v1(client, params)
- rows = data.get("benchmarks") or []
- next_token = data.get("next_token")
- if args.raw:
- return {"success": True, "benchmarks_found": len(rows),
- "benchmarks": rows, "next_token": next_token}
- display_table(rows, benchmarks_displayable_fields)
- if not next_token:
- return
- print(f"Next page token: {next_token}")
- # Never block on a prompt when piped or scripted.
- if not (sys.stdin.isatty() and sys.stdout.isatty()):
- return
- try:
- if input(f"Fetch next page? (page {page + 1}) (y/N): ").strip().lower() != "y":
- return
- except (EOFError, KeyboardInterrupt):
- return
- params["after_token"] = next_token
- # ---------------------------------------------------------------------------
- # search templates
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
- usage="vastai search templates [--help] [--api-key API_KEY] [--raw] <query>",
- help="Search for template results using custom query",
- epilog=deindent("""
- Query syntax:
- query = comparison comparison...
- comparison = field op value
- field = <name of a field>
- op = one of: <, <=, ==, !=, >=, >, in, notin
- value = <bool, int, float, string> | 'any' | [value0, value1, ...]
- bool: True, False
- note: to pass '>' and '<' on the command line, make sure to use quotes
- note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
- Examples:
- # search for somewhat reliable single RTX 3090 instances, filter out any duplicates or offers that conflict with our existing stopped instances
- vastai search templates 'count_created > 100 creator_id in [38382,48982]'
- Available fields:
- Name Type Description
- creator_id int ID of creator
- created_at float time of initial template creation (UTC epoch timestamp)
- count_created int #instances created (popularity)
- default_tag string image default tag
- docker_login_repo string image docker repository
- id int template unique ID
- image string image used for template
- jup_direct bool supports jupyter direct
- hash_id string unique hash ID of template
- name string displayable name
- recent_create_date float last time of instance creation (UTC epoch timestamp)
- recommended_disk_space float min disk space required
- recommended bool is templated on our recommended list
- ssh_direct bool supports ssh direct
- tag string image tag
- use_ssh bool supports ssh (direct or proxy) """),
- aliases=hidden_aliases(["search templates"]),
- )
- def search__templates(args):
- """Creates a query based on search parameters as in the examples above.
- :param argparse.Namespace args: should supply all the command-line options
- """
- from vastai.api.query import parse_query, templates_fields, fix_date_fields
- try:
- query = {}
- if args.query is not None:
- query = parse_query(args.query, query, templates_fields)
- query = fix_date_fields(query, ['created_at', 'recent_create_date'])
- except ValueError as e:
- print("Error: ", e)
- return 1
- client = get_client(args)
- try:
- rows = offers_api.search_templates(client, query=query)
- print(json.dumps(rows, indent=1, sort_keys=True))
- except Exception as e:
- print(f"Error: {e}")
- # ---------------------------------------------------------------------------
- # search invoices
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("query", help="Search query in simple query syntax (see below)", nargs="*", default=None),
- usage="vastai search invoices [--help] [--api-key API_KEY] [--raw] <query>",
- help="Search for invoices using custom query",
- epilog=deindent("""
- Query syntax:
- query = comparison comparison...
- comparison = field op value
- field = <name of a field>
- op = one of: <, <=, ==, !=, >=, >, in, notin
- value = <bool, int, float, string> | 'any' | [value0, value1, ...]
- bool: True, False
- note: to pass '>' and '<' on the command line, make sure to use quotes
- note: to encode a string query value (ie for gpu_name), replace any spaces ' ' with underscore '_'
- Examples:
- # search for somewhat reliable single RTX 3090 instances, filter out any duplicates or offers that conflict with our existing stopped instances
- vastai search invoices 'amount_cents>3000 '
- Available fields:
- Name Type Description
- id int,
- user_id int,
- when float, utc epoch timestamp of initial invoice creation
- paid_on float, actual payment date (utc epoch timestamp )
- payment_expected float, expected payment date (utc epoch timestamp )
- amount_cents int, amount of payment in cents
- is_credit bool, is a credit purchase
- is_delayed bool, is not yet paid
- balance_before float, balance before
- balance_after float, balance after
- original_amount int, original amount of payment
- event_id string,
- cut_amount int,
- cut_percent float,
- extra json,
- service string, type of payment
- stripe_charge json,
- stripe_refund json,
- stripe_payout json,
- error json,
- paypal_email string, email for paypal/wise payments
- transfer_group string,
- failed bool,
- refunded bool,
- is_check bool,
- """),
- aliases=hidden_aliases(["search invoices"]),
- )
- def search__invoices(args):
- """Search for invoices using custom query."""
- from vastai.api.query import parse_query, invoices_fields, fix_date_fields
- try:
- query = {}
- if args.query is not None:
- query = parse_query(args.query, query, invoices_fields)
- query = fix_date_fields(query, ['when', 'paid_on', 'payment_expected'])
- except ValueError as e:
- print("Error: ", e)
- return 1
- client = get_client(args)
- rows = offers_api.search_invoices(client, query=query)
- if args.raw:
- return rows
- print(json.dumps(rows, indent=1, sort_keys=True))
- # ---------------------------------------------------------------------------
- # create / update / delete template
- # ---------------------------------------------------------------------------
- @parser.command(
- argument("--name", help="name of the template", type=str),
- argument("--image", help="docker container image to launch", type=str),
- argument("--image_tag", help="docker image tag", type=str),
- argument("--href", help="link you want to provide", type=str),
- argument("--repo", help="link to repository", type=str),
- argument("--login", help="docker login arguments for private repo authentication, surround with ''", type=str),
- argument("--env", help="Contents of the 'Docker options' field", type=str),
- argument("--ssh", help="Launch as an ssh instance type", action="store_true"),
- argument("--jupyter", help="Launch as a jupyter instance instead of an ssh instance", action="store_true"),
- argument("--direct", help="Use (faster) direct connections for jupyter & ssh", action="store_true"),
- argument("--jupyter-dir", help="For runtype 'jupyter', directory in instance to use to launch jupyter", type=str),
- argument("--jupyter-lab", help="For runtype 'jupyter', Launch instance with jupyter lab", action="store_true"),
- argument("--onstart-cmd", help="contents of onstart script as single argument", type=str),
- argument("--search_params", help="search offers filters", type=str),
- argument("-n", "--no-default", action="store_true", help="Disable default search param query args"),
- argument("--disk_space", help="disk storage space, in GB", type=str),
- argument("--readme", help="readme string", type=str),
- argument("--hide-readme", help="hide the readme from users", action="store_true"),
- argument("--desc", help="description string", type=str),
- argument("--public", help="make template available to public", action="store_true"),
- usage="vastai create template",
- help="Create a new template",
- )
- def create__template(args):
- """Create a new template."""
- fields = offers_api.template_fields_from_flags(
- ssh=args.ssh, jupyter=args.jupyter, direct=args.direct,
- jupyter_lab=args.jupyter_lab, login=args.login,
- hide_readme=args.hide_readme, public=args.public,
- search_params=args.search_params, no_default=args.no_default,
- always_default=True,
- )
- extra_filters = fields["extra_filters"]
- if args.explain:
- print("request json: ")
- print({"name": args.name, "image": args.image, "extra_filters": extra_filters})
- client = get_client(args)
- try:
- rj = offers_api.create_template(
- client, name=args.name, image=args.image, image_tag=args.image_tag,
- href=args.href, repo=args.repo, env=args.env, onstart_cmd=args.onstart_cmd,
- jupyter_dir=args.jupyter_dir, disk_space=args.disk_space,
- readme=args.readme, desc=args.desc, **fields,
- )
- if rj.get("success"):
- print(f"New Template: {rj['template']}")
- else:
- print(rj.get('msg', rj))
- except Exception:
- print("The response is not valid JSON.")
- @parser.command(
- argument("HASH_ID", help="hash id of the template", type=str),
- argument("--name", help="name of the template", type=str),
- argument("--image", help="docker container image to launch", type=str),
- argument("--image_tag", help="docker image tag", type=str),
- argument("--href", help="link you want to provide", type=str),
- argument("--repo", help="link to repository", type=str),
- argument("--login", help="docker login arguments for private repo authentication, surround with ''", type=str),
- argument("--env", help="Contents of the 'Docker options' field", type=str),
- argument("--ssh", help="Launch as an ssh instance type", action="store_true"),
- argument("--jupyter", help="Launch as a jupyter instance instead of an ssh instance", action="store_true"),
- argument("--direct", help="Use (faster) direct connections for jupyter & ssh", action="store_true"),
- argument("--jupyter-dir", help="For runtype 'jupyter', directory in instance to use to launch jupyter", type=str),
- argument("--jupyter-lab", help="For runtype 'jupyter', Launch instance with jupyter lab", action="store_true"),
- argument("--onstart-cmd", help="contents of onstart script as single argument", type=str),
- argument("--search_params", help="search offers filters", type=str),
- argument("-n", "--no-default", action="store_true", help="Disable default search param query args"),
- argument("--disk_space", help="disk storage space, in GB", type=str),
- argument("--readme", help="readme string", type=str),
- argument("--hide-readme", help="hide the readme from users", action="store_true"),
- argument("--desc", help="description string", type=str),
- argument("--public", help="make template available to public", action="store_true"),
- usage="vastai update template HASH_ID",
- help="Update an existing template",
- epilog=deindent("""
- Edits create a new version of the template with a new hash id; the original stays
- valid for anyone already using it. Send the full template, not only the changed fields.
- Examples:
- 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
- """),
- )
- def update__template(args):
- """Update an existing template."""
- fields = offers_api.template_fields_from_flags(
- ssh=args.ssh, jupyter=args.jupyter, direct=args.direct,
- jupyter_lab=args.jupyter_lab, login=args.login,
- hide_readme=args.hide_readme, public=args.public,
- search_params=args.search_params, no_default=args.no_default,
- always_default=True,
- )
- extra_filters = fields["extra_filters"]
- if args.explain:
- print("request json: ")
- print({"hash_id": args.HASH_ID, "name": args.name, "image": args.image})
- client = get_client(args)
- try:
- rj = offers_api.update_template(
- client, hash_id=args.HASH_ID, name=args.name, image=args.image,
- image_tag=args.image_tag, href=args.href, repo=args.repo, env=args.env,
- onstart_cmd=args.onstart_cmd, jupyter_dir=args.jupyter_dir,
- disk_space=args.disk_space, readme=args.readme, desc=args.desc,
- **fields,
- )
- if rj.get("success"):
- print(f"updated template: {json.dumps(rj['template'], indent=1)}")
- else:
- print(rj.get("msg", "template update failed"))
- return 1
- except Exception as e:
- print(_response_msg(getattr(e, "response", None)) or str(e))
- return 1
- def _response_msg(response):
- """Return the API's msg from an error response body, or None when there is none."""
- if response is None:
- return None
- try:
- body = response.json()
- except ValueError:
- return None
- return body.get("msg") if isinstance(body, dict) else None
- @parser.command(
- argument("--template-id", help="Template ID of Template to Delete", type=int),
- argument("--hash-id", help="Hash ID of Template to Delete", type=str),
- usage="vastai delete template [--template-id <id> | --hash-id <hash_id>]",
- help="Delete a Template",
- )
- def delete__template(args):
- """Delete a template."""
- if not args.hash_id and not args.template_id:
- print('ERROR: Must Specify either Template ID or Hash ID to delete a template')
- return
- if args.explain:
- print("request json: ")
- print({"hash_id": args.hash_id, "template_id": args.template_id})
- client = get_client(args)
- try:
- rj = offers_api.delete_template(client, hash_id=args.hash_id, template_id=args.template_id)
- print(rj.get('msg', rj))
- except Exception as e:
- print(f"Error: {e}")
vastai/cli/commands/update.py · Bundled script · 77 lines · 1 finding marked
- """`vastai update` — self-update for managed (curl | bash installer) installs.
- pip installs aren't managed by the installer; for those this command only
- prints the correct pip upgrade instructions and exits non-zero — it never
- shells out to pip, since the CLI cannot know which pip/venv owns its
- environment.
- """
- import sys
- from vastai.cli.parser import argument
- from vastai.cli.display import deindent
- from vastai.cli.util import VERSION
- from vastai.cli.selfupdate import (
- INSTALL_SH_HINT, PIP_UPGRADE_HINT, UpdateError,
- fetch_manifest, is_managed_install, is_newer, perform_update,
- )
- from vastai.cli.utils import get_parser as _get_parser
- parser = _get_parser()
- EXIT_STALE = 10 # `update --check` exit code when a newer version exists
- @parser.command(
- argument("--check", action="store_true", help="only check for a newer version; exit 0 if current, 10 if an update is available"),
- argument("--version", dest="target_version", metavar="VERSION", help="install a specific version instead of the latest (also how you pin or roll back)"),
- usage="vastai update [--check | --version VERSION]",
- help="Update the CLI to the latest version",
- epilog=deindent("""
- Updates a CLI installed with the managed installer
- (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
- installed into a fresh environment and verified before it is swapped
- in, so an interrupted update never leaves a broken CLI. To roll back a
- bad release, pin the previous one: vastai update --version 1.2.3
- For pip installs, update with: pip install --upgrade vastai
- """),
- )
- def update(args):
- try:
- if args.check:
- latest = fetch_manifest()["latest"]
- if is_newer(latest, VERSION):
- print(f"Update available: {latest} (you have {VERSION})")
- return EXIT_STALE
- print(f"vastai {VERSION} is up to date (latest: {latest})")
- return 0
- if not is_managed_install():
- print(
- "This CLI was not installed with the managed installer, so it "
- f"cannot self-update.\n Installed via pip? Run: {PIP_UPGRADE_HINT}\n"
- f" Or switch to the managed install: {INSTALL_SH_HINT}",
- file=sys.stderr,
- )
- return 1
- manifest = fetch_manifest()
- target = args.target_version or manifest["latest"]
- current = VERSION
- if target == current:
- latest = manifest["latest"]
- if args.target_version:
- print(f"vastai {current} is already installed (latest available: {latest}).")
- else:
- print(f"vastai {current} is up to date (latest: {latest}).")
- return 0
- print(f"Updating vastai {current} -> {target} ...")
- perform_update(target, manifest)
- print(f"Done. vastai is now {target}.")
- return 0
- except UpdateError as e:
- print(str(e), file=sys.stderr)
- if not args.check:
- print("The current install was left untouched.", file=sys.stderr)
- return 1
vastai/cli/selfupdate.py · Bundled script · 364 lines · 1 finding marked
- """Self-update engine for the managed (curl | bash) CLI install.
- Layout (docs/install-design.md, XDG Base Directory Specification): the
- install root defaults to $XDG_DATA_HOME/vastai (~/.local/share/vastai),
- override with $VASTAI_INSTALL_DIR — a single active venv at current/, with
- bin/vastai a fixed symlink into it. Updating rebuilds current/ in place
- (build temp → verify → swap); there is no version retention.
- "Managed install?" is detected structurally (``is_managed_install``): a managed
- CLI runs from <root>/current with a sibling <root>/bin/uv. That's ground
- truth — no on-disk marker to drift or hand-copy. pip installs fail the check
- and the updater never touches them.
- The passive nudge is throttled to one manifest GET and one notice per week,
- capped at 1s, and swallows every failure — the CLI must never get slower or
- noisier because the manifest endpoint is unreachable.
- """
- import json
- import os
- import re
- import shutil
- import subprocess
- import sys
- import time
- from pathlib import Path
- import requests
- from vastai.cli.util import DATA_HOME, DIRS, VERSION
- DEFAULT_MANIFEST_URL = "https://vast.ai/cli/manifest.json"
- 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
- PIP_UPGRADE_HINT = "pip install --upgrade vastai"
- # Oldest version whose bundled selfupdate.py understands this layout
- # (<root>/current + <root>/bin/uv, XDG-based root). Versions before this pin
- # assumed ~/.vastai/env and would install fine but then see themselves as unmanaged,
- # permanently losing self-update until the install is rebuilt from scratch.
- MIN_DOWNGRADE_VERSION = "1.4.0"
- UPDATE_CHECK_FILE = os.path.join(DIRS['state'], "update_check.json")
- CHECK_INTERVAL_S = 7 * 24 * 60 * 60
- NUDGE_TIMEOUT_S = 1.0
- # Console scripts shipped in the wheel; each gets a swapped symlink in bin/.
- MANAGED_BINARIES = ("vastai", "serve-vast-deployment", "register-python-argcomplete")
- # Fixed, not XDG-overridable — every other XDG-aware CLI lands its symlink here too.
- LOCAL_BIN = Path.home() / ".local" / "bin"
- class UpdateError(Exception):
- """A self-update step failed; the current install is untouched."""
- class UninstallError(Exception):
- """A step of vastai uninstall failed; nothing further was attempted."""
- # ---------------------------------------------------------------------------
- # Install detection / manifest / versions
- # ---------------------------------------------------------------------------
- def install_root() -> Path:
- """Where the managed install lives.
- Prefers ground truth from where this interpreter is actually running: if
- we're already inside a managed install (``<root>/current`` next to
- ``<root>/bin/uv``), that's authoritative and immune to
- $VASTAI_INSTALL_DIR/$XDG_DATA_HOME differing between install time and
- now — e.g. a custom install dir that isn't set in the current shell would
- otherwise make a real managed install look unmanaged. Only when we're
- *not* currently running from one (a pip install checking
- ``is_managed_install``) do we fall back to computing where a managed
- install would live.
- """
- prefix = Path(sys.prefix).resolve()
- if prefix.name == "current" and (prefix.parent / "bin" / "uv").exists():
- return prefix.parent
- if os.environ.get("VASTAI_INSTALL_DIR"):
- return Path(os.environ["VASTAI_INSTALL_DIR"]).expanduser()
- return Path(DATA_HOME) / "vastai"
- def is_managed_install() -> bool:
- """True iff this CLI was installed by the managed installer.
- Detected from where the interpreter actually runs: a managed CLI lives in
- ``<root>/current`` next to ``<root>/bin/uv``. Ground truth, no marker file.
- pip installs run from elsewhere and fail the check.
- """
- try:
- root = install_root()
- return (
- Path(sys.prefix).resolve() == (root / "current").resolve()
- and (root / "bin" / "uv").exists()
- )
- except Exception:
- return False
- def fetch_manifest(timeout: float = 10.0) -> dict:
- url = os.environ.get("VASTAI_MANIFEST_URL") or DEFAULT_MANIFEST_URL
- try:
- r = requests.get(url, timeout=timeout)
- r.raise_for_status()
- data = r.json()
- except (requests.exceptions.RequestException, ValueError) as e:
- raise UpdateError(f"Could not fetch release manifest from {url}: {e}")
- if not isinstance(data, dict) or "latest" not in data:
- raise UpdateError(f"Malformed release manifest at {url}")
- if data.get("schema") != 1:
- raise UpdateError(
- f"Release manifest schema {data.get('schema')!r} is not supported "
- f"by this CLI version.\nRe-run the installer: {INSTALL_SH_HINT}"
- )
- return data
- def version_key(v: str):
- """Sort key tolerant of dev/rc (below release), post (above), +local (ignored)."""
- v = (v or "").strip().lstrip("v").split("+", 1)[0]
- nums, extra = [], 0
- for part in v.split("."):
- if part.isdigit():
- nums.append(int(part))
- continue
- m = re.match(r"\d+", part)
- if m:
- nums.append(int(m.group()))
- extra = 1 if part.startswith("post") else -1
- break
- while len(nums) < 3:
- nums.append(0)
- return tuple(nums), extra
- def is_newer(candidate: str, current: str) -> bool:
- return version_key(candidate) > version_key(current)
- # ---------------------------------------------------------------------------
- # Passive nudge
- # ---------------------------------------------------------------------------
- def _load_check_state() -> dict:
- try:
- with open(UPDATE_CHECK_FILE) as f:
- data = json.load(f)
- return data if isinstance(data, dict) else {}
- except (OSError, ValueError):
- return {}
- def _save_check_state(state: dict) -> None:
- try:
- with open(UPDATE_CHECK_FILE, "w") as f:
- json.dump(state, f)
- except OSError:
- pass
- def _stderr_is_tty() -> bool:
- try:
- return sys.stderr.isatty()
- except Exception:
- return False
- def notify_update(args=None) -> None:
- """Pre-command, best-effort upgrade nudge. Must never raise or block."""
- try:
- _notify_update(args)
- except Exception:
- pass
- def _notify_update(args) -> None:
- suppressed = (
- os.environ.get("VASTAI_NO_UPDATE_CHECK")
- or os.environ.get("CI")
- or getattr(args, "raw", False)
- or not _stderr_is_tty()
- # the updater itself doesn't need a nudge
- or getattr(getattr(args, "func", None), "__module__", "") == "vastai.cli.commands.update"
- )
- if suppressed:
- return
- now = time.time()
- state = _load_check_state()
- if now - state.get("checked_at", 0) > CHECK_INTERVAL_S:
- state["checked_at"] = now # stamped even on failure, so errors back off too
- try:
- state["latest"] = fetch_manifest(timeout=NUDGE_TIMEOUT_S).get("latest")
- except UpdateError:
- pass
- _save_check_state(state)
- latest = state.get("latest")
- if not latest or not is_newer(latest, VERSION):
- return
- if now - state.get("notified_at", 0) <= CHECK_INTERVAL_S:
- return
- hint = "vastai update" if is_managed_install() else PIP_UPGRADE_HINT
- arrow = "↑" if "utf" in (sys.stderr.encoding or "").lower() else "*"
- # Trailing blank line separates the nudge from the command output that follows it.
- print(
- f"{arrow} vastai {latest} is available (you have {VERSION}). Run `{hint}`.\n",
- file=sys.stderr,
- )
- state["notified_at"] = now
- _save_check_state(state)
- # ---------------------------------------------------------------------------
- # Update / rollback (managed installs only)
- # ---------------------------------------------------------------------------
- def _run(cmd, *, env=None):
- cmd = [str(c) for c in cmd]
- try:
- return subprocess.run(cmd, env=env, check=True, capture_output=True, text=True)
- except subprocess.CalledProcessError as e:
- # Surface the tool's own stderr — that's the real error (e.g. uv's
- # resolution failure), not a category we invent.
- detail = (e.stderr or e.stdout or "").strip()
- raise UpdateError(f"Command failed: {' '.join(cmd)}" + (f"\n{detail}" if detail else ""))
- except OSError as e:
- # Carry the actual OSError through (wrong path, missing binary, …)
- # rather than relabeling everything "Required tool not found".
- raise UpdateError(f"Could not run {' '.join(cmd)}: {e}")
- def _prune_uv_cache(uv: Path, env: dict) -> None:
- """Best-effort prune of uv's cache after a successful swap.
- Each update hardlinks the new env out of the cache, so the cache
- accumulates every version's wheels forever unless pruned. Prune (not
- clean): the cache is the user-global one, shared with any other uv on
- the machine. The update already succeeded — never fail because of this.
- """
- try:
- _run([uv, "cache", "prune", "--quiet"], env=env)
- except UpdateError:
- pass
- def _link_binaries(root: Path) -> None:
- """Point bin/<name> at the fixed current/bin/<name> for each shipped
- console script. The target path is constant across updates (always
- ``current/``), so this is not symlink *retargeting* — just (re)creating
- stable links."""
- for name in MANAGED_BINARIES:
- if not (root / "current" / "bin" / name).exists():
- continue
- link, tmp = root / "bin" / name, root / "bin" / f".{name}.tmp"
- if tmp.is_symlink() or tmp.exists():
- tmp.unlink()
- os.symlink(Path("..") / "current" / "bin" / name, tmp)
- os.replace(tmp, link)
- def wheel_spec(target: str, manifest: dict) -> str:
- """Requirement spec that installs ``target``.
- Latest installs the manifest's release wheel: hash-pinned bytes straight
- from the GitHub Release, no PyPI propagation window. Other targets are
- pins/rollbacks to long-published versions, where the PyPI pin is
- race-free and covers releases that predate wheel_url.
- """
- install = manifest.get("install") or {}
- url, sha = install.get("wheel_url"), install.get("wheel_sha256")
- if target == manifest.get("latest") and url and sha:
- return f"vastai @ {url}#sha256={sha}"
- return f"vastai=={target}"
- def perform_update(target: str, manifest: dict) -> None:
- """Install ``target`` into a fresh env, verify it, then swap it in.
- Single active install (the deno model): the new env is built in a temp dir
- and only swapped over the live one after it verifies, so an interrupted
- update can never leave a half-written or broken ``vastai``. There is no
- version retention — a re-pin or rollback is just ``update --version X``,
- which reinstalls. Wheel integrity comes from ``wheel_spec``; we
- additionally confirm the installed version matches ``target``.
- """
- if version_key(target) < version_key(MIN_DOWNGRADE_VERSION):
- raise UpdateError(
- f"Can't roll back to {target}: versions before {MIN_DOWNGRADE_VERSION} "
- "don't recognize this install's layout and would lose the ability "
- f"to self-update. The oldest version you can roll back to is {MIN_DOWNGRADE_VERSION}."
- )
- root = install_root()
- uv = root / "bin" / "uv"
- if not uv.exists():
- raise UpdateError(f"Bootstrap tool missing at {uv}.\nRe-run the installer: {INSTALL_SH_HINT}")
- python_pin = (manifest.get("install") or {}).get("python") or "3.12"
- env_dir, new_dir, old_dir = root / "current", root / ".current.new", root / ".current.old"
- shutil.rmtree(new_dir, ignore_errors=True)
- uv_env = dict(os.environ,
- UV_PYTHON_INSTALL_DIR=str(root / "python"),
- UV_PYTHON_PREFERENCE="only-managed")
- try:
- # --relocatable: shebangs must not hardcode the build path (env is renamed into place).
- _run([uv, "venv", new_dir, "--python", python_pin, "--relocatable", "--quiet"], env=uv_env)
- _run([uv, "pip", "install", "--python", new_dir / "bin" / "python",
- "--quiet", wheel_spec(target, manifest)], env=uv_env)
- installed = (_run([new_dir / "bin" / "vastai", "--version"]).stdout or "").strip()
- if target not in installed:
- raise UpdateError(
- f"Verification failed: new install reports version {installed!r}, expected {target}"
- )
- except UpdateError:
- shutil.rmtree(new_dir, ignore_errors=True)
- raise
- # Swap via two renames (~instant); a crash mid-build only dirties .current.new.
- shutil.rmtree(old_dir, ignore_errors=True)
- if env_dir.exists():
- os.replace(env_dir, old_dir)
- os.replace(new_dir, env_dir)
- _link_binaries(root)
- shutil.rmtree(old_dir, ignore_errors=True)
- _prune_uv_cache(uv, uv_env)
- # ---------------------------------------------------------------------------
- # Uninstall (managed installs only)
- # ---------------------------------------------------------------------------
- def perform_uninstall() -> Path:
- """Remove the managed install: the install root and its bin/ symlinks.
- Config, cache, and state (``vastai.cli.util.DIRS`` — each independently
- XDG-overridable) are left alone — the same guarantee documented for a
- manual ``rm -rf`` (docs/install-design.md §3). Safe to call while running
- from inside ``root``: like the update swap above, removing the directory
- doesn't disturb an already-running interpreter on POSIX.
- Raises ``UninstallError`` on any real failure (permissions, a symlink
- loop, …) instead of silently reporting success — callers should treat
- this as fallible, not a fire-and-forget best-effort cleanup.
- """
- root = install_root().resolve()
- for name in MANAGED_BINARIES:
- link = LOCAL_BIN / name
- try:
- if link.is_symlink() and root in link.resolve().parents:
- link.unlink()
- except OSError as e:
- raise UninstallError(f"Could not remove {link}: {e}")
- try:
- shutil.rmtree(root)
- except FileNotFoundError:
- pass # already gone — nothing left to remove
- except OSError as e:
- raise UninstallError(f"Could not remove {root}: {e}")
- return root
Ratings and comments
Sign in to rate this skill.
Sign inFree to use. If it’s helped, pass it on.