From 6cc2f3e223d1fc6fb4c7f67d4eec243912935cce Mon Sep 17 00:00:00 2001 From: gauthiier Date: Wed, 28 Nov 2018 09:46:01 +0100 Subject: [PATCH] vhost --- vhost-apache2/vhost.py | 50 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/vhost-apache2/vhost.py b/vhost-apache2/vhost.py index dc0cf08..cb2a552 100755 --- a/vhost-apache2/vhost.py +++ b/vhost-apache2/vhost.py @@ -1,12 +1,14 @@ #!/usr/bin/env python3 -import sys, os, platform, subprocess, logging, argparse +import sys, os, platform, subprocess, re, argparse platform_support = ['Linux'] html_dir_path = "" logs_dir_path = "" +re_domain = r'^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)([a-zA-Z0-9-]{,63}\.){,127}[a-zA-Z0-9-]{1,63}$' + def y_n_question(question_str): yes = {'yes','y', 'ye', ''} @@ -59,6 +61,39 @@ def sanity_chek_platform(): else: sys.exit("Can not configure platform. Aborting...") +def vhost_add(domain): + + global html_dir_path, logs_dir_path, re_domain + + print(" adding vhost domain — " + domain) + + if re.match(re_domain, domain) is None: + print("Invalid domain name: " + domain + " -> pass") + + www = os.path.join(html_dir_path + domain) + os.makedirs(www, exist_ok=True) + + logs = os.path.join(logs_dir_path + domain) + os.makedirs(logs, exist_ok=True) + + # debug: this file might not be here + with open('vhost_tmpl') as vhost_tmpl_fp: + vhost_tmpl = vhost_tmpl_fp.read() + + 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) + +def vhost_remove(domain): + print(" removing domain — " + domain) + + if __name__ == "__main__": p = argparse.ArgumentParser(description='vhost helper') @@ -69,7 +104,20 @@ if __name__ == "__main__": args = p.parse_args() + print('1. sanity checks') + sanity_check_system() sanity_chek_platform() + print('2. vhosting') + + for d in args.domain: + if args.add: + vhost_add(d) + elif args.remove: + vhost_remove(d) + + print('done.') + +