From a227782592bfd54f44d9329dcd6dce79aabb1705 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mert=20K=C4=B1rp=C4=B1c=C4=B1?= Date: Fri, 5 Aug 2022 19:55:28 +0300 Subject: hooks.py: refactor pid and config file getting code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduce two utility functions for getting the pid and config files depending on percona or not. Signed-off-by: Mert Kırpıcı --- hooks/hooks.py | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/hooks/hooks.py b/hooks/hooks.py index ab0e77f..322498f 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -160,6 +160,16 @@ def is_percona_source(): return False +def get_pid_file(): + """Return the path for the pid file.""" + return percona_mongodb_pid_file if is_percona_source() else default_mongodb_pid_file + + +def get_config_file(): + """Return the path for the config file.""" + return percona_mongodb_config if is_percona_source() else default_mongodb_config + + def is_bionic_or_greater(): current_version = distro.linux_distribution()[1] if distutils.version.LooseVersion( @@ -240,11 +250,7 @@ def update_config_file(config_data): mongodb_config = mongodb_conf(config_data) else: mongodb_config = mongodb_conf_yaml(config_data) - if is_percona_source(): - retval = update_file(percona_mongodb_config, mongodb_config) - else: - retval = update_file(default_mongodb_config, mongodb_config) - return retval + return update_file(get_config_file(), mongodb_config) def process_check(pid=None): @@ -511,11 +517,10 @@ def mongodb_conf_yaml(config_data=None): # noqa: C901 return None # https://docs.mongodb.com/v4.2/reference/configuration-options/#file-format config = {} + mongodb_pid_file = get_pid_file() if is_percona_source(): - mongodb_pid_file = percona_mongodb_pid_file user_group = "mongod:mongod" else: - mongodb_pid_file = default_mongodb_pid_file user_group = "mongodb:mongodb" setup_directory_structure_and_permissions(user_group, config_data) @@ -627,10 +632,7 @@ def get_current_mongo_config(): :returns dict: key/value pairs of the configuration file. """ results = {} - if is_percona_source(): - mongodb_config = "/etc/mongod.conf" - else: - mongodb_config = default_mongodb_config + mongodb_config = get_config_file() with open(mongodb_config, "r") as f: for line in f: @@ -1006,10 +1008,7 @@ def enable_configsvr( def mongos_status(wait_for=default_wait_for, max_tries=default_max_tries): config_data = config() - if is_percona_source(): - mongodb_pid_file = percona_mongodb_pid_file - else: - mongodb_pid_file = default_mongodb_pid_file + mongodb_pid_file = get_pid_file() current_try = 0 while ( (process_check_pidfile(mongodb_pid_file) != (None, None)) @@ -1037,10 +1036,7 @@ def mongos_ready(wait_for=default_wait_for, max_tries=default_max_tries): def disable_mongos(port=None): - if is_percona_source(): - mongodb_pid_file = percona_mongodb_pid_file - else: - mongodb_pid_file = default_mongodb_pid_file + mongodb_pid_file = get_pid_file() if port is None: juju_log("disable_mongos: port not defined") return False @@ -1085,10 +1081,7 @@ def enable_mongos( ) cmd_line = "mongos" cmd_line += " --logpath %s" % config_data["mongos_logpath"] - if is_percona_source(): - cmd_line += " --pidfilepath " + percona_mongodb_pid_file - else: - cmd_line += " --pidfilepath " + default_mongodb_pid_file + cmd_line += " --pidfilepath " + get_pid_file() cmd_line += " --port %d" % config_data["mongos_port"] cmd_line += " --fork" # Note(aluria): --configdb used to have a list of comma-separated -- cgit v1.2.3