Is there any way to host django website in maintenance mode using fabric?

I am currently using MaintenanceModeMiddleware to put my site in maintenance mode, but this requires changes to the settings.py file on the remote server. I would like to use fabric to host the site remotely in maintenance mode. Is there any way to achieve this? Or is there a better way to do this? Thanks.

[UPDATE]

Thanks for the feedback to everyone, after all, this is what I did, and it works great for me, http://garthhumphreys.com/2011/06/11/painless-django-maintenance-mode-with-fabric/ - I like the idea of ​​splits, but with my setup, if I did this on the production server, it would be overwritten as soon as I pushed the new version, so, in the end, placing the site in maintenance mode from the server level, and not at the level django, works much better and is really simpler and more flexible, for me at least :)

+7
source share
3 answers

The fabric has commands to help you comment out or uncomment lines in a given file in fabric.contrib.files . See Docs here: http://docs.fabfile.org/en/1.0.1/api/contrib/files.html

Personally, I prefer to handle this in the front-end proxy rather than in the Django middleware. I would consider this question Show a custom page 503 if upstream down , which configures Nginx to use a custom page when the upstream is down .

+8
source

My decision:

  • Create a service mode template and bind it to it via urlconf, therefore it is displayed when visiting / servicing / the service page.
  • Then configure apache to check for the existence of a “Maintenance-mode-on” file, and if present, will redirect 302 the URL of the maintenance mode page.
  • Configure apache to redirect from the service mode URL to the home page if a service-off file is present.
  • A fabric script to facilitate file switching between service modes and service mode.

Here is the relevant section of the Apache configuration file:

 RewriteEngine On # If this file (toggle file) exists then put the site into maintenance mode RewriteCond /path/to/toggle/file/maintenance-mode-on -f RewriteCond %{REQUEST_URI} !^/static.* RewriteCond %{REQUEST_URI} !^/admin.* RewriteCond %{REQUEST_URI} !^/under-maintenance/ # redirect to the maintenance mode page RewriteRule ^(.*) /under-maintenance/ [R,L] #If not under maintenance mode, redirect away from the maintenance page RewriteCond /path/to/toggle/file/maintenance-mode-off -f RewriteCond %{REQUEST_URI} ^/under-maintenance/ RewriteRule ^(.*) / [R,L] 

Then the relevant parts of the fabric script:

 env.var_dir = '/path/to/toggle/file/' def is_in_mm(): "Returns whether the site is in maintenance mode" return files.exists(os.path.join(env.var_dir, 'maintenance-mode-on')) @task def mm_on(): """Turns on maintenance mode""" if not is_in_mm(): with cd(env.var_dir): run('mv maintenance-mode-off maintenance-mode-on') utils.fastprint('Turned on maintenance mode.') else: utils.error('The site is already in maintenance mode!') @task def mm_off(): """Turns off maintenance mode""" if is_in_mm(): with cd(env.var_dir): run('mv maintenance-mode-on maintenance-mode-off') utils.fastprint('Turned off maintenance mode.') else: utils.error('The site is not in maintenance mode!') 

This works well, although it depends on the handling of Django requests in maintenance mode; it would be nice to just serve a static file.

+4
source

There is a django-maintenancemode plug that allows you to enable / disable maintenance mode by setting a value in the database. Thus, you can, for example, create a simple control command to switch the service mode and call it through the fabric. I find it more flexible than using mod_rewrite.

+2
source

All Articles