-
Notifications
You must be signed in to change notification settings - Fork 642
/
ContainerCreateConfig.java
171 lines (145 loc) · 5.5 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
package io.fabric8.maven.docker.access;
import java.io.*;
import java.util.*;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.config.Arguments;
import org.apache.commons.text.StrSubstitutor;
import org.json.JSONArray;
import org.json.JSONObject;
public class ContainerCreateConfig {
private final JSONObject createConfig = new JSONObject();
private 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(Arguments command) {
if (command != null) {
createConfig.put("Cmd", new JSONArray(command.asStrings()));
}
return this;
}
public ContainerCreateConfig domainname(String domainname) {
return add("Domainname", domainname);
}
public ContainerCreateConfig entrypoint(Arguments entrypoint) {
if (entrypoint != null) {
createConfig.put("Entrypoint", new JSONArray(entrypoint.asStrings()));
}
return this;
}
public ContainerCreateConfig environment(String envPropsFile, Map<String, String> env, Map mavenProps) throws IllegalArgumentException {
Properties envProps = new Properties();
if (env != null && env.size() > 0) {
for (Map.Entry<String, String> entry : env.entrySet()) {
String value = entry.getValue();
if (value == null) {
value = "";
}
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 labels(Map<String,String> labels) {
if (labels != null && labels.size() > 0) {
createConfig.put("Labels", new JSONObject(labels));
}
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 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());
}
public ContainerCreateConfig networkingConfig(ContainerNetworkingConfig networkingConfig) {
return add("NetworkingConfig", networkingConfig.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) {
String path = EnvUtil.fixupPath(volume);
if (path.contains(":")) {
String[] parts = path.split(":");
if (parts.length > 1) {
return parts[1];
}
}
return path;
}
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 = "";
}
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);
}
}
}