Skip to content

Commit

Permalink
Refactored so that socket path is directly set.
Browse files Browse the repository at this point in the history
Relates to #179
  • Loading branch information
rhuss committed Jun 14, 2015
1 parent c20ea3f commit ae1719e
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion doc/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* **0.12**
- Add a new parameter 'docker.skipTags' that suppresses tagging of images that have been built.
- 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 `<command>` has been renamed to `<cmd>` 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.
Expand Down
16 changes: 10 additions & 6 deletions doc/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -774,7 +775,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

Expand Down
21 changes: 14 additions & 7 deletions src/main/java/org/jolokia/docker/maven/AbstractDockerMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

// =================================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@

public class UnixSocketClientBuilder {

public CloseableHttpClient build() {

public CloseableHttpClient build(String unixSocketPath) {
final HttpClientBuilder httpBuilder = HttpClients.custom();
final Registry<ConnectionSocketFactory> registry = buildRegistry();
final Registry<ConnectionSocketFactory> registry = buildRegistry(unixSocketPath);
final DnsResolver dnsResolver = nullDnsResolver();
final HttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(registry, dnsResolver);
httpBuilder.setConnectionManager(manager);
return httpBuilder.build();
}

private Registry<ConnectionSocketFactory> buildRegistry() {
private Registry<ConnectionSocketFactory> buildRegistry(String path) {
final RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder.create();
registryBuilder.register("unix", new UnixConnectionSocketFactory());
registryBuilder.register("unix", new UnixConnectionSocketFactory(path));
return registryBuilder.build();
}

Expand Down

0 comments on commit ae1719e

Please sign in to comment.