Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Fix parsing (#77)
Browse files Browse the repository at this point in the history
* Revert "Update akka to 1.4.1 rc1 (#73)"

This reverts commit 50c87b9.

* fixed backwards compat
  • Loading branch information
Aaronontheweb authored Mar 5, 2020
1 parent be2a25c commit e429766
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Petabridge.Cmd.Cluster" Version="0.7.1" />
<PackageReference Include="Petabridge.Cmd.Remote" Version="0.7.1" />
<PackageReference Include="Petabridge.Cmd.Cluster" Version="0.8.0-rc1" />
<PackageReference Include="Petabridge.Cmd.Remote" Version="0.8.0-rc1" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion src/Akka.Bootstrap.Docker.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Akka.Configuration;
using Akka.Event;
using Akka.Routing;
using Hocon;
using Petabridge.Cmd.Cluster;
using Petabridge.Cmd.Host;
using Petabridge.Cmd.Remote;
Expand All @@ -20,7 +21,7 @@ static void Main(string[] args)
Environment.SetEnvironmentVariable("CLUSTER_IP", "localhost");
}

var config = ConfigurationFactory.ParseString(File.ReadAllText("app.conf"))
var config = HoconConfigurationFactory.Default()
.BootstrapFromDocker(); // forces all Docker environment variable substitution
var actorSystem = ActorSystem.Create("DockerBootstrap", config);

Expand Down
59 changes: 21 additions & 38 deletions src/Akka.Bootstrap.Docker.Tests/DockerBootstrapSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Akka.Configuration;
using FluentAssertions;
using Xunit;
using Hocon;

