aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java
blob: a8fb190e48af619c538e0fdf734473231e69e649 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2015 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.sandbox;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.collect.ImmutableList;
import com.google.common.eventbus.Subscribe;
import com.google.devtools.build.lib.actions.ExecutorInitException;
import com.google.devtools.build.lib.buildtool.BuildRequest;
import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
import com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.exec.ActionContextProvider;
import com.google.devtools.build.lib.exec.ExecutorBuilder;
import com.google.devtools.build.lib.runtime.BlazeModule;
import com.google.devtools.build.lib.runtime.Command;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.vfs.FileSystem;
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.OptionsBase;
import java.io.IOException;
import javax.annotation.Nullable;

/**
 * This module provides the Sandbox spawn strategy.
 */
public final class SandboxModule extends BlazeModule {

  /** Environment for the running command. */
  private @Nullable CommandEnvironment env;

  /** Path to the location of the sandboxes. */
  private @Nullable Path sandboxBase;

  /** Instance of the sandboxfs process in use, if enabled. */
  private @Nullable SandboxfsProcess sandboxfsProcess;

  /**
   * Whether to remove the sandbox worker directories after a build or not. Useful for debugging
   * to inspect the state of files on failures.
   */
  private boolean shouldCleanupSandboxBase;

  @Override
  public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) {
    return "build".equals(command.name())
        ? ImmutableList.of(SandboxOptions.class)
        : ImmutableList.of();
  }

  /** Computes the path to the sandbox base tree for the given running command. */
  private static Path computeSandboxBase(SandboxOptions options, CommandEnvironment env)
      throws IOException {
    if (options.sandboxBase.isEmpty()) {
      return env.getOutputBase().getRelative("sandbox");
    } else {
      String dirName = String.format("%s-sandbox.%s", env.getRuntime().getProductName(),
          Fingerprint.md5Digest(env.getOutputBase().toString()));
      FileSystem fileSystem = env.getRuntime().getFileSystem();
      Path resolvedSandboxBase = fileSystem.getPath(options.sandboxBase).resolveSymbolicLinks();
      return resolvedSandboxBase.getRelative(dirName);
    }
  }

  @Override
  public void beforeCommand(CommandEnvironment env) {
    // We can't assert that env is null because the Blaze runtime does not guarantee that
    // afterCommand() will be called if the command fails due to, e.g. a syntax error.
    this.env = env;
    env.getEventBus().register(this);

    // Don't attempt cleanup unless the executor is initialized.
    sandboxfsProcess = null;
    shouldCleanupSandboxBase = false;
  }

  @Override
  public void executorInit(CommandEnvironment cmdEnv, BuildRequest request, ExecutorBuilder builder)
      throws ExecutorInitException {
    checkNotNull(env, "env not initialized; was beforeCommand called?");

    SandboxOptions options = env.getOptions().getOptions(SandboxOptions.class);
    checkNotNull(options, "We were told to initialize the executor but the SandboxOptions are "
        + "not present; were they registered for all build commands?");

    try {
      sandboxBase = computeSandboxBase(options, env);
    } catch (IOException e) {
      throw new ExecutorInitException(
          "--experimental_sandbox_base points to an invalid directory", e);
    }

    ActionContextProvider provider;
    try {
      // Ensure that each build starts with a clean sandbox base directory. Otherwise using the `id`
      // that is provided by SpawnExecutionPolicy#getId to compute a base directory for a sandbox
      // might result in an already existing directory.
      if (sandboxBase.exists()) {
        FileSystemUtils.deleteTree(sandboxBase);
      }

      sandboxBase.createDirectoryAndParents();
      if (options.useSandboxfs) {
        Path mountPoint = sandboxBase.getRelative("sandboxfs");
        mountPoint.createDirectory();
        Path logFile = sandboxBase.getRelative("sandboxfs.log");

        env.getReporter().handle(Event.info("Mounting sandboxfs instance on " + mountPoint));
        sandboxfsProcess = RealSandboxfsProcess.mount(
            PathFragment.create(options.sandboxfsPath), mountPoint, logFile);
        provider = SandboxActionContextProvider.create(cmdEnv, sandboxBase, sandboxfsProcess);
      } else {
        provider = SandboxActionContextProvider.create(cmdEnv, sandboxBase, null);
      }
    } catch (IOException e) {
      throw new ExecutorInitException("Failed to initialize sandbox", e);
    }
    builder.addActionContextProvider(provider);
    builder.addActionContextConsumer(new SandboxActionContextConsumer(cmdEnv));

    // Do not remove the sandbox base when --sandbox_debug was specified so that people can check
    // out the contents of the generated sandbox directories.
    shouldCleanupSandboxBase = !options.sandboxDebug;
  }

  private void unmountSandboxfs(String reason) {
    if (sandboxfsProcess != null) {
      checkNotNull(env, "env not initialized; was beforeCommand called?");
      env.getReporter().handle(Event.info(reason));
      // TODO(jmmv): This can be incredibly slow.  Either fix sandboxfs or do it in the background.
      sandboxfsProcess.destroy();
      sandboxfsProcess = null;
    }
  }

  @Subscribe
  public void buildComplete(BuildCompleteEvent event) {
    unmountSandboxfs("Build complete; unmounting sandboxfs...");
  }

  @Subscribe
  public void buildInterrupted(BuildInterruptedEvent event) {
    unmountSandboxfs("Build interrupted; unmounting sandboxfs...");
  }

  @Override
  public void afterCommand() {
    checkNotNull(env, "env not initialized; was beforeCommand called?");

    if (shouldCleanupSandboxBase) {
      try {
        FileSystemUtils.deleteTree(sandboxBase);
      } catch (IOException e) {
        env.getReporter().handle(Event.warn("Failed to delete sandbox base " + sandboxBase
            + ": " + e));
      }
      shouldCleanupSandboxBase = false;
    }

    checkState(sandboxfsProcess == null, "sandboxfs instance should have been shut down at this "
        + "point; were the buildComplete/buildInterrupted events sent?");
    sandboxBase = null;

    env.getEventBus().unregister(this);
    env = null;
  }
}