Skip to content
Snippets Groups Projects
Commit 178de835 authored by Stéphane Diemer's avatar Stéphane Diemer
Browse files

Restored configuration cache, broken by commit c32f9657

parent 1cf8c911
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,8 @@ DEFAULT_CONF_PATH = "global-conf.sh" ...@@ -20,6 +20,8 @@ DEFAULT_CONF_PATH = "global-conf.sh"
AUTO_CONF_PATH = "auto-generated-conf.sh" AUTO_CONF_PATH = "auto-generated-conf.sh"
CONF_PATH = "conf.sh" CONF_PATH = "conf.sh"
_conf_cache = None
def log(text: str, error: bool = False): def log(text: str, error: bool = False):
"""Output log message to stout or stderr. """Output log message to stout or stderr.
...@@ -27,7 +29,7 @@ def log(text: str, error: bool = False): ...@@ -27,7 +29,7 @@ def log(text: str, error: bool = False):
:param text: Message to log :param text: Message to log
:type text: str :type text: str
:param error: Wether it should output to stderr or not, defaults to False :param error: Wether it should output to stderr or not, defaults to False
:param error: bool, optional :type error: bool, optional
""" """
fo = sys.stderr if error else sys.stdout fo = sys.stderr if error else sys.stdout
...@@ -93,9 +95,9 @@ def exec_cmd(cmd: Any, log_output: bool = True, get_output: bool = True) -> tupl ...@@ -93,9 +95,9 @@ def exec_cmd(cmd: Any, log_output: bool = True, get_output: bool = True) -> tupl
:param cmd: Command to run :param cmd: Command to run
:type cmd: Any :type cmd: Any
:param log_output: Wether to log output or not, defaults to True :param log_output: Wether to log output or not, defaults to True
:param log_output: bool, optional :type log_output: bool, optional
:param get_output: Wether to return output or not, defaults to True :param get_output: Wether to return output or not, defaults to True
:param get_output: bool, optional :type get_output: bool, optional
:return: Return code and output :return: Return code and output
:rtype: tuple :rtype: tuple
""" """
...@@ -133,7 +135,7 @@ def check_cmd(cmd: Any, log_output: bool = False) -> int: ...@@ -133,7 +135,7 @@ def check_cmd(cmd: Any, log_output: bool = False) -> int:
:param cmd: Command to execute :param cmd: Command to execute
:type cmd: Any :type cmd: Any
:param log_output: Wether to log output or not, defaults to False :param log_output: Wether to log output or not, defaults to False
:param log_output: bool, optional :type log_output: bool, optional
:return: Return code :return: Return code
:rtype: int :rtype: int
""" """
...@@ -192,6 +194,8 @@ def load_conf() -> dict: ...@@ -192,6 +194,8 @@ def load_conf() -> dict:
log("Perhaps you forget to change the configuration.") log("Perhaps you forget to change the configuration.")
log("Path of configuration file: %s" % str(Path(base_dir, CONF_PATH))) log("Path of configuration file: %s" % str(Path(base_dir, CONF_PATH)))
log("Perhaps you want to quit this script to change the configuration?\n") log("Perhaps you want to quit this script to change the configuration?\n")
global _conf_cache
_conf_cache = conf
return conf return conf
...@@ -201,14 +205,16 @@ def get_conf(name: str, default: str = None) -> str: ...@@ -201,14 +205,16 @@ def get_conf(name: str, default: str = None) -> str:
:param name: Parameter name :param name: Parameter name
:type name: str :type name: str
:param default: Default parameter value, defaults to None :param default: Default parameter value, defaults to None
:param default: str, optional :type default: str, optional
:return: Parameter value :return: Parameter value
:rtype: str :rtype: str
""" """
conf = load_conf() global _conf_cache
if _conf_cache is None:
load_conf()
return conf.get(name, default) return _conf_cache.get(name, default)
def set_conf(key: str, value: str, override: bool = False) -> bool: def set_conf(key: str, value: str, override: bool = False) -> bool:
...@@ -219,7 +225,7 @@ def set_conf(key: str, value: str, override: bool = False) -> bool: ...@@ -219,7 +225,7 @@ def set_conf(key: str, value: str, override: bool = False) -> bool:
:param value: Option value :param value: Option value
:type value: str :type value: str
:param override: Wether to override the option if it already exists, defaults to False :param override: Wether to override the option if it already exists, defaults to False
:param override: bool, optional :type override: bool, optional
:return: True if the option have changed, False otherwise :return: True if the option have changed, False otherwise
:rtype: bool :rtype: bool
""" """
...@@ -238,15 +244,18 @@ def set_conf(key: str, value: str, override: bool = False) -> bool: ...@@ -238,15 +244,18 @@ def set_conf(key: str, value: str, override: bool = False) -> bool:
conf = regex.sub("{}='{}'".format(key.upper(), str(value)), conf) conf = regex.sub("{}='{}'".format(key.upper(), str(value)), conf)
with open(conf_path, "w") as conf_fh: with open(conf_path, "w") as conf_fh:
conf_fh.write(conf) conf_fh.write(conf)
return True success = True
elif not match: elif not match:
# add option # add option
with open(conf_path, "a") as conf_fh: with open(conf_path, "a") as conf_fh:
conf_fh.write("\n{}='{}'\n".format(key.upper(), str(value))) conf_fh.write("\n{}='{}'\n".format(key.upper(), str(value)))
return True success = True
else: else:
# no match or no override # no match or no override
return False success = False
# reload conf
load_conf()
return success
def run_commands(cmds: list): def run_commands(cmds: list):
...@@ -404,11 +413,11 @@ def mkcert( ...@@ -404,11 +413,11 @@ def mkcert(
:param domains: Domains for which the certificates will be self-signed :param domains: Domains for which the certificates will be self-signed
:type domains: list :type domains: list
:param ecc: Wether to use Elliptic Curve cryptography or not, defaults to True, if Fasle RSA is used :param ecc: Wether to use Elliptic Curve cryptography or not, defaults to True, if Fasle RSA is used
:param ecc: bool, optional :type ecc: bool, optional
:param days: Validity lifetime of the certificate, defaults to 3650 :param days: Validity lifetime of the certificate, defaults to 3650
:param days: int, optional :type days: int, optional
:param config_tpl: OpenSSL config file template, defaults to OPENSSL_CONFIG_TEMPLATE :param config_tpl: OpenSSL config file template, defaults to OPENSSL_CONFIG_TEMPLATE
:param config_tpl: str, optional :type config_tpl: str, optional
""" """
# create certs dir # create certs dir
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment