use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.
the class DockerExplorerContentProvider method open.
/**
* Call the {@link IDockerConnection#getContainers(boolean)} in a background
* job to avoid blocking the UI.
*
* @param connection
* the connection to ping
*/
private void open(final IDockerConnection connection) {
final Job pingJob = new // $NON-NLS-1$
LoadingJob(// $NON-NLS-1$
DVMessages.getString("PingJob.msg"), connection) {
@Override
protected IStatus run(final IProgressMonitor monitor) {
try {
connection.open(true);
connection.ping();
return Status.OK_STATUS;
} catch (DockerException e) {
Activator.logWarningMessage(DVMessages.getFormattedString(// $NON-NLS-1$
"PingJobError.msg.withExplanation", connection.getName(), e.getMessage()));
return Status.CANCEL_STATUS;
}
}
};
pingJob.schedule();
}
use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.
the class NewDockerConnectionPage method onTestConnectionButtonSelection.
/**
* Verifies that the given connection settings work by trying to connect to
* the target Docker daemon
*
* @return
*/
private SelectionListener onTestConnectionButtonSelection() {
return new SelectionAdapter() {
@Override
public void widgetSelected(final SelectionEvent event) {
try {
getWizard().getContainer().run(true, false, monitor -> {
monitor.beginTask(WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.pingTask"), IProgressMonitor.UNKNOWN);
try {
final DockerConnection dockerConnection = getDockerConnection();
dockerConnection.open(false);
dockerConnection.ping();
dockerConnection.close();
// ping succeeded
displaySuccessDialog();
} catch (DockerException e) {
// in Eclipse.org AERI
if (e.getCause() != null) {
displayErrorDialog(e.getCause());
} else {
displayErrorDialog(e);
}
}
});
} catch (InvocationTargetException | InterruptedException o_O) {
Activator.log(o_O);
}
}
private void displaySuccessDialog() {
displayDialog(WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.success"), WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.pingSuccess"), SWT.ICON_INFORMATION, new String[] { WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.ok") });
}
private void displayErrorDialog(final Throwable cause) {
displayDialog(WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.failure"), WizardMessages.getFormattedString(// $NON-NLS-1$
"DockerConnectionPage.pingFailure", cause.getMessage()), SWT.ICON_ERROR, new String[] { WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.ok") });
}
private void displayDialog(final String dialogTitle, final String dialogMessage, final int icon, final String[] buttonLabels) {
Display.getDefault().syncExec(() -> new MessageDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), dialogTitle, null, dialogMessage, icon, buttonLabels, 0).open());
}
};
}
use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.
the class JavaAppInContainerLaunchDelegate method launch.
/* (non-Javadoc)
* @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
*/
@Override
public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
// $NON-NLS-1$
monitor.beginTask(NLS.bind("{0}...", new String[] { configuration.getName() }), 3);
// check for cancellation
if (monitor.isCanceled()) {
return;
}
String connectionURI = configuration.getAttribute(JavaLaunchConfigurationConstants.CONNECTION_URI, (String) null);
String imageID = configuration.getAttribute(JavaLaunchConfigurationConstants.IMAGE_ID, (String) null);
List<String> extraDirs = configuration.getAttribute(JavaLaunchConfigurationConstants.DIRS, Arrays.asList(new String[0]));
try {
DockerConnection conn = (DockerConnection) DockerConnectionManager.getInstance().getConnectionByUri(connectionURI);
if (conn == null) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_connection_not_found_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_connection_not_found_text, connectionURI));
}
});
return;
} else if (!conn.isOpen()) {
try {
conn.open(false);
} catch (DockerException e) {
}
if (!conn.isOpen()) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_connection_not_active_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_connection_not_active_text, connectionURI));
}
});
return;
}
}
IDockerImage img = conn.getImage(imageID);
if (img == null) {
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_image_not_found_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_image_not_found_text, imageID));
}
});
return;
}
// randomized port between 1025 and 65535
int port = ILaunchManager.DEBUG_MODE.equals(mode) ? (int) ((65535 - 1025) * Math.random()) + 1025 : -1;
monitor.subTask(Messages.JavaAppInContainerLaunchDelegate_Verifying_launch_attributes____1);
String mainTypeName = verifyMainTypeName(configuration);
IVMInstall vm = new ContainerVMInstall(configuration, img, port);
ContainerVMRunner runner = new ContainerVMRunner(vm);
File workingDir = verifyWorkingDirectory(configuration);
String workingDirName = null;
if (workingDir != null) {
workingDirName = workingDir.getAbsolutePath();
}
runner.setAdditionalDirectories(extraDirs);
// Environment variables
String[] envp = getEnvironment(configuration);
// Program & VM arguments
String pgmArgs = getProgramArguments(configuration);
String vmArgs = getVMArguments(configuration);
ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs);
// VM-specific attributes
Map<String, Object> vmAttributesMap = getVMSpecificAttributesMap(configuration);
// Classpath
String[] classpath = getClasspath(configuration);
if (Platform.OS_WIN32.equals(Platform.getOS())) {
for (int i = 0; i < classpath.length; i++) {
classpath[i] = UnixFile.convertDOSPathToUnixPath(classpath[i]);
}
}
// Create VM config
VMRunnerConfiguration runConfig = new VMRunnerConfiguration(mainTypeName, classpath);
runConfig.setProgramArguments(execArgs.getProgramArgumentsArray());
runConfig.setEnvironment(envp);
List<String> finalVMArgs = new ArrayList<>(Arrays.asList(execArgs.getVMArgumentsArray()));
if (ILaunchManager.DEBUG_MODE.equals(mode)) {
double version = getJavaVersion(conn, img);
if (version < 1.5) {
// $NON-NLS-1$
finalVMArgs.add("-Xdebug");
// $NON-NLS-1$
finalVMArgs.add("-Xnoagent");
}
// check if java 1.4 or greater
if (version < 1.4) {
// $NON-NLS-1$
finalVMArgs.add("-Djava.compiler=NONE");
}
if (version < 1.5) {
// $NON-NLS-1$
finalVMArgs.add("-Xrunjdwp:transport=dt_socket,server=y,address=" + port);
} else {
// $NON-NLS-1$
finalVMArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,address=" + port);
}
}
runConfig.setVMArguments(finalVMArgs.toArray(new String[0]));
runConfig.setWorkingDirectory(workingDirName);
runConfig.setVMSpecificAttributesMap(vmAttributesMap);
// Bootpath
runConfig.setBootClassPath(getBootpath(configuration));
// check for cancellation
if (monitor.isCanceled()) {
return;
}
// stop in main
prepareStopInMain(configuration);
// done the verification phase
monitor.worked(1);
monitor.subTask(Messages.JavaAppInContainerLaunchDelegate_Creating_source_locator____2);
// set the default source locator if required
setDefaultSourceLocator(launch, configuration);
monitor.worked(1);
// Launch the configuration - 1 unit of work
runner.run(runConfig, launch, monitor);
// check for cancellation
if (monitor.isCanceled()) {
return;
}
if (ILaunchManager.DEBUG_MODE.equals(mode)) {
while (runner.getIPAddress() == null || !runner.isListening()) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
}
IDockerContainerInfo info = runner.getContainerInfo();
// $NON-NLS-1$
String configName = info.name().startsWith("/") ? info.name().substring(1) : info.name();
ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
ILaunchConfigurationType type = manager.getLaunchConfigurationType(IJavaLaunchConfigurationConstants.ID_REMOTE_JAVA_APPLICATION);
ILaunchConfiguration cfgForAttach = type.newInstance(null, configName);
ILaunchConfigurationWorkingCopy wc = cfgForAttach.getWorkingCopy();
String ip = runner.getIPAddress();
// Can we reach it ? Or is it on a different network.
if (!isListening(ip, port)) {
// If the daemon is reachable via TCP it should forward traffic.
if (conn.getSettings() instanceof TCPConnectionSettings) {
ip = ((TCPConnectionSettings) conn.getSettings()).getAddr();
if (!isListening(ip, port)) {
// Try to find some network interface that's listening
ip = getIPAddressListening(port);
if (!isListening(ip, port)) {
ip = null;
}
}
} else {
ip = null;
}
}
if (ip == null) {
String imageName = conn.getImage(imageID).repoTags().get(0);
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run() {
MessageDialog.openError(Display.getDefault().getActiveShell(), Messages.JavaAppInContainerLaunchDelegate_session_unreachable_title, Messages.bind(Messages.JavaAppInContainerLaunchDelegate_session_unreachable_text, new Object[] { imageName, imageID, runner.getIPAddress() }));
}
});
return;
}
Map<String, String> map = new HashMap<>();
// $NON-NLS-1$
map.put("hostname", ip);
// $NON-NLS-1$
map.put("port", String.valueOf(port));
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_CONNECT_MAP, map);
String projectName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String) null);
wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, projectName);
wc.doSave();
DebugUITools.launch(cfgForAttach, ILaunchManager.DEBUG_MODE);
}
} finally {
monitor.done();
}
}
use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.
the class JavaImageTab method createControl.
@Override
public void createControl(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(2, false);
composite.setLayout(layout);
composite.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
Label connLbl = new Label(composite, SWT.NONE);
connLbl.setText(Messages.ImageSelectionDialog_connection_label);
connCmb = new ComboViewer(composite, SWT.READ_ONLY);
connCmb.getCombo().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
connCmb.setContentProvider(new IStructuredContentProvider() {
@Override
public Object[] getElements(Object inputElement) {
for (IDockerConnection conn : DockerConnectionManager.getInstance().getAllConnections()) {
try {
((DockerConnection) conn).open(false);
} catch (DockerException e) {
}
}
return DockerConnectionManager.getInstance().getAllConnections().stream().filter(c -> c.isOpen()).toArray(size -> new IDockerConnection[size]);
}
});
// $NON-NLS-1$
connCmb.setInput("place_holder");
Label imageLbl = new Label(composite, SWT.NONE);
imageLbl.setText(Messages.ImageSelectionDialog_image_label);
imageCmb = new ComboViewer(composite, SWT.READ_ONLY);
imageCmb.getCombo().setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, false));
imageCmb.setContentProvider(new IStructuredContentProvider() {
@Override
public Object[] getElements(Object inputElement) {
IDockerConnection conn = (IDockerConnection) inputElement;
if (conn == null || conn.getImages() == null) {
return new Object[0];
} else {
return conn.getImages().stream().filter(// $NON-NLS-1$
i -> !i.repoTags().get(0).equals("<none>:<none>")).toArray(size -> new IDockerImage[size]);
}
}
});
imageCmb.setLabelProvider(new ColumnLabelProvider() {
@Override
public String getText(Object element) {
IDockerImage img = (IDockerImage) element;
return img.repoTags().get(0);
}
});
imageCmb.setInput(null);
connCmb.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection sel = event.getStructuredSelection();
IDockerConnection conn = (IDockerConnection) sel.getFirstElement();
selectedConnection = conn;
imageCmb.setInput(conn);
updateLaunchConfigurationDialog();
}
});
imageCmb.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection sel = event.getStructuredSelection();
IDockerImage img = (IDockerImage) sel.getFirstElement();
selectedImage = img;
updateLaunchConfigurationDialog();
}
});
Group dirGroup = new Group(composite, SWT.NONE);
dirGroup.setText(Messages.JavaImageTab_additional_dirs);
dirGroup.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
dirGroup.setLayout(new GridLayout(2, false));
directoryList = new List(dirGroup, SWT.SINGLE | SWT.V_SCROLL);
directoryList.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 2));
directoryList.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
removeButton.setEnabled(true);
}
});
addButton = createPushButton(dirGroup, Messages.JavaImageTab_button_add, null);
addButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
addButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
DirectoryDialog dialog = new DirectoryDialog(getShell());
String directory = dialog.open();
if (directory != null && !listContains(directoryList, directory)) {
directoryList.add(directory);
updateLaunchConfigurationDialog();
}
}
});
removeButton = createPushButton(dirGroup, Messages.JavaImageTab_button_remove, null);
removeButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true));
removeButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
int i = directoryList.getSelectionIndex();
if (i >= 0) {
directoryList.remove(i);
updateLaunchConfigurationDialog();
}
if (directoryList.getItemCount() == 0) {
removeButton.setEnabled(false);
}
}
});
removeButton.setEnabled(false);
setControl(composite);
}
use of org.eclipse.linuxtools.docker.core.DockerException in project linuxtools by eclipse.
the class ImageRunSelectionPage method pullSelectedImage.
private void pullSelectedImage() {
try {
getContainer().run(true, true, monitor -> {
final IDockerConnection connection = model.getSelectedConnection();
final String imageName = model.getSelectedImageName();
monitor.beginTask(WizardMessages.getFormattedString("ImageRunSelectionPage.pullingTask", // $NON-NLS-1$
imageName), 1);
try {
connection.pullImage(imageName, new DefaultImagePullProgressHandler(connection, imageName));
} catch (final DockerException e) {
Display.getDefault().syncExec(() -> MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), DVMessages.getFormattedString(ERROR_PULLING_IMAGE, imageName), e.getMessage()));
} finally {
monitor.done();
// refresh the widgets
model.refreshImageNames();
if (model.getImageNames().contains(imageName)) {
model.setSelectedImageName(imageName);
model.setSelectedImageNeedsPulling(false);
// Force revalidation by changing writeValue which
// is made to be a dependency of the ImageCombo
// MultiValidator.
Display.getDefault().syncExec(() -> {
writeValue.setValue(Long.toString(System.currentTimeMillis()));
});
}
}
});
} catch (InvocationTargetException | InterruptedException e) {
Activator.log(e);
}
}
Aggregations