public void String() { var parser = new EnvironmentParser(); Assert.Null(parser.Get(Var("DOESNT_EXIST"), null)); Assert.Equal("hello", parser.Get(Var("DOESNT_EXIST"), "hello")); Assert.Throws <KeyNotFoundException>(() => parser.Get(Var("DOESNT_EXIST"), null, required: true)); Assert.Throws <KeyNotFoundException>(() => parser.Get(Var("DOESNT_EXIST"), "hello", required: true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "test"); Assert.Equal("test", parser.Get(Var("EXISTS"), null)); }
public void Double() { var parser = new EnvironmentParser(); Assert.Equal(123.4, parser.Get(Var("DOESNT_EXIST"), 123.4)); Assert.Throws <KeyNotFoundException>(() => parser.Get(Var("DOESNT_EXIST"), 123.4, required: true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "123.4"); Assert.Equal(123.4, parser.Get(Var("EXISTS"), 567.8)); Environment.SetEnvironmentVariable(Var("EXISTS"), "bad"); Assert.Equal(567.8, parser.Get(Var("EXISTS"), 567.8)); }
public void Integer() { var parser = new EnvironmentParser(); Assert.Equal(55, parser.Get(Var("DOESNT_EXIST"), 55)); Assert.Throws <KeyNotFoundException>(() => parser.Get(Var("DOESNT_EXIST"), 55, required: true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "55"); Assert.Equal(55, parser.Get(Var("EXISTS"), 77)); Environment.SetEnvironmentVariable(Var("EXISTS"), "bad"); Assert.Equal(77, parser.Get(Var("EXISTS"), 77)); }
public void TimeSpan() { var parser = new EnvironmentParser(); Assert.Equal(System.TimeSpan.FromSeconds(5), parser.Get(Var("DOESNT_EXIST"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "10"); Assert.Equal(System.TimeSpan.FromSeconds(10), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20s"); Assert.Equal(System.TimeSpan.FromSeconds(20), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20ms"); Assert.Equal(System.TimeSpan.FromMilliseconds(20), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20m"); Assert.Equal(System.TimeSpan.FromMinutes(20), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20h"); Assert.Equal(System.TimeSpan.FromHours(20), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20d"); Assert.Equal(System.TimeSpan.FromDays(20), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); Environment.SetEnvironmentVariable(Var("EXISTS"), "20.5s"); Assert.Equal(System.TimeSpan.FromSeconds(20.5), parser.Get(Var("EXISTS"), System.TimeSpan.FromSeconds(5))); }
public void Enum() { var parser = new EnvironmentParser(); Assert.Equal(MyEnum.Zero, parser.Get(Var("DOESNT_EXIST"), MyEnum.Zero)); Environment.SetEnvironmentVariable(Var("EXISTS"), "one"); Assert.Equal(MyEnum.One, parser.Get(Var("EXISTS"), MyEnum.Zero)); Environment.SetEnvironmentVariable(Var("EXISTS"), "TWO"); Assert.Equal(MyEnum.Two, parser.Get(Var("EXISTS"), MyEnum.Zero)); Environment.SetEnvironmentVariable(Var("EXISTS"), "ThrEE"); Assert.Equal(MyEnum.Three, parser.Get(Var("EXISTS"), MyEnum.Zero)); }
public void CustomSource() { var source = new EnvironmentParser.VariableSource( variable => { Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(variable), nameof(variable)); if (variable == "NOT-FOUND") { return(null); } return($"{variable}-value"); }); var parser = new EnvironmentParser(source: source); Assert.Null(parser.Get("NOT-FOUND", (string)null)); Assert.Equal("test-value", parser.Get("test", (string)null)); }
public void Bool() { var parser = new EnvironmentParser(); Assert.True(parser.Get(Var("DOESNT_EXIST"), true)); Assert.False(parser.Get(Var("DOESNT_EXIST"), false)); Assert.Throws <KeyNotFoundException>(() => parser.Get(Var("DOESNT_EXIST"), true, required: true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "true"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "TRUE"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "on"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "ON"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "yes"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "YES"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "1"); Assert.True(parser.Get(Var("EXISTS"), false)); Environment.SetEnvironmentVariable(Var("EXISTS"), "false"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "FALSE"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "off"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "OFF"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "no"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "NO"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "0"); Assert.False(parser.Get(Var("EXISTS"), true)); Environment.SetEnvironmentVariable(Var("EXISTS"), "bad"); Assert.True(parser.Get(Var("EXISTS"), true)); Assert.False(parser.Get(Var("EXISTS"), false)); }
/// <summary> /// Application entry point. /// </summary> /// <param name="args">Command line arguments.</param> public static async Task Main(string[] args) { LogManager.Default.SetLogLevel(Environment.GetEnvironmentVariable("LOG_LEVEL")); log = LogManager.Default.GetLogger(typeof(Program)); log.LogInfo(() => $"Starting [{serviceName}]"); log.LogInfo(() => $"LOG_LEVEL={LogManager.Default.LogLevel.ToString().ToUpper()}"); // Parse the environment variable settings. var environment = new EnvironmentParser(log); pollInterval = environment.Get("POLL_INTERVAL", TimeSpan.FromSeconds(5), validator: v => v > TimeSpan.Zero); verifyInterval = environment.Get("VERIFY_INTERVAL", TimeSpan.FromMinutes(5), validator: v => v > TimeSpan.Zero); // Create process terminator to handle process termination signals. terminator = new ProcessTerminator(log); try { // Establish the hive connections. if (NeonHelper.IsDevWorkstation) { hive = HiveHelper.OpenHiveRemote(); // For testing and development, we're going to write a test // hosts file to [%NF_TEMP\neon-dns-hosts.txt] so we can see // what's happening outside of a hive. powerDnsHostsPath = Environment.ExpandEnvironmentVariables("%NF_TEMP%\\neon-dns-hosts.txt"); File.WriteAllText(powerDnsHostsPath, $@"# PowerDNS Recursor authoritatively answers for [*.HIVENAME.nhive.io] hostnames. # on the local node using these mappings. 10.0.0.30 {HiveHelper.Hive.Definition.Hostnames.Consul} # Internal hive Vault mappings: 10.0.0.30 {HiveHelper.Hive.Definition.Hostnames.Vault} 10.0.0.30 {HiveHelper.Hive.FirstManager.Name}.{HiveHelper.Hive.Definition.Hostnames.Vault} # Internal hive registry cache related mappings: 10.0.0.30 {HiveHelper.Hive.FirstManager.Name}.{HiveHelper.Hive.Definition.Hostnames.RegistryCache} # Internal hive log pipeline related mappings: 10.0.0.30 {HiveHelper.Hive.Definition.Hostnames.LogEsData} "); // We're also going to create a temporary folder for the reload signal. reloadSignalPath = Environment.ExpandEnvironmentVariables("%NF_TEMP%\\neon-dns\\reload"); Directory.CreateDirectory(Path.GetDirectoryName(reloadSignalPath)); } else { hive = HiveHelper.OpenHive(); } // Ensure that we're running on a manager node. This is required because // we need to be able to update the [/etc/powerdns/hosts] files deployed // on the managers. var nodeRole = Environment.GetEnvironmentVariable("NEON_NODE_ROLE"); if (string.IsNullOrEmpty(nodeRole)) { log.LogCritical(() => "Service does not appear to be running on a neonHIVE."); Program.Exit(1, immediate: true); } if (!string.Equals(nodeRole, NodeRole.Manager, StringComparison.OrdinalIgnoreCase)) { log.LogCritical(() => $"[neon-dns] service is running on a [{nodeRole}] hive node. Only [{NodeRole.Manager}] nodes are supported."); Program.Exit(1, immediate: true); } // Ensure that the [/etc/powerdns/hosts] file was mapped into the container. if (!File.Exists(powerDnsHostsPath)) { log.LogCritical(() => $"[neon-dns] service cannot locate [{powerDnsHostsPath}] on the host manager. Was this mounted to the container as read/write?"); Program.Exit(1, immediate: true); } // Open Consul and then start the main service task. log.LogDebug(() => $"Connecting: Consul"); using (consul = HiveHelper.OpenConsul()) { await RunAsync(); } } catch (Exception e) { log.LogCritical(e); Program.Exit(1); return; } finally { HiveHelper.CloseHive(); terminator.ReadyToExit(); } Program.Exit(0); return; }
/// <summary> /// Application entry point. /// </summary> /// <param name="args">Command line arguments.</param> public static async Task Main(string[] args) { LogManager.Default.SetLogLevel(Environment.GetEnvironmentVariable("LOG_LEVEL")); log = LogManager.Default.GetLogger(typeof(Program)); log.LogInfo(() => $"Starting [{serviceName}]"); log.LogInfo(() => $"LOG_LEVEL={LogManager.Default.LogLevel.ToString().ToUpper()}"); // Parse the environment variable settings. var environment = new EnvironmentParser(log); nameservers = environment.Get("NAMESERVERS", "8.8.8.8,8.8.4.4").Split(','); pingTimeout = environment.Get("PING_TIMEOUT", TimeSpan.FromSeconds(1.5), validator: v => v > TimeSpan.Zero); pollInterval = environment.Get("POLL_INTERVAL", TimeSpan.FromSeconds(5), validator: v => v > TimeSpan.Zero); warnInterval = environment.Get("WARN_INTERVAL", TimeSpan.FromMinutes(5), validator: v => v > TimeSpan.Zero); // Create a timer so we'll avoid spamming the logs with warnings. warnTimer = new PolledTimer(warnInterval, autoReset: true); warnTimer.FireNow(); // Set so that the first warnings detected will be reported immediately. // Create the object that will actually perform the hostname lookups // and health pings. This object caches things to improve performance. healthResolver = new HealthResolver(nameservers); // Create process terminator to handle termination signals. terminator = new ProcessTerminator(log); try { // Establish the hive connections. if (NeonHelper.IsDevWorkstation) { hive = HiveHelper.OpenHiveRemote(); } else { hive = HiveHelper.OpenHive(); } // Open Consul and then start the main service task. log.LogDebug(() => $"Connecting: Consul"); using (consul = HiveHelper.OpenConsul()) { await RunAsync(); } } catch (Exception e) { log.LogCritical(e); Program.Exit(1); return; } finally { HiveHelper.CloseHive(); terminator.ReadyToExit(); } Program.Exit(0); return; }