Repository navigation
add ability to pass in all config values as environment variables. - #1385
Conversation
69125d9 to
bb851af
Compare
241b7fa to
104d407
Compare
tilgovi
left a comment
There was a problem hiding this comment.
Overall, I like the idea, but interested to hear others' thoughts.
| for k,v in self.env_orig.items(): | ||
| if k.startswith('GUNICORN_'): | ||
| try: | ||
| name = k.split('GUNICORN_', 1)[1].lower() |
There was a problem hiding this comment.
I don't think this can ever fail if k.startswith('GUNICORN_').
|
|
||
| # Load up environment configuration | ||
| env_vars = self.cfg.parse_env_vars() | ||
| for k,v in env_vars.items(): |
There was a problem hiding this comment.
small linting nit here, space between ,v.
|
|
||
| def parse_env_vars(self): | ||
| env_vars = {} | ||
| for k,v in self.env_orig.items(): |
There was a problem hiding this comment.
Same nitpick here about spacing.
| name = k.split('GUNICORN_', 1)[1].lower() | ||
| except: | ||
| continue | ||
| if not name or name not in self.settings: |
There was a problem hiding this comment.
name not in self.settings should be sufficient, no?
104d407 to
d6a1da4
Compare
|
@tilgovi thanks, i fix problems. |
|
I also like the idea, thanks! I haven't reviewed the code yet, but we should document all |
| os.environ["GUNICORN_FOO"] = "BAR" | ||
| with AltArgs(): | ||
| app = NoConfigApp() | ||
| try: |
There was a problem hiding this comment.
This can be written as:
with pytest.raises(AttributeError):
app.cfg.too| assert c.logger_class == MyLogger | ||
|
|
||
| def test_load_enviroment_variables_config(): | ||
| os.environ["GUNICORN_WORKERS"] = "4" |
There was a problem hiding this comment.
We need to unset these environment variables. I don't know what's the best practice to do this in pytest though (I'd suggest writing a context manager if we were using unittest -- perhaps you need a fixture?)
|
@berkerpeksag Thanks, |
|
What is the use case for this change? Can you elaborate about it? |
|
@benoitc i saw
finally i decide to do this. with this change we can change gunicorn settings with environment variables. |
|
There are times, especially in these days of containerization, where it is much easier to change an environment variable than to change the command line invocation or a configuration file. |
|
We can do this in a separate PR, but I'm wondering if there is any reason to clear these keys from the environment when we spawn workers. I'm just wondering if any settings might contain information that we should try to prevent from leaking to workers and possibly then into a response. |
d6a1da4 to
5758b93
Compare
It would be nice to add something like "Environment variable: |
5758b93 to
ec85962
Compare
|
@berkerpeksag thanks for reply, I add documents. |
|
Thanks, |
| ~~~~~~ | ||
|
|
||
| * ``-c CONFIG, --config CONFIG`` | ||
| * ``GUNICORN_CONFIG (env variable)`` |
There was a problem hiding this comment.
"(env variable)" can be removed. I used it as an annotation in my example :)
|
@berkerpeksag Thanks, whta is your opinion about adding a property like
|
|
I'm not sure i'm comfortable with this change, it introduces an extra level of complexity in the configuration, having to document the variables to use is a sign. What about instead having a simple
So we wouldn't have to document each changes and could reuse the current code to handle it. Thoughts? |
45fc933 to
e6bab60
Compare
|
@benoitc Thanks, I change PR as your previous comment. |
|
Looks even better than the Apache 2.4 envvars support.. |
berkerpeksag
left a comment
There was a problem hiding this comment.
The PR looks pretty good to me, thanks! Just left some trivial comments.
We also need to document the new env variable.
| self.settings[name].set(value) | ||
|
|
||
| def get_gunicorn_env_var(self): | ||
| env_vars = [] |
There was a problem hiding this comment.
Unless I'm missing something obvious, env_vars is not needed here:
if 'GUNICORN_CMD_ARGS' in self.env_orig:
return self.env_orig['GUNICORN_CMD_ARGS'].split()
return []| def get_gunicorn_env_var(self): | ||
| env_vars = [] | ||
| if 'GUNICORN_CMD_ARGS' in self.env_orig: | ||
| env_vars = self.env_orig['GUNICORN_CMD_ARGS'].split() |
There was a problem hiding this comment.
Did you consider using shlex.split() here?
| raise AttributeError("No configuration setting for: %s" % name) | ||
| self.settings[name].set(value) | ||
|
|
||
| def get_gunicorn_env_var(self): |
There was a problem hiding this comment.
I'm not a native English speaker, but perhaps we can rename it to something like get_cmd_args_from_env? get_gunicorn_env_var sounds too generic to me. Ping @tilgovi :)
| env_vars = self.cfg.get_gunicorn_env_var() | ||
| if env_vars: | ||
| env_args = parser.parse_args(env_vars) | ||
| for k, v in env_args.__dict__.items(): |
There was a problem hiding this comment.
Wouldn't vars(env_args).items() be better?
| app = NoConfigApp() | ||
| assert app.cfg.workers == 4 | ||
|
|
||
| def test_cli_overrides_enviroment_variables_module(): |
There was a problem hiding this comment.
Tests are look great, thank you! :) Could you also add a test that passes an invalid config option (e.g os.environ["GUNICORN_CMD_ARGS"] = "--monty-python"?
|
@berkerpeksag Thanks, i fix all your comments. |
|
Looks great, thanks! I think you forgot to address two of my previous comments:
|
| os.environ["GUNICORN_CMD_ARGS"] = "--foo=bar" | ||
| with AltArgs(): | ||
| with pytest.raises(SystemExit): | ||
| app = NoConfigApp() |
There was a problem hiding this comment.
Style nit: No need to set NoConfigApp() to app. Just call NoConfigApp().
| c.set('statsd_host', 'localhost:12345') | ||
| # still uses custom logger over statsd | ||
| assert c.logger_class == MyLogger | ||
|
|
There was a problem hiding this comment.
Style nit: Please add two empty lines after each tests.
|
@berkerpeksag thanks!, comments fixed. |
|
Thanks for the PR, @hramezani! :)
Yes, that sounds good to me. Let's open another pull request for documentation. |
|
@berkerpeksag thanks very much for your help. i open another pull request for documentation soon. |
add environment variables that starts with
GUNICORN_likeGUNICORN_WORKERSas settings.skip variables that does not start with
GUNICORN_, and also skip variable that not insettingslikeGUNICORN_FOO