Skip to content

Commit 70dd5d7

Browse files
authored
Merge pull request #207 from bluesliverx/main
Fix handling of local files for services
2 parents 54685e2 + 8a9dce8 commit 70dd5d7

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

README.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ the run step:
555555
# A map specifying files that should be injected into the container.
556556
# The map key is the alias referencing a given file (as configured in
557557
# the "local-files" section of the global configuration file) or a
558-
# relative path to a file/directory in the build directory. The value
558+
# relative path to a file/directory in the build directory. The value
559559
# is the path the given file should be mounted at within the container.
560560
files:
561561
namespaced.file.alias1: "/path/to/readonly/file/or/dir"
@@ -829,12 +829,13 @@ within service container configuration:
829829
830830
# A map specifying files that should be injected into the container.
831831
# The map key is the alias referencing a given file (as configured in
832-
# the "local-files" section of the global configuration file) and the
833-
# value is the path the given file should be mounted at within the
834-
# container.
832+
# the "local-files" section of the global configuration file) or a
833+
# relative path to a file/directory in the build directory. The value
834+
# is the path the given file should be mounted at within the container.
835835
files:
836836
namespaced.file.alias1: "/path/to/readonly/file/or/dir"
837837
namespaced.file.alias2: "/path/to/readwrite/file/or/dir:rw"
838+
build/dir/file: "/path/to/build/dir/file"
838839
839840
# A list specifying other service containers whose exposed volumes
840841
# should be mapped into this service container's file system. Any

buildrunner/steprunner/tasks/run.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,38 @@ def _archive_file(
467467
properties,
468468
)
469469

470+
def _get_local_file(
471+
self, buildrunner_config: BuildRunnerConfig, file_alias: str
472+
) -> str:
473+
"""
474+
Lookup file alias from global config or as a local file.
475+
:param buildrunner_config: buildrunner global configuration
476+
:param file_alias: the file alias
477+
:return: the local file to use (if available)
478+
:raises BuildRunnerConfigurationError: the file alias is not found and is not a valid local file
479+
"""
480+
f_local = buildrunner_config.get_local_files_from_alias(file_alias)
481+
if not f_local:
482+
f_local = os.path.realpath(
483+
os.path.join(self.step_runner.build_runner.build_dir, file_alias)
484+
)
485+
if (
486+
f_local != self.step_runner.build_runner.build_dir
487+
and not f_local.startswith(
488+
self.step_runner.build_runner.build_dir + os.path.sep
489+
)
490+
):
491+
raise BuildRunnerConfigurationError(
492+
f'Mount path of "{file_alias}" attempts to step out of '
493+
f'source directory "{self.step_runner.build_runner.build_dir}"'
494+
)
495+
496+
if not os.path.exists(f_local):
497+
raise BuildRunnerConfigurationError(
498+
f"Cannot find valid alias for files entry '{file_alias}' nor path at '{f_local}'"
499+
)
500+
return f_local
501+
470502
# pylint: disable=too-many-statements,too-many-branches,too-many-locals
471503
def _start_service_container(self, name, service: Service):
472504
"""
@@ -603,11 +635,7 @@ def _start_service_container(self, name, service: Service):
603635
if service.files:
604636
for f_alias, f_path in service.files.items():
605637
# lookup file from alias
606-
f_local = buildrunner_config.get_local_files_from_alias(f_alias)
607-
if not f_local or not os.path.exists(f_local):
608-
raise BuildRunnerConfigurationError(
609-
f"Cannot find valid local file for alias '{f_alias}'"
610-
)
638+
f_local = self._get_local_file(buildrunner_config, f_alias)
611639

612640
if f_path[-3:] not in [":ro", ":rw"]:
613641
f_path = f_path + ":ro"
@@ -980,29 +1008,7 @@ def run(self, context: dict): # pylint: disable=too-many-statements,too-many-br
9801008
# see if we need to inject any files
9811009
if self.step.files:
9821010
for f_alias, f_path in self.step.files.items():
983-
# lookup file from alias
984-
f_local = buildrunner_config.get_local_files_from_alias(
985-
f_alias,
986-
)
987-
if not f_local:
988-
f_local = os.path.realpath(
989-
os.path.join(self.step_runner.build_runner.build_dir, f_alias)
990-
)
991-
if (
992-
f_local != self.step_runner.build_runner.build_dir
993-
and not f_local.startswith(
994-
self.step_runner.build_runner.build_dir + os.path.sep
995-
)
996-
):
997-
raise BuildRunnerConfigurationError(
998-
f'Mount path of "{f_alias}" attempts to step out of '
999-
f'source directory "{self.step_runner.build_runner.build_dir}"'
1000-
)
1001-
1002-
if not os.path.exists(f_local):
1003-
raise BuildRunnerConfigurationError(
1004-
f"Cannot find valid alias for files entry '{f_alias}' nor path at '{f_local}'"
1005-
)
1011+
f_local = self._get_local_file(buildrunner_config, f_alias)
10061012

10071013
if f_path[-3:] not in [":ro", ":rw"]:
10081014
f_path = f_path + ":ro"

tests/test-files/test-build_dir_mount.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@
44
steps:
55
test-build_dir_mount:
66
run:
7+
services:
8+
service1:
9+
image: {{ DOCKER_REGISTRY }}/ubuntu:latest
10+
files:
11+
# Obvious directory that should succeed
12+
.: /test_services_dir
13+
cmd: |
14+
set -eux
15+
[ -d /test_services_dir ]
716
image: {{ DOCKER_REGISTRY }}/ubuntu:latest
817
files:
9-
# Obvious directory that should succeed
1018
.: /test_dir
1119
cmds:
1220
- |

0 commit comments

Comments
 (0)