コード例 #1
0
ファイル: api.py プロジェクト: marianogg9/nginx_proxy
def config():
    if request.method == 'GET':
        if 'format=text' in request.args:
            try:
                payload = crossplane.parse(conf_file)['config'][0]['parsed']    # fetch nginx config from nginx.conf file
                return crossplane.build(payload)                                # get nginx config as a string
            except Exception as e:
                return 'Error: cannot fetch config. More info: %e' % e
        else:
            try:
                payload = crossplane.parse(conf_file)['config'][0]['parsed']    # fetch nginx config from nginx.conf file
                return json.dumps(payload)
            except Exception as e:
                return 'Error: cannot fetch config. More info: %e' % e
    else:
        try:
            json_data = json.loads(str(request.data, encoding='utf-8'))         # convert POST body into JSON
            payload = crossplane.build(json_data)                               # convert JSON from POST body into nginx conf type

            with open(conf_file,'w+') as f:
                f.write(payload)
                f.close()
            try:
                subprocess.check_output('nginx -t', shell=True)
                subprocess.call('/bin/systemctl reload nginx', shell=True)
            except subprocess.CalledProcessError as proc_error:
                return 'Nginx config failed. More info: %s' % proc_error

            return "Successfully updated Nginx conf!. Here's the new one: \n\n%s" % payload
        except Exception as e:
            return "Something went wrong. More info: %" % e
コード例 #2
0
 def __init__(self, path='/etc/nginx/nginx.conf'):
     self.parse = crossplane.parse(path)
     if self.parse['status'] == 'failed':
         err = NginxException()
         for cfg in self.parse['config']:
             for error in cfg['errors']:
                 err.errors.append(error)
         raise err
コード例 #3
0
    def parse(self, include_ssl_certs=True):
        # clear results from the previous run
        self.files = {}
        self.directories = {}

        # clear some bits and pieces from previous run
        self._broken_files = {}
        self._broken_directories = {}
        self.includes = []
        self.ssl_certificates = []

        # use the new parser to parse the nginx config
        self.tree = crossplane.parse(filename=self.filename,
                                     onerror=(lambda e: sys.exc_info()),
                                     catch_errors=True,
                                     ignore=IGNORED_DIRECTIVES)

        for error in self.tree['errors']:
            path = error['file']
            exc_info = error.pop('callback')
            try:
                # these error types are handled by this script already
                if not isinstance(exc_info[1], (OSError, IOError)):
                    self._handle_error(path,
                                       exc_info[1],
                                       exc_info=exc_info,
                                       what='parse')
                    self._add_file(path)
            finally:
                # this speeds things up by deleting traceback, see python docs
                del exc_info

        # for every file in parsed payload, search for files/directories to add
        for config in self.tree['config']:
            if config['parsed']:
                self._add_file(config['file'])
                self._collect_included_files_and_cert_dirs(
                    config['parsed'], include_ssl_certs=include_ssl_certs)

        # construct directory_map
        for dirname, info in self.directories.iteritems():
            self.directory_map[dirname] = {'info': info, 'files': {}}

        for dirname, error in self._broken_directories.iteritems():
            self.directory_map.setdefault(dirname, {'info': {}, 'files': {}})
            self.directory_map[dirname]['error'] = error

        for filename, info in self.files.iteritems():
            dirname = self._dirname(filename)
            self.directory_map[dirname]['files'][filename] = {'info': info}

        for filename, error in self._broken_files.iteritems():
            dirname = self._dirname(filename)
            self.directory_map[dirname]['files'].setdefault(
                filename, {'info': {}})
            self.directory_map[dirname]['files'][filename]['error'] = error
コード例 #4
0
ファイル: __init__.py プロジェクト: kepsic/moitoi_docker_hive
def get_all_nginx_backends():
    """ Helper function to filter out from nginx.conf backend servers

    Returns (str): List of backned servers

    """
    parsed_conf = crossplane.parse(NGINX_CONFIG_FILE)
    config_backends = parsed_conf['config'][0]['parsed'][1]['block'][0][
        'block']
    return list(map(lambda x: x['args'], config_backends))
コード例 #5
0
 def test_add_security(self):
     temp_file = mkstemp()[1]
     previous = ""
     with open(self.nginx, "rt") as f0, open(self.two_roots, "rt") as f1, open(
         temp_file, "wt"
     ) as f2:
         previous = OTemplate(f0.read()).substitute(SERVER_BLOCK=f1.read())
         f2.write(previous)
     ## pp(parse(temp_file))
     self.assertEqual(previous, build(parse(temp_file)))
     remove(temp_file)
