Skip to content

Commit

Permalink
Merge pull request #186 from mariotoffia/feature-custom-host
Browse files Browse the repository at this point in the history
Support for custom IHostService creation in HostBuilder
  • Loading branch information
mariotoffia authored Mar 28, 2021
2 parents 2b4a1f5 + f7a93a0 commit ecfb44b
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 12 deletions.
39 changes: 39 additions & 0 deletions Ductus.FluentDocker/Builders/HostBuilder.cs
Original file line number Diff line number Diff line change
@@ -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<IHostService>
{
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()
Expand All @@ -27,6 +36,36 @@ public HostBuilder UseNative()
return this;
}

public HostBuilder UseHost(IHostService customHostService) {
this.customHostService = customHostService;
return this;
}

/// <summary>
/// Creates a `IHostService` based on a _URI_.
/// </summary>
/// <param name="uri">The _URI_ to the docker daemon.</param>
/// <param name="name">An optional name. If none is specified the _URI_ is the name.</param>
/// <param name="isNative">If the docker daemon is native or not. Default to true.</param>
/// <param name="stopWhenDisposed">If it should be stopped when disposed, default to false.</param>
/// <param name="isWindowsHost">If it is a docker daemon that controls windows containers or not. Default false.</param>
/// <param name="certificatePath">
/// 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_.
/// </param>
/// <returns>Itself for fluent access.</returns>
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);
Expand Down
28 changes: 16 additions & 12 deletions Ductus.FluentDocker/Services/Extensions/NetworkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,22 @@ public static void WaitForHttp(this IContainerService service, string url, long
/// <summary>Get extra long current timestamp</summary>
private static long Millis => (long)((DateTime.UtcNow - Jan1St1970).TotalMilliseconds);

/// <summary>
/// <summary>
/// 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.
/// </summary>
/// <param name="ports">The ports from the <see cref="ContainerNetworkSettings.Ports" /> property.</param>
/// <param name="portAndProto">The port and protocol string.</param>
/// <param name="dockerUri">Optional docker uri to use when the address is 0.0.0.0 in the endpoint.</param>
/// <returns>A endpoint of the host exposed ip and port into the container port. If none is found, null is returned.</returns>
/// <returns>
/// A endpoint of the host exposed ip and port into the container port.
/// If none is found, null is returned.
/// </returns>
public static IPEndPoint ToHostPort(this Dictionary<string, HostIpEndpoint[]> ports, string portAndProto,
Uri dockerUri = null)
{
return ToHostPortCustomResolver(ports, null, portAndProto, dockerUri);
}
Uri dockerUri = null)
{
return ToHostPortCustomResolver(ports, null, portAndProto, dockerUri);
}

/// <summary>
/// Translates a docker exposed port and protocol (on format 'port/proto' e.g. '534/tcp') to a
Expand All @@ -133,22 +136,23 @@ public static IPEndPoint ToHostPort(this Dictionary<string, HostIpEndpoint[]> po
/// <param name="dockerUri">Optional docker uri to use when the address is 0.0.0.0 in the endpoint.</param>
/// <returns>A endpoint of the host exposed ip and port into the container port. If none is found, null is returned.</returns>
public static IPEndPoint ToHostPortCustomResolver(
this Dictionary<string, HostIpEndpoint[]> ports,
Func<Dictionary<string, HostIpEndpoint[]>,string, Uri, IPEndPoint> customResolver,
this Dictionary<string, HostIpEndpoint[]> ports,
Func<Dictionary<string, HostIpEndpoint[]>, 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;

Expand Down
38 changes: 38 additions & 0 deletions Ductus.FluentDocker/Services/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,43 @@ public IHostService Native()
return null;
}

/// <summary>
/// Creates a `IHostService` based on a _URI_.
/// </summary>
/// <param name="uri">The _URI_ to the docker daemon.</param>
/// <param name="name">An optional name. If none is specified the _URI_ is the name.</param>
/// <param name="isNative">If the docker daemon is native or not. Default to true.</param>
/// <param name="stopWhenDisposed">If it should be stopped when disposed, default to false.</param>
/// <param name="isWindowsHost">If it is a docker daemon that controls windows containers or not. Default false.</param>
/// <param name="certificatePath">
/// 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_.
/// </param>
/// <returns>A newly created host service.</returns>
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);
Expand Down

0 comments on commit ecfb44b

Please sign in to comment.