This repository has been archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamically load environment variables when using BootstrapFromDocker (…
…#60) * Remove faulty test. This test implies that you can send through '[]' but no other tests validate that behavior. Additionally, the current bootstrap process will create a value for this hocon key as '[[]]' which is incorrect. * Add environment variable configuration loader * Use environment variable configuration loader in docker bootstrap * Fix formatting issue when writing entries to hocon * Add unit test to validate envvar loading * Move default hostname into fallback Some cases may require us to specify the hostname on startup (ie. multiple NICs). And, the previous code would have blocked environment variables from overriding the value. So, moving it lower into the fallback block. Co-authored-by: Aaron Stannard <aaron@petabridge.com>
- Loading branch information
1 parent
98c7318
commit e778863
Showing
5 changed files
with
285 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="DockerBootstrap.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2018 - 2018 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Akka.Bootstrap.Docker | ||
{ | ||
/// <summary> | ||
/// Defines a source of configuration to be evaluated and applied | ||
/// against a HOCON key/value pair. | ||
/// </summary> | ||
public abstract class ConfigEntrySource | ||
{ | ||
/// <summary> | ||
/// Override to describe the implementation type | ||
/// </summary> | ||
/// <value></value> | ||
public abstract string SourceName { get; } | ||
|
||
/// <summary> | ||
/// The series of key nodes which make up the path | ||
/// </summary> | ||
/// <value></value> | ||
public string[] Nodes { get; } | ||
/// <summary> | ||
/// The full HOCON path for the given value (Derived from `Nodes`) | ||
/// </summary> | ||
/// <value></value> | ||
public string Key { get; } | ||
/// <summary> | ||
/// The value for this given HOCON node | ||
/// </summary> | ||
/// <value></value> | ||
public string Value { get; } | ||
/// <summary> | ||
/// Identifies if the source is a series of values | ||
/// </summary> | ||
public int Index { get; } | ||
/// <summary> | ||
/// Returns the depth of the hocon key (ie. number of nodes on the key) | ||
/// </summary> | ||
public int Depth => Nodes.Length; | ||
|
||
/// <summary> | ||
/// Creates a config entry source from a set of nodes, value and optional index | ||
/// </summary> | ||
/// <param name="nodes">Set of nodes which comprise the path</param> | ||
/// <param name="value">Value stored in this entry</param> | ||
/// <param name="index">Provided index to identify a set of hocon values stored | ||
/// against a single key</param> | ||
protected ConfigEntrySource(string[] nodes, string value, int index = 0) | ||
{ | ||
Nodes = nodes; | ||
Key = String.Join(".", Nodes); | ||
Index = index; | ||
Value = value; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/Akka.Bootstrap.Docker/EnvironmentVariableConfigEntrySource.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="DockerBootstrap.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2018 - 2018 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Akka.Bootstrap.Docker | ||
{ | ||
/// <summary> | ||
/// Configuration entry source from Environment Variables | ||
/// </summary> | ||
public class EnvironmentVariableConfigEntrySource : ConfigEntrySource | ||
{ | ||
public override string SourceName { get; }= "environment-variable"; | ||
|
||
/// <summary> | ||
/// Creates an instance of EnvironmentVariableConfigEntrySource | ||
/// from a raw environment variable key/value pair | ||
/// </summary> | ||
/// <param name="key"></param> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
/// <remarks> | ||
/// Will perform analysis on the key in the following way: | ||
/// - Instances of '__' are treated as path delimiterrs | ||
/// - Instances of '_' are treated as substitutions of '-' | ||
/// - Terminal nodes which appear to be integers will be taken as indexes | ||
/// to support multi-value keys | ||
/// </remarks> | ||
public static EnvironmentVariableConfigEntrySource Create(string key, string value) | ||
{ | ||
var nodes = key.Split(new [] { "__" }, StringSplitOptions.None) | ||
.Select(x => x.Replace("_", "-")) | ||
.ToArray(); | ||
var index = 0; | ||
var maybeIndex = nodes.Last(); | ||
if (Regex.IsMatch(maybeIndex, @"^\d+$")) { | ||
nodes = nodes.Take(nodes.Length - 1).ToArray(); | ||
index = int.Parse(maybeIndex); | ||
} | ||
return new EnvironmentVariableConfigEntrySource(nodes, value, index); | ||
} | ||
|
||
EnvironmentVariableConfigEntrySource(string[] nodes, string value, int index = 0) | ||
:base(nodes, value, index) | ||
{ | ||
|
||
} | ||
} | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
src/Akka.Bootstrap.Docker/EnvironmentVariableConfigLoader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="DockerBootstrap.cs" company="Petabridge, LLC"> | ||
// Copyright (C) 2018 - 2018 Petabridge, LLC <https://petabridge.com> | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using Akka.Configuration; | ||
|
||
namespace Akka.Bootstrap.Docker | ||
{ | ||
/// <summary> | ||
/// Extension and helper methods to support loading a Config instance from | ||
/// the environment variables of the current process. | ||
/// </summary> | ||
public static class EnvironmentVariableConfigLoader | ||
{ | ||
private static IEnumerable<EnvironmentVariableConfigEntrySource> GetEnvironmentVariables() | ||
{ | ||
// Currently, exclude environment variables that do not start with "AKKA__" | ||
// We can implement variable substitution at a later stage which would allow | ||
// us to do something like: akka.some-setting=${?HOSTNAME} which can refer | ||
// 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; | ||
|
||
// Skip empty environment variables | ||
var value = set.Value?.ToString()?.Trim(); | ||
if (string.IsNullOrEmpty(value)) | ||
continue; | ||
|
||
// Ideally, lists should be passed through in an indexed format. | ||
// However, we can allow for lists to be passed in as array format. | ||
// Otherwise, we must format the string as an array. | ||
if (isList) | ||
{ | ||
if (value.First() != '[' || value.Last() != ']') | ||
{ | ||
var values = value.Split(',').Select(x => x.Trim()); | ||
value = $"[\" {String.Join("\",\"", values)} \"]"; | ||
} | ||
else if (String.IsNullOrEmpty(value.Substring(1, value.Length - 2).Trim())) | ||
{ | ||
value = "[]"; | ||
} | ||
} | ||
|
||
yield return EnvironmentVariableConfigEntrySource.Create( | ||
key.ToLower().ToString(), | ||
value | ||
); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Load AKKA configuration from the environment variables that are | ||
/// accessible from the current process. | ||
/// </summary> | ||
/// <param name="input"></param> | ||
/// <returns></returns> | ||
public static Config FromEnvironment(this Config input) | ||
{ | ||
var entries = GetEnvironmentVariables() | ||
.OrderByDescending(x => x.Depth) | ||
.GroupBy(x => x.Key); | ||
|
||
StringBuilder sb = new StringBuilder(); | ||
foreach (var set in entries) | ||
{ | ||
sb.Append($"{set.Key}="); | ||
if (set.Count() > 1) | ||
{ | ||
sb.AppendLine($"[\n\t\"{String.Join("\",\n\t\"", set.OrderBy(y => y.Index).Select(y => y.Value.Trim()))}\"]"); | ||
} | ||
else | ||
{ | ||
sb.AppendLine($"{set.First().Value}"); | ||
} | ||
} | ||
|
||
var config = ConfigurationFactory.ParseString(sb.ToString()); | ||
|
||
return config; | ||
} | ||
} | ||
|
||
} |