mirror of
https://github.com/ansible/awx.git
synced 2026-01-17 12:41:19 -03:30
Ability to remove instances.
This commit is contained in:
parent
1ffca45c4a
commit
d93870d828
54
awx/main/management/commands/remove_instance.py
Normal file
54
awx/main/management/commands/remove_instance.py
Normal file
@ -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)
|
||||
Loading…
x
Reference in New Issue
Block a user