|
| 1 | +# Copyright (c) 2004-present, Facebook, Inc. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. An additional grant |
| 6 | +# of patent rights can be found in the PATENTS file in the same directory. |
| 7 | + |
| 8 | +"""Execute utilities for FBOSS image builder.""" |
| 9 | + |
| 10 | +import logging |
| 11 | +from pathlib import Path |
| 12 | + |
| 13 | +from distro_cli.lib.docker.container import run_container |
| 14 | +from distro_cli.lib.docker.image import build_fboss_builder_image |
| 15 | + |
| 16 | +from .exceptions import BuildError |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +def execute_build_in_container( |
| 22 | + image_name: str, |
| 23 | + command: list[str], |
| 24 | + volumes: dict[Path, Path], |
| 25 | + component_name: str, |
| 26 | + privileged: bool = False, |
| 27 | + working_dir: str | None = None, |
| 28 | + dependency_install_paths: dict[str, Path] | None = None, |
| 29 | +) -> None: |
| 30 | + """Execute build command in Docker container. |
| 31 | +
|
| 32 | + Args: |
| 33 | + image_name: Docker image name |
| 34 | + command: Command to execute as list |
| 35 | + volumes: Host to container path mappings |
| 36 | + component_name: Component name |
| 37 | + privileged: Run in privileged mode |
| 38 | + working_dir: Working directory in container |
| 39 | + dependency_install_paths: Dict mapping dependency names to their container mount paths |
| 40 | + (e.g., {'kernel': Path('/deps/kernel')}) |
| 41 | + RPMs from these paths will be installed before running the build |
| 42 | +
|
| 43 | + Raises: |
| 44 | + BuildError: If build fails |
| 45 | + """ |
| 46 | + logger.info(f"Executing {component_name} build in Docker container: {image_name}") |
| 47 | + |
| 48 | + # Ensure fboss_builder image is built |
| 49 | + build_fboss_builder_image() |
| 50 | + |
| 51 | + # Use build_entrypoint.py from /tools (mounted from distro_cli/tools) |
| 52 | + if dependency_install_paths: |
| 53 | + logger.info( |
| 54 | + f"Build entry point will process {len(dependency_install_paths)} dependencies" |
| 55 | + ) |
| 56 | + |
| 57 | + # Build the command: python3 /tools/build_entrypoint.py <build_command> |
| 58 | + # The entry point will look for dependencies in /deps and install them. |
| 59 | + cmd_list = ["python3", "/tools/build_entrypoint.py", *command] |
| 60 | + logger.info("Build will produce uncompressed artifacts (.tar)") |
| 61 | + |
| 62 | + logger.info(f"Running in container: {' '.join(cmd_list)}") |
| 63 | + |
| 64 | + try: |
| 65 | + exit_code = run_container( |
| 66 | + image=image_name, |
| 67 | + command=cmd_list, |
| 68 | + volumes=volumes, |
| 69 | + privileged=privileged, |
| 70 | + working_dir=working_dir, |
| 71 | + ) |
| 72 | + |
| 73 | + if exit_code != 0: |
| 74 | + raise BuildError( |
| 75 | + f"{component_name} build failed with exit code {exit_code}" |
| 76 | + ) |
| 77 | + |
| 78 | + logger.info(f"{component_name} build in container complete") |
| 79 | + |
| 80 | + except RuntimeError as e: |
| 81 | + raise BuildError(f"Failed to run Docker container: {e}") |
0 commit comments