Skip to content

Commit

Permalink
Add management command to remove address
Browse files Browse the repository at this point in the history
Adds remove_receptor_address to delete a
receptor address from the database

Also, enforce that only 1 canonical address
can be added to an instance via
the add_receptor_address command.

Signed-off-by: Seth Foster <[email protected]>
  • Loading branch information
fosterseth committed Feb 2, 2024
1 parent 9c06370 commit 4c5ac1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
20 changes: 11 additions & 9 deletions awx/main/management/commands/add_receptor_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -40,12 +43,11 @@ def add_arguments(self, parser):
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)")
26 changes: 26 additions & 0 deletions awx/main/management/commands/remove_receptor_address.py
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 4c5ac1d

Please sign in to comment.