コード例 #6
0
ファイル: test_parse.py プロジェクト: zzx199313/crossplane
def test_comments_between_args():
    dirname = os.path.join(here, 'configs', 'comments-between-args')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, comments=True)
    assert payload == {
        'status':
        'ok',
        'errors': [],
        'config': [{
            'file':
            config,
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'http',
                'line':
                1,
                'args': [],
                'block': [{
                    'directive': '#',
                    'line': 1,
                    'args': [],
                    'comment': 'comment 1'
                }, {
                    'directive': 'log_format',
                    'line': 2,
                    'args': ['\\#arg\\ 1', '#arg 2']
                }, {
                    'directive': '#',
                    'line': 2,
                    'args': [],
                    'comment': 'comment 2'
                }, {
                    'directive': '#',
                    'line': 2,
                    'args': [],
                    'comment': 'comment 3'
                }, {
                    'directive': '#',
                    'line': 2,
                    'args': [],
                    'comment': 'comment 4'
                }, {
                    'directive': '#',
                    'line': 2,
                    'args': [],
                    'comment': 'comment 5'
                }]
            }]
        }]
    }
コード例 #7
0
ファイル: test_build.py プロジェクト: yigitbasalma/crossplane
def compare_parsed_and_built(conf_dirname, conf_basename, tmpdir):
    original_dirname = os.path.join(here, 'configs', conf_dirname)
    original_path = os.path.join(original_dirname, conf_basename)
    original_payload = crossplane.parse(original_path)
    original_parsed = original_payload['config'][0]['parsed']

    build1_config = crossplane.build(original_parsed)
    build1_file = tmpdir.join('build1.conf')
    build1_file.write(build1_config)
    build1_payload = crossplane.parse(build1_file.strpath)
    build1_parsed = build1_payload['config'][0]['parsed']

    assert_equal_payloads(original_parsed, build1_parsed, ignore_keys=['line'])

    build2_config = crossplane.build(build1_parsed)
    build2_file = tmpdir.join('build2.conf')
    build2_file.write(build2_config)
    build2_payload = crossplane.parse(build2_file.strpath)
    build2_parsed = build2_payload['config'][0]['parsed']

    assert build1_config == build2_config
    assert_equal_payloads(build1_parsed, build2_parsed, ignore_keys=[])
コード例 #8
0
 def check(self, instance):
     name, location, baseurl = self._get_config(instance)
     self.name = name
     self.baseurl = baseurl
     self.start_snapshot()
     try:
         self.log.debug("Processing file: {file}".format(file=location))
         self.payload = crossplane.parse(location)
         self._parse_topology()
         self._parse_upstreams()
         self._create_topology()
     except Exception as err:
         msg = "Nginx check failed: {}".format(str(err))
         self.log.error(traceback.format_exc())
         self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, message=msg, tags=[])
     finally:
         self.stop_snapshot()
コード例 #9
0
    def configure(self):
        try:
            self.amp_pid = self.read_file(self.amp_pid_file)[0]
            self.amp_owner = self.ps_owner(self.amp_pid)
            self.amp_ps_name = self.ps_name(self.amp_pid)

            self.ngx_pid = self.read_file(self.ngx_pid_file)[0]
            self.ngx_owner = self.ps_owner(self.ngx_pid)
            self.ngx_worker_pid = self.pid('nginx: worker process')[0]
            self.ngx_worker_onwer = self.ps_owner(self.ngx_worker_pid)
            self.ngx_conf = crossplane.parse(self.ngx_conf_file)

            for config in self.ngx_conf['config']:
                for parsed in config.get('parsed', []):
                    for block in parsed.get('block', []):
                        self.ngx_conf_blocks.append(block)
        except IOError, exc:
            pass
コード例 #10
0
ファイル: __init__.py プロジェクト: kepsic/moitoi_docker_hive
def add_backend_to_nginx_conf(mdh_backend):
    """ Add backend server to nginx.conf
    Args:
        mdh_backend (dict): Dictionary object  mdh_backend as backend name and mdh_backend_port as backend port

    Returns:

    """
    parsed_conf = crossplane.parse(NGINX_CONFIG_FILE)
    parsed_conf['config'][0]['parsed'][1]['block'][0]['block'].append({
        'directive':
        'server',
        'args': ['{mdh_backend}:{mdh_backend_port}'.format(**mdh_backend)]
    })

    config_str = crossplane.build(parsed_conf['config'][0]['parsed'])
    write_nging_conf(config_str)
    logging.info(config_str)
コード例 #11
0
ファイル: __init__.py プロジェクト: kepsic/moitoi_docker_hive
def remove_backend_from_nginx_conf(mdh_backend):
    """ Remove backend server to nginx.conf
    Args:
        mdh_backend (dict): Dictionary object  where mdh_backend as backend name and mdh_backend_port as backend port

    Returns:

    """
    parsed_conf = crossplane.parse(NGINX_CONFIG_FILE)
    config_backend_names = list(
        map(lambda x: x[0].split(":")[0], get_all_nginx_backends()))
    logging.info(config_backend_names)
    backend_index = config_backend_names.index(
        '{mdh_backend}'.format(**mdh_backend))
    del parsed_conf['config'][0]['parsed'][1]['block'][0]['block'][
        backend_index]
    config_str = crossplane.build(parsed_conf['config'][0]['parsed'])
    write_nging_conf(config_str)
    logging.info(config_str)
