#!/usr/bin/env python import sys, os, platform, subprocess, logging, argparse platform_support = ['Linux'] html_dir_path = "" logs_dir_path = "" def y_n_question(question_str): 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") continue def sanity_check_system(): # check platform if platform.system() not in platform_support: sys.exit("Platform " + platform.system() + " not supported. Aborting...") # check apache2 r = subprocess.call("apache2 -v", shell=True) == 0 # check apache2 (ubuntu-style) u = os.path.exists('/etc/apache2/sites-available/') if not r and u: sys.exit("Apache2 (ubuntu-style) not installed on your system. Aborting...") def sanity_chek_platform(): 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...") if __name__ == "__main__": 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)") args = p.parse_args() sanity_check_system() sanity_chek_platform()