use of org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings in project linuxtools by eclipse.
the class EditDockerConnectionSWTBotTest method shouldSaveConnectionWhenNameAndTCPConnectionSettingsChanged.
@SuppressWarnings("unchecked")
@Test
public void shouldSaveConnectionWhenNameAndTCPConnectionSettingsChanged() {
// given
final IDockerConnection connection = configureTCPConnection("Test");
final IDockerConnectionStorageManager connectionStorageManager = MockDockerConnectionStorageManagerFactory.providing(connection);
DockerConnectionManagerUtils.configureConnectionManager(connectionStorageManager);
final SWTBotTreeItem connectionTreeItem = SWTUtils.getTreeItem(dockerExplorer.bot(), "Test");
assertThat(connectionTreeItem).isNotNull();
// let's ignore the connection savings that may have occurred when
// adding elements from the extension points
Mockito.reset(connectionStorageManager);
// when
openConnectionEditionWizard("Test");
bot.text(0).setText("foo");
bot.text(2).setText("https://foo.bar:1234");
getFinishButton().click();
// then
final IDockerConnection foundConnection = DockerConnectionManager.getInstance().findConnection("foo");
assertThat(foundConnection).isNotNull();
assertThat(foundConnection.getSettings()).isNotNull().isEqualTo(new TCPConnectionSettings("https://foo.bar:1234", PATH_TO_CERTS));
Mockito.verify(connectionStorageManager).saveConnections(Matchers.anyList());
}
use of org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings in project linuxtools by eclipse.
the class DockerConnectionManager method reloadConnections.
public void reloadConnections() {
this.connections = connectionStorageManager.loadConnections();
for (IDockerConnection connection : connections) {
notifyListeners(connection, IDockerConnectionManagerListener.ADD_EVENT);
}
List<IDockerConnectionSettings> settings = connectionSettingsFinder.getKnownConnectionSettings();
for (IDockerConnectionSettings setting : settings) {
if (setting != null) {
IDockerConnection conn;
if (setting.getType().equals(BindingType.UNIX_SOCKET_CONNECTION)) {
UnixSocketConnectionSettings usetting = (UnixSocketConnectionSettings) setting;
conn = new DockerConnection.Builder().name(usetting.getName()).unixSocketConnection(usetting);
} else {
TCPConnectionSettings tsetting = (TCPConnectionSettings) setting;
conn = new DockerConnection.Builder().name(tsetting.getName()).tcpConnection(tsetting);
}
// add the connection but do not notify the listeners to avoid
// flickering on the Docker Explorer view for each entry
addConnection(conn, false);
}
}
}
use of org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings in project linuxtools by eclipse.
the class NewDockerConnectionPage method retrieveDefaultConnectionSettings.
/**
* Sets the default settings by looking for the:
* <ul>
* <li>a Unix socket at /var/run/docker.sock</li>
* <li>the following environment variables:
* <ul>
* <li>DOCKER_HOST</li>
* <li>DOCKER_CERT_PATH</li>
* <li>DOCKER_TLS_VERIFY</li>
* </ul>
* </li>
* </ul>
* and sets the default connection settings accordingly.
*/
private void retrieveDefaultConnectionSettings() {
// progressbar
try {
getWizard().getContainer().run(true, true, monitor -> {
monitor.beginTask(WizardMessages.getString(// $NON-NLS-1$
"DockerConnectionPage.retrieveTask"), 1);
final DockerConnectionManager dockerConnectionManager = DockerConnectionManager.getInstance();
final IDockerConnectionSettings defaultConnectionSettings = dockerConnectionManager.findDefaultConnectionSettings();
if (defaultConnectionSettings != null) {
model.setCustomSettings(!defaultConnectionSettings.isSettingsResolved());
model.setConnectionName(dockerConnectionManager.resolveConnectionName(defaultConnectionSettings));
switch(defaultConnectionSettings.getType()) {
case TCP_CONNECTION:
final TCPConnectionSettings tcpConnectionSettings = (TCPConnectionSettings) defaultConnectionSettings;
model.setTcpConnectionBindingMode(true);
model.setTcpCertPath(tcpConnectionSettings.getPathToCertificates());
model.setTcpTLSVerify(tcpConnectionSettings.isTlsVerify());
model.setTcpHost(tcpConnectionSettings.getHost());
break;
case UNIX_SOCKET_CONNECTION:
model.setUnixSocketBindingMode(true);
final UnixSocketConnectionSettings unixSocketConnectionSettings = (UnixSocketConnectionSettings) defaultConnectionSettings;
model.setUnixSocketPath(unixSocketConnectionSettings.getPath());
break;
}
} else {
// fall-back to custom settings, suggesting a
// Unix Socket connection to the user.
model.setCustomSettings(true);
model.setUnixSocketBindingMode(true);
}
monitor.done();
});
} catch (InvocationTargetException | InterruptedException e) {
Activator.log(e);
}
}
use of org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings 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();
}
}
Aggregations