1
0
Fork 0

creating constants

This commit is contained in:
Joao Gilberto Magalhaes 2024-11-18 09:13:11 -06:00
parent 6df8e39bb1
commit 9d97d568ec
6 changed files with 34 additions and 24 deletions

View file

@ -6,6 +6,7 @@ import time
import logging import logging
from datetime import datetime from datetime import datetime
from multiprocessing import Process from multiprocessing import Process
from typing import Final
import requests import requests
from OpenSSL import crypto from OpenSSL import crypto
@ -91,17 +92,17 @@ class ContainerEnv:
class Functions: class Functions:
HAPROXY_LOG = "HAPROXY" HAPROXY_LOG: Final[str] = "HAPROXY"
EASYHAPROXY_LOG = "EASYHAPROXY" EASYHAPROXY_LOG: Final[str] = "EASYHAPROXY"
CERTBOT_LOG = "CERTBOT" CERTBOT_LOG: Final[str] = "CERTBOT"
INIT_LOG = "INIT" INIT_LOG: Final[str] = "INIT"
TRACE = "TRACE" TRACE: Final[str] = "TRACE"
DEBUG = "DEBUG" DEBUG: Final[str] = "DEBUG"
INFO = "INFO" INFO: Final[str] = "INFO"
WARN = "WARN" WARN: Final[str] = "WARN"
ERROR = "ERROR" ERROR: Final[str] = "ERROR"
FATAL = "FATAL" FATAL: Final[str] = "FATAL"
@staticmethod @staticmethod
def setup_log(source): def setup_log(source):
@ -178,6 +179,9 @@ class Consts:
class DaemonizeHAProxy: class DaemonizeHAProxy:
HAPROXY_START: Final[str] = "start"
HAPROXY_RELOAD: Final[str] = "reload"
def __init__(self, custom_config_folder = None): def __init__(self, custom_config_folder = None):
self.process = None self.process = None
self.thread = None self.thread = None
@ -196,9 +200,9 @@ class DaemonizeHAProxy:
def get_haproxy_command(self, action, pid_file="/run/haproxy.pid"): def get_haproxy_command(self, action, pid_file="/run/haproxy.pid"):
custom_config_files = "" custom_config_files = ""
if len(list(self.get_custom_config_files().keys())) != 0: 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) return "/usr/sbin/haproxy -W -f /etc/haproxy/haproxy.cfg %s -p %s -S /var/run/haproxy.sock" % (custom_config_files, pid_file)
else: else:
return_code, output = Functions().run_bash(loggerHaproxy, "cat %s" % pid_file, log_output=False) return_code, output = Functions().run_bash(loggerHaproxy, "cat %s" % pid_file, log_output=False)

View file

@ -25,7 +25,7 @@ def start():
old_haproxy = None old_haproxy = None
haproxy = DaemonizeHAProxy() haproxy = DaemonizeHAProxy()
current_custom_config_files = haproxy.get_custom_config_files() current_custom_config_files = haproxy.get_custom_config_files()
haproxy.haproxy("start") haproxy.haproxy(DaemonizeHAProxy.HAPROXY_START)
haproxy.sleep() haproxy.sleep()
certbot = Certbot(Consts.certs_certbot) certbot = Certbot(Consts.certs_certbot)
@ -47,7 +47,7 @@ def start():
old_haproxy = haproxy old_haproxy = haproxy
haproxy = DaemonizeHAProxy() haproxy = DaemonizeHAProxy()
current_custom_config_files = haproxy.get_custom_config_files() current_custom_config_files = haproxy.get_custom_config_files()
haproxy.haproxy("reload") haproxy.haproxy(DaemonizeHAProxy.HAPROXY_RELOAD)
old_haproxy.terminate() old_haproxy.terminate()
except Exception as e: except Exception as e:

View file

@ -1,5 +1,6 @@
import base64 import base64
import socket import socket
from typing import Final
import docker import docker
import yaml import yaml
@ -12,6 +13,11 @@ from functions import loggerEasyHaproxy
class ProcessorInterface: class ProcessorInterface:
STATIC: Final[str] = "static"
DOCKER: Final[str] = "docker"
SWARM: Final[str] = "swarm"
KUBERNETES: Final[str] = "kubernetes"
static_file = Consts.easyhaproxy_config static_file = Consts.easyhaproxy_config
def __init__(self, filename=None): def __init__(self, filename=None):
@ -28,13 +34,13 @@ class ProcessorInterface:
@staticmethod @staticmethod
def factory(mode): def factory(mode):
if mode == "static": if mode == ProcessorInterface.STATIC:
return Static(ProcessorInterface.static_file) return Static(ProcessorInterface.static_file)
elif mode == "docker": elif mode == ProcessorInterface.DOCKER:
return Docker() return Docker()
elif mode == "swarm": elif mode == ProcessorInterface.SWARM:
return Swarm() return Swarm()
elif mode == "kubernetes": elif mode == ProcessorInterface.KUBERNETES:
return Kubernetes() return Kubernetes()
else: else:
loggerEasyHaproxy.fatal("Expected mode to be 'static', 'docker', 'swarm' or 'kubernetes'. I got '%s'" % mode) loggerEasyHaproxy.fatal("Expected mode to be 'static', 'docker', 'swarm' or 'kubernetes'. I got '%s'" % mode)

View file

@ -14,12 +14,12 @@ def test_daemonize_haproxy_check_config():
def test_daemonize_haproxy_get_haproxy_command_start(): def test_daemonize_haproxy_get_haproxy_command_start():
daemon = DaemonizeHAProxy() 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" 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(): def test_daemonize_haproxy_get_haproxy_command_reload():
daemon = DaemonizeHAProxy() 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 " 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(): def test_daemonize_haproxy_check_config():
@ -32,7 +32,7 @@ def test_daemonize_haproxy_check_config():
def test_daemonize_haproxy_get_haproxy_command_start(): def test_daemonize_haproxy_get_haproxy_command_start():
daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') 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") 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: try:
daemon = DaemonizeHAProxy(os.path.abspath(os.path.dirname(__file__)) + '/fixtures') 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) 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: finally:
os.remove(tmp_pid_file) os.remove(tmp_pid_file)

View file

@ -64,7 +64,7 @@ def test_processor_docker():
os.environ['EASYHAPROXY_CERTBOT_EMAIL'] = 'docker@example.org' 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 static.get_certbot_hosts() is None
assert { assert {

View file

@ -6,7 +6,7 @@ from processor import ProcessorInterface
def test_processor_static(): def test_processor_static():
ProcessorInterface.static_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), "./fixtures/static.yml") 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 = [ parsed_object = [
{ {