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
Dynamically load environment variables when using BootstrapFromDocker #60
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3b53861
Remove faulty test.
455e6cf
Add environment variable configuration loader
4e138dc
Use environment variable configuration loader in docker bootstrap
4cf1559
Fix formatting issue when writing entries to hocon
44e1971
Add unit test to validate envvar loading
0549a44
Move default hostname into fallback
23ddbe8
Merge branch 'dev' into feat/envvar-loader
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM