This commit is contained in:
gauthiier 2022-03-19 13:23:29 +01:00
parent c5defb8dea
commit d86043190d
8 changed files with 119 additions and 40 deletions

View File

@ -1,5 +0,0 @@
#!/bin/sh
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-apache

BIN
certbot/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -2,15 +2,19 @@
import sys, os, shutil, platform, subprocess, re, argparse
# only support linux
platform_support = ['Linux']
# only support linux / Debian, Ubuntu / nginx, apache2
platform_system_support = ['Linux']
platform_version_support = ['DEBIAN', 'UBUNTU']
server_support = ['NGINX', "APACHE2"]
server_kind = None
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):
def y_n_question(question_str: str) -> bool:
yes = {'yes','y', 'ye', ''}
no = {'no','n'}
@ -26,22 +30,60 @@ def y_n_question(question_str):
sys.stdout.write("\nPlease respond with 'yes' or 'no'\n")
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
def sanity_check_system():
# check platform
if platform.system() not in platform_support:
sys.exit("Platform " + platform.system() + " not supported. Aborting...")
if platform.system() not in platform_system_support:
sys.exit(f"Platform {platform.system()} not supported. Aborting...")
# check apache2
r = subprocess.call("apache2 -v", shell=True) == 0
dist = check_distribution_version()
if not dist:
sys.exit("OS distribution not supported. Aborting...")
# check apache2 (ubuntu)
u = os.path.exists('/etc/apache2/sites-available/')
sv = check_server_version()
if not sv:
sys.exit("Server distribution not supported. Aborting...")
if not r and u:
sys.exit("Apache2 (ubuntu) not installed on your system. Aborting...")
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...")
def sanity_chek_platform():
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():
global html_dir_path, logs_dir_path
@ -62,7 +104,7 @@ def sanity_chek_platform():
else:
sys.exit("Can not configure platform. Aborting...")
def vhost_add(domain):
def vhost_add(domain: str, sv: str, dist: str):
global html_dir_path, logs_dir_path, re_domain
@ -78,8 +120,16 @@ def vhost_add(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()
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...")
usr = os.getlogin()
@ -94,9 +144,6 @@ def vhost_add(domain):
# debug: this file might not be here...........
shutil.copyfile('c.txt', os.path.join(www, 'c.txt'))
# bla = os.path.join(www, 'itworks.txt')
# with open(bla, 'w+') as bla_fp:
# bla_fp.write("it does.")
# check is ssl cert exists
cert = os.path.join('/etc/letsencrypt/live', domain)
@ -105,22 +152,34 @@ def vhost_add(domain):
print(" warning: Please make sure to place them in " + cert + " to allow secure https connection to your site.")
# mv conf file to apache?
if y_n_question("Move " + vhost_file + " to /etc/apache2/sites-available/ ?"):
vhost_conf_file = os.path.join('/etc/apache2/sites-available/', domain + '.conf')
subprocess.call(['sudo', 'mv', vhost_file, vhost_conf_file])
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("Enable " + domain + "?"):
subprocess.call(['sudo', 'a2ensite', domain + '.conf'])
# enable site?
if y_n_question(f"Enable {domain}?"):
if sv == "APACHE2":
subprocess.call(['sudo', 'a2ensite', f'{domain}.conf'])
def vhost_remove(domain):
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):
print(" removing domain — " + domain)
vhost_conf_file = os.path.join('/etc/apache2/sites-available/', domain + '.conf')
vhost_conf_file = os.path.join(f'/etc/{sv.lower()}/sites-available/{domain}.conf')
if os.path.exists(vhost_conf_file):
if y_n_question("Delete " + vhost_conf_file + " ?"):
subprocess.call(['sudo', 'rm', vhost_conf_file])
subprocess.call(['sudo', 'a2dissite', vhost_conf_file])
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])
www = os.path.join(html_dir_path, domain)
if os.path.exists(www):
@ -145,19 +204,19 @@ if __name__ == "__main__":
print('1. sanity checks')
sanity_check_system()
sanity_chek_platform()
sv, dist = sanity_check_system()
sanity_check_platform()
print('2. vhosting')
for d in args.domain:
if args.add:
vhost_add(d)
vhost_add(d, sv, dist)
elif args.remove:
vhost_remove(d)
vhost_remove(d, sv, dist)
if y_n_question("Reload apache2?"):
subprocess.call(['sudo', 'service', 'apache2', 'reload'])
if y_n_question(f"Reload {sv}?"):
subprocess.call(['sudo', 'service', sv.lower(), 'reload'])
print('done.')

25
vhost/vhost_tmpl_nginx Normal file
View File

@ -0,0 +1,25 @@
server {
listen 80;
server_name %domain?;
rewrite ^ https://%domain?$request_uri? permanent;
}
server {
listen 443 ssl http2;
ssl_certificate /etc/letsencrypt/live/%domain?/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/%domain?/privkey.pem;
ssl_stapling on;
server_name %domain?;
location / {
root /home/%user?/html/%domain?;
index index.html;
}
gzip on;
error_log /home/%user?/logs/%domain?/error.log;
access_log /home/%user?/logs/%domain?/access.log;
}