Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/dstack/_internal/cli/commands/apply.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import argparse
from pathlib import Path

from argcomplete import FilesCompleter

from dstack._internal.cli.commands import APIBaseCommand
from dstack._internal.cli.services.configurators import (
APPLY_STDIN_NAME,
get_apply_configurator_class,
load_apply_configuration,
)
Expand Down Expand Up @@ -40,9 +40,12 @@ def _register(self):
self._parser.add_argument(
"-f",
"--file",
type=Path,
metavar="FILE",
help="The path to the configuration file. Defaults to [code]$PWD/.dstack.yml[/]",
help=(
"The path to the configuration file."
" Specify [code]-[/] to read configuration from stdin."
" Defaults to [code]$PWD/.dstack.yml[/]"
),
dest="configuration_file",
).completer = FilesCompleter(allowednames=["*.yml", "*.yaml"])
self._parser.add_argument(
Expand Down Expand Up @@ -104,6 +107,8 @@ def _command(self, args: argparse.Namespace):
return

super()._command(args)
if not args.yes and args.configuration_file == APPLY_STDIN_NAME:
raise CLIError("Cannot read configuration from stdin if -y/--yes is not specified")
if args.repo and args.no_repo:
raise CLIError("Either --repo or --no-repo can be specified")
repo = None
Expand Down
8 changes: 8 additions & 0 deletions src/dstack/_internal/cli/services/configurators/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from pathlib import Path
from typing import Dict, Optional, Tuple, Type

Expand All @@ -20,6 +21,9 @@
parse_apply_configuration,
)

APPLY_STDIN_NAME = "-"


apply_configurators_mapping: Dict[ApplyConfigurationType, Type[BaseApplyConfigurator]] = {
cls.TYPE: cls
for cls in [
Expand Down Expand Up @@ -62,6 +66,8 @@ def load_apply_configuration(
raise ConfigurationError(
"No configuration file specified via `-f` and no default .dstack.yml configuration found"
)
elif configuration_file == APPLY_STDIN_NAME:
configuration_path = sys.stdin.fileno()
else:
configuration_path = Path(configuration_file)
if not configuration_path.exists():
Expand All @@ -71,4 +77,6 @@ def load_apply_configuration(
conf = parse_apply_configuration(yaml.safe_load(f))
except OSError:
raise ConfigurationError(f"Failed to load configuration from {configuration_path}")
if isinstance(configuration_path, int):
return APPLY_STDIN_NAME, conf
return str(configuration_path.absolute().relative_to(Path.cwd())), conf
2 changes: 1 addition & 1 deletion src/dstack/_internal/cli/services/configurators/fleet.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def _apply_plan(self, plan: FleetPlan, command_args: argparse.Namespace):
time.sleep(LIVE_TABLE_PROVISION_INTERVAL_SECS)
fleet = self.api.client.fleets.get(self.api.project, fleet.name)
except KeyboardInterrupt:
if confirm_ask("Delete the fleet before exiting?"):
if not command_args.yes and confirm_ask("Delete the fleet before exiting?"):
with console.status("Deleting fleet..."):
self.api.client.fleets.delete(
project_name=self.api.project, names=[fleet.name]
Expand Down
2 changes: 1 addition & 1 deletion src/dstack/_internal/cli/services/configurators/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def apply_configuration(
time.sleep(LIVE_TABLE_PROVISION_INTERVAL_SECS)
gateway = self.api.client.gateways.get(self.api.project, gateway.name)
except KeyboardInterrupt:
if confirm_ask("Delete the gateway before exiting?"):
if not command_args.yes and confirm_ask("Delete the gateway before exiting?"):
with console.status("Deleting gateway..."):
self.api.client.gateways.delete(
project_name=self.api.project,
Expand Down
4 changes: 3 additions & 1 deletion src/dstack/_internal/cli/services/configurators/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ def apply_configuration(
exit(1)
except KeyboardInterrupt:
try:
if not confirm_ask(f"\nStop the run [code]{run.name}[/] before detaching?"):
if command_args.yes or not confirm_ask(
f"\nStop the run [code]{run.name}[/] before detaching?"
):
console.print("Detached")
abort_at_exit = False
return
Expand Down
2 changes: 1 addition & 1 deletion src/dstack/_internal/cli/services/configurators/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def apply_configuration(
time.sleep(LIVE_TABLE_PROVISION_INTERVAL_SECS)
volume = self.api.client.volumes.get(self.api.project, volume.name)
except KeyboardInterrupt:
if confirm_ask("Delete the volume before exiting?"):
if not command_args.yes and confirm_ask("Delete the volume before exiting?"):
with console.status("Deleting volume..."):
self.api.client.volumes.delete(
project_name=self.api.project, names=[volume.name]
Expand Down
Loading