Skip to content

Commit

Permalink
Windows 10 named pipe docker client connection - squashed and signed …
Browse files Browse the repository at this point in the history
…commit

Signed-off-by: Jan Zajíc <[email protected]>
  • Loading branch information
jan-zajic committed Aug 4, 2016
1 parent 163ed8c commit 8112cdf
Show file tree
Hide file tree
Showing 11 changed files with 685 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ public String extractUrl(String dockerHost) throws IOException {
File unixSocket = new File("/var/run/docker.sock");
if (unixSocket.exists() && unixSocket.canRead() && unixSocket.canWrite()) {
return "unix:///var/run/docker.sock";
}
throw new IllegalArgumentException("No <dockerHost> or <machine> given, no DOCKER_HOST environment variable, and no read/writable '/var/run/docker.sock'");
}
File windowsPipe = new File("//./pipe/docker_engine");
if(windowsPipe.exists()) {
return "npipe:////./pipe/docker_engine";
}
throw new IllegalArgumentException("No <dockerHost> or <machine> given, no DOCKER_HOST environment variable, and no read/writable '/var/run/docker.sock' or '//./pipe/docker_engine'");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ public class ApacheHttpClientDelegate {
private final CloseableHttpClient httpClient;

public ApacheHttpClientDelegate(ClientBuilder clientBuilder) throws IOException {
this(clientBuilder, true);
}

public ApacheHttpClientDelegate(ClientBuilder clientBuilder, boolean pooled) throws IOException {
this.clientBuilder = clientBuilder;
this.httpClient = clientBuilder.buildPooledClient();
if(pooled)
this.httpClient = clientBuilder.buildPooledClient();
else
this.httpClient = clientBuilder.buildBasicClient();
}

public CloseableHttpClient createBasicClient() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
package io.fabric8.maven.docker.access.hc;

import java.io.*;
import static java.net.HttpURLConnection.HTTP_CREATED;
import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
import static java.net.HttpURLConnection.HTTP_NOT_FOUND;
import static java.net.HttpURLConnection.HTTP_NOT_MODIFIED;
import static java.net.HttpURLConnection.HTTP_NO_CONTENT;
import static java.net.HttpURLConnection.HTTP_OK;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import io.fabric8.maven.docker.access.*;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.json.JSONArray;
import org.json.JSONObject;

import io.fabric8.maven.docker.access.AuthConfig;
import io.fabric8.maven.docker.access.ContainerCreateConfig;
import io.fabric8.maven.docker.access.DockerAccess;
import io.fabric8.maven.docker.access.DockerAccessException;
import io.fabric8.maven.docker.access.NetworkCreateConfig;
import io.fabric8.maven.docker.access.UrlBuilder;
import io.fabric8.maven.docker.access.chunked.BuildJsonResponseHandler;
import io.fabric8.maven.docker.access.chunked.EntityStreamReaderUtil;
import io.fabric8.maven.docker.access.chunked.PullOrPushResponseJsonHandler;
import io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.BodyAndStatusResponseHandler;
import io.fabric8.maven.docker.access.hc.ApacheHttpClientDelegate.HttpBodyAndStatus;
import io.fabric8.maven.docker.access.hc.http.HttpClientBuilder;
import io.fabric8.maven.docker.access.hc.unix.UnixSocketClientBuilder;
import io.fabric8.maven.docker.access.hc.win.NamedPipeClientBuilder;
import io.fabric8.maven.docker.access.log.LogCallback;
import io.fabric8.maven.docker.access.log.LogGetHandle;
import io.fabric8.maven.docker.access.log.LogRequestor;
Expand All @@ -21,16 +50,10 @@
import io.fabric8.maven.docker.model.ContainersListElement;
import io.fabric8.maven.docker.model.Network;
import io.fabric8.maven.docker.model.NetworksListElement;
import io.fabric8.maven.docker.util.*;
import io.fabric8.maven.docker.access.hc.unix.UnixSocketClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import io.fabric8.maven.docker.access.hc.http.HttpClientBuilder;
import org.json.JSONArray;
import org.json.JSONObject;

import static java.net.HttpURLConnection.*;
import io.fabric8.maven.docker.util.EnvUtil;
import io.fabric8.maven.docker.util.ImageName;
import io.fabric8.maven.docker.util.Logger;
import io.fabric8.maven.docker.util.Timestamp;

/**
* Implementation using <a href="http://hc.apache.org/">Apache HttpComponents</a> for accessing
Expand All @@ -52,6 +75,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/";

// Base URL which is given through when using NamedPipe communication but is not really used
private static final String DUMMY_NPIPE_URL = "npipe://127.0.0.1:1/";

// Logging
private final Logger log;

Expand All @@ -77,11 +103,14 @@ public DockerAccessWithHcClient(String apiVersion,
this.log = log;
URI uri = URI.create(baseUrl);
if (uri.getScheme() == null) {
throw new IllegalArgumentException("The docker access url '" + baseUrl + "' must contain a schema tcp:// or unix://");
throw new IllegalArgumentException("The docker access url '" + baseUrl + "' must contain a schema tcp:// or unix:// or npipe://");
}
if (uri.getScheme().equalsIgnoreCase("unix")) {
this.delegate = createHttpClient(new UnixSocketClientBuilder(uri.getPath(), maxConnections));
this.urlBuilder = new UrlBuilder(DUMMY_BASE_URL, apiVersion);
} else if(uri.getScheme().equalsIgnoreCase("npipe")) {
this.delegate = createHttpClient(new NamedPipeClientBuilder(uri.getPath(), maxConnections, log), false);
this.urlBuilder = new UrlBuilder(DUMMY_NPIPE_URL, apiVersion);
} else {
this.delegate = createHttpClient(new HttpClientBuilder(isSSL(baseUrl) ? certPath : null, maxConnections));
this.urlBuilder = new UrlBuilder(baseUrl, apiVersion);
Expand Down Expand Up @@ -207,7 +236,7 @@ public void stopContainer(String containerId, int killWait) throws DockerAccessE
public void buildImage(String image, File dockerArchive, String dockerfileName, boolean forceRemove, boolean noCache,
Map<String, String> buildArgs) throws DockerAccessException {
try {
String url = urlBuilder.buildImage(image, dockerfileName, forceRemove, noCache, buildArgs);
String url = urlBuilder.buildImage(image, dockerfileName, forceRemove, noCache, buildArgs);
delegate.post(url, dockerArchive, createBuildResponseHandler(), HTTP_OK);
} catch (IOException e) {
throw new DockerAccessException(e, "Unable to build image [%s]", image);
Expand Down Expand Up @@ -445,7 +474,11 @@ public void shutdown() {
}

ApacheHttpClientDelegate createHttpClient(ClientBuilder builder) throws IOException {
return new ApacheHttpClientDelegate(builder);
return createHttpClient(builder, true);
}

ApacheHttpClientDelegate createHttpClient(ClientBuilder builder, boolean pooled) throws IOException {
return new ApacheHttpClientDelegate(builder, pooled);
}

// visible for testing?
Expand Down
Loading

0 comments on commit 8112cdf

Please sign in to comment.