mirror of
https://github.com/ansible/awx.git
synced 2026-05-11 11:27:36 -02:30
Update node and link registration to put them in the right state
'Installed' for the nodes, 'Established' for the links.
This commit is contained in:
@@ -27,7 +27,9 @@ class Command(BaseCommand):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def handle(self, **options):
|
def handle(self, **options):
|
||||||
|
# provides a mapping of hostname to Instance objects
|
||||||
nodes = Instance.objects.in_bulk(field_name='hostname')
|
nodes = Instance.objects.in_bulk(field_name='hostname')
|
||||||
|
|
||||||
if options['source'] not in nodes:
|
if options['source'] not in nodes:
|
||||||
raise CommandError(f"Host {options['source']} is not a registered instance.")
|
raise CommandError(f"Host {options['source']} is not a registered instance.")
|
||||||
if not (options['peers'] or options['disconnect'] or options['exact'] is not None):
|
if not (options['peers'] or options['disconnect'] or options['exact'] is not None):
|
||||||
@@ -57,7 +59,9 @@ class Command(BaseCommand):
|
|||||||
|
|
||||||
results = 0
|
results = 0
|
||||||
for target in options['peers']:
|
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:
|
if created:
|
||||||
results += 1
|
results += 1
|
||||||
|
|
||||||
@@ -80,7 +84,9 @@ class Command(BaseCommand):
|
|||||||
links = set(InstanceLink.objects.filter(source=nodes[options['source']]).values_list('target__hostname', flat=True))
|
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()
|
removals, _ = InstanceLink.objects.filter(source=nodes[options['source']], target__hostname__in=links - peers).delete()
|
||||||
for target in peers - links:
|
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:
|
if created:
|
||||||
additions += 1
|
additions += 1
|
||||||
|
|
||||||
|
|||||||
@@ -129,10 +129,13 @@ class InstanceManager(models.Manager):
|
|||||||
# if instance was not retrieved by uuid and hostname was, use the hostname
|
# if instance was not retrieved by uuid and hostname was, use the hostname
|
||||||
instance = self.filter(hostname=hostname)
|
instance = self.filter(hostname=hostname)
|
||||||
|
|
||||||
|
from awx.main.models import Instance
|
||||||
|
|
||||||
# Return existing instance
|
# Return existing instance
|
||||||
if instance.exists():
|
if instance.exists():
|
||||||
instance = instance.first() # in the unusual occasion that there is more than one, only get one
|
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 was retrieved by uuid and hostname has changed, update hostname
|
||||||
if instance.hostname != 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))
|
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 any other fields are to be updated
|
||||||
if instance.ip_address != ip_address:
|
if instance.ip_address != ip_address:
|
||||||
instance.ip_address = ip_address
|
instance.ip_address = ip_address
|
||||||
|
update_fields.append('ip_address')
|
||||||
if instance.node_type != node_type:
|
if instance.node_type != node_type:
|
||||||
instance.node_type = node_type
|
instance.node_type = node_type
|
||||||
update_fields.append('node_type')
|
update_fields.append('node_type')
|
||||||
@@ -151,12 +155,12 @@ class InstanceManager(models.Manager):
|
|||||||
return (False, instance)
|
return (False, instance)
|
||||||
|
|
||||||
# Create new instance, and fill in default values
|
# 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:
|
if defaults is not None:
|
||||||
create_defaults.update(defaults)
|
create_defaults.update(defaults)
|
||||||
uuid_option = {}
|
uuid_option = {}
|
||||||
if uuid is not None:
|
if uuid is not None:
|
||||||
uuid_option = dict(uuid=uuid)
|
uuid_option = {'uuid': uuid}
|
||||||
if node_type == 'execution' and 'version' not in create_defaults:
|
if node_type == 'execution' and 'version' not in create_defaults:
|
||||||
create_defaults['version'] = RECEPTOR_PENDING
|
create_defaults['version'] = RECEPTOR_PENDING
|
||||||
instance = self.create(hostname=hostname, ip_address=ip_address, node_type=node_type, **create_defaults, **uuid_option)
|
instance = self.create(hostname=hostname, ip_address=ip_address, node_type=node_type, **create_defaults, **uuid_option)
|
||||||
|
|||||||
Reference in New Issue
Block a user