mirror of
https://github.com/bunkerity/bunkerized-nginx
synced 2023-12-13 21:30:18 +01:00
templating - multisite support
This commit is contained in:
parent
bbc5bbc9e9
commit
289ad106cb
3 changed files with 43 additions and 18 deletions
|
@ -2,31 +2,46 @@ import jinja2, glob, os, pathlib, copy
|
|||
|
||||
class Templator :
|
||||
|
||||
def __init__(self, config, input_path) :
|
||||
def __init__(self, config, input_path, output_path, target_path) :
|
||||
self.__config = config
|
||||
self.__input_path = input_path
|
||||
self.__template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=input_path))
|
||||
self.__output_path = output_path
|
||||
self.__target_path = target_path
|
||||
if not self.__target_path.endswith("/") :
|
||||
self.__target_path += "/"
|
||||
self.__template_env = jinja2.Environment(loader=jinja2.FileSystemLoader(searchpath=self.__input_path))
|
||||
|
||||
def render_global(self, output_path) :
|
||||
return self.__render("global", output_path)
|
||||
def render_global(self) :
|
||||
return self.__render("global")
|
||||
|
||||
def render_site(self, output_path, server_name) :
|
||||
return self.__render("site", output_path, server_name)
|
||||
def render_site(self, server_name=None, first_server=None) :
|
||||
if server_name is None :
|
||||
server_name = self.__config["SERVER_NAME"]
|
||||
if first_server is None :
|
||||
first_server = self.__config["SERVER_NAME"].split(" ")[0]
|
||||
return self.__render("site", server_name, first_server)
|
||||
|
||||
def __render(self, type, output_path, server_name=None) :
|
||||
def __render(self, type, server_name=None, first_server=None) :
|
||||
real_config = copy.deepcopy(self.__config)
|
||||
if server_name != None :
|
||||
if not server_name is None :
|
||||
real_config["SERVER_NAME"] = server_name
|
||||
if not first_server is None :
|
||||
real_config["FIRST_SERVER"] = first_server
|
||||
real_config["NGINX_PREFIX"] = self.__target_path
|
||||
if real_config["MULTISITE"] == "yes" and type == "site" :
|
||||
real_config["NGINX_PREFIX"] += first_server + "/"
|
||||
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)
|
||||
template.globals["has_value"] = Templator.has_value
|
||||
output = template.render(real_config, all=real_config)
|
||||
if real_config["MULTISITE"] == "yes" and type == "site" :
|
||||
relative_filename = first_server + "/" + relative_filename
|
||||
if "/" in relative_filename :
|
||||
directory = relative_filename.replace(relative_filename.split("/")[-1], "")
|
||||
pathlib.Path(output_path + "/" + directory).mkdir(parents=True, exist_ok=True)
|
||||
with open(output_path + "/" + relative_filename, "w") as f :
|
||||
pathlib.Path(self.__output_path + "/" + directory).mkdir(parents=True, exist_ok=True)
|
||||
with open(self.__output_path + "/" + relative_filename, "w") as f :
|
||||
f.write(output)
|
||||
|
||||
@jinja2.contextfunction
|
||||
|
|
24
gen/main.py
24
gen/main.py
|
@ -13,6 +13,7 @@ if __name__ == "__main__" :
|
|||
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("--target", default="/etc/nginx", type=str, help="where nginx will search for configurations files")
|
||||
parser.add_argument("--variables", default="/opt/variables.env", type=str, help="path to the file containing environment variables")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
@ -51,16 +52,25 @@ if __name__ == "__main__" :
|
|||
print(config)
|
||||
|
||||
# Generate the files from templates and config
|
||||
templator = Templator(config, args.templates)
|
||||
templator.render_global(args.output)
|
||||
templator = Templator(config, args.templates, args.output, args.target)
|
||||
templator.render_global()
|
||||
if config["MULTISITE"] == "no" :
|
||||
templator.render_site(args.output, config["SERVER_NAME"])
|
||||
templator.render_site()
|
||||
else :
|
||||
# Compute a dict of first_server: [list of server_name]
|
||||
map_servers = {}
|
||||
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)
|
||||
if server_name + "_SERVER_NAME" in config :
|
||||
map_servers[server_name] = config[server_name + "_SERVER_NAME"].split(" ")
|
||||
for server_name in config["SERVER_NAME"].split(" ") :
|
||||
if server_name in map_servers :
|
||||
continue
|
||||
for first_server, servers in map_servers.items() :
|
||||
if server_name in servers :
|
||||
continue
|
||||
map_servers[server_name] = [server_name]
|
||||
for first_server, servers in map_servers.items() :
|
||||
templator.render_site(" ".join(servers), first_server)
|
||||
|
||||
# We're done
|
||||
print("[*] Generation done !")
|
||||
|
|
|
@ -834,7 +834,7 @@
|
|||
"env": "SERVER_NAME",
|
||||
"id": "server-name",
|
||||
"label": "Server name",
|
||||
"regex": "^([a-z\\-0-9]+\\.?)+$",
|
||||
"regex": "^([a-z\\-0-9]+\\.? ?)+$",
|
||||
"type": "text"
|
||||
},
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue