Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config blocks are not generated in order #8

Open
farzadanooshah opened this issue Nov 7, 2018 · 2 comments
Open

config blocks are not generated in order #8

farzadanooshah opened this issue Nov 7, 2018 · 2 comments

Comments

@farzadanooshah
Copy link

Hi,

I came across this library a couple of days ago and it's really helpful. However, I've got into an issue with the library not maintaining the order of config blocks. Some nginx configs are sensitive to their position in file. for e.g. 'log_format' that I use should be defined before it's used in access_log.

Any suggestion how to approach such issue?

Thanks

@epeay
Copy link

epeay commented Nov 8, 2018

Hi @farzadanooshah could you provide a small code sample which results in an invalid config when run repeatedly?

@farzadanooshah
Copy link
Author

Hi @epeay,

Sorry for the late reply. Here is a sample code which shows how orders can be mixed and may end up in invalid format:

from nginx.config.builder import NginxConfigBuilder
from nginx.config.api import Section, Config


http = Section('http',
        server_tokens='off')

logfmt_1 = Config(log_format='fmt1 "$request"')
logfmt_2 = Config(log_format='fmt2 "$http_agent"')
logfmt_3 = Config(log_format='fmt3 "$http_agent"')
server = Section('server',
        Config(access_log='/dev/null fmt1'),
        Config(access_log='/dev/null fmt2'),
        Config(access_log='/dev/null fmt3'),
        listen='*:8080')

http.sections.add(logfmt_1, logfmt_2, logfmt_3, server)

print Config( Section('events'), http)

which results in following invalid config, test with nginx -t:

http {
    server_tokens off;
    log_format fmt2 "$http_agent";
    server {
        listen *:8080;
        access_log /dev/null fmt2;
        access_log /dev/null fmt1;
        access_log /dev/null fmt3;
    }
    log_format fmt1 "$request";
    log_format fmt3 "$http_agent";
}
events {
}

but if you just skip adding logfmt_3, i.e. http.sections.add(logfmt_1, logfmt_2, server) it results ina valid config as both log_format would be printed on top of server.

This should be due to use of dictionary in which order is not preserved and so number of directives/child blocks affects the iteration.

I didn't quite get why directives are stored in a dictionary instead of a list :-?
I was thinking that Block class may be implemented using a list that stores tuples in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants