imjoey / pyhaproxy

Python library to parse haproxy configurations
MIT License
54 stars 21 forks source link

Not specify host and port in `frontend` definition #24

Closed dbclkh closed 5 years ago

dbclkh commented 5 years ago

Hi,

I'm having a small problem with my pyhaproxy script, and I'm hoping that you can lead me in the correct direction.

My haproxy.cfg contains two frontends...

frontend http
    bind 0.0.0.0:80
    acl test_site1 hdr(host) -i test_site1.local
    use_backend test_site1_back if test_site1
    etc. etc.
frontend https
    bind 0.0.0.0:443 ssl crt /etc/ssl/private
    option forwardfor
    reqadd X-Forwarded-Proto:\ https
    acl test_site1 hdr(host) -i test_site1.local
    use_backend test_site1_back if test_site1
    etc. etc.

The haproxy works just fine - no problem there.

Part of my clean up script looks like this:

haproxy_cfg = '/etc/haproxy.cfg'
haproxy_bak = '/etc/haproxy.bak'

parser = Parser(haproxy_cfg)
configuration = parser.build_configuration()
frontends = configuration.frontends
backends = configuration.backends

It gives the following error message: [line 287 is configuration = parser.build_configuration()]

Traceback (most recent call last): File "./cleaner.py", line 287, in configuration = parser.build_configuration() File "/blarblarblar-manyfolders/python2.7/site-packages/pyhaproxy/parse.py", line 41, in build_configuration self.build_frontend(section_node)) File "/blarblarblar-manyfolders/python2.7/site-packages/pyhaproxy/parse.py", line 203, in build_frontend 'Not specify host and port in frontend definition') Exception: Not specify host and port in frontend definition Segmentation fault

What am I doing wrong or missing?

Regards - Lars

imjoey commented 5 years ago

Thanks for your issue. I can not reproduce the error using your frontend configurations via the following code. Could you please run nosetests -sv to take a test in your local environment and see the results, as well as your pyhaproxy version?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pyhaproxy.parse as parse

class TestParse(object):

    @classmethod
    def setup_class(cls):
        pass

    @classmethod
    def teardown_class(cls):
        pass

    def setup(self):
        filestring = r"""
frontend http
    bind 0.0.0.0:80
    acl test_site1 hdr(host) -i test_site1.local
    use_backend test_site1_back if test_site1

frontend https
    bind 0.0.0.0:443 ssl crt /etc/ssl/private
    option forwardfor
    reqadd X-Forwarded-Proto:\ https
    acl test_site1 hdr(host) -i test_site1.local
    use_backend test_site1_back if test_site1

"""
        self.parser = parse.Parser(filestring=filestring)
        self.configration = self.parser.build_configuration()

    def teardown(self):
        pass

    def test_parse_frontend_section(self):
        for frontend in self.configration.frontends:
            print '-' * 15
            print frontend.name, frontend.host, frontend.port
            for acl in frontend.acls():
                print acl
dbclkh commented 5 years ago

Hi Joey,

The version I'm using is 1.7.5-2 2017/05/17. I'll have a look at the nosetests and then get back to you.

Regards, Lars Hansen

dbclkh commented 5 years ago

Hi Joey,

larshansen@<comp-name> Downloads $ python3 /usr/local/bin/nosetests -sv --all-modules testpyha.py
testpyha.TestParse.test_parse_frontend_section ... ---------------
http 0.0.0.0 80
<acl_line: acl test_site1 hdr(host) -i test_site1.local>
---------------
https 0.0.0.0 443
<acl_line: acl test_site1 hdr(host) -i test_site1.local>
ok

----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

That seems okay to me?

imjoey commented 5 years ago

Hi, yes and the passed test means that the two frontend sections could be parsed correctly. Would you mind providing all the configurations? Thanks.

BTW: I noticed that the python verions is 3. While to be honest, this library has not been well tested in Python 3 yet. Could you please run it in Python 2 and see the results instead? Apologize for the inconvenience.

dbclkh commented 5 years ago

Hi Joey, I've tested it on another machine here at my company - it works fine there. It must be something on the first machine then.

imjoey commented 5 years ago

So glad to hear that. 😄