Search in sources :

Example 16 with IDockerContainer

use of org.eclipse.linuxtools.docker.core.IDockerContainer in project linuxtools by eclipse.

the class DockerConnectionTest method shouldLoadContainers.

@Test
public void shouldLoadContainers() throws DockerException {
    // given
    final Container fooContainer = MockContainerFactory.id("foo").build();
    final Container barContainer = MockContainerFactory.id("bar").build();
    final DockerClient client = MockDockerClientFactory.container(fooContainer).container(barContainer).build();
    final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).withDefaultTCPConnectionSettings();
    dockerConnection.open(false);
    // when
    final List<IDockerContainer> containers = dockerConnection.getContainers();
    // then
    assertThat(containers).hasSize(2);
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) DockerClient(com.spotify.docker.client.DockerClient) Test(org.junit.Test)

Example 17 with IDockerContainer

use of org.eclipse.linuxtools.docker.core.IDockerContainer in project linuxtools by eclipse.

the class DockerConnection method listContainers.

private List<IDockerContainer> listContainers() throws DockerException {
    final Map<String, IDockerContainer> updatedContainersById = new HashMap<>();
    List<IDockerContainer> sortedContainers;
    synchronized (containerLock) {
        try {
            final List<Container> nativeContainers = new ArrayList<>();
            synchronized (clientLock) {
                // containers list left in the queue
                if (client == null) {
                    // there's no client.
                    return Collections.emptyList();
                }
                nativeContainers.addAll(client.listContainers(DockerClient.ListContainersParam.allContainers()));
            }
            // in the future.
            for (Container nativeContainer : nativeContainers) {
                // them with a logging thread.
                if (nativeContainer.status() != null && nativeContainer.status().startsWith(Messages.Exited_specifier)) {
                    synchronized (loggingThreads) {
                        if (loggingThreads.containsKey(nativeContainer.id())) {
                            loggingThreads.get(nativeContainer.id()).requestStop();
                            loggingThreads.remove(nativeContainer.id());
                        }
                    }
                }
                // skip containers that are being removed
                if (nativeContainer.status() != null && nativeContainer.status().equals(Messages.Removal_In_Progress_specifier)) {
                    continue;
                }
                // re-use info from existing container with same id
                if (this.containers != null && this.containersById.containsKey(nativeContainer.id())) {
                    final IDockerContainer container = this.containersById.get(nativeContainer.id());
                    updatedContainersById.put(nativeContainer.id(), new DockerContainer(this, nativeContainer, container.info()));
                } else {
                    updatedContainersById.put(nativeContainer.id(), new DockerContainer(this, nativeContainer));
                }
            }
        } catch (DockerTimeoutException e) {
            if (isOpen()) {
                Activator.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, Messages.Docker_Connection_Timeout, e));
                close();
            }
        } catch (com.spotify.docker.client.exceptions.DockerException | InterruptedException e) {
            if (isOpen() && e.getCause() != null && e.getCause().getCause() != null && e.getCause().getCause() instanceof ProcessingException) {
                close();
            } else {
                throw new DockerException(e.getMessage());
            }
        } finally {
            this.containersById = updatedContainersById;
            sortedContainers = sort(updatedContainersById.values(), (container, otherContainer) -> container.name().compareTo(otherContainer.name()));
            this.containers = sortedContainers;
        }
    }
    // perform notification outside of containerLock so we don't have a View
    // causing a deadlock
    // TODO: we should probably notify the listeners only if the containers
    // list changed.
    notifyContainerListeners(sortedContainers);
    return sortedContainers;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Status(org.eclipse.core.runtime.Status) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) DockerException(org.eclipse.linuxtools.docker.core.DockerException) BuildParam(com.spotify.docker.client.DockerClient.BuildParam) Arrays(java.util.Arrays) IDockerVersion(org.eclipse.linuxtools.docker.core.IDockerVersion) DockerImageQualifier(org.eclipse.linuxtools.internal.docker.core.DockerImage.DockerImageQualifier) Info(com.spotify.docker.client.messages.Info) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerConnectionInfo(org.eclipse.linuxtools.docker.core.IDockerConnectionInfo) ILogger(org.eclipse.linuxtools.docker.core.ILogger) InetAddress(java.net.InetAddress) DockerClient(com.spotify.docker.client.DockerClient) Map(java.util.Map) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) IDockerImageInfo(org.eclipse.linuxtools.docker.core.IDockerImageInfo) Ipam(com.spotify.docker.client.messages.Ipam) NetworkInterface(java.net.NetworkInterface) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) Path(org.eclipse.core.runtime.Path) IDockerConnection2(org.eclipse.linuxtools.docker.core.IDockerConnection2) ProcessingException(javax.ws.rs.ProcessingException) IDockerHostConfig(org.eclipse.linuxtools.docker.core.IDockerHostConfig) NetworkConfig(com.spotify.docker.client.messages.NetworkConfig) IDockerNetworkCreation(org.eclipse.linuxtools.docker.core.IDockerNetworkCreation) IDockerProgressHandler(org.eclipse.linuxtools.docker.core.IDockerProgressHandler) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) ContainerConfig(com.spotify.docker.client.messages.ContainerConfig) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) ContainerInfo(com.spotify.docker.client.messages.ContainerInfo) IDockerConnectionSettings(org.eclipse.linuxtools.docker.core.IDockerConnectionSettings) ITerminalServiceOutputStreamMonitorListener(org.eclipse.tm.terminal.view.core.interfaces.ITerminalServiceOutputStreamMonitorListener) Network(com.spotify.docker.client.messages.Network) ListenerList(org.eclipse.core.runtime.ListenerList) ArrayList(java.util.ArrayList) ITerminalsConnectorConstants(org.eclipse.tm.terminal.view.core.interfaces.constants.ITerminalsConnectorConstants) LogsParam(com.spotify.docker.client.DockerClient.LogsParam) SocketException(java.net.SocketException) IDockerConfParameter(org.eclipse.linuxtools.docker.core.IDockerConfParameter) LxcConfParameter(com.spotify.docker.client.messages.HostConfig.LxcConfParameter) IRegistryAccount(org.eclipse.linuxtools.docker.core.IRegistryAccount) EnumDockerConnectionState(org.eclipse.linuxtools.docker.core.EnumDockerConnectionState) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) IOException(java.io.IOException) IDockerNetworkConfig(org.eclipse.linuxtools.docker.core.IDockerNetworkConfig) Messages(org.eclipse.linuxtools.docker.core.Messages) UnknownHostException(java.net.UnknownHostException) BindingType(org.eclipse.linuxtools.docker.core.IDockerConnectionSettings.BindingType) IDockerImageBuildOptions(org.eclipse.linuxtools.docker.core.IDockerImageBuildOptions) ITerminalService(org.eclipse.tm.terminal.view.core.interfaces.ITerminalService) Container(com.spotify.docker.client.messages.Container) ContainerChange(com.spotify.docker.client.messages.ContainerChange) IDockerImageSearchResult(org.eclipse.linuxtools.docker.core.IDockerImageSearchResult) IDockerNetwork(org.eclipse.linuxtools.docker.core.IDockerNetwork) SecurePreferencesFactory(org.eclipse.equinox.security.storage.SecurePreferencesFactory) ImageSearchResult(com.spotify.docker.client.messages.ImageSearchResult) WritableByteChannel(java.nio.channels.WritableByteChannel) FileSystems(java.nio.file.FileSystems) RegistryAuth(com.spotify.docker.client.messages.RegistryAuth) AttachParameter(com.spotify.docker.client.DockerClient.AttachParameter) IDockerIpamConfig(org.eclipse.linuxtools.docker.core.IDockerIpamConfig) ByteBuffer(java.nio.ByteBuffer) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus) IStatus(org.eclipse.core.runtime.IStatus) Activator(org.eclipse.linuxtools.docker.core.Activator) IDockerImageListener(org.eclipse.linuxtools.docker.core.IDockerImageListener) IPath(org.eclipse.core.runtime.IPath) EncodingUtils(org.eclipse.equinox.security.storage.EncodingUtils) TerminalServiceFactory(org.eclipse.tm.terminal.view.core.TerminalServiceFactory) IDockerContainerChange(org.eclipse.linuxtools.docker.core.IDockerContainerChange) ImageInfo(com.spotify.docker.client.messages.ImageInfo) IDockerPortBinding(org.eclipse.linuxtools.docker.core.IDockerPortBinding) NLS(org.eclipse.osgi.util.NLS) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) DockerConnectionManager(org.eclipse.linuxtools.docker.core.DockerConnectionManager) StorageException(org.eclipse.equinox.security.storage.StorageException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ContainerExit(com.spotify.docker.client.messages.ContainerExit) List(java.util.List) ExecCreateParam(com.spotify.docker.client.DockerClient.ExecCreateParam) Version(com.spotify.docker.client.messages.Version) Entry(java.util.Map.Entry) IDockerImageHierarchyNode(org.eclipse.linuxtools.docker.core.IDockerImageHierarchyNode) HostConfig(com.spotify.docker.client.messages.HostConfig) IDockerContainerListener(org.eclipse.linuxtools.docker.core.IDockerContainerListener) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) HashMap(java.util.HashMap) LogStream(com.spotify.docker.client.LogStream) HashSet(java.util.HashSet) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerContainerConfig(org.eclipse.linuxtools.docker.core.IDockerContainerConfig) ContainerCreation(com.spotify.docker.client.messages.ContainerCreation) OutputStream(java.io.OutputStream) Job(org.eclipse.core.runtime.jobs.Job) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException) ExecCreation(com.spotify.docker.client.messages.ExecCreation) Image(com.spotify.docker.client.messages.Image) PortBinding(com.spotify.docker.client.messages.PortBinding) Closeable(java.io.Closeable) Comparator(java.util.Comparator) Collections(java.util.Collections) IDockerContainerExit(org.eclipse.linuxtools.docker.core.IDockerContainerExit) InputStream(java.io.InputStream) ISecurePreferences(org.eclipse.equinox.security.storage.ISecurePreferences) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) HashMap(java.util.HashMap) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ArrayList(java.util.ArrayList) IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) Container(com.spotify.docker.client.messages.Container) ProcessingException(javax.ws.rs.ProcessingException)

