Skip to content

Commit

Permalink
Add ability to pass settings to GUNICORN_CMD_ARGS. (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani authored and berkerpeksag committed Jan 8, 2017
1 parent 7dd8793 commit 3f9eace
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions THANKS
Expand Up @@ -163,3 +163,4 @@ Jason Madden <jason@nextthought.com>
Eugene Obukhov <irvind25@gmail.com>
Jan-Philip Gehrcke <jgehrcke@googlemail.com>
Mark Adams <mark@markadams.me>
Hasan Ramezani <hasan.r67@gmail.com>
11 changes: 11 additions & 0 deletions gunicorn/app/base.py
Expand Up @@ -154,6 +154,17 @@ def load_config(self):
if default_config is not None:
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
# settings.
for k, v in args.__dict__.items():
Expand Down
6 changes: 6 additions & 0 deletions gunicorn/config.py
Expand Up @@ -18,6 +18,7 @@
import ssl
import sys
import textwrap
import shlex

from gunicorn import __version__
from gunicorn import _compat
Expand Down Expand Up @@ -69,6 +70,11 @@ def set(self, name, value):
raise AttributeError("No configuration setting for: %s" % name)
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):
kwargs = {
"usage": self.usage,
Expand Down
21 changes: 21 additions & 0 deletions tests/test_config.py
Expand Up @@ -285,3 +285,24 @@ def test_always_use_configured_logger():
c.set('statsd_host', 'localhost:12345')
# still uses custom logger over statsd
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

0 comments on commit 3f9eace

Please sign in to comment.