Search in sources :

Example 6 with DockerContainerNotFoundException

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

the class DockerConnection method waitForContainer.

@Override
public IDockerContainerExit waitForContainer(final String id) throws DockerException, InterruptedException {
    try {
        // wait for container to exit
        DockerClient copy = getClientCopy();
        ContainerExit x = copy.waitContainer(id);
        DockerContainerExit exit = new DockerContainerExit(x.statusCode());
        // update container list
        listContainers();
        // dispose of copy now we are finished
        copy.close();
        return exit;
    } 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) DockerClient(com.spotify.docker.client.DockerClient) ContainerExit(com.spotify.docker.client.messages.ContainerExit) IDockerContainerExit(org.eclipse.linuxtools.docker.core.IDockerContainerExit) IDockerContainerExit(org.eclipse.linuxtools.docker.core.IDockerContainerExit) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Example 7 with DockerContainerNotFoundException

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

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

Example 9 with DockerContainerNotFoundException

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

the class DockerConnection method unpauseContainer.

@Override
public void unpauseContainer(final String id, final OutputStream stream) throws DockerException, InterruptedException {
    try {
        // unpause container
        client.unpauseContainer(id);
        if (stream != null) {
            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();
                } else {
                    // we aren't going to use the stream given...close it
                    try {
                        stream.close();
                    } catch (IOException e) {
                    // do nothing...we tried to close the stream
                    }
                }
            }
        }
        // update container list
        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) IOException(java.io.IOException) ContainerNotFoundException(com.spotify.docker.client.exceptions.ContainerNotFoundException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)

Example 10 with DockerContainerNotFoundException

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

the class RunImageCommandHandler method runImage.

/**
 * Run the given {@link IDockerImage} with the given settings
 *
 * @param image
 * @param containerConfig
 * @param hostConfig
 * @param containerName
 * @param removeWhenExits
 */
public static void runImage(final IDockerImage image, final IDockerContainerConfig containerConfig, final IDockerHostConfig hostConfig, final String containerName, final boolean removeWhenExits) {
    final IDockerConnection connection = image.getConnection();
    if (containerConfig.tty()) {
        // show the console view
        Display.getDefault().asyncExec(() -> {
            try {
                PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(IConsoleConstants.ID_CONSOLE_VIEW);
            } catch (Exception e) {
                Activator.log(e);
            }
        });
    }
    // Create the container in a non-UI thread.
    final Job runImageJob = new Job(// $NON-NLS-1$
    DVMessages.getString("RunImageCreateContainer.job")) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getString("RunImageRunningTask.msg"), // $NON-NLS-1$
            2);
            String containerId = null;
            RunConsole console = null;
            try {
                final SubMonitor createContainerMonitor = SubMonitor.convert(monitor, 1);
                // create the container
                createContainerMonitor.beginTask(DVMessages.getString(// $NON-NLS-1$
                "RunImageCreatingContainerTask.msg"), 1);
                containerId = ((DockerConnection) connection).createContainer(containerConfig, hostConfig, containerName);
                final IDockerContainer container = ((DockerConnection) connection).getContainer(containerId);
                createContainerMonitor.done();
                // abort if operation was cancelled
                if (monitor.isCanceled()) {
                    return Status.CANCEL_STATUS;
                }
                // start the container
                final SubMonitor startContainerMonitor = SubMonitor.convert(monitor, 1);
                startContainerMonitor.beginTask(DVMessages.getString("RunImageStartingContainerTask.msg"), // $NON-NLS-1$
                1);
                console = getRunConsole(connection, container);
                if (console != null) {
                    // if we are auto-logging, show the console
                    console.showConsole();
                    ((DockerConnection) connection).startContainer(containerId, console.getOutputStream());
                } else {
                    ((DockerConnection) connection).startContainer(containerId, null);
                }
                startContainerMonitor.done();
                // create a launch configuration from the container
                LaunchConfigurationUtils.createRunImageLaunchConfiguration(image, containerConfig, hostConfig, containerName, removeWhenExits);
            } catch (final DockerException | InterruptedException e) {
                if (console != null) {
                    RunConsole.removeConsole(console);
                }
                Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_CREATING_CONTAINER, containerConfig.image()), e.getMessage()));
            } finally {
                final String tmpContainerId = containerId;
                if (removeWhenExits) {
                    final Job waitContainerJob = new Job(CommandMessages.getString(// $NON-NLS-1$
                    "RunImageCommandHandler.waitContainer.label")) {

                        @Override
                        protected IStatus run(IProgressMonitor monitor) {
                            try {
                                if (tmpContainerId != null) {
                                    // Wait for the container to finish
                                    ((DockerConnection) connection).waitForContainer(tmpContainerId);
                                    // Drain the logging thread before we remove the
                                    // container
                                    ((DockerConnection) connection).stopLoggingThread(tmpContainerId);
                                    while (((DockerConnection) connection).loggingStatus(tmpContainerId) == EnumDockerLoggingStatus.LOGGING_ACTIVE) {
                                        Thread.sleep(1000);
                                    }
                                }
                            } catch (DockerContainerNotFoundException e) {
                                // container not created correctly
                                return Status.OK_STATUS;
                            } catch (DockerException | InterruptedException e) {
                            // ignore any errors in waiting for container or
                            // draining log
                            }
                            try {
                                // try and remove the container if it was created
                                if (tmpContainerId != null)
                                    ((DockerConnection) connection).removeContainer(tmpContainerId);
                            } catch (DockerException | InterruptedException e) {
                                final String id = tmpContainerId;
                                Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_REMOVING_CONTAINER, id), e.getMessage()));
                            }
                            return Status.OK_STATUS;
                        }
                    };
                    // Do not display this job in the UI
                    waitContainerJob.setSystem(true);
                    waitContainerJob.schedule();
                }
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    runImageJob.schedule();
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) SubMonitor(org.eclipse.core.runtime.SubMonitor) DockerContainerNotFoundException(org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException) CoreException(org.eclipse.core.runtime.CoreException) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) RunConsole(org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole) CommandUtils.getRunConsole(org.eclipse.linuxtools.internal.docker.ui.commands.CommandUtils.getRunConsole) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) Job(org.eclipse.core.runtime.jobs.Job)

Aggregations

DockerContainerNotFoundException (org.eclipse.linuxtools.docker.core.DockerContainerNotFoundException)10 DockerException (org.eclipse.linuxtools.docker.core.DockerException)10 ContainerNotFoundException (com.spotify.docker.client.exceptions.ContainerNotFoundException)9 IOException (java.io.IOException)2 IDockerContainerInfo (org.eclipse.linuxtools.docker.core.IDockerContainerInfo)2 DockerClient (com.spotify.docker.client.DockerClient)1 ContainerConfig (com.spotify.docker.client.messages.ContainerConfig)1 ContainerCreation (com.spotify.docker.client.messages.ContainerCreation)1 ContainerExit (com.spotify.docker.client.messages.ContainerExit)1 ContainerInfo (com.spotify.docker.client.messages.ContainerInfo)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 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CoreException (org.eclipse.core.runtime.CoreException)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ListenerList (org.eclipse.core.runtime.ListenerList)1 SubMonitor (org.eclipse.core.runtime.SubMonitor)1