Skip to content

Commit

Permalink
Split config file (#193)
Browse files Browse the repository at this point in the history
* Split config file

* Refactor.

Signed-off-by: RileyW <wrllrwwrllrw@gmail.com>

---------

Signed-off-by: RileyW <wrllrwwrllrw@gmail.com>
Co-authored-by: RileyW <wrllrwwrllrw@gmail.com>
  • Loading branch information
MCKZX-llx and RileyWen authored Oct 17, 2023
1 parent 232ac38 commit b82598b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,7 @@ install(FILES
install(FILES etc/config.yaml
DESTINATION /etc/crane
RENAME config_example.yaml)

install(FILES etc/database.yaml
DESTINATION /etc/crane
RENAME database_example.yaml)
13 changes: 1 addition & 12 deletions etc/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ ServerKeyFilePath: /etc/crane/server.key
CaCertFilePath: /etc/crane/ca.crt
DomainSuffix: riley.local

# EmbeddedDb settings
CraneEmbeddedDbBackend: Unqlite # BerkeleyDB or Unqlite(default)
CraneCtldDbPath: /tmp/cranectld/embedded.db

# Mongodb settings
DbUser: admin
DbPassword: "123456"
DbHost: localhost
DbPort: 27017
DbReplSetName: rs0
DbName: crane_db

DbConfigPath: /etc/crane/database.yaml

# Craned Options

Expand Down
11 changes: 11 additions & 0 deletions etc/database.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# EmbeddedDb settings
CraneEmbeddedDbBackend: Unqlite # BerkeleyDB or Unqlite(default)
CraneCtldDbPath: /tmp/cranectld/embedded.db

# Mongodb settings
DbUser: admin
DbPassword: "123456"
DbHost: localhost
DbPort: 27017
DbReplSetName: rs0
DbName: crane_db
98 changes: 58 additions & 40 deletions src/CraneCtld/CraneCtld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ void ParseConfig(int argc, char** argv) {
auto parsed_args = options.parse(argc, argv);

std::string config_path = parsed_args["config"].as<std::string>();
std::string db_config_path = Ctld::kDefaultDbConfigPath;
if (std::filesystem::exists(config_path)) {
try {
YAML::Node config = YAML::LoadFile(config_path);
Expand Down Expand Up @@ -153,46 +154,10 @@ void ParseConfig(int argc, char** argv) {
g_config.ListenConf.UseTls = false;
}

if (config["CraneEmbeddedDbBackend"] &&
!config["CraneEmbeddedDbBackend"].IsNull())
g_config.CraneEmbeddedDbBackend =
config["CraneEmbeddedDbBackend"].as<std::string>();
else
g_config.CraneEmbeddedDbBackend = "Unqlite";

if (config["CraneCtldDbPath"] && !config["CraneCtldDbPath"].IsNull())
g_config.CraneCtldDbPath = config["CraneCtldDbPath"].as<std::string>();
else
g_config.CraneCtldDbPath = Ctld::kDefaultDbPath;

if (config["DbUser"] && !config["DbUser"].IsNull()) {
g_config.DbUser = config["DbUser"].as<std::string>();
if (config["DbPassword"] && !config["DbPassword"].IsNull())
g_config.DbPassword = config["DbPassword"].as<std::string>();
if (config["DbConfigPath"]) {
db_config_path = config["DbConfigPath"].as<std::string>();
}

if (config["DbHost"] && !config["DbHost"].IsNull())
g_config.DbHost = config["DbHost"].as<std::string>();
else
g_config.DbHost = "localhost";

if (config["DbPort"] && !config["DbPort"].IsNull())
g_config.DbPort = config["DbPort"].as<std::string>();
else
g_config.DbPort = "27017"; // default port 27017

if (config["DbReplSetName"] && !config["DbReplSetName"].IsNull())
g_config.DbRSName = config["DbReplSetName"].as<std::string>();
else {
CRANE_ERROR("Unknown Replica Set name");
std::exit(1);
}

if (config["DbName"] && !config["DbName"].IsNull())
g_config.DbName = config["DbName"].as<std::string>();
else
g_config.DbName = "crane_db";

if (config["CraneCtldForeground"]) {
g_config.CraneCtldForeground = config["CraneCtldForeground"].as<bool>();
}
Expand Down Expand Up @@ -403,14 +368,67 @@ void ParseConfig(int argc, char** argv) {
}
}
} catch (YAML::BadFile& e) {
CRANE_CRITICAL("Can't open config file {}: {}", kDefaultConfigPath,
e.what());
CRANE_CRITICAL("Can't open config file {}: {}", config_path, e.what());
std::exit(1);
}
} else {
CRANE_CRITICAL("Config file '{}' not existed", config_path);
std::exit(1);
}

if (std::filesystem::exists(db_config_path)) {
try {
YAML::Node config = YAML::LoadFile(db_config_path);

if (config["CraneEmbeddedDbBackend"] &&
!config["CraneEmbeddedDbBackend"].IsNull())
g_config.CraneEmbeddedDbBackend =
config["CraneEmbeddedDbBackend"].as<std::string>();
else
g_config.CraneEmbeddedDbBackend = "Unqlite";

if (config["CraneCtldDbPath"] && !config["CraneCtldDbPath"].IsNull())
g_config.CraneCtldDbPath = config["CraneCtldDbPath"].as<std::string>();
else
g_config.CraneCtldDbPath = Ctld::kDefaultDbPath;

if (config["DbUser"] && !config["DbUser"].IsNull()) {
g_config.DbUser = config["DbUser"].as<std::string>();
if (config["DbPassword"] && !config["DbPassword"].IsNull())
g_config.DbPassword = config["DbPassword"].as<std::string>();
}

if (config["DbHost"] && !config["DbHost"].IsNull())
g_config.DbHost = config["DbHost"].as<std::string>();
else
g_config.DbHost = "localhost";

if (config["DbPort"] && !config["DbPort"].IsNull())
g_config.DbPort = config["DbPort"].as<std::string>();
else
g_config.DbPort = "27017"; // default port 27017

if (config["DbReplSetName"] && !config["DbReplSetName"].IsNull())
g_config.DbRSName = config["DbReplSetName"].as<std::string>();
else {
CRANE_ERROR("Unknown Replica Set name");
std::exit(1);
}

if (config["DbName"] && !config["DbName"].IsNull())
g_config.DbName = config["DbName"].as<std::string>();
else
g_config.DbName = "crane_db";
} catch (YAML::BadFile& e) {
CRANE_CRITICAL("Can't open database config file {}: {}", db_config_path,
e.what());
std::exit(1);
}
} else {
CRANE_CRITICAL("Database config file '{}' not existed", db_config_path);
std::exit(1);
}

if (parsed_args.count("listen")) {
g_config.ListenConf.CraneCtldListenAddr =
parsed_args["listen"].as<std::string>();
Expand Down
1 change: 1 addition & 0 deletions src/CraneCtld/CtldPublicDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using task_db_id_t = int64_t;

inline const char* kCraneCtldDefaultLogPath = "/tmp/cranectld/cranectld.log";
inline const char* kDefaultDbPath = "/tmp/cranectld/embedded.db";
inline const char* kDefaultDbConfigPath = "/etc/crane/database.yaml";

constexpr uint64_t kTaskScheduleIntervalMs = 1000;
constexpr uint16_t kCompletionQueueDelaySeconds = 15;
Expand Down
5 changes: 3 additions & 2 deletions src/CraneCtld/TaskScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ bool TaskScheduler::Init() {
[this](const uvw::timer_event&, uvw::timer_handle&) {
CancelTaskTimerCb_();
});
m_cancel_task_timer_handle_->start(std::chrono::milliseconds(1000),
std::chrono::milliseconds(500));
m_cancel_task_timer_handle_->start(
std::chrono::milliseconds(kCancelTaskTimeoutMs * 3),
std::chrono::milliseconds(kCancelTaskTimeoutMs));

m_cancel_task_async_handle_ = uvw_loop->resource<uvw::async_handle>();
m_cancel_task_async_handle_->on<uvw::async_event>(
Expand Down

0 comments on commit b82598b

Please sign in to comment.