Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stop writing settings files

Stop writing settings files

Bruno Renié

May 15, 2013
Tweet

More Decks by Bruno Renié

Other Decks in Programming

Transcript

  1. local_settings # settings.py # settings goes here, and at the

    end… try: from local_settings import * except ImportError: pass
  2. # settings/base.py INSTALLED_APPS = … # settings/production.py from .base import

    * # override here with # production-specific settings Multiple settings files $ python manage.py shell --settings=settings.production
  3. — 12factor.net " " … strict separation of config from

    code. Config varies substantially across deploys,code does not.
  4. Expose your configuration as environment variables Derive your settings from

    the environment DEBUG = bool(os.environ.get('DEBUG', False)) Use one settings file
  5. daemontools' envdir path/to/env/ DATABASE_URL SENTRY_DSN … $ envdir path/to/env <child

    program> $ envdir path/to/env django-admin.py <command> Sane defaults for development kept under version control
  6. envdir in ur manage.py if __name__ == "__main__": if 'test'

    in sys.argv: env_dir = os.path.join('tests', 'envdir') else: env_dir = 'envdir' env_vars = glob.glob(os.path.join(env_dir, '*')) for env_var in env_vars: with open(env_var, 'r') as env_var_file: os.environ.setdefault(env_var.split(os.sep)[-1], env_var_file.read().strip()) from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
  7. One settings files, less moving parts Configuration as environment variables

    Production: set them with daemontools, circus, supervisor… Development: use envdir and/or a custom manage.py Your PaaS already supports them Easy to setup with Salt/Puppet/Chef Your sysadmin will thank you