X Tutup
Skip to content

Commit b2f47f6

Browse files
author
michael.freund
committed
Made the client swarm-compatible
1 parent 1cf2a46 commit b2f47f6

File tree

14 files changed

+407
-18
lines changed

14 files changed

+407
-18
lines changed

src/main/java/com/github/dockerjava/api/model/Container.java

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Map;
44

5+
import com.fasterxml.jackson.annotation.JsonIgnore;
56
import org.apache.commons.lang.builder.ToStringBuilder;
67

78
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@@ -17,7 +18,6 @@
1718
@JsonIgnoreProperties(ignoreUnknown = true)
1819
@JsonInclude(Include.NON_NULL)
1920
public class Container {
20-
2121
@JsonProperty("Command")
2222
private String command;
2323

@@ -42,6 +42,11 @@ public class Container {
4242
@JsonProperty("Status")
4343
private String status;
4444

45+
@JsonIgnore
46+
private String name;
47+
@JsonIgnore
48+
private String host;
49+
4550
public String getId() {
4651
return id;
4752
}
@@ -74,6 +79,39 @@ public String[] getNames() {
7479
return names;
7580
}
7681

82+
/**
83+
* example for a container name in swarm could be "swarm-host-01/loving_swanson"
84+
* we want only "loving_swanson"
85+
* the result is cached in this.name and this.host
86+
* @return String
87+
*/
88+
@JsonIgnore
89+
public String getNameInSwarm() {
90+
if (name == null) {
91+
String[] split = findCorrectNameInSwarm().split("/");
92+
if (split.length == 3) {
93+
host = split[1];
94+
name = split[2];
95+
} else {
96+
name = split[1];
97+
}
98+
}
99+
return name;
100+
}
101+
102+
/**
103+
* see getNameInSwarm, is splits host from containername
104+
* @return String|null
105+
*/
106+
@JsonIgnore
107+
public String getSwarmHost() {
108+
if (name == null) {
109+
//used to split host from name and vice versa + cache the result
110+
getNameInSwarm();
111+
}
112+
return host;
113+
}
114+
77115
@Override
78116
public String toString() {
79117
return ToStringBuilder.reflectionToString(this);
@@ -115,4 +153,28 @@ public String toString() {
115153
return ToStringBuilder.reflectionToString(this);
116154
}
117155
}
156+
157+
@JsonIgnore
158+
private String findCorrectNameInSwarm() {
159+
160+
//if a container is linked to another, it has multiple names
161+
//for example: "swarm-node-01/auth/etcd" and "swarm-node-01/etcd"
162+
//we search for that one, that only has one / in it, thats the "root" container
163+
//in our example => "etcd"
164+
//all other names are the alias-names in the linked containers
165+
166+
//no other containers are linked to us, so just return the first one
167+
if (getNames().length == 1) {
168+
return getNames()[0];
169+
}
170+
171+
for(String name: getNames()) {
172+
if (name.split("/").length <= 3) {
173+
return name;
174+
}
175+
}
176+
177+
//fallback
178+
return getNames()[0];
179+
}
118180
}

src/main/java/com/github/dockerjava/api/model/PullResponseItem.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public class PullResponseItem extends ResponseItem {
1111

1212
private static final long serialVersionUID = -2575482839766823293L;
1313

14-
private static final String LEGACY_REGISTRY = "this image was pulled from a legacy registry";
15-
16-
private static final String DOWNLOADED_NEWER_IMAGE = "Downloaded newer image";
17-
18-
private static final String IMAGE_UP_TO_DATE = "Image is up to date";
19-
20-
private static final String DOWNLOAD_COMPLETE = "Download complete";
14+
private static final String[] SUCCESS_STRINGS = new String[]{
15+
"this image was pulled from a legacy registry",
16+
"Downloaded newer image",
17+
"Image is up to date",
18+
"Download complete",
19+
" downloaded" //Swarm sends another text, when download is complete!
20+
};
2121

2222
/**
2323
* Returns whether the status indicates a successful pull operation
@@ -30,7 +30,9 @@ public boolean isPullSuccessIndicated() {
3030
return false;
3131
}
3232

33-
return (getStatus().contains(DOWNLOAD_COMPLETE) || getStatus().contains(IMAGE_UP_TO_DATE)
34-
|| getStatus().contains(DOWNLOADED_NEWER_IMAGE) || getStatus().contains(LEGACY_REGISTRY));
33+
for(String str : SUCCESS_STRINGS) {
34+
if (getStatus().contains(str)) return true;
35+
}
36+
return false;
3537
}
3638
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package com.github.dockerjava.api.model;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonInclude;
6+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
7+
8+
import java.util.*;
9+
10+
/**
11+
*
12+
* @author Michael Freund (fragger@xfragger.de)
13+
*
14+
*/
15+
@JsonInclude(Include.NON_NULL)
16+
@JsonIgnoreProperties(ignoreUnknown = true)
17+
public class SwarmInfo extends Info {
18+
19+
@SuppressWarnings("unchecked")
20+
private static List<String> castLine(Object line) {
21+
return (List<String>)line;
22+
}
23+
24+
@JsonIgnore
25+
public Map<String, Node> getSwarmNodes() {
26+
final Map<String, Node> nodes = new HashMap<String, Node>();
27+
28+
boolean nodesFound = false;
29+
Node lastNode = null;
30+
31+
for(Object lineObj : getDriverStatuses()) {
32+
List<String> lineArr = castLine(lineObj);
33+
34+
//these strings have control chars (backspace as first char for example), remove all of them
35+
String key = lineArr.get(0).replaceAll("[\u0000-\u001f]", "");
36+
37+
if (key.equals("Nodes")) {
38+
nodesFound = true;
39+
continue;
40+
}
41+
42+
if (nodesFound && !key.substring(0, 3).equals(" └ ")) {
43+
Node node = new Node(key, lineArr.get(1));
44+
nodes.put(key, node);
45+
lastNode = node;
46+
continue;
47+
}
48+
49+
if (lastNode != null) {
50+
switch (key) {
51+
case " └ Status":
52+
lastNode.status = lineArr.get(1);
53+
break;
54+
case " └ Containers":
55+
lastNode.containers = Integer.valueOf(lineArr.get(1));
56+
break;
57+
case " └ Reserved CPUs":
58+
String[] splittedCPU = lineArr.get(1).split("/");
59+
lastNode.reservedCpus = Integer.valueOf(splittedCPU[0].trim());
60+
lastNode.cpus = Integer.valueOf(splittedCPU[1].trim());
61+
break;
62+
case " └ Reserved Memory":
63+
String[] splittedMem = lineArr.get(1).split("/");
64+
lastNode.reservedMemory = splittedMem[0].trim();
65+
lastNode.memory = splittedMem[1].trim();
66+
break;
67+
case " └ Labels":
68+
for (String label : lineArr.get(1).split(", ")) {
69+
String[] split = label.split("=");
70+
lastNode.labels.put(split[0], split[1]);
71+
}
72+
break;
73+
default:
74+
//ignore unknown nodes for the moment
75+
}
76+
}
77+
}
78+
79+
return nodes;
80+
}
81+
82+
public class Node {
83+
private final String name;
84+
private final String ip;
85+
private final int port;
86+
private final Map<String, String> labels = new HashMap<>();
87+
private String status;
88+
private int containers;
89+
private int cpus;
90+
private int reservedCpus;
91+
private String memory;
92+
private String reservedMemory;
93+
94+
public Node(String name, String ipPort) {
95+
String[] splitted = ipPort.split(":");
96+
this.name = name;
97+
this.ip = splitted[0];
98+
this.port = Integer.valueOf(splitted[1]);
99+
}
100+
101+
public String getName() {
102+
return name;
103+
}
104+
105+
public String getIp() {
106+
return ip;
107+
}
108+
109+
public int getPort() {
110+
return port;
111+
}
112+
113+
public String getStatus() {
114+
return status;
115+
}
116+
117+
public int getContainers() {
118+
return containers;
119+
}
120+
121+
public int getCpus() {
122+
return cpus;
123+
}
124+
125+
public int getReservedCpus() {
126+
return reservedCpus;
127+
}
128+
129+
public String getMemory() {
130+
return memory;
131+
}
132+
133+
public String getReservedMemory() {
134+
return reservedMemory;
135+
}
136+
137+
public Map<String, String> getLabels() {
138+
return labels;
139+
}
140+
}
141+
}

0 commit comments

Comments
 (0)
X Tutup