コード例 #12
0
ファイル: test_parser.py プロジェクト: offscale/nginxctl
    def setUp(self):
        app_name = PythonPackageInfo().get_app_name()

        assert app_name is not None
        self.nginx_conf_join = partial(
            path.join,
            path.join(
                path.dirname(path.join(resource_filename(app_name, "__init__.py"))),
                "_config",
            ),
        )
        self.nginx_conf_fname = self.nginx_conf_join("nginx.conf")
        self.server_conf_fname = self.nginx_conf_join("server.conf")
        # self.nginx_conf_lex = tuple(crossplane.lex(nginx_conf))
        self.nginx_conf_parse = crossplane.parse(
            self.nginx_conf_fname, catch_errors=False, comments=False
        )

        self.temp_dir = mkdtemp(app_name, self.__class__.__name__)
        self.tmp_nginx_conf_fname = path.join(self.temp_dir, "nginx.conf")
コード例 #13
0
def test_includes_single():
    dirname = os.path.join(here, 'configs', 'includes-regular')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, single=True)
    assert payload == {
        'status':
        'ok',
        'errors': [],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [
                {
                    'directive': 'events',
                    'line': 1,
                    'args': [],
                    'block': []
                },
                {
                    'directive':
                    'http',
                    'line':
                    2,
                    'args': [],
                    'block': [{
                        'directive': 'include',
                        'line': 3,
                        'args': ['conf.d/server.conf']
                        # no 'includes' key
                    }]
                }
            ]
        }
                   # single config parsed
                   ]
    }
コード例 #14
0
def get_config(app_name):
    c = crossplane.parse(VIDEO_FILE)
    rtmp_apps = c['config'][0]['parsed']
    apps = {}
    for rtmp_app in rtmp_apps:
        if rtmp_app['directive'] == 'application':
            name = rtmp_app['args'][0]
            if name == app_name:
                apps[name] = {'push': [], 'push_lines': []}

                for block in rtmp_app['block']:

                    if block['directive'] == 'allow' and block['args'][
                            0] == 'play':
                        apps[name]['play_allow'] = block['args'][1]
                        apps[name]['play_line'] = block['line']

                    elif block['directive'] == 'allow' and block['args'][
                            0] == 'publish':
                        apps[name]['publish_allow'] = block['args'][1]
                        apps[name]['publish_line'] = block['line']

                    elif block['directive'] == 'push':
                        rtmp_url = divide_url(block['args'][0])
                        apps[name]['push'].append({
                            'url': rtmp_url[0],
                            'streamkey': rtmp_url[1],
                            'line': block['line']
                        })
                        apps[name]['push_lines'].append(block['line'])

                    elif block['directive'] == 'access_log':
                        pass

                    # store app last line
                    apps[name]['last_line'] = block['line']

    return apps[app_name]
コード例 #15
0
def main(nginx_path):
    print("Got nginx file {}".format(nginx_path))
    print(os.path.join(nginx_path,'nginx.conf'))
    payload = crossplane.parse(os.path.join(nginx_path,'nginx.conf'))
    # print("Payload is {}".format(payload))
    
    for i in payload['config'][0]['parsed']:
        if 'directive' in i and i['directive'] == "http":
            for block in i['block']:
                if block['directive'] == "upstream":
                    upstream[block['args'][0]] = 0

    for i in payload['config'][0]['parsed']:
        if 'directive' in i and i['directive'] == "http":
                if block['directive'] == "map":
                    for arg in block['block']:
                        if arg['args'][0] in upstream:
                            upstream[arg['args'][0]] += 1
    
    print(upstream)
    for up_st in upstream:
        if findfiles(os.path.join(nginx_path), up_st) > 0:
            upstream[up_st] += 1 
    print(upstream)
コード例 #16
0
ファイル: mergeswarm.py プロジェクト: PiTiLeZarD/nginx-proxy
import os
import subprocess
import sys
from crossplane import parse, build

SWARM_CONFIG_FILE = '/etc/nginx/node.conf.d/swarm.conf'
NGINX_OUTPUT = '/etc/nginx/conf.d/default.conf'
NGINX_RELOAD = 'nginx -s reload'

if not os.path.isfile(SWARM_CONFIG_FILE):
    with open(SWARM_CONFIG_FILE, 'w') as f:
        f.write("http { include ./*.conf; }")

nginx_config = []
cache = []
swarm_config = parse(SWARM_CONFIG_FILE)['config']
nodes = [
    f['parsed'] for f in swarm_config
    if 'node.conf.d' in f['file'] and 'swarm.conf' not in f['file']
]


