mirror of
https://github.com/ansible/awx.git
synced 2026-01-10 15:32:07 -03:30
Add management command to remove address
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 <fosterbseth@gmail.com>
This commit is contained in:
parent
9c06370e33
commit
4c5ac1d3da
@ -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)")
|
||||
|
||||
26
awx/main/management/commands/remove_receptor_address.py
Normal file
26
awx/main/management/commands/remove_receptor_address.py
Normal file
@ -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")
|
||||
Loading…
x
Reference in New Issue
Block a user