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.
seddonym
source share