-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathJavaDockerClient.java
More file actions
81 lines (69 loc) · 2.81 KB
/
Copy pathJavaDockerClient.java
File metadata and controls
81 lines (69 loc) · 2.81 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
/**
* Copyright 2014 Transmode AB
*
* 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 se.transmode.gradle.plugins.docker.client;
import com.google.common.base.Preconditions;
import com.sun.jersey.api.client.ClientResponse;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.GradleException;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import java.io.File;
public class JavaDockerClient extends com.github.dockerjava.client.DockerClient implements DockerClient {
private static Logger log = Logging.getLogger(JavaDockerClient.class);
JavaDockerClient() {
super();
}
JavaDockerClient(String url) {
super(url);
}
@Override
public void buildImage(File buildDir, String tag) {
Preconditions.checkNotNull(tag, "Image tag can not be null.");
Preconditions.checkArgument(!tag.isEmpty(), "Image tag can not be empty.");
ClientResponse response = buildImageCmd(buildDir).withTag(tag).exec();
checkAndPrintResponse(response);
}
@Override
public void pushImage(String tag) {
Preconditions.checkNotNull(tag, "Image tag can not be null.");
Preconditions.checkArgument(!tag.isEmpty(), "Image tag can not be empty.");
ClientResponse response = pushImageCmd(tag).exec();
checkAndPrintResponse(response);
}
private static void checkAndPrintResponse(ClientResponse response) {
String msg = response.getEntity(String.class);
if (response.getStatusInfo() != ClientResponse.Status.OK) {
throw new GradleException(
"Docker API error: Failed to build Image:\n"+msg);
}
System.out.println(msg);
}
public static JavaDockerClient create(String url, String user, String password, String email) {
JavaDockerClient client;
if (StringUtils.isEmpty(url)) {
log.info("Connecting to localhost");
// TODO -- use no-arg constructor once we switch to java-docker 0.9.1
client = new JavaDockerClient("http://localhost:2375");
} else {
log.info("Connecting to {}", url);
client = new JavaDockerClient(url);
}
if (StringUtils.isNotEmpty(user)) {
client.setCredentials(user, password, email);
}
return client;
}
}