diff --git a/Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj b/Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj index 47b8fc6..7f10584 100644 --- a/Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj +++ b/Ductus.FluentDocker.MsTest/Ductus.FluentDocker.MsTest.csproj @@ -2,7 +2,7 @@ netcoreapp1.1;netcoreapp2.0;net452 - 2.3.1 + 2.3.2 Ductus.FluentDocker.MsTest Mario Toffia https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE @@ -19,7 +19,7 @@ Documentation: https://github.com/mariotoffia/FluentDocker © 2016, 2017, 2018 Mario Toffia $(DefineConstants);COREFX True - 2.3.1 + 2.3.2 ..\keypair.snk true true diff --git a/Ductus.FluentDocker/Ductus.FluentDocker.csproj b/Ductus.FluentDocker/Ductus.FluentDocker.csproj index abbfaeb..fdc7042 100644 --- a/Ductus.FluentDocker/Ductus.FluentDocker.csproj +++ b/Ductus.FluentDocker/Ductus.FluentDocker.csproj @@ -1,7 +1,7 @@  netstandard1.6;netstandard2.0;net452 - 2.3.1 + 2.3.2 Ductus.FluentDocker Mario Toffia https://github.com/mariotoffia/FluentDocker/blob/master/LICENSE @@ -16,9 +16,9 @@ $(DefineConstants);COREFX $(DefineConstants);COREFX True - 2.3.1.0 - 2.3.1.0 - 2.3.1 + 2.3.2.0 + 2.3.2.0 + 2.3.2 ..\keypair.snk true true diff --git a/Ductus.FluentDocker/Extensions/ConversionExtension.cs b/Ductus.FluentDocker/Extensions/ConversionExtension.cs new file mode 100644 index 0000000..8410d68 --- /dev/null +++ b/Ductus.FluentDocker/Extensions/ConversionExtension.cs @@ -0,0 +1,47 @@ +using System.Linq; + +namespace Ductus.FluentDocker.Extensions +{ + public static class ConversionExtension + { + /// + /// Converts a numeric expression combined with an optional suffix to denote + /// b, k, m, g. + /// + /// The value to be parsed. + /// An optional custom array of suffix. But has to be among b, k, m, g. + /// If successful the number, otherwise is returned. + public static long Convert(this string value, params string[] unit) + { + if (null == unit || 0 == unit.Length) unit = new[] {"b", "k", "m", "g"}; + + if (string.IsNullOrWhiteSpace(value)) return long.MinValue; + + var num = value.Substring(0, value.Length - 2); + var u = value.Substring(value.Length - 2, 1).ToLower(); + + if (char.IsDigit(u[0])) return !long.TryParse(value, out var n) ? long.MinValue : n; + + if (!unit.Contains(u)) return long.MinValue; + + if (!long.TryParse(num, out var val)) + return long.MinValue; + + if (u == "b") return val; + + switch (u) + { + case "b": + return val; + case "k": + return val * 1024; + case "m": + return val * 1024 * 1024; + case "g": + return val * 1024 * 1024 * 1024; + default: + return long.MinValue; + } + } + } +} \ No newline at end of file diff --git a/Ductus.FluentDocker/Extensions/ModelExtensions.cs b/Ductus.FluentDocker/Extensions/ModelExtensions.cs index 63f1803..21359a6 100644 --- a/Ductus.FluentDocker/Extensions/ModelExtensions.cs +++ b/Ductus.FluentDocker/Extensions/ModelExtensions.cs @@ -11,50 +11,50 @@ namespace Ductus.FluentDocker.Extensions { public static class ModelExtensions { - public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string value) + public static StringBuilder SizeOptionIfValid(this StringBuilder sb, string option, string value, + long maxSize = long.MaxValue) { if (!string.IsNullOrEmpty(value)) { - sb.Append($" {option}{value}"); + var num = value.Convert(); + if (num == long.MinValue) + return sb; + + if (num <= maxSize) + sb.Append($" {option}{value}"); } return sb; } + + public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string value) + { + if (!string.IsNullOrEmpty(value)) sb.Append($" {option}{value}"); + + return sb; + } + public static StringBuilder OptionIfExists(this StringBuilder sb, string option, bool enabled) { - if (enabled) - { - sb.Append($" {option}"); - } + if (enabled) sb.Append($" {option}"); return sb; } + public static StringBuilder OptionIfExists(this StringBuilder sb, string option, string[] values) { - if (null == values || 0 == values.Length) - { - return sb; - } + if (null == values || 0 == values.Length) return sb; - foreach (var value in values) - { - sb.Append($" {option}{value}"); - } + foreach (var value in values) sb.Append($" {option}{value}"); return sb; } - public static StringBuilder OptionIfExists(this StringBuilder sb, string option, IDictionary values) + public static StringBuilder OptionIfExists(this StringBuilder sb, string option, IDictionary values) { - if (null == values || 0 == values.Count) - { - return sb; - } + if (null == values || 0 == values.Count) return sb; - foreach (var value in values) - { - sb.Append($" {option}{value.Key}={value.Value}"); - } + foreach (var value in values) sb.Append($" {option}{value.Key}={value.Value}"); return sb; } @@ -92,41 +92,20 @@ public static TemplateString AsTemplate(this string str) public static ServiceRunningState ToServiceState(this ContainerState state) { - if (null == state) - { - return ServiceRunningState.Unknown; - } + if (null == state) return ServiceRunningState.Unknown; - if (state.Running) - { - return ServiceRunningState.Running; - } + if (state.Running) return ServiceRunningState.Running; - if (state.Dead) - { - return ServiceRunningState.Stopped; - } + if (state.Dead) return ServiceRunningState.Stopped; - if (state.Restarting) - { - return ServiceRunningState.Starting; - } + if (state.Restarting) return ServiceRunningState.Starting; - if (state.Paused) - { - return ServiceRunningState.Paused; - } + if (state.Paused) return ServiceRunningState.Paused; var status = state.Status?.ToLower() ?? string.Empty; - if (status == "created") - { - return ServiceRunningState.Stopped; - } + if (status == "created") return ServiceRunningState.Stopped; - if (status == "exited") - { - return ServiceRunningState.Stopped; - } + if (status == "exited") return ServiceRunningState.Stopped; return ServiceRunningState.Unknown; } @@ -146,15 +125,12 @@ public static string ToDocker(this MountType access) public static string[] ArrayAddDistinct(this string[] arr, params string[] values) { - return ArrayAdd(arr,values).Distinct().ToArray(); + return ArrayAdd(arr, values).Distinct().ToArray(); } public static string[] ArrayAdd(this string[] arr, params string[] values) { - if (null == values || 0 == values.Length) - { - return arr; - } + if (null == values || 0 == values.Length) return arr; if (null == arr) { diff --git a/Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs b/Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs index 6588dea..e238255 100644 --- a/Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs +++ b/Ductus.FluentDocker/Model/Containers/ContainerCreateParams.cs @@ -193,8 +193,48 @@ public sealed class ContainerCreateParams /// public string Network { get; set; } + /// + /// Restart policy for this container. + /// + /// + /// --restart + /// public RestartPolicy RestartPolicy { get; set; } + /// + /// The maximum amount of memory the container can use. If you set this option, the minimum allowed value is 4m (4 + /// megabyte). + /// + /// + /// -m, --memory= + /// + public string Memory { get; set; } + + /// + /// The amount of memory this container is allowed to swap to disk. + /// + /// + /// --memory-swap * + /// --memory-swap is a modifier flag that only has meaning if --memory is also set. Using swap allows the container to + /// write excess memory requirements to disk when the container has exhausted all the RAM that is available to it. There + /// is a performance penalty for applications that swap memory to disk often. + /// Its setting can have complicated effects: + /// If --memory-swap is set to a positive integer, then both --memory and --memory-swap must be set. --memory-swap + /// represents the total amount of memory and swap that can be used, and --memory controls the amount used by non-swap + /// memory. So if --memory="300m" and --memory-swap="1g", the container can use 300m of memory and 700m (1g - 300m) swap. + /// If --memory-swap is set to 0, the setting is ignored, and the value is treated as unset. + /// If --memory-swap is set to the same value as --memory, and --memory is set to a positive integer, the container does + /// not have access to swap. See Prevent a container from using swap. + /// If --memory-swap is unset, and --memory is set, the container can use twice as much swap as the --memory setting, if + /// the host container has swap memory configured. For instance, if --memory="300m" and --memory-swap is not set, the + /// container can use 300m of memory and 600m of swap. + /// If --memory-swap is explicitly set to -1, the container is allowed to use unlimited swap, up to the amount available + /// on the host system. + /// Inside the container, tools like free report the host’s available swap, not what’s available inside the container. + /// Don’t rely on the output of free or similar tools to determine whether swap is present. + /// + public string MemorySwap { get; set; }// TODO: https://docs.docker.com/config/containers/resource_constraints/#understand-the-risks-of-running-out-of-memory + /// /// Renders the argument string from this instance. /// @@ -258,6 +298,8 @@ public override string ToString() break; } + sb.SizeOptionIfValid("--memory=", Memory, 4 * 1024 * 1024 /*4m*/); + return sb.ToString(); } } @@ -309,6 +351,5 @@ public override string ToString() --tmpfs=[] Mount a tmpfs directory --ulimit=[] Ulimit options --uts UTS namespace to use - --network Connect a container to a network */ } \ No newline at end of file diff --git a/appveyor.yml b/appveyor.yml index f2a0a11..bf429eb 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,5 +1,5 @@ #version: 0.0.0.{build} -version: 2.3.1 +version: 2.3.2 skip_non_tags: true image: Visual Studio 2017 configuration: Release