Search in sources :

Example 66 with DockerException

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

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

the class EnableConnectionCommandHandler method execute.

@Override
public Object execute(ExecutionEvent event) {
    final IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
    if (activePart instanceof CommonNavigator) {
        final CommonViewer viewer = ((CommonNavigator) activePart).getCommonViewer();
        final ITreeSelection selection = (ITreeSelection) viewer.getSelection();
        for (TreePath treePath : selection.getPaths()) {
            final IDockerConnection conn = (IDockerConnection) treePath.getLastSegment();
            if (!conn.isOpen()) {
                final Job openConnectionJob = new Job(CommandMessages.getFormattedString(// $NON-NLS-1$
                "command.enableconnection", conn.getUri())) {

                    @Override
                    protected IStatus run(IProgressMonitor monitor) {
                        try {
                            conn.open(true);
                            Display.getDefault().asyncExec(() -> viewer.refresh(conn));
                        } catch (DockerException e) {
                            Activator.logErrorMessage(CommandMessages.getString(// $NON-NLS-1$
                            "command.enableconnection.failure"), e);
                            return Status.CANCEL_STATUS;
                        }
                        return Status.OK_STATUS;
                    }
                };
                openConnectionJob.schedule();
            }
        }
    }
    return null;
}
Also used : DockerException(org.eclipse.linuxtools.docker.core.DockerException) CommonNavigator(org.eclipse.ui.navigator.CommonNavigator) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITreeSelection(org.eclipse.jface.viewers.ITreeSelection) TreePath(org.eclipse.jface.viewers.TreePath) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) CommonViewer(org.eclipse.ui.navigator.CommonViewer) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) Job(org.eclipse.core.runtime.jobs.Job)

Example 68 with DockerException

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

the class PullImageCommandHandler method performPullImage.

private void performPullImage(final IDockerConnection connection, final String imageName, final AbstractRegistry registry) {
    final Job pullImageJob = new Job(DVMessages.getFormattedString(PULL_IMAGE_JOB_TITLE, imageName)) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(DVMessages.getString(PULL_IMAGE_JOB_TASK), IProgressMonitor.UNKNOWN);
            // handler refresh the images when done
            try {
                if (registry == null || registry.isDockerHubRegistry()) {
                    ((DockerConnection) connection).pullImage(imageName, new DefaultImagePullProgressHandler(connection, imageName));
                } else {
                    String fullImageName = registry.getServerHost() + '/' + imageName;
                    if (registry instanceof IRegistryAccount) {
                        IRegistryAccount account = (IRegistryAccount) registry;
                        ((DockerConnection) connection).pullImage(fullImageName, account, new DefaultImagePullProgressHandler(connection, fullImageName));
                    } else {
                        ((DockerConnection) connection).pullImage(fullImageName, new DefaultImagePullProgressHandler(connection, fullImageName));
                    }
                }
            } catch (final DockerException e) {
                Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_PULLING_IMAGE, imageName), e.getMessage()));
            // for now
            } catch (InterruptedException | DockerCertificateException e) {
            // do nothing
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    pullImageJob.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) IRegistryAccount(org.eclipse.linuxtools.docker.core.IRegistryAccount) DockerCertificateException(com.spotify.docker.client.exceptions.DockerCertificateException) Job(org.eclipse.core.runtime.jobs.Job) DefaultImagePullProgressHandler(org.eclipse.linuxtools.internal.docker.core.DefaultImagePullProgressHandler)

Example 69 with DockerException

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

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

Aggregations

DockerException (org.eclipse.linuxtools.docker.core.DockerException)78 IDockerConnection (org.eclipse.linuxtools.docker.core.IDockerConnection)26 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