From 14381732e3b3024e12b5c058316e2c34704abb0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roland=20Hu=C3=9F?= Date: Sun, 14 Jun 2015 19:12:44 +0200 Subject: [PATCH] Refactored so that socket path is directly set. Relates to #179 --- doc/changelog.md | 2 +- doc/manual.md | 16 ++++++++------ .../docker/maven/AbstractDockerMojo.java | 21 ++++++++++++------- .../access/hc/DockerAccessWithHcClient.java | 14 ++++++------- .../hc/unix/UnixConnectionSocketFactory.java | 8 ++++++- .../maven/access/hc/unix/UnixSocket.java | 2 +- .../hc/unix/UnixSocketClientBuilder.java | 9 ++++---- 7 files changed, 45 insertions(+), 27 deletions(-) diff --git a/doc/changelog.md b/doc/changelog.md index 3eaa33aa4..0ea6e98b4 100644 --- a/doc/changelog.md +++ b/doc/changelog.md @@ -2,7 +2,7 @@ * **0.12.0** - Allow CMD and ENTRYPOINT with shell and exec arguments (#130, #149) - - Add Unix Socker support (#179) + - Unix Socket support (#179) Please note that for consistencies sake `` has been renamed to `` which contains inner elements to match better the equivalent Dockerfile argument. The update should be trivial and easy to spot since a build will croak immediately. diff --git a/doc/manual.md b/doc/manual.md index 94117f9e4..b32d65b33 100644 --- a/doc/manual.md +++ b/doc/manual.md @@ -77,13 +77,14 @@ parentheses. to on your Docker Daemon is listening. This plugin requires the usage of the Docker remote API so this must be enabled. If this configuration option is not given, the environment variable - `DOCKER_HOST` is evaluated. If this is also not set the plugin will - stop with an error. The scheme of this URL can be either given + `DOCKER_HOST` is evaluated. If this is also not set the plugin will use `unix:///var/run/docker.sock` + as a default. The scheme of this URL can be either given directly as `http` or `https` depending on whether plain HTTP - communication is enabled (< 1.3.0) or SSL should be used (>= - 1.3.0). Or the scheme could be `tcp` in which case the protocol is + communication is enabled or SSL should be used (default since Docker + 1.3.0). Alternatively the scheme could be `tcp` in which case the protocol is determined via the IANA assigned port: 2375 for `http` and 2376 for - `https`. + `https`. Finally Unix sockets are supported with when a scheme `unix` is used together with the + filesystem path to the unix socket. * **apiVersion** (`docker.apiVersion`) Use this variable if you are using an older version of docker not compatible with the current default use to communicate with the server. @@ -772,7 +773,10 @@ or `keepRunning` is used). You can use maven properties in each condition, too. In the example, the `${host.port}` property is probably set before within a port mapping section. -The property `${docker.host.address}` is set implicitly to the address of the Docker host. +The property `${docker.host.address}` is set implicitly to the address of the Docker host. This host will +be taken from the `docker.host` configuration if HTTP or HTTPS is used. If a Unix socket is used for communication +with the docker daemon, then `localhost` is assumed. You can override this property always by setting this Maven +property explicitly. ##### Log configuration diff --git a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java index 8f7a9ca35..cee52fa33 100644 --- a/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java +++ b/src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java @@ -249,14 +249,21 @@ private DockerAccess createDockerAccess(String baseUrl) throws MojoExecutionExce // Registry for managed containers private void setDockerHostAddressProperty(String dockerUrl) throws MojoFailureException { - final String host; - try { - host = new URI(dockerUrl).getHost(); - } catch (URISyntaxException e) { - throw new MojoFailureException("Cannot parse " + dockerUrl + " as URI: " + e.getMessage(), e); + Properties props = project.getProperties(); + if (props.getProperty("docker.host.address") == null) { + final String host; + try { + URI uri = new URI(dockerUrl); + if (uri.getHost() == null && uri.getScheme().equals("unix")) { + host = "localhost"; + } else { + host = uri.getHost(); + } + } catch (URISyntaxException e) { + throw new MojoFailureException("Cannot parse " + dockerUrl + " as URI: " + e.getMessage(), e); + } + props.setProperty("docker.host.address", host == null ? "" : host); } - - project.getProperties().setProperty("docker.host.address", host == null ? "" : host); } // ================================================================================= diff --git a/src/main/java/org/jolokia/docker/maven/access/hc/DockerAccessWithHcClient.java b/src/main/java/org/jolokia/docker/maven/access/hc/DockerAccessWithHcClient.java index ad0a0b3fa..3b56bdef7 100644 --- a/src/main/java/org/jolokia/docker/maven/access/hc/DockerAccessWithHcClient.java +++ b/src/main/java/org/jolokia/docker/maven/access/hc/DockerAccessWithHcClient.java @@ -33,6 +33,9 @@ */ public class DockerAccessWithHcClient implements DockerAccess { + // Base URL which is given through when using UnixSocket communication but is not really used + private static final String DUMMY_BASE_URL = "unix://127.0.0.1:1/"; + // Logging private final Logger log; @@ -47,16 +50,13 @@ public class DockerAccessWithHcClient implements DockerAccess { */ public DockerAccessWithHcClient(String apiVersion, String baseUrl, String certPath, Logger log) throws IOException { this.log = log; - this.delegate = createDelegate(baseUrl, certPath); - this.urlBuilder = new UrlBuilder(baseUrl, apiVersion); - } - - private ApacheHttpClientDelegate createDelegate(String baseUrl, String certPath) throws IOException { URI uri = URI.create(baseUrl); if (uri.getScheme().equalsIgnoreCase("unix")) { - return new ApacheHttpClientDelegate(new UnixSocketClientBuilder().build()); + this.delegate = new ApacheHttpClientDelegate(new UnixSocketClientBuilder().build(uri.getPath())); + this.urlBuilder = new UrlBuilder(DUMMY_BASE_URL,apiVersion); } else { - return new ApacheHttpClientDelegate(new HttpClientBuilder(isSSL(baseUrl) ? certPath : null).build()); + this.delegate = new ApacheHttpClientDelegate(new HttpClientBuilder(isSSL(baseUrl) ? certPath : null).build()); + this.urlBuilder = new UrlBuilder(baseUrl, apiVersion); } } diff --git a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixConnectionSocketFactory.java b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixConnectionSocketFactory.java index f3c283064..aef9bd592 100644 --- a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixConnectionSocketFactory.java +++ b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixConnectionSocketFactory.java @@ -13,6 +13,12 @@ final class UnixConnectionSocketFactory implements ConnectionSocketFactory { + private final File unixSocketFile; + + public UnixConnectionSocketFactory(String unixSocketPath) { + this.unixSocketFile = new File(unixSocketPath); + } + @Override public Socket createSocket(HttpContext context) throws IOException { return new UnixSocket(); @@ -22,7 +28,7 @@ public Socket createSocket(HttpContext context) throws IOException { public Socket connectSocket(int connectTimeout, Socket sock, HttpHost host, InetSocketAddress remoteAddress, InetSocketAddress localAddress, HttpContext context) throws IOException { - sock.connect(new UnixSocketAddress(new File(host.getHostName())), connectTimeout); + sock.connect(new UnixSocketAddress(unixSocketFile), connectTimeout); return sock; } } diff --git a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocket.java b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocket.java index e58bfa6a8..0761958f5 100644 --- a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocket.java +++ b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocket.java @@ -243,7 +243,7 @@ public void setReuseAddress(boolean on) throws SocketException { @Override public boolean getReuseAddress() throws SocketException { - throw new UnsupportedOperationException("Getting the SO_RESUEADDR option is not supported"); + throw new UnsupportedOperationException("Getting the SO_REUSEADDR option is not supported"); } @Override diff --git a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocketClientBuilder.java b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocketClientBuilder.java index 75edb4cd4..6aad333f9 100644 --- a/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocketClientBuilder.java +++ b/src/main/java/org/jolokia/docker/maven/access/hc/unix/UnixSocketClientBuilder.java @@ -12,18 +12,19 @@ public class UnixSocketClientBuilder { - public CloseableHttpClient build() { + + public CloseableHttpClient build(String unixSocketPath) { final HttpClientBuilder httpBuilder = HttpClients.custom(); - final Registry registry = buildRegistry(); + final Registry registry = buildRegistry(unixSocketPath); final DnsResolver dnsResolver = nullDnsResolver(); final HttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry, dnsResolver); httpBuilder.setConnectionManager(manager); return httpBuilder.build(); } - private Registry buildRegistry() { + private Registry buildRegistry(String path) { final RegistryBuilder registryBuilder = RegistryBuilder.create(); - registryBuilder.register("unix", new UnixConnectionSocketFactory()); + registryBuilder.register("unix", new UnixConnectionSocketFactory(path)); return registryBuilder.build(); }