diff --git a/awx/main/management/commands/add_receptor_address.py b/awx/main/management/commands/add_receptor_address.py index ef37e5e89a..7ac7ac8be7 100644 --- a/awx/main/management/commands/add_receptor_address.py +++ b/awx/main/management/commands/add_receptor_address.py @@ -10,28 +10,31 @@ def add_address(**kwargs): try: instance = Instance.objects.get(hostname=kwargs.pop('instance')) kwargs['instance'] = instance + + if kwargs.get('canonical') and instance.receptor_addresses.filter(canonical=True).exclude(address=kwargs['address']).exists(): + print(f"Instance {instance.hostname} already has a canonical address, skipping") + return False # if ReceptorAddress already exists with address, just update # otherwise, create new ReceptorAddress addr, _ = ReceptorAddress.objects.update_or_create(address=kwargs.pop('address'), defaults=kwargs) print(f"Successfully added receptor address {addr.get_full_address()}") - changed = True + return True except Exception as e: - changed = False print(f"Error adding receptor address: {e}") - return changed + return False class Command(BaseCommand): """ - Internal tower command. + Internal controller command. Register receptor address to an already-registered instance. """ help = "Add receptor address to an instance." def add_arguments(self, parser): - parser.add_argument('--instance', dest='instance', type=str, help="Instance hostname this address is added to") - parser.add_argument('--address', dest='address', type=str, help="Receptor address") + parser.add_argument('--instance', dest='instance', required=True, type=str, help="Instance hostname this address is added to") + parser.add_argument('--address', dest='address', required=True, type=str, help="Receptor address") parser.add_argument('--port', dest='port', type=int, help="Receptor listener port") parser.add_argument('--websocket_path', dest='websocket_path', type=str, default="", help="Path for websockets") parser.add_argument('--is_internal', action='store_true', help="If true, address only resolvable within the Kubernetes cluster") @@ -40,12 +43,11 @@ class Command(BaseCommand): parser.add_argument('--peers_from_control_nodes', action='store_true', help="If true, control nodes will peer to this address") def handle(self, **options): - self.changed = False address_options = { k: options[k] for k in ('instance', 'address', 'port', 'websocket_path', 'is_internal', 'protocol', 'peers_from_control_nodes', 'canonical') if options[k] } - self.changed = add_address(**address_options) - if self.changed: + changed = add_address(**address_options) + if changed: print("(changed: True)") diff --git a/awx/main/management/commands/remove_receptor_address.py b/awx/main/management/commands/remove_receptor_address.py new file mode 100644 index 0000000000..de7426a53f --- /dev/null +++ b/awx/main/management/commands/remove_receptor_address.py @@ -0,0 +1,26 @@ +# Copyright (c) 2015 Ansible, Inc. +# All Rights Reserved + +from django.core.management.base import BaseCommand + +from awx.main.models import ReceptorAddress + + +class Command(BaseCommand): + """ + Internal controller command. + Delete a receptor address. + """ + + help = "Add receptor address to an instance." + + def add_arguments(self, parser): + parser.add_argument('--address', dest='address', type=str, help="Receptor address to remove") + + def handle(self, **options): + deleted = ReceptorAddress.objects.filter(address=options['address']).delete() + if deleted[0]: + print(f"Successfully removed {options['address']}") + print("(changed: True)") + else: + print(f"Did not remove {options['address']}, not found")