Search in sources :

Example 81 with IDockerConnection

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

the class PushImageCommandHandler method performPushImage.

private void performPushImage(final ImagePush wizard, final IDockerConnection connection) {
    if (connection == null) {
        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_PUSHING_IMAGE, wizard.getSelectedImageTag()), DVMessages.getFormattedString(NO_CONNECTION)));
        return;
    }
    final Job pushImageJob = new Job(DVMessages.getFormattedString(PUSH_IMAGE_JOB_TITLE, wizard.getSelectedImageTag())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            final IDockerImage image = wizard.getImage();
            final String defaultImageNameTag = wizard.getDefaultImageName();
            final String selectedImageNameTag = wizard.getSelectedImageTag();
            // TODO: remove cast once AbstractRegistry methods are
            // part of the IRegistry interface
            final AbstractRegistry registry = (AbstractRegistry) wizard.getRegistry();
            final boolean forceTagging = wizard.isForceTagging();
            final boolean keepTaggedImage = wizard.isKeepTaggedImage();
            monitor.beginTask(DVMessages.getString(PUSH_IMAGE_JOB_TASK), IProgressMonitor.UNKNOWN);
            // push the image and let the progress
            // handler refresh the images when done
            final String tmpRegistryTag = getNameToTag(selectedImageNameTag, registry);
            boolean tagCreated = false;
            try {
                // selected)
                if (!image.repoTags().contains(tmpRegistryTag) || forceTagging) {
                    // TODO: remove cast to DockerConnection once the
                    // 'tagImage' is added in the public API
                    ((DockerConnection) connection).tagImage(defaultImageNameTag, tmpRegistryTag, forceTagging);
                    tagCreated = true;
                }
                // push image
                if (!registry.isAuthProvided()) {
                    connection.pushImage(tmpRegistryTag, new DefaultImagePushProgressHandler(connection, tmpRegistryTag));
                } else {
                    final IRegistryAccount registryAccount = (IRegistryAccount) registry;
                    connection.pushImage(tmpRegistryTag, registryAccount, new DefaultImagePushProgressHandler(connection, tmpRegistryTag));
                }
            } catch (final DockerException e) {
                Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_PUSHING_IMAGE, defaultImageNameTag), e.getMessage()));
                Activator.logErrorMessage(DVMessages.getFormattedString(ERROR_PUSHING_IMAGE, defaultImageNameTag), e);
            // for now
            } catch (InterruptedException e) {
            // do nothing
            } finally {
                if (tagCreated && !keepTaggedImage) {
                    try {
                        connection.removeTag(tmpRegistryTag);
                        connection.getImages(true);
                    } catch (Exception e) {
                    // do nothing
                    }
                }
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    pushImageJob.schedule();
}
Also used : IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) AbstractRegistry(org.eclipse.linuxtools.docker.core.AbstractRegistry) DefaultImagePushProgressHandler(org.eclipse.linuxtools.internal.docker.core.DefaultImagePushProgressHandler) IDockerImage(org.eclipse.linuxtools.docker.core.IDockerImage) IRegistryAccount(org.eclipse.linuxtools.docker.core.IRegistryAccount) Job(org.eclipse.core.runtime.jobs.Job) DockerException(org.eclipse.linuxtools.docker.core.DockerException)

Example 82 with IDockerConnection

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

the class RefreshCommandHandler method getRefreshJobs.

