1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

robust check for HA environment setup

This commit is contained in:
Chris Meyers 2015-06-23 11:21:00 -04:00
parent 0344a801fe
commit 4669c44af9
2 changed files with 67 additions and 8 deletions

View File

@ -1,11 +1,16 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved.
# Python
import os
from IPy import IP
# Django
from django.conf import settings
# AWX
from awx.main.models import Instance
def is_ha_environment():
"""Return True if this is an HA environment, and False
otherwise.
@ -16,8 +21,24 @@ def is_ha_environment():
# If the database is not local, then we are in an HA environment.
host = settings.DATABASES['default'].get('HOST', 'localhost')
if host and host.lower() not in ('127.0.0.1', 'localhost') and not host.startswith('/'):
return True
# We are not in an HA environment.
# Is host special case 'localhost' ?
if host is 'localhost':
return False
# Check if host is an absolute file (i.e. named socket)
if os.path.isabs(host):
return False
# Is host a LOCAL or REMOTE ip address ?
try:
if IP(host).iptype() is 'LOOPBACK':
return False
else:
return True
except ValueError:
pass
# host may be a domain name like postgres.mycompany.com
# Assume HA Environment
return True

View File

@ -13,15 +13,38 @@ from awx.main.ha import * # noqa
__all__ = ['HAUnitTest',]
TEST_LOCALHOST = {
'default': {
'HOST': 'localhost'
}
}
TEST_127_0_0_1 = {
'default': {
'HOST': '127.0.0.1'
}
}
TEST_FILE = {
'HOST': '/i/might/be/a/file',
'default': {
'HOST': '/i/might/be/a/file'
}
}
TEST_DOMAIN = {
'default': {
'HOST': 'postgres.mycompany.com'
}
}
TEST_REMOTE_IP = {
'default': {
'HOST': '8.8.8.8'
}
}
TEST_EMPTY = {
'default': {
}
}
class HAUnitTest(SimpleTestCase):
@ -44,3 +67,18 @@ class HAUnitTest(SimpleTestCase):
@mock.patch.dict('django.conf.settings.DATABASES', TEST_FILE)
def test_db_file_socket(self, ignore):
self.assertFalse(is_ha_environment())
@mock.patch('awx.main.models.Instance.objects.count', return_value=1)
@mock.patch.dict('django.conf.settings.DATABASES', TEST_DOMAIN)
def test_db_domain(self, ignore):
self.assertTrue(is_ha_environment())
@mock.patch('awx.main.models.Instance.objects.count', return_value=1)
@mock.patch.dict('django.conf.settings.DATABASES', TEST_REMOTE_IP)
def test_db_remote_ip(self, ignore):
self.assertTrue(is_ha_environment())
@mock.patch('awx.main.models.Instance.objects.count', return_value=1)
@mock.patch.dict('django.conf.settings.DATABASES', TEST_EMPTY)
def test_db_empty(self, ignore):
self.assertFalse(is_ha_environment())