Skip to content

Commit

Permalink
Refactor access to Environment Variables
Browse files Browse the repository at this point in the history
Re ECFLOW-1957
  • Loading branch information
marcosbento committed Sep 16, 2024
1 parent 66d4a54 commit d9521cb
Show file tree
Hide file tree
Showing 29 changed files with 254 additions and 176 deletions.
9 changes: 3 additions & 6 deletions libs/base/src/ecflow/base/Openssl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ void Openssl::enable(std::string host, const std::string& port) {
}

void Openssl::enable_if_defined(std::string host, const std::string& port) {
if (auto ecf_ssl = getenv(ecf::environment::ECF_SSL); ecf_ssl) {
std::string ecf_ssl_env = ecf_ssl;
if (auto ecf_ssl = ecf::environment::fetch<std::string>(ecf::environment::ECF_SSL); ecf_ssl) {
std::string ecf_ssl_env = ecf_ssl.value();

if (host == Str::LOCALHOST())
host = Host().name();
Expand Down Expand Up @@ -160,10 +160,7 @@ std::string Openssl::get_password() const {
}

std::string Openssl::certificates_dir() const {
std::string home_path;
ecf::environment::get_environment_variable("HOME", home_path);
home_path += "/.ecflowrc/ssl/";
return home_path;
return ecf::environment::get<std::string>("HOME") + "/.ecflowrc/ssl/";
}

std::string Openssl::pem() const {
Expand Down
40 changes: 20 additions & 20 deletions libs/client/src/ecflow/client/ClientEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ std::string ClientEnvironment::toString() const {

std::string ClientEnvironment::hostSpecified() {
std::string specified_host;
ecf::environment::get_environment_variable(ecf::environment::ECF_HOST, specified_host);
ecf::environment::get(ecf::environment::ECF_HOST, specified_host);
return specified_host;
}

std::string ClientEnvironment::portSpecified() {
std::string specified_port = Str::DEFAULT_PORT_NUMBER();
ecf::environment::get_environment_variable(ecf::environment::ECF_PORT, specified_port);
ecf::environment::get(ecf::environment::ECF_PORT, specified_port);
return specified_port;
}

Expand All @@ -239,37 +239,37 @@ void ClientEnvironment::read_environment_variables() {
std::cout << "ClientEnvironment::read_environment_variables()\n";
#endif

ecf::environment::get_environment_variable(ecf::environment::ECF_NAME, task_path_);
ecf::environment::get(ecf::environment::ECF_NAME, task_path_);

ecf::environment::get_environment_variable(ecf::environment::ECF_PASS, jobs_password_);
ecf::environment::get(ecf::environment::ECF_PASS, jobs_password_);

ecf::environment::get_environment_variable(ecf::environment::ECF_TRYNO, task_try_num_);
ecf::environment::get(ecf::environment::ECF_TRYNO, task_try_num_);

ecf::environment::get_environment_variable("ECF_HOSTFILE", host_file_);
ecf::environment::get("ECF_HOSTFILE", host_file_);

ecf::environment::get_environment_variable(ecf::environment::ECF_RID, remote_id_);
ecf::environment::get(ecf::environment::ECF_RID, remote_id_);

ecf::environment::get_environment_variable("ECF_USER", user_name_);
ecf::environment::get("ECF_USER", user_name_);

ecf::environment::get_environment_variable("ECF_TIMEOUT", timeout_);
ecf::environment::get("ECF_TIMEOUT", timeout_);
timeout_ = timeout_ > MAX_TIMEOUT ? MAX_TIMEOUT : timeout_;
timeout_ = timeout_ < MIN_TIMEOUT ? MIN_TIMEOUT : timeout_;

ecf::environment::get_environment_variable("ECF_ZOMBIE_TIMEOUT", zombie_timeout_);
ecf::environment::get("ECF_ZOMBIE_TIMEOUT", zombie_timeout_);
zombie_timeout_ = (zombie_timeout_ > MAX_TIMEOUT) ? MAX_TIMEOUT : zombie_timeout_;
zombie_timeout_ = (zombie_timeout_ < MIN_TIMEOUT) ? MIN_TIMEOUT : zombie_timeout_;

ecf::environment::get_environment_variable("ECF_CONNECT_TIMEOUT", connect_timeout_);
ecf::environment::get("ECF_CONNECT_TIMEOUT", connect_timeout_);

ecf::environment::get_environment_variable("ECF_DENIED", denied_);
ecf::environment::get("ECF_DENIED", denied_);

ecf::environment::get_environment_variable("NO_ECF", no_ecf_);
ecf::environment::get("NO_ECF", no_ecf_);

ecf::environment::get_environment_variable("ECF_DEBUG_CLIENT", debug_);
ecf::environment::get("ECF_DEBUG_CLIENT", debug_);

if (auto var = getenv("ECF_DEBUG_LEVEL"); var) {
if (auto var = ecf::environment::fetch("ECF_DEBUG_LEVEL"); var) {
try {
Ecf::set_debug_level(ecf::convert_to<unsigned int>(var));
Ecf::set_debug_level(ecf::convert_to<unsigned int>(var.value()));
}
catch (...) {
throw std::runtime_error("The environment variable ECF_DEBUG_LEVEL must be an unsigned integer.");
Expand All @@ -284,8 +284,8 @@ void ClientEnvironment::read_environment_variables() {
port = host_vec_[0].second; // first entry is the config port
}

if (auto var = getenv(ecf::environment::ECF_PORT); var) {
port = var;
if (auto var = ecf::environment::fetch(ecf::environment::ECF_PORT); var) {
port = var.value();
host_vec_.clear(); // remove config settings, net effect is overriding
host_vec_.emplace_back(host, port);
}
Expand Down Expand Up @@ -381,8 +381,8 @@ const std::string& ClientEnvironment::get_password(const char* env, const std::s
return passwd_;
}

if (auto file = getenv(env); file) {
std::string user_passwd_file = file;
if (auto file = ecf::environment::fetch(env); file) {
std::string user_passwd_file = file.value();
// cout << " ClientEnvironment::get_password() ECF_CUSTOM_PASSWD " << user_passwd_file << "\n";
if (!user_passwd_file.empty() && fs::exists(user_passwd_file)) {
// cout << " ClientEnvironment::get_password() LOADING password file\n";
Expand Down
2 changes: 1 addition & 1 deletion libs/client/test/InvokeServer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class InvokeServer {
if (!msg.empty()) {
std::cout << msg << " port(" << port_ << ")";
#ifdef ECF_OPENSSL
if (ecf::environment::has_environment_variable(ecf::environment::ECF_SSL)) {
if (ecf::environment::has(ecf::environment::ECF_SSL)) {
std::cout << " (ssl)";
}
#endif
Expand Down
20 changes: 9 additions & 11 deletions libs/client/test/SCPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,23 @@ int SCPort::thePort_ = 3144;

std::string SCPort::next() {
bool debug = false;
ecf::environment::get_environment_variable("ECF_DEBUG_TEST", debug);
ecf::environment::get("ECF_DEBUG_TEST", debug);

if (debug) {
std::cout << "\nSCPort::next() : ";
}

// Allow parallel tests

if (auto ECF_FREE_PORT = getenv("ECF_FREE_PORT"); ECF_FREE_PORT) {
if (auto port = ecf::environment::fetch("ECF_FREE_PORT"); port) {
if (debug) {
std::cout << " seed_port=ECF_FREE_PORT=(" << ECF_FREE_PORT << ")";
std::cout << " seed_port=ECF_FREE_PORT=(" << port.value() << ")";
}
std::string port = ECF_FREE_PORT;
try {
thePort_ = ecf::convert_to<int>(port);
thePort_ = ecf::convert_to<int>(port.value());
}
catch (...) {
std::cout << "SCPort::next() ECF_FREE_PORT(" << ECF_FREE_PORT << ") not convertible to an integer\n";
std::cout << "SCPort::next() ECF_FREE_PORT(" << port.value() << ") not convertible to an integer\n";
}
}

Expand All @@ -61,13 +60,12 @@ std::string SCPort::next() {
}

if (host == Str::LOCALHOST()) {
if (auto ecf_port = getenv(ecf::environment::ECF_PORT); ecf_port) {
std::string port = ecf_port;
if (!port.empty()) {
if (auto port = ecf::environment::fetch(ecf::environment::ECF_PORT); port) {
if (!port.value().empty()) {
if (debug) {
std::cout << " ECF_PORT('" << ecf_port << "')\n";
std::cout << " ECF_PORT('" << port.value() << "')\n";
}
return port;
return port.value();
}
}
if (debug) {
Expand Down
6 changes: 3 additions & 3 deletions libs/client/test/TestServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ BOOST_AUTO_TEST_CASE(test_server_stress_test) {
ClientInvoker theClient(invokeServer.host(), invokeServer.port());
int load = 125;
#ifdef ECF_OPENSSL
if (ecf::environment::has_environment_variable(ecf::environment::ECF_SSL)) {
if (ecf::environment::has(ecf::environment::ECF_SSL)) {
load = 30;
}
#endif
Expand Down Expand Up @@ -359,7 +359,7 @@ BOOST_AUTO_TEST_CASE(test_server_group_stress_test) {

int load = 125;
#ifdef ECF_OPENSSL
if (ecf::environment::has_environment_variable(ecf::environment::ECF_SSL)) {
if (ecf::environment::has(ecf::environment::ECF_SSL)) {
load = 30;
}
#endif
Expand Down Expand Up @@ -411,7 +411,7 @@ BOOST_AUTO_TEST_CASE(test_server_stress_test_2) {
#endif

#ifdef ECF_OPENSSL
if (ecf::environment::has_environment_variable(ecf::environment::ECF_SSL)) {
if (ecf::environment::has(ecf::environment::ECF_SSL)) {
load = 10;
}
#endif
Expand Down
10 changes: 5 additions & 5 deletions libs/client/test/TestSinglePerf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ void time_load_and_downloads(ClientInvoker& theClient,
}

BOOST_AUTO_TEST_CASE(test_perf_for_large_defs) {
if (ecf::environment::has_environment_variable(ecf::environment::ECF_SSL)) {
if (ecf::environment::has(ecf::environment::ECF_SSL)) {
load_threshold_ms = 8000; // 4500;
begin_threshold_ms = 800; // 400;
sync_full_threshold_s = 4.5; // 2.6;
Expand All @@ -359,11 +359,11 @@ BOOST_AUTO_TEST_CASE(test_perf_for_large_defs) {
client_cmds_threshold_s = 950; // 8.5;
}

if (auto ecf_test_defs_dir = getenv("ECF_TEST_DEFS_DIR"); !ecf_test_defs_dir) {
if (auto ecf_test_defs_dir = ecf::environment::fetch("ECF_TEST_DEFS_DIR"); !ecf_test_defs_dir) {
std::cout << "Ignoring test! Environment variable ECF_TEST_DEFS_DIR is not defined\n";
}
else if (!fs::exists(ecf_test_defs_dir)) {
std::cout << "Ignoring test! Test definitions directory " << ecf_test_defs_dir << " does not exist\n";
else if (!fs::exists(ecf_test_defs_dir.value())) {
std::cout << "Ignoring test! Test definitions directory " << ecf_test_defs_dir.value() << " does not exist\n";
}
else {
/// This will remove checkpt and backup , to avoid server from loading it. (i.e from previous test)
Expand All @@ -372,7 +372,7 @@ BOOST_AUTO_TEST_CASE(test_perf_for_large_defs) {
"Server failed to start on " << invokeServer.host() << ":" << invokeServer.port());

ClientInvoker theClient(invokeServer.host(), invokeServer.port());
time_load_and_downloads(theClient, invokeServer.host(), invokeServer.port(), ecf_test_defs_dir);
time_load_and_downloads(theClient, invokeServer.host(), invokeServer.port(), ecf_test_defs_dir.value());
}
}

Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/ecflow/core/EcfPortLock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class EcfPortLock {
// We need the *SAME* location so that different process find the same file.
// When going across compiler the root_build_dir is not sufficient
std::string path = File::root_source_dir();
ecf::environment::get_environment_variable("ECF_PORT_LOCK_DIR", path);
ecf::environment::get("ECF_PORT_LOCK_DIR", path);

path += "/";
path += the_port;
Expand Down
Loading

0 comments on commit d9521cb

Please sign in to comment.