diff --git a/common/dbconnector.cpp b/common/dbconnector.cpp index dc080d9b2..c8c9ae7e9 100644 --- a/common/dbconnector.cpp +++ b/common/dbconnector.cpp @@ -27,7 +27,14 @@ void SonicDBConfig::initialize(const string &file) throw runtime_error("SonicDBConfig already initialized"); } - ifstream i(file); + string filename = file; + if ( filename == "" ) + { + const auto env = getenv(SONIC_DB_CONFIG_FILE_ENV_VAL); + filename = env ? string(env) : DEFAULT_SONIC_DB_CONFIG_FILE; + } + + ifstream i(filename); if (i.good()) { try @@ -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); } } diff --git a/common/dbconnector.h b/common/dbconnector.h index 62fbbde96..72bb81360 100644 --- a/common/dbconnector.h +++ b/common/dbconnector.h @@ -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); @@ -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>> m_inst_info; // { dbName, {instName, dbId} }