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/selfupdate.py

View on GitHub
"""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"
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
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