forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildImageCmdExec.java
More file actions
102 lines (89 loc) · 4.24 KB
/
Copy pathBuildImageCmdExec.java
File metadata and controls
102 lines (89 loc) · 4.24 KB
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
package com.github.dockerjava.jaxrs;
import com.github.dockerjava.api.async.ResultCallback;
import com.github.dockerjava.api.command.BuildImageCmd;
import com.github.dockerjava.api.model.AuthConfigurations;
import com.github.dockerjava.api.model.BuildResponseItem;
import com.github.dockerjava.core.DockerClientConfig;
import com.github.dockerjava.core.async.JsonStreamProcessor;
import com.github.dockerjava.jaxrs.async.AbstractCallbackNotifier;
import com.github.dockerjava.jaxrs.async.POSTCallbackNotifier;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.RequestEntityProcessing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import static javax.ws.rs.client.Entity.entity;
public class BuildImageCmdExec extends AbstrAsyncDockerCmdExec<BuildImageCmd, BuildResponseItem> implements
BuildImageCmd.Exec {
private static final Logger LOGGER = LoggerFactory.getLogger(BuildImageCmdExec.class);
public BuildImageCmdExec(WebTarget baseResource, DockerClientConfig dockerClientConfig) {
super(baseResource, dockerClientConfig);
}
private Invocation.Builder resourceWithOptionalAuthConfig(BuildImageCmd command, Invocation.Builder request) {
final AuthConfigurations authConfigs = firstNonNull(command.getBuildAuthConfigs(), getBuildAuthConfigs());
if (authConfigs != null) {
request = request.header("X-Registry-Config", registryConfigs(authConfigs));
}
return request;
}
private static AuthConfigurations firstNonNull(final AuthConfigurations fromCommand,
final AuthConfigurations fromConfig) {
if (fromCommand != null) {
return fromCommand;
}
if (fromConfig != null) {
return fromConfig;
}
return null;
}
@Override
protected AbstractCallbackNotifier<BuildResponseItem> callbackNotifier(BuildImageCmd command,
ResultCallback<BuildResponseItem> resultCallback) {
WebTarget webTarget = getBaseResource().path("/build");
String dockerFilePath = command.getPathToDockerfile();
if (dockerFilePath != null && command.getRemote() == null && !"Dockerfile".equals(dockerFilePath)) {
webTarget = webTarget.queryParam("dockerfile", dockerFilePath);
}
if (command.getTag() != null) {
webTarget = webTarget.queryParam("t", command.getTag());
}
if (command.getRemote() != null) {
webTarget = webTarget.queryParam("remote", command.getRemote().toString());
}
if (command.isQuiet()) {
webTarget = webTarget.queryParam("q", "true");
}
if (command.hasNoCacheEnabled()) {
webTarget = webTarget.queryParam("nocache", "true");
}
if (command.hasPullEnabled()) {
webTarget = webTarget.queryParam("pull", "true");
}
if (!command.hasRemoveEnabled()) {
webTarget = webTarget.queryParam("rm", "false");
}
if (command.isForcerm()) {
webTarget = webTarget.queryParam("forcerm", "true");
}
if (command.getMemory() != null) {
webTarget = webTarget.queryParam("memory", command.getMemory());
}
if (command.getMemswap() != null) {
webTarget = webTarget.queryParam("memswap", command.getMemswap());
}
if (command.getCpushares() != null) {
webTarget = webTarget.queryParam("cpushares", command.getCpushares());
}
if (command.getCpusetcpus() != null) {
webTarget = webTarget.queryParam("cpusetcpus", command.getCpusetcpus());
}
webTarget.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
webTarget.property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024 * 1024);
LOGGER.trace("POST: {}", webTarget);
return new POSTCallbackNotifier<>(new JsonStreamProcessor<>(BuildResponseItem.class), resultCallback,
resourceWithOptionalAuthConfig(command, webTarget.request()).accept(MediaType.TEXT_PLAIN), entity(
command.getTarInputStream(), "application/tar"));
}
}