Skip to content

Commit

Permalink
Changed behaviour on Host.Discover and fixed bug.
Browse files Browse the repository at this point in the history
The Discover method now takes an optional boolean to prefer native, if it is found, macine is never probed. It also contains a bugfix on issue #48
  • Loading branch information
Mario Toffia committed Jul 2, 2018
1 parent 1774f59 commit 56e8bdb
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 26 deletions.
4 changes: 2 additions & 2 deletions Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFrameworks>netcoreapp1.1;net452</TargetFrameworks>
<VersionPrefix>2.2.19</VersionPrefix>
<VersionPrefix>2.3.0</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker.MsTest</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -19,7 +19,7 @@ Documentation: https://github.com/mariotoffia/FluentDocker
<Copyright>© 2016, 2017, 2018 Mario Toffia</Copyright>
<DefineConstants Condition=" '$(TargetFramework)' == 'netcoreapp1.1' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>2.2.19</Version>
<Version>2.3.0</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand Down
9 changes: 7 additions & 2 deletions Ductus.FluentDocker/Builders/ContainerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public override IContainerService Build()
.FirstOrDefault(x => IsNameMatch(x.Name, _config.CreateParams.Name));

if (null != existing)
{
existing.RemoveOnDispose = _config.DeleteOnDispose;
existing.StopOnDispose = _config.StopOnDispose;

return existing;
}
}

