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
20 changes: 19 additions & 1 deletion src/dstack/_internal/cli/services/configurators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ def get_run_configurator_class(configurator_type: str) -> Type[BaseRunConfigurat
def load_apply_configuration(
configuration_file: Optional[str],
) -> Tuple[str, AnyApplyConfiguration]:
"""
Loads and parses the configuration for `dstack apply` and `dstack delete`.

Args:
configuration_file: The `-f` argument as passed by the user: a path to the configuration
file, absolute or relative to the current directory, or `-` to read from stdin.
If `None`, `.dstack.yml` or `.dstack.yaml` in the current directory is used.

Returns:
The configuration path and the parsed configuration. The configuration path is relative
to the current directory if the file is under it, and absolute otherwise; `-` for stdin.
It's shown to the user and sent to the server as part of the spec. Its parent directory
is the base for relative paths in the configuration, such as `repos` and `files`.
"""
if configuration_file is None:
configuration_path = Path.cwd() / ".dstack.yml"
if not configuration_path.exists():
Expand All @@ -85,4 +99,8 @@ def load_apply_configuration(
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
absolute_path = configuration_path.absolute()
cwd = Path.cwd()
if absolute_path.is_relative_to(cwd):
return str(absolute_path.relative_to(cwd)), conf
return str(absolute_path), conf
4 changes: 2 additions & 2 deletions src/dstack/_internal/cli/services/configurators/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def apply_configuration(

Args:
conf: The apply configuration.
configuration_path: The path to the configuration file.
configuration_path: The configuration path as returned by `load_apply_configuration`.
command_args: The args parsed by `dstack apply`.
configurator_args: The known args parsed by `cls.get_parser()`.
"""
Expand All @@ -56,7 +56,7 @@ def delete_configuration(

Args:
conf: The apply configuration.
configuration_path: The path to the configuration file.
configuration_path: The configuration path as returned by `load_apply_configuration`.
command_args: The args parsed by `dstack delete`.
"""
pass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from pathlib import Path

import pytest

from dstack._internal.cli.services.configurators import load_apply_configuration
from dstack._internal.core.errors import ConfigurationError
from dstack._internal.core.models.configurations import TaskConfiguration

CONFIGURATION_YAML = "type: task\ncommands:\n - echo hi\n"


class TestLoadApplyConfiguration:
@pytest.fixture
def cwd(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
cwd = tmp_path / "cwd"
cwd.mkdir()
monkeypatch.chdir(cwd)
return cwd

def test_returns_relative_path_for_configuration_under_cwd(self, cwd: Path):
_write_configuration(cwd / "conf" / "task.dstack.yml")

path, conf = load_apply_configuration(str(cwd / "conf" / "task.dstack.yml"))

assert path == str(Path("conf") / "task.dstack.yml")
assert isinstance(conf, TaskConfiguration)

def test_returns_absolute_path_for_configuration_outside_cwd(self, cwd: Path):
configuration_path = _write_configuration(cwd.parent / "conf" / "task.dstack.yml")

path, conf = load_apply_configuration(str(configuration_path))

assert path == str(configuration_path)
assert isinstance(conf, TaskConfiguration)

def test_keeps_parent_segments_of_relative_path(self, cwd: Path):
_write_configuration(cwd.parent / "conf" / "task.dstack.yml")
configuration_file = str(Path("..") / "conf" / "task.dstack.yml")

path, _ = load_apply_configuration(configuration_file)

assert path == configuration_file

def test_loads_default_configuration_from_cwd(self, cwd: Path):
_write_configuration(cwd / ".dstack.yml")

path, conf = load_apply_configuration(None)

assert path == ".dstack.yml"
assert isinstance(conf, TaskConfiguration)

def test_raises_if_configuration_does_not_exist(self, cwd: Path):
with pytest.raises(ConfigurationError):
load_apply_configuration(str(cwd / "missing.dstack.yml"))


def _write_configuration(path: Path) -> Path:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(CONFIGURATION_YAML)
return path
Loading