Search in sources :

Example 76 with IDockerConnection

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

the class CopyFromContainerCommandHandler method performCopyFromContainer.

private void performCopyFromContainer(final IDockerConnection connection, final IDockerContainer container, final String target, final List<ContainerFileProxy> files) {
    final Job copyFromContainerJob = new Job(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_TITLE, container.name())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(CommandMessages.getString(COPY_FROM_CONTAINER_JOB_TASK), files.size());
            try (Closeable token = ((DockerConnection) connection).getOperationToken()) {
                for (ContainerFileProxy proxy : files) {
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    try {
                        monitor.setTaskName(CommandMessages.getFormattedString(COPY_FROM_CONTAINER_JOB_SUBTASK, proxy.getFullPath()));
                        monitor.worked(1);
                        InputStream in = ((DockerConnection) connection).copyContainer(token, container.id(), proxy.getLink());
                        /*
							 * The input stream from copyContainer might be
							 * incomplete or non-blocking so we should wrap it
							 * in a stream that is guaranteed to block until
							 * data is available.
							 */
                        TarArchiveInputStream k = new TarArchiveInputStream(new BlockingInputStream(in));
                        TarArchiveEntry te = null;
                        while ((te = k.getNextTarEntry()) != null) {
                            long size = te.getSize();
                            IPath path = new Path(target);
                            path = path.append(te.getName());
                            File f = new File(path.toOSString());
                            if (te.isDirectory()) {
                                f.mkdir();
                                continue;
                            } else {
                                f.createNewFile();
                            }
                            FileOutputStream os = new FileOutputStream(f);
                            int bufferSize = ((int) size > 4096 ? 4096 : (int) size);
                            byte[] barray = new byte[bufferSize];
                            int result = -1;
                            while ((result = k.read(barray, 0, bufferSize)) > -1) {
                                if (monitor.isCanceled()) {
                                    monitor.done();
                                    k.close();
                                    os.close();
                                    return Status.CANCEL_STATUS;
                                }
                                os.write(barray, 0, result);
                            }
                            os.close();
                        }
                        k.close();
                    } catch (final DockerException e) {
                        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), CommandMessages.getFormattedString(ERROR_COPYING_FROM_CONTAINER, proxy.getLink(), container.name()), e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
                    }
                }
            } catch (InterruptedException e) {
            // do nothing
            } catch (IOException e) {
                Activator.log(e);
            } catch (DockerException e1) {
                Activator.log(e1);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    copyFromContainerJob.schedule();
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) DockerException(org.eclipse.linuxtools.docker.core.DockerException) IPath(org.eclipse.core.runtime.IPath) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) Closeable(java.io.Closeable) IOException(java.io.IOException) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ContainerFileProxy(org.eclipse.linuxtools.internal.docker.core.ContainerFileProxy) FileOutputStream(java.io.FileOutputStream) Job(org.eclipse.core.runtime.jobs.Job) File(java.io.File)

Example 77 with IDockerConnection

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

the class CopyToContainerCommandHandler method performCopyToContainer.

private void performCopyToContainer(final IDockerConnection connection, final IDockerContainer container, final String target, final List<Object> files) {
    final Job copyToContainerJob = new Job(CommandMessages.getFormattedString(COPY_TO_CONTAINER_JOB_TITLE, container.name())) {

        @Override
        protected IStatus run(final IProgressMonitor monitor) {
            monitor.beginTask(CommandMessages.getString(COPY_TO_CONTAINER_JOB_TASK), files.size() + 1);
            java.nio.file.Path tmpDir = null;
            try (Closeable token = ((DockerConnection) connection).getOperationToken()) {
                for (Object proxy : files) {
                    File file = (File) proxy;
                    if (monitor.isCanceled()) {
                        monitor.done();
                        return Status.CANCEL_STATUS;
                    }
                    try {
                        monitor.setTaskName(CommandMessages.getFormattedString(COPY_TO_CONTAINER_JOB_SUBTASK, proxy.toString()));
                        monitor.worked(1);
                        // files to a temporary directory
                        if (tmpDir == null) {
                            tmpDir = Files.createTempDirectory(Activator.PLUGIN_ID);
                        }
                        if (file.isDirectory()) {
                            java.nio.file.Path sourcePath = FileSystems.getDefault().getPath(file.getAbsolutePath());
                            final java.nio.file.Path target = tmpDir.resolve(sourcePath.getFileName());
                            Files.createDirectory(target);
                            Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {

                                @Override
                                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
                                    java.nio.file.Path targetPath = target.resolve(sourcePath.relativize(dir));
                                    Files.createDirectories(targetPath);
                                    return FileVisitResult.CONTINUE;
                                }

                                @Override
                                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                                    monitor.setTaskName(CommandMessages.getFormattedString(COPY_TO_CONTAINER_JOB_SUBTASK, file.toString()));
                                    Files.copy(file, target.resolve(sourcePath.relativize(file)));
                                    return FileVisitResult.CONTINUE;
                                }
                            });
                        } else {
                            monitor.worked(1);
                            java.nio.file.Path sourcePath = FileSystems.getDefault().getPath(file.getAbsolutePath());
                            java.nio.file.Path targetPath = tmpDir.resolve(sourcePath.getFileName());
                            Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
                        }
                    } catch (final IOException e) {
                        Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), CommandMessages.getFormattedString(ERROR_COPYING_TO_CONTAINER, proxy.toString(), container.name()), e.getMessage()));
                    }
                }
                // via the temporary directory
                try {
                    ((DockerConnection) connection).copyToContainer(token, tmpDir.toString(), container.id(), target);
                    deleteTmpDir(tmpDir);
                } catch (final DockerException | IOException e) {
                    Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), CommandMessages.getFormattedString(ERROR_COPYING_TO_CONTAINER, target, container.name()), e.getCause() != null ? e.getCause().getMessage() : e.getMessage()));
                }
            } catch (InterruptedException e) {
            // do nothing
            } catch (IOException e1) {
                Activator.log(e1);
            } catch (DockerException e1) {
                Activator.log(e1);
            } finally {
                monitor.done();
            }
            return Status.OK_STATUS;
        }
    };
    copyToContainerJob.schedule();
}
Also used : Path(java.nio.file.Path) DockerException(org.eclipse.linuxtools.docker.core.DockerException) Closeable(java.io.Closeable) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) IDockerConnection(org.eclipse.linuxtools.docker.core.IDockerConnection) DockerConnection(org.eclipse.linuxtools.internal.docker.core.DockerConnection) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job) Path(java.nio.file.Path) File(java.io.File) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 78 with IDockerConnection

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

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

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

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