fix: Init config w/ (optional) server arg in cli

This commit is contained in:
John Colvin committed 2023-04-16 00:29:31 -04:00
1 parent 2ec956b430
commit 6d2cd54deb
20 files changed
+120 -54

No files matched your search

@@ -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<string>();
var excluded_keys = opts.excluded_keys?.ToArray() ?? Array.Empty<string>();
return Configuration.run_config_query(
@@ -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);
@@ -21,5 +21,8 @@ namespace Spark.Console {
[Value(1, MetaName = "Tests", HelpText = "Test list")]
public IEnumerable<string>? tests { get; set; }
[Option("sparkd", MetaValue = "URL", HelpText = "The sparkd server")]
public string? sparkd { get; set; }
}
}
+2 -1
View File
@@ -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
+4 -1
View File
@@ -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<PersistenceService>("settings");
+7 -2
View File
@@ -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,
+3 -3
View File
@@ -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; }
/// <summary>
@@ -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);
}
@@ -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<LoginModel> _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<bool> 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;
+10 -3
View File
@@ -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<CompositionHost> _container = new Lazy<CompositionHost>(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<ConfigSourceExporter>().config_source = config_source;
container.SatisfyImports(this);
} catch (ReflectionTypeLoadException rtlex) {
var message = new StringBuilder(rtlex.ToString());
+3 -6
View File
@@ -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);
}
@@ -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;
+5 -4
View File
@@ -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());
}
@@ -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);
}
}
+1 -1
View File
@@ -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 &&
+2 -2
View File
@@ -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);
+25 -12
View File
@@ -357,7 +357,7 @@ void check_rule(
}
void ConfigurationState::check_rules() {
initialize();
initialize("");
std::vector<std::shared_ptr<proto::Message>>::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<std::recursive_mutex> 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<std::string> descs;
@@ -417,13 +420,22 @@ int ConfigurationState::load(const std::string &url, IConfigLog &log) {
std::shared_ptr<proto::Message> 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;
}
+9 -1
View File
@@ -52,7 +52,15 @@ class ConfigurationState {
void initialize_defaults(IConfigLog &log);
void initialize_defaults(IConfigLog &log, const std::vector<std::string> &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);
+9 -3
View File
@@ -10,6 +10,7 @@
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/text_format.h>
#include <string>
//#include <boost/thread.hpp>
@@ -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) {
+3 -2
View File
@@ -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<T>() 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<const google::protobuf::FieldDescriptor *> get_fields(
const std::vector<std::string> keys = {},