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

[DBConnector]: Add database config env support #345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 10 additions & 3 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ void SonicDBConfig::initialize(const string &file)
throw runtime_error("SonicDBConfig already initialized");
}

ifstream i(file);
string filename = file;
if ( filename == "" )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( filename == "" )
if (filename.empty())

{
const auto env = getenv(SONIC_DB_CONFIG_FILE_ENV_VAL);
filename = env ? string(env) : DEFAULT_SONIC_DB_CONFIG_FILE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string(env) [](start = 25, length = 11)

FYI: no need to explicit convert

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filename = env ? string(env) : DEFAULT_SONIC_DB_CONFIG_FILE; [](start = 8, length = 60)

Add some testcases?

}

ifstream i(filename);
if (i.good())
{
try
Expand Down Expand Up @@ -68,8 +75,8 @@ void SonicDBConfig::initialize(const string &file)
}
else
{
SWSS_LOG_ERROR("Sonic database config file doesn't exist at %s\n", file.c_str());
throw runtime_error("Sonic database config file doesn't exist at " + file);
SWSS_LOG_ERROR("Sonic database config file doesn't exist at %s\n", filename.c_str());
throw runtime_error("Sonic database config file doesn't exist at " + filename);
}
}

Expand Down
3 changes: 2 additions & 1 deletion common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SonicDBInfo
class SonicDBConfig
{
public:
static void initialize(const std::string &file = DEFAULT_SONIC_DB_CONFIG_FILE);
static void initialize(const std::string &file = "");
static std::string getDbInst(const std::string &dbName);
static int getDbId(const std::string &dbName);
static std::string getSeparator(const std::string &dbName);
Expand All @@ -36,6 +36,7 @@ class SonicDBConfig

private:
static constexpr const char *DEFAULT_SONIC_DB_CONFIG_FILE = "/var/run/redis/sonic-db/database_config.json";
static constexpr const char *SONIC_DB_CONFIG_FILE_ENV_VAL = "SONIC_DB_CONFIG_FILE";
// { instName, { unix_socket_path, {hostname, port} } }
static std::unordered_map<std::string, std::pair<std::string, std::pair<std::string, int>>> m_inst_info;
// { dbName, {instName, dbId} }
Expand Down