def get_sub_statements(statement, directive):
    for sub_statement in statement['block']:
        if sub_statement['directive'] == directive:
            yield sub_statement


def get_sub_statement(statement, directive):
    return list(get_sub_statements(statement, directive))[0]
コード例 #17
0
ファイル: serve.py プロジェクト: offscale/nginxctl
def serve(
    known,
    nginx_command,
    parsed_config,
    parsed_config_str,
    parsed_config_http,
    parsed_config_http_str,
):
    if not os.path.isdir(known.temp_dir):
        os.mkdir(known.temp_dir)
    logger.debug("temp_dir:\t{!r}".format(known.temp_dir))
    _config_files = "nginx.conf", "mime.types"
    nginx_conf_join = partial(
        os.path.join,
        os.path.join(
            os.path.dirname(
                os.path.join(
                    resource_filename(PythonPackageInfo().get_app_name(),
                                      "__init__.py"))),
            "_config",
        ),
    )
    config_files = tuple(map(nginx_conf_join, _config_files))
    deque(map(partial(copy, dst=known.temp_dir), config_files), maxlen=0)
    sites_available = os.path.join(known.temp_dir, "sites-available")
    if not os.path.isdir(sites_available):
        os.mkdir(sites_available)
    server_conf = os.path.join(sites_available, "server.conf")
    if parsed_config is not None:
        pp(parsed_config)
        pp(
            research(
                parsed_config,
                query=lambda p, k, v: is_directive(v) and print(
                    "k:", k, ";\nv:", v, ";"),
            ))
    # pp(research(parsed_config, query=lambda p, k, v: is_directive(v)))
    with open(server_conf, "wt") as f:
        if parsed_config_http_str is not None:
            f.write(parsed_config_http_str)
        if parsed_config_str is not None:
            f.write(parsed_config_str)
    # Include this config in the new nginx.conf
    nginx_conf = os.path.join(known.temp_dir, _config_files[0])
    nginx_conf_parsed = crossplane.parse(nginx_conf,
                                         catch_errors=False,
                                         comments=False)
    nginx_conf_parse = next(
        config for config in nginx_conf_parsed["config"]
        if os.path.basename(config["file"]) == "nginx.conf")

    line = count(nginx_conf_parse["parsed"][-1]["block"][-1]["line"])
    del nginx_conf_parse["parsed"][-1]["block"][-1]
    nginx_conf_parse["parsed"].insert(1, {
        "args": ["off"],
        "directive": "daemon"
    })
    nginx_conf_parse["parsed"][-1]["block"] += [
        {
            "args": ["stderr", "warn"],
            "directive": "error_log",
            "line": next(line)
        },
        {
            "args": ["/dev/stdout"],
            "directive": "access_log",
            "line": next(line)
        },
        {
            "args": [os.path.join(sites_available, "*.conf")],
            "directive": "include",
            "includes": [2],
            "line": next(line),
        },
    ]
    config_str = crossplane.build(nginx_conf_parse["parsed"])
    os.remove(nginx_conf)
    with open(nginx_conf, "wt") as f:
        f.write(config_str + os.linesep)
    # logger.error
    print("nginx is running. Stop with: {}".format(" ".join(
        (known.nginx, "-c", nginx_conf, "-s", "stop"))))
    Popen([known.nginx, "-c", nginx_conf] + nginx_command)
コード例 #18
0
ファイル: test_lua.py プロジェクト: xuhengpw/crossplane
def test_parse_lua_block_tricky():
    dirname = os.path.join(tests_dir, 'configs', 'lua-block-tricky')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, comments=True)
    assert payload == {
        'status':
        'ok',
        'errors': [],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'line':
                1,
                'args': [],
                'block': [{
                    'line':
                    2,
                    'args': [],
                    'block': [{
                        'line': 3,
                        'args': ['127.0.0.1:8080'],
                        'directive': 'listen'
                    }, {
                        'line': 4,
                        'args': ['content_by_lua_block'],
                        'directive': 'server_name'
                    }, {
                        'comment': u" make sure this doesn't trip up lexers",
                        'line': 4,
                        'args': [],
                        'directive': '#'
                    }, {
                        'line':
                        5,
                        'args': [
                            '$res', ' -- irregular lua block directive'
                            '\n            local a = 32'
                            '\n            local b = 56'
                            '\n'
                            '\n            ngx.var.diff = a - b;  -- write to $diff directly'
                            '\n            return a + b;          -- return the $sum value normally'
                            '\n        '
                        ],
                        'directive':
                        'set_by_lua_block'
                    }, {
                        'line':
                        12,
                        'args': [
                            ' -- have valid braces in Lua code and quotes around directive'
                            '\n            do_something("hello, world!\\nhiya\\n")'
                            '\n            a = { 1, 2, 3 }'
                            '\n            btn = iup.button({title="ok"})'
                            '\n        '
                        ],
                        'directive':
                        'rewrite_by_lua_block'
                    }],
                    'directive':
                    'server'
                }, {
                    'line':
                    18,
                    'args': ['content_by_lua_block'],
                    'block': [{
                        'comment': ' stuff',
                        'line': 19,
                        'args': [],
                        'directive': '#'
                    }],
                    'directive':
                    'upstream'
                }],
                'directive':
                'http'
            }]
        }]
    }
