nginx-configurator/n_ssl.py

125 lines
3.8 KiB
Python
Raw Normal View History

2023-10-26 12:36:19 +02:00
import os
import subprocess
import re
2023-10-26 15:55:27 +02:00
import sysrsync
from dotenv import load_dotenv
2023-10-26 12:36:19 +02:00
# NGINX_DIR="/etc/nginx"
# DOMAINS_TXT = "/etc/autossl/domains.txt"
# DEHYDRATED_LOC = "/etc/autossl/dehydrated.sh"
2023-10-26 15:55:27 +02:00
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')
2023-10-26 12:36:19 +02:00
def create_domfile():
# Get nginx config files with "# AUTOSSL" tag, parse IDs and domains and create domains.txt file for Dehydrated
sites_path = NGINX_DIR + "/sites"
# It's probably not the best to use grep here, but it's really fast unlike reading files in Python directly. But what can go wrong? (lol)
grep_out = subprocess.run(
["grep", "-Rh", "AUTOSSL", sites_path], capture_output=True, text=True
)
if grep_out.returncode == 0:
DOMAIN_LINES = []
for line in grep_out.stdout.splitlines():
id = re.findall(r"\d+", line)[-1]
domains = re.findall(r"(?<=server_name )(.*)(?=;)", line)[0]
DOMAIN_LINES.append(domains + " > " + str(id))
if len(DOMAIN_LINES) > 0:
with open(DOMAINS_TXT, "w") as fp:
for line in DOMAIN_LINES:
# write each item on a new line
fp.write("%s\n" % line)
else:
print("No data to write to domains.txt. \n Aborting")
exit()
else:
print("Finding #AUTOSSL comments in nginx configs failed.")
exit()
def request_cert():
print("Requesting certificate")
dehydrated_run = subprocess.run(
[DEHYDRATED_LOC, "-c"], capture_output=True, text=True
)
if dehydrated_run.returncode != 0:
print("Something went wrong with dehydrated.sh")
print(dehydrated_run.stdout)
else:
print(
"Certificates are successfully dehydrated. (It went OK and cert is now generated)"
)
def reload_local_nginx():
nginx_check = subprocess.run(["nginx", "-t"], capture_output=True, text=True)
if nginx_check.returncode != 0:
print("nginx config is not valid! Aborting")
print(nginx_check.stdout)
exit()
nginx_reload = subprocess.run(
["systemctl", "reload", "nginx.service"], capture_output=True, text=True
)
if nginx_reload.returncode != 0:
print("Nginx reload returned non-zero status code")
print(nginx_reload.stdout)
exit()
2023-10-26 15:55:27 +02:00
def remote_replication(remote, ssh_key):
# Copy nginx config to second server
sysrsync.run(
source="/etc/nginx/",
destination="/etc/nginx/",
destination_ssh=remote,
private_key=ssh_key,
options=["-a"],
)
# Copy certificates to second server
sysrsync.run(
source="/etc/autossl/",
destination="/etc/autossl/",
destination_ssh=remote,
private_key=ssh_key,
options=["-a"],
)
2023-10-26 12:36:19 +02:00
2023-10-26 15:55:27 +02:00
def remote_reload(remote, ssh_key):
2023-10-26 12:36:19 +02:00
# Check and reload nginx on second server
2023-10-26 15:55:27 +02:00
nginx_check = subprocess.run(
["ssh", "-i", ssh_key, remote, "nginx", "-t"], capture_output=True, text=True
)
if nginx_check.returncode != 0:
print("Remote nginx config is not valid! Please check manually.")
print(nginx_check.stdout)
return False
else:
nginx_reload = subprocess.run(
["ssh", "-i", ssh_key, remote, "systemctl", "reload", "nginx.service"],
capture_output=True,
text=True,
)
if nginx_reload.returncode != 0:
print("Remote nginx reload failed, please check manually.")
print(nginx_reload.stdout)
2023-10-26 12:36:19 +02:00
def main():
2023-10-26 15:55:27 +02:00
create_domfile()
2023-10-26 12:36:19 +02:00
request_cert()
reload_local_nginx()
2023-10-26 15:55:27 +02:00
remote_replication(REMOTE, REMOTE_SSH_KEY)
remote_reload(REMOTE, REMOTE_SSH_KEY)
2023-10-26 12:36:19 +02:00
if __name__ == "__main__":
main()