namespace Akka.Bootstrap.Docker.Tests
{
Expand All @@ -20,20 +19,18 @@ namespace Akka.Bootstrap.Docker.Tests
public class DockerBootstrapSpecs
{
[Theory]
[InlineData("\"akka.tcp://MySys@localhost:9140\"")]
[InlineData("\"akka.tcp://MySys@localhost:9140\", \"akka.tcp://MySys@localhost:9141\"")]
[InlineData("\"akka.tcp://MySys@localhost:9140\", \"akka.tcp://MySys@localhost:9141\", \"akka.tcp://MySys@localhost:9142\"")]
[InlineData("akka.tcp://MySys@localhost:9140")]
[InlineData("akka.tcp://MySys@localhost:9140, akka.tcp://MySys@localhost:9141")]
[InlineData("akka.tcp://MySys@localhost:9140, akka.tcp://MySys@localhost:9141, akka.tcp://MySys@localhost:9142")]
public void ShouldStartIfValidSeedNodesIfSupplied(string seedNodes)
{
try
{
Environment.SetEnvironmentVariable("CLUSTER_SEEDS", seedNodes, EnvironmentVariableTarget.Process);
var myConfig = ConfigurationFactory.Empty.BootstrapFromDocker();
Config expected = $"array=[{seedNodes}]";

myConfig.HasPath("akka.cluster.seed-nodes").Should().BeTrue();
var seeds = myConfig.GetStringList("akka.cluster.seed-nodes");
seeds.Should().BeEquivalentTo(expected.GetStringList("array"));
var seeds = myConfig.GetStringList("akka.cluster.seed-nodes").Select(x => x.Trim());
seeds.Should().BeEquivalentTo(seedNodes.Split(",").Select(x => x.Trim()));
}
finally
{
Expand Down Expand Up @@ -90,37 +87,23 @@ public void ShouldStartNormallyIfNotEnvironmentVariablesAreSupplied()
[Fact]
public void ShouldStartIfValidAkkaConfigurationSuppliedByEnvironmentVariables()
{
try
{
Environment.SetEnvironmentVariable("AKKA__COORDINATED_SHUTDOWN__EXIT_CLR", "on", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__ACTOR__PROVIDER", "cluster", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__HOSTNAME", "127.0.0.1", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PUBLIC_HOSTNAME", "example.local", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PORT", "2559", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__0", "demo", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__1", "test", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__2", "backup", EnvironmentVariableTarget.Process);

var myConfig = ConfigurationFactory.Empty.BootstrapFromDocker();
Environment.SetEnvironmentVariable("AKKA__COORDINATED_SHUTDOWN__EXIT_CLR", "on", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__ACTOR__PROVIDER", "cluster", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__HOSTNAME", "127.0.0.1", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PUBLIC_HOSTNAME", "example.local", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PORT", "2559", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__0", "demo", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__1", "test", EnvironmentVariableTarget.Process);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__2", "backup", EnvironmentVariableTarget.Process);

myConfig.GetBoolean("akka.coordinated-shutdown.exit-clr").Should().BeTrue();
myConfig.GetString("akka.actor.provider").Should().Be("cluster");
myConfig.GetString("akka.remote.dot-netty.tcp.hostname").Should().Be("127.0.0.1");
myConfig.GetString("akka.remote.dot-netty.tcp.public-hostname").Should().Be("example.local");
myConfig.GetInt("akka.remote.dot-netty.tcp.port").Should().Be(2559);
myConfig.GetStringList("akka.cluster.roles").Should().BeEquivalentTo(new[] { "demo", "test", "backup" });
}
finally
{
Environment.SetEnvironmentVariable("AKKA__COORDINATED_SHUTDOWN__EXIT_CLR", null);
Environment.SetEnvironmentVariable("AKKA__ACTOR__PROVIDER", null);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__HOSTNAME", null);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PUBLIC_HOSTNAME", null);
Environment.SetEnvironmentVariable("AKKA__REMOTE__DOT_NETTY__TCP__PORT", null);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__0", null);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__1", null);
Environment.SetEnvironmentVariable("AKKA__CLUSTER__ROLES__2", null);
}
var myConfig = ConfigurationFactory.Empty.BootstrapFromDocker();

myConfig.GetBoolean("akka.coordinated-shutdown.exit-clr").Should().BeTrue();
myConfig.GetString("akka.actor.provider").Should().Be("cluster");
myConfig.GetString("akka.remote.dot-netty.tcp.hostname").Should().Be("127.0.0.1");
myConfig.GetString("akka.remote.dot-netty.tcp.public-hostname").Should().Be("example.local");
myConfig.GetInt("akka.remote.dot-netty.tcp.port").Should().Be(2559);
myConfig.GetStringList("akka.cluster.roles").Should().BeEquivalentTo(new [] { "demo", "test", "backup" });
}
}
}
8 changes: 0 additions & 8 deletions src/Akka.Bootstrap.Docker/Akka.Bootstrap.Docker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@
<DocumentationFile>bin\Release\netstandard1.6\Akka.Bootstrap.Docker.xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
<None Remove="Docker.Environment.conf" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Docker.Environment.conf" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Akka" Version="$(AkkaVersion)" />
Expand Down
10 changes: 0 additions & 10 deletions src/Akka.Bootstrap.Docker/AssemblyMarker.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/Akka.Bootstrap.Docker/Docker.Environment.conf

This file was deleted.

53 changes: 34 additions & 19 deletions src/Akka.Bootstrap.Docker/EnvironmentVariableConfigLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using System.Linq;
using System.Text;
using Akka.Configuration;
using System.Net;
using Hocon;

namespace Akka.Bootstrap.Docker
Expand All @@ -21,8 +20,6 @@ namespace Akka.Bootstrap.Docker
/// </summary>
public static class EnvironmentVariableConfigLoader
{
private const string DefaultConfigResource = "Akka.Bootstrap.Docker.Docker.Environment.conf";

private static IEnumerable<EnvironmentVariableConfigEntrySource> GetEnvironmentVariables()
{
// Currently, exclude environment variables that do not start with "AKKA__"
Expand All @@ -31,11 +28,34 @@ private static IEnumerable<EnvironmentVariableConfigEntrySource> GetEnvironmentV
// to other non "AKKA__" variables.
bool UseAllEnvironmentVariables = false;

// List of environment variable mappings that do not follow the "AKKA__" convention.
// We are currently supporting these out of convenience, and may choose to officially
// create a set of aliases in the future. Doing so would allow envvar configuration
// to be less verbose but might perpetuate confusion as to source of truth for keys.
Dictionary<string, string> ExistingMappings = new Dictionary<string, string>()
{
{ "CLUSTER_IP", "akka.remote.dot-netty.tcp.public-hostname" },
{ "CLUSTER_PORT", "akka.remote.dot-netty.tcp.port" },
{ "CLUSTER_SEEDS", "akka.cluster.seed-nodes" }
};

// Identify environment variable mappings that are expected to be lists
string[] ExistingMappingLists = new string[] { "CLUSTER_SEEDS" };

foreach (DictionaryEntry set in Environment.GetEnvironmentVariables(EnvironmentVariableTarget.Process))
{
var key = set.Key.ToString();
var isList = false;

if (ExistingMappings.TryGetValue(key, out var mappedKey))
{
isList = ExistingMappingLists.Contains(key);

// Format the key to appear as if it were an environment variable
// in the "AKKA__" format
key = mappedKey.ToUpper().Replace(".", "__").Replace("-", "_");
}

if (!UseAllEnvironmentVariables)
if (!key.StartsWith("AKKA__", StringComparison.OrdinalIgnoreCase))
continue;
Expand Down Expand Up @@ -74,36 +94,31 @@ private static IEnumerable<EnvironmentVariableConfigEntrySource> GetEnvironmentV
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static Config FromEnvironment(this Config _)
public static Config FromEnvironment(this Config input)
{
var environmentConfig = HoconConfigurationFactory.FromResource<AssemblyMarker>(DefaultConfigResource);
var defaultValues = new StringBuilder();
defaultValues.AppendLine($@"
akka.remote.dot-netty.tcp {{
hostname=0.0.0.0
public-hostname={Dns.GetHostName()}
}}");
if (environmentConfig.HasPath("environment.seed-nodes"))
defaultValues.AppendLine($"akka.cluster.seed-nodes=[{environmentConfig.GetString("environment.seed-nodes")}]");

var entries = GetEnvironmentVariables()
.OrderByDescending(x => x.Depth)
.GroupBy(x => x.Key);

StringBuilder sb = new StringBuilder();
foreach (var set in entries)
{
defaultValues.Append($"{set.Key}=");
sb.Append($"{set.Key}=");
if (set.Count() > 1)
{
defaultValues.AppendLine($"[\n\t\"{String.Join("\",\n\t\"", set.OrderBy(y => y.Index).Select(y => y.Value.Trim()))}\"]");
{
sb.AppendLine($"[\n\t\"{String.Join("\",\n\t\"", set.OrderBy(y => y.Index).Select(y => y.Value.Trim()))}\"]");
}
else
{
defaultValues.AppendLine($"{set.First().Value}");
sb.AppendLine($"{set.First().Value}");
}
}

return environmentConfig.WithFallback(HoconConfigurationFactory.ParseString(defaultValues.ToString()));
if(sb.Length == 0)
return Config.Empty;
var config = ConfigurationFactory.ParseString(sb.ToString());

return config;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.Linq;
using Akka.Configuration;
using Hocon;
using FluentAssertions;
using Xunit;

Expand Down
2 changes: 1 addition & 1 deletion src/common.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Copyright>Copyright © 2015-2019 Petabridge®</Copyright>
<Copyright>Copyright © 2015-2020 Petabridge®</Copyright>
<Authors>Petabridge</Authors>
<VersionPrefix>0.4.0</VersionPrefix>
<PackageReleaseNotes>Upgraded to Akka.NET v1.4.1-rc1 and used native environment variable substitution from HOCON</PackageReleaseNotes>
Expand Down

0 comments on commit e429766

Please sign in to comment.