コード例 #1
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images     = new ImageOperations(this);
            Containers = new ContainerOperations(this);
            System     = new SystemOperations(this);
            Networks   = new NetworkOperations(this);
            Secrets    = new SecretsOperations(this);
            Swarm      = new SwarmOperations(this);
            Tasks      = new TasksOperations(this);
            Volumes    = new VolumeOperations(this);
            Plugin     = new PluginOperations(this);
            Exec       = new ExecOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                if (string.Equals(serverName, "localhost", StringComparison.OrdinalIgnoreCase))
                {
                    // npipe schemes dont work with npipe://localhost/... and need npipe://./... so fix that for a client here.
                    serverName = ".";
                }

                var pipeName = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    int timeout      = (int)this.Configuration.NamedPipeConnectTimeout.TotalMilliseconds;
                    var stream       = new NamedPipeClientStream(serverName, pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if (NETSTANDARD1_6 || NETSTANDARD2_0)
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            DefaultTimeout  = Configuration.DefaultTimeout;
            _client.Timeout = s_InfiniteTimeout;
        }
コード例 #2
0
ファイル: DockerClient.cs プロジェクト: gonbike/Docker.DotNet
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration        = configuration;
            _requestedApiVersion = requestedApiVersion;
            JsonSerializer       = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler handler;
            var            uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri     = new UriBuilder("http", pipeName).Uri;
                handler = new ManagedHandler(async(host, port, cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    var timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);

#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                });

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri)
                {
                    Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http"
                };
                uri     = builder.Uri;
                handler = new ManagedHandler();
                break;

            case "https":
                handler = new ManagedHandler();
                break;

#if NETSTANDARD1_6
            case "unix":
                var pipeString = uri.LocalPath;
                handler = new ManagedHandler(async(string host, int port, CancellationToken cancellationToken) =>
                {
                    var sock = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified);
                    await sock.ConnectAsync(new UnixDomainSocketEndPoint(pipeString));
                    return(sock);
                });
                uri = new UriBuilder("http", uri.Segments.Last()).Uri;
                break;
#endif

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = s_InfiniteTimeout;
        }
コード例 #3
0
        internal DockerClient(DockerClientConfiguration configuration, Version requestedApiVersion)
        {
            Configuration       = configuration;
            RequestedApiVersion = requestedApiVersion;
            JsonSerializer      = new JsonSerializer();

            Images        = new ImageOperations(this);
            Containers    = new ContainerOperations(this);
            Miscellaneous = new MiscellaneousOperations(this);
            Networks      = new NetworkOperations(this);

            ManagedHandler.StreamOpener opener;
            var uri = Configuration.EndpointBaseUri;

            switch (uri.Scheme.ToLowerInvariant())
            {
            case "npipe":
                if (Configuration.Credentials.IsTlsCredentials())
                {
                    throw new Exception("TLS not supported over npipe");
                }

                var segments = uri.Segments;
                if (segments.Length != 3 || !segments[1].Equals("pipe/", StringComparison.OrdinalIgnoreCase))
                {
                    throw new ArgumentException($"{Configuration.EndpointBaseUri} is not a valid npipe URI");
                }

                var serverName = uri.Host;
                var pipeName   = uri.Segments[2];

                uri    = new UriBuilder("http", pipeName).Uri;
                opener = async(string host, int port, CancellationToken cancellationToken) =>
                {
                    // NamedPipeClientStream handles file not found by polling until the server arrives. Use a short
                    // timeout so that the user doesn't get stuck waiting for a dockerd instance that is not running.
                    int timeout      = 100; // 100ms
                    var stream       = new NamedPipeClientStream(serverName, pipeName);
                    var dockerStream = new DockerPipeStream(stream);
#if NET45
                    await Task.Run(() => stream.Connect(timeout), cancellationToken);
#else
                    await stream.ConnectAsync(timeout, cancellationToken);
#endif
                    return(dockerStream);
                };

                break;

            case "tcp":
            case "http":
                var builder = new UriBuilder(uri);
                builder.Scheme = configuration.Credentials.IsTlsCredentials() ? "https" : "http";
                uri            = builder.Uri;
                opener         = null;
                break;

            case "https":
                opener = null;
                break;

            //case "unix":
            // TODO

            default:
                throw new Exception($"Unknown URL scheme {configuration.EndpointBaseUri.Scheme}");
            }

            _endpointBaseUri = uri;

            ManagedHandler handler;
            if (opener == null)
            {
                handler = new ManagedHandler();
            }
            else
            {
                handler = new ManagedHandler(opener);
            }

            _client         = new HttpClient(Configuration.Credentials.GetHandler(handler), true);
            _defaultTimeout = _client.Timeout;
            _client.Timeout = InfiniteTimeout;
        }