Skip to content

vast-ai/vast-cli / vastai

vastai

Flagged

Vast.ai CLI to manage GPU instances, volumes, serverless endpoints, and billing.

FileKindSize
SKILL.mdskill md18.6 KB
__init__.pyscript1.8 KB
_base.pyscript1.6 KB
api/__init__.pyscript31 B
api/auth.pyscript15.3 KB
api/billing.pyscript10.4 KB
api/client.pyscript9.1 KB
api/clusters.pyscript4.5 KB
api/deployments.pyscript1.4 KB
api/endpoints.pyscript12.0 KB
api/instances.pyscript17.1 KB
api/keys.pyscript4.3 KB
api/machines.pyscript7.4 KB
api/metrics.pyscript2.6 KB
api/offers.pyscript21.2 KB
api/price_increase.pyscript4.0 KB
api/query.pyscript12.2 KB
api/storage.pyscript14.5 KB
api/teams.pyscript4.4 KB
async_/__init__.pyscript79 B
async_/client.pyscript9.6 KB
async_/results.pyscript1.3 KB
cli/__init__.pyscript25 B
cli/commands/__init__.pyscript757 B
cli/commands/auth.pyscript42.8 KB
cli/commands/benchmarks.pyscript42.0 KB
cli/commands/billing.pyscript30.4 KB
cli/commands/clusters.pyscript7.8 KB
cli/commands/deployments.pyscript7.8 KB
cli/commands/endpoints.pyscript17.3 KB
cli/commands/instances.pyscript55.4 KB
cli/commands/keys.pyscript7.9 KB
cli/commands/machines.pyscript78.4 KB
cli/commands/metrics.pyscript12.7 KB
cli/commands/misc.pyscript11.0 KB
cli/commands/offers.pyscript30.0 KB
cli/commands/price_increase.pyscript14.5 KB
cli/commands/repl.pyscript719 B
cli/commands/storage.pyscript38.9 KB
cli/commands/teams.pyscript6.4 KB
cli/commands/uninstall.pyscript2.4 KB
cli/commands/update.pyscript3.1 KB
cli/display.pyscript20.1 KB
cli/main.pyscript11.5 KB
cli/parser.pyscript18.1 KB
cli/repl/__init__.pyscript318 B
cli/repl/bridge.pyscript7.3 KB
cli/repl/catalog.pyscript6.3 KB
cli/repl/completion.pyscript8.2 KB
cli/repl/session.pyscript7.3 KB
cli/self_test/__init__.pyscript46 B
cli/self_test/machine_diagnostics.pyscript21.0 KB
cli/self_test/runtime_diagnostics.pyscript18.6 KB
cli/self_test/support_bundle.pyscript11.0 KB
cli/selfupdate.pyscript14.0 KB
cli/util.pyscript26.3 KB
cli/utils.pyscript577 B
data/__init__.pyscript303 B
data/deployment.pyscript3.5 KB
data/endpoint.pyscript2.1 KB
data/instance.pyscript3.6 KB
data/offer.pyscript4.4 KB
data/query.pyscript12.8 KB
data/workergroup.pyscript1.4 KB
pdf/__init__.pyscript0 B
pdf/vast.ai-logo-50pct.pngbinary7.4 KB
pdf/vast.ai-logo.pngbinary10.1 KB
pdf/vast_pdf.pyscript19.4 KB
pdf/watcher.shexecutablescript390 B
sdk.pyscript52.2 KB
serverless/__init__.pyscript193 B
serverless/client/__init__.pyscript346 B
serverless/client/client.pyscript36.7 KB
serverless/client/connection.pyscript11.5 KB
serverless/client/endpoint.pyscript6.9 KB
serverless/client/managed.pyscript7.3 KB
serverless/client/request_status.pyscript371 B
serverless/client/session.pyscript2.8 KB
serverless/client/worker.pyscript1.3 KB
serverless/remote/__init__.pyscript245 B
serverless/remote/base.pyscript6.3 KB
serverless/remote/deploy.pyscript12.6 KB
serverless/remote/serialization.pyscript3.7 KB
serverless/remote/serve.pyscript8.6 KB
serverless/remote/serve_deployment.pyscript6.3 KB
serverless/remote/utils.pyscript9.7 KB
serverless/server/__init__.pyscript0 B
serverless/server/lib/__init__.pyscript0 B
serverless/server/lib/backend.pyscript43.3 KB
serverless/server/lib/data_types.pyscript10.6 KB
serverless/server/lib/metrics.pyscript12.2 KB
serverless/server/lib/server.pyscript3.2 KB
serverless/server/worker.pyscript17.1 KB
sync/__init__.pyscript76 B
sync/client.pyscript4.9 KB
sync/results.pyscript1.2 KB
utils.pyscript12.2 KB

cli/main.py

View on GitHub
"""CLI entry point for vastai."""
import sys
import os
import json
import requests
from datetime import datetime, timezone

from vastai.api.client import is_search_quota_exceeded
from vastai.cli.parser import apwrap, argument, MyWideHelpFormatter, set_completers
from vastai.cli.util import (
    APIKEY_FILE, TFAKEY_FILE, VERSION, server_url_default, api_key_guard,
    format_key_suffix,
)

try:
    JSONDecodeError = json.JSONDecodeError
except AttributeError:
    JSONDecodeError = ValueError


def _is_tfa_session_expired(status_code, errmsg):
    """True if this error signals an expired 2FA session key.

    401 "Invalid user key" is sent when a deleted API key is used; an expired
    TFA session instead returns 404 "Session expired. Please log in again."
    (matches legacy vast.py's handling of both signals).
    """
    return (
        (status_code == 401 and errmsg == "Invalid user key")
        or (status_code == 404 and errmsg == "Session expired. Please log in again.")
    )


def with_quota_reset(response, errmsg):
    """Append the reset time from ``X-RateLimit-Reset`` to a search quota denial's message."""
    if not is_search_quota_exceeded(response):
        return errmsg
    try:
        reset = datetime.fromtimestamp(int(response.headers["X-RateLimit-Reset"]), timezone.utc)
    except (KeyError, ValueError):
        return errmsg
    return f"{errmsg}. Resets at {reset:%Y-%m-%d %H:%M} UTC."


def read_key(path):
    """The key stored at a path, or None if it is absent or unreadable."""
    try:
        with open(path) as f:
            return f.read().strip() or None
    except OSError:
        return None


def _emit_error(args, status_code, message):
    """Emit a command error in the appropriate format.

    In ``--raw`` mode, prints a JSON error object to stderr so scripts can
    parse it; otherwise prints a human-readable line. Always goes to stderr
    so stdout stays clean for scripting consumers.
    """
    if getattr(args, "raw", False):
        payload = {"error": True, "status_code": status_code, "msg": message}
        print(json.dumps(payload), file=sys.stderr)
        return

    if status_code:
        print(f"Failed with error {status_code}: {message}", file=sys.stderr)
    else:
        print(message, file=sys.stderr)

    if status_code == 401:
        if "Two Factor Authentication" in message:
            print(
                "\nThis endpoint requires a 2FA session. Authenticate with the method you have set up:\n"
                "  vastai tfa login --method-type totp -c <CODE>\n"
                "  vastai tfa login --method-type sms   --secret <SECRET> -c <CODE>\n"
                "  vastai tfa login --method-type email --secret <SECRET> -c <CODE>\n"
                "  vastai tfa login --backup-code ABCD-EFGH-IJKL\n"
                "For SMS or email, first run 'vastai tfa send-sms' or 'vastai tfa send-email' to get <SECRET>.",
                file=sys.stderr,
            )
            return

        env = os.environ.get("VAST_API_KEY")
        file_key = read_key(APIKEY_FILE)
        tfa_key = read_key(TFAKEY_FILE)

        # A --api-key outranks the rest, so a value matching none came from there.
        sent = getattr(args, "api_key", None)
        key_missing = not sent and not env and not file_key

        if sent and sent == tfa_key:
            print(f"  Sent your 2FA session key from {TFAKEY_FILE} (ends in {format_key_suffix(sent)}).", file=sys.stderr)
            print("  If it has expired, start a new one: vastai tfa login", file=sys.stderr)
        elif sent and sent != env and sent != file_key:
            server = getattr(args, "url", None) or server_url_default
            print(f"  Sent key from --api-key (ends in {format_key_suffix(sent)}).", file=sys.stderr)
            print(f"  Check that key is valid for {server}.", file=sys.stderr)
        elif key_missing:
            print("  No API key is configured.", file=sys.stderr)
            print("  Run: vastai set api-key <KEY>", file=sys.stderr)
            print("  Create a key at https://console.vast.ai/manage-keys/", file=sys.stderr)
        elif env and file_key:
            print(f"  Sent key from $VAST_API_KEY (ends in {format_key_suffix(env)}). Env var overrides the file.", file=sys.stderr)
            print(f"  Unset the VAST_API_KEY env var to use the saved key in {APIKEY_FILE} (ends in {format_key_suffix(file_key)}) instead.", file=sys.stderr)
        elif env:
            print(f"  Sent key from $VAST_API_KEY (ends in {format_key_suffix(env)}). Update the env var with a new key.", file=sys.stderr)
        elif file_key:
            print(f"  Sent key from {APIKEY_FILE} (ends in {format_key_suffix(file_key)}). Update with: vastai set api-key <KEY>", file=sys.stderr)