コード例 #19
0
ファイル: test_lua.py プロジェクト: xuhengpw/crossplane
def test_parse_lua_block_simple():
    dirname = os.path.join(tests_dir, 'configs', 'lua-block-simple')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config)
    assert payload == {
        'status':
        'ok',
        'errors': [],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'line':
                1,
                'args': [],
                'block': [{
                    'line':
                    2,
                    'args': [
                        '\n        print("Lua block code with curly brace str {")\n    '
                    ],
                    'directive':
                    'init_by_lua_block'
                }, {
                    'line':
                    5,
                    'args':
                    ['\n        print("Work that every worker")\n    '],
                    'directive':
                    'init_worker_by_lua_block'
                }, {
                    'line':
                    8,
                    'args': [
                        '\n        local data, eof = ngx.arg[1], ngx.arg[2]\n    '
                    ],
                    'directive':
                    'body_filter_by_lua_block'
                }, {
                    'line':
                    11,
                    'args':
                    ['\n        ngx.header["content-length"] = nil\n    '],
                    'directive':
                    'header_filter_by_lua_block'
                }, {
                    'line':
                    14,
                    'args': [],
                    'block': [{
                        'line': 15,
                        'args': ['127.0.0.1:8080'],
                        'directive': 'listen'
                    }, {
                        'line':
                        16,
                        'args': ['/'],
                        'block': [{
                            'line':
                            17,
                            'args': [
                                '\n                ngx.say("I need no extra escaping here, for example: \\r\\nblah")'
                                '\n            '
                            ],
                            'directive':
                            'content_by_lua_block'
                        }, {
                            'line': 20,
                            'args': ['200', 'foo bar baz'],
                            'directive': 'return'
                        }],
                        'directive':
                        'location'
                    }, {
                        'line':
                        22,
                        'args': [
                            '\n            print("About to initiate a new SSL handshake!")'
                            '\n        '
                        ],
                        'directive':
                        'ssl_certificate_by_lua_block'
                    }, {
                        'line':
                        25,
                        'args': ['/a'],
                        'block': [{
                            'line': 26,
                            'args': ['100k'],
                            'directive': 'client_max_body_size'
                        }, {
                            'line': 27,
                            'args': ['100k'],
                            'directive': 'client_body_buffer_size'
                        }],
                        'directive':
                        'location'
                    }],
                    'directive':
                    'server'
                }, {
                    'line':
                    31,
                    'args': ['foo'],
                    'block': [{
                        'line': 32,
                        'args': ['127.0.0.1'],
                        'directive': 'server'
                    }, {
                        'line':
                        33,
                        'args': [
                            '\n            -- use Lua to do something interesting here'
                            '\n        '
                        ],
                        'directive':
                        'balancer_by_lua_block'
                    }, {
                        'line':
                        36,
                        'args': [
                            '\n            print("I need no extra escaping here, for example: \\r\\nblah")'
                            '\n        '
                        ],
                        'directive':
                        'log_by_lua_block'
                    }],
                    'directive':
                    'upstream'
                }],
                'directive':
                'http'
            }]
        }]
    }
コード例 #20
0
ファイル: ominous-client.py プロジェクト: omussell/ominous
async def get_nginx_config():
    payload = crossplane.parse('/usr/local/etc/nginx/nginx.conf')
    return payload
