Skip to content

Commit

Permalink
if the user provides a uuid and it exists, allow that to tie to the i…
Browse files Browse the repository at this point in the history
…nstance, which allows the user to update the instance based on the UUID (includeding updating the hostname) should they choose to do so.
  • Loading branch information
rebeccahhh committed Sep 13, 2021
1 parent 1f34d4c commit 7691b2b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions awx/main/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,13 @@ class InstanceManager(models.Manager):
instance or role.
"""

UUID_DEFAULT = '00000000-0000-0000-0000-000000000000'

def me(self):
"""Return the currently active instance."""
# If we are running unit tests, return a stub record.
if settings.IS_TESTING(sys.argv) or hasattr(sys, '_called_from_test'):
return self.model(id=1, hostname=settings.CLUSTER_HOST_ID, uuid='00000000-0000-0000-0000-000000000000')
return self.model(id=1, hostname=settings.CLUSTER_HOST_ID, uuid=self.UUID_DEFAULT)

node = self.filter(hostname=settings.CLUSTER_HOST_ID)
if node.exists():
Expand All @@ -115,6 +117,7 @@ def me(self):
def register(self, uuid=None, hostname=None, ip_address=None, node_type='hybrid', defaults=None):
if not hostname:
hostname = settings.CLUSTER_HOST_ID

with advisory_lock('instance_registration_%s' % hostname):
if settings.AWX_AUTO_DEPROVISION_INSTANCES:
# detect any instances with the same IP address.
Expand All @@ -127,11 +130,21 @@ def register(self, uuid=None, hostname=None, ip_address=None, node_type='hybrid'
other_inst.save(update_fields=['ip_address'])
logger.warning("IP address {0} conflict detected, ip address unset for host {1}.".format(ip_address, other_hostname))

# Return existing instance that matches hostname
# get the instance based on the hostname
instance = self.filter(hostname=hostname)

# Check or update the existing uuid
if uuid != None and uuid != self.UUID_DEFAULT:
if self.filter(uuid=uuid).exists():
instance = self.filter(uuid=uuid)

# Return existing instance
if instance.exists():
instance = instance.get()
update_fields = []
if instance.hostname != hostname:
instance.hostname = hostname
update_fields.append('hostname')
if instance.ip_address != ip_address:
instance.ip_address = ip_address
update_fields.append('ip_address')
Expand Down

0 comments on commit 7691b2b

Please sign in to comment.