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();
}
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();
}
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;
}
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;
}
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();
}
Aggregations