use of org.eclipse.linuxtools.docker.core.IDockerContainerConfig in project linuxtools by eclipse.
the class LaunchConfigurationUtils method createRunImageLaunchConfiguration.
/**
* Creates a new {@link ILaunchConfiguration} for the given
* {@link IDockerContainer}.
*
* @param baseConfigurationName
* the base configuration name to use when creating the
* {@link ILaunchConfiguration}.
* @param image
* the {@link IDockerImage} used to create the container
* @param containerName
* the actual container name (given by the user or generated by
* the Docker daemon)
* @param containerConfig
* @param hostConfig
* the user-provided {@link IDockerHostConfig} (created
* container's one)
* @param removeWhenExits
* flag to indicate if container should be removed when exited
* @return the generated {@link ILaunchConfiguration}
*/
public static ILaunchConfiguration createRunImageLaunchConfiguration(final IDockerImage image, final IDockerContainerConfig containerConfig, final IDockerHostConfig hostConfig, final String containerName, final boolean removeWhenExits) {
try {
final ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
final ILaunchConfigurationType type = manager.getLaunchConfigurationType(IRunDockerImageLaunchConfigurationConstants.CONFIG_TYPE_ID);
final String imageName = createRunImageLaunchConfigurationName(image);
// using the image repo + first tag
final ILaunchConfigurationWorkingCopy workingCopy = getLaunchConfigurationWorkingCopy(type, imageName);
workingCopy.setAttribute(CREATION_DATE, DATE_FORMAT.format(new Date()));
workingCopy.setAttribute(CONNECTION_NAME, image.getConnection().getName());
workingCopy.setAttribute(IMAGE_ID, image.id());
workingCopy.setAttribute(IMAGE_NAME, createRunImageLaunchConfigurationName(image));
if (containerName != null && !containerName.isEmpty()) {
workingCopy.setAttribute(CONTAINER_NAME, containerName);
}
// if we know the raw command string, use it since the container
// config will remove quotes to split up command properly
DockerContainerConfig config = (DockerContainerConfig) containerConfig;
if (config.rawcmd() != null) {
workingCopy.setAttribute(COMMAND, config.rawcmd());
} else {
workingCopy.setAttribute(COMMAND, toString(containerConfig.cmd()));
}
workingCopy.setAttribute(ENTRYPOINT, toString(containerConfig.entrypoint()));
// selected ports
workingCopy.setAttribute(PUBLISH_ALL_PORTS, hostConfig.publishAllPorts());
// format: <containerPort><type>:<hostIP>:<hostPort>
if (hostConfig.publishAllPorts()) {
final IDockerImageInfo imageInfo = image.getConnection().getImageInfo(image.id());
if (imageInfo != null) {
workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(imageInfo.containerConfig().exposedPorts()));
}
} else {
workingCopy.setAttribute(PUBLISHED_PORTS, serializePortBindings(hostConfig.portBindings()));
}
// links (with format being: "<containerName>:<containerAlias>")
workingCopy.setAttribute(LINKS, hostConfig.links());
// env variables
workingCopy.setAttribute(ENV_VARIABLES, containerConfig.env());
// labels
workingCopy.setAttribute(LABELS, containerConfig.labels());
// volumes
final List<String> volumes = new ArrayList<>();
// volumes from other containers
for (String volumeFrom : hostConfig.volumesFrom()) {
final DataVolumeModel volume = DataVolumeModel.parseVolumeFrom(volumeFrom);
if (volume != null) {
volumes.add(volume.toString());
}
}
// bindings to host directory or file
for (String bind : hostConfig.binds()) {
final DataVolumeModel volume = DataVolumeModel.parseHostBinding(bind);
if (volume != null) {
volumes.add(volume.toString());
}
}
// TODO: container path declaration
workingCopy.setAttribute(DATA_VOLUMES, volumes);
// options
workingCopy.setAttribute(AUTO_REMOVE, removeWhenExits);
workingCopy.setAttribute(ALLOCATE_PSEUDO_CONSOLE, containerConfig.tty());
workingCopy.setAttribute(INTERACTIVE, containerConfig.openStdin());
workingCopy.setAttribute(PRIVILEGED, hostConfig.privileged());
workingCopy.setAttribute(READONLY, ((DockerHostConfig) hostConfig).readonlyRootfs());
if (hostConfig.networkMode() != null)
workingCopy.setAttribute(NETWORK_MODE, hostConfig.networkMode());
// resources limitations
if (containerConfig.memory() != null) {
workingCopy.setAttribute(ENABLE_LIMITS, true);
// memory in containerConfig is expressed in bytes
workingCopy.setAttribute(MEMORY_LIMIT, Long.toString(containerConfig.memory().longValue() / MB));
}
if (containerConfig.cpuShares() != null) {
workingCopy.setAttribute(ENABLE_LIMITS, true);
workingCopy.setAttribute(CPU_PRIORITY, containerConfig.cpuShares().toString());
}
return workingCopy.doSave();
} catch (CoreException e) {
Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, LaunchMessages.getString(// $NON-NLS-1$
"RunDockerImageLaunchConfiguration.creation.failure"), e));
}
return null;
}
use of org.eclipse.linuxtools.docker.core.IDockerContainerConfig in project linuxtools by eclipse.
the class RunDockerImageLaunchConfigurationDelegate method launch.
@Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) {
try {
ILaunchConfiguration config = launch.getLaunchConfiguration();
final IDockerContainerConfig containerConfig = getDockerContainerConfig(config);
final IDockerHostConfig hostConfig = getDockerHostConfig(config);
final IDockerConnection connection = getDockerConnection(config);
if (connection == null)
return;
final IDockerImage image = getDockerImage(config, connection);
RunImageCommandHandler.runImage(image, containerConfig, hostConfig, config.getAttribute(IRunDockerImageLaunchConfigurationConstants.CONTAINER_NAME, (String) null), config.getAttribute(IRunDockerImageLaunchConfigurationConstants.AUTO_REMOVE, false));
} catch (CoreException e) {
Activator.log(e);
}
}
use of org.eclipse.linuxtools.docker.core.IDockerContainerConfig in project linuxtools by eclipse.
the class DockerConnection method createContainer.
@Override
public String createContainer(final IDockerContainerConfig c, IDockerHostConfig hc, final String containerName) throws DockerException, InterruptedException {
try {
HostConfig.Builder hbuilder = HostConfig.builder().containerIdFile(hc.containerIDFile()).publishAllPorts(hc.publishAllPorts()).privileged(hc.privileged()).networkMode(hc.networkMode()).readonlyRootfs(((DockerHostConfig) hc).readonlyRootfs());
if (((DockerHostConfig) hc).tmpfs() != null) {
hbuilder.tmpfs(ImmutableMap.copyOf(((DockerHostConfig) hc).tmpfs()));
}
if (((DockerHostConfig) hc).capAdd() != null) {
hbuilder.capAdd(((DockerHostConfig) hc).capAdd());
}
if (((DockerHostConfig) hc).capDrop() != null) {
hbuilder.capDrop(((DockerHostConfig) hc).capDrop());
}
if (hc.binds() != null)
hbuilder.binds(hc.binds());
if (hc.dns() != null)
hbuilder.dns(hc.dns());
if (hc.dnsSearch() != null)
hbuilder.dnsSearch(hc.dnsSearch());
if (hc.links() != null)
hbuilder.links(hc.links());
if (hc.lxcConf() != null) {
List<IDockerConfParameter> lxcconf = hc.lxcConf();
ArrayList<LxcConfParameter> lxcreal = new ArrayList<>();
for (IDockerConfParameter param : lxcconf) {
// TODO: Fix This
}
hbuilder.lxcConf(lxcreal);
}
if (hc.portBindings() != null) {
Map<String, List<IDockerPortBinding>> bindings = hc.portBindings();
HashMap<String, List<PortBinding>> realBindings = new HashMap<>();
for (Entry<String, List<IDockerPortBinding>> entry : bindings.entrySet()) {
String key = entry.getKey();
List<IDockerPortBinding> bindingList = entry.getValue();
ArrayList<PortBinding> newList = new ArrayList<>();
for (IDockerPortBinding binding : bindingList) {
newList.add(PortBinding.of(binding.hostIp(), binding.hostPort()));
}
realBindings.put(key, newList);
}
hbuilder.portBindings(realBindings);
}
if (hc.volumesFrom() != null) {
hbuilder.volumesFrom(hc.volumesFrom());
}
if (hc.securityOpt() != null) {
hbuilder.securityOpt(hc.securityOpt());
}
// interface
if (((DockerHostConfig) hc).memory() != null) {
hbuilder.memory(((DockerHostConfig) hc).memory());
}
// interface
if (((DockerHostConfig) hc).cpuShares() != null && ((DockerHostConfig) hc).cpuShares().longValue() > 0) {
hbuilder.cpuShares(((DockerHostConfig) hc).cpuShares());
}
ContainerConfig.Builder builder = ContainerConfig.builder().hostname(c.hostname()).domainname(c.domainname()).user(c.user()).attachStdin(c.attachStdin()).attachStdout(c.attachStdout()).attachStderr(c.attachStderr()).tty(c.tty()).openStdin(c.openStdin()).stdinOnce(c.stdinOnce()).cmd(c.cmd()).image(c.image()).hostConfig(hbuilder.build()).workingDir(c.workingDir()).labels(c.labels()).networkDisabled(c.networkDisabled());
// don't set those fields in the builder.
if (c.portSpecs() != null) {
builder = builder.portSpecs(c.portSpecs());
}
if (c.exposedPorts() != null) {
builder = builder.exposedPorts(c.exposedPorts());
}
if (c.env() != null) {
builder = builder.env(c.env());
}
if (c.volumes() != null) {
builder = builder.volumes(c.volumes());
}
if (c.entrypoint() != null) {
builder = builder.entrypoint(c.entrypoint());
}
if (c.onBuild() != null) {
builder = builder.onBuild(c.onBuild());
}
// create container with default random name if an empty/null
// containerName argument was passed
final ContainerCreation creation = client.createContainer(builder.build(), (containerName != null && !containerName.isEmpty()) ? containerName : null);
final String id = creation.id();
// force a refresh of the current containers to include the new one
listContainers();
return id;
} catch (ContainerNotFoundException e) {
throw new DockerContainerNotFoundException(e);
} catch (com.spotify.docker.client.exceptions.DockerRequestException e) {
throw new DockerException(e.message());
} catch (com.spotify.docker.client.exceptions.DockerException e) {
throw new DockerException(e);
}
}
use of org.eclipse.linuxtools.docker.core.IDockerContainerConfig in project linuxtools by eclipse.
the class ContainerInspectContentProvider method getChildren.
@Override
public Object[] getChildren(Object parentElement) {
final Object propertyValue = ((Object[]) parentElement)[1];
if (propertyValue instanceof IDockerContainerState) {
final IDockerContainerState containerState = (IDockerContainerState) propertyValue;
return new Object[] { // $NON-NLS-1$
new Object[] { "ExitCode", containerState.exitCode() }, // $NON-NLS-1$
new Object[] { "Finished at", LabelProviderUtils.toFinishedDate(containerState.finishDate()) }, // $NON-NLS-1$
new Object[] { "Running", containerState.running() }, // $NON-NLS-1$
new Object[] { "Paused", containerState.paused() }, // $NON-NLS-1$
new Object[] { "Pid", containerState.pid() } };
} else if (propertyValue instanceof IDockerHostConfig) {
final DockerHostConfig hostConfig = (DockerHostConfig) propertyValue;
return new Object[] { // $NON-NLS-1$
new Object[] { "Binds", LabelProviderUtils.reduce(hostConfig.binds()) }, // $NON-NLS-1$
new Object[] { "CapAdd", hostConfig.capAdd() }, // $NON-NLS-1$
new Object[] { "CapDrop", hostConfig.capDrop() }, // $NON-NLS-1$
new Object[] { "ContainerIDFile", hostConfig.containerIDFile() }, // $NON-NLS-1$
new Object[] { "Dns", LabelProviderUtils.reduce(hostConfig.dns()) }, // $NON-NLS-1$
new Object[] { "DnsSearch", LabelProviderUtils.reduce(hostConfig.dnsSearch()) }, // $NON-NLS-1$
new Object[] { "Links", splitLinks(hostConfig.links()) }, // $NON-NLS-1$
new Object[] { "LxcConf", hostConfig.lxcConf() }, // $NON-NLS-1$
new Object[] { "NetworkMode", hostConfig.networkMode() }, // $NON-NLS-1$
new Object[] { "PortBindings", LabelProviderUtils.reduce(hostConfig.portBindings()) }, // $NON-NLS-1$
new Object[] { "Privileged", hostConfig.privileged() }, // $NON-NLS-1$
new Object[] { "PublishAllPorts", hostConfig.publishAllPorts() }, new Object[] { // $NON-NLS-1$
"ReadonlyRootfs", hostConfig.readonlyRootfs() }, // $NON-NLS-1$
new Object[] { "SecurityOpt", hostConfig.securityOpt() }, // $NON-NLS-1$
new Object[] { "Tmpfs", hostConfig.tmpfs() }, // $NON-NLS-1$
new Object[] { "VolumesFrom", LabelProviderUtils.reduce(hostConfig.volumesFrom()) } };
} else if (propertyValue instanceof IDockerContainerConfig) {
final IDockerContainerConfig config = (IDockerContainerConfig) propertyValue;
return new Object[] { // $NON-NLS-1$
new Object[] { "AttachStderr", config.attachStderr() }, // $NON-NLS-1$
new Object[] { "AttachStdin", config.attachStdin() }, // $NON-NLS-1$
new Object[] { "AttachStdout", config.attachStdout() }, // $NON-NLS-1$
new Object[] { "Cmd", LabelProviderUtils.reduce(config.cmd()) }, // $NON-NLS-1$
new Object[] { "CpuSet", config.cpuset() }, // $NON-NLS-1$
new Object[] { "CpuShares", config.cpuShares() }, // $NON-NLS-1$
new Object[] { "Domainname", config.domainname() }, // $NON-NLS-1$
new Object[] { "Entrypoint", LabelProviderUtils.reduce(config.entrypoint()) }, // $NON-NLS-1$
new Object[] { "Env", LabelProviderUtils.reduce(config.env()) }, // $NON-NLS-1$
new Object[] { "ExposedPorts", LabelProviderUtils.reduce(config.exposedPorts()) }, // $NON-NLS-1$
new Object[] { "Hostname", config.hostname() }, // $NON-NLS-1$
new Object[] { "Image", config.image() }, new Object[] { // $NON-NLS-1$
"Labels", ((DockerContainerConfig) config).labels() }, // $NON-NLS-1$
new Object[] { "Memory", config.memory() }, // $NON-NLS-1$
new Object[] { "MemorySwap", config.memorySwap() }, // $NON-NLS-1$
new Object[] { "NetworkDisabled", config.networkDisabled() }, // $NON-NLS-1$
new Object[] { "OnBuild", config.onBuild() }, // $NON-NLS-1$
new Object[] { "OpenStdin", config.openStdin() }, // $NON-NLS-1$
new Object[] { "PortSpecs", LabelProviderUtils.reduce(config.portSpecs()) }, // $NON-NLS-1$
new Object[] { "StdinOnce", config.stdinOnce() }, // $NON-NLS-1$
new Object[] { "Tty", config.tty() }, new Object[] { // $NON-NLS-1$
"Volumes", LabelProviderUtils.reduce(config.volumes().keySet()) }, // $NON-NLS-1$
new Object[] { "WorkingDir", config.workingDir() } };
} else if (propertyValue instanceof IDockerPortBinding) {
final IDockerPortBinding portBinding = (IDockerPortBinding) propertyValue;
return new Object[] { // $NON-NLS-1$
new Object[] { "Host IP/Port", LabelProviderUtils.toString(portBinding) } };
} else if (propertyValue instanceof IDockerNetworkSettings) {
final IDockerNetworkSettings networkSettings = (IDockerNetworkSettings) propertyValue;
return new Object[] { // $NON-NLS-1$
new Object[] { "Bridge", networkSettings.bridge() }, // $NON-NLS-1$
new Object[] { "Gateway", networkSettings.gateway() }, // $NON-NLS-1$
new Object[] { "IPAddress", networkSettings.ipAddress() }, // $NON-NLS-1$
new Object[] { "IPPrefixLen", networkSettings.ipPrefixLen() }, // $NON-NLS-1$
new Object[] { "PortMapping", networkSettings.portMapping() }, // $NON-NLS-1$
new Object[] { "Ports", LabelProviderUtils.reduce(networkSettings.ports()) } };
} else if (propertyValue instanceof List<?>) {
@SuppressWarnings("unchecked") final List<Object> propertyValues = (List<Object>) propertyValue;
final Object[] result = new Object[propertyValues.size()];
for (int i = 0; i < propertyValues.size(); i++) {
// $NON-NLS-1$
result[i] = new Object[] { "", LabelProviderUtils.toString(propertyValues.get(i)) };
}
return result;
} else if (propertyValue instanceof Set<?>) {
@SuppressWarnings("unchecked") final Set<Object> propertyValues = (Set<Object>) propertyValue;
final Object[] result = new Object[propertyValues.size()];
Iterator<Object> iterator = propertyValues.iterator();
for (int i = 0; i < propertyValues.size(); i++) {
result[i] = new Object[] { // $NON-NLS-1$
"", LabelProviderUtils.toString(iterator.next()) };
}
return result;
} else if (propertyValue instanceof Map<?, ?>) {
final Map<?, ?> propertyValues = (Map<?, ?>) propertyValue;
final Object[] result = new Object[propertyValues.size()];
int i = 0;
for (Entry<?, ?> entry : propertyValues.entrySet()) {
result[i] = new Object[] { entry.getKey(), entry.getValue() };
i++;
}
return result;
}
return EMPTY;
}
use of org.eclipse.linuxtools.docker.core.IDockerContainerConfig in project linuxtools by eclipse.
the class RunDockerImageLaunchConfigurationDelegate method getDockerContainerConfig.
public IDockerContainerConfig getDockerContainerConfig(ILaunchConfiguration lconfig) throws CoreException {
String command = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.COMMAND, // $NON-NLS-1$
"");
String entrypoint = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.ENTRYPOINT, // $NON-NLS-1$
"");
String imageName = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.IMAGE_NAME, // $NON-NLS-1$
"");
boolean useTTY = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.ALLOCATE_PSEUDO_CONSOLE, false);
boolean interactive = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.INTERACTIVE, false);
final Builder config = new DockerContainerConfig.Builder().cmd(command).entryPoint(entrypoint).image(imageName).tty(useTTY).openStdin(interactive);
boolean limits_enabled = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.ENABLE_LIMITS, false);
if (limits_enabled) {
long memory = Long.parseLong(lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.MEMORY_LIMIT, "0"));
config.memory(memory);
long cpuShares = Long.parseLong(lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.CPU_PRIORITY, "0"));
config.cpuShares(cpuShares);
}
// environment variables
final List<String> environmentVariables = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.ENV_VARIABLES, new ArrayList<String>());
config.env(environmentVariables);
// labels
final Map<String, String> labelVariables = lconfig.getAttribute(IRunDockerImageLaunchConfigurationConstants.LABELS, (Map<String, String>) null);
if (labelVariables != null)
config.labels(labelVariables);
return config.build();
}
Aggregations