bunkerized-nginx/gen/main.py
2022-06-03 17:24:14 +02:00

92 lines
4.1 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
import argparse, os, sys, shutil, glob, traceback
import sys
sys.path.append("/opt/bunkerweb/deps/python")
sys.path.append("/opt/bunkerweb/gen")
sys.path.append("/opt/bunkerweb/utils")
from logger import log
from Configurator import Configurator
from Templator import Templator
if __name__ == "__main__" :
try :
# Parse arguments
parser = argparse.ArgumentParser(description="BunkerWeb config generator")
parser.add_argument("--settings", default="/opt/bunkerweb/settings.json", type=str, help="file containing the main settings")
parser.add_argument("--templates", default="/opt/bunkerweb/confs", type=str, help="directory containing the main template files")
parser.add_argument("--core", default="/opt/bunkerweb/core", type=str, help="directory containing the core plugins")
parser.add_argument("--plugins", default="/opt/bunkerweb/plugins", type=str, help="directory containing the external plugins")
parser.add_argument("--output", default="/etc/nginx", type=str, help="where to write the rendered files")
parser.add_argument("--target", default="/etc/nginx", type=str, help="where nginx will search for configurations files")
parser.add_argument("--variables", default="/opt/bunkerweb/variables.env", type=str, help="path to the file containing environment variables")
args = parser.parse_args()
log("GENERATOR", "", "Generator started ...")
log("GENERATOR", "", "Settings : " + args.settings)
log("GENERATOR", "", "Templates : " + args.templates)
log("GENERATOR", "", "Core : " + args.core)
log("GENERATOR", "", "Plugins : " + args.plugins)
log("GENERATOR", "", "Output : " + args.output)
log("GENERATOR", "", "Target : " + args.target)
log("GENERATOR", "", "Variables : " + args.variables)
# Check existences and permissions
log("GENERATOR", "", "Checking arguments ...")
files = [args.settings, args.variables]
paths_rx = [args.core, args.plugins, args.templates]
paths_rwx = [args.output]
for file in files :
if not os.path.exists(file) :
log("GENERATOR", "", "Missing file : " + file)
sys.exit(1)
if not os.access(file, os.R_OK) :
log("GENERATOR", "", "Can't read file : " + file)
sys.exit(1)
for path in paths_rx + paths_rwx :
if not os.path.isdir(path) :
log("GENERATOR", "", "Missing directory : " + path)
sys.exit(1)
if not os.access(path, os.R_OK | os.X_OK) :
log("GENERATOR", "", "Missing RX rights on directory : " + path)
sys.exit(1)
for path in paths_rwx :
if not os.access(path, os.W_OK) :
log("GENERATOR", "", "Missing W rights on directory : " + path)
sys.exit(1)
# Compute the config
log("GENERATOR", "", "Computing config ...")
configurator = Configurator(args.settings, args.core, args.plugins, args.variables)
config = configurator.get_config()
# Remove old files
log("GENERATOR", "", "Removing old files ...")
files = glob.glob(args.output + "/*")
for file in files :
if os.path.islink(file) :
os.unlink(file)
elif os.path.isfile(file) :
os.remove(file)
elif os.path.isdir(file) :
shutil.rmtree(file, ignore_errors=False)
# Render the templates
log("GENERATOR", "", "Rendering templates ...")
templator = Templator(args.templates, args.core, args.plugins, args.output, args.target, config)
templator.render()
# We're done
log("GENERATOR", "", "Generator successfully executed !")
sys.exit(0)
except SystemExit as e :
sys.exit(e)
except :
log("GENERATOR", "", "Exception while executing generator : " + traceback.format_exc())
sys.exit(1)