Skip to content

Commit

Permalink
Add option to enable Docker sandboxing.
Browse files Browse the repository at this point in the history
RELNOTES: None.
PiperOrigin-RevId: 199467128
  • Loading branch information
lberki authored and Copybara-Service committed Jun 6, 2018
1 parent 188a29a commit 4b80f24
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.Spawns;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.exec.ActionContextProvider;
import com.google.devtools.build.lib.exec.SpawnRunner;
import com.google.devtools.build.lib.exec.apple.XcodeLocalEnvProvider;
Expand Down Expand Up @@ -71,28 +72,35 @@ public static SandboxActionContextProvider create(CommandEnvironment cmdEnv, Pat
contexts.add(new ProcessWrapperSandboxedStrategy(cmdEnv.getExecRoot(), spawnRunner));
}

// This strategy uses Docker to execute spawns. It should work on all platforms that support
// Docker.
getPathToDockerClient(cmdEnv)
.ifPresent(
dockerClient -> {
if (DockerSandboxedSpawnRunner.isSupported(cmdEnv, dockerClient)) {
String defaultImage = options.getOptions(SandboxOptions.class).dockerImage;
boolean useCustomizedImages =
options.getOptions(SandboxOptions.class).dockerUseCustomizedImages;
SpawnRunner spawnRunner =
withFallback(
cmdEnv,
new DockerSandboxedSpawnRunner(
cmdEnv,
dockerClient,
sandboxBase,
defaultImage,
timeoutKillDelay,
useCustomizedImages));
contexts.add(new DockerSandboxedStrategy(cmdEnv.getExecRoot(), spawnRunner));
}
});
SandboxOptions sandboxOptions = options.getOptions(SandboxOptions.class);

if (sandboxOptions.enableDockerSandbox) {
// This strategy uses Docker to execute spawns. It should work on all platforms that support
// Docker.
getPathToDockerClient(cmdEnv)
.ifPresent(
dockerClient -> {
if (DockerSandboxedSpawnRunner.isSupported(cmdEnv, dockerClient)) {
String defaultImage = sandboxOptions.dockerImage;
boolean useCustomizedImages = sandboxOptions.dockerUseCustomizedImages;
SpawnRunner spawnRunner =
withFallback(
cmdEnv,
new DockerSandboxedSpawnRunner(
cmdEnv,
dockerClient,
sandboxBase,
defaultImage,
timeoutKillDelay,
useCustomizedImages));
contexts.add(new DockerSandboxedStrategy(cmdEnv.getExecRoot(), spawnRunner));
}
});
} else if (sandboxOptions.dockerVerbose) {
cmdEnv.getReporter().handle(Event.info(
"Docker sandboxing disabled. Use the '--experimental_enable_docker_sandbox' command "
+ "line option to enable it"));
}

// This is the preferred sandboxing strategy on Linux.
if (LinuxSandboxedSpawnRunner.isSupported(cmdEnv)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ public ImmutableSet<Path> getInaccessiblePaths(FileSystem fs) {
)
public boolean collectLocalSandboxExecutionStatistics;

@Option(
name = "experimental_enable_docker_sandbox",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
effectTags = {OptionEffectTag.EXECUTION},
help = "Enable Docker-based sandboxing. This option has no effect if Docker is not installed.")
public boolean enableDockerSandbox;

@Option(
name = "experimental_docker_image",
defaultValue = "",
Expand Down

0 comments on commit 4b80f24

Please sign in to comment.