summaryrefslogtreecommitdiff
diff options
authorMert Kirpici <mert.kirpici@canonical.com>2022-08-12 08:28:52 +0000
committerCanonical IS Mergebot <canonical-is-mergebot@canonical.com>2022-08-12 08:28:52 +0000
commitd0ccbd721106e5b40460a06685b36b19357b8ee4 (patch)
tree03a7b4c926e42694faf2e086d269f32c7338ad77
parent8b17446d13eb73b3cd593d95ab3b28a755f8ddb2 (diff)
parenta227782592bfd54f44d9329dcd6dce79aabb1705 (diff)
hooks.py: refactor pid and config file getting code22.10
Reviewed-on: https://code.launchpad.net/~mertkirpici/charm-mongodb/+git/charm-mongodb/+merge/427994 Reviewed-by: Eric Chen <eric.chen@canonical.com>
-rwxr-xr-xhooks/hooks.py39
1 files 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