Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config file: camelCase naming, support for soc config file added #348

Merged
merged 7 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ if ( NOT ENVIRONMENT_VARIABLES)
set( ENVIRONMENT_VARIABLES "\"XDG_RUNTIME_DIR=/tmp\",\"GST_REGISTRY=/tmp/rialto-server-gstreamer-cache.bin\",\"WESTEROS_SINK_USE_ESSRMGR=1\"" )
endif()

if ( NOT EXTRA_ENV_VARIABLES)
set( EXTRA_ENV_VARIABLES "" )
endif()

if (NOT SESSION_SERVER_PATH)
set( SESSION_SERVER_PATH "\"/usr/bin/RialtoServer\"" )
endif()
Expand Down
21 changes: 11 additions & 10 deletions serverManager/config/rialto-config.in.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
// with fields that mimic the structure of this file, but with
// alternative values

"environment_variables" : [@ENVIRONMENT_VARIABLES@],
"session_server_path" : @SESSION_SERVER_PATH@,
"startup_timeout_ms" : @STARTUP_TIMEOUT_MS@,
"healthcheck_interval_s" : @HEALTHCHECK_INTERVAL_S@,
"socket_permissions" : @SOCKET_PERMISSIONS@,
"socket_owner" : @SOCKET_OWNER@,
"socket_group" : @SOCKET_GROUP@,
"num_of_preloaded_servers" : @NUM_OF_PRELOADED_SERVERS@,
"log_level" : @LOG_LEVEL@,
"num_of_pings_before_recovery" : @NUM_OF_PINGS_BEFORE_RECOVERY@
"environmentVariables" : [@ENVIRONMENT_VARIABLES@],
"extraEnvVariables" : [@EXTRA_ENV_VARIABLES@],
"sessionServerPath" : @SESSION_SERVER_PATH@,
"startupTimeoutMs" : @STARTUP_TIMEOUT_MS@,
"healthcheckIntervalInSeconds" : @HEALTHCHECK_INTERVAL_S@,
"socketPermissions" : @SOCKET_PERMISSIONS@,
"socketOwner" : @SOCKET_OWNER@,
"socketGroup" : @SOCKET_GROUP@,
"numOfPreloadedServers" : @NUM_OF_PRELOADED_SERVERS@,
"logLevel" : @LOG_LEVEL@,
"numOfPingsBeforeRecovery" : @NUM_OF_PINGS_BEFORE_RECOVERY@
}
1 change: 1 addition & 0 deletions serverManager/service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if( RIALTO_ENABLE_CONFIG_FILE )
add_compile_definitions(RIALTO_ENABLE_CONFIG_FILE)
add_compile_definitions(RIALTO_CONFIG_PATH="${RIALTO_CONFIG_FILE_DIR}/rialto-config.json")
add_compile_definitions(RIALTO_CONFIG_OVERRIDES_PATH="${RIALTO_CONFIG_OVERRIDES_FILE_DIR}/rialto-config-overrides.json")
add_compile_definitions(RIALTO_CONFIG_SOC_PATH="${RIALTO_CONFIG_FILE_DIR}/rialto_soc.json")
skywojciechowskim marked this conversation as resolved.
Show resolved Hide resolved

