Search in sources :

Example 21 with IDockerContainer

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

the class HideStoppedContainersViewerFilter method select.

@Override
public boolean select(final Viewer viewer, final Object parentElement, final Object element) {
    if (element instanceof IDockerContainer) {
        final IDockerContainer container = (IDockerContainer) element;
        final EnumDockerStatus containerStatus = EnumDockerStatus.fromStatusMessage(container.status());
        if (containerStatus == EnumDockerStatus.RUNNING || containerStatus == EnumDockerStatus.PAUSED) {
            return true;
        }
        return false;
    }
    return true;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) EnumDockerStatus(org.eclipse.linuxtools.docker.core.EnumDockerStatus)

Example 22 with IDockerContainer

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

the class ContainerDataVolumeDialog method validateInput.

private IStatus validateInput() {
    final String containerPath = model.getContainerPath();
    final MountType mountType = model.getMountType();
    final String hostPath = model.getHostPathMount();
    if (containerPath == null || containerPath.isEmpty()) {
        return ValidationStatus.error(null);
    } else if (mountType == null) {
        return ValidationStatus.error(null);
    } else if (mountType == MountType.HOST_FILE_SYSTEM && (hostPath == null || hostPath.isEmpty())) {
        return ValidationStatus.error(null);
    } else if (mountType == MountType.HOST_FILE_SYSTEM && !new File(hostPath).exists()) {
        return ValidationStatus.warning(// $NON-NLS-1$
        "The specified path does not exist on the host.");
    } else if (mountType == MountType.CONTAINER) {
        final IDockerContainer container = WizardUtils.getContainer(connection, model.getContainerMount());
        if (container == null) {
            // just make sure that the dialog cannot complete
            return ValidationStatus.error(null);
        }
        final IDockerContainerInfo selectedContainerInfo = container.info();
        if (selectedContainerInfo != null && selectedContainerInfo.volumes() != null && !selectedContainerInfo.volumes().containsKey(model.getContainerPath())) {
            return ValidationStatus.warning(WizardMessages.getFormattedString(// $NON-NLS-1$
            "ContainerDataVolumeDialog.volumeWarning", model.getContainerPath()));
        }
    }
    return ValidationStatus.ok();
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) MountType(org.eclipse.linuxtools.internal.docker.ui.wizards.ImageRunResourceVolumesVariablesModel.MountType) IDockerContainerInfo(org.eclipse.linuxtools.docker.core.IDockerContainerInfo) File(java.io.File)

Example 23 with IDockerContainer

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

the class DisplayContainerLogCommandHandler method execute.

@Override
public Object execute(final ExecutionEvent event) {
    final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    final IDockerConnection connection = CommandUtils.getCurrentConnection(activePart);
    final List<IDockerContainer> selectedContainers = CommandUtils.getSelectedContainers(activePart);
    if (selectedContainers.size() != 1 || connection == null) {
        return null;
    }
    final IDockerContainer container = selectedContainers.get(0);
    final String id = container.id();
    final String name = container.name();
    if (connection.getContainerInfo(id).config().tty()) {
        RunConsole.attachToTerminal(connection, id, null);
        return null;
    }
    try {
        final RunConsole rc = RunConsole.findConsole(id);
        if (rc != null) {
            if (!rc.isAttached()) {
                rc.attachToConsole(connection);
            }
            Display.getDefault().syncExec(() -> rc.setTitle(DVMessages.getFormattedString(CONTAINER_LOG_TITLE, name)));
            OutputStream stream = rc.getOutputStream();
            // Only bother to ask for a log if
            // one isn't currently active
            EnumDockerLoggingStatus status = ((DockerConnection) connection).loggingStatus(id);
            if (status != EnumDockerLoggingStatus.LOGGING_ACTIVE && !((DockerConnection) connection).getContainerInfo(id).config().tty()) {
                rc.clearConsole();
                ((DockerConnection) connection).logContainer(id, stream);
            }
            rc.showConsole();
        }
    } catch (DockerException | InterruptedException e) {
        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_LOGGING_CONTAINER, id), e.getMessage()));
    }
    return null;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) DockerException(org.eclipse.linuxtools.docker.core.DockerException) RunConsole(org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) OutputStream(java.io.OutputStream) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) EnumDockerLoggingStatus(org.eclipse.linuxtools.docker.core.EnumDockerLoggingStatus)

Example 24 with IDockerContainer

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

Example 25 with IDockerContainer

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

the class ShowInPropertiesViewCommandHandler method execute.

@Override
public Object execute(final ExecutionEvent event) {
    final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    final List<IDockerContainer> containers = CommandUtils.getSelectedContainers(activePart);
    if (containers == null || containers.isEmpty()) {
        return null;
    }
    Display.getDefault().asyncExec(() -> {
        try {
            final PropertySheet propertySheet = (PropertySheet) PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(// $NON-NLS-1$
            "org.eclipse.ui.views.PropertySheet");
            final PropertyShowInContext showInContext = new PropertyShowInContext(activePart, HandlerUtil.getCurrentSelection(event));
            propertySheet.show(showInContext);
        } catch (PartInitException e) {
            Activator.logErrorMessage(CommandMessages.getString(// $NON-NLS-1$
            "command.showIn.propertiesView.failure"), e);
        }
    });
    return null;
}
Also used : IDockerContainer(org.eclipse.linuxtools.docker.core.IDockerContainer) PropertySheet(org.eclipse.ui.views.properties.PropertySheet) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) PropertyShowInContext(org.eclipse.ui.views.properties.PropertyShowInContext) PartInitException(org.eclipse.ui.PartInitException)

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