diff --git a/src/functions/__init__.py b/src/functions/__init__.py index d174dcf..7f0a331 100644 --- a/src/functions/__init__.py +++ b/src/functions/__init__.py @@ -100,21 +100,19 @@ class Functions: class Consts: easyhaproxy_config = "/etc/haproxy/static/config.yml" haproxy_config = "/etc/haproxy/haproxy.cfg" + custom_config_folder = "/etc/haproxy/conf.d" certs_letsencrypt = "/certs/letsencrypt" certs_haproxy = "/certs/haproxy" class DaemonizeHAProxy: - def __init__(self): + def __init__(self, custom_config_folder = None): self.process = None self.thread = None self.sleep_secs = None + self.custom_config_folder = custom_config_folder if custom_config_folder is not None else Consts.custom_config_folder def haproxy(self, action): - if action == "start": - self.__prepare("/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock") - else: - pid = "".join(Functions().run_bash(Functions.HAPROXY_LOG, "cat /run/haproxy.pid", log_output=False)) - self.__prepare("/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf %s" % (pid)) + self.__prepare(self.get_haproxy_command(action)) if self.process is None: return @@ -122,6 +120,15 @@ class DaemonizeHAProxy: self.thread = Process(target=self.__start, args=()) self.thread.start() + def get_haproxy_command(self, action): + custom_config_files = " ".join(["-f %s" % (file) for file in self.get_custom_config_files()]) + + if action == "start": + return "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg %s -p /run/haproxy.pid -S /var/run/haproxy.sock" % (custom_config_files) + else: + pid = "".join(Functions().run_bash(Functions.HAPROXY_LOG, "cat /run/haproxy.pid", log_output=False)) + return "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg %s -p /run/haproxy.pid -x /var/run/haproxy.sock -sf %s" % (custom_config_files, pid) + def __prepare(self, command): source = Functions.HAPROXY_LOG if not isinstance(command, (list, tuple)): @@ -172,6 +179,16 @@ class DaemonizeHAProxy: time.sleep(self.sleep_secs) + def get_custom_config_files(self): + if not os.path.exists(self.custom_config_folder): + return {} + + files = {} + for file in os.listdir(self.custom_config_folder): + if file.endswith(".cfg"): + files[os.path.join(self.custom_config_folder, file)] = os.path.getmtime(os.path.join(self.custom_config_folder, file)) + return dict(sorted(files.items(), key=lambda t: t[0])) + class Certbot: def __init__(self, certs, email, test_server): diff --git a/src/main.py b/src/main.py index 34e29d8..48440e1 100644 --- a/src/main.py +++ b/src/main.py @@ -19,6 +19,7 @@ def start(): old_haproxy = None haproxy = DaemonizeHAProxy() + current_custom_config_files = haproxy.get_custom_config_files() haproxy.haproxy("start") haproxy.sleep() @@ -31,7 +32,7 @@ def start(): try: old_parsed = processor_obj.get_parsed_object() processor_obj.refresh() - if certbot.check_certificates(letsencrypt_certs_found) or DeepDiff(old_parsed, processor_obj.get_parsed_object()) != {} or not haproxy.is_alive(): + if certbot.check_certificates(letsencrypt_certs_found) or DeepDiff(old_parsed, processor_obj.get_parsed_object()) != {} or not haproxy.is_alive() or DeepDiff(current_custom_config_files, haproxy.get_custom_config_files()) != {}: Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'New configuration found. Reloading...') Functions.log(Functions.EASYHAPROXY_LOG, Functions.TRACE, 'Object Found: %s' % (processor_obj.get_parsed_object())) processor_obj.save_config(Consts.haproxy_config) @@ -40,6 +41,7 @@ def start(): Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'Found hosts: %s' % ", ".join(processor_obj.get_hosts())) # Needs to after save_config old_haproxy = haproxy haproxy = DaemonizeHAProxy() + current_custom_config_files = haproxy.get_custom_config_files() haproxy.haproxy("reload") old_haproxy.terminate() diff --git a/src/tests/fixtures/00_haproxy.cfg b/src/tests/fixtures/00_haproxy.cfg new file mode 100644 index 0000000..eaec319 --- /dev/null +++ b/src/tests/fixtures/00_haproxy.cfg @@ -0,0 +1,2 @@ +global + maxconn 4000 \ No newline at end of file diff --git a/src/tests/fixtures/10_haproxy.cfg b/src/tests/fixtures/10_haproxy.cfg new file mode 100644 index 0000000..9a855a9 --- /dev/null +++ b/src/tests/fixtures/10_haproxy.cfg @@ -0,0 +1,2 @@ +global + maxconn 5000 \ No newline at end of file diff --git a/src/tests/test_daemonize.py b/src/tests/test_daemonize.py new file mode 100644 index 0000000..5b8baa3 --- /dev/null +++ b/src/tests/test_daemonize.py @@ -0,0 +1,44 @@ +import json +import pytest +import os +import re +import random +import string +from functions import DaemonizeHAProxy + +def test_daemonize_haproxy(): + daemon = DaemonizeHAProxy() + assert daemon is not None + +def test_daemonize_haproxy_check_config(): + daemon = DaemonizeHAProxy() + filed = daemon.get_custom_config_files() + assert filed == {} + +def test_daemonize_haproxy_get_haproxy_command_start(): + daemon = DaemonizeHAProxy() + command = daemon.get_haproxy_command("start") + assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock" + +def test_daemonize_haproxy_get_haproxy_command_reload(): + daemon = DaemonizeHAProxy() + command = daemon.get_haproxy_command("reload") + assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf " + +def test_daemonize_haproxy_check_config(): + daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') + filed = daemon.get_custom_config_files() + assert filed == { + os.path.dirname(__file__) + "/fixtures/00_haproxy.cfg": os.path.getmtime(os.path.dirname(__file__) + "/fixtures/00_haproxy.cfg"), + os.path.dirname(__file__) + "/fixtures/10_haproxy.cfg": os.path.getmtime(os.path.dirname(__file__) + "/fixtures/10_haproxy.cfg") + } + +def test_daemonize_haproxy_get_haproxy_command_start(): + daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') + command = daemon.get_haproxy_command("start") + assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -f %s/00_haproxy.cfg -f %s/10_haproxy.cfg -p /run/haproxy.pid -S /var/run/haproxy.sock" % (os.path.dirname(__file__) + "/fixtures", os.path.dirname(__file__) + "/fixtures") + +def test_daemonize_haproxy_get_haproxy_command_reload(): + daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') + command = daemon.get_haproxy_command("reload") + assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -f %s/00_haproxy.cfg -f %s/10_haproxy.cfg -p /run/haproxy.pid -x /var/run/haproxy.sock -sf " % (os.path.dirname(__file__) + "/fixtures", os.path.dirname(__file__) + "/fixtures")