Permalink
Browse files

Add ability to pass settings to GUNICORN_CMD_ARGS. (#1385)

  • Loading branch information...
1 parent 7dd8793 commit 3f9eace246fd11d0a88c3a65183e097cb6bbb7f7 @hramezani hramezani committed with berkerpeksag Jan 8, 2017
Showing with 39 additions and 0 deletions.
  1. +1 −0 THANKS
  2. +11 −0 gunicorn/app/base.py
  3. +6 −0 gunicorn/config.py
  4. +21 −0 tests/test_config.py
View
@@ -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>
View
@@ -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():
View
@@ -18,6 +18,7 @@
import ssl
import sys
import textwrap
+import shlex
from gunicorn import __version__
from gunicorn import _compat
@@ -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,
View
@@ -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.