コード例 #21
0
def test_includes_globbed():
    dirname = os.path.join(here, 'configs', 'includes-globbed')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config)
    assert payload == {
        'status':
        'ok',
        'errors': [],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive': 'events',
                'line': 1,
                'args': [],
                'block': []
            }, {
                'directive': 'include',
                'line': 2,
                'args': ['http.conf'],
                'includes': [1]
            }]
        }, {
            'file':
            os.path.join(dirname, 'http.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'http',
                'args': [],
                'line':
                1,
                'block': [{
                    'directive': 'include',
                    'line': 2,
                    'args': ['servers/*.conf'],
                    'includes': [2, 3]
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'servers', 'server1.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'server',
                'args': [],
                'line':
                1,
                'block': [{
                    'directive': 'listen',
                    'args': ['8080'],
                    'line': 2
                }, {
                    'directive': 'include',
                    'args': ['locations/*.conf'],
                    'line': 3,
                    'includes': [4, 5]
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'servers', 'server2.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'server',
                'args': [],
                'line':
                1,
                'block': [{
                    'directive': 'listen',
                    'args': ['8081'],
                    'line': 2
                }, {
                    'directive': 'include',
                    'args': ['locations/*.conf'],
                    'line': 3,
                    'includes': [4, 5]
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'locations', 'location1.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'location',
                'args': ['/foo'],
                'line':
                1,
                'block': [{
                    'directive': 'return',
                    'args': ['200', 'foo'],
                    'line': 2
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'locations', 'location2.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'location',
                'args': ['/bar'],
                'line':
                1,
                'block': [{
                    'directive': 'return',
                    'args': ['200', 'bar'],
                    'line': 2
                }]
            }]
        }]
    }
コード例 #22
0
ファイル: configs.py プロジェクト: omussell/ominous-client
async def get_nginx_config():
    #payload = crossplane.parse('/usr/local/etc/nginx/nginx.conf')
    payload = crossplane.parse('/home/oem/ominous-client/nginx.conf')
    return payload
コード例 #23
0
def test_includes_globbed_combined():
    dirname = os.path.join(here, 'configs', 'includes-globbed')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, combine=True)
    assert payload == {
        "status":
        "ok",
        "errors": [],
        "config": [{
            "file":
            os.path.join(dirname, "nginx.conf"),
            "status":
            "ok",
            "errors": [],
            "parsed": [{
                "directive": "events",
                "args": [],
                "file": os.path.join(dirname, "nginx.conf"),
                "line": 1,
                "block": []
            }, {
                "directive":
                "http",
                "args": [],
                "file":
                os.path.join(dirname, "http.conf"),
                "line":
                1,
                "block": [{
                    "directive":
                    "server",
                    "args": [],
                    "file":
                    os.path.join(dirname, "servers", "server1.conf"),
                    "line":
                    1,
                    "block": [{
                        "directive":
                        "listen",
                        "args": ["8080"],
                        "file":
                        os.path.join(dirname, "servers", "server1.conf"),
                        "line":
                        2
                    }, {
                        "directive":
                        "location",
                        "args": ["/foo"],
                        "file":
                        os.path.join(dirname, "locations", "location1.conf"),
                        "line":
                        1,
                        "block": [{
                            "directive":
                            "return",
                            "args": ["200", "foo"],
                            "file":
                            os.path.join(dirname, "locations",
                                         "location1.conf"),
                            "line":
                            2
                        }]
                    }, {
                        "directive":
                        "location",
                        "args": ["/bar"],
                        "file":
                        os.path.join(dirname, "locations", "location2.conf"),
                        "line":
                        1,
                        "block": [{
                            "directive":
                            "return",
                            "args": ["200", "bar"],
                            "file":
                            os.path.join(dirname, "locations",
                                         "location2.conf"),
                            "line":
                            2
                        }]
                    }]
                }, {
                    "directive":
                    "server",
                    "args": [],
                    "file":
                    os.path.join(dirname, "servers", "server2.conf"),
                    "line":
                    1,
                    "block": [{
                        "directive":
                        "listen",
                        "args": ["8081"],
                        "file":
                        os.path.join(dirname, "servers", "server2.conf"),
                        "line":
                        2
                    }, {
                        "directive":
                        "location",
                        "args": ["/foo"],
                        "file":
                        os.path.join(dirname, "locations", "location1.conf"),
                        "line":
                        1,
                        "block": [{
                            "directive":
                            "return",
                            "args": ["200", "foo"],
                            "file":
                            os.path.join(dirname, "locations",
                                         "location1.conf"),
                            "line":
                            2
                        }]
                    }, {
                        "directive":
                        "location",
                        "args": ["/bar"],
                        "file":
                        os.path.join(dirname, "locations", "location2.conf"),
                        "line":
                        1,
                        "block": [{
                            "directive":
                            "return",
                            "args": ["200", "bar"],
                            "file":
                            os.path.join(dirname, "locations",
                                         "location2.conf"),
                            "line":
                            2
                        }]
                    }]
                }]
            }]
        }]
    }
コード例 #24
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import crossplane
import json

NGINX_CONFIG_PATH = '/etc/nginx/nginx.conf'
HTTPS_PORT = 'ssl'

domainsList = []
nginxConfig = crossplane.parse(NGINX_CONFIG_PATH)
if nginxConfig['config']:
    for cfile in nginxConfig['config']:
        for parsed in cfile['parsed']:
            if 'block' in parsed:
                foundHttps = False
                httpsDomain = None

                for blk in parsed['block']:

                    if blk['directive'] == 'listen':
                        if HTTPS_PORT in blk['args']:
                            foundHttps = True

                    if foundHttps and blk['directive'] == 'server_name' and len(
                            blk['args']) > 0:
                        httpsDomain = blk['args'][0]

                if foundHttps and httpsDomain != None:
                    domainsList.append({"{#DOMAIN_HTTPS}": httpsDomain})
コード例 #25
0
def test_ignore_directives():
    dirname = os.path.join(here, 'configs', 'simple')
    config = os.path.join(dirname, 'nginx.conf')

    # check that you can ignore multiple directives
    payload = crossplane.parse(config, ignore=['listen', 'server_name'])
    assert payload == {
        "status":
        "ok",
        "errors": [],
        "config": [{
            "file":
            os.path.join(dirname, 'nginx.conf'),
            "status":
            "ok",
            "errors": [],
            "parsed": [{
                "directive":
                "events",
                "line":
                1,
                "args": [],
                "block": [{
                    "directive": "worker_connections",
                    "line": 2,
                    "args": ["1024"]
                }]
            }, {
                "directive":
                "http",
                "line":
                5,
                "args": [],
                "block": [{
                    "directive":
                    "server",
                    "line":
                    6,
                    "args": [],
                    "block": [{
                        "directive":
                        "location",
                        "line":
                        9,
                        "args": ["/"],
                        "block": [{
                            "directive": "return",
                            "line": 10,
                            "args": ["200", "foo bar baz"]
                        }]
                    }]
                }]
            }]
        }]
    }

    # check that you can also ignore block directives
    payload = crossplane.parse(config, ignore=['events', 'server'])
    assert payload == {
        "status":
        "ok",
        "errors": [],
        "config": [{
            "file":
            os.path.join(dirname, 'nginx.conf'),
            "status":
            "ok",
            "errors": [],
            "parsed": [{
                "directive": "http",
                "line": 5,
                "args": [],
                "block": []
            }]
        }]
    }
コード例 #26
0
def test_includes_regular():
    dirname = os.path.join(here, 'configs', 'includes-regular')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config)
    assert payload == {
        'status':
        'failed',
        'errors': [{
            'file':
            os.path.join(dirname, 'conf.d', 'server.conf'),
            'error':
            '[Errno 2] No such file or directory: %r' %
            os.path.join(dirname, 'bar.conf'),
            'line':
            5
        }],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive': 'events',
                'line': 1,
                'args': [],
                'block': []
            }, {
                'directive':
                'http',
                'line':
                2,
                'args': [],
                'block': [{
                    'directive': 'include',
                    'line': 3,
                    'args': ['conf.d/server.conf'],
                    'includes': [1]
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'conf.d', 'server.conf'),
            'status':
            'failed',
            'errors': [{
                'error':
                '[Errno 2] No such file or directory: %r' %
                os.path.join(dirname, 'bar.conf'),
                'line':
                5
            }],
            'parsed': [{
                'directive':
                'server',
                'line':
                1,
                'args': [],
                'block': [{
                    'directive': 'listen',
                    'line': 2,
                    'args': ['127.0.0.1:8080']
                }, {
                    'directive': 'server_name',
                    'line': 3,
                    'args': ['default_server']
                }, {
                    'directive': 'include',
                    'line': 4,
                    'args': ['foo.conf'],
                    'includes': [2]
                }, {
                    'directive': 'include',
                    'line': 5,
                    'args': ['bar.conf'],
                    'includes': []
                }]
            }]
        }, {
            'file':
            os.path.join(dirname, 'foo.conf'),
            'status':
            'ok',
            'errors': [],
            'parsed': [{
                'directive':
                'location',
                'line':
                1,
                'args': ['/foo'],
                'block': [{
                    'directive': 'return',
                    'line': 2,
                    'args': ['200', 'foo']
                }]
            }]
        }]
    }
コード例 #27
0
def test_parse_strict():
    dirname = os.path.join(here, 'configs', 'spelling-mistake')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, comments=True, strict=True)
    assert payload == {
        'status':
        'failed',
        'errors': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'error':
            'unknown directive "proxy_passs" in %s:7' %
            os.path.join(dirname, 'nginx.conf'),
            'line':
            7
        }],
        'config': [{
            'file':
            os.path.join(dirname, 'nginx.conf'),
            'status':
            'failed',
            'errors': [{
                'error':
                'unknown directive "proxy_passs" in %s:7' %
                os.path.join(dirname, 'nginx.conf'),
                'line':
                7
            }],
            'parsed': [{
                'directive': 'events',
                'line': 1,
                'args': [],
                'block': []
            }, {
                'directive':
                'http',
                'line':
                3,
                'args': [],
                'block': [{
                    'directive':
                    'server',
                    'line':
                    4,
                    'args': [],
                    'block': [{
                        'directive':
                        'location',
                        'line':
                        5,
                        'args': ['/'],
                        'block': [{
                            'directive': '#',
                            'line': 6,
                            'args': [],
                            'comment': 'directive is misspelled'
                        }]
                    }]
                }]
            }]
        }]
    }
