Merge pull request #294 from chrismeyersfsu/feature-IPy

robust check for HA env setup
This commit is contained in:
Chris Meyers 2015-06-24 09:21:26 -04:00
commit 5eb4f51be9
4 changed files with 1720 additions and 8 deletions

1652
awx/lib/site-packages/IPy.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -76,3 +76,4 @@ simplejson==3.6.0 (simplejson/*, excluded simplejson/_speedups.so)
six==1.9.0 (six.py)
South==0.8.4 (south/*)
stevedor==1.3.0 (stevedor/*)
IPy==0.83 (IPy.py)

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.
return False
# 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 = {
'HOST': 'localhost'
'default': {
'HOST': 'localhost'
}
}
TEST_127_0_0_1 = {
'HOST': '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())