18đź‘Ť
Create a main settings.py file, which should include this:
# Pull in hostname-based changes.
import socket
HOSTNAME = socket.gethostname().lower().split('.')[0].replace('-','')
try:
exec "from myproject.settings.host_%s import *" % HOSTNAME
except ImportError:
pass
# Pull in the local changes.
try:
from myproject.settings.local import *
except ImportError:
pass
Now you create a new settings file for each host name you care about. But these are really small. Each of your production server’s file just contains:
from myproject.settings.production import *
and your staging servers have:
from myproject.settings.staging import *
Now you can create a production.py file with production overrides for settings, a staging.py, and so on. You can make new files for each role a server plays.
Finally, you can create a local.py file on any machine (including developers’ machines) with local overrides, and mark this file as ignored by source control, so that changes don’t get checked in.
We’ve used this structure for years, it works really well.
4đź‘Ť
+1 for Ned’s answer, but want to mention a slight variation.
I see Django settings as falling into 2 areas: project and instance.
Project settings are common to all the instances (dev, testing, production, multiple sites in production) and instance settings are local to just that specific server instance.
Project settings are things like INSTALLED_APPS
(although local settings may also include this to add things like django debug toolbar for developers), MIDDLEWARE_CLASSES
, and TEMPLATE_LOADERS
. Instance settings are things like the database settings, MEDIA_URL
settings, etc.
Project settings go in settings.py
and instance settings in local_settings.py
, which is imported into settings.py
. local_settings.py
is listed in .gitignore
and project settings are stored in git.
I’ve tried several other variations on this but ended here because it’s just so much simpler.
The only time I don’t like this setup is for multiple sites (using Django sites framework), which end up proliferating into things like sitename_settings.py
which imports sitename_local_settings.py
etc.
Finally, I do keep a local_settings_template.py
in git, to use as a starting point for new instances and for devs to track changes they might need to their own local settings.
- [Django]-Django 1.8 ArrayField append & extend
- [Django]-AWS Cognito as Django authentication back-end for web site
- [Django]-How to assign items inside a Model object with Django?
3đź‘Ť
Lets separate those two distinct problems: 1) managing site-specific settings and 2) managing secrets.
1) Site-specific settings
Version everything (except secrets), even developer-specific settings.
With Django and a lot of other softwares, the configuration file is a piece of executable code, which makes it easy to load common configuration settings and override whatever needs to be overridden. This way you can stay DRY.
# settings_prod.py
from settings_base import *
... # override whatever needs to be overridden for production environment
So now you have settings_base.py
, settings_prod.py
, settings_dev.py
, settings_developper_john.py
, etc. How do you tell Django which one to use?
Deploying the appropriate settings file to the server is a task for the deployment script, I believe. The deployment script would know that you’re deploying to host prod17 which is a production server, so it would generate on the fly a settings.py
file that would look like this:
# settings.py (generated by deployment script)
from settings_prod import *
Another solution is to have that logic in a generic settings.py
: it could read an environment variable or get the host name (or apply any other logic) and load the appropriate settings module:
# settings.py
import os
if os.environ["MY_APP_ENV"] == "prod":
from settings_prod import *
elif ...
My favorite solution for Django settings is described here.
For any other software that is not as flexible with it’s configuration file, the best option is probably to have the deployment script generate the configuration file, possibly using templates (tools like Chef or Puppet make this easy). This allows you to stay DRY: for example, say a software requires a flat config.ini
file, then the deployment script could read a common.ini
and a production.ini
file, mix them together appropriately and produce a config.ini
ready to be deployed to production.
Managing secrets
First of all, do not store your passwords in a version control system. 🙂
One solution for managing secrets is to have the deployment script transfert the secrets. For example, bob is responsible for the deployment of web applications, he knows the password to the database, so when he launches the deployment script, he is prompted for the database password, and the script transfers it to the server. Or the deployment script simply reads the password in a file on bob’s computer and transfers it. This is probably the most common solution. It’s fine in most cases.
secrets
deployer ================> server
If you need to automate the creation of VMs and you do not want the automated-deployer to know any secret, then you could include the secrets in the VM-image. Of course someone must include the secrets in the VM image in the first place.
VM image including secrets
human deployer -------------------------------+
|
|
image_name v
automated deployer ==============> Cloud Service ========> VM including secrets
The problem with this solution is that you need to generate a new VM image every time any secret changes. If you want to avoid that, then you might want a “secret-server”: a server to manage every other server’s secrets. Then the only secret you need to include in the VM image is the bootstrap secret needed to connect to the “secret-server”.
step 1:
VM image including bootstrap secret
human deployer -----------------------------------+
|
|
image_name v
automated deployer ==================> Cloud Service ========> VM including secrets
step 2:
bootstrap secret
==================>
VM Secret Server
<==================
secrets
For example, the secret server could be a Chef server, the secrets could be store in encrypted data bags, and the bootstrap secret would be the key to decrypt these bags.
- [Django]-AccessDenied when calling the CreateMultipartUpload operation in Django using django-storages and boto3
- [Django]-How to add readonly inline on django admin
- [Django]-How to receive json data using HTTP POST request in Django 1.6?
0đź‘Ť
The way I handle this is to have a base settings.py file and then a settings file for each environment (eg dev_settings.py, live_settings.py)
At the top of each environment specific file I have
from settings import *
I can then simply override any settings I need to change on an environment specific basis
To ensure each environment uses the correct settings I just modify the DJANGO_SETTINGS_MODULE environment variable. How you do this depends how you are deploying django (mod_wsgi, mod_python and so on…)
- [Django]-How to get field type string from db model in django
- [Django]-Passing arguments to model methods in Django templates
- [Django]-Add Text on Image using PIL