Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Coulon2015-06-01 12:13:16 -0400
committerJeff Johnston2015-06-01 19:03:05 -0400
commit61ae9452023518ba93ac987f25af17ce8116f783 (patch)
treedb9131ef3ef345ff782e896e4d86bbc8d1dfab4d
parentc44c226d9e41f363f35afdc8fa8bac9b55ac6f0d (diff)
downloadorg.eclipse.linuxtools-61ae9452023518ba93ac987f25af17ce8116f783.tar.gz
org.eclipse.linuxtools-61ae9452023518ba93ac987f25af17ce8116f783.tar.xz
org.eclipse.linuxtools-61ae9452023518ba93ac987f25af17ce8116f783.zip
Bug 469008 - Add script to detect connection settings on Windows
Refactored the code to only create the script file in the data dir when needed Check the OS name and return the name of the script to use (script.sh / scrip.bat) Using try-with-resources to avoid having to manually close the resources. Change-Id: Ia1055f2064e0f7d92893948be60e6ce71cc6645d Signed-off-by: Xavier Coulon <xcoulon@redhat.com> Reviewed-on: https://git.eclipse.org/r/49150 Tested-by: Hudson CI Reviewed-by: Jeff Johnston <jjohnstn@redhat.com> Tested-by: Jeff Johnston <jjohnstn@redhat.com>
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/resources/script.bat2
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.java1
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.properties3
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java122
4 files changed, 83 insertions, 45 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.core/resources/script.bat b/containers/org.eclipse.linuxtools.docker.core/resources/script.bat
new file mode 100644
index 0000000000..cea9f0637b
--- /dev/null
+++ b/containers/org.eclipse.linuxtools.docker.core/resources/script.bat
@@ -0,0 +1,2 @@
+@echo off
+SET DOCKER \ No newline at end of file
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.java
index 3412796595..19b6cbe46e 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.java
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.java
@@ -31,6 +31,7 @@ public class Messages extends NLS {
public static String Retrieve_Docker_Certificates_Failure;
public static String List_Docker_Containers_Failure;
public static String Docker_General_Info_Failure;
+ public static String Docker_No_Settings_Description_Script;
static {
// Initialize resource bundle.
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.properties b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.properties
index 72323a1aa6..5a6fd27dc0 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.properties
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/docker/core/Messages.properties
@@ -24,4 +24,5 @@ Open_Connection_Failure=Failed to open a connection
Docker_Daemon_Ping_Failure=Failed to ping the Docker daemon
Retrieve_Docker_Certificates_Failure=Unable to retrieve certificates to connect to the Docker daemon
List_Docker_Containers_Failure=Failed to load list of Docker containers from {0}
-Docker_General_Info_Failure=Failed to get Docker connection info \ No newline at end of file
+Docker_General_Info_Failure=Failed to get Docker connection info
+Docker_No_Settings_Description_Script=There is no settings detection script for this OS \ No newline at end of file
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
index b79c62048b..abfa65a033 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
@@ -26,7 +26,6 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URI;
-import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.file.FileSystems;
@@ -40,9 +39,6 @@ import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
-import jnr.unixsocket.UnixSocketAddress;
-import jnr.unixsocket.UnixSocketChannel;
-
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
@@ -100,6 +96,9 @@ import com.spotify.docker.client.messages.Info;
import com.spotify.docker.client.messages.PortBinding;
import com.spotify.docker.client.messages.Version;
+import jnr.unixsocket.UnixSocketAddress;
+import jnr.unixsocket.UnixSocketChannel;
+
/**
* A connection to a Docker daemon. The connection may rely on Unix Socket or TCP connection (using the REST API).
* All low-level communication is delegated to a wrapped {@link DockerClient}.
@@ -110,32 +109,12 @@ public class DockerConnection implements IDockerConnection {
public static class Defaults {
- public static final String DEFAULT_UNIX_SOCKET_PATH = "unix:///var/run/docker.sock";
+ public static final String DEFAULT_UNIX_SOCKET_PATH = "unix:///var/run/docker.sock"; //$NON-NLS-1$
private String name = Messages.Default_Name;
private final Map<EnumDockerConnectionSettings, Object> settings = new HashMap<>();
public Defaults() throws DockerException {
- File scriptFile = Activator.getDefault().getBundle()
- .getDataFile("script.sh"); //$NON-NLS-1$
- if (!scriptFile.exists()) {
- InputStream is = DockerConnection.class
- .getResourceAsStream("/resources/script.sh"); //$NON-NLS-1$
- FileOutputStream fo;
- try {
- byte[] buff = new byte[1024];
- fo = new FileOutputStream(scriptFile);
- int n;
- while ((n = is.read(buff)) > 0) {
- fo.write(buff, 0, n);
- }
- fo.close();
- is.close();
- scriptFile.setExecutable(true);
- } catch (IOException e) {
- Activator.logErrorMessage(e.getMessage());
- }
- }
// first, looking for a Unix socket at /var/run/docker.sock
if (defaultsWithUnixSocket() || defaultsWithSystemEnv()
|| defaultWithShellEnv()) {
@@ -162,7 +141,7 @@ public class DockerConnection implements IDockerConnection {
* {@code false} otherwise.
*/
private boolean defaultsWithUnixSocket() {
- final File unixSocketFile = new File("/var/run/docker.sock");
+ final File unixSocketFile = new File("/var/run/docker.sock"); //$NON-NLS-1$
if (unixSocketFile.exists() && unixSocketFile.canRead()) {
try {
final UnixSocketAddress address = new UnixSocketAddress(
@@ -189,15 +168,15 @@ public class DockerConnection implements IDockerConnection {
* {@code false} otherwise.
*/
private boolean defaultsWithSystemEnv() {
- final String dockerHostEnv = System.getenv("DOCKER_HOST");
+ final String dockerHostEnv = System.getenv("DOCKER_HOST"); //$NON-NLS-1$
if (dockerHostEnv != null) {
settings.put(BINDING_MODE, TCP_CONNECTION);
settings.put(TCP_HOST, dockerHostEnv);
- final String tlsVerifyEnv = System.getenv("DOCKER_TLS_VERIFY");
+ final String tlsVerifyEnv = System.getenv("DOCKER_TLS_VERIFY"); //$NON-NLS-1$
if (tlsVerifyEnv != null && tlsVerifyEnv.equals("1")) {
settings.put(TCP_TLS_VERIFY, Boolean.TRUE);
final String dockerCertPathEnv = System
- .getenv("DOCKER_CERT_PATH");
+ .getenv("DOCKER_CERT_PATH"); //$NON-NLS-1$
if (dockerCertPathEnv != null) {
settings.put(TCP_CERT_PATH, dockerCertPathEnv);
}
@@ -220,11 +199,16 @@ public class DockerConnection implements IDockerConnection {
*/
private boolean defaultWithShellEnv() throws DockerException {
try {
- // FIXME need to verify the OS and decide which script to run
- File scriptFile = Activator.getDefault().getBundle()
- .getDataFile("script.sh"); //$NON-NLS-1$
+ final String connectionSettingsDetectionScriptName = getConnectionSettingsDetectionScriptName();
+ if (connectionSettingsDetectionScriptName == null) {
+ Activator.log(new Status(IStatus.WARNING,
+ Activator.PLUGIN_ID,
+ Messages.Docker_No_Settings_Description_Script));
+ return false;
+ }
+ final File connectionSettingsDetectionScript = getConnectionSettingsDetectionScript(connectionSettingsDetectionScriptName);
final Process process = Runtime.getRuntime().exec(
- new String[] { scriptFile.getAbsolutePath() });
+ new String[] {connectionSettingsDetectionScript.getAbsolutePath()});
process.waitFor();
if (process.exitValue() == 0) {
final InputStream processInputStream = process
@@ -233,20 +217,20 @@ public class DockerConnection implements IDockerConnection {
Properties dockerSettings = new Properties();
dockerSettings.load(processInputStream);
settings.put(BINDING_MODE, TCP_CONNECTION);
- if (dockerSettings.containsKey("DOCKER_HOST")) {
- settings.put(TCP_HOST, dockerSettings
- .get("DOCKER_HOST").toString());
+ if (dockerSettings.containsKey("DOCKER_HOST")) { //$NON-NLS-1$
+ settings.put(TCP_HOST,
+ dockerSettings.get("DOCKER_HOST").toString()); //$NON-NLS-1$
}
- if (dockerSettings.containsKey("DOCKER_CERT_PATH")) {
+ if (dockerSettings.containsKey("DOCKER_CERT_PATH")) { //$NON-NLS-1$
settings.put(TCP_CERT_PATH,
- dockerSettings.get("DOCKER_CERT_PATH")
+ dockerSettings.get("DOCKER_CERT_PATH") //$NON-NLS-1$
.toString());
}
- if (dockerSettings.containsKey("DOCKER_TLS_VERIFY")) {
- settings.put(
- TCP_TLS_VERIFY,
- Boolean.valueOf(dockerSettings
- .get("DOCKER_TLS_VERIFY").toString()
+ if (dockerSettings.containsKey("DOCKER_TLS_VERIFY")) { //$NON-NLS-1$
+ settings.put(TCP_TLS_VERIFY,
+ Boolean.valueOf(
+ dockerSettings.get("DOCKER_TLS_VERIFY") //$NON-NLS-1$
+ .toString()
.equals("1")));
}
return true;
@@ -264,6 +248,56 @@ public class DockerConnection implements IDockerConnection {
return false;
}
+ /**
+ * Finds the script file in the data directory of the bundle given its
+ * name, or creates it from the 'resources' dir in the bundle if it was
+ * not found in the data dir.
+ *
+ * @param scriptName
+ * the name of the script to load in the data dir or in the
+ * 'resources' dir in the bundle
+ * @return the script {@link File}
+ */
+ private File getConnectionSettingsDetectionScript(
+ final String scriptName) {
+ final File script = Activator.getDefault().getBundle()
+ .getDataFile(scriptName);
+ if (script != null && !script.exists()) {
+ try (final FileOutputStream output = new FileOutputStream(
+ script);
+ final InputStream is = DockerConnection.class
+ .getResourceAsStream(
+ "/resources/" + scriptName)) { //$NON-NLS-1$
+ byte[] buff = new byte[1024];
+ int n;
+ while ((n = is.read(buff)) > 0) {
+ output.write(buff, 0, n);
+ }
+ script.setExecutable(true);
+ } catch (IOException e) {
+ Activator.logErrorMessage(e.getMessage());
+ }
+ }
+ return script;
+ }
+
+ /**
+ * @return the name of the script to run, depending on the OS (Windows,
+ * MAc, *Nix)
+ */
+ private String getConnectionSettingsDetectionScriptName() {
+ final String osName = System.getProperty("os.name"); //$NON-NLS-1$
+ if (osName.toLowerCase().startsWith("win")) { //$NON-NLS-1$
+ return "script.bat"; //$NON-NLS-1$
+ } else if (osName.toLowerCase().startsWith("mac") //$NON-NLS-1$
+ || osName.toLowerCase().contains("linux") //$NON-NLS-1$
+ || osName.toLowerCase().contains("nix")) { //$NON-NLS-1$
+ return "script.sh";//$NON-NLS-1$
+ } else {
+ return null;
+ }
+ }
+
private String streamToString(InputStream stream) {
BufferedReader buff = new BufferedReader(new InputStreamReader(
stream));
@@ -271,7 +305,7 @@ public class DockerConnection implements IDockerConnection {
String line = "";
try {
while ((line = buff.readLine()) != null) {
- res.append(System.getProperty("line.separator"));
+ res.append(System.getProperty("line.separator")); //$NON-NLS-1$
res.append(line);
}
buff.close();

Back to the top