From 6c56f2b35b3573d3a5355b8a9877cdc6ea377871 Mon Sep 17 00:00:00 2001 From: Alan Rominger Date: Wed, 30 Mar 2022 13:35:42 -0400 Subject: [PATCH] Delete dead code from get_or_register, move, and test --- .../management/commands/provision_instance.py | 18 ++++++++- awx/main/managers.py | 18 --------- .../commands/test_provision_instance.py | 40 +++++++++++++++++++ 3 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 awx/main/tests/functional/commands/test_provision_instance.py diff --git a/awx/main/management/commands/provision_instance.py b/awx/main/management/commands/provision_instance.py index aeae496306..75c297e8f0 100644 --- a/awx/main/management/commands/provision_instance.py +++ b/awx/main/management/commands/provision_instance.py @@ -1,6 +1,8 @@ # Copyright (c) 2015 Ansible, Inc. # All Rights Reserved +import os + from django.core.management.base import BaseCommand, CommandError from django.db import transaction from django.conf import settings @@ -14,7 +16,12 @@ class Command(BaseCommand): Register this instance with the database for HA tracking. """ - help = "Add instance to the database. When no options are provided, the hostname of the current system will be used. Override with `--hostname`." + help = ( + "Add instance to the database. " + "When no options are provided, values from Django settings will be used to register the current system, " + "as well as the default queues if needed (only used or enabled for Kubernetes installs). " + "Override with `--hostname`." + ) def add_arguments(self, parser): parser.add_argument('--hostname', dest='hostname', type=str, help="Hostname used during provisioning") @@ -25,7 +32,14 @@ class Command(BaseCommand): if not hostname: if not settings.AWX_AUTO_DEPROVISION_INSTANCES: raise CommandError('Registering with values from settings only intended for use in K8s installs') - (changed, instance) = Instance.objects.get_or_register() + + from awx.main.management.commands.register_queue import RegisterQueue + + (changed, instance) = Instance.objects.register(ip_address=os.environ.get('MY_POD_IP'), node_type='control', uuid=settings.SYSTEM_UUID) + RegisterQueue(settings.DEFAULT_CONTROL_PLANE_QUEUE_NAME, 100, 0, [], is_container_group=False).register() + RegisterQueue( + settings.DEFAULT_EXECUTION_QUEUE_NAME, 100, 0, [], is_container_group=True, pod_spec_override=settings.DEFAULT_EXECUTION_QUEUE_POD_SPEC_OVERRIDE + ).register() else: (changed, instance) = Instance.objects.register(hostname=hostname, node_type=node_type, uuid=uuid) if changed: diff --git a/awx/main/managers.py b/awx/main/managers.py index 4702ad6a9e..b87d80954a 100644 --- a/awx/main/managers.py +++ b/awx/main/managers.py @@ -2,7 +2,6 @@ # All Rights Reserved. import logging -import os from django.db import models from django.conf import settings from django.db.models.functions import Lower @@ -164,23 +163,6 @@ class InstanceManager(models.Manager): instance = self.create(hostname=hostname, ip_address=ip_address, node_type=node_type, **create_defaults, **uuid_option) return (True, instance) - def get_or_register(self): - if settings.AWX_AUTO_DEPROVISION_INSTANCES: - from awx.main.management.commands.register_queue import RegisterQueue - - pod_ip = os.environ.get('MY_POD_IP') - if settings.IS_K8S: - registered = self.register(ip_address=pod_ip, node_type='control', uuid=settings.SYSTEM_UUID) - else: - registered = self.register(ip_address=pod_ip, uuid=settings.SYSTEM_UUID) - RegisterQueue(settings.DEFAULT_CONTROL_PLANE_QUEUE_NAME, 100, 0, [], is_container_group=False).register() - RegisterQueue( - settings.DEFAULT_EXECUTION_QUEUE_NAME, 100, 0, [], is_container_group=True, pod_spec_override=settings.DEFAULT_EXECUTION_QUEUE_POD_SPEC_OVERRIDE - ).register() - return registered - else: - return (False, self.me()) - class InstanceGroupManager(models.Manager): """A custom manager class for the Instance model. diff --git a/awx/main/tests/functional/commands/test_provision_instance.py b/awx/main/tests/functional/commands/test_provision_instance.py new file mode 100644 index 0000000000..bb02318636 --- /dev/null +++ b/awx/main/tests/functional/commands/test_provision_instance.py @@ -0,0 +1,40 @@ +import pytest + +from awx.main.management.commands.provision_instance import Command +from awx.main.models.ha import InstanceGroup, Instance +from awx.main.tasks.system import apply_cluster_membership_policies + +from django.test.utils import override_settings + + +@pytest.mark.django_db +def test_traditional_registration(): + assert not Instance.objects.exists() + assert not InstanceGroup.objects.exists() + + Command().handle(hostname='bar_node', node_type='execution', uuid='4321') + + inst = Instance.objects.first() + assert inst.hostname == 'bar_node' + assert inst.node_type == 'execution' + assert inst.uuid == '4321' + + assert not InstanceGroup.objects.exists() + + +@pytest.mark.django_db +def test_register_self_openshift(): + assert not Instance.objects.exists() + assert not InstanceGroup.objects.exists() + + with override_settings(AWX_AUTO_DEPROVISION_INSTANCES=True, CLUSTER_HOST_ID='foo_node', SYSTEM_UUID='12345'): + Command().handle() + inst = Instance.objects.first() + assert inst.hostname == 'foo_node' + assert inst.uuid == '12345' + assert inst.node_type == 'control' + + apply_cluster_membership_policies() # populate instance list using policy rules + + assert list(InstanceGroup.objects.get(name='default').instances.all()) == [] # container group + assert list(InstanceGroup.objects.get(name='controlplane').instances.all()) == [inst]