diff --git a/README.rst b/README.rst index b14262b5..65f515fd 100755 --- a/README.rst +++ b/README.rst @@ -555,7 +555,7 @@ the run step: # A map specifying files that should be injected into the container. # The map key is the alias referencing a given file (as configured in # the "local-files" section of the global configuration file) or a - # relative path to a file/directory in the build directory. The value + # relative path to a file/directory in the build directory. The value # is the path the given file should be mounted at within the container. files: namespaced.file.alias1: "/path/to/readonly/file/or/dir" @@ -829,12 +829,13 @@ within service container configuration: # A map specifying files that should be injected into the container. # The map key is the alias referencing a given file (as configured in - # the "local-files" section of the global configuration file) and the - # value is the path the given file should be mounted at within the - # container. + # the "local-files" section of the global configuration file) or a + # relative path to a file/directory in the build directory. The value + # is the path the given file should be mounted at within the container. files: namespaced.file.alias1: "/path/to/readonly/file/or/dir" namespaced.file.alias2: "/path/to/readwrite/file/or/dir:rw" + build/dir/file: "/path/to/build/dir/file" # A list specifying other service containers whose exposed volumes # should be mapped into this service container's file system. Any diff --git a/buildrunner/steprunner/tasks/run.py b/buildrunner/steprunner/tasks/run.py index b31dd248..7624730f 100644 --- a/buildrunner/steprunner/tasks/run.py +++ b/buildrunner/steprunner/tasks/run.py @@ -467,6 +467,38 @@ def _archive_file( properties, ) + def _get_local_file( + self, buildrunner_config: BuildRunnerConfig, file_alias: str + ) -> str: + """ + Lookup file alias from global config or as a local file. + :param buildrunner_config: buildrunner global configuration + :param file_alias: the file alias + :return: the local file to use (if available) + :raises BuildRunnerConfigurationError: the file alias is not found and is not a valid local file + """ + f_local = buildrunner_config.get_local_files_from_alias(file_alias) + if not f_local: + f_local = os.path.realpath( + os.path.join(self.step_runner.build_runner.build_dir, file_alias) + ) + if ( + f_local != self.step_runner.build_runner.build_dir + and not f_local.startswith( + self.step_runner.build_runner.build_dir + os.path.sep + ) + ): + raise BuildRunnerConfigurationError( + f'Mount path of "{file_alias}" attempts to step out of ' + f'source directory "{self.step_runner.build_runner.build_dir}"' + ) + + if not os.path.exists(f_local): + raise BuildRunnerConfigurationError( + f"Cannot find valid alias for files entry '{file_alias}' nor path at '{f_local}'" + ) + return f_local + # pylint: disable=too-many-statements,too-many-branches,too-many-locals def _start_service_container(self, name, service: Service): """ @@ -603,11 +635,7 @@ def _start_service_container(self, name, service: Service): if service.files: for f_alias, f_path in service.files.items(): # lookup file from alias - f_local = buildrunner_config.get_local_files_from_alias(f_alias) - if not f_local or not os.path.exists(f_local): - raise BuildRunnerConfigurationError( - f"Cannot find valid local file for alias '{f_alias}'" - ) + f_local = self._get_local_file(buildrunner_config, f_alias) if f_path[-3:] not in [":ro", ":rw"]: f_path = f_path + ":ro" @@ -980,29 +1008,7 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br # see if we need to inject any files if self.step.files: for f_alias, f_path in self.step.files.items(): - # lookup file from alias - f_local = buildrunner_config.get_local_files_from_alias( - f_alias, - ) - if not f_local: - f_local = os.path.realpath( - os.path.join(self.step_runner.build_runner.build_dir, f_alias) - ) - if ( - f_local != self.step_runner.build_runner.build_dir - and not f_local.startswith( - self.step_runner.build_runner.build_dir + os.path.sep - ) - ): - raise BuildRunnerConfigurationError( - f'Mount path of "{f_alias}" attempts to step out of ' - f'source directory "{self.step_runner.build_runner.build_dir}"' - ) - - if not os.path.exists(f_local): - raise BuildRunnerConfigurationError( - f"Cannot find valid alias for files entry '{f_alias}' nor path at '{f_local}'" - ) + f_local = self._get_local_file(buildrunner_config, f_alias) if f_path[-3:] not in [":ro", ":rw"]: f_path = f_path + ":ro" diff --git a/tests/test-files/test-build_dir_mount.yaml b/tests/test-files/test-build_dir_mount.yaml index 488bea9b..d25d874c 100644 --- a/tests/test-files/test-build_dir_mount.yaml +++ b/tests/test-files/test-build_dir_mount.yaml @@ -4,9 +4,17 @@ steps: test-build_dir_mount: run: + services: + service1: + image: {{ DOCKER_REGISTRY }}/ubuntu:latest + files: + # Obvious directory that should succeed + .: /test_services_dir + cmd: | + set -eux + [ -d /test_services_dir ] image: {{ DOCKER_REGISTRY }}/ubuntu:latest files: - # Obvious directory that should succeed .: /test_dir cmds: - |