diff --git a/awx/main/management/commands/register_peers.py b/awx/main/management/commands/register_peers.py index 6d26ebfbb2..078edb08c7 100644 --- a/awx/main/management/commands/register_peers.py +++ b/awx/main/management/commands/register_peers.py @@ -27,7 +27,9 @@ class Command(BaseCommand): ) def handle(self, **options): + # provides a mapping of hostname to Instance objects nodes = Instance.objects.in_bulk(field_name='hostname') + if options['source'] not in nodes: raise CommandError(f"Host {options['source']} is not a registered instance.") if not (options['peers'] or options['disconnect'] or options['exact'] is not None): @@ -57,7 +59,9 @@ class Command(BaseCommand): results = 0 for target in options['peers']: - _, created = InstanceLink.objects.get_or_create(source=nodes[options['source']], target=nodes[target]) + _, created = InstanceLink.objects.update_or_create( + source=nodes[options['source']], target=nodes[target], defaults={'link_state': InstanceLink.States.ESTABLISHED} + ) if created: results += 1 @@ -80,7 +84,9 @@ class Command(BaseCommand): links = set(InstanceLink.objects.filter(source=nodes[options['source']]).values_list('target__hostname', flat=True)) removals, _ = InstanceLink.objects.filter(source=nodes[options['source']], target__hostname__in=links - peers).delete() for target in peers - links: - _, created = InstanceLink.objects.get_or_create(source=nodes[options['source']], target=nodes[target]) + _, created = InstanceLink.objects.update_or_create( + source=nodes[options['source']], target=nodes[target], defaults={'link_state': InstanceLink.States.ESTABLISHED} + ) if created: additions += 1 diff --git a/awx/main/managers.py b/awx/main/managers.py index 23acd15139..88e8384c43 100644 --- a/awx/main/managers.py +++ b/awx/main/managers.py @@ -129,10 +129,13 @@ class InstanceManager(models.Manager): # if instance was not retrieved by uuid and hostname was, use the hostname instance = self.filter(hostname=hostname) + from awx.main.models import Instance + # Return existing instance if instance.exists(): instance = instance.first() # in the unusual occasion that there is more than one, only get one - update_fields = [] + instance.node_state = Instance.States.INSTALLED # Wait for it to show up on the mesh + update_fields = ['node_state'] # if instance was retrieved by uuid and hostname has changed, update hostname if instance.hostname != hostname: logger.warning("passed in hostname {0} is different from the original hostname {1}, updating to {0}".format(hostname, instance.hostname)) @@ -141,6 +144,7 @@ class InstanceManager(models.Manager): # if any other fields are to be updated if instance.ip_address != ip_address: instance.ip_address = ip_address + update_fields.append('ip_address') if instance.node_type != node_type: instance.node_type = node_type update_fields.append('node_type') @@ -151,12 +155,12 @@ class InstanceManager(models.Manager): return (False, instance) # Create new instance, and fill in default values - create_defaults = dict(capacity=0) + create_defaults = {'node_state': Instance.States.INSTALLED, 'capacity': 0} if defaults is not None: create_defaults.update(defaults) uuid_option = {} if uuid is not None: - uuid_option = dict(uuid=uuid) + uuid_option = {'uuid': uuid} if node_type == 'execution' and 'version' not in create_defaults: create_defaults['version'] = RECEPTOR_PENDING instance = self.create(hostname=hostname, ip_address=ip_address, node_type=node_type, **create_defaults, **uuid_option)