Skip to content

Commit b3344e9

Browse files
author
saville
committed
Fix handling of local files for services
1 parent 7695024 commit b3344e9

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

buildrunner/steprunner/tasks/run.py

Lines changed: 34 additions & 9 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,10 +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-
)
1011+
f_local = self._get_local_file(buildrunner_config, f_alias)
9871012
if not f_local:
9881013
f_local = os.path.realpath(
9891014
os.path.join(self.step_runner.build_runner.build_dir, f_alias)

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)