From f7a93a0bc6524e5c4281b048da788693a160ea77 Mon Sep 17 00:00:00 2001 From: Mario Toffia Date: Sun, 28 Mar 2021 12:12:42 +0200 Subject: [PATCH] added FromUri, UseHost. Both supports creation of custom IHostService. --- Ductus.FluentDocker/Builders/HostBuilder.cs | 39 +++++++++++++++++++ .../Services/Extensions/NetworkExtensions.cs | 28 +++++++------ Ductus.FluentDocker/Services/Hosts.cs | 38 ++++++++++++++++++ 3 files changed, 93 insertions(+), 12 deletions(-) diff --git a/Ductus.FluentDocker/Builders/HostBuilder.cs b/Ductus.FluentDocker/Builders/HostBuilder.cs index a0c089a5..0b4c41cf 100644 --- a/Ductus.FluentDocker/Builders/HostBuilder.cs +++ b/Ductus.FluentDocker/Builders/HostBuilder.cs @@ -1,17 +1,26 @@ using System.Linq; +using Ductus.FluentDocker.Model.Common; using Ductus.FluentDocker.Services; namespace Ductus.FluentDocker.Builders { public sealed class HostBuilder : BaseBuilder { + private IHostService customHostService; + internal HostBuilder(IBuilder builder) : base(builder) { } public override IHostService Build() { + + if (this.customHostService != null) { + return this.customHostService; + } + return IsNative ? new Hosts().Native() : null; + } protected override IBuilder InternalCreate() @@ -27,6 +36,36 @@ public HostBuilder UseNative() return this; } + public HostBuilder UseHost(IHostService customHostService) { + this.customHostService = customHostService; + return this; + } + + /// + /// Creates a `IHostService` based on a _URI_. + /// + /// The _URI_ to the docker daemon. + /// An optional name. If none is specified the _URI_ is the name. + /// If the docker daemon is native or not. Default to true. + /// If it should be stopped when disposed, default to false. + /// If it is a docker daemon that controls windows containers or not. Default false. + /// + /// Optional path to where certificates are located in order to do TLS communication with docker daemon. If not provided, + /// it will try to get it from the environment _DOCKER_CERT_PATH_. + /// + /// Itself for fluent access. + public HostBuilder FromUri( + DockerUri uri, + string name = null, + bool isNative = true, + bool stopWhenDisposed = false, + bool isWindowsHost = false, + string certificatePath = null) + { + this.customHostService = new Hosts().FromUri(uri,name,isNative,stopWhenDisposed,isWindowsHost,certificatePath); + return this; + } + public MachineBuilder UseMachine() { var existing = Childs.FirstOrDefault(x => x is MachineBuilder); diff --git a/Ductus.FluentDocker/Services/Extensions/NetworkExtensions.cs b/Ductus.FluentDocker/Services/Extensions/NetworkExtensions.cs index 3b209e6c..3a0ea781 100644 --- a/Ductus.FluentDocker/Services/Extensions/NetworkExtensions.cs +++ b/Ductus.FluentDocker/Services/Extensions/NetworkExtensions.cs @@ -107,19 +107,22 @@ public static void WaitForHttp(this IContainerService service, string url, long /// Get extra long current timestamp private static long Millis => (long)((DateTime.UtcNow - Jan1St1970).TotalMilliseconds); -/// + /// /// Translates a docker exposed port and protocol (on format 'port/proto' e.g. '534/tcp') to a /// host endpoint that can be contacted outside the container. /// /// The ports from the property. /// The port and protocol string. /// Optional docker uri to use when the address is 0.0.0.0 in the endpoint. - /// A endpoint of the host exposed ip and port into the container port. If none is found, null is returned. + /// + /// A endpoint of the host exposed ip and port into the container port. + /// If none is found, null is returned. + /// public static IPEndPoint ToHostPort(this Dictionary ports, string portAndProto, - Uri dockerUri = null) - { - return ToHostPortCustomResolver(ports, null, portAndProto, dockerUri); - } + Uri dockerUri = null) + { + return ToHostPortCustomResolver(ports, null, portAndProto, dockerUri); + } /// /// Translates a docker exposed port and protocol (on format 'port/proto' e.g. '534/tcp') to a @@ -133,22 +136,23 @@ public static IPEndPoint ToHostPort(this Dictionary po /// Optional docker uri to use when the address is 0.0.0.0 in the endpoint. /// A endpoint of the host exposed ip and port into the container port. If none is found, null is returned. public static IPEndPoint ToHostPortCustomResolver( - this Dictionary ports, - Func,string, Uri, IPEndPoint> customResolver, + this Dictionary ports, + Func, string, Uri, IPEndPoint> customResolver, string portAndProto, Uri dockerUri = null) { - if (customResolver != null) + if (customResolver != null) { var ep = customResolver.Invoke(ports, portAndProto, dockerUri); - - if (ep != null) { + + if (ep != null) + { return ep; } } - + if (null == ports || string.IsNullOrEmpty(portAndProto)) return null; diff --git a/Ductus.FluentDocker/Services/Hosts.cs b/Ductus.FluentDocker/Services/Hosts.cs index 5cd917b8..ce2feb2b 100644 --- a/Ductus.FluentDocker/Services/Hosts.cs +++ b/Ductus.FluentDocker/Services/Hosts.cs @@ -4,6 +4,7 @@ using Ductus.FluentDocker.Commands; using Ductus.FluentDocker.Common; using Ductus.FluentDocker.Extensions; +using Ductus.FluentDocker.Model.Common; using Ductus.FluentDocker.Services.Impl; namespace Ductus.FluentDocker.Services @@ -47,6 +48,43 @@ public IHostService Native() return null; } + /// + /// Creates a `IHostService` based on a _URI_. + /// + /// The _URI_ to the docker daemon. + /// An optional name. If none is specified the _URI_ is the name. + /// If the docker daemon is native or not. Default to true. + /// If it should be stopped when disposed, default to false. + /// If it is a docker daemon that controls windows containers or not. Default false. + /// + /// Optional path to where certificates are located in order to do TLS communication with docker daemon. If not provided, + /// it will try to get it from the environment _DOCKER_CERT_PATH_. + /// + /// A newly created host service. + public IHostService FromUri( + DockerUri uri, + string name = null, + bool isNative = true, + bool stopWhenDisposed = false, + bool isWindowsHost = false, + string certificatePath = null) + { + + if (string.IsNullOrEmpty(certificatePath)) + { + certificatePath = Environment.GetEnvironmentVariable(DockerHostService.DockerCertPath); + } + + if (string.IsNullOrEmpty(name)) + { + name = uri.ToString(); + } + + return new DockerHostService( + name, isNative, stopWhenDisposed, uri.ToString(), certificatePath, isWindowsHost + ); + } + public IHostService FromMachineName(string name, bool isWindowsHost = false, bool throwIfNotStarted = false) { return new DockerHostService(name, false, isWindowsHost, throwIfNotStarted);