rename insight tracking field; handle duplicate

* rename from insights_machine_id to insights_system_id
* gracefully handle duplicate insights machine id's
This commit is contained in:
Chris Meyers
2017-05-12 10:47:49 -04:00
parent b9b0b29d97
commit 3cdc332497
4 changed files with 26 additions and 20 deletions

View File

@@ -12,6 +12,7 @@ from kombu.mixins import ConsumerMixin
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.conf import settings from django.conf import settings
from django.utils import timezone from django.utils import timezone
from django.db import IntegrityError
# AWX # AWX
from awx.main.models.jobs import Job from awx.main.models.jobs import Job
@@ -121,11 +122,16 @@ class FactBrokerWorker(ConsumerMixin):
if job.store_facts is True: if job.store_facts is True:
if module_name == 'insights': if module_name == 'insights':
try: system_id = facts.get('system_id', None)
host_obj.insights_machine_id = facts['machine_id'] if system_id:
host_obj.save() host_obj.insights_system_id = system_id
except StandardError: try:
logger.warn('Failed to find insights machine id in insights fact scan.') host_obj.save()
except IntegrityError:
host_obj.insights_system_id = None
logger.warn('Inisghts system_id %s not assigned to host %s because it already exists.' % (system_id, host_obj.pk))
else:
logger.warn('Failed to find insights system id in insights fact scan.')
self._do_gather_facts_update(host_obj, module_name, facts, self.timestamp) self._do_gather_facts_update(host_obj, module_name, facts, self.timestamp)
message.ack() message.ack()

View File

@@ -240,11 +240,11 @@ class Migration(migrations.Migration):
# Insights # Insights
migrations.AddField( migrations.AddField(
model_name='host', model_name='host',
name='insights_machine_id', name='insights_system_id',
field=models.TextField(default=None, help_text='Red Hat Insights host unique identifier.', null=True, db_index=True, blank=True), field=models.TextField(default=None, help_text='Red Hat Insights host unique identifier.', null=True, db_index=True, blank=True),
), ),
migrations.AlterUniqueTogether( migrations.AlterUniqueTogether(
name='host', name='host',
unique_together=set([('insights_machine_id', 'inventory'), ('name', 'inventory')]), unique_together=set([('insights_system_id', 'inventory'), ('name', 'inventory')]),
), ),
] ]

View File

@@ -350,7 +350,7 @@ class Host(CommonModelNameNotUnique):
class Meta: class Meta:
app_label = 'main' app_label = 'main'
unique_together = (("name", "inventory"), ("insights_machine_id", "inventory"),) # FIXME: Add ('instance_id', 'inventory') after migration. unique_together = (("name", "inventory"), ("insights_system_id", "inventory"),) # FIXME: Add ('instance_id', 'inventory') after migration.
ordering = ('name',) ordering = ('name',)
inventory = models.ForeignKey( inventory = models.ForeignKey(
@@ -411,7 +411,7 @@ class Host(CommonModelNameNotUnique):
default={}, default={},
help_text=_('Arbitrary JSON structure of most recent ansible_facts, per-host.'), help_text=_('Arbitrary JSON structure of most recent ansible_facts, per-host.'),
) )
insights_machine_id = models.TextField( insights_system_id = models.TextField(
blank=True, blank=True,
default=None, default=None,
null=True, null=True,

View File

@@ -20,29 +20,29 @@ EXAMPLES = '''
# host | success >> { # host | success >> {
# "ansible_facts": { # "ansible_facts": {
# "insights": { # "insights": {
# "machine_id": "4da7d1f8-14f3-4cdc-acd5-a3465a41f25d" # "system_id": "4da7d1f8-14f3-4cdc-acd5-a3465a41f25d"
# }, ... } # }, ... }
''' '''
INSIGHTS_MACHINE_ID_FILE='/etc/redhat-access-insights/machine-id' INSIGHTS_SYSTEM_ID_FILE='/etc/redhat-access-insights/machine-id'
def get_machine_uuid(filname): def get_system_uuid(filname):
machine_uuid = None system_uuid = None
try: try:
f = open(INSIGHTS_MACHINE_ID_FILE, "r") f = open(INSIGHTS_SYSTEM_ID_FILE, "r")
except IOError: except IOError:
return None return None
else: else:
try: try:
data = f.readline() data = f.readline()
machine_uuid = str(uuid.UUID(data)) system_uuid = str(uuid.UUID(data))
except (IOError, ValueError): except (IOError, ValueError):
pass pass
finally: finally:
f.close() f.close()
return machine_uuid return system_uuid
def main(): def main():
@@ -50,18 +50,18 @@ def main():
argument_spec = dict() argument_spec = dict()
) )
machine_uuid = get_machine_uuid(INSIGHTS_MACHINE_ID_FILE) system_uuid = get_system_uuid(INSIGHTS_SYSTEM_ID_FILE)
if machine_uuid is not None: if system_uuid is not None:
results = { results = {
'ansible_facts': { 'ansible_facts': {
'insights': { 'insights': {
'machine_id': machine_uuid 'system_id': system_uuid
} }
} }
} }
else: else:
results = dict(skipped=True, msg="Insights machine id not found") results = dict(skipped=True, msg="Insights system id not found")
module.exit_json(**results) module.exit_json(**results)