diff --git a/src/functions/__init__.py b/src/functions/__init__.py index cf87eb1..d7a6eaa 100644 --- a/src/functions/__init__.py +++ b/src/functions/__init__.py @@ -6,6 +6,7 @@ import time import logging from datetime import datetime from multiprocessing import Process +from typing import Final import requests from OpenSSL import crypto @@ -91,17 +92,17 @@ class ContainerEnv: class Functions: - HAPROXY_LOG = "HAPROXY" - EASYHAPROXY_LOG = "EASYHAPROXY" - CERTBOT_LOG = "CERTBOT" - INIT_LOG = "INIT" + HAPROXY_LOG: Final[str] = "HAPROXY" + EASYHAPROXY_LOG: Final[str] = "EASYHAPROXY" + CERTBOT_LOG: Final[str] = "CERTBOT" + INIT_LOG: Final[str] = "INIT" - TRACE = "TRACE" - DEBUG = "DEBUG" - INFO = "INFO" - WARN = "WARN" - ERROR = "ERROR" - FATAL = "FATAL" + TRACE: Final[str] = "TRACE" + DEBUG: Final[str] = "DEBUG" + INFO: Final[str] = "INFO" + WARN: Final[str] = "WARN" + ERROR: Final[str] = "ERROR" + FATAL: Final[str] = "FATAL" @staticmethod def setup_log(source): @@ -178,6 +179,9 @@ class Consts: class DaemonizeHAProxy: + HAPROXY_START: Final[str] = "start" + HAPROXY_RELOAD: Final[str] = "reload" + def __init__(self, custom_config_folder = None): self.process = None self.thread = None @@ -196,9 +200,9 @@ class DaemonizeHAProxy: def get_haproxy_command(self, action, pid_file="/run/haproxy.pid"): custom_config_files = "" if len(list(self.get_custom_config_files().keys())) != 0: - custom_config_files = "-f %s" % (self.custom_config_folder) + custom_config_files = "-f %s" % self.custom_config_folder - if action == "start": + if action == DaemonizeHAProxy.HAPROXY_START: return "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg %s -p %s -S /var/run/haproxy.sock" % (custom_config_files, pid_file) else: return_code, output = Functions().run_bash(loggerHaproxy, "cat %s" % pid_file, log_output=False) diff --git a/src/main.py b/src/main.py index 1009b7e..30412a3 100644 --- a/src/main.py +++ b/src/main.py @@ -25,7 +25,7 @@ def start(): old_haproxy = None haproxy = DaemonizeHAProxy() current_custom_config_files = haproxy.get_custom_config_files() - haproxy.haproxy("start") + haproxy.haproxy(DaemonizeHAProxy.HAPROXY_START) haproxy.sleep() certbot = Certbot(Consts.certs_certbot) @@ -47,7 +47,7 @@ def start(): old_haproxy = haproxy haproxy = DaemonizeHAProxy() current_custom_config_files = haproxy.get_custom_config_files() - haproxy.haproxy("reload") + haproxy.haproxy(DaemonizeHAProxy.HAPROXY_RELOAD) old_haproxy.terminate() except Exception as e: diff --git a/src/processor/__init__.py b/src/processor/__init__.py index c455ae2..ec038bf 100644 --- a/src/processor/__init__.py +++ b/src/processor/__init__.py @@ -1,5 +1,6 @@ import base64 import socket +from typing import Final import docker import yaml @@ -12,6 +13,11 @@ from functions import loggerEasyHaproxy class ProcessorInterface: + STATIC: Final[str] = "static" + DOCKER: Final[str] = "docker" + SWARM: Final[str] = "swarm" + KUBERNETES: Final[str] = "kubernetes" + static_file = Consts.easyhaproxy_config def __init__(self, filename=None): @@ -28,13 +34,13 @@ class ProcessorInterface: @staticmethod def factory(mode): - if mode == "static": + if mode == ProcessorInterface.STATIC: return Static(ProcessorInterface.static_file) - elif mode == "docker": + elif mode == ProcessorInterface.DOCKER: return Docker() - elif mode == "swarm": + elif mode == ProcessorInterface.SWARM: return Swarm() - elif mode == "kubernetes": + elif mode == ProcessorInterface.KUBERNETES: return Kubernetes() else: loggerEasyHaproxy.fatal("Expected mode to be 'static', 'docker', 'swarm' or 'kubernetes'. I got '%s'" % mode) diff --git a/src/tests/test_daemonize.py b/src/tests/test_daemonize.py index e5d87c9..dad002f 100644 --- a/src/tests/test_daemonize.py +++ b/src/tests/test_daemonize.py @@ -14,12 +14,12 @@ def test_daemonize_haproxy_check_config(): def test_daemonize_haproxy_get_haproxy_command_start(): daemon = DaemonizeHAProxy() - command = daemon.get_haproxy_command("start") + command = daemon.get_haproxy_command(DaemonizeHAProxy.HAPROXY_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") + command = daemon.get_haproxy_command(DaemonizeHAProxy.HAPROXY_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(): @@ -32,7 +32,7 @@ def test_daemonize_haproxy_check_config(): def test_daemonize_haproxy_get_haproxy_command_start(): daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') - command = daemon.get_haproxy_command("start") + command = daemon.get_haproxy_command(DaemonizeHAProxy.HAPROXY_START) assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -f %s -p /run/haproxy.pid -S /var/run/haproxy.sock" % (os.path.dirname(__file__) + "/fixtures") @@ -42,7 +42,7 @@ def test_daemonize_haproxy_get_haproxy_command_reload(): try: daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') - command = daemon.get_haproxy_command("reload", tmp_pid_file) + command = daemon.get_haproxy_command(DaemonizeHAProxy.HAPROXY_RELOAD, tmp_pid_file) assert command == "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg -f %s -p %s -x /var/run/haproxy.sock -sf %s" % (os.path.dirname(__file__) + "/fixtures", tmp_pid_file, 10) finally: os.remove(tmp_pid_file) diff --git a/src/tests/test_docker.py b/src/tests/test_docker.py index e31063e..340ee5b 100644 --- a/src/tests/test_docker.py +++ b/src/tests/test_docker.py @@ -64,7 +64,7 @@ def test_processor_docker(): os.environ['EASYHAPROXY_CERTBOT_EMAIL'] = 'docker@example.org' - static = ProcessorInterface.factory("docker") + static = ProcessorInterface.factory(ProcessorInterface.DOCKER) assert static.get_certbot_hosts() is None assert { diff --git a/src/tests/test_static.py b/src/tests/test_static.py index 7e6998c..91eedc5 100644 --- a/src/tests/test_static.py +++ b/src/tests/test_static.py @@ -6,7 +6,7 @@ from processor import ProcessorInterface def test_processor_static(): ProcessorInterface.static_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "./fixtures/static.yml") - static = ProcessorInterface.factory("static") + static = ProcessorInterface.factory(ProcessorInterface.STATIC) parsed_object = [ {