target_link_libraries(
RialtoServerManager
Expand Down
3 changes: 3 additions & 0 deletions serverManager/service/include/ConfigHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ class ConfigHelper
#ifdef RIALTO_ENABLE_CONFIG_FILE
private:
void readConfigFile(const std::string &filePath);
void mergeEnvVariables();
#endif // RIALTO_ENABLE_CONFIG_FILE

private:
std::unique_ptr<IConfigReaderFactory> m_configReaderFactory;
std::map<std::string, std::string> m_sessionServerEnvVars;
std::map<std::string, std::string> m_jsonEnvVars;
skywojciechowskim marked this conversation as resolved.
Show resolved Hide resolved
std::map<std::string, std::string> m_jsonExtraEnvVars;
std::string m_sessionServerPath;
std::chrono::milliseconds m_sessionServerStartupTimeout;
std::chrono::seconds m_healthcheckInterval;
Expand Down
3 changes: 3 additions & 0 deletions serverManager/service/include/ConfigReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ConfigReader : public IConfigReader
bool read() override;

std::list<std::string> getEnvironmentVariables() override;
std::list<std::string> getExtraEnvVariables() override;
std::optional<std::string> getSessionServerPath() override;
std::optional<std::chrono::milliseconds> getSessionServerStartupTimeout() override;
std::optional<std::chrono::seconds> getHealthcheckInterval() override;
Expand All @@ -51,6 +52,7 @@ class ConfigReader : public IConfigReader

private:
void parseEnvironmentVariables(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root);
void parseExtraEnvVariables(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root);
void parseSessionServerPath(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root);
void parseSessionServerStartupTimeout(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root);
void parseHealthcheckInterval(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root);
Expand All @@ -65,6 +67,7 @@ class ConfigReader : public IConfigReader
std::shared_ptr<IFileReader> m_fileReader;

std::list<std::string> m_envVars;
std::list<std::string> m_extraEnvVars;
std::optional<std::string> m_sessionServerPath;
std::optional<std::chrono::milliseconds> m_sessionServerStartupTimeout;
std::optional<std::chrono::seconds> m_healthcheckInterval;
Expand Down
1 change: 1 addition & 0 deletions serverManager/service/include/IConfigReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class IConfigReader
virtual bool read() = 0;

virtual std::list<std::string> getEnvironmentVariables() = 0;
virtual std::list<std::string> getExtraEnvVariables() = 0;
virtual std::optional<std::string> getSessionServerPath() = 0;
virtual std::optional<std::chrono::milliseconds> getSessionServerStartupTimeout() = 0;
virtual std::optional<std::chrono::seconds> getHealthcheckInterval() = 0;
Expand Down
35 changes: 28 additions & 7 deletions serverManager/service/source/ConfigHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ ConfigHelper::ConfigHelper(std::unique_ptr<IConfigReaderFactory> &&configReaderF
m_numOfFailedPingsBeforeRecovery{config.numOfFailedPingsBeforeRecovery}, m_loggingLevels{}
{
#ifdef RIALTO_ENABLE_CONFIG_FILE
// Read from least to most important file
readConfigFile(RIALTO_CONFIG_PATH);
readConfigFile(RIALTO_CONFIG_SOC_PATH);
readConfigFile(RIALTO_CONFIG_OVERRIDES_PATH);
mergeEnvVariables();
#endif // RIALTO_ENABLE_CONFIG_FILE
}

Expand Down Expand Up @@ -126,14 +129,18 @@ void ConfigHelper::readConfigFile(const std::string &filePath)
return;
}

std::map<std::string, std::string> envVariables{convertToMap(configReader->getEnvironmentVariables())};
for (const auto &[name, value] : envVariables)
// Always override env variables when present in "more important" file
// (envVariables + extraEnvVariables from less important file will be wiped if envVariables are present)
skywojciechowskim marked this conversation as resolved.
Show resolved Hide resolved
const std::map<std::string, std::string> envVariables{convertToMap(configReader->getEnvironmentVariables())};
if (!envVariables.empty())
{
// If environment variable exists in ServerManagerConfig, do not overwrite it
if (m_sessionServerEnvVars.end() == m_sessionServerEnvVars.find(name))
{
m_sessionServerEnvVars.emplace(name, value);
}
m_jsonEnvVars = envVariables;
m_jsonExtraEnvVars.clear();
}
const std::map<std::string, std::string> extraEnvVariables{convertToMap(configReader->getExtraEnvVariables())};
if (!extraEnvVariables.empty())
{
m_jsonExtraEnvVars = extraEnvVariables;
}

if (configReader->getSessionServerPath())
Expand Down Expand Up @@ -163,5 +170,19 @@ void ConfigHelper::readConfigFile(const std::string &filePath)
if (configReader->getLoggingLevels())
m_loggingLevels = configReader->getLoggingLevels().value();
}

void ConfigHelper::mergeEnvVariables()
{
// Env vars from json are more important than values from config struct
if (!m_jsonEnvVars.empty())
{
m_sessionServerEnvVars = m_jsonEnvVars;
}
for (const auto &[name, value] : m_jsonExtraEnvVars)
{
// If env variable exists both in envVariables and extraEnvVariables, overwrite it.
m_sessionServerEnvVars[name] = value;
}
}
#endif // RIALTO_ENABLE_CONFIG_FILE
} // namespace rialto::servermanager::service
111 changes: 95 additions & 16 deletions serverManager/service/source/ConfigReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ bool ConfigReader::read()
}

