diff --git a/src/client/Spark.Console/src/Commands/ConfigCommand.cs b/src/client/Spark.Console/src/Commands/ConfigCommand.cs index f5b16dd2a..f57a4f864 100644 --- a/src/client/Spark.Console/src/Commands/ConfigCommand.cs +++ b/src/client/Spark.Console/src/Commands/ConfigCommand.cs @@ -8,10 +8,7 @@ namespace Spark.Console { class ConfigCommand { public static int run(ConfigCommandOptions opts) { - if (opts.sparkd is not null) { - var args = Environment.GetCommandLineArgs(); - BTS.set_config_arg(args, opts.sparkd); - } + Configuration.initialize(opts.sparkd); var keys = opts.keys?.ToArray() ?? Array.Empty(); var excluded_keys = opts.excluded_keys?.ToArray() ?? Array.Empty(); return Configuration.run_config_query( diff --git a/src/client/Spark.Console/src/Commands/TestCommand.cs b/src/client/Spark.Console/src/Commands/TestCommand.cs index 241cc0a5f..a1cfae74a 100644 --- a/src/client/Spark.Console/src/Commands/TestCommand.cs +++ b/src/client/Spark.Console/src/Commands/TestCommand.cs @@ -19,7 +19,9 @@ namespace Spark.Console { if (o.type == TestType.Unit) { // Skip loading any local config Configuration.initialize_for_test(); - } else Configuration.initialize(); + } else { + Configuration.initialize(o.sparkd); + } ConsoleEx.WriteLine(JsonConvert.SerializeObject(o), ConsoleColor.DarkGray); diff --git a/src/client/Spark.Console/src/Commands/TestCommandOptions.cs b/src/client/Spark.Console/src/Commands/TestCommandOptions.cs index 2e985065b..1c870d284 100644 --- a/src/client/Spark.Console/src/Commands/TestCommandOptions.cs +++ b/src/client/Spark.Console/src/Commands/TestCommandOptions.cs @@ -21,5 +21,8 @@ namespace Spark.Console { [Value(1, MetaName = "Tests", HelpText = "Test list")] public IEnumerable? tests { get; set; } + + [Option("sparkd", MetaValue = "URL", HelpText = "The sparkd server")] + public string? sparkd { get; set; } } } diff --git a/src/client/spark/App.xaml.cs b/src/client/spark/App.xaml.cs index d162a79d5..5854045c3 100644 --- a/src/client/spark/App.xaml.cs +++ b/src/client/spark/App.xaml.cs @@ -83,7 +83,8 @@ namespace mbts.ui.shell { CultureInfo.DefaultThreadCurrentUICulture = us_culture; base.OnStartup(e); - _runtime = new ShellBootstrapper(_opts?.user, _opts?.password, _opts?.restore_workspace); + _runtime = new ShellBootstrapper( + _opts?.user, _opts?.password, _opts?.sparkd, _opts?.restore_workspace); _runtime.Run(); // Select text on tab in diff --git a/src/client/spark/Daemons/ShellServices.cs b/src/client/spark/Daemons/ShellServices.cs index 83d1db901..330b8b99a 100644 --- a/src/client/spark/Daemons/ShellServices.cs +++ b/src/client/spark/Daemons/ShellServices.cs @@ -16,8 +16,11 @@ using System.Composition; namespace Spark { class ShellServices { + public ShellServices([Import("config_source")]string config_source) => + context = new(config_source); + [Export] - public ServiceContext context { get; } = new ServiceContext(); + public ServiceContext context { get; } [Export("settings")] public PersistenceService settings_service => context.locate("settings"); diff --git a/src/client/spark/ShellBootstrapper.cs b/src/client/spark/ShellBootstrapper.cs index 6ee1fd6f1..95908b210 100644 --- a/src/client/spark/ShellBootstrapper.cs +++ b/src/client/spark/ShellBootstrapper.cs @@ -19,9 +19,14 @@ class ShellBootstrapper : BTSApplicationBootstrapper { Window _login; - public ShellBootstrapper(string user = null, string password = null, bool? restore_workspace = null) { + public ShellBootstrapper( + string user = null, + string password = null, + string config_source = null, + bool? restore_workspace = null + ) : base(config_source) { // Get the LoginModel instance on this thread to make sure it performs syncronous config initialization. - var login_model = LoginModel.instance; + var login_model = LoginModel.init(config_source); // Show the dialog on a background thread so animations aren't blocked during the main application startup var thread = new Thread(thread_start) { IsBackground = true, diff --git a/src/client/spark/Spark.Cli/SparkCmdOpts.cs b/src/client/spark/Spark.Cli/SparkCmdOpts.cs index a82a31ac9..bd4c698ed 100644 --- a/src/client/spark/Spark.Cli/SparkCmdOpts.cs +++ b/src/client/spark/Spark.Cli/SparkCmdOpts.cs @@ -5,8 +5,8 @@ namespace Spark.Cli { using System.Collections.Generic; class SparkCmdOpts { - const string _SPARKD_OPT_NAME = "sparkd"; - + const string _SPARKD_OPT_NAME = "sparkd"; + [Option("multiple", HelpText = "Allow multple shells to run simultaneously")] public bool allow_multiple_shells { get; set; } @@ -39,7 +39,7 @@ namespace Spark.Cli { set => restore_workspace = !value; } - [Option(_SPARKD_OPT_NAME, MetaValue = "URL", HelpText = "The sparkd config server url")] + [Option(_SPARKD_OPT_NAME, MetaValue = "URL", HelpText = "The config server url")] public string? sparkd { get; set; } /// diff --git a/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs b/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs index 5344e7add..a4e5c30e5 100644 --- a/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs +++ b/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs @@ -9,6 +9,8 @@ using System.Windows.Threading; namespace mbts.ui { public abstract class BTSApplicationBootstrapper : IDisposable { + readonly string _config_source; + private ShellModel _model; static BTSApplicationBootstrapper() { @@ -16,6 +18,10 @@ namespace mbts.ui { Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_HandleClipboardError; } + protected BTSApplicationBootstrapper(string config_source = null) { + _config_source = config_source; + } + public static void CurrentDispatcher_HandleClipboardError(object sender, DispatcherUnhandledExceptionEventArgs e) { // Dont' allow clipboard failure to bring down the shell var com_exeption = e.Exception as System.Runtime.InteropServices.COMException; @@ -37,7 +43,7 @@ namespace mbts.ui { public void Run() { _model = new ShellModel(); - _model.Initialize(); + _model.Initialize(_config_source); CreateShell(_model); } diff --git a/src/client/spark/view/Shell/Login/LoginModel.cs b/src/client/spark/view/Shell/Login/LoginModel.cs index 20ff414ef..fcfecf785 100644 --- a/src/client/spark/view/Shell/Login/LoginModel.cs +++ b/src/client/spark/view/Shell/Login/LoginModel.cs @@ -10,6 +10,7 @@ using System; using System.Collections.Generic; using System.Composition; using System.Linq; +using System.Threading; using System.Threading.Tasks; using System.Windows.Threading; @@ -58,24 +59,38 @@ namespace mbts.ui.shell { public class LoginModel : IDisposable { // Singleton to help bootstrap the system - private static LoginModel _instance = new LoginModel(); - public static LoginModel instance { get { return _instance; } } + static Lazy _instance; + + readonly string _config_source; private string _workspace; private PreloginWorkspaceEnumerator _enum_workspace; ServiceContext _ctx; DispatcherFrame _frame = new DispatcherFrame(); - private LoginModel() { + LoginModel(string config_source) { + _config_source = config_source; init().ignore(); } // Properties used in binding to the UI public event Action Ready; + + public static LoginModel instance => + _instance?.Value ?? throw new InvalidOperationException( + "LoginModel._instance is null (call LoginModel.init first)"); + public string ErrorMessage { get; private set; } public bool HasFatalError { get; set; } public User[] Users { get; private set; } + public static LoginModel init(string config_source) { + // '_instance' is lazy to guarentee it is only instantiated once + Interlocked.CompareExchange( + ref _instance, new(() => new(config_source)), null); + return _instance.Value; + } + public bool try_get_user(string name, out User user) => Users.TryGetFirst(u => u.name == name, out user); @@ -104,7 +119,7 @@ namespace mbts.ui.shell { public async Task init() { // Make sure config is initialized and working try { - Configuration.initialize(); + Configuration.initialize(_config_source); } catch (ApplicationException e) { ErrorMessage = (e.Message ?? string.Empty) + " " + (App.stunnel_job_error ?? string.Empty); HasFatalError = true; diff --git a/src/client/spark/view/Shell/ShellModel.cs b/src/client/spark/view/Shell/ShellModel.cs index c8797c4d5..726c771df 100644 --- a/src/client/spark/view/Shell/ShellModel.cs +++ b/src/client/spark/view/Shell/ShellModel.cs @@ -28,6 +28,12 @@ using System.Runtime.Loader; using System.Text; + [Export] + public class ConfigSourceExporter { + [Export("config_source")] + public string config_source { get; set; } + } + // Models a generic multi-window host public class ShellModel : IDisposable { Lazy _container = new Lazy(create_container); @@ -53,8 +59,8 @@ public ISecurityMetadataStore security_metadata_store => SecurityMetadataStore.instance; - public void Initialize() { - PerformMEFComposition(); + public void Initialize(string config_source) { + PerformMEFComposition(config_source); InitInitializers(); } @@ -93,11 +99,12 @@ return value; } - void PerformMEFComposition() { + void PerformMEFComposition(string config_source) { Log.Ui.info($"Generating MEF dependencies for the shell " + $"({typeof(IInitializable)}s, {typeof(IShellModule)}s, " + $"{typeof(IShellDaemon)}s, etc.)"); try { + container.GetExport().config_source = config_source; container.SatisfyImports(this); } catch (ReflectionTypeLoadException rtlex) { var message = new StringBuilder(rtlex.ToString()); diff --git a/src/client/spark_api/api/ServiceContext.cs b/src/client/spark_api/api/ServiceContext.cs index 7d9ed1f94..9b22d1a34 100644 --- a/src/client/spark_api/api/ServiceContext.cs +++ b/src/client/spark_api/api/ServiceContext.cs @@ -12,8 +12,8 @@ namespace bts.api.core { IntPtr _qbus; AuthToken? _token; - public ServiceContext() { - BTS.init(); + public ServiceContext(string? config_source = null) { + BTS.init(config_source); _qbus = ApiNativeMethods.api_create_quickbus(null, 0); } @@ -28,10 +28,7 @@ namespace bts.api.core { public static ServiceContext create(string? config_server = null) { var args = Environment.GetCommandLineArgs(); - if (config_server is not null) { - args = BTS.set_config_arg(args, config_server).ToArray(); - } - BTS.init(args); + BTS.init(config_server, args); var qbus = ApiNativeMethods.api_create_quickbus(null, 0); return new ServiceContext(qbus); } diff --git a/src/client/spark_core/config/Configuration.cs b/src/client/spark_core/config/Configuration.cs index f64ea2a8e..446184364 100644 --- a/src/client/spark_core/config/Configuration.cs +++ b/src/client/spark_core/config/Configuration.cs @@ -50,14 +50,14 @@ namespace mbts.config { } } - public static void initialize() { + public static void initialize(string? config_source = null) { if (_state == State.OK) { return; } // Considered doing this async on a background thread but it wouldn't do any good. The next // guy down the line to ask for config will just block until this completes. try { - BTS.init(); + BTS.init(config_source); } catch (ApplicationException) { _state = State.Error; throw; diff --git a/src/client/spark_core/utils_api/BTS.cs b/src/client/spark_core/utils_api/BTS.cs index 83e3e3290..3f680124c 100644 --- a/src/client/spark_core/utils_api/BTS.cs +++ b/src/client/spark_core/utils_api/BTS.cs @@ -9,16 +9,17 @@ namespace bts { static int _needs_init_spark_lib = 0; - public static void init() { + public static void init(string config_source = null) { var args = Environment.GetCommandLineArgs(); - init(args); + init(config_source, args); } - public static void init(string[] args) { + public static void init(string config_source = null, params string[] args) { utils.BTSNativeMethods.provide_command_line(args, args.Length); var buffer = new StringBuilder(4096); - bool config_ok = utils.BTSNativeMethods.initialize_nothrow(buffer, buffer.Capacity); + bool config_ok = utils.BTSNativeMethods.initialize_nothrow( + config_source ?? string.Empty, buffer, buffer.Capacity); if (!config_ok) { throw new ApplicationException(buffer.ToString()); } diff --git a/src/client/spark_core/utils_api/BTSNativeMethods.cs b/src/client/spark_core/utils_api/BTSNativeMethods.cs index 52ae7b9b8..1a6eee190 100644 --- a/src/client/spark_core/utils_api/BTSNativeMethods.cs +++ b/src/client/spark_core/utils_api/BTSNativeMethods.cs @@ -27,6 +27,7 @@ namespace bts.utils public static extern bool SetConsoleTitle(string lpConsoleTitle); [DllImport("libspark")] - public static extern bool initialize_nothrow(StringBuilder buffer, int length); + public static extern bool initialize_nothrow( + string config_source, StringBuilder buffer, int length); } } diff --git a/src/lib/api/core/ApiClientInterop.cpp b/src/lib/api/core/ApiClientInterop.cpp index 5b413e1d0..328d8f680 100644 --- a/src/lib/api/core/ApiClientInterop.cpp +++ b/src/lib/api/core/ApiClientInterop.cpp @@ -37,7 +37,7 @@ CAPI Quickbus *api_create_quickbus(const char *qbconfig, long length) { // If config hasn't been initialized yet do it now and suppress exceptions. // When we're called from a nuget package we may not have defaults which can // cause errors - config::initialize_nothrow(nullptr, 0); + config::initialize_nothrow("", nullptr, 0); config::QuickbusNodeCfg cfg; if (length > 0 && qbconfig != nullptr && diff --git a/src/lib/config/ConfigQuerier.cpp b/src/lib/config/ConfigQuerier.cpp index eaf439d99..5d63c30a4 100644 --- a/src/lib/config/ConfigQuerier.cpp +++ b/src/lib/config/ConfigQuerier.cpp @@ -97,7 +97,7 @@ void run_config_query( void ConfigQuerier::init_(bool verbose) { char buffer[4096] = {0}; utils::TimeStamp start = utils::TimeStamp::now(); - bool success = config::initialize_nothrow(buffer, sizeof(buffer)); + bool success = config::initialize_nothrow("", buffer, sizeof(buffer)); double time = (utils::TimeStamp::now() - start).seconds(); if (success) { @@ -158,7 +158,7 @@ int ConfigQuerier::run( std::cerr << k << "\n"; } } - + // Print all messages to console google::protobuf::TextFormat::Printer printer; printer.SetInitialIndentLevel(2); diff --git a/src/lib/config/ConfigurationState.cpp b/src/lib/config/ConfigurationState.cpp index 3d39f27a4..42867c60f 100644 --- a/src/lib/config/ConfigurationState.cpp +++ b/src/lib/config/ConfigurationState.cpp @@ -357,7 +357,7 @@ void check_rule( } void ConfigurationState::check_rules() { - initialize(); + initialize(""); std::vector>::iterator iter; for (iter = _config_chain.begin(); iter != _config_chain.end(); ++iter) { proto::Message *message = (*iter).get(); @@ -366,25 +366,28 @@ void ConfigurationState::check_rules() { check_rule(_defaults.get(), _rule_engine); } -void ConfigurationState::initialize_settings() { +void ConfigurationState::initialize_settings(string config_source) { std::lock_guard lock(_initialization_mutex); if (_is_initialized) return; ConfigLog log; + log.info(FMT("Initializing settings from '{}'", config_source)); // For verbose config logs, uncomment line below // log.set_print_errors(true); initialize_defaults(log); - std::string settings = settings_path(); - bts_assert_msg(!settings.empty(), "Failed to determine config path"); - os::setenv(CONFIG_SERVER_ENV_VAR, settings.c_str()); + if (config_source.empty()) { + config_source = settings_path(); + } + bts_assert_msg(!config_source.empty(), "Failed to determine config source"); + os::setenv(CONFIG_SERVER_ENV_VAR, config_source.c_str()); { DEFER( // Do this even if load() fails initialize_builtins(&log); initialize_macros(); _deferred_queue->process();); - load(settings, log); + load(config_source, log); } std::vector descs; @@ -417,13 +420,22 @@ int ConfigurationState::load(const std::string &url, IConfigLog &log) { std::shared_ptr new_message(_prototype->New()); bool loaded = false; + log.info(FMT("Loading '{}'", url)); loaded = load_message(url, *new_message.get(), log, *_deferred_queue.get()); - if (_host.empty() || _host == "localhost") { + const auto old_host = _host; + const auto old_port = _port; tie(_host, _port) = hostname(url); + if (_host != old_host || _port != old_port) { + log.info( + FMT("Changing config host:port from '{}:{}' to '{}:{}'", old_host, + old_port, _host, _port)); + } } - if (!loaded) + if (!loaded) { + log.info(FMT("Failed to load '{}'", url)); return 0; + } std::string include = get_include(*new_message.get()); @@ -842,7 +854,7 @@ PathType::type get_path_type(std::string path) { } urdl::url url_from_string( - const std::string &path, bool required, IConfigLog *log = nullptr) { + const std::string &path, bool required, IConfigLog *log = nullptr) { boost::system::error_code ec; // attempt to parse the path as a URL - if that fails assume it is an // absolute file path and append file:/// @@ -853,8 +865,9 @@ urdl::url url_from_string( url = urdl::url::from_string("file:///" + url_path, ec); if (ec && log != nullptr) { log->warning( - FMT("Failed to convert '{}' to a url. urdl::url::from_string resulted in error code {}", - path, ec)); + FMT("Failed to convert '{}' to a url. urdl::url::from_string " + "resulted in error code {}", + path, ec)); } } bts_assert_msg(!required || !ec, ec.message().c_str()); @@ -1003,7 +1016,7 @@ ConfigDownloader::Result load_file( string url = url_from_string(path, required).to_string(); ConfigDownloader::Result result = downloader.get(url, url_stream); if (!(result == ConfigDownloader::SUCCESS)) { - log.warning(FMT("Failed to load config from '{}'", url)); + log.info(FMT("Failed to load config from '{}'", url)); bts_assert_msg(!required || url_stream, (path + " not found.").c_str()); return result; } diff --git a/src/lib/config/ConfigurationState.h b/src/lib/config/ConfigurationState.h index b77b05677..e41a53717 100644 --- a/src/lib/config/ConfigurationState.h +++ b/src/lib/config/ConfigurationState.h @@ -52,7 +52,15 @@ class ConfigurationState { void initialize_defaults(IConfigLog &log); void initialize_defaults(IConfigLog &log, const std::vector &proto_dirs); - void initialize_settings(); + + /** + * @brief Initialize config with values loaded from a file, dir, or config + * server. + * + * @param config_source The source from which to load values. Can be a path + * (file or dir), or URL (config server). + */ + void initialize_settings(std::string config_source = ""); int load(const std::string &url, IConfigLog &); int load_string(const std::string &values, bool print_errors = false); diff --git a/src/lib/config/config.cpp b/src/lib/config/config.cpp index 6e557da7c..e107b8474 100644 --- a/src/lib/config/config.cpp +++ b/src/lib/config/config.cpp @@ -10,6 +10,7 @@ #include #include #include +#include //#include @@ -18,17 +19,22 @@ namespace proto = google::protobuf; namespace bts { namespace config { +using std::string; + internal::ConfigurationState config_state; ////////////////////////////////////////// // Public API ////////////////////////////////////////// -extern "C" BTS_EXPORT void initialize() { config_state.initialize_settings(); } +extern "C" BTS_EXPORT void initialize(const char *config_source) { + config_state.initialize_settings(string{config_source}); +} -extern "C" BTS_EXPORT bool initialize_nothrow(char *error_message, size_t len) { +extern "C" BTS_EXPORT bool initialize_nothrow( + const char *config_source, char *error_message, size_t len) { try { - initialize(); + initialize(config_source); } catch (std::exception &e) { std::string msg = e.what(); if (msg.size() < len) { diff --git a/src/lib/config/config.h b/src/lib/config/config.h index 4c553cbbc..77934b0eb 100644 --- a/src/lib/config/config.h +++ b/src/lib/config/config.h @@ -27,11 +27,12 @@ namespace config { // config can result in network requests this function is available for // applications that want to manage pauses in execution. Ex: initialize() on a // background thread so that config::get() is fast on a UI thread -extern "C" BTS_EXPORT void initialize(); +extern "C" BTS_EXPORT void initialize(const char *config_path); // Calls initialize() but catches any thrown exception and copies it to the // provided buffer -extern "C" BTS_EXPORT bool initialize_nothrow(char *error_msg, size_t length); +extern "C" BTS_EXPORT bool initialize_nothrow( + const char *config_source, char *error_msg, size_t length); std::vector get_fields( const std::vector keys = {},