# Create the global parser instance
parser = apwrap(
    epilog="Use 'vastai COMMAND --help' for more info about a command. AI agent? See https://raw.githubusercontent.com/vast-ai/vast-cli/master/vastai/SKILL.md",
    formatter_class=MyWideHelpFormatter
)


def main():
    # Import all command modules - the import itself triggers decorator
    # registrations on the global parser via _get_parser().
    from vastai.cli.commands import (  # noqa: F401
        instances, offers, machines, teams, keys, endpoints,
        billing, storage, auth, misc, deployments, metrics,
        benchmarks,
        price_increase,
        repl,
        update,
        uninstall,
        # clusters,  # cluster/overlay commands disabled for now
    )

    # Wire up tab completers now that command modules are loaded
    set_completers(
        instance_machine_fn=lambda **kw: instances.show__instances(
            type('Args', (), {'api_key': os.getenv('VAST_API_KEY'), 'url': server_url_default,
                              'retry': 3, 'explain': False, 'curl': False,
                              'quiet': False, 'raw': False})(),
            {'internal': True, 'field': 'machine_id'}),
        instance_fn=lambda **kw: instances.show__instances(
            type('Args', (), {'api_key': os.getenv('VAST_API_KEY'), 'url': server_url_default,
                              'retry': 3, 'explain': False, 'curl': False,
                              'quiet': False, 'raw': False})(),
            {'internal': True, 'field': 'id'}),
    )

    # Add global arguments
    parser.add_argument("--url", help="Server REST API URL", default=server_url_default)
    parser.add_argument("--retry", help="Retry limit", type=int, default=3)
    parser.add_argument("--explain", action="store_true", help="Output verbose explanation of mapping of CLI calls to HTTPS API endpoints")
    parser.add_argument("--raw", action="store_true", help="Output machine-readable json")
    parser.add_argument("--full", action="store_true", help="Print full results instead of paging with `less` for commands that support it")
    parser.add_argument("--curl", action="store_true", help="Show a curl equivalency to the call")
    parser.add_argument("--api-key", help="API Key to use. defaults to using the one stored in {}".format(APIKEY_FILE), type=str, required=False, default=os.getenv("VAST_API_KEY", api_key_guard))
    parser.add_argument("--version", help="Show CLI version", action="version", version=VERSION)
    parser.add_argument("--no-color", action="store_true", help="Disable colored output for commands that support it")

    # Tab completion
    try:
        import argcomplete
        import argparse

        from typing import List, Optional
        from vastai.cli.parser import build_command_maps, two_stage_command_completions
        class MyAutocomplete(argcomplete.CompletionFinder):
            # Two-stage (verb -> object) completion over the flat "verb object"
            # subparser names. Logic in parser.py (pure + unit-tested).
            def _command_maps(self):
                cached = getattr(self, "_cmd_maps", None)
                if cached is None:
                    from vastai.cli.util import get_role
                    cached = self._cmd_maps = build_command_maps(self._parser, role=get_role())
                return cached

            def _get_completions(self, comp_words, cword_prefix, cword_prequote, last_wordbreak_pos):
                verbs, verb_objs, singles = self._command_maps()
                cands, merged = two_stage_command_completions(
                    comp_words, cword_prefix, verbs, verb_objs, singles)
                if cands is not None:
                    return cands
                return super()._get_completions(merged, cword_prefix, cword_prequote, last_wordbreak_pos)

            def quote_completions(self, completions: List[str], cword_prequote: str, last_wordbreak_pos: Optional[int]) -> List[str]:
                pre = super().quote_completions(completions, cword_prequote, last_wordbreak_pos)
                return sorted(pre, key=lambda x: x.startswith('-'))

        myautocc = MyAutocomplete()
        myautocc(parser.parser)
    except ImportError:
        pass

    args = parser.parse_args()

    # Passive upgrade nudge: best-effort, ≤1 manifest GET + ≤1 stderr line per
    # 24h, silent when offline/piped/CI. Never raises. Opt out with
    # VASTAI_NO_UPDATE_CHECK=1. Runs first so it's visible before any command
    # output, not buried after it. See selfupdate.py and §7.
    from vastai.cli.selfupdate import notify_update
    notify_update(args)

    # API key resolution
    if args.api_key is api_key_guard:
        key_file = TFAKEY_FILE if os.path.exists(TFAKEY_FILE) else APIKEY_FILE
        if os.path.exists(key_file):
            with open(key_file, "r") as reader:
                args.api_key = reader.read().strip()
        else:
            args.api_key = None

    run_command(args)


