add rsync --delete for cleanup

This commit is contained in:
mdivecky 2023-10-26 16:19:40 +02:00
parent 5becd6d9a1
commit ef0ab2752b
2 changed files with 24 additions and 17 deletions

View file

@ -12,8 +12,8 @@ with open("clusters.json") as json_file:
# Setup Jinja2 # Setup Jinja2
jin = Environment(loader=PackageLoader("n_gen"), autoescape=select_autoescape()) jin = Environment(loader=PackageLoader("n_gen"), autoescape=select_autoescape())
load_dotenv() load_dotenv()
NGINX_DIR = os.getenv('NGINX_DIR') NGINX_DIR = os.getenv("NGINX_DIR")
# Go through config names and find highest config number. Increment by 1 and use as new ID # 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 domain_list = list_auto + list_custom
last_id = 0 last_id = 0
for dom in domain_list: for dom in domain_list:
id = int(dom.split('-')[0]) id = int(dom.split("-")[0])
if id > last_id: if id > last_id:
last_id = id last_id = id
new_id = last_id + 1 new_id = last_id + 1
return(new_id) return new_id
def get_domains(): def get_domains():
new_domain = True 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 id=id, domains=domains, upstreams=upstreams, port=port, proto=proto
) )
def write_nginx_config(config, nginx_dir, domains, conf_id): def write_nginx_config(config, nginx_dir, domains, conf_id):
filename = str(conf_id) + "-" + domains[0] + ".conf" filename = str(conf_id) + "-" + domains[0] + ".conf"
path = nginx_dir + "/sites/auto/" + filename path = nginx_dir + "/sites/auto/" + filename
with open(path, "w") as conf_file: with open(path, "w") as conf_file:
conf_file.write(config) conf_file.write(config)
def create_ssl_config(conf_id): def create_ssl_config(conf_id):
template = jin.get_template("ssl.conf") template = jin.get_template("ssl.conf")
return template.render(id=conf_id) return template.render(id=conf_id)
def write_ssl_config(config, conf_id, nginx_dir): def write_ssl_config(config, conf_id, nginx_dir):
filename = str(conf_id) + ".conf" filename = str(conf_id) + ".conf"
path = nginx_dir + "/ssl/" + filename path = nginx_dir + "/ssl/" + filename
with open(path, "w") as conf_file: with open(path, "w") as conf_file:
conf_file.write(config) conf_file.write(config)
def ssl_continue(): 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() n_ssl.main()
else: else:
print("Ok, you can run n_ssl.py to do it later.") print("Ok, you can run n_ssl.py to do it later.")
exit() exit()
def main(): def main():
print("This script will generate nginx configuration and for new service.\n") print("This script will generate nginx configuration and for new service.\n")
conf_id = get_conf_id(NGINX_DIR) conf_id = get_conf_id(NGINX_DIR)
@ -148,12 +159,8 @@ def main():
ssl_continue() ssl_continue()
# def test(): # def test():
# print(create_nginx_config("1110", ['nolog.cz', 'www.nolog.cz'], ['10.0.0.1', '10.0.0.2'], 80, 'https://')) # print(create_nginx_config("1110", ['nolog.cz', 'www.nolog.cz'], ['10.0.0.1', '10.0.0.2'], 80, 'https://'))
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View file

@ -8,12 +8,12 @@ from dotenv import load_dotenv
# DOMAINS_TXT = "/etc/autossl/domains.txt" # DOMAINS_TXT = "/etc/autossl/domains.txt"
# DEHYDRATED_LOC = "/etc/autossl/dehydrated.sh" # DEHYDRATED_LOC = "/etc/autossl/dehydrated.sh"
load_dotenv() load_dotenv()
NGINX_DIR = os.getenv('NGINX_DIR') NGINX_DIR = os.getenv("NGINX_DIR")
DOMAINS_TXT = os.getenv('DOMAINS_TXT') DOMAINS_TXT = os.getenv("DOMAINS_TXT")
DEHYDRATED_LOC = os.getenv('DEHYDRATED_LOC') DEHYDRATED_LOC = os.getenv("DEHYDRATED_LOC")
REMOTE = os.getenv('REMOTE') REMOTE = os.getenv("REMOTE")
REMOTE_SSH_KEY = os.getenv('REMOTE_SSH_KEY') REMOTE_SSH_KEY = os.getenv("REMOTE_SSH_KEY")
def create_domfile(): def create_domfile():
@ -80,7 +80,7 @@ def remote_replication(remote, ssh_key):
destination="/etc/nginx/", destination="/etc/nginx/",
destination_ssh=remote, destination_ssh=remote,
private_key=ssh_key, private_key=ssh_key,
options=["-a"], options=["-a", "--delete"],
) )
# Copy certificates to second server # Copy certificates to second server
sysrsync.run( sysrsync.run(
@ -88,7 +88,7 @@ def remote_replication(remote, ssh_key):
destination="/etc/autossl/", destination="/etc/autossl/",
destination_ssh=remote, destination_ssh=remote,
private_key=ssh_key, private_key=ssh_key,
options=["-a"], options=["-a", "--delete"],
) )