var container = host.Value.Create(_config.Image, _config.CreateParams, _config.StopOnDispose,
Expand Down Expand Up @@ -168,7 +173,7 @@ public ContainerBuilder ExposePort(int containerPort)

public ContainerBuilder Mount(string fqHostPath, string fqContainerPath, MountType access)
{
var hp = OperatingSystem.IsWindows() && CommandExtensions.IsToolbox()
var hp = Ductus.FluentDocker.Common.OperatingSystem.IsWindows() && CommandExtensions.IsToolbox()
? ((TemplateString) fqHostPath).Rendered.ToMsysPath()
: ((TemplateString) fqHostPath).Rendered;

Expand Down Expand Up @@ -407,7 +412,7 @@ private void AddHooks(IService container)

// docker execute when disposing
if (null != _config.ExecuteOnDisposingArguments && _config.ExecuteOnDisposingArguments.Count > 0)
container.AddHook(ServiceRunningState.Running, service =>
container.AddHook(ServiceRunningState.Removing, service =>
{
var svc = (IContainerService) service;
foreach (var binaryAndArguments in _config.ExecuteOnDisposingArguments)
Expand Down
2 changes: 1 addition & 1 deletion Ductus.FluentDocker/Builders/HostBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal HostBuilder(IBuilder builder) : base(builder)

public override IHostService Build()
{
return _useNative ? new Hosts().Discover().First(x => x.IsNative) : null;
return _useNative ? new Hosts().Discover(_useNative).First(x => x.IsNative) : null;
}

protected override IBuilder InternalCreate()
Expand Down
8 changes: 4 additions & 4 deletions Ductus.FluentDocker/Ductus.FluentDocker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<TargetFrameworks>netstandard1.6;netstandard2.0;net452</TargetFrameworks>
<VersionPrefix>2.2.19</VersionPrefix>
<VersionPrefix>2.3.0</VersionPrefix>
<AssemblyTitle>Ductus.FluentDocker</AssemblyTitle>
<Authors>Mario Toffia</Authors>
<PackageLicenseUrl>https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE</PackageLicenseUrl>
Expand All @@ -18,9 +18,9 @@
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard1.6' ">$(DefineConstants);COREFX</DefineConstants>
<DefineConstants Condition=" '$(TargetFramework)' == 'netstandard2.0' ">$(DefineConstants);COREFX</DefineConstants>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>2.2.19.0</AssemblyVersion>
<FileVersion>2.2.19.0</FileVersion>
<Version>2.2.19</Version>
<AssemblyVersion>2.3.0.0</AssemblyVersion>
<FileVersion>2.3.0.0</FileVersion>
<Version>2.3.0</Version>
<AssemblyOriginatorKeyFile>..\keypair.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
Expand Down
15 changes: 9 additions & 6 deletions Ductus.FluentDocker/Services/Hosts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,18 @@ namespace Ductus.FluentDocker.Services
{
public sealed class Hosts
{
public IList<IHostService> Discover()
public IList<IHostService> Discover(bool preferNative = false)
{
var list = new List<IHostService>();

if (CommandExtensions.IsEmulatedNative() || CommandExtensions.IsNative())
list.Add(new DockerHostService("native", true, false,
null,
Environment.GetEnvironmentVariable(DockerHostService.DockerCertPath)));

if (list.Count > 0 && preferNative)
return list;

if (Machine.IsPresent())
{
var ls = Machine.Ls();
Expand All @@ -24,11 +32,6 @@ public IList<IHostService> Discover()
inspect.Data.AuthConfig.CertDir));
}

if (CommandExtensions.IsEmulatedNative() || CommandExtensions.IsNative())
list.Add(new DockerHostService("native", true, false,
null,
Environment.GetEnvironmentVariable(DockerHostService.DockerCertPath)));

return list;
}
}
Expand Down
22 changes: 20 additions & 2 deletions Ductus.FluentDocker/Services/IContainerService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Ductus.FluentDocker.Model.Common;
using Ductus.FluentDocker.Model.Containers;

namespace Ductus.FluentDocker.Services
{
public interface IContainerService : IService
{
/// <summary>
/// The container id of the running container.
/// </summary>
string Id { get; }

/// <summary>
/// The <see cref="System.Uri" /> to the docker daemon in control of this service.
/// </summary>
DockerUri DockerHost { get; }

/// <summary>
/// Dettermines if this container is based on a windows image or linux image.
/// When set to true the container is stopped automatically on <see cref="IDisposable.Dispose()" />.
/// </summary>
bool StopOnDispose { get; set; }

/// <summary>
/// When set to true the container is removed automaticallyh on <see cref="IDisposable.Dispose()" />.
/// </summary>
bool RemoveOnDispose { get; set; }

/// <summary>
/// Dettermines if this container is based on a windows image or linux image.
/// </summary>
bool IsWindowsContainer { get; }

Expand Down
17 changes: 9 additions & 8 deletions Ductus.FluentDocker/Services/Impl/DockerContainerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public sealed class DockerContainerService : IContainerService
private readonly ServiceHooks _hooks = new ServiceHooks();
private readonly bool _removeMountOnDispose;
private readonly bool _removeNamedMountOnDispose;
private readonly bool _removeOnDispose;
private readonly bool _stopOnDispose;
private Container _containerConfigCache;
private ServiceRunningState _state = ServiceRunningState.Unknown;

Expand All @@ -27,8 +25,8 @@ public DockerContainerService(string name, string id, DockerUri docker, ServiceR
Certificates = certificates;
_removeNamedMountOnDispose = removeNamedMountOnDispose;
_removeMountOnDispose = removeMountOnDispose;
_stopOnDispose = stopOnDispose;
_removeOnDispose = removeOnDispose;
StopOnDispose = stopOnDispose;
RemoveOnDispose = removeOnDispose;

Name = name;
Id = id;
Expand All @@ -38,6 +36,11 @@ public DockerContainerService(string name, string id, DockerUri docker, ServiceR

public string Id { get; }
public DockerUri DockerHost { get; }

public bool StopOnDispose { get; set; }

public bool RemoveOnDispose { get; set; }

public bool IsWindowsContainer { get; }

public Container GetConfiguration(bool fresh = false)
Expand Down Expand Up @@ -78,10 +81,10 @@ public void Dispose()
if (string.IsNullOrEmpty(Id))
return;

if (_stopOnDispose)
if (StopOnDispose)
Stop();

if (_removeOnDispose)
if (RemoveOnDispose)
Remove(true, _removeMountOnDispose);
}

Expand Down Expand Up @@ -146,9 +149,7 @@ public IList<INetworkService> GetNetworks()

var list = new List<INetworkService>();
foreach (var n in config.NetworkSettings.Networks)
{
list.Add(new DockerNetworkService(n.Value.NetworkID, n.Key, DockerHost, Certificates));
}

return list;
}
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#version: 0.0.0.{build}
version: 2.2.19
version: 2.3.0
skip_non_tags: true
image: Visual Studio 2017
configuration: Release
Expand Down

0 comments on commit 56e8bdb

Please sign in to comment.