def run_command(args):
    """Execute ``args.func(args)``, retrying once on an expired 2FA session key."""
    while True:
        try:
            res = args.func(args)

            if args.raw and res is not None:
                try:
                    print(json.dumps(res, indent=1, sort_keys=True))
                except (TypeError, ValueError):
                    print(json.dumps(res.json(), indent=1, sort_keys=True))
                sys.exit(0)
            sys.exit(res)

        except requests.exceptions.HTTPError as e:
            try:
                errmsg = e.response.json().get("msg")
            except JSONDecodeError:
                if e.response.status_code == 401:
                    errmsg = "Please log in or sign up"
                else:
                    errmsg = "(no detail message supplied)"

            # 2FA session key expired -> retry with long-lived API key.
            if _is_tfa_session_expired(e.response.status_code, errmsg) and os.path.exists(TFAKEY_FILE):
                print(f"Failed with error {e.response.status_code}: Your 2FA session has expired.")
                os.remove(TFAKEY_FILE)
                if os.path.exists(APIKEY_FILE):
                    with open(APIKEY_FILE, "r") as reader:
                        args.api_key = reader.read().strip()
                        print(f"Trying again with your normal API Key from {APIKEY_FILE}...")
                        print("To start a new 2FA session, run: vastai tfa login")
                        continue
                else:
                    print("Run `vastai tfa login` to start a new 2FA session and try again.")
                    break

            _emit_error(args, e.response.status_code, with_quota_reset(e.response, errmsg))
            break

        except ValueError as e:
            _emit_error(args, 0, str(e))
            break


if __name__ == "__main__":
    try:
        main()
    except (KeyboardInterrupt, BrokenPipeError):
        pass
slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack."Stars: 198kUpdated Claude CodeCodexVerifiedInstall
ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report.Stars: 194kUpdated Claude CodeCodexVerifiedInstall
ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt.Stars: 194kUpdated Claude CodeCodexVerifiedInstall