Search in sources :

Example 41 with DockerException

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

the class DockerConnection method openTerminal.

private void openTerminal(LogStream pty_stream, String name, DockerConsoleOutputStream out) throws DockerException {
    try {
        OutputStream tout = noBlockingOutputStream(HttpHijackWorkaround.getOutputStream(pty_stream, getUri()));
        InputStream tin = HttpHijackWorkaround.getInputStream(pty_stream);
        TerminalOutputMonitorListener monitor = new TerminalOutputMonitorListener(out);
        // org.eclipse.tm.terminal.connector.ssh.controls.SshWizardConfigurationPanel
        Map<String, Object> properties = new HashMap<>();
        properties.put(ITerminalsConnectorConstants.PROP_DELEGATE_ID, // $NON-NLS-1$
        "org.eclipse.tm.terminal.connector.streams.launcher.streams");
        properties.put(ITerminalsConnectorConstants.PROP_TERMINAL_CONNECTOR_ID, // $NON-NLS-1$
        "org.eclipse.tm.terminal.connector.streams.StreamsConnector");
        properties.put(ITerminalsConnectorConstants.PROP_TITLE, name);
        properties.put(ITerminalsConnectorConstants.PROP_LOCAL_ECHO, false);
        properties.put(ITerminalsConnectorConstants.PROP_FORCE_NEW, true);
        properties.put(ITerminalsConnectorConstants.PROP_STREAMS_STDIN, tout);
        properties.put(ITerminalsConnectorConstants.PROP_STREAMS_STDOUT, tin);
        properties.put(ITerminalsConnectorConstants.PROP_STDERR_LISTENERS, new ITerminalServiceOutputStreamMonitorListener[] { monitor });
        properties.put(ITerminalsConnectorConstants.PROP_STDOUT_LISTENERS, new ITerminalServiceOutputStreamMonitorListener[] { monitor });
        properties.put(ITerminalsConnectorConstants.PROP_DATA, pty_stream);
        /*
			 * The JVM will call finalize() on 'pty_stream' (LogStream)
			 * since we hold no references to it (although we do hold
			 * references to one of its heavily nested fields. The
			 * LogStream overrides finalize() to close the stream being
			 * used so we must preserve a reference to it.
			 */
        // $NON-NLS-1$
        properties.put("PREVENT_JVM_GC_FINALIZE", pty_stream);
        // save properties to remove terminal later
        if (out != null) {
            out.setTerminalProperties(properties);
        }
        ITerminalService service = TerminalServiceFactory.getService();
        service.openConsole(properties, null);
    } catch (Exception e) {
        throw new DockerException(e.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ITerminalService(org.eclipse.tm.terminal.view.core.interfaces.ITerminalService) DockerPingConnectionException(org.eclipse.linuxtools.docker.core.DockerPingConnectionException) DockerTimeoutException(com.spotify.docker.client.exceptions.DockerTimeoutException) ProcessingException(javax.ws.rs.ProcessingException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) StorageException(org.eclipse.equinox.security.storage.StorageException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerOpenConnectionException(org.eclipse.linuxtools.docker.core.DockerOpenConnectionException)

Example 42 with DockerException

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

the class DockerConnection method restartContainer.

public void restartContainer(final String id, int secondsToWait, final OutputStream stream) throws DockerException, InterruptedException {
    try {
        // restart container
        client.restartContainer(id, secondsToWait);
        // Log the started container if a stream is provided
        final IDockerContainerInfo containerInfo = getContainerInfo(id);
        if (stream != null && containerInfo != null && containerInfo.config() != null && !containerInfo.config().tty()) {
            // display logs for container
            synchronized (loggingThreads) {
                LogThread t = loggingThreads.get(id);
                if (t == null || !t.isAlive()) {
                    t = new LogThread(id, getClientCopy(), true);
                    loggingThreads.put(id, t);
                    t.setOutputStream(stream);
                    t.start();
                }
            }
        }
        // list of containers needs to be refreshed once the container
        // started, to reflect it new state.
        listContainers();
    } 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.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Example 43 with DockerException

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

the class DockerConnection method searchImages.

@Override
public List<IDockerImageSearchResult> searchImages(final String term) throws DockerException {
    try {
        final List<ImageSearchResult> searchResults = client.searchImages(term);
        final List<IDockerImageSearchResult> results = new ArrayList<>();
        for (ImageSearchResult r : searchResults) {
            if (r.name().contains(term)) {
                results.add(new DockerImageSearchResult(r.description(), r.official(), r.automated(), r.name(), r.starCount()));
            }
        }
        return results;
    } catch (com.spotify.docker.client.exceptions.DockerException | InterruptedException e) {
        throw new DockerException(e);
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerImageSearchResult(org.eclipse.linuxtools.docker.core.IDockerImageSearchResult) ArrayList(java.util.ArrayList) IDockerImageSearchResult(org.eclipse.linuxtools.docker.core.IDockerImageSearchResult) ImageSearchResult(com.spotify.docker.client.messages.ImageSearchResult) IDockerImageSearchResult(org.eclipse.linuxtools.docker.core.IDockerImageSearchResult)

Example 44 with DockerException

use of org.eclipse.linuxtools.docker.core.DockerException 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 45 with DockerException

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

the class DockerConnection method stopContainer.

@Override
public void stopContainer(final String id) throws DockerException, InterruptedException {
    try {
        // stop container or kill after 10 seconds
        // allow up to 10 seconds to stop
        client.stopContainer(id, 10);
        synchronized (loggingThreads) {
            if (loggingThreads.containsKey(id)) {
                loggingThreads.get(id).kill();
                loggingThreads.remove(id);
            }
        }
        // list of containers needs to be updated once the given container is stopped, to reflect it new state.
        listContainers();
    } 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.getMessage(), e.getCause());
    }
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Aggregations

DockerException (org.eclipse.linuxtools.docker.core.DockerException)80 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)27 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)19 Job (org.eclipse.core.runtime.jobs.Job)17 IOException (java.io.IOException)16 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)16 DockerContainerNotFoundException (org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)16 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)15 ArrayList (java.util.ArrayList)14 DockerClient (com.spotify.docker.client.DockerClient)13 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)10 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)9 DockerCertificateException (com.spotify.docker.client.exceptions.DockerCertificateException)8 DockerTimeoutException (com.spotify.docker.client.exceptions.DockerTimeoutException)7 ProcessingException (javax.ws.rs.ProcessingException)7 IPath (org.eclipse.core.runtime.IPath)7 RunConsole (org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole)7 LogStream (com.spotify.docker.client.LogStream)6 File (java.io.File)6 HashMap (java.util.HashMap)6