mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
Add ability to pass settings to GUNICORN_CMD_ARGS. (#1385)
This commit is contained in:
parent
7dd8793dcf
commit
3f9eace246
1
THANKS
1
THANKS
@ -163,3 +163,4 @@ Jason Madden <jason@nextthought.com>
|
|||||||
Eugene Obukhov <irvind25@gmail.com>
|
Eugene Obukhov <irvind25@gmail.com>
|
||||||
Jan-Philip Gehrcke <jgehrcke@googlemail.com>
|
Jan-Philip Gehrcke <jgehrcke@googlemail.com>
|
||||||
Mark Adams <mark@markadams.me>
|
Mark Adams <mark@markadams.me>
|
||||||
|
Hasan Ramezani <hasan.r67@gmail.com>
|
||||||
|
|||||||
@ -154,6 +154,17 @@ class Application(BaseApplication):
|
|||||||
if default_config is not None:
|
if default_config is not None:
|
||||||
self.load_config_from_file(default_config)
|
self.load_config_from_file(default_config)
|
||||||
|
|
||||||
|
# Load up environment configuration
|
||||||
|
env_vars = self.cfg.get_cmd_args_from_env()
|
||||||
|
if env_vars:
|
||||||
|
env_args = parser.parse_args(env_vars)
|
||||||
|
for k, v in vars(env_args).items():
|
||||||
|
if v is None:
|
||||||
|
continue
|
||||||
|
if k == "args":
|
||||||
|
continue
|
||||||
|
self.cfg.set(k.lower(), v)
|
||||||
|
|
||||||
# Lastly, update the configuration with any command line
|
# Lastly, update the configuration with any command line
|
||||||
# settings.
|
# settings.
|
||||||
for k, v in args.__dict__.items():
|
for k, v in args.__dict__.items():
|
||||||
|
|||||||
@ -18,6 +18,7 @@ import re
|
|||||||
import ssl
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
import textwrap
|
import textwrap
|
||||||
|
import shlex
|
||||||
|
|
||||||
from gunicorn import __version__
|
from gunicorn import __version__
|
||||||
from gunicorn import _compat
|
from gunicorn import _compat
|
||||||
@ -69,6 +70,11 @@ class Config(object):
|
|||||||
raise AttributeError("No configuration setting for: %s" % name)
|
raise AttributeError("No configuration setting for: %s" % name)
|
||||||
self.settings[name].set(value)
|
self.settings[name].set(value)
|
||||||
|
|
||||||
|
def get_cmd_args_from_env(self):
|
||||||
|
if 'GUNICORN_CMD_ARGS' in self.env_orig:
|
||||||
|
return shlex.split(self.env_orig['GUNICORN_CMD_ARGS'])
|
||||||
|
return []
|
||||||
|
|
||||||
def parser(self):
|
def parser(self):
|
||||||
kwargs = {
|
kwargs = {
|
||||||
"usage": self.usage,
|
"usage": self.usage,
|
||||||
|
|||||||
@ -285,3 +285,24 @@ def test_always_use_configured_logger():
|
|||||||
c.set('statsd_host', 'localhost:12345')
|
c.set('statsd_host', 'localhost:12345')
|
||||||
# still uses custom logger over statsd
|
# still uses custom logger over statsd
|
||||||
assert c.logger_class == MyLogger
|
assert c.logger_class == MyLogger
|
||||||
|
|
||||||
|
|
||||||
|
def test_load_enviroment_variables_config(monkeypatch):
|
||||||
|
monkeypatch.setenv("GUNICORN_CMD_ARGS", "--workers=4")
|
||||||
|
with AltArgs():
|
||||||
|
app = NoConfigApp()
|
||||||
|
assert app.cfg.workers == 4
|
||||||
|
|
||||||
|
|
||||||
|
def test_invalid_enviroment_variables_config(monkeypatch):
|
||||||
|
monkeypatch.setenv("GUNICORN_CMD_ARGS", "--foo=bar")
|
||||||
|
with AltArgs():
|
||||||
|
with pytest.raises(SystemExit):
|
||||||
|
NoConfigApp()
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_overrides_enviroment_variables_module(monkeypatch):
|
||||||
|
monkeypatch.setenv("GUNICORN_CMD_ARGS", "--workers=4")
|
||||||
|
with AltArgs(["prog_name", "-c", cfg_file(), "--workers", "3"]):
|
||||||
|
app = NoConfigApp()
|
||||||
|
assert app.cfg.workers == 3
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user