parseEnvironmentVariables(root);
parseExtraEnvVariables(root);
parseSessionServerPath(root);
parseSessionServerStartupTimeout(root);
parseHealthcheckInterval(root);
Expand All @@ -61,7 +62,19 @@ bool ConfigReader::read()

void ConfigReader::parseEnvironmentVariables(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("environment_variables") && root->at("environment_variables")->isArray())
if (root->isMember("environmentVariables") && root->at("environmentVariables")->isArray())
{
std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> envVarsJson = root->at("environmentVariables");
Json::ArrayIndex size = envVarsJson->size();
for (Json::ArrayIndex index = 0; index < size; ++index)
{
if (envVarsJson->at(index)->isString())
{
m_envVars.emplace_back(envVarsJson->at(index)->asString());
}
}
}
else if (root->isMember("environment_variables") && root->at("environment_variables")->isArray())
{
std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> envVarsJson = root->at("environment_variables");
Json::ArrayIndex size = envVarsJson->size();
Expand All @@ -75,33 +88,71 @@ void ConfigReader::parseEnvironmentVariables(std::shared_ptr<firebolt::rialto::w
}
}

void ConfigReader::parseExtraEnvVariables(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("extraEnvVariables") && root->at("extraEnvVariables")->isArray())
{
std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> envVarsJson = root->at("extraEnvVariables");
Json::ArrayIndex size = envVarsJson->size();
for (Json::ArrayIndex index = 0; index < size; ++index)
{
if (envVarsJson->at(index)->isString())
{
m_extraEnvVars.emplace_back(envVarsJson->at(index)->asString());
}
}
}
}

void ConfigReader::parseSessionServerPath(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("session_server_path") && root->at("session_server_path")->isString())
if (root->isMember("sessionServerPath") && root->at("sessionServerPath")->isString())
skywojciechowskim marked this conversation as resolved.
Show resolved Hide resolved
{
m_sessionServerPath = root->at("sessionServerPath")->asString();
}
else if (root->isMember("session_server_path") && root->at("session_server_path")->isString())
{
m_sessionServerPath = root->at("session_server_path")->asString();
}
}

void ConfigReader::parseSessionServerStartupTimeout(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("startup_timeout_ms") && root->at("startup_timeout_ms")->isUInt())
if (root->isMember("startupTimeoutMs") && root->at("startupTimeoutMs")->isUInt())
{
m_sessionServerStartupTimeout = std::chrono::milliseconds(root->at("startupTimeoutMs")->asUInt());
}
else if (root->isMember("startup_timeout_ms") && root->at("startup_timeout_ms")->isUInt())
{
m_sessionServerStartupTimeout = std::chrono::milliseconds(root->at("startup_timeout_ms")->asUInt());
}
}

void ConfigReader::parseHealthcheckInterval(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("healthcheck_interval_s") && root->at("healthcheck_interval_s")->isUInt())
if (root->isMember("healthcheckIntervalInSeconds") && root->at("healthcheckIntervalInSeconds")->isUInt())
{
m_healthcheckInterval = std::chrono::seconds(root->at("healthcheckIntervalInSeconds")->asUInt());
}
else if (root->isMember("healthcheck_interval_s") && root->at("healthcheck_interval_s")->isUInt())
{
m_healthcheckInterval = std::chrono::seconds(root->at("healthcheck_interval_s")->asUInt());
}
}

void ConfigReader::parseSocketPermissions(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("socket_permissions") && root->at("socket_permissions")->isUInt())
if (root->isMember("socketPermissions") && root->at("socketPermissions")->isUInt())
{
unsigned permissions = root->at("socketPermissions")->asUInt();

firebolt::rialto::common::SocketPermissions socketPermissions;
socketPermissions.ownerPermissions = (permissions / 100) % 10;
socketPermissions.groupPermissions = (permissions / 10) % 10;
socketPermissions.otherPermissions = (permissions) % 10;
m_socketPermissions = socketPermissions;
}
else if (root->isMember("socket_permissions") && root->at("socket_permissions")->isUInt())
{
unsigned permissions = root->at("socket_permissions")->asUInt();

Expand All @@ -115,38 +166,57 @@ void ConfigReader::parseSocketPermissions(std::shared_ptr<firebolt::rialto::wrap

void ConfigReader::parseSocketOwner(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
const char *kFieldString = "socket_owner";
if (root->isMember(kFieldString) && root->at(kFieldString)->isString())
if (root->isMember("socketOwner") && root->at("socketOwner")->isString())
{
m_socketOwner = root->at(kFieldString)->asString();
m_socketOwner = root->at("socketOwner")->asString();
}
else if (root->isMember("socket_owner") && root->at("socket_owner")->isString())
{
m_socketOwner = root->at("socket_owner")->asString();
}
}

void ConfigReader::parseSocketGroup(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
const char *kFieldString = "socket_group";
if (root->isMember(kFieldString) && root->at(kFieldString)->isString())
if (root->isMember("socketGroup") && root->at("socketGroup")->isString())
{
m_socketGroup = root->at("socketGroup")->asString();
}
else if (root->isMember("socket_group") && root->at("socket_group")->isString())
{
m_socketGroup = root->at(kFieldString)->asString();
m_socketGroup = root->at("socket_group")->asString();
}
}

void ConfigReader::parseNumOfPreloadedServers(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("num_of_preloaded_servers") && root->at("num_of_preloaded_servers")->isUInt())
if (root->isMember("numOfPreloadedServers") && root->at("numOfPreloadedServers")->isUInt())
{
m_numOfPreloadedServers = root->at("numOfPreloadedServers")->asUInt();
}
else if (root->isMember("num_of_preloaded_servers") && root->at("num_of_preloaded_servers")->isUInt())
{
m_numOfPreloadedServers = root->at("num_of_preloaded_servers")->asUInt();
}
}

void ConfigReader::parseLogLevel(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("log_level") && root->at("log_level")->isUInt())
std::optional<unsigned> loggingLevel{std::nullopt};
if (root->isMember("logLevel") && root->at("logLevel")->isUInt())
{
loggingLevel = root->at("logLevel")->asUInt();
}
else if (root->isMember("log_level") && root->at("log_level")->isUInt())
{
loggingLevel = root->at("log_level")->asUInt();
}

if (loggingLevel)
{
unsigned loggingLevel = root->at("log_level")->asUInt();
rialto::servermanager::service::LoggingLevel newLevel{rialto::servermanager::service::LoggingLevel::UNCHANGED};

switch (loggingLevel)
switch (*loggingLevel)
{
case 0:
newLevel = rialto::servermanager::service::LoggingLevel::FATAL;
Expand Down Expand Up @@ -176,7 +246,11 @@ void ConfigReader::parseLogLevel(std::shared_ptr<firebolt::rialto::wrappers::IJs

void ConfigReader::parseNumOfPingsBeforeRecovery(std::shared_ptr<firebolt::rialto::wrappers::IJsonValueWrapper> root)
{
if (root->isMember("num_of_pings_before_recovery") && root->at("num_of_pings_before_recovery")->isUInt())
if (root->isMember("numOfPingsBeforeRecovery") && root->at("numOfPingsBeforeRecovery")->isUInt())
{
m_numOfPingsBeforeRecovery = root->at("numOfPingsBeforeRecovery")->asUInt();
}
else if (root->isMember("num_of_pings_before_recovery") && root->at("num_of_pings_before_recovery")->isUInt())
{
m_numOfPingsBeforeRecovery = root->at("num_of_pings_before_recovery")->asUInt();
}
Expand All @@ -187,6 +261,11 @@ std::list<std::string> ConfigReader::getEnvironmentVariables()
return m_envVars;
}

std::list<std::string> ConfigReader::getExtraEnvVariables()
{
return m_extraEnvVars;
}

std::optional<std::string> ConfigReader::getSessionServerPath()
{
return m_sessionServerPath;
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/serverManager/mocks/ConfigReaderMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ConfigReaderMock : public IConfigReader
public:
MOCK_METHOD(bool, read, (), (override));
MOCK_METHOD(std::list<std::string>, getEnvironmentVariables, (), (override));
MOCK_METHOD(std::list<std::string>, getExtraEnvVariables, (), (override));
MOCK_METHOD(std::optional<std::string>, getSessionServerPath, (), (override));
MOCK_METHOD(std::optional<std::chrono::milliseconds>, getSessionServerStartupTimeout, (), (override));
MOCK_METHOD(std::optional<std::chrono::seconds>, getHealthcheckInterval, (), (override));
Expand Down
Loading
Loading