Example 18 with IDockerContainer

use of org.eclipse.linuxtools.docker.core.IDockerContainer in project linuxtools by eclipse.

the class DockerContainersView method setConnection.

/**
 * Sets the active connection
 *
 * @param connection
 *            the active connection
 */
public void setConnection(final IDockerConnection connection) {
    if (connection != null && connection.equals(this.connection)) {
        return;
    }
    // remove 'this' as listener on the previous connection (if applicable)
    if (this.connection != null) {
        this.connection.removeContainerListener(this);
    }
    this.connection = connection;
    if (this.viewer != null && this.connection != null) {
        final Job refreshJob = new Job(DVMessages.getString("ContainersRefresh.msg")) {

            @Override
            protected IStatus run(IProgressMonitor monitor) {
                connection.getContainers(true);
                setLabelFilterIds();
                connection.addContainerListener(DockerContainersView.this);
                Display.getDefault().asyncExec(() -> {
                    viewer.setInput(connection);
                    refreshViewTitle();
                });
                return Status.OK_STATUS;
            }
        };
        refreshJob.schedule();
    } else if (this.viewer != null) {
        viewer.setInput(new IDockerContainer[0]);
        form.setText(DVMessages.getString(NoConnectionSelected));
    }
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 19 with IDockerContainer

use of org.eclipse.linuxtools.docker.core.IDockerContainer in project linuxtools by eclipse.

the class DockerExplorerContentProvider method loadContainerInfo.

/**
 * Call the {@link IDockerConnection#getContainers(boolean)} in a background
 * job to avoid blocking the UI.
 *
 * @param container
 *            the selected {@link DockerContainersCategory}
 */
private void loadContainerInfo(final IDockerContainer container) {
    final Job loadContainersJob = new LoadingJob(// $NON-NLS-1$
    DVMessages.getString("ContainerInfoLoadJob.msg"), // $NON-NLS-1$
    container) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            ((DockerContainer) container).info(true);
            return Status.OK_STATUS;
        }
    };
    loadContainersJob.schedule();
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) DockerContainer(org.eclipse.linuxtools.internal.docker.core.DockerContainer) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 20 with IDockerContainer

use of org.eclipse.linuxtools.docker.core.IDockerContainer 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)

Aggregations

IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)25 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)9 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)8 Job (org.eclipse.core.runtime.jobs.Job)7 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)7 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)6 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)4 IDockerImageHierarchyNode (org.eclipse.linuxtools.docker.core.IDockerImageHierarchyNode)4 Test (org.junit.Test)4 DockerException (org.eclipse.linuxtools.docker.core.DockerException)3 RunConsole (org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole)3 DockerClient (com.spotify.docker.client.DockerClient)2 Container (com.spotify.docker.client.messages.Container)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 DockerContainerNotFoundException (org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)2 EnumDockerLoggingStatus (org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus)2 EnumDockerStatus (org.eclipse.linuxtools.docker.core.EnumDockerStatus)2 IDockerHostConfig (org.eclipse.linuxtools.docker.core.IDockerHostConfig)2 IDockerPortBinding (org.eclipse.linuxtools.docker.core.IDockerPortBinding)2