standard-build/scripts/setup-template.py

59 lines
1.5 KiB
Python
Raw Normal View History

2022-09-23 21:41:25 +01:00
#IMPORT
2023-08-14 22:49:43 +01:00
import os
import argparse
2023-08-24 16:57:17 +01:00
import warnings
#CACHE_FUNCTIONS
def get_cached_email() -> str:
with open("../email.cache", "r") as f:
email = f.read().strip()
return email
def cache_email(email: str) -> None:
try:
with open("../email.cache", "w") as f:
f.write(email)
except Exception as e:
warnings.warn(e, Warning)
2023-08-14 22:49:43 +01:00
#PARSE_ARGS
parser = argparse.ArgumentParser()
parser.add_argument("domain")
2023-08-24 16:57:17 +01:00
parser.add_argument("email", default=None) # to not recache emails retreived from cache
parser.add_argument("cache_email", default=True)
2023-08-14 22:49:43 +01:00
args = parser.parse_args()
2022-09-23 21:41:25 +01:00
2023-08-24 16:57:17 +01:00
#CACHE
if args.cache_email is True and args.email is not None:
cache_email(email)
if args.email is None:
args.email = get_cached_email()
2023-08-14 23:13:31 +01:00
#QUICK_RECOVER
os.makedirs(os.path.dirname("/opt/quick-recover/"), exist_ok=True)
with open("/opt/quick-recover/#service.conf", "w") as f:
2023-08-23 19:36:00 +01:00
f.write(f"export DOMAIN='{args.domain}'\n")
f.write(f"export EMAIL='{args.email}'\n")
2023-08-14 23:13:31 +01:00
2022-09-23 21:41:25 +01:00
#FILE_READ
with open("../nginx-site-template.conf", "r") as f:
2023-08-14 22:49:43 +01:00
template = f.read().replace("#serverNameVar", f"{args.domain}").replace("#http://127.0.0.1:8080/", f"#ip_address")
2022-09-23 21:58:47 +01:00
#ADDITIONAL_REPLACE
2022-09-23 21:41:25 +01:00
#FILE_WRITE
with open("/etc/nginx/sites-available/#service.conf", "w") as f:
f.write(template)
2022-09-26 14:26:55 +01:00
#GET_CERT
2023-08-14 22:49:43 +01:00
os.system(f"sh ../getcert.sh {args.domain} {args.email}")
2022-09-26 14:26:55 +01:00
2022-09-23 21:41:25 +01:00
#GENERATE_NGINX
# depreceated, TODO: replace with os.subprocess
os.system("sh ../nginx-ensite.sh #service")
#START_CONTAINER
2022-09-29 20:01:59 +01:00
os.system("sh launch-docker.sh")
#POST_LAUNCH_COMMANDS