mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 09:27:36 -02:30
add logging to system tracking migration
This commit is contained in:
@@ -1,44 +1,54 @@
|
|||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
|
from django.utils.encoding import smart_text
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from awx.fact.models import FactVersion
|
from awx.fact.models import FactVersion
|
||||||
from awx.fact.utils.dbtransform import KeyTransform
|
from awx.fact.utils.dbtransform import KeyTransform
|
||||||
from mongoengine.connection import ConnectionError
|
from mongoengine.connection import ConnectionError
|
||||||
from pymongo.errors import OperationFailure
|
from pymongo.errors import OperationFailure
|
||||||
from django.conf import settings
|
|
||||||
|
|
||||||
def drop_system_tracking_db():
|
logger = logging.getLogger(__name__)
|
||||||
try:
|
|
||||||
db = FactVersion._get_db()
|
|
||||||
db.connection.drop_database(settings.MONGO_DB)
|
|
||||||
except ConnectionError:
|
|
||||||
# TODO: Log this. Not a deal-breaker. Just let the user know they
|
|
||||||
# may need to manually drop/delete the database.
|
|
||||||
pass
|
|
||||||
except OperationFailure:
|
|
||||||
# TODO: This means the database was up but something happened when we tried to query it
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
def log_migration(wrapped):
|
||||||
|
'''setup the logging mechanism for each migration method
|
||||||
|
as it runs, Django resets this, so we use a decorator
|
||||||
|
to re-add the handler for each method.
|
||||||
|
'''
|
||||||
|
handler = logging.FileHandler("/tmp/tower_system_tracking_migrations.log", mode="a", encoding="UTF-8")
|
||||||
|
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||||
|
handler.setLevel(logging.DEBUG)
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
logger.handlers = []
|
||||||
|
logger.addHandler(handler)
|
||||||
|
return wrapped(*args, **kwargs)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
@log_migration
|
||||||
def migrate_facts(apps, schema_editor):
|
def migrate_facts(apps, schema_editor):
|
||||||
Fact = apps.get_model('main', "Fact")
|
Fact = apps.get_model('main', "Fact")
|
||||||
Host = apps.get_model('main', "Host")
|
Host = apps.get_model('main', "Host")
|
||||||
|
|
||||||
if (not hasattr(settings, 'MONGO_HOST')) or settings.MONGO_HOST == NotImplemented:
|
if (not hasattr(settings, 'MONGO_HOST')) or settings.MONGO_HOST == NotImplemented:
|
||||||
|
logger.info("failed to find MONGO_HOST in settings. Will NOT attempt to migrate system_tracking data from Mongo to Postgres.")
|
||||||
# If settings do not specify a mongo database, do not raise error or drop db
|
# If settings do not specify a mongo database, do not raise error or drop db
|
||||||
return (0, 0)
|
return (0, 0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
n = FactVersion.objects.all().count()
|
n = FactVersion.objects.all().count()
|
||||||
except ConnectionError:
|
except ConnectionError:
|
||||||
# TODO: Let the user know about the error. Likely this is
|
# Let the user know about the error. Likely this is
|
||||||
# a new install and we just don't need to do this
|
# a new install and we just don't need to do this
|
||||||
|
logger.info(smart_text(u"failed to connect to mongo database host {}. Will NOT attempt to migrate system_tracking data from Mongo to Postgres.".format(settings.MONGO_HOST)))
|
||||||
return (0, 0)
|
return (0, 0)
|
||||||
except OperationFailure:
|
except OperationFailure:
|
||||||
# TODO: This means the database was up but something happened when we tried to query it
|
# The database was up but something happened when we tried to query it
|
||||||
|
logger.info(smart_text(u"failed to connect to issue Mongo query on host {}. Will NOT attempt to migrate system_tracking data from Mongo to Postgres.".format(settings.MONGO_HOST)))
|
||||||
return (0, 0)
|
return (0, 0)
|
||||||
|
|
||||||
# Migration already happened
|
|
||||||
if Fact.objects.all().count() > 0:
|
|
||||||
return (migrated_count, not_migrated_count)
|
|
||||||
|
|
||||||
migrated_count = 0
|
migrated_count = 0
|
||||||
not_migrated_count = 0
|
not_migrated_count = 0
|
||||||
transform = KeyTransform([('.', '\uff0E'), ('$', '\uff04')])
|
transform = KeyTransform([('.', '\uff0E'), ('$', '\uff04')])
|
||||||
@@ -49,8 +59,11 @@ def migrate_facts(apps, schema_editor):
|
|||||||
Fact.objects.create(host_id=host.id, timestamp=fact_obj.timestamp, module=fact_obj.module, facts=fact_obj.fact).save()
|
Fact.objects.create(host_id=host.id, timestamp=fact_obj.timestamp, module=fact_obj.module, facts=fact_obj.fact).save()
|
||||||
migrated_count += 1
|
migrated_count += 1
|
||||||
except Host.DoesNotExist:
|
except Host.DoesNotExist:
|
||||||
# TODO: Log this. No host was found to migrate the facts to.
|
# No host was found to migrate the facts to.
|
||||||
# This isn't a hard error. Just something the user would want to know.
|
# This isn't a hard error. Just something the user would want to know.
|
||||||
|
logger.info(smart_text(u"unable to migrate fact {} <inventory, hostname> not found in Postgres <{}, {}>".format(factver.id, factver.host.inventory_id, factver.host.hostname)))
|
||||||
not_migrated_count += 1
|
not_migrated_count += 1
|
||||||
|
|
||||||
|
logger.info(smart_text(u"successfully migrated {} records of system_tracking data from Mongo to Postgres. {} records not migrated due to corresponding <inventory, hostname> pairs not found in Postgres.".format(migrated_count, not_migrated_count)))
|
||||||
return (migrated_count, not_migrated_count)
|
return (migrated_count, not_migrated_count)
|
||||||
|
|
||||||
|
|||||||
@@ -9,9 +9,6 @@ from awx.main.models.fact import Fact
|
|||||||
|
|
||||||
from awx.main.migrations import _system_tracking as system_tracking
|
from awx.main.migrations import _system_tracking as system_tracking
|
||||||
|
|
||||||
from awx.fact.models.fact import Fact as FactMongo
|
|
||||||
from awx.fact.models.fact import FactVersion, FactHost
|
|
||||||
|
|
||||||
def micro_to_milli(micro):
|
def micro_to_milli(micro):
|
||||||
return micro - (((int)(micro / 1000)) * 1000)
|
return micro - (((int)(micro / 1000)) * 1000)
|
||||||
|
|
||||||
@@ -64,20 +61,3 @@ def test_migrate_facts_hostname_does_not_exist(inventories, hosts, hosts_mongo,
|
|||||||
assert len(fact) == 1
|
assert len(fact) == 1
|
||||||
assert fact[0] is not None
|
assert fact[0] is not None
|
||||||
|
|
||||||
@pytest.mark.skipif(not getattr(settings, 'MONGO_DB', None), reason="MongoDB not configured")
|
|
||||||
@pytest.mark.django_db
|
|
||||||
@pytest.mark.mongo_db
|
|
||||||
def test_drop_system_tracking_db(inventories, hosts, hosts_mongo, fact_scans):
|
|
||||||
inventory_objs = inventories(1)
|
|
||||||
hosts_mongo(1, inventory_objs)
|
|
||||||
fact_scans(1, inventory_objs)
|
|
||||||
|
|
||||||
assert FactMongo.objects.all().count() > 0
|
|
||||||
assert FactVersion.objects.all().count() > 0
|
|
||||||
assert FactHost.objects.all().count() > 0
|
|
||||||
|
|
||||||
system_tracking.drop_system_tracking_db()
|
|
||||||
|
|
||||||
assert FactMongo.objects.all().count() == 0
|
|
||||||
assert FactVersion.objects.all().count() == 0
|
|
||||||
assert FactHost.objects.all().count() == 0
|
|
||||||
|
|||||||
Reference in New Issue
Block a user