diff options
author | Xavier Coulon | 2015-11-17 07:58:26 +0000 |
---|---|---|
committer | Xavier Coulon | 2015-11-27 14:43:15 +0000 |
commit | fd21fefe291b356704127a81ba5df73e6fcd600f (patch) | |
tree | 2e1b9a702e83143805b6254caa1702d6baa96279 | |
parent | 3620577e776af367e761c1e8aea946aafb589517 (diff) | |
download | org.eclipse.linuxtools-fd21fefe291b356704127a81ba5df73e6fcd600f.tar.gz org.eclipse.linuxtools-fd21fefe291b356704127a81ba5df73e6fcd600f.tar.xz org.eclipse.linuxtools-fd21fefe291b356704127a81ba5df73e6fcd600f.zip |
Bug 480885 - Docker-machine detection does not work on Mac OSX
Added a preference page for Docker Machine with validation
Default values are set based on the OS name
Fixed missing validation support in main Docker page for
refresh rate
Refactored preference page ids in plugin.xml for consistency
with class names
Moved "Search" button on NewConnectionPage to top right, after
the connection name field
Change-Id: I7ac2768676dfdcbab8ad864b8f74c2c7fe4f4020
Signed-off-by: Xavier Coulon <xcoulon@redhat.com>
Reviewed-on: https://git.eclipse.org/r/59283
Tested-by: Hudson CI
11 files changed, 394 insertions, 67 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java index 3da6905613..ece5f92791 100644 --- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java +++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java @@ -11,63 +11,129 @@ package org.eclipse.linuxtools.internal.docker.core; import java.io.BufferedReader; +import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; +import java.util.Map; -public class DockerMachine { +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.linuxtools.docker.core.Activator; - private static final String DM = "docker-machine"; //$NON-NLS-1$ - private static final String LS = "ls"; //$NON-NLS-1$ - private static final String URL = "url"; //$NON-NLS-1$ - private static final String ENV = "env"; //$NON-NLS-1$ +/** + * Utility class to discover Docker machines using the 'docker-machine' command + * line in a process. + */ +public class DockerMachine { - public static String[] getNames() { - return call(new String[] { LS, "-q" }); //$NON-NLS-1$ + /** + * @param pathToDockerMachine + * the path to 'docker-machine' stored in the preferences + * @return the names of the existing Docker Machines + */ + public static String[] getNames(final String pathToDockerMachine) { + return execute(pathToDockerMachine, new String[] { "ls", "-q" }); //$NON-NLS-1$ //$NON-NLS-2$ } - public static String getHost(String name) { - String[] res = call(new String[] { URL, name }); + /** + * @param name + * the name of the Docker Machine to inspect + * @param dockerMachineInstallDir + * the installation directory for Docker Machine + * @param vmDriverInstallDir + * the installation directory for the underlying VM driver used + * by Docker Machine + * @return the host URI to use to connect to it + */ + public static String getHost(final String name, + final String dockerMachineInstallDir, + final String vmDriverInstallDir) { + final String[] res = execute(dockerMachineInstallDir, + new String[] { "url", name }, //$NON-NLS-1$ + vmDriverInstallDir); return res.length == 1 ? res[0] : null; } - public static String getCertPath(String name) { - String[] res = getEnv(name); - for (String l : res) { - if (l.contains("DOCKER_CERT_PATH")) { //$NON-NLS-1$ + /** + * @param name + * the name of the Docker Machine to inspect + * @param pathToDockerMachine + * the path to 'docker-machine' stored in the preferences + * @param vmDriverInstallDir + * the installation directory for the underlying VM driver used + * by Docker Machine + * @return the path to the directory containing the certificates + */ + public static String getCertPath(final String name, + final String pathToDockerMachine, final String vmDriverInstallDir) { + final String[] envVariables = execute(pathToDockerMachine, + new String[] { "env", name }, //$NON-NLS-1$ + vmDriverInstallDir); + for (String envVariable : envVariables) { + if (envVariable.contains("DOCKER_CERT_PATH")) { //$NON-NLS-1$ // DOCKER_CERT_PATH="/path/to/cert-folder" - return l.split("=")[1].replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + return envVariable.split("=")[1].replace("\"", ""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } } return null; } - private static String[] getEnv(String name) { - return call(new String[] { ENV, name }); - } - - private static String[] call(String[] args) { - List<String> result = new ArrayList<>(); + /** + * Executes the command given in parameter + * + * @param args + * command arguments + * @return the lines read in the {@link Process}' {@link InputStream} + */ + private static String[] execute(final String dockerMachineInstallDir, + final String[] args, final String... extraPaths) { try { - List<String> cmd = new ArrayList<>(); - cmd.add(DM); - cmd.addAll(Arrays.asList(args)); - Process p = Runtime.getRuntime().exec(cmd.toArray(new String[0])); - BufferedReader buff = new BufferedReader(new InputStreamReader(p.getInputStream())); - if (p.waitFor() == 0) { - String line; - while ((line = buff.readLine()) != null) { - result.add(line); + final String[] command = new String[args.length + 1]; + command[0] = dockerMachineInstallDir + + (dockerMachineInstallDir.endsWith(File.separator) ? "" //$NON-NLS-1$ + : File.separator) + + "docker-machine"; + System.arraycopy(args, 0, command, 1, args.length); + final ProcessBuilder processBuilder = new ProcessBuilder(command); + final Map<String, String> environment = processBuilder + .environment(); + final StringBuilder path = new StringBuilder( + dockerMachineInstallDir); + for (String extraPath : extraPaths) { + path.append(File.pathSeparator).append(extraPath); + } + environment.put("PATH", path.toString()); + final Process p = processBuilder.start(); + p.waitFor(); + if (p.exitValue() == 0) { + final List<String> result = new ArrayList<>(); + try (BufferedReader buff = new BufferedReader( + new InputStreamReader(p.getInputStream()))) { + String line; + while ((line = buff.readLine()) != null) { + result.add(line); + } } + return result.toArray(new String[0]); } else { - return new String[0]; + final StringBuffer errorMessage = new StringBuffer(); + try (BufferedReader buff = new BufferedReader( + new InputStreamReader(p.getErrorStream()))) { + String line; + while ((line = buff.readLine()) != null) { + errorMessage.append(line).append('\n'); + } + } + Activator.log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, + errorMessage.toString())); } - } catch (IOException e) { - } catch (InterruptedException e) { + } catch (IOException | InterruptedException e) { + Activator.log(e); } - return result.toArray(new String[0]); + return new String[0]; } } diff --git a/containers/org.eclipse.linuxtools.docker.ui/plugin.properties b/containers/org.eclipse.linuxtools.docker.ui/plugin.properties index e900cb56e2..2bc3b5af16 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/plugin.properties +++ b/containers/org.eclipse.linuxtools.docker.ui/plugin.properties @@ -15,8 +15,9 @@ IntermediateImagesViewFilter.name=Intermediate images DanglingImagesViewFilter.name=Dangling (untagged) images StoppedContainerViewFilter.name=Stopped containers -PreferencesPage.name=Docker -LoggingPreferencesPage.name=Logging +DockerPreferencePage.name=Docker +LoggingPreferencePage.name=Logging +DockerMachinePreferencePage.name=Docker Machine Info.label=Info Inspect.label=Inspect diff --git a/containers/org.eclipse.linuxtools.docker.ui/plugin.xml b/containers/org.eclipse.linuxtools.docker.ui/plugin.xml index 3fa347c888..49e7208b77 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/plugin.xml +++ b/containers/org.eclipse.linuxtools.docker.ui/plugin.xml @@ -5,15 +5,21 @@ <extension point="org.eclipse.ui.preferencePages"> <page - name="%PreferencesPage.name" + name="%DockerPreferencePage.name" class="org.eclipse.linuxtools.internal.docker.ui.preferences.DockerPreferencePage" id="org.eclipse.linuxtools.docker.ui.preferences.DockerPreferencePage"> </page> <page category="org.eclipse.linuxtools.docker.ui.preferences.DockerPreferencePage" class="org.eclipse.linuxtools.internal.docker.ui.preferences.LoggingPreferencePage" - id="org.eclipse.linuxtools.docker.ui.preferences.LoggingPage" - name="%LoggingPreferencesPage.name"> + id="org.eclipse.linuxtools.docker.ui.preferences.LoggingPreferencePage" + name="%LoggingPreferencePage.name"> + </page> + <page + category="org.eclipse.linuxtools.docker.ui.preferences.DockerPreferencePage" + class="org.eclipse.linuxtools.internal.docker.ui.preferences.DockerMachinePreferencePage" + id="org.eclipse.linuxtools.docker.ui.preferences.DockerMachinePreferencePage" + name="%DockerMachinePreferencePage.name"> </page> </extension> <extension diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java new file mode 100644 index 0000000000..3bc9d4f16a --- /dev/null +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerMachinePreferencePage.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright (c) 2014 Red Hat. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - Initial Contribution + *******************************************************************************/ +package org.eclipse.linuxtools.internal.docker.ui.preferences; + +import org.eclipse.jface.preference.DirectoryFieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.StringFieldEditor; +import org.eclipse.jface.resource.JFaceResources; +import org.eclipse.linuxtools.docker.ui.Activator; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; + +public class DockerMachinePreferencePage extends FieldEditorPreferencePage + implements IWorkbenchPreferencePage { + + /** + * The {@link DirectoryFieldEditor} to select the installation directory for + * the Docker-Machine command. + */ + private DirectoryFieldEditor dockerMachineInstallDir; + + /** + * The {@link DirectoryFieldEditor} to select the installation directory for + * the underlying VM driver commands used by 'docker-machine'. + */ + private DirectoryFieldEditor vmDriverInstallDir; + + public DockerMachinePreferencePage() { + super(GRID); + setPreferenceStore(Activator.getDefault().getPreferenceStore()); + setDescription(Messages.getString("DockerMachine.message")); //$NON-NLS-1$ + } + + @Override + public void init(final IWorkbench workbench) { + } + + @Override + protected void createFieldEditors() { + // installation directory for docker-machine + this.dockerMachineInstallDir = new CustomDirectoryFieldEditor( + PreferenceConstants.DOCKER_MACHINE_INSTALLATION_DIRECTORY, + Messages.getString("DockerMachinePath.label"), //$NON-NLS-1$ + getFieldEditorParent()); + addField(this.dockerMachineInstallDir); + this.dockerMachineInstallDir.setPreferenceStore(getPreferenceStore()); + // allow empty value if docker-machine is not installed + this.dockerMachineInstallDir.setEmptyStringAllowed(true); + this.dockerMachineInstallDir + .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE); + this.dockerMachineInstallDir.setPage(this); + this.dockerMachineInstallDir.setErrorMessage( + Messages.getString("DockerMachinePath.invalid.label")); //$NON-NLS-1$ + this.dockerMachineInstallDir.showErrorMessage(); + this.dockerMachineInstallDir.load(); + // installation directory for underlying VM driver + this.vmDriverInstallDir = new CustomDirectoryFieldEditor( + PreferenceConstants.VM_DRIVER_INSTALLATION_DIRECTORY, + Messages.getString("VMDriverPath.label"), //$NON-NLS-1$ + getFieldEditorParent()); + addField(this.vmDriverInstallDir); + this.vmDriverInstallDir.setPreferenceStore(getPreferenceStore()); + // allow empty value if docker-machine is not installed + this.vmDriverInstallDir.setPage(this); + this.vmDriverInstallDir.setEmptyStringAllowed(true); + this.vmDriverInstallDir.setErrorMessage( + Messages.getString("VMDriverPath.invalid.label")); //$NON-NLS-1$ + this.vmDriverInstallDir.showErrorMessage(); + this.vmDriverInstallDir + .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE); + this.vmDriverInstallDir.load(); + } + + /** + * Subclass of the {@link DirectoryFieldEditor} but with the + * {@link StringFieldEditor#VALIDATE_ON_KEY_STROKE} validation strategy. + */ + private static class CustomDirectoryFieldEditor + extends DirectoryFieldEditor { + + public CustomDirectoryFieldEditor(String name, String labelText, + Composite parent) { + init(name, labelText); + setErrorMessage(JFaceResources + .getString("DirectoryFieldEditor.errorMessage"));//$NON-NLS-1$ + setChangeButtonText(JFaceResources.getString("openBrowse"));//$NON-NLS-1$ + setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE); + createControl(parent); + } + + } +}
\ No newline at end of file diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerPreferencePage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerPreferencePage.java index d4a3ce6889..1155801784 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerPreferencePage.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/DockerPreferencePage.java @@ -30,6 +30,8 @@ public class DockerPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { private static final String REFRESH_TIME_MSG = "RefreshTime.label"; //$NON-NLS-1$ + + private static final String INVALID_REFRESH_TIME_MSG = "RefreshTime.invalid.label"; //$NON-NLS-1$ private IntegerFieldEditor refreshTimeField; @@ -93,6 +95,10 @@ public class DockerPreferencePage extends PreferencePage implements refreshTimeField .setValidateStrategy(StringFieldEditor.VALIDATE_ON_KEY_STROKE); refreshTimeField.setValidRange(5, 200); + refreshTimeField.setPage(this); + refreshTimeField + .setErrorMessage(Messages.getString(INVALID_REFRESH_TIME_MSG)); + refreshTimeField.showErrorMessage(); refreshTimeField.load(); // If the preference changes, alert the Refresh Manager refreshTimeField diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/Messages.properties b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/Messages.properties index a22499eaab..cff6d392a0 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/Messages.properties +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/Messages.properties @@ -9,7 +9,14 @@ # Red Hat - Initial Contribution ############################################################################### RefreshTime.label=Container Refresh Rate (seconds) +RefreshTime.invalid.label=Container refresh rate is invalid DefaultHost.label=Default Docker Host AutoLogOnStart.label=Automatically log when Container starts LogTimeStamp.label=Request time stamp for logs -PreferencesDesc.txt=Preferences for Docker Manager Plug-ins
\ No newline at end of file +PreferencesDesc.txt=Preferences for Docker Manager Plug-ins +DockerMachine.label=Docker Machine +DockerMachine.message=Specify the installation directories for Docker Machine and for the underlying VM Driver it uses. +DockerMachinePath.label=Docker Machine +DockerMachinePath.invalid.label=Path to 'docker-machine' is invalid +VMDriverPath.label=VM Driver +VMDriverPath.invalid.label=Path to VM driver installation directory is invalid diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceConstants.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceConstants.java index 9e93035760..ed0bc39ba8 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceConstants.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceConstants.java @@ -21,5 +21,12 @@ public class PreferenceConstants { public static final String AUTOLOG_ON_START = "autoLogOnStart"; //$NON-NLS-1$ public static final String LOG_TIMESTAMP = "logTimestamp"; //$NON-NLS-1$ + + /** the installation directory for Docker Machine. */ + public static final String DOCKER_MACHINE_INSTALLATION_DIRECTORY = "dockerMachineInstallDir"; //$NON-NLS-1$ + /** + * the installation directory for the VM driver (eg, VirtualBox). + */ + public static final String VM_DRIVER_INSTALLATION_DIRECTORY = "vmDriverInstallDir"; //$NON-NLS-1$ } diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceInitializer.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceInitializer.java index efb3701af2..edef18e292 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceInitializer.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/preferences/PreferenceInitializer.java @@ -11,24 +11,45 @@ package org.eclipse.linuxtools.internal.docker.ui.preferences; import static org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants.AUTOLOG_ON_START; +import static org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants.DOCKER_MACHINE_INSTALLATION_DIRECTORY; import static org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants.LOG_TIMESTAMP; import static org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants.REFRESH_TIME; +import static org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants.VM_DRIVER_INSTALLATION_DIRECTORY; import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.linuxtools.docker.ui.Activator; +import org.eclipse.linuxtools.internal.docker.ui.utils.SystemUtils; /** * Class used to initialize default preference values. */ public class PreferenceInitializer extends AbstractPreferenceInitializer { + @Override public void initializeDefaultPreferences() { final IPreferenceStore store = Activator.getDefault().getPreferenceStore(); store.setDefault(REFRESH_TIME, 15); store.setDefault(AUTOLOG_ON_START, true); store.setDefault(LOG_TIMESTAMP, true); + // set docker-machine preferences based on the user's platform + if (SystemUtils.isWindows()) { + store.setDefault(DOCKER_MACHINE_INSTALLATION_DIRECTORY, + "C:\\Program Files\\Docker Toolbox"); //$NON-NLS-1$ + store.setDefault(VM_DRIVER_INSTALLATION_DIRECTORY, + "C:\\Program Files\\Oracle\\VirtualBox"); //$NON-NLS-1$ + } else if (SystemUtils.isMac()) { + store.setDefault(DOCKER_MACHINE_INSTALLATION_DIRECTORY, + "/usr/local/bin"); //$NON-NLS-1$ + store.setDefault(VM_DRIVER_INSTALLATION_DIRECTORY, + "/usr/local/bin"); //$NON-NLS-1$ + } else if (SystemUtils.isLinux()) { + store.setDefault(DOCKER_MACHINE_INSTALLATION_DIRECTORY, + "/usr/local/bin"); //$NON-NLS-1$ + store.setDefault(VM_DRIVER_INSTALLATION_DIRECTORY, + "/usr/local/bin"); //$NON-NLS-1$ + } } } diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/utils/SystemUtils.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/utils/SystemUtils.java new file mode 100644 index 0000000000..b0524e1d52 --- /dev/null +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/utils/SystemUtils.java @@ -0,0 +1,45 @@ +/******************************************************************************* + * Copyright (c) 2015 Red Hat. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Red Hat - Initial Contribution + *******************************************************************************/ + +package org.eclipse.linuxtools.internal.docker.ui.utils; + +import org.eclipse.core.runtime.Platform; + +/** + * Utility class for System/OS info + */ +public class SystemUtils { + + /** + * @return <code>true</code> if if the current OS is Windows, + * <code>false</code> otherwise. + */ + public static boolean isWindows() { + return Platform.getOS().equals(Platform.OS_WIN32); + } + + /** + * @return <code>true</code> if if the current OS is Mac, <code>false</code> + * otherwise. + */ + public static boolean isMac() { + return Platform.getOS().equals(Platform.OS_MACOSX); + } + + /** + * @return <code>true</code> if if the current OS is Linux, + * <code>false</code> otherwise. + */ + public static boolean isLinux() { + return Platform.getOS().equals(Platform.OS_LINUX); + } + +} diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java index 70799f7e02..0a8c2ff4ef 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/NewDockerConnectionPage.java @@ -18,6 +18,8 @@ import java.lang.reflect.InvocationTargetException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; import java.util.List; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.TimeUnit; @@ -57,6 +59,7 @@ import org.eclipse.linuxtools.internal.docker.core.DockerMachine; import org.eclipse.linuxtools.internal.docker.core.TCPConnectionSettings; import org.eclipse.linuxtools.internal.docker.core.UnixSocketConnectionSettings; import org.eclipse.linuxtools.internal.docker.ui.SWTImagesFactory; +import org.eclipse.linuxtools.internal.docker.ui.preferences.PreferenceConstants; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; @@ -72,6 +75,7 @@ import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Text; import org.eclipse.ui.dialogs.ListDialog; +import org.eclipse.ui.dialogs.PreferencesUtil; /** * {@link WizardPage} to input the settings to connect to a Docker @@ -80,7 +84,10 @@ import org.eclipse.ui.dialogs.ListDialog; */ public class NewDockerConnectionPage extends WizardPage { + private static final String DOCKER_MACHINE_PREFERENCE_PAGE_ID = "org.eclipse.linuxtools.docker.ui.preferences.DockerMachinePreferencePage"; //$NON-NLS-1$ + private final DataBindingContext dbc; + private final NewDockerConnectionPageModel model; public NewDockerConnectionPage() { @@ -140,7 +147,17 @@ public class NewDockerConnectionPage extends WizardPage { connectionNameText.setToolTipText(WizardMessages .getString("NewDockerConnectionPage.nameTooltip")); //$NON-NLS-1$ GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER) - .grab(true, false).span(2, 1).applyTo(connectionNameText); + .grab(true, false).span(1, 1).applyTo(connectionNameText); + + // the 'Search' button + final Button searchButton = new Button(container, SWT.NONE); + searchButton.setText(WizardMessages + .getString("NewDockerConnectionPage.searchButton")); //$NON-NLS-1$ + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).span(1, 1) + .align(SWT.BEGINNING, SWT.CENTER).grab(false, false) + .applyTo(searchButton); + searchButton.addSelectionListener(onSearchButtonSelection()); + // custom settings checkbox final Button customConnectionSettingsButton = new Button(container, @@ -229,22 +246,12 @@ public class NewDockerConnectionPage extends WizardPage { GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER) .applyTo(tcpCertPathBrowseButton); - // the 'Search' button - final Button searchButton = new Button(container, SWT.NONE); - searchButton.setText(WizardMessages - .getString("NewDockerConnectionPage.searchButton")); //$NON-NLS-1$ - GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER) - .span(COLUMNS - 1, 1).align(SWT.BEGINNING, SWT.CENTER) - .applyTo(searchButton); - searchButton.addSelectionListener(onSearchButtonSelection()); - // the 'test connection' button final Button testConnectionButton = new Button(container, SWT.NONE); testConnectionButton.setText(WizardMessages .getString("NewDockerConnectionPage.testConnection")); //$NON-NLS-1$ - GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER) - .span(1, 1).align(SWT.END, SWT.CENTER) - .applyTo(testConnectionButton); + GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER).span(3, 1) + .align(SWT.END, SWT.CENTER).applyTo(testConnectionButton); testConnectionButton .addSelectionListener(onTestConnectionButtonSelection()); @@ -624,12 +631,53 @@ public class NewDockerConnectionPage extends WizardPage { private SelectionListener onSearchButtonSelection() { return new SelectionAdapter() { + + private String getVMDriverInstallDir() { + return Activator.getDefault() + .getPreferenceStore().getString( + PreferenceConstants.VM_DRIVER_INSTALLATION_DIRECTORY); + } + + private String getDockerMachineInstallDir() { + return Activator.getDefault() + .getPreferenceStore() + .getString(PreferenceConstants.DOCKER_MACHINE_INSTALLATION_DIRECTORY); + } + @Override public void widgetSelected(SelectionEvent e) { - String[] dmNames = DockerMachine.getNames(); - List<String> activeNames = new ArrayList<>(); + if (getDockerMachineInstallDir().equals("") + || getVMDriverInstallDir().equals("")) { + final boolean confirm = MessageDialog.openConfirm( + getShell(), + WizardMessages.getString( + "DockerMachineSupport.title"), //$NON-NLS-1$ + WizardMessages + .getString("DockerMachineSupport.message")); //$NON-NLS-1$ + if (confirm) { + PreferencesUtil.createPreferenceDialogOn(getShell(), + DOCKER_MACHINE_PREFERENCE_PAGE_ID, + new String[] { + DOCKER_MACHINE_PREFERENCE_PAGE_ID }, + null).open(); + // after user input data, check again + if (getDockerMachineInstallDir().equals("") + || getVMDriverInstallDir().equals("")) { + return; + } + } else { + return; + } + } + final String dockerMachineInstallDir = getDockerMachineInstallDir(); + final String vmDriverInstallDir = getVMDriverInstallDir(); + + final String[] dmNames = DockerMachine + .getNames(dockerMachineInstallDir); + final List<String> activeNames = new ArrayList<>(); for (String name : dmNames) { - if (DockerMachine.getHost(name) != null) { + if (DockerMachine.getHost(name, dockerMachineInstallDir, + vmDriverInstallDir) != null) { activeNames.add(name); } } @@ -644,8 +692,10 @@ public class NewDockerConnectionPage extends WizardPage { connPrompt.setInput(activeNames.toArray(new String[0])); if (connPrompt.open() == 0 && connPrompt.getResult().length > 0) { String name = ((String) connPrompt.getResult()[0]); - String host = DockerMachine.getHost(name); - String certPath = DockerMachine.getCertPath(name); + String host = DockerMachine.getHost(name, + dockerMachineInstallDir, vmDriverInstallDir); + String certPath = DockerMachine.getCertPath(name, + dockerMachineInstallDir, vmDriverInstallDir); model.setBindingMode(EnumDockerConnectionSettings.TCP_CONNECTION); model.setConnectionName(name); model.setUnixSocketPath(null); @@ -659,18 +709,29 @@ public class NewDockerConnectionPage extends WizardPage { } } } else { - if (dmNames.length > 0) { - StringBuffer connections = new StringBuffer(); - for (String conn : dmNames) { - connections.append(", "); //$NON-NLS-1$ - connections.append(conn); + if (dmNames.length == 1) { + MessageDialog.openInformation(getShell(), + WizardMessages.getString( + "NewDockerConnectionPage.searchDialog.discovery.title"), //$NON-NLS-1$ + WizardMessages.getFormattedString( + "NewDockerConnectionPage.searchDialog.discovery.innactive.single", //$NON-NLS-1$ + dmNames[0])); + } else if (dmNames.length > 1) { + final StringBuffer connections = new StringBuffer(); + for (Iterator<String> iterator = Arrays.asList(dmNames) + .iterator(); iterator.hasNext();) { + final String dmName = iterator.next(); + connections.append(dmName); + if (iterator.hasNext()) { + connections.append(", "); //$NON-NLS-1$ + } } MessageDialog.openInformation(getShell(), WizardMessages.getString( "NewDockerConnectionPage.searchDialog.discovery.title"), //$NON-NLS-1$ WizardMessages.getFormattedString( - "NewDockerConnectionPage.searchDialog.discovery.innactive", //$NON-NLS-1$ - connections.substring(2))); + "NewDockerConnectionPage.searchDialog.discovery.innactive.multiple", //$NON-NLS-1$ + connections.toString())); } else { MessageDialog.openInformation(getShell(), WizardMessages.getString( @@ -681,6 +742,8 @@ public class NewDockerConnectionPage extends WizardPage { } } }; + + } /** diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties index 239ae0d09f..6e69e92a97 100644 --- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties +++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/wizards/WizardMessages.properties @@ -165,7 +165,7 @@ NewDockerConnectionPage.tcpAuthButton=Enable authentication NewDockerConnectionPage.tcpPathLabel=Path: NewDockerConnectionPage.tcpPathTooltip=Path to the certificates folder NewDockerConnectionPage.testConnection=Test Connection -NewDockerConnectionPage.searchButton=Search +NewDockerConnectionPage.searchButton=Search... NewDockerConnectionPage.retrieveTask=Retrieving Docker connection settings... NewDockerConnectionPage.pingTask=Pinging Docker daemon... NewDockerConnectionPage.success=Success @@ -193,7 +193,11 @@ NewDockerConnectionPage.searchDialog.title=Docker Connection Selection NewDockerConnectionPage.searchDialog.message=Select the connection to create. NewDockerConnectionPage.searchDialog.discovery.title=Connection Discovery NewDockerConnectionPage.searchDialog.discovery.empty=No connections were found. -NewDockerConnectionPage.searchDialog.discovery.innactive="Discovered connection(s): {0} but none are active." +NewDockerConnectionPage.searchDialog.discovery.innactive.single=Discovered a connection ({0}) but it is not active. +NewDockerConnectionPage.searchDialog.discovery.innactive.multiple=Discovered multiple connections ({0}) but none are active. + +DockerMachineSupport.title=Docker Machine Support +DockerMachineSupport.message=You need to set the preferences for Docker Machine. ImageSearch.title=Search and pull a Docker image ImageSearchPage.title=Search the Docker Registry for images |