Skip to content

Commit 796cbd9

Browse files
committed
feat(env): Only allow bind mounts with -v
For more complex usecases like using named volumes or weird flags, users should use `--mount`, which will be implemented next.
1 parent 3d8e58f commit 796cbd9

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/dda/env/dev/types/linux_container.py

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,30 @@ class LinuxContainerConfig(DeveloperEnvironmentConfig):
6666
] = None
6767
# This parameter stores the raw volume specifications as provided by the user.
6868
# Use the `extra_mounts` property to get the list of extra mounts as Mount objects.
69-
extra_mount_specs: Annotated[
69+
extra_volume_specs: Annotated[
7070
list[str],
7171
msgspec.Meta(
7272
extra={
7373
"params": ["-v", "--volume"],
7474
"help": (
7575
"""\
76-
Additional directories or volumes to be mounted into the dev env. This option may be supplied multiple
76+
Additional host directories to be mounted into the dev env. This option may be supplied multiple
7777
times, and has the same syntax as the `-v/--volume` flag of `docker run`. Examples:
7878
7979
- `./some-repo:/root/repos/some-repo` (bind mount from relative path on host to container)
8080
- `/tmp/some-location:/location:ro` (bind mount from absolute path on host to container with read-only flag)
81-
- `~/projects:/root/projects:ro,z` (bind mount from absolute path on host to container with read-only flag and `z` volume option)
82-
- `some-volume:/location` (volume mount from named volume to container)
81+
- `~/projects:/root/projects:ro` (bind mount from absolute path on host to container with read-only flag)
82+
83+
84+
> **WARNING:**
85+
> This option only supports a subset of the syntax for the `-v/--volume` flag of `docker run`, and is provided only as a convenience.
86+
>
87+
> In particular, only bind mounts are supported, not volume mounts. Additionally, only the `ro` flag is supported.
88+
>
89+
> To mount a volume or use other options, use the more explicit `-m/--mount` option instead.
8390
"""
8491
),
85-
"callback": __validate_extra_mount_specs,
92+
"callback": __validate_extra_volume_specs,
8693
}
8794
),
8895
] = msgspec.field(default_factory=list)
@@ -403,7 +410,7 @@ def extra_mounts(self) -> list[Mount]:
403410
from dda.utils.container.model import Mount
404411

405412
mounts = []
406-
for spec in self.config.extra_mount_specs:
413+
for spec in self.config.extra_volume_specs:
407414
src, dst, *extra = spec.split(":", 2)
408415
flags = extra[0].split(",") if extra else []
409416
read_only = "ro" in flags
@@ -450,7 +457,7 @@ def repo_path(self, repo: str | None) -> str:
450457
return f"{self.home_dir}/repos/{repo}"
451458

452459

453-
def __validate_extra_mount_specs(_ctx: Context, _param: Option, value: list[str]) -> list[str]:
460+
def __validate_extra_volume_specs(_ctx: Context, _param: Option, value: list[str]) -> list[str]:
454461
from click import BadParameter
455462

456463
from dda.utils.fs import Path
@@ -459,13 +466,16 @@ def __validate_extra_mount_specs(_ctx: Context, _param: Option, value: list[str]
459466
return value
460467

461468
for spec in value:
462-
src, dst, *_ = spec.split(":", 2)
463-
mount_type: Literal["bind", "volume"] = "bind" if src.startswith(("/", ".")) else "volume"
464-
if mount_type == "bind" and not Path(src).exists():
465-
msg = f"Invalid mount source: {spec}. Source must be an existing path on the host."
469+
src, dst, *extra = spec.split(":", 2)
470+
flag = extra[0] if extra else None
471+
if flag not in {None, "ro", "rw"}:
472+
msg = f"Invalid volume flag: {flag}. Only the `ro` flag is supported."
473+
raise BadParameter(msg)
474+
if not Path(src).exists():
475+
# NOTE: We have here a slight discrepancy with the behavior of `docker run -v`, which will create the directory if it doesn't exist.
476+
msg = f"Invalid volume source: {src}. Source must be an existing path on the host."
466477
raise BadParameter(msg)
467-
468478
if not Path(dst).is_absolute():
469-
msg = f"Invalid mount destination: {spec}. Destination must be an absolute path."
479+
msg = f"Invalid volume destination: {dst}. Destination must be an absolute path."
470480
raise BadParameter(msg)
471481
return value

tests/env/dev/types/test_linux_container.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_default_config(app):
9090
"no_pull": False,
9191
"repos": ["datadog-agent"],
9292
"shell": "zsh",
93-
"extra_mount_specs": [],
93+
"extra_volume_specs": [],
9494
}
9595

9696

@@ -657,7 +657,7 @@ def test_extra_bind_mounts(self, dda, helpers, mocker, temp_dir, host_user_args,
657657

658658
# Disable argument validation for the extra mount specs
659659
mocker.patch(
660-
"dda.env.dev.types.linux_container.__validate_extra_mount_specs",
660+
"dda.env.dev.types.linux_container.__validate_extra_volume_specs",
661661
return_value=volume_specs,
662662
)
663663
shared_dir = temp_dir / "data" / "env" / "dev" / "linux-container" / ".shared"

0 commit comments

Comments
 (0)