Load custom config files
This commit is contained in:
parent
7b61be60d8
commit
c2beff08fc
5 changed files with 74 additions and 7 deletions
|
|
@ -100,21 +100,19 @@ class Functions:
|
||||||
class Consts:
|
class Consts:
|
||||||
easyhaproxy_config = "/etc/haproxy/static/config.yml"
|
easyhaproxy_config = "/etc/haproxy/static/config.yml"
|
||||||
haproxy_config = "/etc/haproxy/haproxy.cfg"
|
haproxy_config = "/etc/haproxy/haproxy.cfg"
|
||||||
|
custom_config_folder = "/etc/haproxy/conf.d"
|
||||||
certs_letsencrypt = "/certs/letsencrypt"
|
certs_letsencrypt = "/certs/letsencrypt"
|
||||||
certs_haproxy = "/certs/haproxy"
|
certs_haproxy = "/certs/haproxy"
|
||||||
|
|
||||||
class DaemonizeHAProxy:
|
class DaemonizeHAProxy:
|
||||||
def __init__(self):
|
def __init__(self, custom_config_folder = None):
|
||||||
self.process = None
|
self.process = None
|
||||||
self.thread = None
|
self.thread = None
|
||||||
self.sleep_secs = 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):
|
def haproxy(self, action):
|
||||||
if action == "start":
|
self.__prepare(self.get_haproxy_command(action))
|
||||||
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))
|
|
||||||
|
|
||||||
if self.process is None:
|
if self.process is None:
|
||||||
return
|
return
|
||||||
|
|
@ -122,6 +120,15 @@ class DaemonizeHAProxy:
|
||||||
self.thread = Process(target=self.__start, args=())
|
self.thread = Process(target=self.__start, args=())
|
||||||
self.thread.start()
|
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):
|
def __prepare(self, command):
|
||||||
source = Functions.HAPROXY_LOG
|
source = Functions.HAPROXY_LOG
|
||||||
if not isinstance(command, (list, tuple)):
|
if not isinstance(command, (list, tuple)):
|
||||||
|
|
@ -172,6 +179,16 @@ class DaemonizeHAProxy:
|
||||||
|
|
||||||
time.sleep(self.sleep_secs)
|
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:
|
class Certbot:
|
||||||
def __init__(self, certs, email, test_server):
|
def __init__(self, certs, email, test_server):
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ def start():
|
||||||
|
|
||||||
old_haproxy = None
|
old_haproxy = None
|
||||||
haproxy = DaemonizeHAProxy()
|
haproxy = DaemonizeHAProxy()
|
||||||
|
current_custom_config_files = haproxy.get_custom_config_files()
|
||||||
haproxy.haproxy("start")
|
haproxy.haproxy("start")
|
||||||
haproxy.sleep()
|
haproxy.sleep()
|
||||||
|
|
||||||
|
|
@ -31,7 +32,7 @@ def start():
|
||||||
try:
|
try:
|
||||||
old_parsed = processor_obj.get_parsed_object()
|
old_parsed = processor_obj.get_parsed_object()
|
||||||
processor_obj.refresh()
|
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.DEBUG, 'New configuration found. Reloading...')
|
||||||
Functions.log(Functions.EASYHAPROXY_LOG, Functions.TRACE, 'Object Found: %s' % (processor_obj.get_parsed_object()))
|
Functions.log(Functions.EASYHAPROXY_LOG, Functions.TRACE, 'Object Found: %s' % (processor_obj.get_parsed_object()))
|
||||||
processor_obj.save_config(Consts.haproxy_config)
|
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
|
Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'Found hosts: %s' % ", ".join(processor_obj.get_hosts())) # Needs to after save_config
|
||||||
old_haproxy = haproxy
|
old_haproxy = haproxy
|
||||||
haproxy = DaemonizeHAProxy()
|
haproxy = DaemonizeHAProxy()
|
||||||
|
current_custom_config_files = haproxy.get_custom_config_files()
|
||||||
haproxy.haproxy("reload")
|
haproxy.haproxy("reload")
|
||||||
old_haproxy.terminate()
|
old_haproxy.terminate()
|
||||||
|
|
||||||
|
|
|
||||||
2
src/tests/fixtures/00_haproxy.cfg
vendored
Normal file
2
src/tests/fixtures/00_haproxy.cfg
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
global
|
||||||
|
maxconn 4000
|
||||||
2
src/tests/fixtures/10_haproxy.cfg
vendored
Normal file
2
src/tests/fixtures/10_haproxy.cfg
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
global
|
||||||
|
maxconn 5000
|
||||||
44
src/tests/test_daemonize.py
Normal file
44
src/tests/test_daemonize.py
Normal file
|
|
@ -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")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue