__maison/vhost/vhost.py

224 lines
6.3 KiB
Python
Raw Normal View History

2018-11-28 08:55:19 +01:00
#!/usr/bin/env python3
2018-11-28 08:18:39 +01:00
2018-12-06 10:36:36 +01:00
import sys, os, shutil, platform, subprocess, re, argparse
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
# only support linux / Debian, Ubuntu / nginx, apache2
platform_system_support = ['Linux']
platform_version_support = ['DEBIAN', 'UBUNTU']
server_support = ['NGINX', "APACHE2"]
2018-11-28 08:53:35 +01:00
html_dir_path = ""
logs_dir_path = ""
2018-11-28 09:46:01 +01:00
re_domain = r'^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)([a-zA-Z0-9-]{,63}\.){,127}[a-zA-Z0-9-]{1,63}$'
2022-03-19 13:23:29 +01:00
def y_n_question(question_str: str) -> bool:
2018-11-28 08:53:35 +01:00
yes = {'yes','y', 'ye', ''}
no = {'no','n'}
while True:
sys.stdout.write(question_str + " [Y/n]: ")
choice = input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
sys.stdout.write("\nPlease respond with 'yes' or 'no'\n")
2022-03-19 13:23:29 +01:00
continue
def check_server_version() -> str:
a = subprocess.call("sudo apache2 -v", shell=True)
if not a:
return "APACHE2"
n = subprocess.call("sudo nginx -v", shell=True)
if not n:
return "NGINX"
return None
def check_distribution_version() -> str:
dist = platform.version().lower()
if 'debian' in dist:
return "DEBIAN"
if 'ubuntu' in dist:
return "UBUNTU"
return None
2018-11-28 08:53:35 +01:00
def sanity_check_system():
2018-11-28 08:18:39 +01:00
# check platform
2022-03-19 13:23:29 +01:00
if platform.system() not in platform_system_support:
sys.exit(f"Platform {platform.system()} not supported. Aborting...")
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
dist = check_distribution_version()
if not dist:
sys.exit("OS distribution not supported. Aborting...")
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
sv = check_server_version()
if not sv:
sys.exit("Server distribution not supported. Aborting...")
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
if dist in platform_version_support and sv == "APACHE2":
# check apache2 (ubuntu or debian)
u = os.path.exists('/etc/apache2/sites-available/')
if not u:
sys.exit(f"Apache2 ({dist}) not installed on your system. Aborting...")
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
if dist in platform_version_support and sv in server_support:
# check apache2 (ubuntu or debian)
u = os.path.exists(f'/etc/{sv.lower()}/sites-available/')
if not u:
sys.exit(f"{sv} ({dist}) not installed on your system. Aborting...")
return sv, dist
def sanity_check_platform():
2018-11-28 08:53:35 +01:00
global html_dir_path, logs_dir_path
usr = os.getlogin()
html_dir_path = os.path.join('/home', usr, 'html')
logs_dir_path = os.path.join('/home', usr, 'logs')
if not os.path.exists(html_dir_path):
if y_n_question("Path - " + html_dir_path + ' - does not exists. Create it?'):
os.makedirs(html_dir_path)
else:
sys.exit("Can not configure platform. Aborting...")
if not os.path.exists(logs_dir_path):
if y_n_question("Path - " + logs_dir_path + ' - does not exists. Create it?'):
os.makedirs(logs_dir_path)
else:
sys.exit("Can not configure platform. Aborting...")
2018-11-28 08:18:39 +01:00
2022-03-19 13:23:29 +01:00
def vhost_add(domain: str, sv: str, dist: str):
2018-11-28 09:46:01 +01:00
global html_dir_path, logs_dir_path, re_domain
2020-06-25 12:48:54 +00:00
print(" adding vhost domain - " + domain)
2018-11-28 09:46:01 +01:00
if re.match(re_domain, domain) is None:
print("Invalid domain name: " + domain + " -> pass")
2018-11-28 09:54:47 +01:00
www = os.path.join(html_dir_path, domain)
2018-11-28 09:46:01 +01:00
os.makedirs(www, exist_ok=True)
2018-11-28 09:54:47 +01:00
logs = os.path.join(logs_dir_path, domain)
2018-11-28 09:46:01 +01:00
os.makedirs(logs, exist_ok=True)
2018-12-06 10:36:36 +01:00
# debug: this file might not be here...........
2022-03-19 13:23:29 +01:00
if sv == "APACHE2":
with open('vhost_tmpl_apache2') as vhost_tmpl_fp:
vhost_tmpl = vhost_tmpl_fp.read()
elif sv == "NGINX":
with open('vhost_tmpl_nginx') as vhost_tmpl_fp:
vhost_tmpl = vhost_tmpl_fp.read()
else:
sys.exit(f"{sv} not recognised. Aborting...")
2018-11-28 09:46:01 +01:00
usr = os.getlogin()
vhost = vhost_tmpl.replace("%domain?", domain).replace("%user?", usr)
# debug: write file directly to '/etc/apache2/sites-available/' ?
vhost_file = os.path.join(www, domain + '.conf')
with open(vhost_file, 'w+') as vhost_file_fp:
vhost_file_fp.write(vhost)
2018-12-06 10:36:36 +01:00
# debug: this file might not be here...........
2025-09-26 14:05:49 +02:00
if y_n_question("Create index.html placeholder?"):
2025-09-26 14:04:30 +02:00
shutil.copyfile('c.txt', os.path.join(www, 'index.html'))
2018-12-06 10:36:36 +01:00
2018-11-28 10:35:14 +01:00
2018-11-28 09:54:47 +01:00
# check is ssl cert exists
cert = os.path.join('/etc/letsencrypt/live', domain)
if not os.path.exists(cert):
2018-11-29 21:40:47 +01:00
print(" warning: SSL certificates do not exist for domain - " + domain + " - this will problaly cause errors...")
2018-11-28 10:27:58 +01:00
print(" warning: Please make sure to place them in " + cert + " to allow secure https connection to your site.")
2018-11-28 10:13:04 +01:00
2018-11-29 21:40:47 +01:00
# mv conf file to apache?
2022-03-19 13:23:29 +01:00
if dist in platform_version_support:
if y_n_question(f"Move {vhost_file} to /etc/{sv.lower()}/sites-available/ ?"):
vhost_conf_file = os.path.join(f'/etc/{sv.lower()}/sites-available/', f'{domain}.conf')
subprocess.call(['sudo', 'mv', vhost_file, vhost_conf_file])
# enable site?
if y_n_question(f"Enable {domain}?"):
if sv == "APACHE2":
subprocess.call(['sudo', 'a2ensite', f'{domain}.conf'])
if sv == "NGINX":
vhost_conf_file_enabled = os.path.join(f'/etc/nginx/sites-enabled/', f'{domain}.conf')
subprocess.call(['sudo', 'ln', '-s', vhost_conf_file, vhost_conf_file_enabled])
def vhost_remove(domain: str, sv: str, dist: str):
2018-11-28 09:46:01 +01:00
print(" removing domain — " + domain)
2022-03-19 13:23:29 +01:00
vhost_conf_file = os.path.join(f'/etc/{sv.lower()}/sites-available/{domain}.conf')
2018-11-28 10:21:43 +01:00
if os.path.exists(vhost_conf_file):
if y_n_question("Delete " + vhost_conf_file + " ?"):
subprocess.call(['sudo', 'rm', vhost_conf_file])
2022-03-19 13:23:29 +01:00
if sv == "APACHE2":
subprocess.call(['sudo', 'a2dissite', vhost_conf_file])
if sv == "NGINX":
vhost_conf_file_enabled = os.path.join(f'/etc/nginx/sites-enabled/', f'{domain}.conf')
subprocess.call(['sudo', 'rm', vhost_conf_file_enabled])
2018-11-28 10:21:43 +01:00
www = os.path.join(html_dir_path, domain)
if os.path.exists(www):
if y_n_question("Delete " + www + " ?"):
subprocess.call(['sudo', 'rm', '-r', www])
logs = os.path.join(logs_dir_path, domain)
if os.path.exists(logs):
if y_n_question("Delete " + logs + " ?"):
subprocess.call(['sudo', 'rm', '-r', logs])
2018-11-28 09:46:01 +01:00
2018-11-28 08:18:39 +01:00
if __name__ == "__main__":
2022-03-19 13:28:06 +01:00
p = argparse.ArgumentParser(description='vhost helper')
p.add_argument('domain', metavar="domain", help="vhost domain(s)", nargs="+")
g = p.add_mutually_exclusive_group()
g.add_argument('-a', '--add', action='store_true', help="adds vhost for given domain(s)")
g.add_argument('-r', '--remove', action='store_true', help="removes vhost for given domain(s)")
2018-11-28 08:18:39 +01:00
2022-03-19 13:28:06 +01:00
args = p.parse_args()
2018-11-28 08:18:39 +01:00
2022-03-19 13:28:06 +01:00
print('1. sanity checks')
2018-11-28 09:46:01 +01:00
2022-03-19 13:28:06 +01:00
sv, dist = sanity_check_system()
sanity_check_platform()
2018-11-28 08:53:35 +01:00
2022-03-19 13:28:06 +01:00
print('2. vhosting')
2018-11-28 09:46:01 +01:00
2022-03-19 13:28:06 +01:00
for d in args.domain:
if args.add:
vhost_add(d, sv, dist)
elif args.remove:
vhost_remove(d, sv, dist)
2018-11-28 10:27:58 +01:00
2022-03-19 13:28:06 +01:00
if y_n_question(f"Reload {sv}?"):
2022-03-19 13:23:29 +01:00
subprocess.call(['sudo', 'service', sv.lower(), 'reload'])
2022-03-19 13:28:06 +01:00
print('done.')
2018-11-28 09:46:01 +01:00
2018-11-28 08:18:39 +01:00