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
"""Shared utility functions for the Vast.ai SDK and CLI.

Generic helpers that are not specific to any single layer (CLI, API, SDK).
"""

import re
import os
import sys
import time
import math
import argparse
import subprocess
import importlib.metadata
from datetime import datetime, timedelta, timezone
from concurrent.futures import ThreadPoolExecutor


# ---------------------------------------------------------------------------
# Version helpers
# ---------------------------------------------------------------------------

def parse_version(version: str) -> tuple:
    parts = version.split(".")

    if len(parts) < 3:
        print(f"Invalid version format: {version}", file=sys.stderr)

    return tuple(int(part) for part in parts)


def _get_git_version():
    try:
        result = subprocess.run(
            ["git", "describe", "--tags", "--abbrev=0"],
            capture_output=True,
            text=True,
            check=True,
            cwd=os.path.dirname(__file__),
        )
        tag = result.stdout.strip()
        return tag[1:] if tag.startswith("v") else tag
    except Exception:
        return "0.0.0"


def _get_local_version():
    try:
        return importlib.metadata.version("vastai")
    except Exception:
        return _get_git_version()


VERSION = _get_local_version()


# ---------------------------------------------------------------------------
# Validation helpers
# ---------------------------------------------------------------------------

def validate_seconds(value):
    """Validate that the input value is a valid number for seconds between yesterday and Jan 1, 2100."""
    try:
        val = int(value)

        # Calculate min_seconds as the start of yesterday in seconds
        yesterday = datetime.now() - timedelta(days=1)
        min_seconds = int(yesterday.timestamp())

        # Calculate max_seconds for Jan 1st, 2100 in seconds
        max_date = datetime(2100, 1, 1, 0, 0, 0)
        max_seconds = int(max_date.timestamp())

        if not (min_seconds <= val <= max_seconds):
            raise argparse.ArgumentTypeError(f"{value} is not a valid second timestamp.")
        return val
    except ValueError:
        raise argparse.ArgumentTypeError(f"{value} is not a valid integer.")


# ---------------------------------------------------------------------------
# VRL (Vast Resource Locator) parsing
# ---------------------------------------------------------------------------

class VRLException(Exception):
    pass

def parse_vast_url(url_str):
    """
    Breaks up a vast-style url in the form instance_id:path and does
    some basic sanity type-checking.

    :param url_str:
    :return:
    """

    instance_id = None
    path = url_str
    #print(f'url_str: {url_str}')
    if (":" in url_str):
        url_parts = url_str.split(":", 2)
        if len(url_parts) == 2:
            (instance_id, path) = url_parts
        else:
            raise VRLException("Invalid VRL (Vast resource locator).")
    else:
        try:
            instance_id = int(path)
            path = "/"
        except:
            pass

    valid_unix_path_regex = re.compile('^(/)?([^/\0]+(/)?)+$')
    # Got this regex from https://stackoverflow.com/questions/537772/what-is-the-most-correct-regular-expression-for-a-unix-file-path
    if (path != "/") and (valid_unix_path_regex.match(path) is None):
        raise VRLException(f"Path component: {path} of VRL is not a valid Unix style path.")

    #print(f'instance_id: {instance_id}')
    #print(f'path: {path}')
    return (instance_id, path)


# ---------------------------------------------------------------------------
# String / environment parsing
# ---------------------------------------------------------------------------

def smart_split(s, char):
    in_double_quotes = False
    in_single_quotes = False  # note that isn't designed to work with nested quotes within the env
    parts = []
    current = []

    for c in s:
        if c == char and not (in_double_quotes or in_single_quotes):
            parts.append(''.join(current))
            current = []
        elif c == '\'':
            in_single_quotes = not in_single_quotes
            current.append(c)
        elif c == '\"':
            in_double_quotes = not in_double_quotes
            current.append(c)
        else:
            current.append(c)
    parts.append(''.join(current))  # add last part
    return parts


