diff --git a/awx/main/middleware.py b/awx/main/middleware.py index b553ffed87..b09d5405f5 100644 --- a/awx/main/middleware.py +++ b/awx/main/middleware.py @@ -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) diff --git a/awx/templates/ha/redirect.html b/awx/templates/ha/redirect.html new file mode 100644 index 0000000000..244575756f --- /dev/null +++ b/awx/templates/ha/redirect.html @@ -0,0 +1,13 @@ + + + Ansible Tower: Secondary + + + +

Ansible Tower

+

This Ansible Tower server (version {{ version }}) is in secondary + mode. The current master is {{ primary.ip_address }}. +

+

This page will redirect in {{ redirect_seconds }} seconds.

+ +