1
0
Fork 0

Refresh haproxy after letsencrypt

This commit is contained in:
Joao M 2022-09-02 21:22:17 +00:00
parent f8046670a9
commit 3a3d5204a3
2 changed files with 16 additions and 4 deletions

View file

@ -4,6 +4,8 @@ import subprocess
import shlex import shlex
import time import time
import os import os
import re
import time
class Functions: class Functions:
HAPROXY_LOG="HAPROXY" HAPROXY_LOG="HAPROXY"
@ -105,6 +107,7 @@ class DaemonizeHAProxy:
def __init__(self): def __init__(self):
self.process = None self.process = None
self.thread = None self.thread = None
self.sleep_secs = None
def haproxy(self, action): def haproxy(self, action):
if action == "start": if action == "start":
@ -160,6 +163,15 @@ class DaemonizeHAProxy:
self.process.terminate() self.process.terminate()
self.thread.terminate() self.thread.terminate()
def sleep(self):
if self.sleep_secs is None:
try:
self.sleep_secs = int(os.getenv("EASYHAPROXY_REFRESH_CONF", "10"))
except ValueError:
self.sleep_secs = 10
time.sleep(self.sleep_secs)
class Certbot: class Certbot:
def __init__(self, certs, email): def __init__(self, certs, email):

View file

@ -1,7 +1,6 @@
from functions import Functions, DaemonizeHAProxy, Certbot, Consts from functions import Functions, DaemonizeHAProxy, Certbot, Consts
from processor import ProcessorInterface from processor import ProcessorInterface
import os import os
import time
from deepdiff import DeepDiff from deepdiff import DeepDiff
def start(): def start():
@ -21,18 +20,18 @@ def start():
old_haproxy = None old_haproxy = None
haproxy = DaemonizeHAProxy() haproxy = DaemonizeHAProxy()
haproxy.haproxy("start") haproxy.haproxy("start")
haproxy.sleep()
certbot = Certbot(Consts.certs_letsencrypt, os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL")) certbot = Certbot(Consts.certs_letsencrypt, os.getenv("EASYHAPROXY_LETSENCRYPT_EMAIL"))
while True: while True:
time.sleep(10)
if old_haproxy is not None: if old_haproxy is not None:
old_haproxy.kill() old_haproxy.kill()
old_haproxy = None old_haproxy = None
try: try:
old_parsed = processor_obj.get_parsed_object() old_parsed = processor_obj.get_parsed_object()
processor_obj.refresh() processor_obj.refresh()
if 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():
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)
@ -44,10 +43,11 @@ def start():
haproxy.haproxy("reload") haproxy.haproxy("reload")
old_haproxy.terminate() old_haproxy.terminate()
certbot.check_certificates(letsencrypt_certs_found)
except Exception as e: except Exception as e:
Functions.log(Functions.EASYHAPROXY_LOG, Functions.FATAL, "Err: %s" % (e)) Functions.log(Functions.EASYHAPROXY_LOG, Functions.FATAL, "Err: %s" % (e))
Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'Heartbeat') Functions.log(Functions.EASYHAPROXY_LOG, Functions.DEBUG, 'Heartbeat')
haproxy.sleep()