Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Coulon2015-11-17 07:58:26 +0000
committerXavier Coulon2015-11-27 14:43:15 +0000
commitfd21fefe291b356704127a81ba5df73e6fcd600f (patch)
tree2e1b9a702e83143805b6254caa1702d6baa96279 /containers/org.eclipse.linuxtools.docker.core/src/org/eclipse
parent3620577e776af367e761c1e8aea946aafb589517 (diff)
downloadorg.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
Diffstat (limited to 'containers/org.eclipse.linuxtools.docker.core/src/org/eclipse')
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerMachine.java134
1 files changed, 100 insertions, 34 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];
}
}

Back to the top