HA redirection.

This commit is contained in:
Luke Sneeringer
2014-10-02 14:03:09 -05:00
parent 0e623f2337
commit 1ae333ca1d
2 changed files with 47 additions and 7 deletions

View File

@@ -1,20 +1,27 @@
# Copyright (c) 2014 AnsibleWorks, Inc.
# All Rights Reserved.
import json
import logging
import threading
import uuid
import urllib2
from django.conf import settings
from django.contrib.auth.models import User, AnonymousUser
from django.db.models.signals import pre_save, post_save
from django.db import IntegrityError
from django.http import HttpResponseRedirect
from django.template.response import TemplateResponse
from django.utils.functional import curry
from awx.main.models import ActivityStream, AuthToken
import json
import threading
import uuid
import urllib2
import logging
from awx import __version__ as version
from awx.main.models import ActivityStream, AuthToken, Instance
logger = logging.getLogger('awx.main.middleware')
class ActivityStreamMiddleware(threading.local):
def __init__(self):
@@ -63,7 +70,7 @@ class ActivityStreamMiddleware(threading.local):
self.instance_ids.append(instance.id)
class HAMiddlware(object):
class HAMiddleware(object):
"""A middleware class that checks to see whether the request is being
served on a secondary instance, and redirects the request back to the
primary instance if so.
@@ -72,3 +79,23 @@ class HAMiddlware(object):
"""Process the request, and redirect if this is a request on a
secondary node.
"""
# Is this the primary node? If so, we can just return None and be done;
# we just want normal behavior in this case.
if Instance.objects.my_role() == 'primary':
return None
# Get the primary instance.
primary = Instance.objects.primary()
# If this is a request to /, then we return a special landing page that
# informs the user that they are on the secondary instance and will
# be redirected.
if request.path == '/':
return TemplateResponse(request, 'ha/redirect.html', {
'primary': primary,
'redirect_seconds': 30,
'version': version,
})
# Redirect to the base page of the primary instance.
return HttpResponseRedirect('http://%s/' % primary.ip_address)

View File

@@ -0,0 +1,13 @@
<html>
<head>
<title>Ansible Tower: Secondary</title>
<meta http-equiv="refresh" content="{{ redirect_seconds }}; url=//{{ primary.ip_address }}/" />
</head>
<body>
<h1>Ansible Tower</h1>
<p>This Ansible Tower server (version {{ version }}) is in secondary
mode. The current master is {{ primary.ip_address }}.
</p>
<p>This page will redirect in {{ redirect_seconds }} seconds.</p>
</body>
</html>