Plumb isRelay CLI arg through to config

pull/1186/head
Stephen Shelton 4 years ago
parent 028e55e997
commit 923e73f693
No known key found for this signature in database
GPG Key ID: EE4BADACCE8B631C

@ -57,7 +57,7 @@ namespace
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
llarp::Config config; llarp::Config config;
if (!config.Load(configFile.c_str())) if(!config.Load(configFile.c_str(), false))
{ {
llarp::LogError("Failed to load from config file: ", configFile); llarp::LogError("Failed to load from config file: ", configFile);
return false; return false;

@ -65,7 +65,7 @@ run_main_context(std::string conffname, llarp_main_runtime_opts opts)
// this is important, can downgrade from Info though // this is important, can downgrade from Info though
llarp::LogDebug("Running from: ", fs::current_path().string()); llarp::LogDebug("Running from: ", fs::current_path().string());
llarp::LogInfo("Using config file: ", conffname); llarp::LogInfo("Using config file: ", conffname);
ctx = llarp_main_init(conffname.c_str()); ctx = llarp_main_init(conffname.c_str(), opts.isRelay);
int code = 1; int code = 1;
if (ctx) if (ctx)
{ {
@ -164,12 +164,18 @@ main(int argc, char* argv[])
opts.background = true; opts.background = true;
} }
if (result.count("force") > 0) if(result.count("relay") > 0)
{
opts.isRelay = true;
}
if(result.count("force") > 0)
{ {
overwrite = true; overwrite = true;
} }
if (result.count("router") > 0) // TODO: remove this
if(result.count("router") > 0)
{ {
asRouter = true; asRouter = true;
// we should generate and exit (docker needs this, so we don't write a // we should generate and exit (docker needs this, so we don't write a

@ -26,6 +26,7 @@ extern "C"
bool background = false; bool background = false;
bool debug = false; bool debug = false;
bool singleThreaded = false; bool singleThreaded = false;
bool isRelay = false;
}; };
/// llarp_application config /// llarp_application config
@ -138,44 +139,44 @@ extern "C"
/// allocates new config and puts it into c /// allocates new config and puts it into c
/// return false on failure /// return false on failure
bool bool
llarp_config_load_file(const char* fname, struct llarp_config** c); llarp_config_load_file(const char *fname, struct llarp_config **c, bool isRelay);
/// loads config from file by name /// loads config from file by name
/// uses already allocated config /// uses already allocated config
/// return false on failure /// return false on failure
bool bool
llarp_config_read_file(struct llarp_config* c, const char* f); llarp_config_read_file(struct llarp_config *c, const char *f, bool isRelay);
/// make a main context from configuration /// make a main context from configuration
/// copies config contents /// copies config contents
struct llarp_main* struct llarp_main *
llarp_main_init_from_config(struct llarp_config* conf); llarp_main_init_from_config(struct llarp_config *conf, bool isRelay);
/// initialize application context and load config /// initialize application context and load config
static struct llarp_main* static struct llarp_main *
llarp_main_init(const char* fname) llarp_main_init(const char *fname, bool isRelay)
{ {
struct llarp_main* m = 0; struct llarp_main *m = 0;
struct llarp_config* conf = 0; struct llarp_config *conf = 0;
if (!llarp_config_load_file(fname, &conf)) if(!llarp_config_load_file(fname, &conf, isRelay))
return 0; return 0;
if (conf == NULL) if (conf == NULL)
return 0; return 0;
m = llarp_main_init_from_config(conf); m = llarp_main_init_from_config(conf, isRelay);
llarp_config_free(conf); llarp_config_free(conf);
return m; return m;
} }
/// initialize applicatin context with all defaults /// initialize applicatin context with all defaults
static struct llarp_main* static struct llarp_main *
llarp_main_default_init() llarp_main_default_init(bool isRelay)
{ {
struct llarp_main* m; struct llarp_main* m;
struct llarp_config* conf; struct llarp_config* conf;
conf = llarp_default_config(); conf = llarp_default_config();
if (conf == 0) if (conf == 0)
return 0; return 0;
m = llarp_main_init_from_config(conf); m = llarp_main_init_from_config(conf, isRelay);
llarp_config_free(conf); llarp_config_free(conf);
return m; return m;
} }
@ -183,7 +184,7 @@ extern "C"
/// (re)configure main context /// (re)configure main context
/// return true if (re)configuration was successful /// return true if (re)configuration was successful
bool bool
llarp_main_configure(struct llarp_main* ptr, struct llarp_config* conf); llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf, bool isRelay);
/// return true if this main context is running /// return true if this main context is running
/// return false otherwise /// return false otherwise

@ -55,7 +55,7 @@ namespace llarp
std::string nodedb_dir; std::string nodedb_dir;
bool bool
LoadConfig(const std::string& fname); LoadConfig(const std::string &fname, bool isRelay);
void void
Close(); Close();
@ -73,7 +73,7 @@ namespace llarp
HandleSignal(int sig); HandleSignal(int sig);
bool bool
Configure(); Configure(bool isRelay);
bool bool
IsUp() const; IsUp() const;
@ -113,9 +113,6 @@ namespace llarp
void void
SigINT(); SigINT();
bool
ReloadConfig();
std::string configfile; std::string configfile;
std::string pidfile; std::string pidfile;
std::unique_ptr<std::promise<void>> closeWaiter; std::unique_ptr<std::promise<void>> closeWaiter;

@ -92,7 +92,7 @@ namespace llarp
} }
void void
RouterConfig::defineConfigOptions(Configuration& conf) RouterConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
conf.defineOption<int>("router", "job-queue-size", false, m_JobQueueSize, conf.defineOption<int>("router", "job-queue-size", false, m_JobQueueSize,
[this](int arg) { [this](int arg) {
@ -216,8 +216,10 @@ namespace llarp
} }
void void
NetworkConfig::defineConfigOptions(Configuration& conf) NetworkConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
// TODO: review default value // TODO: review default value
conf.defineOption<bool>("network", "profiling", false, m_enableProfiling, conf.defineOption<bool>("network", "profiling", false, m_enableProfiling,
[this](bool arg) { [this](bool arg) {
@ -238,8 +240,10 @@ namespace llarp
} }
void void
NetdbConfig::defineConfigOptions(Configuration& conf) NetdbConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.defineOption<std::string>("netdb", "dir", false, m_nodedbDir, conf.defineOption<std::string>("netdb", "dir", false, m_nodedbDir,
[this](std::string arg) { [this](std::string arg) {
m_nodedbDir = str(arg); m_nodedbDir = str(arg);
@ -247,8 +251,10 @@ namespace llarp
} }
void void
DnsConfig::defineConfigOptions(Configuration& conf) DnsConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
// TODO: this was previously a multi-value option // TODO: this was previously a multi-value option
conf.defineOption<std::string>("dns", "upstream", false, "", conf.defineOption<std::string>("dns", "upstream", false, "",
[this](std::string arg) { [this](std::string arg) {
@ -288,8 +294,10 @@ namespace llarp
} }
void void
LinksConfig::defineConfigOptions(Configuration& conf) LinksConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) { conf.addUndeclaredHandler("bind", [&](string_view, string_view name, string_view value) {
LinkInfo info = LinkInfoFromINIValues(name, value); LinkInfo info = LinkInfoFromINIValues(name, value);
@ -312,8 +320,10 @@ namespace llarp
} }
void void
ConnectConfig::defineConfigOptions(Configuration& conf) ConnectConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.addUndeclaredHandler("connect", [this](string_view section, conf.addUndeclaredHandler("connect", [this](string_view section,
string_view name, string_view name,
@ -326,8 +336,10 @@ namespace llarp
} }
void void
ServicesConfig::defineConfigOptions(Configuration& conf) ServicesConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.addUndeclaredHandler("services", [this](string_view section, conf.addUndeclaredHandler("services", [this](string_view section,
string_view name, string_view name,
string_view value) { string_view value) {
@ -338,8 +350,10 @@ namespace llarp
} }
void void
SystemConfig::defineConfigOptions(Configuration& conf) SystemConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.defineOption<std::string>("system", "pidfile", false, pidfile, conf.defineOption<std::string>("system", "pidfile", false, pidfile,
[this](std::string arg) { [this](std::string arg) {
pidfile = std::move(arg); pidfile = std::move(arg);
@ -347,8 +361,10 @@ namespace llarp
} }
void void
ApiConfig::defineConfigOptions(Configuration& conf) ApiConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.defineOption<bool>("api", "enabled", false, m_enableRPCServer, conf.defineOption<bool>("api", "enabled", false, m_enableRPCServer,
[this](bool arg) { [this](bool arg) {
m_enableRPCServer = arg; m_enableRPCServer = arg;
@ -363,8 +379,10 @@ namespace llarp
} }
void void
LokidConfig::defineConfigOptions(Configuration& conf) LokidConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.defineOption<std::string>("lokid", "service-node-seed", false, "", conf.defineOption<std::string>("lokid", "service-node-seed", false, "",
[this](std::string arg) { [this](std::string arg) {
if (not arg.empty()) if (not arg.empty())
@ -397,8 +415,10 @@ namespace llarp
} }
void void
BootstrapConfig::defineConfigOptions(Configuration& conf) BootstrapConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.addUndeclaredHandler("bootstrap", [&](string_view, string_view name, string_view value) { conf.addUndeclaredHandler("bootstrap", [&](string_view, string_view name, string_view value) {
if (name != "add-node") if (name != "add-node")
{ {
@ -413,8 +433,10 @@ namespace llarp
} }
void void
LoggingConfig::defineConfigOptions(Configuration& conf) LoggingConfig::defineConfigOptions(Configuration& conf, bool isRelay)
{ {
(void)isRelay;
conf.defineOption<std::string>("logging", "type", false, "file", conf.defineOption<std::string>("logging", "type", false, "file",
[this](std::string arg) { [this](std::string arg) {
LoggingConfig::LogType type = LogTypeFromString(arg); LoggingConfig::LogType type = LogTypeFromString(arg);
@ -440,13 +462,13 @@ namespace llarp
} }
bool bool
Config::Load(const char *fname) Config::Load(const char *fname, bool isRelay)
{ {
// TODO: DRY // TODO: DRY
try try
{ {
Configuration conf; Configuration conf;
initializeConfig(conf); initializeConfig(conf, isRelay);
ConfigParser parser; ConfigParser parser;
if(!parser.LoadFile(fname)) if(!parser.LoadFile(fname))
@ -472,25 +494,25 @@ namespace llarp
} }
void void
Config::initializeConfig(Configuration& conf) Config::initializeConfig(Configuration& conf, bool isRelay)
{ {
// TODO: this seems like a random place to put this, should this be closer // TODO: this seems like a random place to put this, should this be closer
// to main() ? // to main() ?
if(Lokinet_INIT()) if(Lokinet_INIT())
throw std::runtime_error("Can't initializeConfig() when Lokinet_INIT() == true"); throw std::runtime_error("Can't initializeConfig() when Lokinet_INIT() == true");
router.defineConfigOptions(conf); router.defineConfigOptions(conf, isRelay);
network.defineConfigOptions(conf); network.defineConfigOptions(conf, isRelay);
connect.defineConfigOptions(conf); connect.defineConfigOptions(conf, isRelay);
netdb.defineConfigOptions(conf); netdb.defineConfigOptions(conf, isRelay);
dns.defineConfigOptions(conf); dns.defineConfigOptions(conf, isRelay);
links.defineConfigOptions(conf); links.defineConfigOptions(conf, isRelay);
services.defineConfigOptions(conf); services.defineConfigOptions(conf, isRelay);
system.defineConfigOptions(conf); system.defineConfigOptions(conf, isRelay);
api.defineConfigOptions(conf); api.defineConfigOptions(conf, isRelay);
lokid.defineConfigOptions(conf); lokid.defineConfigOptions(conf, isRelay);
bootstrap.defineConfigOptions(conf); bootstrap.defineConfigOptions(conf, isRelay);
logging.defineConfigOptions(conf); logging.defineConfigOptions(conf, isRelay);
} }
fs::path fs::path
@ -564,7 +586,7 @@ namespace llarp
Config::generateBaseClientConfig() Config::generateBaseClientConfig()
{ {
llarp::Configuration def; llarp::Configuration def;
initializeConfig(def); initializeConfig(def, false);
// TODO: pass these in // TODO: pass these in
const std::string basepath = ""; const std::string basepath = "";
@ -683,7 +705,7 @@ namespace llarp
Config::generateBaseRouterConfig() Config::generateBaseRouterConfig()
{ {
llarp::Configuration def; llarp::Configuration def;
initializeConfig(def); initializeConfig(def, true);
// lokid // lokid
def.addSectionComment("lokid", "Lokid configuration (settings for talking to lokid"); def.addSectionComment("lokid", "Lokid configuration (settings for talking to lokid");

@ -91,7 +91,7 @@ namespace llarp
// clang-format on // clang-format on
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
class NetworkConfig class NetworkConfig
@ -114,7 +114,7 @@ namespace llarp
// clang-format on // clang-format on
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
class NetdbConfig class NetdbConfig
@ -128,7 +128,7 @@ namespace llarp
// clang-format on // clang-format on
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct DnsConfig struct DnsConfig
@ -136,7 +136,7 @@ namespace llarp
std::unordered_multimap<std::string, std::string> netConfig; std::unordered_multimap<std::string, std::string> netConfig;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
class LinksConfig class LinksConfig
@ -165,7 +165,7 @@ namespace llarp
// clang-format on // clang-format on
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct ConnectConfig struct ConnectConfig
@ -173,14 +173,14 @@ namespace llarp
std::vector<std::string> routers; std::vector<std::string> routers;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct ServicesConfig struct ServicesConfig
{ {
std::vector< std::pair< std::string, std::string > > services; std::vector< std::pair< std::string, std::string > > services;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct SystemConfig struct SystemConfig
@ -188,7 +188,7 @@ namespace llarp
std::string pidfile; std::string pidfile;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
class ApiConfig class ApiConfig
@ -204,7 +204,7 @@ namespace llarp
// clang-format on // clang-format on
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct LokidConfig struct LokidConfig
@ -217,14 +217,14 @@ namespace llarp
std::string lokidRPCPassword; std::string lokidRPCPassword;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct BootstrapConfig struct BootstrapConfig
{ {
std::vector< std::string > routers; std::vector< std::string > routers;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct LoggingConfig struct LoggingConfig
@ -243,7 +243,7 @@ namespace llarp
std::string m_logFile; std::string m_logFile;
void void
defineConfigOptions(Configuration& conf); defineConfigOptions(Configuration& conf, bool isRelay);
}; };
struct Config struct Config
@ -263,11 +263,11 @@ namespace llarp
// Initialize config definition // Initialize config definition
void void
initializeConfig(Configuration& conf); initializeConfig(Configuration& conf, bool isRelay);
// Load a config from the given file // Load a config from the given file
bool bool
Load(const char* fname); Load(const char* fname, bool isRelay);
std::string std::string
generateBaseClientConfig(); generateBaseClientConfig();
@ -290,10 +290,4 @@ namespace llarp
} // namespace llarp } // namespace llarp
void
llarp_ensure_router_config(std::ofstream& f, std::string basepath);
bool
llarp_ensure_client_config(std::ofstream& f, std::string basepath);
#endif #endif

@ -28,12 +28,12 @@ namespace llarp
} }
bool bool
Context::Configure() Context::Configure(bool isRelay)
{ {
// llarp::LogInfo("loading config at ", configfile); // llarp::LogInfo("loading config at ", configfile);
if (configfile.size()) if (configfile.size())
{ {
if (!config->Load(configfile.c_str())) if(!config->Load(configfile.c_str(), isRelay))
{ {
config.release(); config.release();
llarp::LogError("failed to load config file ", configfile); llarp::LogError("failed to load config file ", configfile);
@ -257,11 +257,11 @@ namespace llarp
} }
bool bool
Context::LoadConfig(const std::string& fname) Context::LoadConfig(const std::string &fname, bool isRelay)
{ {
config = std::make_unique<Config>(); config = std::make_unique<Config>();
configfile = fname; configfile = fname;
return Configure(); return Configure(isRelay);
} }
#ifdef LOKINET_HIVE #ifdef LOKINET_HIVE
@ -335,31 +335,31 @@ extern "C"
delete conf; delete conf;
} }
struct llarp_main* struct llarp_main *
llarp_main_init_from_config(struct llarp_config* conf) llarp_main_init_from_config(struct llarp_config *conf, bool isRelay)
{ {
if (conf == nullptr) if (conf == nullptr)
return nullptr; return nullptr;
llarp_main* m = new llarp_main(conf); llarp_main *m = new llarp_main(conf);
if (m->ctx->Configure()) if(m->ctx->Configure(isRelay))
return m; return m;
delete m; delete m;
return nullptr; return nullptr;
} }
bool bool
llarp_config_read_file(struct llarp_config* conf, const char* fname) llarp_config_read_file(struct llarp_config *conf, const char *fname, bool isRelay)
{ {
if (conf == nullptr) if (conf == nullptr)
return false; return false;
return conf->impl.Load(fname); return conf->impl.Load(fname, isRelay);
} }
bool bool
llarp_config_load_file(const char* fname, struct llarp_config** conf) llarp_config_load_file(const char *fname, struct llarp_config **conf, bool isRelay)
{ {
llarp_config* c = new llarp_config(); llarp_config *c = new llarp_config();
if (c->impl.Load(fname)) if(c->impl.Load(fname, isRelay))
{ {
*conf = c; *conf = c;
return true; return true;
@ -496,13 +496,13 @@ extern "C"
} }
bool bool
llarp_main_configure(struct llarp_main* ptr, struct llarp_config* conf) llarp_main_configure(struct llarp_main *ptr, struct llarp_config *conf, bool isRelay)
{ {
if (ptr == nullptr || conf == nullptr) if (ptr == nullptr || conf == nullptr)
return false; return false;
// give new config // give new config
ptr->ctx->config.reset(new llarp::Config(conf->impl)); ptr->ctx->config.reset(new llarp::Config(conf->impl));
return ptr->ctx->Configure(); return ptr->ctx->Configure(isRelay);
} }
bool bool

Loading…
Cancel
Save