From 0ae2b5ac8134230fd5f1bcd668647afb5b345baf Mon Sep 17 00:00:00 2001 From: Melanie Thielker Date: Thu, 22 Dec 2016 18:23:04 +0000 Subject: Allow the use of modular configs with Robust as we already can with OpenSim --- OpenSim/Server/Base/ServicesServerBase.cs | 148 +++++++++++++++++++++++++----- bin/Robust.HG.ini.example | 2 +- bin/Robust.ini.example | 2 +- 3 files changed, 125 insertions(+), 27 deletions(-) diff --git a/OpenSim/Server/Base/ServicesServerBase.cs b/OpenSim/Server/Base/ServicesServerBase.cs index a7cffd7..e18594f 100644 --- a/OpenSim/Server/Base/ServicesServerBase.cs +++ b/OpenSim/Server/Base/ServicesServerBase.cs @@ -55,11 +55,7 @@ namespace OpenSim.Server.Base // protected string[] m_Arguments; - public string ConfigDirectory - { - get; - private set; - } + protected string m_configDirectory = "."; // Run flag // @@ -101,31 +97,24 @@ namespace OpenSim.Server.Base logConfig =startupConfig.GetString("logconfig", logConfig); } - // Find out of the file name is a URI and remote load it if possible. - // Load it as a local file otherwise. - Uri configUri; + Config = ReadConfigSource(iniFile); - try + List sources = new List(); + sources.Add(iniFile); + + int sourceIndex = 1; + + while (AddIncludes(Config, sources)) { - if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) && - configUri.Scheme == Uri.UriSchemeHttp) + for ( ; sourceIndex < sources.Count ; ++sourceIndex) { - XmlReader r = XmlReader.Create(iniFile); - Config = new XmlConfigSource(r); - } - else - { - Config = new IniConfigSource(iniFile); + IConfigSource s = ReadConfigSource(sources[sourceIndex]); + Config.Merge(s); } } - catch (Exception e) - { - System.Console.WriteLine("Error reading from config source. {0}", e.Message); - Environment.Exit(1); - } // Merge OpSys env vars - m_log.Info("[CONFIG]: Loading environment variables for Config"); + Console.WriteLine("[CONFIG]: Loading environment variables for Config"); Util.MergeEnvironmentToConfig(Config); // Merge the configuration from the command line into the loaded file @@ -141,7 +130,7 @@ namespace OpenSim.Server.Base if (startupConfig != null) { - ConfigDirectory = startupConfig.GetString("ConfigDirectory", "."); + m_configDirectory = startupConfig.GetString("ConfigDirectory", m_configDirectory); prompt = startupConfig.GetString("Prompt", prompt); } @@ -241,5 +230,114 @@ namespace OpenSim.Server.Base protected virtual void Initialise() { } + + /// + /// Adds the included files as ini configuration files + /// + /// List of URL strings or filename strings + private bool AddIncludes(IConfigSource configSource, List sources) + { + bool sourcesAdded = false; + + //loop over config sources + foreach (IConfig config in configSource.Configs) + { + // Look for Include-* in the key name + string[] keys = config.GetKeys(); + foreach (string k in keys) + { + if (k.StartsWith("Include-")) + { + // read the config file to be included. + string file = config.GetString(k); + if (IsUri(file)) + { + if (!sources.Contains(file)) + { + sourcesAdded = true; + sources.Add(file); + } + } + else + { + string basepath = Path.GetFullPath(m_configDirectory); + // Resolve relative paths with wildcards + string chunkWithoutWildcards = file; + string chunkWithWildcards = string.Empty; + int wildcardIndex = file.IndexOfAny(new char[] { '*', '?' }); + if (wildcardIndex != -1) + { + chunkWithoutWildcards = file.Substring(0, wildcardIndex); + chunkWithWildcards = file.Substring(wildcardIndex); + } + string path = Path.Combine(basepath, chunkWithoutWildcards); + path = Path.GetFullPath(path) + chunkWithWildcards; + string[] paths = Util.Glob(path); + + // If the include path contains no wildcards, then warn the user that it wasn't found. + if (wildcardIndex == -1 && paths.Length == 0) + { + Console.WriteLine("[CONFIG]: Could not find include file {0}", path); + } + else + { + foreach (string p in paths) + { + if (!sources.Contains(p)) + { + sourcesAdded = true; + sources.Add(p); + } + } + } + } + } + } + } + + return sourcesAdded; + } + + /// + /// Check if we can convert the string to a URI + /// + /// String uri to the remote resource + /// true if we can convert the string to a Uri object + bool IsUri(string file) + { + Uri configUri; + + return Uri.TryCreate(file, UriKind.Absolute, + out configUri) && configUri.Scheme == Uri.UriSchemeHttp; + } + + IConfigSource ReadConfigSource(string iniFile) + { + // Find out of the file name is a URI and remote load it if possible. + // Load it as a local file otherwise. + Uri configUri; + IConfigSource s = null; + + try + { + if (Uri.TryCreate(iniFile, UriKind.Absolute, out configUri) && + configUri.Scheme == Uri.UriSchemeHttp) + { + XmlReader r = XmlReader.Create(iniFile); + s = new XmlConfigSource(r); + } + else + { + s = new IniConfigSource(iniFile); + } + } + catch (Exception e) + { + System.Console.WriteLine("Error reading from config source. {0}", e.Message); + Environment.Exit(1); + } + + return s; + } } -} \ No newline at end of file +} diff --git a/bin/Robust.HG.ini.example b/bin/Robust.HG.ini.example index b4e426a..20c0804 100644 --- a/bin/Robust.HG.ini.example +++ b/bin/Robust.HG.ini.example @@ -58,7 +58,7 @@ ; Modular configurations ; Set path to directory for modular ini files... ; The Robust.exe process must have R/W access to the location - ConfigDirectory = "." + ConfigDirectory = "robust-include" ; Console commands can be saved to a file, so the command history persists after a restart. (default is true) ConsoleHistoryFileEnabled = true diff --git a/bin/Robust.ini.example b/bin/Robust.ini.example index 7c9f6da..d33178c 100644 --- a/bin/Robust.ini.example +++ b/bin/Robust.ini.example @@ -50,7 +50,7 @@ ; Modular configurations ; Set path to directory for modular ini files... ; The Robust.exe process must have R/W access to the location - ConfigDirectory = "." + ConfigDirectory = "robust-include" ; Console commands can be saved to a file, so the command history persists after a restart. (default is true) ConsoleHistoryFileEnabled = true -- cgit v1.1