Search in sources :

Example 6 with IDockerPortBinding

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>&lt;containerPort&gt;&lt;type&gt;:&lt;hostIP&gt;:&lt;hostPort&gt;</code>
 *         Note that the <code>&lt;hostIP&gt;</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;
}
Also used : IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 7 with IDockerPortBinding

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;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) DockerContainer(org.eclipse.linuxtools.internal.docker.core.DockerContainer) IDockerNetworkSettings(org.eclipse.linuxtools.docker.core.IDockerNetworkSettings) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) Job(org.eclipse.core.runtime.jobs.Job)

Example 8 with IDockerPortBinding

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();
}
Also used : IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) DockerHostConfig(org.eclipse.linuxtools.internal.docker.core.DockerHostConfig) ExposedPortModel(org.eclipse.linuxtools.internal.docker.ui.wizards.ImageRunSelectionModel.ExposedPortModel) HashMap(java.util.HashMap) Builder(org.eclipse.linuxtools.internal.docker.core.DockerContainerConfig.Builder) ArrayList(java.util.ArrayList) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) DockerPortBinding(org.eclipse.linuxtools.internal.docker.core.DockerPortBinding) ContainerLinkModel(org.eclipse.linuxtools.internal.docker.ui.wizards.ImageRunSelectionModel.ContainerLinkModel) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IDockerPortBinding (org.eclipse.linuxtools.docker.core.IDockerPortBinding)8 List (java.util.List)6 IDockerHostConfig (org.eclipse.linuxtools.docker.core.IDockerHostConfig)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)3 IDockerContainerConfig (org.eclipse.linuxtools.docker.core.IDockerContainerConfig)3 DockerHostConfig (org.eclipse.linuxtools.internal.docker.core.DockerHostConfig)3 DockerPortBinding (org.eclipse.linuxtools.internal.docker.core.DockerPortBinding)3 Map (java.util.Map)2 DockerException (org.eclipse.linuxtools.docker.core.DockerException)2 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)2 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)2 IDockerNetworkSettings (org.eclipse.linuxtools.docker.core.IDockerNetworkSettings)2 DockerContainerConfig (org.eclipse.linuxtools.internal.docker.core.DockerContainerConfig)2 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 HostConfig (com.spotify.docker.client.messages.HostConfig)1 LxcConfParameter (com.spotify.docker.client.messages.HostConfig.LxcConfParameter)1 PortBinding (com.spotify.docker.client.messages.PortBinding)1