From 54a6fd14958d19701dcfba907701c92dd74ab0c8 Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Mon, 21 Apr 2025 12:08:21 +0200 Subject: [PATCH 1/8] [Feature]: Support `dstack offer` #2142 (initial version) --- src/dstack/_internal/cli/commands/offer.py | 199 ++++++++++++++++++ src/dstack/_internal/cli/main.py | 2 + .../cli/services/configurators/run.py | 4 +- src/dstack/_internal/server/routers/runs.py | 1 + src/dstack/_internal/server/schemas/runs.py | 1 + src/dstack/_internal/server/services/runs.py | 3 +- src/dstack/api/server/_runs.py | 4 +- 7 files changed, 209 insertions(+), 5 deletions(-) create mode 100644 src/dstack/_internal/cli/commands/offer.py diff --git a/src/dstack/_internal/cli/commands/offer.py b/src/dstack/_internal/cli/commands/offer.py new file mode 100644 index 0000000000..d2f74506ee --- /dev/null +++ b/src/dstack/_internal/cli/commands/offer.py @@ -0,0 +1,199 @@ +import argparse +import json +from pathlib import Path + +from rich.table import Table + +from dstack._internal.cli.commands import APIBaseCommand +from dstack._internal.cli.services.configurators.run import ( + BaseRunConfigurator, +) +from dstack._internal.cli.utils.common import console +from dstack._internal.core.models.configurations import ( + ApplyConfigurationType, + TaskConfiguration, +) +from dstack._internal.core.models.instances import InstanceAvailability +from dstack._internal.core.models.runs import RunSpec +from dstack.api.utils import load_profile + + +class OfferConfigurator(BaseRunConfigurator): + # TODO: The command currently uses `BaseRunConfigurator` to register arguments. + # This includes --env, --retry-policy, and other arguments that are unnecessary for this command. + # Eventually, we should introduce a base `OfferConfigurator` that doesn't include those arguments— + # `BaseRunConfigurator` will inherit from `OfferConfigurator`. + # + # Additionally, it should have its own type: `ApplyConfigurationType.OFFER`. + TYPE = ApplyConfigurationType.TASK + + @classmethod + def register_args( + cls, + parser: argparse.ArgumentParser, + ): + super().register_args(parser, default_max_offers=50) + + +# TODO: Support aggregated offers +# TODO: Add tests +class OfferCommand(APIBaseCommand): + NAME = "offer" + DESCRIPTION = "List offers" + + def _register(self): + super()._register() + self._parser.add_argument( + "--format", + choices=["plain", "json"], + default="plain", + help="Output format (default: plain)", + ) + self._parser.add_argument( + "--json", + action="store_const", + const="json", + dest="format", + help="Output in JSON format (equivalent to --format json)", + ) + OfferConfigurator.register_args(self._parser) + + def _command(self, args: argparse.Namespace): + super()._command(args) + conf = TaskConfiguration(commands=[":"]) + + configurator = OfferConfigurator(api_client=self.api) + configurator.apply_args(conf, args, []) + profile = load_profile(Path.cwd(), profile_name=args.profile) + + if args.format == "json": + run_spec = RunSpec( + configuration=conf, + ssh_key_pub=Path(self.api.ssh_identity_file + ".pub").read_text().strip(), + profile=profile, + ) + run_plan = self.api.client.runs.get_plan( + self.api.project, + run_spec, + max_offers=args.max_offers, + ) + else: + with console.status("Getting offers..."): + run_spec = RunSpec( + configuration=conf, + ssh_key_pub=Path(self.api.ssh_identity_file + ".pub").read_text().strip(), + profile=profile, + ) + run_plan = self.api.client.runs.get_plan( + self.api.project, + run_spec, + max_offers=args.max_offers, + ) + + job_plan = run_plan.job_plans[0] + + if args.format == "json": + output = { + "project": run_plan.project_name, + "user": run_plan.user, + "resources": job_plan.job_spec.requirements.resources.dict(), + "max_price": (job_plan.job_spec.requirements.max_price), + "spot": run_spec.configuration.spot_policy, + "reservation": run_plan.run_spec.configuration.reservation, + "offers": [], + } + + for offer in job_plan.offers: + availability = None + if offer.availability in { + InstanceAvailability.NOT_AVAILABLE, + InstanceAvailability.NO_QUOTA, + InstanceAvailability.IDLE, + InstanceAvailability.BUSY, + }: + availability = offer.availability.value + + output["offers"].append( + { + "backend": ( + "ssh" if offer.backend.value == "remote" else offer.backend.value + ), + "region": offer.region, + "resources": offer.instance.resources.dict(), + "spot": offer.instance.resources.spot, + "price": float(offer.price), + "availability": availability, + } + ) + + print(json.dumps(output, indent=2)) + return + + props = Table(box=None, show_header=False) + props.add_column(no_wrap=True) # key + props.add_column() # value + + req = job_plan.job_spec.requirements + pretty_req = req.pretty_format(resources_only=True) + max_price = f"${req.max_price:g}" if req.max_price else "-" + profile = run_plan.run_spec.merged_profile + if req.spot is None: + spot_policy = "auto" + elif req.spot: + spot_policy = "spot" + else: + spot_policy = "on-demand" + + def th(s: str) -> str: + return f"[bold]{s}[/bold]" + + props.add_row(th("Project"), run_plan.project_name) + props.add_row(th("User"), run_plan.user) + props.add_row(th("Resources"), pretty_req) + props.add_row(th("Max price"), max_price) + props.add_row(th("Spot policy"), spot_policy) + props.add_row(th("Reservation"), run_plan.run_spec.configuration.reservation or "-") + console.print(props) + console.print() + + table = Table(box=None) + table.add_column("#") + table.add_column("BACKEND") + table.add_column("REGION") + table.add_column("RESOURCES") + table.add_column("SPOT") + table.add_column("PRICE", justify="right") + table.add_column("") + + offers = run_plan.job_plans[0].offers + for i, offer in enumerate(offers, 1): + price = f"${offer.price:.3f}" + availability = "" + if offer.availability in { + InstanceAvailability.NOT_AVAILABLE, + InstanceAvailability.NO_QUOTA, + InstanceAvailability.IDLE, + InstanceAvailability.BUSY, + }: + availability = offer.availability.value.replace("_", " ").lower() + + # TODO: rename `remote` to `ssh` everywhere + table.add_row( + str(i), + "ssh" if offer.backend.value == "remote" else offer.backend.value, + offer.region, + offer.instance.resources.pretty_format(), + "yes" if offer.instance.resources.spot else "no", + price, + availability, + ) + + if job_plan.total_offers > len(job_plan.offers): + table.add_row("", "...", style="secondary") + console.print(table) + if job_plan.total_offers > len(job_plan.offers): + console.print( + f"[secondary]Shown {len(job_plan.offers)} of {job_plan.total_offers} offers, " + f"${job_plan.max_price:g} max[/]" + ) + console.print() diff --git a/src/dstack/_internal/cli/main.py b/src/dstack/_internal/cli/main.py index 8155e4d75a..79b1d5bd73 100644 --- a/src/dstack/_internal/cli/main.py +++ b/src/dstack/_internal/cli/main.py @@ -14,6 +14,7 @@ from dstack._internal.cli.commands.init import InitCommand from dstack._internal.cli.commands.logs import LogsCommand from dstack._internal.cli.commands.metrics import MetricsCommand +from dstack._internal.cli.commands.offer import OfferCommand from dstack._internal.cli.commands.ps import PsCommand from dstack._internal.cli.commands.server import ServerCommand from dstack._internal.cli.commands.stats import StatsCommand @@ -65,6 +66,7 @@ def main(): FleetCommand.register(subparsers) GatewayCommand.register(subparsers) InitCommand.register(subparsers) + OfferCommand.register(subparsers) LogsCommand.register(subparsers) MetricsCommand.register(subparsers) PsCommand.register(subparsers) diff --git a/src/dstack/_internal/cli/services/configurators/run.py b/src/dstack/_internal/cli/services/configurators/run.py index c00e38c1eb..abb628bfe4 100644 --- a/src/dstack/_internal/cli/services/configurators/run.py +++ b/src/dstack/_internal/cli/services/configurators/run.py @@ -274,7 +274,7 @@ def delete_configuration( console.print(f"Run [code]{conf.name}[/] deleted") @classmethod - def register_args(cls, parser: argparse.ArgumentParser): + def register_args(cls, parser: argparse.ArgumentParser, default_max_offers: int = 3): configuration_group = parser.add_argument_group(f"{cls.TYPE.value} Options") configuration_group.add_argument( "-n", @@ -286,7 +286,7 @@ def register_args(cls, parser: argparse.ArgumentParser): "--max-offers", help="Number of offers to show in the run plan", type=int, - default=3, + default=default_max_offers, ) cls.register_env_args(configuration_group) configuration_group.add_argument( diff --git a/src/dstack/_internal/server/routers/runs.py b/src/dstack/_internal/server/routers/runs.py index ff5fff70f6..c6a4b60f80 100644 --- a/src/dstack/_internal/server/routers/runs.py +++ b/src/dstack/_internal/server/routers/runs.py @@ -100,6 +100,7 @@ async def get_plan( project=project, user=user, run_spec=body.run_spec, + max_offers=body.max_offers, ) return run_plan diff --git a/src/dstack/_internal/server/schemas/runs.py b/src/dstack/_internal/server/schemas/runs.py index 38c55ddda8..193fc6f7d4 100644 --- a/src/dstack/_internal/server/schemas/runs.py +++ b/src/dstack/_internal/server/schemas/runs.py @@ -26,6 +26,7 @@ class GetRunRequest(CoreModel): class GetRunPlanRequest(CoreModel): run_spec: RunSpec + max_offers: int = Field(50, ge=1, le=1000) class SubmitRunRequest(CoreModel): diff --git a/src/dstack/_internal/server/services/runs.py b/src/dstack/_internal/server/services/runs.py index 12ceaf92f1..4bfd69e69d 100644 --- a/src/dstack/_internal/server/services/runs.py +++ b/src/dstack/_internal/server/services/runs.py @@ -275,6 +275,7 @@ async def get_plan( project: ProjectModel, user: UserModel, run_spec: RunSpec, + max_offers: int = 50, ) -> RunPlan: _validate_run_spec_and_set_defaults(run_spec) @@ -342,7 +343,7 @@ async def get_plan( job_plan = JobPlan( job_spec=job_spec, - offers=job_offers[:50], + offers=job_offers[:max_offers], total_offers=len(job_offers), max_price=max((offer.price for offer in job_offers), default=None), ) diff --git a/src/dstack/api/server/_runs.py b/src/dstack/api/server/_runs.py index cdd128e6e1..7c7d888482 100644 --- a/src/dstack/api/server/_runs.py +++ b/src/dstack/api/server/_runs.py @@ -53,8 +53,8 @@ def get(self, project_name: str, run_name: str) -> Run: resp = self._request(f"/api/project/{project_name}/runs/get", body=json_body) return parse_obj_as(Run.__response__, resp.json()) - def get_plan(self, project_name: str, run_spec: RunSpec) -> RunPlan: - body = GetRunPlanRequest(run_spec=run_spec) + def get_plan(self, project_name: str, run_spec: RunSpec, max_offers: int = 50) -> RunPlan: + body = GetRunPlanRequest(run_spec=run_spec, max_offers=max_offers) resp = self._request( f"/api/project/{project_name}/runs/get_plan", body=body.json(exclude=_get_run_spec_excludes(run_spec)), From 657873aa78a21e95a25816e3152d983476367868 Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Mon, 21 Apr 2025 15:00:30 +0200 Subject: [PATCH 2/8] [Feature]: Support `dstack offer` #2142 (old server back-compatibility) --- src/dstack/_internal/server/schemas/runs.py | 2 +- src/dstack/_internal/server/services/runs.py | 6 ++++-- src/dstack/api/server/_runs.py | 17 +++++++++++++++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/dstack/_internal/server/schemas/runs.py b/src/dstack/_internal/server/schemas/runs.py index 193fc6f7d4..6210670ba1 100644 --- a/src/dstack/_internal/server/schemas/runs.py +++ b/src/dstack/_internal/server/schemas/runs.py @@ -26,7 +26,7 @@ class GetRunRequest(CoreModel): class GetRunPlanRequest(CoreModel): run_spec: RunSpec - max_offers: int = Field(50, ge=1, le=1000) + max_offers: Optional[int] = Field(description="The maximum number of offers to return") class SubmitRunRequest(CoreModel): diff --git a/src/dstack/_internal/server/services/runs.py b/src/dstack/_internal/server/services/runs.py index 4bfd69e69d..a2cb645a65 100644 --- a/src/dstack/_internal/server/services/runs.py +++ b/src/dstack/_internal/server/services/runs.py @@ -92,6 +92,8 @@ JobTerminationReason.FAILED_TO_START_DUE_TO_NO_CAPACITY, } +DEFAULT_MAX_OFFERS = 50 + async def list_user_runs( session: AsyncSession, @@ -275,7 +277,7 @@ async def get_plan( project: ProjectModel, user: UserModel, run_spec: RunSpec, - max_offers: int = 50, + max_offers: Optional[int], ) -> RunPlan: _validate_run_spec_and_set_defaults(run_spec) @@ -343,7 +345,7 @@ async def get_plan( job_plan = JobPlan( job_spec=job_spec, - offers=job_offers[:max_offers], + offers=job_offers[: (max_offers or DEFAULT_MAX_OFFERS)], total_offers=len(job_offers), max_price=max((offer.price for offer in job_offers), default=None), ) diff --git a/src/dstack/api/server/_runs.py b/src/dstack/api/server/_runs.py index 7c7d888482..a3d44782b5 100644 --- a/src/dstack/api/server/_runs.py +++ b/src/dstack/api/server/_runs.py @@ -53,11 +53,13 @@ def get(self, project_name: str, run_name: str) -> Run: resp = self._request(f"/api/project/{project_name}/runs/get", body=json_body) return parse_obj_as(Run.__response__, resp.json()) - def get_plan(self, project_name: str, run_spec: RunSpec, max_offers: int = 50) -> RunPlan: + def get_plan( + self, project_name: str, run_spec: RunSpec, max_offers: Optional[int] = None + ) -> RunPlan: body = GetRunPlanRequest(run_spec=run_spec, max_offers=max_offers) resp = self._request( f"/api/project/{project_name}/runs/get_plan", - body=body.json(exclude=_get_run_spec_excludes(run_spec)), + body=body.json(exclude=_get_get_plan_excludes(body)), ) return parse_obj_as(RunPlan.__response__, resp.json()) @@ -96,6 +98,17 @@ def _get_apply_plan_excludes(plan: ApplyRunPlanInput) -> Optional[Dict]: return None +def _get_get_plan_excludes(request: GetRunPlanRequest) -> Optional[Dict]: + """ + Excludes new fields when they are not set to keep + clients backward-compatibility with older servers. + """ + run_spec_excludes = _get_run_spec_excludes(request.run_spec) + if request.max_offers is None: + run_spec_excludes["max_offers"] = True + return run_spec_excludes + + def _get_run_spec_excludes(run_spec: RunSpec) -> Optional[Dict]: """ Returns `run_spec` exclude mapping to exclude certain fields from the request. From b6963d43ef20ab3387a3d6f7e78f2c9ffe25497f Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Mon, 21 Apr 2025 15:10:55 +0200 Subject: [PATCH 3/8] [Feature]: Support dstack offer #2142 (use dummy values for public SSH key) --- src/dstack/_internal/cli/commands/offer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dstack/_internal/cli/commands/offer.py b/src/dstack/_internal/cli/commands/offer.py index d2f74506ee..044eafe7cb 100644 --- a/src/dstack/_internal/cli/commands/offer.py +++ b/src/dstack/_internal/cli/commands/offer.py @@ -69,7 +69,7 @@ def _command(self, args: argparse.Namespace): if args.format == "json": run_spec = RunSpec( configuration=conf, - ssh_key_pub=Path(self.api.ssh_identity_file + ".pub").read_text().strip(), + ssh_key_pub="(dummy)", profile=profile, ) run_plan = self.api.client.runs.get_plan( @@ -81,7 +81,7 @@ def _command(self, args: argparse.Namespace): with console.status("Getting offers..."): run_spec = RunSpec( configuration=conf, - ssh_key_pub=Path(self.api.ssh_identity_file + ".pub").read_text().strip(), + ssh_key_pub="(dummy)", profile=profile, ) run_plan = self.api.client.runs.get_plan( From 4cfe275bc2e76dce87966e1c3c546aa8abc77f57 Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Mon, 21 Apr 2025 16:11:18 +0200 Subject: [PATCH 4/8] [Feature]: Support `dstack offer` #2142 (removed repeated code; added `total_offers` in JSON) --- src/dstack/_internal/cli/commands/offer.py | 41 +++++++--------------- 1 file changed, 13 insertions(+), 28 deletions(-) diff --git a/src/dstack/_internal/cli/commands/offer.py b/src/dstack/_internal/cli/commands/offer.py index 044eafe7cb..e5f53092d6 100644 --- a/src/dstack/_internal/cli/commands/offer.py +++ b/src/dstack/_internal/cli/commands/offer.py @@ -1,4 +1,5 @@ import argparse +import contextlib import json from pathlib import Path @@ -66,29 +67,21 @@ def _command(self, args: argparse.Namespace): configurator.apply_args(conf, args, []) profile = load_profile(Path.cwd(), profile_name=args.profile) - if args.format == "json": - run_spec = RunSpec( - configuration=conf, - ssh_key_pub="(dummy)", - profile=profile, - ) + run_spec = RunSpec( + configuration=conf, + ssh_key_pub="(dummy)", + profile=profile, + ) + if args.format == "plain": + status = console.status("Getting offers...") + else: + status = contextlib.nullcontext() + with status: run_plan = self.api.client.runs.get_plan( self.api.project, run_spec, max_offers=args.max_offers, ) - else: - with console.status("Getting offers..."): - run_spec = RunSpec( - configuration=conf, - ssh_key_pub="(dummy)", - profile=profile, - ) - run_plan = self.api.client.runs.get_plan( - self.api.project, - run_spec, - max_offers=args.max_offers, - ) job_plan = run_plan.job_plans[0] @@ -101,18 +94,10 @@ def _command(self, args: argparse.Namespace): "spot": run_spec.configuration.spot_policy, "reservation": run_plan.run_spec.configuration.reservation, "offers": [], + "total_offers": job_plan.total_offers, } for offer in job_plan.offers: - availability = None - if offer.availability in { - InstanceAvailability.NOT_AVAILABLE, - InstanceAvailability.NO_QUOTA, - InstanceAvailability.IDLE, - InstanceAvailability.BUSY, - }: - availability = offer.availability.value - output["offers"].append( { "backend": ( @@ -122,7 +107,7 @@ def _command(self, args: argparse.Namespace): "resources": offer.instance.resources.dict(), "spot": offer.instance.resources.spot, "price": float(offer.price), - "availability": availability, + "availability": offer.availability.value, } ) From 94e865d77d949611adb7e24cafd3ce3f58a0706e Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Mon, 21 Apr 2025 17:45:47 +0200 Subject: [PATCH 5/8] [Feature]: Support dstack offer #2142 (reuse printing offers code from `dstack apply`) --- src/dstack/_internal/cli/commands/offer.py | 73 +------------------ .../cli/services/configurators/run.py | 2 +- src/dstack/_internal/cli/utils/run.py | 69 ++++++++++-------- 3 files changed, 40 insertions(+), 104 deletions(-) diff --git a/src/dstack/_internal/cli/commands/offer.py b/src/dstack/_internal/cli/commands/offer.py index e5f53092d6..53dd00e36f 100644 --- a/src/dstack/_internal/cli/commands/offer.py +++ b/src/dstack/_internal/cli/commands/offer.py @@ -3,18 +3,16 @@ import json from pathlib import Path -from rich.table import Table - from dstack._internal.cli.commands import APIBaseCommand from dstack._internal.cli.services.configurators.run import ( BaseRunConfigurator, ) from dstack._internal.cli.utils.common import console +from dstack._internal.cli.utils.run import print_run_plan from dstack._internal.core.models.configurations import ( ApplyConfigurationType, TaskConfiguration, ) -from dstack._internal.core.models.instances import InstanceAvailability from dstack._internal.core.models.runs import RunSpec from dstack.api.utils import load_profile @@ -113,72 +111,5 @@ def _command(self, args: argparse.Namespace): print(json.dumps(output, indent=2)) return - - props = Table(box=None, show_header=False) - props.add_column(no_wrap=True) # key - props.add_column() # value - - req = job_plan.job_spec.requirements - pretty_req = req.pretty_format(resources_only=True) - max_price = f"${req.max_price:g}" if req.max_price else "-" - profile = run_plan.run_spec.merged_profile - if req.spot is None: - spot_policy = "auto" - elif req.spot: - spot_policy = "spot" else: - spot_policy = "on-demand" - - def th(s: str) -> str: - return f"[bold]{s}[/bold]" - - props.add_row(th("Project"), run_plan.project_name) - props.add_row(th("User"), run_plan.user) - props.add_row(th("Resources"), pretty_req) - props.add_row(th("Max price"), max_price) - props.add_row(th("Spot policy"), spot_policy) - props.add_row(th("Reservation"), run_plan.run_spec.configuration.reservation or "-") - console.print(props) - console.print() - - table = Table(box=None) - table.add_column("#") - table.add_column("BACKEND") - table.add_column("REGION") - table.add_column("RESOURCES") - table.add_column("SPOT") - table.add_column("PRICE", justify="right") - table.add_column("") - - offers = run_plan.job_plans[0].offers - for i, offer in enumerate(offers, 1): - price = f"${offer.price:.3f}" - availability = "" - if offer.availability in { - InstanceAvailability.NOT_AVAILABLE, - InstanceAvailability.NO_QUOTA, - InstanceAvailability.IDLE, - InstanceAvailability.BUSY, - }: - availability = offer.availability.value.replace("_", " ").lower() - - # TODO: rename `remote` to `ssh` everywhere - table.add_row( - str(i), - "ssh" if offer.backend.value == "remote" else offer.backend.value, - offer.region, - offer.instance.resources.pretty_format(), - "yes" if offer.instance.resources.spot else "no", - price, - availability, - ) - - if job_plan.total_offers > len(job_plan.offers): - table.add_row("", "...", style="secondary") - console.print(table) - if job_plan.total_offers > len(job_plan.offers): - console.print( - f"[secondary]Shown {len(job_plan.offers)} of {job_plan.total_offers} offers, " - f"${job_plan.max_price:g} max[/]" - ) - console.print() + print_run_plan(run_plan, include_run_properties=False) diff --git a/src/dstack/_internal/cli/services/configurators/run.py b/src/dstack/_internal/cli/services/configurators/run.py index abb628bfe4..66007c565d 100644 --- a/src/dstack/_internal/cli/services/configurators/run.py +++ b/src/dstack/_internal/cli/services/configurators/run.py @@ -92,7 +92,7 @@ def apply_configuration( profile=profile, ) - print_run_plan(run_plan, offers_limit=configurator_args.max_offers) + print_run_plan(run_plan, max_offers=configurator_args.max_offers) confirm_message = "Submit a new run?" stop_run_name = None diff --git a/src/dstack/_internal/cli/utils/run.py b/src/dstack/_internal/cli/utils/run.py index 6f5d19177b..f750db3619 100644 --- a/src/dstack/_internal/cli/utils/run.py +++ b/src/dstack/_internal/cli/utils/run.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List, Union +from typing import Any, Dict, List, Optional, Union from rich.markup import escape from rich.table import Table @@ -24,7 +24,9 @@ from dstack.api import Run -def print_run_plan(run_plan: RunPlan, offers_limit: int = 3): +def print_run_plan( + run_plan: RunPlan, max_offers: Optional[int] = None, include_run_properties: bool = True +): job_plan = run_plan.job_plans[0] props = Table(box=None, show_header=False) @@ -39,29 +41,30 @@ def print_run_plan(run_plan: RunPlan, offers_limit: int = 3): if job_plan.job_spec.max_duration else "-" ) - inactivity_duration = None - if isinstance(run_plan.run_spec.configuration, DevEnvironmentConfiguration): - inactivity_duration = "-" - if isinstance(run_plan.run_spec.configuration.inactivity_duration, int): - inactivity_duration = format_pretty_duration( - run_plan.run_spec.configuration.inactivity_duration - ) - if job_plan.job_spec.retry is None: - retry = "-" - else: - retry = escape(job_plan.job_spec.retry.pretty_format()) - - profile = run_plan.run_spec.merged_profile - creation_policy = profile.creation_policy - # FIXME: This assumes the default idle_duration is the same for client and server. - # If the server changes idle_duration, old clients will see incorrect value. - termination_policy, termination_idle_time = get_termination( - profile, DEFAULT_RUN_TERMINATION_IDLE_TIME - ) - if termination_policy == TerminationPolicy.DONT_DESTROY: - idle_duration = "-" - else: - idle_duration = format_pretty_duration(termination_idle_time) + if include_run_properties: + inactivity_duration = None + if isinstance(run_plan.run_spec.configuration, DevEnvironmentConfiguration): + inactivity_duration = "-" + if isinstance(run_plan.run_spec.configuration.inactivity_duration, int): + inactivity_duration = format_pretty_duration( + run_plan.run_spec.configuration.inactivity_duration + ) + if job_plan.job_spec.retry is None: + retry = "-" + else: + retry = escape(job_plan.job_spec.retry.pretty_format()) + + profile = run_plan.run_spec.merged_profile + creation_policy = profile.creation_policy + # FIXME: This assumes the default idle_duration is the same for client and server. + # If the server changes idle_duration, old clients will see incorrect value. + termination_policy, termination_idle_time = get_termination( + profile, DEFAULT_RUN_TERMINATION_IDLE_TIME + ) + if termination_policy == TerminationPolicy.DONT_DESTROY: + idle_duration = "-" + else: + idle_duration = format_pretty_duration(termination_idle_time) if req.spot is None: spot_policy = "auto" @@ -76,29 +79,31 @@ def th(s: str) -> str: props.add_row(th("Project"), run_plan.project_name) props.add_row(th("User"), run_plan.user) props.add_row(th("Configuration"), run_plan.run_spec.configuration_path) - props.add_row(th("Type"), run_plan.run_spec.configuration.type) + if include_run_properties: + props.add_row(th("Type"), run_plan.run_spec.configuration.type) props.add_row(th("Resources"), pretty_req) props.add_row(th("Max price"), max_price) props.add_row(th("Max duration"), max_duration) - if inactivity_duration is not None: # None means n/a + if include_run_properties and inactivity_duration is not None: # None means n/a props.add_row(th("Inactivity duration"), inactivity_duration) props.add_row(th("Spot policy"), spot_policy) - props.add_row(th("Retry policy"), retry) - props.add_row(th("Creation policy"), creation_policy) - props.add_row(th("Idle duration"), idle_duration) + if include_run_properties: + props.add_row(th("Retry policy"), retry) + props.add_row(th("Creation policy"), creation_policy) + props.add_row(th("Idle duration"), idle_duration) props.add_row(th("Reservation"), run_plan.run_spec.configuration.reservation or "-") offers = Table(box=None) offers.add_column("#") offers.add_column("BACKEND") offers.add_column("REGION") - offers.add_column("INSTANCE") + offers.add_column("INSTANCE TYPE") offers.add_column("RESOURCES") offers.add_column("SPOT") offers.add_column("PRICE") offers.add_column() - job_plan.offers = job_plan.offers[:offers_limit] + job_plan.offers = job_plan.offers[:max_offers] if max_offers else job_plan.offers for i, offer in enumerate(job_plan.offers, start=1): r = offer.instance.resources From dcce08766e7b4792da751b48ad7de8b32d13e3cc Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Tue, 22 Apr 2025 10:34:10 +0200 Subject: [PATCH 6/8] [Feature]: Support dstack offer #2142 (add `instance_type` to JSON; added limits for `max_offers`) --- src/dstack/_internal/cli/commands/offer.py | 1 + src/dstack/_internal/server/schemas/runs.py | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/dstack/_internal/cli/commands/offer.py b/src/dstack/_internal/cli/commands/offer.py index 53dd00e36f..92b54e2b12 100644 --- a/src/dstack/_internal/cli/commands/offer.py +++ b/src/dstack/_internal/cli/commands/offer.py @@ -102,6 +102,7 @@ def _command(self, args: argparse.Namespace): "ssh" if offer.backend.value == "remote" else offer.backend.value ), "region": offer.region, + "instance_type": offer.instance.name, "resources": offer.instance.resources.dict(), "spot": offer.instance.resources.spot, "price": float(offer.price), diff --git a/src/dstack/_internal/server/schemas/runs.py b/src/dstack/_internal/server/schemas/runs.py index 6210670ba1..8ae875df09 100644 --- a/src/dstack/_internal/server/schemas/runs.py +++ b/src/dstack/_internal/server/schemas/runs.py @@ -26,7 +26,9 @@ class GetRunRequest(CoreModel): class GetRunPlanRequest(CoreModel): run_spec: RunSpec - max_offers: Optional[int] = Field(description="The maximum number of offers to return") + max_offers: Optional[int] = Field( + description="The maximum number of offers to return", ge=1, le=10000 + ) class SubmitRunRequest(CoreModel): From 7e63b25c56032002b2ccef1bbea45dd2e3072235 Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Tue, 22 Apr 2025 11:14:05 +0200 Subject: [PATCH 7/8] [Feature]: Support `dstack offer` #2142 ( https://github.com/dstackai/gpuhunt/issues/134) --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 69f8604df7..8e00b5c4a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -32,7 +32,7 @@ dependencies = [ "python-multipart>=0.0.16", "filelock", "psutil", - "gpuhunt>=0.1.2,<0.2.0", + "gpuhunt>=0.1.3,<0.2.0", "argcomplete>=3.5.0", ] From 0da7223c9eafa8fc95b676d924960acc4d5281db Mon Sep 17 00:00:00 2001 From: peterschmidt85 Date: Tue, 22 Apr 2025 12:25:06 +0200 Subject: [PATCH 8/8] [Feature]: Support `dstack offer` #2142 (updated output) --- src/dstack/_internal/cli/utils/run.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/dstack/_internal/cli/utils/run.py b/src/dstack/_internal/cli/utils/run.py index f750db3619..638ff7976c 100644 --- a/src/dstack/_internal/cli/utils/run.py +++ b/src/dstack/_internal/cli/utils/run.py @@ -78,19 +78,19 @@ def th(s: str) -> str: props.add_row(th("Project"), run_plan.project_name) props.add_row(th("User"), run_plan.user) - props.add_row(th("Configuration"), run_plan.run_spec.configuration_path) if include_run_properties: + props.add_row(th("Configuration"), run_plan.run_spec.configuration_path) props.add_row(th("Type"), run_plan.run_spec.configuration.type) props.add_row(th("Resources"), pretty_req) - props.add_row(th("Max price"), max_price) - props.add_row(th("Max duration"), max_duration) - if include_run_properties and inactivity_duration is not None: # None means n/a - props.add_row(th("Inactivity duration"), inactivity_duration) props.add_row(th("Spot policy"), spot_policy) + props.add_row(th("Max price"), max_price) if include_run_properties: props.add_row(th("Retry policy"), retry) props.add_row(th("Creation policy"), creation_policy) props.add_row(th("Idle duration"), idle_duration) + props.add_row(th("Max duration"), max_duration) + if inactivity_duration is not None: # None means n/a + props.add_row(th("Inactivity duration"), inactivity_duration) props.add_row(th("Reservation"), run_plan.run_spec.configuration.reservation or "-") offers = Table(box=None)