templating - road to full jinja2 templates
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import json, re
|
||||
|
||||
class Settings :
|
||||
class Configurator :
|
||||
|
||||
def __init__(self) :
|
||||
self.__settings = {}
|
||||
@@ -1,6 +1,6 @@
|
||||
import jinja2, glob, os, pathlib
|
||||
import jinja2, glob, os, pathlib, copy
|
||||
|
||||
class Templates :
|
||||
class Templator :
|
||||
|
||||
def __init__(self, config, input_path) :
|
||||
self.__config = config
|
||||
@@ -14,11 +14,14 @@ class Templates :
|
||||
return self.__render("site", output_path, server_name)
|
||||
|
||||
def __render(self, type, output_path, server_name=None) :
|
||||
real_config = copy.deepcopy(self.__config)
|
||||
if server_name != None :
|
||||
real_config["SERVER_NAME"] = server_name
|
||||
for filename in glob.iglob(self.__input_path + "/" + type + "**/**", recursive=True) :
|
||||
if os.path.isfile(filename) :
|
||||
relative_filename = filename.replace(self.__input_path, "").replace(type + "/", "")
|
||||
template = self.__template_env.get_template(type + "/" + relative_filename)
|
||||
output = template.render(self.__config)
|
||||
output = template.render(real_config)
|
||||
if "/" in relative_filename :
|
||||
directory = relative_filename.replace(relative_filename.split("/")[-1], "")
|
||||
pathlib.Path(output_path + "/" + directory).mkdir(parents=True, exist_ok=True)
|
||||
75
gen/main.py
75
gen/main.py
@@ -1,18 +1,67 @@
|
||||
#!/usr/bin/python3
|
||||
|
||||
from Settings import Settings
|
||||
from Templates import Templates
|
||||
import argparse, os, sys
|
||||
|
||||
import utils
|
||||
from Configurator import Configurator
|
||||
from Templator import Templator
|
||||
|
||||
if __name__ == "__main__" :
|
||||
|
||||
my_settings = Settings()
|
||||
my_settings.load_settings("../settings.json")
|
||||
variables = {}
|
||||
variables["MULTISITE"] = "yes"
|
||||
variables["BLOCK_PROXIES"] = "no"
|
||||
variables["omg"] = "lol"
|
||||
variables["www.toto.com_BLOCK_PROXIES"] = "yes"
|
||||
my_settings.load_variables(variables)
|
||||
print(my_settings.get_config())
|
||||
my_templates = Templates(my_settings.get_config(), "/tmp/input")
|
||||
my_templates.render_global("/tmp/output")
|
||||
# Parse arguments
|
||||
parser = argparse.ArgumentParser(description="bunkerized-nginx config generator v1.0")
|
||||
parser.add_argument("--settings", default="/opt/settings.json", type=str, help="path to the files containing the default settings")
|
||||
parser.add_argument("--templates", default="/opt/confs", type=str, help="directory containing the templates files")
|
||||
parser.add_argument("--output", default="/etc/nginx", type=str, help="where to write the rendered files")
|
||||
parser.add_argument("--variables", default="/opt/variables.env", type=str, help="path to the file containing environment variables")
|
||||
args = parser.parse_args()
|
||||
|
||||
# Check existences and permissions
|
||||
if not os.path.exists(args.settings) :
|
||||
print("[!] Missing settings file : " + args.settings)
|
||||
sys.exit(1)
|
||||
if not os.access(args.settings, os.R_OK) :
|
||||
print("[!] Can't read settings file : " + args.settings)
|
||||
sys.exit(2)
|
||||
if not os.path.isdir(args.templates) :
|
||||
print("[!] Missing templates directory : " + args.templates)
|
||||
sys.exit(1)
|
||||
if not os.access(args.templates, os.R_OK | os.X_OK) :
|
||||
print("[!] Can't read the templates directory : " + args.templates)
|
||||
sys.exit(2)
|
||||
if not os.path.isdir(args.output) :
|
||||
print("[!] Missing output directory : " + args.output)
|
||||
sys.exit(1)
|
||||
if not os.access(args.output, os.W_OK | os.X_OK) :
|
||||
print("[!] Can't write to the templates directory : " + args.output)
|
||||
sys.exit(2)
|
||||
if not os.path.exists(args.variables) :
|
||||
print("[!] Missing variables file : " + args.variables)
|
||||
sys.exit(1)
|
||||
if not os.access(args.variables, os.R_OK) :
|
||||
print("[!] Can't read variables file : " + args.variables)
|
||||
sys.exit(2)
|
||||
|
||||
# Compute the final config
|
||||
configurator = Configurator()
|
||||
configurator.load_settings(args.settings)
|
||||
variables = utils.load_variables(args.variables)
|
||||
configurator.load_variables(variables)
|
||||
config = configurator.get_config()
|
||||
print(config)
|
||||
|
||||
# Generate the files from templates and config
|
||||
templator = Templator(config, args.templates)
|
||||
templator.render_global(args.output)
|
||||
if config["MULTISITE"] == "no" :
|
||||
templator.render_site(args.output, config["SERVER_NAME"])
|
||||
else :
|
||||
for server_name in config["SERVER_NAME"].split(" ") :
|
||||
real_server_name = server_name
|
||||
if server_name + "_SERVER_NAME" in variables :
|
||||
real_server_name = variables[server_name + "_SERVER_NAME"]
|
||||
templator.render_site(args.output, real_server_name)
|
||||
|
||||
# We're done
|
||||
print("[*] Generation done !")
|
||||
sys.exit(0)
|
||||
|
||||
1
gen/test.txt
Normal file
1
gen/test.txt
Normal file
@@ -0,0 +1 @@
|
||||
./main.py --settings /opt/work/bunkerized-nginx/settings.json --templates /opt/work/bunkerized-nginx/confs2 --output /tmp/debug --variables /tmp/variables.env
|
||||
10
gen/utils.py
Normal file
10
gen/utils.py
Normal file
@@ -0,0 +1,10 @@
|
||||
def load_variables(path) :
|
||||
variables = {}
|
||||
with open(path) as f :
|
||||
lines = f.read().splitlines()
|
||||
for line in lines :
|
||||
var = line.split("=")[0]
|
||||
value = line[len(var)+1:]
|
||||
variables[var] = value
|
||||
return variables
|
||||
|
||||
Reference in New Issue
Block a user