/
WithContainerStep.java
421 lines (372 loc) · 19.1 KB
/
WithContainerStep.java
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
* The MIT License
*
* Copyright (c) 2015, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.docker.workflow;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.LauncherDecorator;
import hudson.Proc;
import hudson.Util;
import hudson.model.Computer;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.slaves.WorkspaceList;
import hudson.util.VersionNumber;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.jenkinsci.plugins.docker.commons.fingerprint.DockerFingerprints;
import org.jenkinsci.plugins.docker.commons.tools.DockerTool;
import org.jenkinsci.plugins.docker.workflow.client.DockerClient;
import org.jenkinsci.plugins.docker.workflow.client.WindowsDockerClient;
import org.jenkinsci.plugins.workflow.steps.AbstractStepDescriptorImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepExecutionImpl;
import org.jenkinsci.plugins.workflow.steps.AbstractStepImpl;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepContextParameter;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
public class WithContainerStep extends AbstractStepImpl {
private static final Logger LOGGER = Logger.getLogger(WithContainerStep.class.getName());
private final @Nonnull String image;
private String args;
private String toolName;
@DataBoundConstructor public WithContainerStep(@Nonnull String image) {
this.image = image;
}
public String getImage() {
return image;
}
@DataBoundSetter
public void setArgs(String args) {
this.args = Util.fixEmpty(args);
}
public String getArgs() {
return args;
}
public String getToolName() {
return toolName;
}
@DataBoundSetter public void setToolName(String toolName) {
this.toolName = Util.fixEmpty(toolName);
}
private static void destroy(String container, Launcher launcher, Node node, EnvVars launcherEnv, String toolName) throws Exception {
new DockerClient(launcher, node, toolName).stop(launcherEnv, container);
}
// TODO switch to GeneralNonBlockingStepExecution
public static class Execution extends AbstractStepExecutionImpl {
private static final long serialVersionUID = 1;
@Inject(optional=true) private transient WithContainerStep step;
@StepContextParameter private transient Launcher launcher;
@StepContextParameter private transient TaskListener listener;
@StepContextParameter private transient FilePath workspace;
@StepContextParameter private transient EnvVars env;
@StepContextParameter private transient Computer computer;
@StepContextParameter private transient Node node;
@SuppressWarnings("rawtypes") // TODO not compiling on cloudbees.ci
@StepContextParameter private transient Run run;
private String container;
private String toolName;
public Execution() {
}
@Override public boolean start() throws Exception {
EnvVars envReduced = new EnvVars(env);
EnvVars envHost = computer.getEnvironment();
envReduced.entrySet().removeAll(envHost.entrySet());
// Remove PATH during cat.
envReduced.remove("PATH");
envReduced.remove("");
LOGGER.log(Level.FINE, "reduced environment: {0}", envReduced);
workspace.mkdirs(); // otherwise it may be owned by root when created for -v
String ws = getPath(workspace);
toolName = step.toolName;
DockerClient dockerClient = launcher.isUnix()
? new DockerClient(launcher, node, toolName)
: new WindowsDockerClient(launcher, node, toolName);
VersionNumber dockerVersion = dockerClient.version();
if (dockerVersion != null) {
if (dockerVersion.isOlderThan(new VersionNumber("1.7"))) {
throw new AbortException("The docker version is less than v1.7. Pipeline functions requiring 'docker exec' (e.g. 'docker.inside') or SELinux labeling will not work.");
} else if (dockerVersion.isOlderThan(new VersionNumber("1.8"))) {
listener.error("The docker version is less than v1.8. Running a 'docker.inside' from inside a container will not work.");
} else if (dockerVersion.isOlderThan(new VersionNumber("1.13"))) {
if (!launcher.isUnix())
throw new AbortException("The docker version is less than v1.13. Running a 'docker.inside' from inside a Windows container will not work.");
}
} else {
listener.error("Failed to parse docker version. Please note there is a minimum docker version requirement of v1.7.");
}
FilePath tempDir = tempDir(workspace);
tempDir.mkdirs();
String tmp = getPath(tempDir);
Map<String, String> volumes = new LinkedHashMap<String, String>();
Collection<String> volumesFromContainers = new LinkedHashSet<String>();
Optional<String> containerId = dockerClient.getContainerIdIfContainerized();
if (containerId.isPresent()) {
listener.getLogger().println(node.getDisplayName() + " seems to be running inside container " + containerId.get());
final Collection<String> mountedVolumes = dockerClient.getVolumes(env, containerId.get());
final String[] dirs = {ws, tmp};
for (String dir : dirs) {
// check if there is any volume which contains the directory
boolean found = false;
for (String vol : mountedVolumes) {
boolean dirStartsWithVol = launcher.isUnix()
? dir.startsWith(vol) // Linux
: dir.toLowerCase().startsWith(vol.toLowerCase()); // Windows
if (dirStartsWithVol) {
volumesFromContainers.add(containerId.get());
found = true;
break;
}
}
if (!found) {
listener.getLogger().println("but " + dir + " could not be found among " + mountedVolumes);
volumes.put(dir, dir);
}
}
} else {
listener.getLogger().println(node.getDisplayName() + " does not seem to be running inside a container");
volumes.put(ws, ws);
volumes.put(tmp, tmp);
}
String command = launcher.isUnix() ? "cat" : "cmd.exe";
container = dockerClient.run(env, step.image, step.args, ws, volumes, volumesFromContainers, envReduced, dockerClient.whoAmI(), /* expected to hang until killed */ command);
final List<String> ps = dockerClient.listProcess(env, container);
if (!ps.contains(command)) {
listener.error(
"The container started but didn't run the expected command. " +
"Please double check your ENTRYPOINT does execute the command passed as docker run argument, " +
"as required by official docker images (see https://github.com/docker-library/official-images#consistency for entrypoint consistency requirements).\n" +
"Alternatively you can force image entrypoint to be disabled by adding option `--entrypoint=''`.");
}
DockerFingerprints.addRunFacet(dockerClient.getContainerRecord(env, container), run);
ImageAction.add(step.image, run);
getContext().newBodyInvoker().
withContext(BodyInvoker.mergeLauncherDecorators(getContext().get(LauncherDecorator.class), new Decorator(container, envHost, ws, toolName, dockerVersion))).
withCallback(new Callback(container, toolName)).
start();
return false;
}
private String getPath(FilePath filePath)
throws IOException, InterruptedException {
if (launcher.isUnix()) {
return filePath.getRemote();
} else {
return filePath.toURI().getPath().substring(1).replace("\\", "/");
}
}
// TODO use 1.652 use WorkspaceList.tempDir
private static FilePath tempDir(FilePath ws) {
return ws.sibling(ws.getName() + System.getProperty(WorkspaceList.class.getName(), "@") + "tmp");
}
@Override public void stop(@Nonnull Throwable cause) throws Exception {
if (container != null) {
LOGGER.log(Level.FINE, "stopping container " + container, cause);
destroy(container, launcher, getContext().get(Node.class), env, toolName);
}
}
}
private static class Decorator extends LauncherDecorator implements Serializable {
private static final long serialVersionUID = 1;
private final String container;
private final String[] envHost;
private final String ws;
private final @CheckForNull String toolName;
private final boolean hasEnv;
private final boolean hasWorkdir;
Decorator(String container, EnvVars envHost, String ws, String toolName, VersionNumber dockerVersion) {
this.container = container;
this.envHost = Util.mapToEnv(envHost);
this.ws = ws;
this.toolName = toolName;
this.hasEnv = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("1.13.0")) >= 0;
this.hasWorkdir = dockerVersion != null && dockerVersion.compareTo(new VersionNumber("17.12")) >= 0;
}
@Override public Launcher decorate(final Launcher launcher, final Node node) {
return new Launcher.DecoratedLauncher(launcher) {
@Override public Proc launch(Launcher.ProcStarter starter) throws IOException {
String executable;
try {
executable = getExecutable();
} catch (InterruptedException x) {
throw new IOException(x);
}
List<String> prefix = new ArrayList<>(Arrays.asList(executable, "exec"));
List<Boolean> masksPrefixList = new ArrayList<>(Arrays.asList(false, false));
if (ws != null) {
FilePath cwd = starter.pwd();
if (cwd != null) {
String path = cwd.getRemote();
if (!path.equals(ws)) {
if (hasWorkdir) {
prefix.add("--workdir");
masksPrefixList.add(false);
prefix.add(path);
masksPrefixList.add(false);
} else {
String safePath = path.replace("'", "'\"'\"'");
starter.cmds().addAll(0, Arrays.asList("sh", "-c", "cd '" + safePath + "'; exec \"$@\"", "--"));
}
}
}
} // otherwise we are loading an old serialized Decorator
Set<String> envReduced = new TreeSet<String>(Arrays.asList(starter.envs()));
envReduced.removeAll(Arrays.asList(envHost));
// Remove PATH during `exec` as well.
Iterator<String> it = envReduced.iterator();
while (it.hasNext()) {
if (it.next().startsWith("PATH=")) {
it.remove();
}
}
LOGGER.log(Level.FINE, "(exec) reduced environment: {0}", envReduced);
if (hasEnv) {
for (String e : envReduced) {
prefix.add("--env");
masksPrefixList.add(false);
prefix.add(e);
masksPrefixList.add(true);
}
prefix.add(container);
masksPrefixList.add(false);
} else {
prefix.add(container);
masksPrefixList.add(false);
prefix.add("env");
masksPrefixList.add(false);
prefix.addAll(envReduced);
masksPrefixList.addAll(envReduced.stream()
.map(v -> true)
.collect(Collectors.toList()));
}
boolean[] originalMasks = starter.masks();
if (originalMasks == null) {
originalMasks = new boolean[starter.cmds().size()];
}
// Adapted from decorateByPrefix:
starter.cmds().addAll(0, prefix);
boolean[] masks = new boolean[originalMasks.length + prefix.size()];
boolean[] masksPrefix = new boolean[masksPrefixList.size()];
for (int i = 0; i < masksPrefix.length; i++) {
masksPrefix[i] = masksPrefixList.get(i);
}
System.arraycopy(masksPrefix, 0, masks, 0, masksPrefix.length);
System.arraycopy(originalMasks, 0, masks, prefix.size(), originalMasks.length);
starter.masks(masks);
return super.launch(starter);
}
@Override public void kill(Map<String,String> modelEnvVars) throws IOException, InterruptedException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String executable = getExecutable();
if (getInner().launch().cmds(executable, "exec", container, "ps", "-A", "-o", "pid,command", "e").stdout(baos).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) {
throw new IOException("failed to run ps");
}
List<String> pids = new ArrayList<String>();
LINE: for (String line : baos.toString(Charset.defaultCharset().name()).split("\n")) {
for (Map.Entry<String,String> entry : modelEnvVars.entrySet()) {
// TODO this is imprecise: false positive when argv happens to match KEY=value even if environment does not. Cf. trick in BourneShellScript.
if (!line.contains(entry.getKey() + "=" + entry.getValue())) {
continue LINE;
}
}
line = line.trim();
int spc = line.indexOf(' ');
if (spc == -1) {
continue;
}
pids.add(line.substring(0, spc));
}
LOGGER.log(Level.FINE, "killing {0}", pids);
if (!pids.isEmpty()) {
List<String> cmds = new ArrayList<>(Arrays.asList(executable, "exec", container, "kill"));
cmds.addAll(pids);
if (getInner().launch().cmds(cmds).quiet(true).start().joinWithTimeout(DockerClient.CLIENT_TIMEOUT, TimeUnit.SECONDS, listener) != 0) {
throw new IOException("failed to run kill");
}
}
}
private String getExecutable() throws IOException, InterruptedException {
EnvVars env = new EnvVars();
for (String pair : envHost) {
env.addLine(pair);
}
return DockerTool.getExecutable(toolName, node, getListener(), env);
}
};
}
}
private static class Callback extends BodyExecutionCallback.TailCall {
private static final long serialVersionUID = 1;
private final String container;
private final String toolName;
Callback(String container, String toolName) {
this.container = container;
this.toolName = toolName;
}
@Override protected void finished(StepContext context) throws Exception {
destroy(container, context.get(Launcher.class), context.get(Node.class), context.get(EnvVars.class), toolName);
}
}
@Extension public static class DescriptorImpl extends AbstractStepDescriptorImpl {
public DescriptorImpl() {
super(Execution.class);
}
@Override public String getFunctionName() {
return "withDockerContainer";
}
@Override public String getDisplayName() {
return "Run build steps inside a Docker container";
}
@Override public boolean takesImplicitBlockArgument() {
return true;
}
@Override public boolean isAdvanced() {
return true;
}
}
}