cert.py
This commit is contained in:
parent
c88b24b14b
commit
5abbf6a168
@ -2,14 +2,66 @@
|
|||||||
|
|
||||||
import sys, os, platform, subprocess, re, argparse
|
import sys, os, platform, subprocess, re, argparse
|
||||||
|
|
||||||
|
# only support linux / Debian, Ubuntu / nginx, apache2
|
||||||
|
platform_system_support = ['Linux']
|
||||||
|
platform_version_support = ['DEBIAN', 'UBUNTU']
|
||||||
|
server_support = ['NGINX', "APACHE2"]
|
||||||
|
|
||||||
re_domain = r'^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)([a-zA-Z0-9-]{,63}\.){,127}[a-zA-Z0-9-]{1,63}$'
|
re_domain = r'^(?=.{1,253}$)(?!.*\.\..*)(?!\..*)([a-zA-Z0-9-]{,63}\.){,127}[a-zA-Z0-9-]{1,63}$'
|
||||||
|
|
||||||
def sanity_check_system():
|
def check_server_version() -> str:
|
||||||
# check apache2
|
|
||||||
if subprocess.call("apache2 -v", shell=True):
|
|
||||||
sys.exit("Apache2 not installed on your system. Aborting...")
|
|
||||||
|
|
||||||
def cert_add(domains):
|
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_system_support:
|
||||||
|
sys.exit(f"Platform {platform.system()} not supported. Aborting...")
|
||||||
|
|
||||||
|
dist = check_distribution_version()
|
||||||
|
if not dist:
|
||||||
|
sys.exit("OS distribution not supported. Aborting...")
|
||||||
|
|
||||||
|
sv = check_server_version()
|
||||||
|
if not sv:
|
||||||
|
sys.exit("Server distribution not supported. 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...")
|
||||||
|
|
||||||
|
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 cert_add(domains, sv: str, dist: str):
|
||||||
|
|
||||||
dms = []
|
dms = []
|
||||||
for d in domains:
|
for d in domains:
|
||||||
@ -18,13 +70,18 @@ def cert_add(domains):
|
|||||||
continue
|
continue
|
||||||
dms.append(d)
|
dms.append(d)
|
||||||
|
|
||||||
|
if sv == "APACHE2":
|
||||||
certbot_args = ['sudo', 'certbot', '--apache', 'certonly', '-d']
|
certbot_args = ['sudo', 'certbot', '--apache', 'certonly', '-d']
|
||||||
|
|
||||||
|
if sv == "NGINX":
|
||||||
|
certbot_args = ['sudo', 'certbot', '--nginx', 'certonly', '-d']
|
||||||
|
|
||||||
certbot_args.extend(dms)
|
certbot_args.extend(dms)
|
||||||
|
|
||||||
subprocess.call(certbot_args)
|
subprocess.call(certbot_args)
|
||||||
|
|
||||||
def cert_remove(domain):
|
def cert_remove(domain):
|
||||||
pass
|
subprocess.call(['sudo', 'cerbot', 'delete', '--cert-name', domain])
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
@ -36,10 +93,10 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
args = p.parse_args()
|
args = p.parse_args()
|
||||||
|
|
||||||
sanity_check_system()
|
sv, dist = sanity_check_system()
|
||||||
|
|
||||||
if args.add:
|
if args.add:
|
||||||
cert_add(args.domain)
|
cert_add(args.domain, sv, dist)
|
||||||
elif args.remove:
|
elif args.remove:
|
||||||
cert_remove(args.domain)
|
cert_remove(args.domain)
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,6 @@ platform_system_support = ['Linux']
|
|||||||
platform_version_support = ['DEBIAN', 'UBUNTU']
|
platform_version_support = ['DEBIAN', 'UBUNTU']
|
||||||
server_support = ['NGINX', "APACHE2"]
|
server_support = ['NGINX', "APACHE2"]
|
||||||
|
|
||||||
server_kind = None
|
|
||||||
|
|
||||||
html_dir_path = ""
|
html_dir_path = ""
|
||||||
logs_dir_path = ""
|
logs_dir_path = ""
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ server {
|
|||||||
|
|
||||||
location / {
|
location / {
|
||||||
root /home/%user?/html/%domain?;
|
root /home/%user?/html/%domain?;
|
||||||
index index.html;
|
try_files $uri $uri/index.html;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user