
io.fabric8.maven.docker.access.ContainerHostConfig Maven / Gradle / Ivy
The newest version!
package io.fabric8.maven.docker.access;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import io.fabric8.maven.docker.config.LogConfiguration;
import org.json.JSONArray;
import org.json.JSONObject;
public class ContainerHostConfig {
final JSONObject startConfig = new JSONObject();
public ContainerHostConfig() {}
public ContainerHostConfig binds(List bind) {
if (bind != null && !bind.isEmpty()) {
JSONArray binds = new JSONArray();
for (String volume : bind) {
if (volume.contains(":")) {
// Hack-fix for mounting on Windows where the ${projectDir} variable and other
// contain backslashes and what not. Related to #188
volume = volume.replace("\\", "/").replaceAll("^(?i:C:)", "/c");
binds.put(volume);
}
}
startConfig.put("Binds", binds);
}
return this;
}
public ContainerHostConfig capAdd(List capAdd) {
return addAsArray("CapAdd", capAdd);
}
public ContainerHostConfig capDrop(List capDrop) {
return addAsArray("CapDrop", capDrop);
}
public ContainerHostConfig dns(List dns) {
return addAsArray("Dns", dns);
}
public ContainerHostConfig networkConfig(String net) {
return add("NetworkMode",net);
}
public ContainerHostConfig dnsSearch(List dnsSearch) {
return addAsArray("DnsSearch", dnsSearch);
}
public ContainerHostConfig extraHosts(List extraHosts) throws IllegalArgumentException {
if (extraHosts != null) {
List mapped = new ArrayList<>();
for (int i = 0; i < extraHosts.size(); i++) {
String[] parts = extraHosts.get(i).split(":");
if (parts.length == 1) {
throw new IllegalArgumentException("extraHosts must be in the form ");
}
try {
mapped.add(i, parts[0] + ":" + InetAddress.getByName(parts[1]).getHostAddress());
} catch (UnknownHostException e) {
throw new IllegalArgumentException("unable to resolve ip address for " + parts[1], e);
}
}
return addAsArray("ExtraHosts", mapped);
}
return this;
}
public ContainerHostConfig volumesFrom(List volumesFrom) {
return addAsArray("VolumesFrom", volumesFrom);
}
public ContainerHostConfig links(List links) {
return addAsArray("Links", links);
}
public ContainerHostConfig portBindings(PortMapping portMapping) {
JSONObject portBindings = portMapping.toDockerPortBindingsJson();
if (portBindings != null) {
startConfig.put("PortBindings", portBindings);
}
return this;
}
public ContainerHostConfig privileged(Boolean privileged) {
return add("Privileged", privileged);
}
public ContainerHostConfig restartPolicy(String name, int retry) {
if (name != null) {
JSONObject policy = new JSONObject();
policy.put("Name", name);
policy.put("MaximumRetryCount", retry);
startConfig.put("RestartPolicy", policy);
}
return this;
}
public ContainerHostConfig logConfig(LogConfiguration logConfig) {
if (logConfig != null) {
LogConfiguration.LogDriver logDriver = logConfig.getDriver();
if (logDriver != null) {
JSONObject logConfigJson = new JSONObject();
logConfigJson.put("Type", logDriver.getName());
Map opts = logDriver.getOpts();
if (opts != null && opts.size() > 0) {
JSONObject config = new JSONObject();
for (Map.Entry logOpt : opts.entrySet()) {
config.put(logOpt.getKey(), logOpt.getValue());
}
logConfigJson.put("Config", config);
}
startConfig.put("LogConfig", logConfigJson);
}
}
return this;
}
/**
* Get JSON which is used for starting a container
*
* @return string representation for JSON representing the configuration for starting a container
*/
public String toJson() {
return startConfig.toString();
}
public Object toJsonObject() {
return startConfig;
}
ContainerHostConfig addAsArray(String propKey, List props) {
if (props != null) {
startConfig.put(propKey, new JSONArray(props));
}
return this;
}
private ContainerHostConfig add(String name, Object value) {
if (value != null) {
startConfig.put(name, value);
}
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy