From d93870d828d4d27309fecd5bb906fc1e81ac220d Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Fri, 5 Dec 2014 14:19:51 -0600 Subject: [PATCH] Ability to remove instances. --- .../management/commands/remove_instance.py | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 awx/main/management/commands/remove_instance.py diff --git a/awx/main/management/commands/remove_instance.py b/awx/main/management/commands/remove_instance.py new file mode 100644 index 0000000000..56c817e760 --- /dev/null +++ b/awx/main/management/commands/remove_instance.py @@ -0,0 +1,54 @@ +# Copyright (c) 2014 Ansible, Inc. +# All Rights Reserved + +from optparse import make_option + +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError + +from awx.main.models import Instance + + +class Command(BaseCommand): + """Remove an existing instance from the HA instance table. + + This command is idempotent. It will remove the machine if it is + present and do nothing if the machine is absent. + + This command will cowardly refuse to remove the primary machine. + """ + option_list = BaseCommand.option_list + ( + make_option('--hostname', dest='hostname', default=''), + make_option('--uuid', dest='uuid', default=''), + ) + + def handle(self, **options): + # Remove any empty options from the options dictionary. + fields = {} + for field in ('uuid', 'hostname'): + if options[field]: + fields[field] = options[field] + + # At least one of hostname or uuid must be set. + if not fields: + raise CommandError('You must provide either --uuid or --hostname.') + + # Is there an existing record for this machine? + # If so, retrieve that record and look for issues. + try: + # Get the instance. + instance = Instance.objects.get(**fields) + + # Sanity check: Do not remove the primary instance. + if instance.primary: + raise CommandError('I cowardly refuse to remove the primary ' + 'instance.') + + # Remove the instance. + instance.delete() + dirty = True + except Instance.DoesNotExist: + dirty = False + + # Done! + print('Instance removed (changed: %r).' % dirty)