main
Brett 2024-06-17 15:55:48 -04:00
parent 3e6e56f7f6
commit aca2352452
2 changed files with 86 additions and 54 deletions

140
create_site.py Normal file → Executable file
View File

@ -1,19 +1,28 @@
#!/bin/python3 #!/usr/bin/python3
import argparse import argparse
import subprocess import subprocess
import util.color_io as color_io import util.color_io as color_io
import sys
parser = argparse.ArgumentParser(prog='Site Generator', description='Apache/Nginx Site Generator', epilog='Currently Only For Nginx') parser = argparse.ArgumentParser(prog='Site Generator', description='Apache/Nginx Site Generator',
epilog='Currently Only For Nginx')
parse_type_group = parser.add_mutually_exclusive_group() parse_type_group = parser.add_mutually_exclusive_group()
parse_type_group.add_argument('-p', '--proxy', action='store_true', help="Generate a site which will act as a proxy") parse_type_group.add_argument('-p', '--proxy', action='store_true', help="Generate a site which will act as a proxy")
parse_type_group.add_argument('-r', '--root', action='store_true', help="Generate a site which serves from a document root") parse_type_group.add_argument('-r', '--root', action='store_true',
help="Generate a site which serves from a document root")
parser.add_argument('-i', '--install', default="/etc/nginx/sites-available/", help="Set the install location for the generated config, defaults to /etc/nginx/sites-available/") parser.add_argument('-i', '--install', default=None, const="/etc/nginx/sites-available/",
parser.add_argument('-l', '--log', default="/var/log/nginx/", help="Set the default log location for the generated config, defaults to /var/log/nginx/") help="Set the install location for the generated config, defaults to /etc/nginx/sites-available/",
parser.add_argument('-s', '--symb', default="/etc/nginx/sites-enabled/", help="Set the location for the symbolic link to enable the config") nargs="?")
parser.add_argument('-l', '--log', default="/var/log/nginx/",
help="Set the default log location for the generated config, defaults to /var/log/nginx/")
parser.add_argument('-s', '--symb', default=None, const="/etc/nginx/sites-enabled/", nargs="?",
help="Set the location for the symbolic link to enable the config")
parser.add_argument("--no-ssl", action="store_const", default="ssl", const="", help="Disable SSL", dest="ssl")
parser.add_argument("--http", default="", required=False, help="Provide HTTP version")
args = parser.parse_args() args = parser.parse_args()
@ -21,73 +30,96 @@ install_location = args.install
link_location = args.symb link_location = args.symb
log_location = args.log log_location = args.log
if not install_location.endswith('/'): if install_location is None:
install_location = install_location + "/" print("Hint: you can provide install location or use the default (nginx dir) via command-line args")
if not log_location.endswith('/'): install_location = color_io.input_print("Enter install location (default: stdout):", "stdout")
log_location = log_location + "/" if install_location.lower() == "stdout":
install_location = None
if install_location is not None and not install_location.endswith('/'):
install_location = install_location + "/"
if not log_location.endswith('/'):
log_location = log_location + "/"
site = {
'name': color_io.input_print("Enter site address (eg: 'tpgc.me')")
}
site = {}
site['name'] = color_io.input_print("Enter site address (eg: 'tpgc.me')")
def get_config_name(): def get_config_name():
return site['name'] return site['name']
def get_config_path(): def get_config_path():
return install_location + get_config_name() return install_location + get_config_name()
def create_file(): def create_file():
return open(get_config_path(), 'a') if install_location is None:
return sys.stdout
return open(get_config_path(), 'a')
def create_nginx(): def create_nginx():
f = create_file() f = create_file()
f.write("#--- --- --- {Begin Site " + site['name'] + "} --- --- ---#\n") f.write("#--- --- --- {Begin Site " + site['name'] + "} --- --- ---#\n")
f.write("server {\n") f.write("server {\n")
f.write(" listen 443 ssl http2;\n\n") port = "443"
f.write(" server_name " + site['name'] + ";\n\n") if len(args.ssl) == 0:
f.write(" access_log " + log_location + site['name'] + "-access.log;\n") port = "80"
f.write(" error_log " + log_location + site['name'] + "-error.log error;\n\n") f.write(" listen " + port + " " + args.ssl + " " + args.http + ";\n\n")
return f f.write(" server_name " + site['name'] + ";\n\n")
f.write(" access_log " + log_location + site['name'] + "-access.log;\n")
f.write(" error_log " + log_location + site['name'] + "-error.log error;\n\n")
return f
def end_nginx(f): def end_nginx(f):
f.write("}\n") f.write("}\n")
f.write("#--- --- --- {End Site " + site['name'] + "} --- --- ---#\n\n") f.write("#--- --- --- {End Site " + site['name'] + "} --- --- ---#\n\n")
f.close() if f is not sys.stdout:
subprocess.run(["ln", "-s", get_config_path(), link_location + get_config_name()]) f.close()
if link_location is not None:
subprocess.run(["ln", "-s", get_config_path(), link_location + get_config_name()])
def write_location(f, loc, mod): def write_location(f, loc, mod):
f.write(" location " + loc + " {\n") f.write(" location " + loc + " {\n")
for m in mod: for m in mod:
f.write(" " + m + ";\n") f.write(" " + m + ";\n")
f.write(" }\n\n") f.write(" }\n\n")
def create_proxy(): def create_proxy():
color_io.green_print("Creating proxy site") color_io.green_print("Creating proxy site")
site['location'] = color_io.input_print("Enter site location (default '/')", "/") site['location'] = color_io.input_print("Enter site location (default: /)", "/")
site['address'] = color_io.input_print("Enter address to proxy to (default: http://127.0.0.1)", "http://127.0.0.1") site['address'] = color_io.input_print("Enter address to proxy to (eg: http://127.0.0.1:)")
site['port'] = color_io.input_print("Enter port") f = create_nginx()
f = create_nginx() write_location(f, site['location'], ["proxy_pass " + site['address'],
write_location(f, site['location'], ["proxy_pass " + site['address'] + ":" + site['port'], "proxy_set_header Upgrade $http_upgrade",
"proxy_set_header Upgrade $http_upgrade", 'proxy_set_header Connection "Upgrade"',
'proxy_set_header Connection "Upgrade"', "proxy_set_header Host $host"])
"proxy_set_header Host $host"]) end_nginx(f)
end_nginx(f)
def create_root(): def create_root():
color_io.green_print("Creating root site") color_io.green_print("Creating root site")
site['path'] = color_io.input_print("Enter site files path") site['path'] = color_io.input_print("Enter site files path")
f = create_nginx() f = create_nginx()
f.write(" root " + site['path'] + ";\n\n"); f.write(" root " + site['path'] + ";\n\n")
f.write(" include php-fpm;\n\n"); f.write(" include php-fpm;\n\n")
end_nginx(f) end_nginx(f)
# Process user input based on args provided # Process user input based on args provided
def execute_input(): def execute_input():
if args.proxy: if args.proxy:
create_proxy() create_proxy()
elif args.root: elif args.root:
create_root() create_root()
else: else:
create_proxy() create_proxy()
if __name__ == "__main__": if __name__ == "__main__":
execute_input() execute_input()