private List<Job> getRefreshJobs(final IWorkbenchPart activePart) {
    final IDockerConnection connection = getCurrentConnection(activePart);
    final ArrayList<Job> jobs = new ArrayList<>();
    if (activePart instanceof DockerImagesView) {
        jobs.add(getRefreshImagesJob(connection));
    } else if (activePart instanceof DockerContainersView) {
        jobs.add(getRefreshContainersJob(connection));
    } else if (activePart instanceof DockerExplorerView) {
        DockerExplorerView dockerExplorerView = (DockerExplorerView) activePart;
        final ITreeSelection selection = dockerExplorerView.getCommonViewer().getStructuredSelection();
        if (selection.getFirstElement() instanceof DockerContainersCategory) {
            jobs.add(getRefreshContainersJob(connection));
        } else if (selection.getFirstElement() instanceof DockerImagesCategory) {
            jobs.add(getRefreshImagesJob(connection));
        } else {
            final IDockerConnection[] connections = DockerConnectionManager.getInstance().getConnections();
            for (IDockerConnection selectedConnection : connections) {
                if (!selectedConnection.isOpen()) {
                    try {
                        selectedConnection.open(true);
                    } catch (DockerException e) {
                    // do nothing
                    }
                }
                if (selectedConnection.isOpen()) {
                    jobs.add(getRefreshContainersJob(selectedConnection));
                    jobs.add(getRefreshImagesJob(selectedConnection));
                }
            }
        }
    }
    return jobs;
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) DockerExplorerView(org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerView) ITreeSelection(org.eclipse.jface.viewers.ITreeSelection) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) ArrayList(java.util.ArrayList) DockerImagesView(org.eclipse.linuxtools.internal.docker.ui.views.DockerImagesView) DockerContainersView(org.eclipse.linuxtools.internal.docker.ui.views.DockerContainersView) Job(org.eclipse.core.runtime.jobs.Job) DockerContainersCategory(org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerContainersCategory) DockerImagesCategory(org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerContentProvider.DockerImagesCategory)

Example 83 with IDockerConnection

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

the class RefreshConnectionHandler method getRefreshJobs.

private List<Job> getRefreshJobs(final IWorkbenchPart activePart) {
    final IDockerConnection connection = getCurrentConnection(activePart);
    final ArrayList<Job> jobs = new ArrayList<>();
    if (!connection.isOpen()) {
        try {
            connection.open(true);
        } catch (DockerException e) {
        // do nothing
        }
    }
    if (connection.isOpen()) {
        jobs.add(getRefreshContainersJob(connection));
        jobs.add(getRefreshImagesJob(connection));
    }
    return jobs;
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) ArrayList(java.util.ArrayList) Job(org.eclipse.core.runtime.jobs.Job)

Example 84 with IDockerConnection

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

the class RemoveTagCommandHandler method performRemoveTagImage.

private void performRemoveTagImage(final IDockerConnection connection, final String tag) {
    final Job removeTagImageJob = new Job(DVMessages.getString(REMOVE_TAG_JOB_TITLE)) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getString(REMOVE_TAG_MSG), 2);
            try {
                ((DockerConnection) connection).removeTag(tag);
                monitor.worked(1);
                ((DockerConnection) connection).getImages(true);
                monitor.worked(1);
            } catch (final DockerException e) {
                Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_REMOVING_TAG_IMAGE, tag), e.getMessage()));
            // for now
            } catch (InterruptedException e) {
            // do nothing
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    removeTagImageJob.schedule();
}
Also used : IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 85 with IDockerConnection

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

IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)90 DockerException (org.eclipse.linuxtools.docker.core.DockerException)24 DockerConnection (org.eclipse.linuxtools.internal.docker.core.DockerConnection)20 Job (org.eclipse.core.runtime.jobs.Job)17 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)16 IDockerImage (org.eclipse.linuxtools.docker.core.IDockerImage)15 Test (org.junit.Test)15 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)12 SWTBotTreeItem (org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem)9 File (java.io.File)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8 IDockerContainer (org.eclipse.linuxtools.docker.core.IDockerContainer)8 IPath (org.eclipse.core.runtime.IPath)7 ITreeSelection (org.eclipse.jface.viewers.ITreeSelection)7 List (java.util.List)5 IDockerConnectionStorageManager (org.eclipse.linuxtools.docker.core.IDockerConnectionStorageManager)5 RunConsole (org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole)5 DockerClient (com.spotify.docker.client.DockerClient)4 HashMap (java.util.HashMap)4