From ef0ab2752bcdd4261b3e03207ee9137c17f8f8a6 Mon Sep 17 00:00:00 2001 From: mdivecky Date: Thu, 26 Oct 2023 16:19:40 +0200 Subject: [PATCH] add rsync --delete for cleanup --- n_gen.py | 25 ++++++++++++++++--------- n_ssl.py | 16 ++++++++-------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/n_gen.py b/n_gen.py index fe87d03..d6d89e7 100755 --- a/n_gen.py +++ b/n_gen.py @@ -12,8 +12,8 @@ with open("clusters.json") as json_file: # Setup Jinja2 jin = Environment(loader=PackageLoader("n_gen"), autoescape=select_autoescape()) -load_dotenv() -NGINX_DIR = os.getenv('NGINX_DIR') +load_dotenv() +NGINX_DIR = os.getenv("NGINX_DIR") # Go through config names and find highest config number. Increment by 1 and use as new ID @@ -23,11 +23,12 @@ def get_conf_id(nginx_dir): domain_list = list_auto + list_custom last_id = 0 for dom in domain_list: - id = int(dom.split('-')[0]) + id = int(dom.split("-")[0]) if id > last_id: last_id = id new_id = last_id + 1 - return(new_id) + return new_id + def get_domains(): new_domain = True @@ -107,29 +108,39 @@ def create_nginx_config(id, domains, upstreams, port, proto): id=id, domains=domains, upstreams=upstreams, port=port, proto=proto ) + def write_nginx_config(config, nginx_dir, domains, conf_id): filename = str(conf_id) + "-" + domains[0] + ".conf" path = nginx_dir + "/sites/auto/" + filename with open(path, "w") as conf_file: conf_file.write(config) + def create_ssl_config(conf_id): template = jin.get_template("ssl.conf") return template.render(id=conf_id) + def write_ssl_config(config, conf_id, nginx_dir): filename = str(conf_id) + ".conf" path = nginx_dir + "/ssl/" + filename with open(path, "w") as conf_file: conf_file.write(config) + def ssl_continue(): - if pyip.inputYesNo("Do you want to prepare ssl certs and replicate the config? (y/n) ") == "yes": + if ( + pyip.inputYesNo( + "Do you want to prepare ssl certs and replicate the config? (y/n) " + ) + == "yes" + ): n_ssl.main() else: print("Ok, you can run n_ssl.py to do it later.") exit() + def main(): print("This script will generate nginx configuration and for new service.\n") conf_id = get_conf_id(NGINX_DIR) @@ -148,12 +159,8 @@ def main(): ssl_continue() - - # def test(): # print(create_nginx_config("1110", ['nolog.cz', 'www.nolog.cz'], ['10.0.0.1', '10.0.0.2'], 80, 'https://')) if __name__ == "__main__": main() - - \ No newline at end of file diff --git a/n_ssl.py b/n_ssl.py index 2f43553..3ab64c1 100644 --- a/n_ssl.py +++ b/n_ssl.py @@ -8,12 +8,12 @@ from dotenv import load_dotenv # DOMAINS_TXT = "/etc/autossl/domains.txt" # DEHYDRATED_LOC = "/etc/autossl/dehydrated.sh" -load_dotenv() -NGINX_DIR = os.getenv('NGINX_DIR') -DOMAINS_TXT = os.getenv('DOMAINS_TXT') -DEHYDRATED_LOC = os.getenv('DEHYDRATED_LOC') -REMOTE = os.getenv('REMOTE') -REMOTE_SSH_KEY = os.getenv('REMOTE_SSH_KEY') +load_dotenv() +NGINX_DIR = os.getenv("NGINX_DIR") +DOMAINS_TXT = os.getenv("DOMAINS_TXT") +DEHYDRATED_LOC = os.getenv("DEHYDRATED_LOC") +REMOTE = os.getenv("REMOTE") +REMOTE_SSH_KEY = os.getenv("REMOTE_SSH_KEY") def create_domfile(): @@ -80,7 +80,7 @@ def remote_replication(remote, ssh_key): destination="/etc/nginx/", destination_ssh=remote, private_key=ssh_key, - options=["-a"], + options=["-a", "--delete"], ) # Copy certificates to second server sysrsync.run( @@ -88,7 +88,7 @@ def remote_replication(remote, ssh_key): destination="/etc/autossl/", destination_ssh=remote, private_key=ssh_key, - options=["-a"], + options=["-a", "--delete"], )