Skip to content

Commit ebe0ce8

Browse files
[Nexthop] Add command execution support for FBOSS Image Builder
Add command execution capabilities for running build operations. - Implement execute module for running commands in Docker containers - Add support for command execution with output capture - Enable build operations to run in isolated container environments
1 parent 52bc9d1 commit ebe0ce8

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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}")

fboss-image/distro_cli/ruff.toml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,17 @@ select = [
2222
"RUF", # ruff-specific
2323
]
2424

25+
ignore = [
26+
"B904", # Allow raising exceptions without 'from' for cleaner error messages
27+
"PLR0913", # Allow more than 8 arguments to be specified
28+
]
29+
2530
[lint.per-file-ignores]
26-
# Tests need to modify sys.path before importing local modules
2731
# Tests may have unused mock arguments (ARG002)
28-
"tests/*.py" = ["E402", "ARG002"]
29-
"*/tests/*.py" = ["E402", "ARG002"]
32+
# Tests may use open() instead of Path.open() for simplicity (PTH123)
33+
"tests/*.py" = ["ARG002", "PTH123"]
34+
"*/tests/*.py" = ["ARG002", "PTH123"]
35+
36+
[lint.pylint]
37+
# Allow more arguments for builder/execute functions
38+
max-args = 8

0 commit comments

Comments
 (0)