File size: 2,626 Bytes
3044426
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause

"""Resolve the named-volume mount points declared in ``docker-compose.yaml``.

``docker-compose.yaml`` is the single source of truth for the directories that
persist across containers. The non-root image build calls this module to
pre-create and ``chown`` each named-volume mount point to the runtime user, and
the regression test calls it to validate that parsing works. Keeping one parser
avoids a hand-maintained second list in the Dockerfiles drifting from compose.

A fresh Docker named volume inherits ownership from the image directory at its
mount path on first mount; if that directory is missing or root-owned, the
volume comes up root-owned and the non-root ``isaaclab`` user cannot write it.
"""

from __future__ import annotations

import os
import sys
from pathlib import Path

import yaml

# docker-compose.yaml lives one level up from this ``docker/utils`` package.
DEFAULT_COMPOSE = Path(__file__).resolve().parents[1] / "docker-compose.yaml"

# Top-level compose extension field holding the shared volume list.
_VOLUMES_KEY = "x-default-isaac-lab-volumes"


def named_volume_targets(compose_path: str | os.PathLike = DEFAULT_COMPOSE) -> list[str]:
    """Return the in-container target path of every ``type: volume`` mount.

    Args:
        compose_path: Path to the compose file to read.

    Returns:
        The target paths, with any ``${VAR}`` references left intact.
    """
    data = yaml.safe_load(Path(compose_path).read_text(encoding="utf-8"))
    mounts = data.get(_VOLUMES_KEY) or []
    return [m["target"] for m in mounts if m.get("type") == "volume"]


def resolved_targets(compose_path: str | os.PathLike = DEFAULT_COMPOSE) -> list[str]:
    """Like :func:`named_volume_targets` but with ``${VAR}`` expanded from the environment."""
    return [os.path.expandvars(t).rstrip("/") for t in named_volume_targets(compose_path)]


def main() -> int:
    """Print one resolved mount point per line; fail loudly on an empty or unresolved list."""
    targets = resolved_targets()
    unresolved = [t for t in targets if "$" in t]
    if unresolved:
        print(f"unresolved variables in volume targets: {unresolved}", file=sys.stderr)
        return 1
    if not targets:
        print(f"no named-volume targets found under '{_VOLUMES_KEY}' in docker-compose.yaml", file=sys.stderr)
        return 1
    print("\n".join(targets))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())