def process_include(self, globpath): if not path.isabs(globpath): globpath = path.join(self.config_dir, globpath) conf = [] for fname in sorted(glob(globpath)): nc = NginxConfig() nc.load(open(fname).read()) conf += list(nc) return conf
def loadNginxConfig(configPath='/etc/nginx/nginx.conf'): """ Load the nginx configuration, then parse with pynginxconfig. Default to loading from /etc/nginx/nginx.conf """ with open (configPath, 'r') as configFile: raw=configFile.readlines() confData = ''.join(raw) nc = NginxConfig() nc.load(confData) return nc
def __init__(self, conf_files, default_addr="127.0.0.1", excluded_hosts=None, config_dir="/etc/nginx", use_listen=False ): self.default_addr = default_addr self.excluded_hosts = excluded_hosts if isinstance(excluded_hosts, list) else [] self.config_dir = config_dir self.use_listen = use_listen self.data = [] nc = NginxConfig() for file in conf_files: nc.load(file.read()) self.process_main(nc)
def install_nginx(): """ Install NGINX and make it use certs. """ require.arch.package("nginx") contents = PROXIED_SITE_TEMPLATE % {"server_name": "cozy", "port": 443, "proxy_url": "http://127.0.0.1:9104"} if files.exists("/etc/nginx/conf.d"): require.files.file("/etc/nginx/conf.d/cozy.conf", contents=contents, use_sudo=True) else: config = NginxConfig() config.load(sudo("cat /etc/nginx/nginx.conf")) server = NginxConfig() server.load(contents) config.append(server[0], root=config.get_value(config.get(("http",)))) put(StringIO(config.gen_config()), "/etc/nginx/nginx.conf", use_sudo=True) if files.exists("/etc/nginx/conf.d/default.conf"): su_delete("/etc/nginx/conf.d/default.conf") if files.exists("/etc/nginx/conf.d/example_ssl.conf"): su_delete("/etc/nginx/conf.d/example_ssl.conf") service.restart("nginx") print(green("Nginx successfully installed."))
#!/usr/bin/env python __author__ = 'babnik' from pynginxconfig import NginxConfig import argparse import subprocess parser = argparse.ArgumentParser(description='Test editor for nginx host') parser.add_argument('-b', '--backend', metavar=' ip:port', type=str, help='Add backend servers to pool', nargs='+') args=parser.parse_args() val=[] for host in args.backend: val.append(('server', host)) nc = NginxConfig() nc.load('') nc.append({'name': 'upstream', 'param': 'backend', 'value': val}, position=1) f1 = open("/etc/nginx/sites-available/default", 'a') f1.write(str(nc.gen_config())) f1.close()