import os import json import pyinputplus as pyip from jinja2 import Environment, PackageLoader, select_autoescape from dotenv import load_dotenv import n_ssl # Get clusters from json config with open("clusters.json") as json_file: CLUSTERS = json.load(json_file)["clusters"] # Setup Jinja2 jin = Environment(loader=PackageLoader("n_gen"), autoescape=select_autoescape()) 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 def get_conf_id(nginx_dir): list_auto = os.listdir(nginx_dir + "/sites/auto") list_custom = os.listdir(nginx_dir + "/sites/custom") domain_list = list_auto + list_custom last_id = 0 for dom in domain_list: id = int(dom.split("-")[0]) if id > last_id: last_id = id new_id = last_id + 1 return new_id def get_domains(): new_domain = True domains = [] while new_domain: domain = pyip.inputStr("Enter a full domain name: ") domains.append(domain) next_domain = pyip.inputYesNo("Do you want to add another domain? (y/n) ") if next_domain == "no": new_domain = False return domains def get_upstreams(clusters): print("\nNow, we will select upstream server(s).") if ( pyip.inputYesNo( "Is the service located on existing upstream cluster (like Swarm)? (y/n) " ) == "yes" ): cluster_list = [d["name"] for d in clusters] sel_cluster_name = pyip.inputMenu(cluster_list, lettered=True, blank=True) cluster = [ element for element in clusters if element["name"] == sel_cluster_name ][0] print("Selected cluster " + cluster["name"] + " with nodes:") for node in cluster["nodes"]: print(node) return cluster["nodes"] else: new_upstream = True upstreams = [] while new_upstream: upstream = pyip.inputStr("Enter IPv4 address of one upstream server: ") upstreams.append(upstream) next_upstream = pyip.inputYesNo( "Do you want to add another upstream server? (y/n) " ) if next_upstream == "no": new_upstream = False return upstreams def get_port(): return pyip.inputInt( "\nEnter a port number for the upstream servers: ", min=81, max=65534 ) def get_proto(): print("\nEnter the upstream protocol (between service and reverse proxy)") return pyip.inputMenu(["http://", "https://"], lettered=True) def input_check(domains, upstreams, port, proto): print("\n-----------------------------------------------") print("You have entered following service information:") print("Domains:") for domain in domains: print("\t" + domain) print("Upstream servers with proto and port:") for upstream in upstreams: print("\t" + proto + upstream + ":" + str(port)) if pyip.inputYesNo("Is this information correct? (y/n) ") == "yes": return True else: print("Sorry to hear that, please start again. Exiting") exit() def create_nginx_config(id, domains, upstreams, port, proto): template = jin.get_template("nginx-site.conf") return template.render( 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" ): 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) domains = get_domains() upstreams = get_upstreams(CLUSTERS) port = get_port() proto = get_proto() input_check(domains, upstreams, port, proto) nginx_config = create_nginx_config(conf_id, domains, upstreams, port, proto) write_nginx_config(nginx_config, NGINX_DIR, domains, conf_id) ssl_config = create_ssl_config(conf_id) write_ssl_config(ssl_config, conf_id, NGINX_DIR) print("Nginx config created.") 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()