rostainer-package 0.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,236 @@
1
+ """Packages: deployable images of a workspace, for robots.
2
+
3
+ ``rostainer package create`` builds a runtime image with the workspace
4
+ baked in and built, saves it, and assembles a bundle that needs only
5
+ Docker (and Python) on the target: the image, a runtime Compose file, its
6
+ settings, and ``./rostainer``, which installs and runs the package there
7
+ without rostainer (``assets/runtime_cli.py``; :func:`runtime_cli` loads it
8
+ here). ``rostainer package install DIR`` runs a bundle's ``./rostainer
9
+ install``. See :mod:`rostainer_package.create`.
10
+
11
+ Its settings are the ``[plugins.package]`` table (:class:`PackageConfig`);
12
+ the image is also built from the workspace's own ones that dev containers
13
+ use (``command``, ``extra_dirs``, ``environment``, devices, ...).
14
+ """
15
+
16
+ from __future__ import annotations
17
+
18
+ import argparse
19
+ import functools
20
+ import importlib.util
21
+ import subprocess
22
+ import sys
23
+ from pathlib import Path
24
+ from types import ModuleType
25
+ from typing import List, Optional, Sequence
26
+
27
+ from pydantic import Field, field_validator
28
+ from typing_extensions import Literal
29
+
30
+ from rostainer_cli.plugin_api import (
31
+ Command,
32
+ Context,
33
+ HostScript,
34
+ Plugin,
35
+ PluginConfig,
36
+ host_script,
37
+ log,
38
+ )
39
+
40
+ #: Values of ``build_type`` (CMake's ``CMAKE_BUILD_TYPE``).
41
+ CMAKE_BUILD_TYPES = ("Release", "RelWithDebInfo", "Debug", "MinSizeRel")
42
+
43
+ #: Where the plugin's own files (Dockerfile stages, runtime_cli.py, ...) are.
44
+ ASSETS_DIR = Path(__file__).resolve().parent / "assets"
45
+
46
+ #: A package's own ``rostainer``: :data:`RUNTIME_CLI_PATH`, as it's named
47
+ #: in a bundle.
48
+ RUNTIME_CLI_NAME = "rostainer"
49
+ RUNTIME_CLI_PATH = ASSETS_DIR / "runtime_cli.py"
50
+
51
+
52
+ @functools.lru_cache(maxsize=None)
53
+ def runtime_cli() -> ModuleType:
54
+ """The module a bundle ships as ``./rostainer``, loaded from its file:
55
+ it's standalone on the target, so it isn't part of this package."""
56
+ spec = importlib.util.spec_from_file_location("rostainer_package_runtime_cli", RUNTIME_CLI_PATH)
57
+ assert spec is not None and spec.loader is not None
58
+ module = importlib.util.module_from_spec(spec)
59
+ spec.loader.exec_module(module)
60
+ return module
61
+
62
+
63
+ class PackageConfig(PluginConfig):
64
+ """The ``[plugins.package]`` table."""
65
+
66
+ build_type: Literal[CMAKE_BUILD_TYPES] = Field( # type: ignore[valid-type]
67
+ "Release",
68
+ description="CMAKE_BUILD_TYPE of the packaged build: Release, RelWithDebInfo, Debug, MinSizeRel.",
69
+ )
70
+ sudo: bool = Field(
71
+ False,
72
+ description=(
73
+ "Packages' container user has no sudo rights (only the build does); set to\n"
74
+ "true if something in the running container needs sudo."
75
+ ),
76
+ )
77
+ healthcheck: str = Field(
78
+ "",
79
+ description=(
80
+ "Health check for the runtime command: a shell command run in the container's\n"
81
+ "ROS environment, e.g. \"ros2 node list | grep -q /my_driver\". While it keeps\n"
82
+ "failing, the container is restarted. Empty: none."
83
+ ),
84
+ )
85
+ healthcheck_interval: int = Field(30, ge=1, description="Seconds between checks.")
86
+ healthcheck_retries: int = Field(
87
+ 3, ge=1, description="Failures in a row before restarting."
88
+ )
89
+ healthcheck_start_period: int = Field(
90
+ 60, ge=0, description="Seconds after start before checking."
91
+ )
92
+ host_pre_script: HostScript = Field(
93
+ "",
94
+ description=(
95
+ "Runs on the HOST before 'package create' copies the source into the image,\n"
96
+ "like host_pre_start_script (often the same script). A path, or one per\n"
97
+ "host platform: { default = \"pre.sh\", windows = \"pre.ps1\" }. Empty: none."
98
+ ),
99
+ )
100
+ dependencies_first: bool = Field(
101
+ True,
102
+ description=(
103
+ "Install dependencies (rosdep, pip) from the package.xml and requirements.txt\n"
104
+ "files before the rest of the source is copied in, so a change to the source\n"
105
+ "alone rebuilds - and ships - only the workspace's own layers. Set to false if\n"
106
+ "the pre-build script needs the whole source."
107
+ ),
108
+ )
109
+ keep: int = Field(
110
+ 3,
111
+ ge=0,
112
+ description="How many of the newest package images and bundles 'rostainer prune' keeps.",
113
+ )
114
+ offline: Literal["auto", "true", "false"] = Field(
115
+ "true",
116
+ description=(
117
+ "Whether the package's starts are offline on its targets (ROSTAINER_OFFLINE),\n"
118
+ "apart from this machine's ('offline' above is for dev containers here):\n"
119
+ "true/false: always/never, with nothing checked on start (the package\n"
120
+ 'installs nothing there); "auto" checks offline_check_url on each start.'
121
+ ),
122
+ )
123
+ offline_check_url: str = Field(
124
+ "",
125
+ description=(
126
+ 'What "auto" checks, from the targets, e.g. a server on their network\n'
127
+ "(empty: PyPI, i.e. the internet)."
128
+ ),
129
+ )
130
+ offline_check_timeout: int = Field(
131
+ 3, ge=1, description='How many seconds "auto" waits before calling a start offline.'
132
+ )
133
+
134
+ @field_validator("offline", mode="before")
135
+ @classmethod
136
+ def _offline_bool(cls, value: object) -> object:
137
+ # As the workspace's 'offline': TOML booleans too.
138
+ if isinstance(value, bool):
139
+ return "true" if value else "false"
140
+ return value
141
+
142
+ @field_validator("offline_check_url")
143
+ @classmethod
144
+ def _check_url(cls, value: str) -> str:
145
+ if value and not value.startswith(("http://", "https://")):
146
+ raise ValueError(f"must be an http:// or https:// URL, got {value!r}")
147
+ return value
148
+
149
+ @field_validator("host_pre_script", mode="before")
150
+ @classmethod
151
+ def _host_script(cls, value: object) -> object:
152
+ return host_script(value)
153
+
154
+
155
+ def restart_policy(ctx: Context) -> str:
156
+ """A package's restart policy: the workspace's ``restart_policy``, else
157
+ ``"always"``: packages start at boot and restart when their command
158
+ exits ('./rostainer install --enable docker' and the health check rely on it)."""
159
+ return ctx.cfg.restart_policy or "always"
160
+
161
+
162
+ class PackageCommand(Command):
163
+ """``rostainer package <create|install>``."""
164
+
165
+ name = "package"
166
+ summary = "Build a deployable runtime image ('package create') and install it ('package install')"
167
+
168
+ def add_arguments(self, parser: argparse.ArgumentParser) -> None:
169
+ actions = parser.add_subparsers(dest="action", metavar="ACTION")
170
+ actions.required = True
171
+ from rostainer_package import create
172
+
173
+ create.add_arguments(
174
+ actions.add_parser(
175
+ "create",
176
+ help="Build a deployable runtime image with the workspace baked in",
177
+ description="Build a deployable runtime image with the workspace baked in.",
178
+ )
179
+ )
180
+ install = actions.add_parser(
181
+ "install",
182
+ help="Install a package bundle here: its './rostainer install' (on a target, "
183
+ "run that directly: it needs no rostainer)",
184
+ description="Run PACKAGE_DIR/rostainer install with the given options. It "
185
+ "needs only Docker and Python: on a target machine, run "
186
+ "'PACKAGE_DIR/rostainer install' directly.",
187
+ )
188
+ install.add_argument("package_dir", metavar="PACKAGE_DIR")
189
+ install.add_argument(
190
+ "install_args",
191
+ nargs=argparse.REMAINDER,
192
+ help="Its options, e.g. --start, --enable systemd, --install-dir DIR",
193
+ )
194
+
195
+ def run(self, args: argparse.Namespace, ctx: Optional[Context]) -> int:
196
+ assert ctx is not None
197
+ if args.action == "create":
198
+ from rostainer_package import create
199
+
200
+ return create.run(args, ctx)
201
+ return install(Path(args.package_dir), args.install_args)
202
+
203
+
204
+ def install(package_dir: Path, install_args: Sequence[str]) -> int:
205
+ """Run ``package_dir``'s ``./rostainer install`` with ``install_args``."""
206
+ script = package_dir / RUNTIME_CLI_NAME
207
+ if not script.is_file():
208
+ log.error(f"'{package_dir}' isn't a package bundle: no {RUNTIME_CLI_NAME} in it.")
209
+ return 1
210
+ # Through sys.executable, not the script's own shebang: Windows has no
211
+ # shebang support at all.
212
+ return subprocess.run([sys.executable, str(script), "install", *install_args]).returncode
213
+
214
+
215
+ class PackagePlugin(Plugin):
216
+ """Deployable images of the workspace, for robots."""
217
+
218
+ name = "package"
219
+ api_version = 1
220
+ summary = "Deployable images of the workspace, for robots"
221
+ config_model = PackageConfig
222
+
223
+ def commands(self) -> Sequence[Command]:
224
+ return [PackageCommand()]
225
+
226
+ def prunable_images(self, ctx: Context, removed: Sequence[str]) -> List[str]:
227
+ """This workspace's package images, except the ``keep`` newest."""
228
+ from rostainer_package import create
229
+
230
+ return create.package_images(ctx)[ctx.config.keep :]
231
+
232
+ def prunable_paths(self, ctx: Context) -> List[Path]:
233
+ """This workspace's bundles in ``builds/package/``, except the ``keep`` newest."""
234
+ from rostainer_package import create
235
+
236
+ return create.package_bundles(ctx)[ctx.config.keep :]
@@ -0,0 +1,87 @@
1
+ # Runtime Docker Compose file for packaged deployments.
2
+ #
3
+ # Unlike docker-compose.yml, this has no 'build:' section - it only ever
4
+ # runs an image that was already built by 'rostainer package create' and
5
+ # loaded with 'docker load'. It must never try to pull or build
6
+ # 'ros:${ROS_DISTRO}-ros-base'.
7
+ x-shared-environment: &shared-environment
8
+ RUNTIME_COMMAND: ${RUNTIME_COMMAND:-}
9
+ USER_UID: ${USER_UID:-1000}
10
+ USER_GID: ${USER_GID:-1000}
11
+ USERNAME: ${USERNAME:-rosuser}
12
+ ROS_LOG_MAX_SIZE_MB: ${ROS_LOG_MAX_SIZE_MB:-1024}
13
+ ROS_LOG_MAX_AGE_DAYS: ${ROS_LOG_MAX_AGE_DAYS:-30}
14
+ # Optional watchdog (see runtime_entrypoint.supervise).
15
+ RUNTIME_HEALTHCHECK: ${RUNTIME_HEALTHCHECK:-}
16
+ RUNTIME_HEALTHCHECK_INTERVAL: ${RUNTIME_HEALTHCHECK_INTERVAL:-30}
17
+ RUNTIME_HEALTHCHECK_RETRIES: ${RUNTIME_HEALTHCHECK_RETRIES:-3}
18
+ RUNTIME_HEALTHCHECK_START_PERIOD: ${RUNTIME_HEALTHCHECK_START_PERIOD:-60}
19
+ # Whether a start is offline ('offline' in .rostainer.toml), for
20
+ # ROSTAINER_OFFLINE (see runtime_entrypoint.detect_offline).
21
+ ROSTAINER_OFFLINE_SETTING: ${ROSTAINER_OFFLINE_SETTING:-true}
22
+ ROSTAINER_OFFLINE_CHECK_URL: ${ROSTAINER_OFFLINE_CHECK_URL:-}
23
+ ROSTAINER_OFFLINE_CHECK_TIMEOUT: ${ROSTAINER_OFFLINE_CHECK_TIMEOUT:-}
24
+ # ROS 2 discovery (see 'ros_discovery_range' in .rostainer.toml).
25
+ ROS_AUTOMATIC_DISCOVERY_RANGE: ${ROS_AUTOMATIC_DISCOVERY_RANGE:-SUBNET}
26
+ ROS_STATIC_PEERS: ${ROS_STATIC_PEERS:-}
27
+ ROS_DOMAIN_ID: ${ROS_DOMAIN_ID:-0}
28
+ # Humble's equivalent of ROS_AUTOMATIC_DISCOVERY_RANGE=LOCALHOST.
29
+ ROS_LOCALHOST_ONLY: ${ROS_LOCALHOST_ONLY:-}
30
+ # Docker keeps a container's stdout/stderr (everything 'docker logs' shows)
31
+ # without any limit by default, which on a long-running deployment
32
+ # eventually fills the disk.
33
+ x-shared-logging: &shared-logging
34
+ driver: json-file
35
+ options:
36
+ max-size: ${DOCKER_LOG_MAX_SIZE:-10m}
37
+ max-file: "${DOCKER_LOG_MAX_FILES:-5}"
38
+ # Shows the watchdog's state in 'docker ps'. Restarting is the
39
+ # entrypoint's job: Docker never restarts an unhealthy container itself.
40
+ x-shared-healthcheck: &shared-healthcheck
41
+ test: ["CMD-SHELL", "[ ! -f /tmp/.runtime_unhealthy ]"]
42
+ interval: 10s
43
+ timeout: 5s
44
+ retries: 1
45
+
46
+ services:
47
+ ros1_runtime:
48
+ image: ${ROS_DOCKER_IMAGE_TAG}
49
+ container_name: ${ROS1_CONTAINER_NAME:-ros1_container}
50
+ profiles:
51
+ - ros1
52
+ tty: true
53
+ stdin_open: true
54
+ # Root at first: the entrypoint gives the image's user USER_UID/USER_GID
55
+ # (this machine's user's, from './rostainer install'), then runs as it.
56
+ user: "0:0"
57
+ environment:
58
+ <<: *shared-environment
59
+ network_mode: ${NETWORK_MODE:-host}
60
+ # 'restart_policy' in .rostainer.toml; packages restart by default.
61
+ restart: ${RESTART_POLICY:-always}
62
+ healthcheck: *shared-healthcheck
63
+ logging: *shared-logging
64
+ # A real PID 1 that forwards SIGTERM (the runtime command, or 'tail -f
65
+ # /dev/null', may ignore it as PID 1) and reaps the background pruner.
66
+ init: true
67
+
68
+ ros2_runtime:
69
+ image: ${ROS_DOCKER_IMAGE_TAG}
70
+ container_name: ${ROS2_CONTAINER_NAME:-ros2_container}
71
+ profiles:
72
+ - ros2
73
+ tty: true
74
+ stdin_open: true
75
+ # Root at first: the entrypoint gives the image's user USER_UID/USER_GID
76
+ # (this machine's user's, from './rostainer install'), then runs as it.
77
+ user: "0:0"
78
+ environment:
79
+ <<: *shared-environment
80
+ network_mode: ${NETWORK_MODE:-host}
81
+ # 'restart_policy' in .rostainer.toml; packages restart by default.
82
+ restart: ${RESTART_POLICY:-always}
83
+ healthcheck: *shared-healthcheck
84
+ logging: *shared-logging
85
+ # A real PID 1 that forwards SIGTERM (the runtime command, or 'tail -f
86
+ # /dev/null', may ignore it as PID 1) and reaps the background pruner.
87
+ init: true
@@ -0,0 +1,131 @@
1
+ # Appended to rostainer's own ros1.Dockerfile by 'rostainer package create'
2
+ # (see rostainer_package.create._stage_build_context), so it builds on its
3
+ # 'system' stage -- the same layers, and build cache, as the dev image.
4
+
5
+ # --- Runtime stage ---
6
+ # Used by 'rostainer package create' to build a deployable image with the
7
+ # workspace baked in and built at image-build time (no bind mount, no
8
+ # rebuild on container start). Not used by 'rostainer start'/'shell'.
9
+ FROM system AS runtime
10
+
11
+ ARG USER_UID=1000
12
+ ARG USER_GID=1000
13
+ ARG USERNAME=rosuser
14
+
15
+ # ROS's setup.bash relies on bash-specific syntax, so RUN needs bash, not the
16
+ # default sh, to 'source' it.
17
+ SHELL ["/bin/bash", "-c"]
18
+
19
+ # Layer order: what changes least comes first, so a change to the source
20
+ # alone rebuilds - and a registry ships - only the last few, small layers:
21
+ # extra dirs -> manifests (package.xml, requirements.txt) -> pre-build
22
+ # script + rosdep -> pip -> the source -> the build.
23
+ # 'rostainer package create' stages .package_manifests with just the files
24
+ # the dependencies are installed from (or the whole source, with
25
+ # '[plugins.package] dependencies_first = false').
26
+
27
+ # extra_dirs from .rostainer.toml, staged by 'rostainer package create' and
28
+ # installed at their container paths by extra_dirs.py (as root, since a
29
+ # target can be anywhere; see that script for why this isn't a plain COPY).
30
+ COPY extra_dirs.py ros_log_pruner.py pip_requirements.py /usr/local/lib/rostainer/
31
+ COPY .package_extra_dirs/ /tmp/rostainer_extra_dirs/
32
+ RUN python3 /usr/local/lib/rostainer/extra_dirs.py install /tmp/rostainer_extra_dirs ${USER_UID} ${USER_GID} && \
33
+ rm -rf /tmp/rostainer_extra_dirs
34
+
35
+ # sudo rights for the build only (the pre-build script and rosdep install
36
+ # with them); taken away again below unless package_sudo is set.
37
+ COPY pre_build.sh /usr/local/bin/pre_build.sh
38
+ # The workspace's plugins' files, for their pre-build hooks (only those:
39
+ # the rest are copied in last, below).
40
+ COPY .package_plugins_pre_build/ /usr/local/lib/rostainer/plugins/
41
+ RUN chmod 755 /usr/local/bin/pre_build.sh && \
42
+ echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/rostainer && \
43
+ chmod 440 /etc/sudoers.d/rostainer && \
44
+ mkdir -p /usr/local/share/rostainer && \
45
+ chown ${USER_UID}:${USER_GID} /usr/local/share/rostainer
46
+ USER ${USERNAME}
47
+
48
+ COPY --chown=${USER_UID}:${USER_GID} .package_manifests/ src/
49
+
50
+ # The same pre-build script and rosdep install the dev container runs, so
51
+ # the image has every system dependency the workspace needs. The script
52
+ # gets the variables it does in dev: a new image is a first start (so steps
53
+ # gated on it run), and the build is online (it downloads anyway). One step, so
54
+ # the apt lists they need don't stay in the image.
55
+ ARG ROSDEP_INSTALL=true
56
+ ARG ROSDEP_SKIP_KEYS=""
57
+ RUN set -e; \
58
+ sudo apt-get update; \
59
+ export ROSTAINER_FIRST_START=1 ROSTAINER_OFFLINE=0 ROSTAINER_PACKAGE=1; \
60
+ if [ -f /usr/local/lib/rostainer/plugins/pre_build.sh ]; then \
61
+ /usr/local/lib/rostainer/plugins/pre_build.sh; \
62
+ fi; \
63
+ /usr/local/bin/pre_build.sh; \
64
+ unset ROSTAINER_FIRST_START ROSTAINER_OFFLINE ROSTAINER_PACKAGE; \
65
+ if [ "${ROSDEP_INSTALL}" = "true" ]; then \
66
+ rosdep update --rosdistro "${ROS_DISTRO}"; \
67
+ rosdep install --from-paths src --ignore-src -y --rosdistro "${ROS_DISTRO}" \
68
+ ${ROSDEP_SKIP_KEYS:+--skip-keys "${ROSDEP_SKIP_KEYS}"}; \
69
+ fi; \
70
+ sudo rm -rf /var/lib/apt/lists/*
71
+
72
+ # Python packages: the requirements files - here, before the source,
73
+ # unless one installs from the source itself (PIP_BEFORE_SOURCE=false;
74
+ # then after it, below). pip_requirements.py merges the files into one,
75
+ # installs it with one pip install and runs 'pip check' (kept as
76
+ # /usr/local/share/rostainer/requirements.txt). Every step must succeed: a
77
+ # missing or conflicting Python dependency would otherwise only show up on
78
+ # the robot, as a crash. Python
79
+ # packages are held to the versions in rostainer.lock (a layer of its own:
80
+ # it changes after a first build).
81
+ ARG PIP_INSTALL_REQUIREMENTS=true
82
+ ARG ROSTAINER_PIP_OPTIONS=
83
+ ARG PIP_BEFORE_SOURCE=true
84
+ COPY pip-constraints.txt /usr/local/lib/rostainer/
85
+ RUN set -e; \
86
+ export PIP_CONSTRAINT=/usr/local/lib/rostainer/pip-constraints.txt; \
87
+ if [ "${PIP_BEFORE_SOURCE}" = "true" ]; then \
88
+ python3 /usr/local/lib/rostainer/pip_requirements.py install --output /usr/local/share/rostainer/requirements.txt -- --no-cache-dir; \
89
+ fi
90
+
91
+ COPY --chown=${USER_UID}:${USER_GID} .package_workspace_src/ src/
92
+
93
+ # Release by default: a package is what runs on the robot. build/ is a
94
+ # BuildKit cache, kept on the building machine between packages (never in
95
+ # the image), so a source change rebuilds only what it touches, as in dev:
96
+ # one per workspace, target, build type and set of dependencies (see
97
+ # rostainer_package.create.build_cache_id); CLEAN_BUILD=true empties it
98
+ # first. devel/, which the package runs from, is made anew each time:
99
+ # --force-cmake regenerates its setup files, and make relinks what's
100
+ # missing there. Last, a record of what went in.
101
+ ARG CMAKE_BUILD_TYPE=Release
102
+ ARG BUILD_CACHE_ID=rostainer-catkin-build
103
+ ARG CLEAN_BUILD=false
104
+ RUN --mount=type=cache,id=${BUILD_CACHE_ID},target=/home/${USERNAME}/ros_ws/build,uid=${USER_UID},gid=${USER_GID},sharing=locked \
105
+ set -e; \
106
+ export PIP_CONSTRAINT=/usr/local/lib/rostainer/pip-constraints.txt; \
107
+ if [ "${PIP_BEFORE_SOURCE}" != "true" ]; then \
108
+ python3 /usr/local/lib/rostainer/pip_requirements.py install --output /usr/local/share/rostainer/requirements.txt -- --no-cache-dir; \
109
+ fi; \
110
+ if [ "${CLEAN_BUILD}" = "true" ]; then find build -mindepth 1 -delete; fi; \
111
+ source /opt/ros/${ROS_DISTRO}/setup.bash; \
112
+ catkin_make --force-cmake -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}; \
113
+ test -f devel/setup.bash; \
114
+ pip3 freeze --all --user > /usr/local/share/rostainer/pip.txt; \
115
+ dpkg-query -W -f='${Package}=${Version}\n' > /usr/local/share/rostainer/dpkg.txt
116
+
117
+ # The post-build script runs on every start, before the runtime command, and
118
+ # './rostainer shell' sources the shell setup script, as in dev containers -
119
+ # each after the plugins' hooks for it. Last, so changing any of them only
120
+ # changes these small layers.
121
+ ARG PACKAGE_SUDO=false
122
+ USER root
123
+ RUN rm -rf /usr/local/lib/rostainer/plugins
124
+ COPY .package_plugins/ /usr/local/lib/rostainer/plugins/
125
+ COPY runtime_entrypoint.py post_build.sh shell_setup.sh /usr/local/bin/
126
+ RUN chmod 755 /usr/local/bin/runtime_entrypoint.py /usr/local/bin/post_build.sh \
127
+ /usr/local/bin/shell_setup.sh && \
128
+ if [ "${PACKAGE_SUDO}" != "true" ]; then rm /etc/sudoers.d/rostainer; fi
129
+ USER ${USERNAME}
130
+
131
+ ENTRYPOINT ["runtime_entrypoint.py"]
@@ -0,0 +1,137 @@
1
+ # Appended to rostainer's own ros2.Dockerfile by 'rostainer package create'
2
+ # (see rostainer_package.create._stage_build_context), so it builds on its
3
+ # 'system' stage -- the same layers, and build cache, as the dev image.
4
+
5
+ # --- Runtime stage ---
6
+ # Used by 'rostainer package create' to build a deployable image with the
7
+ # workspace baked in and built at image-build time (no bind mount, no
8
+ # rebuild on container start). Not used by 'rostainer start'/'shell'.
9
+ FROM system AS runtime
10
+
11
+ ARG USER_UID=1000
12
+ ARG USER_GID=1000
13
+ ARG USERNAME=rosuser
14
+
15
+ # ROS's setup.bash relies on bash-specific syntax, so RUN needs bash, not the
16
+ # default sh, to 'source' it.
17
+ SHELL ["/bin/bash", "-c"]
18
+
19
+ # Layer order: what changes least comes first, so a change to the source
20
+ # alone rebuilds - and a registry ships - only the last few, small layers:
21
+ # extra dirs -> manifests (package.xml, requirements.txt) -> pre-build
22
+ # script + rosdep -> pip -> the source -> the build.
23
+ # 'rostainer package create' stages .package_manifests with just the files
24
+ # the dependencies are installed from (or the whole source, with
25
+ # '[plugins.package] dependencies_first = false').
26
+
27
+ # extra_dirs from .rostainer.toml, staged by 'rostainer package create' and
28
+ # installed at their container paths by extra_dirs.py (as root, since a
29
+ # target can be anywhere; see that script for why this isn't a plain COPY).
30
+ COPY extra_dirs.py ros_log_pruner.py pip_requirements.py /usr/local/lib/rostainer/
31
+ COPY .package_extra_dirs/ /tmp/rostainer_extra_dirs/
32
+ RUN python3 /usr/local/lib/rostainer/extra_dirs.py install /tmp/rostainer_extra_dirs ${USER_UID} ${USER_GID} && \
33
+ rm -rf /tmp/rostainer_extra_dirs
34
+
35
+ # sudo rights for the build only (the pre-build script and rosdep install
36
+ # with them); taken away again below unless package_sudo is set.
37
+ COPY pre_build.sh /usr/local/bin/pre_build.sh
38
+ # The workspace's plugins' files, for their pre-build hooks (only those:
39
+ # the rest are copied in last, below).
40
+ COPY .package_plugins_pre_build/ /usr/local/lib/rostainer/plugins/
41
+ RUN chmod 755 /usr/local/bin/pre_build.sh && \
42
+ echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/rostainer && \
43
+ chmod 440 /etc/sudoers.d/rostainer && \
44
+ mkdir -p /usr/local/share/rostainer && \
45
+ chown ${USER_UID}:${USER_GID} /usr/local/share/rostainer
46
+ USER ${USERNAME}
47
+
48
+ COPY --chown=${USER_UID}:${USER_GID} .package_manifests/ src/
49
+
50
+ # The same pre-build script and rosdep install the dev container runs, so
51
+ # the image has every system dependency the workspace needs. The script
52
+ # gets the variables it does in dev: a new image is a first start (so steps
53
+ # gated on it run), and the build is online (it downloads anyway). One step, so
54
+ # the apt lists they need don't stay in the image.
55
+ ARG ROSDEP_INSTALL=true
56
+ ARG ROSDEP_SKIP_KEYS=""
57
+ RUN set -e; \
58
+ sudo apt-get update; \
59
+ export ROSTAINER_FIRST_START=1 ROSTAINER_OFFLINE=0 ROSTAINER_PACKAGE=1; \
60
+ if [ -f /usr/local/lib/rostainer/plugins/pre_build.sh ]; then \
61
+ /usr/local/lib/rostainer/plugins/pre_build.sh; \
62
+ fi; \
63
+ /usr/local/bin/pre_build.sh; \
64
+ unset ROSTAINER_FIRST_START ROSTAINER_OFFLINE ROSTAINER_PACKAGE; \
65
+ if [ "${ROSDEP_INSTALL}" = "true" ]; then \
66
+ rosdep update --rosdistro "${ROS_DISTRO}"; \
67
+ rosdep install --from-paths src --ignore-src -y --rosdistro "${ROS_DISTRO}" \
68
+ ${ROSDEP_SKIP_KEYS:+--skip-keys "${ROSDEP_SKIP_KEYS}"}; \
69
+ fi; \
70
+ sudo rm -rf /var/lib/apt/lists/*
71
+
72
+ # Python packages: pip, the tools colcon's packages expect, and the
73
+ # requirements files - here, before the source, unless one installs from
74
+ # the source itself (PIP_BEFORE_SOURCE=false; then after it, below).
75
+ # pip_requirements.py merges the files into one, installs it with one pip
76
+ # install and runs 'pip check' (kept as /usr/local/share/rostainer/
77
+ # requirements.txt). Every step must succeed: a missing or conflicting
78
+ # Python dependency would otherwise only show up on the robot, as a crash. Python packages are held to the versions
79
+ # in rostainer.lock (a layer of its own: it changes after a first build).
80
+ ARG PIP_INSTALL_REQUIREMENTS=true
81
+ ARG ROSTAINER_PIP_OPTIONS=
82
+ ARG PIP_BEFORE_SOURCE=true
83
+ COPY pip-constraints.txt /usr/local/lib/rostainer/
84
+ RUN set -e; \
85
+ export PIP_CONSTRAINT=/usr/local/lib/rostainer/pip-constraints.txt; \
86
+ sudo mkdir -p /opt/venv; \
87
+ sudo chown -R ${USER_UID}:${USER_GID} /opt/venv; \
88
+ python3 -m venv --system-site-packages /opt/venv; \
89
+ source /opt/venv/bin/activate; \
90
+ pip install --no-cache-dir --upgrade pip; \
91
+ pip install --no-cache-dir pytest empy psutil lark argcomplete; \
92
+ if [ "${PIP_BEFORE_SOURCE}" = "true" ]; then \
93
+ python3 /usr/local/lib/rostainer/pip_requirements.py install --output /usr/local/share/rostainer/requirements.txt -- --no-cache-dir; \
94
+ fi
95
+
96
+ COPY --chown=${USER_UID}:${USER_GID} .package_workspace_src/ src/
97
+
98
+ # Release by default: a package is what runs on the robot. build/ is a
99
+ # BuildKit cache, kept on the building machine between packages (never in
100
+ # the image), so a source change rebuilds only what it touches, as in dev:
101
+ # one per workspace, target, build type and set of dependencies (see
102
+ # rostainer_package.create.build_cache_id); CLEAN_BUILD=true empties it
103
+ # first. log/ isn't needed to run, and goes in the same step so it never
104
+ # reaches the image. Last, a record of what went in.
105
+ ARG CMAKE_BUILD_TYPE=Release
106
+ ARG BUILD_CACHE_ID=rostainer-colcon-build
107
+ ARG CLEAN_BUILD=false
108
+ RUN --mount=type=cache,id=${BUILD_CACHE_ID},target=/home/${USERNAME}/ros_ws/build,uid=${USER_UID},gid=${USER_GID},sharing=locked \
109
+ set -e; \
110
+ export PIP_CONSTRAINT=/usr/local/lib/rostainer/pip-constraints.txt; \
111
+ source /opt/venv/bin/activate; \
112
+ if [ "${PIP_BEFORE_SOURCE}" != "true" ]; then \
113
+ python3 /usr/local/lib/rostainer/pip_requirements.py install --output /usr/local/share/rostainer/requirements.txt -- --no-cache-dir; \
114
+ fi; \
115
+ if [ "${CLEAN_BUILD}" = "true" ]; then find build -mindepth 1 -delete; fi; \
116
+ source /opt/ros/${ROS_DISTRO}/setup.bash; \
117
+ colcon build --cmake-args -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}; \
118
+ test -f install/setup.bash; \
119
+ rm -rf log; \
120
+ pip freeze --all --local > /usr/local/share/rostainer/pip.txt; \
121
+ dpkg-query -W -f='${Package}=${Version}\n' > /usr/local/share/rostainer/dpkg.txt
122
+
123
+ # The post-build script runs on every start, before the runtime command, and
124
+ # './rostainer shell' sources the shell setup script, as in dev containers -
125
+ # each after the plugins' hooks for it. Last, so changing any of them only
126
+ # changes these small layers.
127
+ ARG PACKAGE_SUDO=false
128
+ USER root
129
+ RUN rm -rf /usr/local/lib/rostainer/plugins
130
+ COPY .package_plugins/ /usr/local/lib/rostainer/plugins/
131
+ COPY runtime_entrypoint.py post_build.sh shell_setup.sh /usr/local/bin/
132
+ RUN chmod 755 /usr/local/bin/runtime_entrypoint.py /usr/local/bin/post_build.sh \
133
+ /usr/local/bin/shell_setup.sh && \
134
+ if [ "${PACKAGE_SUDO}" != "true" ]; then rm /etc/sudoers.d/rostainer; fi
135
+ USER ${USERNAME}
136
+
137
+ ENTRYPOINT ["runtime_entrypoint.py"]