mirror of
https://github.com/ansible/awx.git
synced 2024-10-31 23:51:09 +03:00
Know how to spot an HA environment.
This commit is contained in:
parent
ac4bde69cc
commit
bc763eab57
23
awx/main/ha.py
Normal file
23
awx/main/ha.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# Copyright (c) 2014 Ansible, Inc.
|
||||||
|
# All Rights Reserved.
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
from awx.main.models import Instance
|
||||||
|
|
||||||
|
|
||||||
|
def is_ha_environment():
|
||||||
|
"""Return True if this is an HA environment, and False
|
||||||
|
otherwise.
|
||||||
|
"""
|
||||||
|
# If there are two or more instances, then we are in an HA environment.
|
||||||
|
if Instance.objects.count() > 1:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# 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'):
|
||||||
|
return True
|
||||||
|
|
||||||
|
# We are not in an HA environment.
|
||||||
|
return False
|
@ -1,4 +1,4 @@
|
|||||||
# Copyright (c) 2014 AnsibleWorks, Inc.
|
# Copyright (c) 2014 Ansible, Inc.
|
||||||
# All Rights Reserved.
|
# All Rights Reserved.
|
||||||
|
|
||||||
import json
|
import json
|
||||||
|
@ -21,10 +21,13 @@ from Crypto.Cipher import AES
|
|||||||
# ZeroMQ
|
# ZeroMQ
|
||||||
import zmq
|
import zmq
|
||||||
|
|
||||||
|
|
||||||
__all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore',
|
__all__ = ['get_object_or_400', 'get_object_or_403', 'camelcase_to_underscore',
|
||||||
'get_ansible_version', 'get_awx_version', 'update_scm_url',
|
'get_ansible_version', 'get_awx_version', 'update_scm_url',
|
||||||
'get_type_for_model', 'get_model_for_type', 'ignore_inventory_computed_fields',
|
'get_type_for_model', 'get_model_for_type',
|
||||||
'ignore_inventory_group_removal', '_inventory_updates']
|
'ignore_inventory_computed_fields', 'ignore_inventory_group_removal',
|
||||||
|
'_inventory_updates']
|
||||||
|
|
||||||
|
|
||||||
def get_object_or_400(klass, *args, **kwargs):
|
def get_object_or_400(klass, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
@ -40,6 +43,7 @@ def get_object_or_400(klass, *args, **kwargs):
|
|||||||
except queryset.model.MultipleObjectsReturned, e:
|
except queryset.model.MultipleObjectsReturned, e:
|
||||||
raise ParseError(*e.args)
|
raise ParseError(*e.args)
|
||||||
|
|
||||||
|
|
||||||
def get_object_or_403(klass, *args, **kwargs):
|
def get_object_or_403(klass, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Return a single object from the given model or queryset based on the query
|
Return a single object from the given model or queryset based on the query
|
||||||
@ -54,6 +58,7 @@ def get_object_or_403(klass, *args, **kwargs):
|
|||||||
except queryset.model.MultipleObjectsReturned, e:
|
except queryset.model.MultipleObjectsReturned, e:
|
||||||
raise PermissionDenied(*e.args)
|
raise PermissionDenied(*e.args)
|
||||||
|
|
||||||
|
|
||||||
def camelcase_to_underscore(s):
|
def camelcase_to_underscore(s):
|
||||||
'''
|
'''
|
||||||
Convert CamelCase names to lowercase_with_underscore.
|
Convert CamelCase names to lowercase_with_underscore.
|
||||||
@ -61,6 +66,7 @@ def camelcase_to_underscore(s):
|
|||||||
s = re.sub(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', s)
|
s = re.sub(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', s)
|
||||||
return s.lower().strip('_')
|
return s.lower().strip('_')
|
||||||
|
|
||||||
|
|
||||||
class RequireDebugTrueOrTest(logging.Filter):
|
class RequireDebugTrueOrTest(logging.Filter):
|
||||||
'''
|
'''
|
||||||
Logging filter to output when in DEBUG mode or running tests.
|
Logging filter to output when in DEBUG mode or running tests.
|
||||||
@ -70,6 +76,7 @@ class RequireDebugTrueOrTest(logging.Filter):
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
return settings.DEBUG or 'test' in sys.argv
|
return settings.DEBUG or 'test' in sys.argv
|
||||||
|
|
||||||
|
|
||||||
def get_ansible_version():
|
def get_ansible_version():
|
||||||
'''
|
'''
|
||||||
Return Ansible version installed.
|
Return Ansible version installed.
|
||||||
@ -83,6 +90,7 @@ def get_ansible_version():
|
|||||||
except:
|
except:
|
||||||
return 'unknown'
|
return 'unknown'
|
||||||
|
|
||||||
|
|
||||||
def get_awx_version():
|
def get_awx_version():
|
||||||
'''
|
'''
|
||||||
Return Ansible Tower version as reported by setuptools.
|
Return Ansible Tower version as reported by setuptools.
|
||||||
@ -94,6 +102,7 @@ def get_awx_version():
|
|||||||
except:
|
except:
|
||||||
return __version__
|
return __version__
|
||||||
|
|
||||||
|
|
||||||
def get_encryption_key(instance, field_name):
|
def get_encryption_key(instance, field_name):
|
||||||
'''
|
'''
|
||||||
Generate key for encrypted password based on instance pk and field name.
|
Generate key for encrypted password based on instance pk and field name.
|
||||||
@ -105,6 +114,7 @@ def get_encryption_key(instance, field_name):
|
|||||||
h.update(field_name)
|
h.update(field_name)
|
||||||
return h.digest()[:16]
|
return h.digest()[:16]
|
||||||
|
|
||||||
|
|
||||||
def encrypt_field(instance, field_name, ask=False):
|
def encrypt_field(instance, field_name, ask=False):
|
||||||
'''
|
'''
|
||||||
Return content of the given instance and field name encrypted.
|
Return content of the given instance and field name encrypted.
|
||||||
@ -121,6 +131,7 @@ def encrypt_field(instance, field_name, ask=False):
|
|||||||
b64data = base64.b64encode(encrypted)
|
b64data = base64.b64encode(encrypted)
|
||||||
return '$encrypted$%s$%s' % ('AES', b64data)
|
return '$encrypted$%s$%s' % ('AES', b64data)
|
||||||
|
|
||||||
|
|
||||||
def decrypt_field(instance, field_name):
|
def decrypt_field(instance, field_name):
|
||||||
'''
|
'''
|
||||||
Return content of the given instance and field name decrypted.
|
Return content of the given instance and field name decrypted.
|
||||||
@ -137,6 +148,7 @@ def decrypt_field(instance, field_name):
|
|||||||
value = cipher.decrypt(encrypted)
|
value = cipher.decrypt(encrypted)
|
||||||
return value.rstrip('\x00')
|
return value.rstrip('\x00')
|
||||||
|
|
||||||
|
|
||||||
def update_scm_url(scm_type, url, username=True, password=True,
|
def update_scm_url(scm_type, url, username=True, password=True,
|
||||||
check_special_cases=True):
|
check_special_cases=True):
|
||||||
'''
|
'''
|
||||||
@ -233,6 +245,7 @@ def update_scm_url(scm_type, url, username=True, password=True,
|
|||||||
parts.query, parts.fragment])
|
parts.query, parts.fragment])
|
||||||
return new_url
|
return new_url
|
||||||
|
|
||||||
|
|
||||||
def model_instance_diff(old, new, serializer_mapping=None):
|
def model_instance_diff(old, new, serializer_mapping=None):
|
||||||
"""
|
"""
|
||||||
Calculate the differences between two model instances. One of the instances may be None (i.e., a newly
|
Calculate the differences between two model instances. One of the instances may be None (i.e., a newly
|
||||||
@ -283,6 +296,7 @@ def model_instance_diff(old, new, serializer_mapping=None):
|
|||||||
|
|
||||||
return diff
|
return diff
|
||||||
|
|
||||||
|
|
||||||
def model_to_dict(obj, serializer_mapping=None):
|
def model_to_dict(obj, serializer_mapping=None):
|
||||||
"""
|
"""
|
||||||
Serialize a model instance to a dictionary as best as possible
|
Serialize a model instance to a dictionary as best as possible
|
||||||
@ -309,6 +323,7 @@ def model_to_dict(obj, serializer_mapping=None):
|
|||||||
attr_d[field.name] = "hidden"
|
attr_d[field.name] = "hidden"
|
||||||
return attr_d
|
return attr_d
|
||||||
|
|
||||||
|
|
||||||
def get_type_for_model(model):
|
def get_type_for_model(model):
|
||||||
'''
|
'''
|
||||||
Return type name for a given model class.
|
Return type name for a given model class.
|
||||||
@ -317,6 +332,7 @@ def get_type_for_model(model):
|
|||||||
opts = get_concrete_model(model)._meta
|
opts = get_concrete_model(model)._meta
|
||||||
return camelcase_to_underscore(opts.object_name)
|
return camelcase_to_underscore(opts.object_name)
|
||||||
|
|
||||||
|
|
||||||
def get_model_for_type(type):
|
def get_model_for_type(type):
|
||||||
'''
|
'''
|
||||||
Return model class for a given type name.
|
Return model class for a given type name.
|
||||||
@ -331,6 +347,7 @@ def get_model_for_type(type):
|
|||||||
if type == ct_type:
|
if type == ct_type:
|
||||||
return ct_model
|
return ct_model
|
||||||
|
|
||||||
|
|
||||||
def get_system_task_capacity():
|
def get_system_task_capacity():
|
||||||
'''
|
'''
|
||||||
Measure system memory and use it as a baseline for determining the system's capacity
|
Measure system memory and use it as a baseline for determining the system's capacity
|
||||||
@ -345,6 +362,7 @@ def get_system_task_capacity():
|
|||||||
return 50
|
return 50
|
||||||
return 50 + ((int(total_mem_value) / 1024) - 2) * 75
|
return 50 + ((int(total_mem_value) / 1024) - 2) * 75
|
||||||
|
|
||||||
|
|
||||||
def emit_websocket_notification(endpoint, event, payload):
|
def emit_websocket_notification(endpoint, event, payload):
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
if getattr(settings, 'SOCKETIO_NOTIFICATION_PORT', None):
|
if getattr(settings, 'SOCKETIO_NOTIFICATION_PORT', None):
|
||||||
@ -357,6 +375,7 @@ def emit_websocket_notification(endpoint, event, payload):
|
|||||||
|
|
||||||
_inventory_updates = threading.local()
|
_inventory_updates = threading.local()
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def ignore_inventory_computed_fields():
|
def ignore_inventory_computed_fields():
|
||||||
'''
|
'''
|
||||||
@ -369,6 +388,7 @@ def ignore_inventory_computed_fields():
|
|||||||
finally:
|
finally:
|
||||||
_inventory_updates.is_updating = previous_value
|
_inventory_updates.is_updating = previous_value
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextlib.contextmanager
|
||||||
def ignore_inventory_group_removal():
|
def ignore_inventory_group_removal():
|
||||||
'''
|
'''
|
||||||
|
Loading…
Reference in New Issue
Block a user