def parse_env(envs):
    result = {}
    if (envs is None):
        return result
    env = smart_split(envs, ' ')
    prev = None
    for e in env:
        if (prev is None):
            if (e in {"-e", "-p", "-h", "-v", "-n"}):
                prev = e
            else:
                pass
        else:
            if (prev == "-p"):
                if set(e).issubset(set("0123456789:tcp/udp")):
                    result["-p " + e] = "1"
                else:
                    pass
            elif (prev == "-e"):
                kv = e.split('=')
                if len(kv) >= 2:
                    val = kv[1]
                    if len(kv) > 2:
                        val = '='.join(kv[1:])
                    result[kv[0]] = val.strip("'\"")
                else:
                    pass
            elif (prev == "-v"):
                if (set(e).issubset(set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789:./_"))):
                    result["-v " + e] = "1"
            elif (prev == "-n"):
                if (set(e).issubset(set("abcdefghijklmnopqrstuvwxyz0123456789-"))):
                    result["-n " + e] = "1"
            else:
                result[prev] = e
            prev = None
    return result


# ---------------------------------------------------------------------------
# List / threading helpers
# ---------------------------------------------------------------------------

def split_list(lst, k):
    """
    Splits a list into sublists of maximum size k.
    """
    return [lst[i:i + k] for i in range(0, len(lst), k)]


def exec_with_threads(f, args, nt=16, max_retries=5):
    def worker(sub_args):
        for arg in sub_args:
            retries = 0
            while retries <= max_retries:
                try:
                    result = None
                    if isinstance(arg, tuple):
                        result = f(*arg)
                    else:
                        result = f(arg)
                    if result:  # Assuming a truthy return value means success
                        break
                except Exception as e:
                    print(str(e))
                    pass
                retries += 1
                stime = 0.25 * 1.3 ** retries
                print(f"retrying in {stime}s")
                time.sleep(stime)  # Exponential backoff

    # Split args into nt sublists
    args_per_thread = math.ceil(len(args) / nt)
    sublists = [args[i:i + args_per_thread] for i in range(0, len(args), args_per_thread)]

    with ThreadPoolExecutor(max_workers=nt) as executor:
        executor.map(worker, sublists)


# ---------------------------------------------------------------------------
# Date / scheduling helpers
# ---------------------------------------------------------------------------

def convert_timestamp_to_date(unix_timestamp):
    utc_datetime = datetime.fromtimestamp(unix_timestamp, tz=timezone.utc)
    return utc_datetime.strftime("%Y-%m-%d")

def parse_day_cron_style(value):
    """
    Accepts an integer string 0-6 or '*' to indicate 'Every day'.
    Returns 0-6 as int, or None if '*'.
    """
    val = str(value).strip()
    if val == "*":
        return None
    try:
        day = int(val)
        if 0 <= day <= 6:
            return day
    except ValueError:
        pass
    raise argparse.ArgumentTypeError("Day must be 0-6 (0=Sunday) or '*' for every day.")

def parse_hour_cron_style(value):
    """
    Accepts an integer string 0-23 or '*' to indicate 'Every hour'.
    Returns 0-23 as int, or None if '*'.
    """
    val = str(value).strip()
    if val == "*":
        return None
    try:
        hour = int(val)
        if 0 <= hour <= 23:
            return hour
    except ValueError:
        pass
    raise argparse.ArgumentTypeError("Hour must be 0-23 or '*' for every hour.")

# ---------------------------------------------------------------------------
# Georegion mapping and query/result hooks
# ---------------------------------------------------------------------------

_regions = {
    'AF': ('DZ,AO,BJ,BW,BF,BI,CM,CV,CF,TD,KM,CD,CG,DJ,EG,GQ,ER,ET,GA,GM,GH,GN,'
           'GW,KE,LS,LR,LY,MW,MA,ML,MR,MU,MZ,NA,NE,NG,RW,SH,ST,SN,SC,SL,SO,ZA,'
           'SS,SD,SZ,TZ,TG,TN,UG,YE,ZM,ZW'),  # Africa
    'AS': ('AE,AM,AR,AU,AZ,BD,BH,BN,BT,MM,KH,KP,IN,ID,IR,IQ,IL,JP,JO,KZ,LV,'
           'LI,MY,MV,MN,NP,KR,PK,PH,QA,SA,SG,LK,SY,TW,TJ,TH,TR,TM,VN,YE,HK,'
           'CN,OM'),  # Asia
    'EU': ('AL,AD,AT,BY,BE,BA,BG,HR,CY,CZ,DK,EE,'
           'FI,FR,GE,DE,GR,HU,IS,IT,KZ,LV,LI,LT,'
           'LU,MT,MD,MC,ME,NL,NO,PL,PT,RO,RU,RS,'
           'SK,SI,ES,SE,CH,UA,GB,VA,MK'),  # Europe
    'LC': ('AG,AR,BS,BB,BZ,BO,BR,CL,CO,CR,CU,DO,EC,SV,GY,HT,HN,JM,MX,NI,PA,PY,'
           'PE,PR,RD,SUR,TT,UR,VZ'),  # Latin America and the Caribbean
    'NA': 'CA,US',  # Northern America
    'OC': ('AU,FJ,GU,KI,MH,FM,NR,NZ,PG,PW,SL,TO,TV,VU'),  # Oceania
}


def _reverse_mapping(regions):
    reversed_mapping = {}
    for region, countries in regions.items():
        for country in countries.split(','):
            reversed_mapping[country] = region
    return reversed_mapping


_regions_rev = _reverse_mapping(_regions)


def preprocess_search_query(query_str):
    """Pre-process a query string to expand georegion and chunked directives.

    If the query contains ``georegion=true``, any ``geolocation = <REGION_CODE>``
    clause is expanded into ``geolocation in [<country_list>]`` using :data:`_regions`.

    Both ``georegion=true`` and ``chunked=true`` are stripped from the query
    before it reaches the API.

    Returns:
        A ``(georegion_active, chunked_active, processed_query_str)`` tuple.
    """
    if query_str is None:
        return False, False, query_str

    from pyparsing import Word, alphas, alphanums, one_of, Group, ZeroOrMore

    key = Word(alphas + "_", alphanums + "_")
    operator = one_of("= in != > < >= <=")
    value = Word(alphanums + "_")
    expr = Group(key + operator + value)
    query = ZeroOrMore(expr)
    parsed = query.parse_string(query_str)

    directives = {'georegion', 'chunked'}
    state = {}
    for d in directives:
        state[d] = any([d, '=', 'true'] == list(e) for e in parsed)

    if not any(state.values()):
        return False, False, query_str

    parts = []
    for e in parsed:
        if e[0] in directives:
            continue
        elif e[0] == 'geolocation' and state['georegion'] and e[2] in _regions:
            parts.append(f'geolocation in [{_regions[e[2]]}]')
        else:
            parts.append(' '.join(e))

    return state['georegion'], state['chunked'], ' '.join(parts)


def postprocess_search_results(results, georegion_active=False, chunked=False):
    """Post-process search results with datacenter flag, georegion, and chunked filters.

    Always adds a ``datacenter`` boolean derived from ``hosting_type``.
    When *georegion_active*, appends continent codes to geolocation.
    When *chunked*, filters and rounds values for skypilot compatibility.
    """
    _chunked_cutoffs = {
        'cpu_ram': 64 * 1024,
        'cpu_cores': 32,
        'min_bid': 0,
    }

    filtered = []
    for res in results:
        res['datacenter'] = (res.get('hosting_type') == 1)

        if georegion_active:
            geo = res.get('geolocation', '')
            if geo:
                country = geo[-2:]
                region = _regions_rev.get(country)
                if region:
                    res['geolocation'] = f'{geo}, {region}'

        if chunked:
            good = True
            try:
                for k, v in _chunked_cutoffs.items():
                    if res.get(k) is not None and res[k] < v:
                        good = False
                    else:
                        res[k] = v
            except Exception:
                good = False
            if not good:
                continue
            res['gpu_ram'] = res.get('gpu_ram', 0) & 0xffffffffff0
            res['disk_space'] = int(res.get('disk_space', 0)) & 0xffffffffffc0

        filtered.append(res)

    return filtered
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