From a411ba2270bd33a4fdb5a3b7f2d36a6134beaeef Mon Sep 17 00:00:00 2001 From: Achilleas Koutsou Date: Mon, 14 Jun 2021 17:40:41 +0200 Subject: [PATCH] stages: add org.osbuild.nginx.conf Add new stage for writing an nginx configuration file. --- stages/org.osbuild.nginx.conf | 85 +++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100755 stages/org.osbuild.nginx.conf diff --git a/stages/org.osbuild.nginx.conf b/stages/org.osbuild.nginx.conf new file mode 100755 index 00000000..3c2ac8c3 --- /dev/null +++ b/stages/org.osbuild.nginx.conf @@ -0,0 +1,85 @@ +#!/usr/bin/python3 +""" +Write nginx configuration +""" + +import os +import sys + +import osbuild.api +from osbuild.util.path import in_tree + +SCHEMA = r""" +"additionalProperties": false, +"properties": { + "path": { + "type": "string", + "description": "Config file location", + "default": "/etc/nginx/nginx.conf", + "pattern": "^\\/(?!\\.\\.)((?!\\/\\.\\.\\/).)+$" + }, + "config": { + "type": "object", + "properties": { + "listen": { + "type": "string", + "description": "The address and/or port on which the server will accept requests", + "default": "*:80" + }, + "root": { + "type": "string", + "description": "The root directory for requests", + "default": "/usr/share/nginx/html" + }, + "pid": { + "type": "string", + "description": "File that will store the process ID of the main process", + "default": "/run/nginx.pid" + }, + "daemon": { + "type": "boolean", + "description": "Whether nginx should become a daemon", + "default": true + } + } + } +} +""" + +def main(tree, options): + path = options.get("path", "etc/nginx/nginx.conf").lstrip("/") + + config = options.get("config", {}) + listen = config.get("listen", "*:80") + pid = config.get("pid", "/run/nginx.pid") + if config.get("daemon", True): + daemon = "on" + else: + daemon = "off" + root = config.get("root", "/usr/share/nginx/html") + + target = os.path.join(tree, path) + if not in_tree(target, tree, must_exist=False): + raise ValueError(f"config file path {target} not in tree") + + content = f"""events {{}} +http {{ + server {{ + listen {listen}; + root {root}; + }} +}} + +pid {pid}; +daemon {daemon}; +""" + + with open(target, "w") as f: + f.write(content) + + return 0 + + +if __name__ == "__main__": + args = osbuild.api.arguments() + sys.exit(main(args["tree"], args["options"]))