コード例 #28
0
def test_config_without_comments():
    dirname = os.path.join(here, 'configs', 'with-comments')
    config = os.path.join(dirname, 'nginx.conf')
    payload = crossplane.parse(config, comments=False)
    assert payload == {
        "errors": [],
        "status":
        "ok",
        "config": [{
            "errors": [],
            "parsed": [{
                "block": [{
                    "directive": "worker_connections",
                    "args": ["1024"],
                    "line": 2
                }],
                "line":
                1,
                "args": [],
                "directive":
                "events"
            }, {
                "block": [{
                    "args": [],
                    "directive":
                    "server",
                    "line":
                    6,
                    "block": [{
                        "args": ["127.0.0.1:8080"],
                        "directive": "listen",
                        "line": 7
                    }, {
                        "args": ["default_server"],
                        "directive": "server_name",
                        "line": 8
                    }, {
                        "block": [{
                            "line": 11,
                            "directive": "return",
                            "args": ["200", "foo bar baz"]
                        }],
                        "line":
                        9,
                        "directive":
                        "location",
                        "args": ["/"]
                    }]
                }],
                "line":
                5,
                "args": [],
                "directive":
                "http"
            }],
            "status":
            "ok",
            "file":
            os.path.join(dirname, 'nginx.conf')
        }]
    }
コード例 #29
0
def test_parse_missing_semicolon():
    dirname = os.path.join(here, 'configs', 'missing-semicolon')

    # test correct error is raised when broken proxy_pass is in upper block
    above_config = os.path.join(dirname, 'broken-above.conf')
    above_payload = crossplane.parse(above_config)
    assert above_payload == {
        "status":
        "failed",
        "errors": [{
            "file":
            above_config,
            "error":
            "directive \"proxy_pass\" is not terminated by \";\" in %s:4" %
            above_config,
            "line":
            4
        }],
        "config": [{
            "file":
            above_config,
            "status":
            "failed",
            "errors": [{
                "error":
                "directive \"proxy_pass\" is not terminated by \";\" in %s:4" %
                above_config,
                "line":
                4
            }],
            "parsed": [{
                "directive":
                "http",
                "line":
                1,
                "args": [],
                "block": [{
                    "directive":
                    "server",
                    "line":
                    2,
                    "args": [],
                    "block": [{
                        "directive": "location",
                        "line": 3,
                        "args": ["/is-broken"],
                        "block": []
                    }, {
                        "directive":
                        "location",
                        "line":
                        6,
                        "args": ["/not-broken"],
                        "block": [{
                            "directive": "proxy_pass",
                            "line": 7,
                            "args": ["http://not.broken.example"]
                        }]
                    }]
                }]
            }]
        }]
    }

    # test correct error is raised when broken proxy_pass is in lower block
    below_config = os.path.join(dirname, 'broken-below.conf')
    below_payload = crossplane.parse(below_config)
    assert below_payload == {
        "status":
        "failed",
        "errors": [{
            "file":
            below_config,
            "error":
            "directive \"proxy_pass\" is not terminated by \";\" in %s:7" %
            below_config,
            "line":
            7
        }],
        "config": [{
            "file":
            below_config,
            "status":
            "failed",
            "errors": [{
                "error":
                "directive \"proxy_pass\" is not terminated by \";\" in %s:7" %
                below_config,
                "line":
                7
            }],
            "parsed": [{
                "directive":
                "http",
                "line":
                1,
                "args": [],
                "block": [{
                    "directive":
                    "server",
                    "line":
                    2,
                    "args": [],
                    "block": [{
                        "directive":
                        "location",
                        "line":
                        3,
                        "args": ["/not-broken"],
                        "block": [{
                            "directive": "proxy_pass",
                            "line": 4,
                            "args": ["http://not.broken.example"]
                        }]
                    }, {
                        "directive": "location",
                        "line": 6,
                        "args": ["/is-broken"],
                        "block": []
                    }]
                }]
            }]
        }]
    }
コード例 #30
0
import crossplane
import sys

payload = crossplane.parse('/etc/nginx/nginx.conf')
#payload = crossplane.parse('./new.conf')
#print(payload)
#print("\n***********************")
print(sys.argv[1])
print(sys.argv[2])
print(sys.argv[3])
b=[]
for f in payload["config"]:
	if f["file"] == '/etc/nginx/conf.d/wallak.conf':
		#print(f["parsed"][3])
		#print(f["parsed"])
		
		#print("\n\n\n", f["parsed"], "\n\n\n")
		for e in f["parsed"]:
			
			if e["directive"] == "upstream":
		#		print(e["block"][0]["args"]) 
				e["block"][0]["args"] = [sys.argv[1]+':8000']
		#		print(e["block"][0]["args"])
			if e["directive"] == "server":
				#print(e) 
				for k in e["block"]:
					if k["directive"] == "server_name":
		#				print(k["args"] )	
						k["args"] = [sys.argv[2]]
		#				print(k["args"] ) 
					if k["directive"] == "listen":