diff options
| author | peppepetra86 <giuseppe.petralia@canonical.com> | 2020-02-20 16:21:03 +0100 |
|---|---|---|
| committer | peppepetra86 <giuseppe.petralia@canonical.com> | 2020-02-20 16:21:03 +0100 |
| commit | 43eecbefd78ca66375d5dee44aeab8ea63f863de (patch) | |
| tree | a8fb6a99927f992ac6af807ca8cc59bca4f98573 | |
| parent | ba7a4e0b188d871815abf4ce617f6ff9cce14960 (diff) | |
Refactor bind_ip selection and improve param description.
| -rw-r--r-- | config.yaml | 9 | ||||
| -rwxr-xr-x | hooks/hooks.py | 34 |
2 files changed, 23 insertions, 20 deletions
diff --git a/config.yaml b/config.yaml index c8e668b..263867f 100644 --- a/config.yaml +++ b/config.yaml @@ -28,12 +28,9 @@ options: type: string description: > IP address that mongodb should listen for connections: - - values as "all" and "0.0.0.0" will be treated the same and mongo will - listen on any configured interface - - values as "localhost" and "127.0.0.1" will be treated the same and mongo - will listen only on localhost - - if a given ip is set, it will be used only if it is configured on - on the unit, otherwise the default "0.0.0.0" will be used. + - "0.0.0.0" (or "all", for backward compatibility) will listen on any configured interface (default value). + - "127.0.0.1" (or "localhost") will listen on localhost + - Any other value needs to map to a unit IP (if multiple units exist, the default value is used). port: default: 27017 type: int diff --git a/hooks/hooks.py b/hooks/hooks.py index 821add8..e2bc2b5 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -5,6 +5,7 @@ Created on Aug 1, 2012 @author: negronjl ''' +import collections import commands import distutils import json @@ -233,10 +234,24 @@ def is_valid_ip(bind_ip): return False -def is_localhost(bind_ip): - if bind_ip == "127.0.0.1" or bind_ip == "localhost": - return True - return False +def choose_bind_ip(bind_ip): + default_value, localhost = "0.0.0.0", "127.0.0.1" + known_values = collections.defaultdict(lambda: False) + known_values.update({ + "0.0.0.0": default_value, + "all": default_value, + "localhost": localhost, + "127.0.0.1": localhost, + }) + + if known_values[bind_ip]: + return known_values[bind_ip] + + if not is_valid_ip(bind_ip): + juju_log("bind_ip not valid. Using default '0.0.0.0' instead") + return default_value + + return bind_ip class MasterNotFoundException(Exception): @@ -287,16 +302,7 @@ def mongodb_conf(config_data=None): config.append("") # bind_ip - bind_ip = "0.0.0.0" # Default value - - if is_localhost(config_data["bind_ip"]): - bind_ip = "127.0.0.1" - elif config_data["bind_ip"] != "all" and config_data["bind_ip"] != "0.0.0.0": - if is_valid_ip(config_data["bind_ip"]): - bind_ip = config_data["bind_ip"] - else: - juju_log("bind_ip not valid. Using default '0.0.0.0' instead") - + bind_ip = choose_bind_ip(config_data["bind_ip"]) config.append("bind_ip = %s" % bind_ip) config.append("") |
