forked from docker-java/docker-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerImageMeta.java
More file actions
131 lines (116 loc) · 5.1 KB
/
Copy pathDockerImageMeta.java
File metadata and controls
131 lines (116 loc) · 5.1 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
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
package pi.docker;
import avro.shaded.com.google.common.collect.Lists;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.DockerCmdExecFactory;
import com.github.dockerjava.api.command.InspectImageResponse;
import com.github.dockerjava.api.exception.NotFoundException;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.command.PullImageResultCallback;
import org.apache.avro.tool.IdlTool;
import org.apache.avro.tool.Tool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Singleton;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Created by xiaofeiwu on 2016/12/1.
*/
@Singleton
public class DockerImageMeta {
public static final Logger LOG = LoggerFactory.getLogger(DockerImageMeta.class);
private DockerClient dockerClient;
DockerImageMeta(){
LOG.info("Connecting to Docker server");
DockerCmdExecFactory dockerCmdExecFactory= DockerClientBuilder.getDefaultDockerCmdExecFactory();
dockerClient = DockerClientBuilder.getInstance(config())
.withDockerCmdExecFactory(dockerCmdExecFactory)
.build();
}
public int getLabels(InputStream in, PrintStream out, PrintStream err) throws Exception {
Tool tool = new IdlTool();
List<String> args = Lists.newArrayList();
// tool.run(System.in, System.out, System.err, args);
return tool.run(in, out, err, args);
}
public void checkImage(String imageName){
try {
dockerClient.inspectImageCmd(imageName).exec(); //"busybox"
} catch (NotFoundException e) {
LOG.info("Pulling image '{}'", imageName);
// need to block until image is pulled completely
dockerClient.pullImageCmd(imageName).withTag("latest").exec(new PullImageResultCallback()).awaitSuccess();
}
}
/**
* Process service labels
* @param imageName
* @return return null if process fail or no packages definition
* @throws Exception
*/
public String processServiceLabels(String imageName) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream err = new PrintStream(baos);
String ret= processServiceLabels(imageName, err);
if(ret==null) {
String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
LOG.error(content);
}
return ret;
}
public String processServiceLabels(String imageName, PrintStream err) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
int ret=processServiceLabels(imageName, ps, err);
if(ret==0) {
String content = new String(baos.toByteArray(), StandardCharsets.UTF_8);
return content;
}
return null;
}
public int processServiceLabels(String imageName, PrintStream out, PrintStream err) throws Exception {
InspectImageResponse response=dockerClient.inspectImageCmd(imageName).exec(); // "scala_rest"
Map<String, String> labels= response.getConfig().getLabels();
String packages=labels.get("packages");
if(packages!=null){
String serviceDefs=labels.get(packages);
if(serviceDefs!=null) {
serviceDefs="@namespace(\""+packages+"\")\n"+serviceDefs;
InputStream stream = new ByteArrayInputStream(serviceDefs.getBytes(StandardCharsets.UTF_8));
return getLabels(stream, out, err);
}
}
return -1;
}
public void printVersionLabels(String imageName){
InspectImageResponse response=dockerClient.inspectImageCmd(imageName).exec(); // "scala_rest"
Map<String, String> labels= response.getConfig().getLabels();
System.out.println(labels.get("version"));
System.out.println(labels.get("description"));
System.out.println("other labels:");
for (Map.Entry label:labels.entrySet()) {
System.out.format("%s = %s\n", label.getKey(),label.getValue());
}
}
private DefaultDockerClientConfig config() {
return config(null);
}
protected DefaultDockerClientConfig config(String password) {
DefaultDockerClientConfig.Builder builder = DefaultDockerClientConfig.createDefaultConfigBuilder()
// .withRegistryUrl("https://index.docker.io/v1/");
// .withDockerHost("unix:///var/run/docker.sock")
// docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -p 2377:2375 \
// bobrik/socat TCP-LISTEN:2375,reuseaddr,fork UNIX-CLIENT:/var/run/docker.sock
// .withDockerHost("tcp://localhost:2377")
.withDockerHost("tcp://socat:2375")
.withDockerTlsVerify(false);
if (password != null) {
builder = builder.withRegistryPassword(password);
}
return builder.build();
}
}