use of org.eclipse.linuxtools.docker.core.IDockerPortBinding in project linuxtools by eclipse.
the class LaunchConfigurationUtils method serializePortBindings.
/**
* Serializes the given Port Bindings to save them in an
* {@link ILaunchConfiguration}
*
* @param bindings
* @return a {@link List} of port bindings serialized in the following
* format:
* <code><containerPort><type>:<hostIP>:<hostPort></code>
* Note that the <code><hostIP></code> part may be empty if
* undefined by the user.
*/
public static List<String> serializePortBindings(final Map<String, List<IDockerPortBinding>> bindings) {
final List<String> serializedBindings = new ArrayList<>();
if (bindings != null) {
for (Entry<String, List<IDockerPortBinding>> entry : bindings.entrySet()) {
for (IDockerPortBinding portBinding : entry.getValue()) {
final StringBuilder portBindingBuilder = new StringBuilder();
portBindingBuilder.append(entry.getKey());
// $NON-NLS-1$
portBindingBuilder.append(':');
if (portBinding.hostIp() != null) {
portBindingBuilder.append(portBinding.hostIp());
}
// $NON-NLS-1$
portBindingBuilder.append(':');
portBindingBuilder.append(portBinding.hostPort());
serializedBindings.add(portBindingBuilder.toString());
}
}
}
return serializedBindings;
}
use of org.eclipse.linuxtools.docker.core.IDockerPortBinding in project linuxtools by eclipse.
the class DockerExplorerContentProvider method getChildren.
@Override
public Object[] getChildren(final Object parentElement) {
if (parentElement instanceof IDockerConnection) {
// check the connection availability before returning the
// 'containers' and 'images' child nodes.
final IDockerConnection connection = (IDockerConnection) parentElement;
if (connection.isOpen()) {
return new Object[] { new DockerImagesCategory(connection), new DockerContainersCategory(connection) };
} else if (connection.getState() == EnumDockerConnectionState.UNKNOWN) {
open(connection);
return new Object[] { new LoadingStub(connection) };
} else if (connection.getState() == EnumDockerConnectionState.CLOSED) {
synchronized (openRetryJobs) {
Job job = openRetryJobs.get(connection);
if (job == null) {
openRetry(connection);
}
}
return new Object[] { new LoadingStub(connection) };
}
return new Object[0];
} else if (parentElement instanceof DockerContainersCategory) {
final DockerContainersCategory containersCategory = (DockerContainersCategory) parentElement;
final IDockerConnection connection = containersCategory.getConnection();
if (connection.isContainersLoaded()) {
return connection.getContainers().toArray();
}
loadContainers(containersCategory);
return new Object[] { new LoadingStub(containersCategory) };
} else if (parentElement instanceof DockerImagesCategory) {
final DockerImagesCategory imagesCategory = (DockerImagesCategory) parentElement;
final IDockerConnection connection = imagesCategory.getConnection();
if (connection.isImagesLoaded()) {
// here we duplicate the images to show one org/repo with all
// its tags per node in the tree
final List<IDockerImage> allImages = connection.getImages();
final List<IDockerImage> explorerImages = splitImageTagsByRepo(allImages);
return explorerImages.toArray();
}
loadImages(imagesCategory);
return new Object[] { new LoadingStub(imagesCategory) };
} else if (parentElement instanceof IDockerContainer) {
final DockerContainer container = (DockerContainer) parentElement;
if (container.isInfoLoaded()) {
final IDockerContainerInfo info = container.info();
final IDockerNetworkSettings networkSettings = (info != null) ? info.networkSettings() : null;
final IDockerHostConfig hostConfig = (info != null) ? info.hostConfig() : null;
return new Object[] { new DockerContainerPortMappingsCategory(container, (networkSettings != null) ? networkSettings.ports() : Collections.<String, List<IDockerPortBinding>>emptyMap()), new DockerContainerVolumesCategory(container, (hostConfig != null) ? hostConfig.binds() : Collections.<String>emptyList()), new DockerContainerLinksCategory(container, (hostConfig != null) ? hostConfig.links() : Collections.<String>emptyList()) };
}
loadContainerInfo(container);
return new Object[] { new LoadingStub(container) };
} else if (parentElement instanceof DockerContainerLinksCategory) {
final DockerContainerLinksCategory linksCategory = (DockerContainerLinksCategory) parentElement;
return linksCategory.getLinks().toArray();
} else if (parentElement instanceof DockerContainerPortMappingsCategory) {
final DockerContainerPortMappingsCategory portMappingsCategory = (DockerContainerPortMappingsCategory) parentElement;
return portMappingsCategory.getPortMappings().toArray();
} else if (parentElement instanceof DockerContainerVolumesCategory) {
final DockerContainerVolumesCategory volumesCategory = (DockerContainerVolumesCategory) parentElement;
return volumesCategory.getVolumes().toArray();
}
return EMPTY;
}
use of org.eclipse.linuxtools.docker.core.IDockerPortBinding in project linuxtools by eclipse.
the class ImageRun method getDockerHostConfig.
public IDockerHostConfig getDockerHostConfig() {
final ImageRunSelectionModel selectionModel = this.imageRunSelectionPage.getModel();
final ImageRunResourceVolumesVariablesModel resourcesModel = this.imageRunResourceVolumesPage.getModel();
final ImageRunNetworkModel networkModel = this.imageRunNetworkPage.getModel();
final DockerHostConfig.Builder hostConfigBuilder = new DockerHostConfig.Builder();
if (selectionModel.isPublishAllPorts()) {
hostConfigBuilder.publishAllPorts(true);
} else {
final Map<String, List<IDockerPortBinding>> portBindings = new HashMap<>();
for (Iterator<ExposedPortModel> iterator = selectionModel.getExposedPorts().iterator(); iterator.hasNext(); ) {
final ExposedPortModel exposedPort = iterator.next();
// only selected Ports in the CheckboxTableViewer are exposed.
if (!selectionModel.getSelectedPorts().contains(exposedPort)) {
continue;
}
final DockerPortBinding portBinding = new DockerPortBinding(exposedPort.getHostAddress(), exposedPort.getHostPort());
portBindings.put(exposedPort.getContainerPort() + exposedPort.getPortType(), Arrays.<IDockerPortBinding>asList(portBinding));
}
hostConfigBuilder.portBindings(portBindings);
}
// container links
final List<String> links = new ArrayList<>();
for (Iterator<ContainerLinkModel> iterator = selectionModel.getLinks().iterator(); iterator.hasNext(); ) {
final ContainerLinkModel link = iterator.next();
links.add(link.getContainerName() + ':' + link.getContainerAlias());
}
hostConfigBuilder.links(links);
// data volumes
final List<String> volumesFrom = new ArrayList<>();
final List<String> binds = new ArrayList<>();
for (Iterator<DataVolumeModel> iterator = resourcesModel.getDataVolumes().iterator(); iterator.hasNext(); ) {
final DataVolumeModel dataVolume = iterator.next();
// included.
if (!resourcesModel.getSelectedDataVolumes().contains(dataVolume)) {
continue;
}
switch(dataVolume.getMountType()) {
case HOST_FILE_SYSTEM:
String bind = LaunchConfigurationUtils.convertToUnixPath(dataVolume.getHostPathMount()) + ':' + dataVolume.getContainerPath() + // $NON-NLS-1$ //$NON-NLS-2$
":Z";
if (dataVolume.isReadOnly()) {
// $NON-NLS-1$
bind += ",ro";
}
binds.add(bind);
break;
case CONTAINER:
volumesFrom.add(dataVolume.getContainerMount());
break;
default:
break;
}
}
hostConfigBuilder.binds(binds);
hostConfigBuilder.volumesFrom(volumesFrom);
hostConfigBuilder.privileged(selectionModel.isPrivileged());
// make /tmp and /run use tmpfs, and drop all capabilities
if (selectionModel.isBasicSecurity()) {
hostConfigBuilder.readonlyRootfs(true);
Map<String, String> tmpfsValues = new HashMap<>();
// $NON-NLS-1$ //$NON-NLS-2$
tmpfsValues.put("/run", "rw,exec");
// $NON-NLS-1$ //$NON-NLS-2$
tmpfsValues.put("/tmp", "rw,exec");
hostConfigBuilder.tmpfs(tmpfsValues);
List<String> capDropList = new ArrayList<>();
// $NON-NLS-1$
capDropList.add("all");
hostConfigBuilder.capDrop(capDropList);
}
if (selectionModel.isUnconfined()) {
List<String> seccomp = new ArrayList<>();
// $NON-NLS-1$
seccomp.add("seccomp:unconfined");
hostConfigBuilder.securityOpt(seccomp);
}
String networkMode = networkModel.getNetworkModeString();
// if network mode is not default, set it in host config
if (networkMode != null && !networkMode.equals(ImageRunNetworkModel.DEFAULT_MODE))
hostConfigBuilder.networkMode(networkMode);
// memory constraints (in bytes)
if (resourcesModel.isEnableResourceLimitations()) {
hostConfigBuilder.memory(resourcesModel.getMemoryLimit() * MB);
hostConfigBuilder.cpuShares(resourcesModel.getCpuShareWeight());
}
return hostConfigBuilder.build();
}
Aggregations