mirror of
https://github.com/ansible/awx.git
synced 2026-05-15 13:27:40 -02:30
Add a new register_peers management command
and alter provision_instance to accept hop nodes.
This commit is contained in:
@@ -13,19 +13,19 @@ class Command(BaseCommand):
|
|||||||
Register this instance with the database for HA tracking.
|
Register this instance with the database for HA tracking.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
help = 'Add instance to the database. ' 'Specify `--hostname` to use this command.'
|
help = "Add instance to the database. Specify `--hostname` to use this command."
|
||||||
|
|
||||||
def add_arguments(self, parser):
|
def add_arguments(self, parser):
|
||||||
parser.add_argument('--hostname', dest='hostname', type=str, help='Hostname used during provisioning')
|
parser.add_argument('--hostname', dest='hostname', type=str, help="Hostname used during provisioning")
|
||||||
parser.add_argument('--node_type', type=str, default="hybrid", choices=["control", "execution", "hybrid"], help='Instance Node type')
|
parser.add_argument('--node_type', type=str, default='hybrid', choices=['control', 'execution', 'hop', 'hybrid'], help="Instance Node type")
|
||||||
parser.add_argument('--uuid', type=str, help='Instance UUID')
|
parser.add_argument('--uuid', type=str, help="Instance UUID")
|
||||||
|
|
||||||
def _register_hostname(self, hostname, node_type, uuid):
|
def _register_hostname(self, hostname, node_type, uuid):
|
||||||
if not hostname:
|
if not hostname:
|
||||||
return
|
return
|
||||||
(changed, instance) = Instance.objects.register(hostname=hostname, node_type=node_type, uuid=uuid)
|
(changed, instance) = Instance.objects.register(hostname=hostname, node_type=node_type, uuid=uuid)
|
||||||
if changed:
|
if changed:
|
||||||
print('Successfully registered instance {}'.format(hostname))
|
print("Successfully registered instance {}".format(hostname))
|
||||||
else:
|
else:
|
||||||
print("Instance already registered {}".format(instance.hostname))
|
print("Instance already registered {}".format(instance.hostname))
|
||||||
self.changed = changed
|
self.changed = changed
|
||||||
@@ -37,4 +37,4 @@ class Command(BaseCommand):
|
|||||||
self.changed = False
|
self.changed = False
|
||||||
self._register_hostname(options.get('hostname'), options.get('node_type'), options.get('uuid'))
|
self._register_hostname(options.get('hostname'), options.get('node_type'), options.get('uuid'))
|
||||||
if self.changed:
|
if self.changed:
|
||||||
print('(changed: True)')
|
print("(changed: True)")
|
||||||
|
|||||||
30
awx/main/management/commands/register_peers.py
Normal file
30
awx/main/management/commands/register_peers.py
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
from django.core.management.base import BaseCommand, CommandError
|
||||||
|
|
||||||
|
from awx.main.models import Instance, InstanceLink
|
||||||
|
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""
|
||||||
|
Internal tower command.
|
||||||
|
Register the peers of a receptor node.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def add_arguments(self, parser):
|
||||||
|
parser.add_argument('source', type=str, help="")
|
||||||
|
parser.add_argument('--peers', type=str, nargs='+', required=True, help="")
|
||||||
|
|
||||||
|
def handle(self, **options):
|
||||||
|
nodes = Instance.objects.in_bulk(field_name='hostname')
|
||||||
|
if options['source'] not in nodes:
|
||||||
|
raise CommandError()
|
||||||
|
missing_peers = set(options['peers']) - set(nodes)
|
||||||
|
if missing_peers:
|
||||||
|
raise CommandError()
|
||||||
|
|
||||||
|
results = 0
|
||||||
|
for target in options['peers']:
|
||||||
|
instance, created = InstanceLink.objects.get_or_create(source=nodes[options['source']], target=nodes[target])
|
||||||
|
if created:
|
||||||
|
results += 1
|
||||||
|
|
||||||
|
print(f"{results} new peer links added to the database.")
|
||||||
Reference in New Issue
Block a user