-
Notifications
You must be signed in to change notification settings - Fork 634
/
ContainerCreateConfig.java
176 lines (149 loc) · 5.77 KB
/
ContainerCreateConfig.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
package org.jolokia.docker.maven.access;
import java.io.*;
import java.util.*;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.jolokia.docker.maven.util.EnvUtil;
import org.json.JSONArray;
import org.json.JSONObject;
public class ContainerCreateConfig {
final JSONObject createConfig = new JSONObject();
final String imageName;
public ContainerCreateConfig(String imageName) {
this.imageName = imageName;
createConfig.put("Image", imageName);
}
public ContainerCreateConfig binds(List<String> volumes) {
if (volumes != null && !volumes.isEmpty()) {
JSONObject extractedVolumes = new JSONObject();
for (String volume : volumes) {
extractedVolumes.put(extractContainerPath(volume),
new JSONObject());
}
createConfig.put("Volumes", extractedVolumes);
}
return this;
}
public ContainerCreateConfig command(String command) {
if (command != null) {
createConfig.put("Cmd", splitOnWhiteSpace(command));
}
return this;
}
public ContainerCreateConfig domainname(String domainname) {
return add("Domainname", domainname);
}
public ContainerCreateConfig entrypoint(String entrypoint) {
if (entrypoint != null) {
createConfig.put("Entrypoint", splitOnWhiteSpace(entrypoint));
}
return this;
}
private JSONArray splitOnWhiteSpace(String entrypoint) {
JSONArray a = new JSONArray();
for (String s : EnvUtil.splitOnSpaceWithEscape(entrypoint)) {
a.put(s);
}
return a;
}
public ContainerCreateConfig environment(String envPropsFile, Map<String, String> env, Map mavenProps) throws IllegalArgumentException {
Properties envProps = new Properties();
if (env != null && env.size() > 0) {
JSONArray a = new JSONArray();
for (Map.Entry<String, String> entry : env.entrySet()) {
String value = entry.getValue();
if (value == null || value.length() == 0) {
throw new IllegalArgumentException(String.format("Env variable '%s' must not be null or empty",
entry.getKey()));
}
envProps.put(entry.getKey(), StrSubstitutor.replace(value, mavenProps));
}
}
if (envPropsFile != null) {
// Props from external file take precedence
addPropertiesFromFile(envPropsFile, envProps);
}
if (envProps.size() > 0) {
addEnvironment(envProps);
}
return this;
}
public ContainerCreateConfig exposedPorts(Set<String> portSpecs) {
if (portSpecs != null && portSpecs.size() > 0) {
JSONObject exposedPorts = new JSONObject();
for (String portSpec : portSpecs) {
exposedPorts.put(portSpec, new JSONObject());
}
createConfig.put("ExposedPorts", exposedPorts);
}
return this;
}
public String getImageName() {
return imageName;
}
public ContainerCreateConfig hostname(String hostname) {
return add("Hostname", hostname);
}
public ContainerCreateConfig memory(Long memory) {
return add("Memory", memory);
}
public ContainerCreateConfig memorySwap(Long memorySwap) {
return add("MemorySwap", memorySwap);
}
public ContainerCreateConfig user(String user) {
return add("User", user);
}
public ContainerCreateConfig workingDir(String workingDir) {
return add("WorkingDir", workingDir);
}
public ContainerCreateConfig hostConfig(ContainerHostConfig startConfig) {
return add("HostConfig", startConfig.toJsonObject());
}
/**
* Get JSON which is used for <em>creating</em> a container
*
* @return string representation for JSON representing creating a container
*/
public String toJson() {
return createConfig.toString();
}
// =======================================================================
private ContainerCreateConfig add(String name, Object value) {
if (value != null) {
createConfig.put(name, value);
}
return this;
}
private String extractContainerPath(String volume) {
if (volume.contains(":")) {
String[] parts = volume.split(":");
if (parts.length > 1) {
return parts[1];
}
}
return volume;
}
private void addEnvironment(Properties envProps) {
JSONArray containerEnv = new JSONArray();
Enumeration keys = envProps.keys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
String value = envProps.getProperty(key);
if (value == null || value.length() == 0) {
throw new IllegalArgumentException(String.format("Env variable '%s' must not be null or empty",key));
}
containerEnv.put(key + "=" + value);
}
createConfig.put("Env", containerEnv);
}
private void addPropertiesFromFile(String envPropsFile, Properties envProps) {
// External properties override internally specified properties
try {
FileReader reader = new FileReader(envPropsFile);
envProps.load(reader);
} catch (FileNotFoundException e) {
throw new IllegalArgumentException(String.format("Cannot find environment property file '%s'", envPropsFile));
} catch (IOException e) {
throw new IllegalArgumentException(String.format("Error while loading environment properties: %s", e.getMessage()), e);
}
}
}