use of org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole in project linuxtools by eclipse.
the class RestartContainersCommandHandler method executeInJob.
@Override
void executeInJob(final IDockerContainer container, final IDockerConnection connection) {
try {
final RunConsole console = getRunConsole(connection, container);
long waitTime = Platform.getPreferencesService().getLong(// $NON-NLS-1$
"org.eclipse.linuxtools.docker.ui", PreferenceConstants.RESTART_WAIT_TIME, DEFAULT_WAIT_TIME, null);
if (console != null) {
// if we are auto-logging, show the console
console.showConsole();
// Start the container
((DockerConnection) connection).restartContainer(container.id(), (int) waitTime, console.getOutputStream());
} else {
((DockerConnection) connection).restartContainer(container.id(), (int) waitTime, null);
}
connection.getContainers(true);
} catch (DockerException | InterruptedException e) {
final String errorMessage = DVMessages.getFormattedString(CONTAINER_RESTART_ERROR_MSG, container.id());
openError(errorMessage, e);
}
}
use of org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole 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.internal.docker.ui.consoles.RunConsole in project linuxtools by eclipse.
the class RemoveContainersCommandHandler method executeInJob.
@Override
void executeInJob(final IDockerContainer container, final IDockerConnection connection) {
try {
connection.removeContainer(container.id());
RunConsole rc = RunConsole.findConsole(container.id());
if (rc != null)
RunConsole.removeConsole(rc);
} catch (DockerException | InterruptedException e) {
final String errorMessage = DVMessages.getFormattedString(CONTAINER_REMOVE_ERROR_MSG, container.name());
openError(errorMessage, e);
} finally {
// always get images as we sometimes get errors on intermediate
// images
// being removed but we will remove some top ones successfully
connection.getContainers(true);
}
}
use of org.eclipse.linuxtools.internal.docker.ui.consoles.RunConsole 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();
}
Aggregations