Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / backend-remove.py @ a6e6fe48

History | View | Annotate | Download (3.6 kB)

1
# Copyright 2011-2014 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or without
4
# modification, are permitted provided that the following conditions
5
# are met:
6
#
7
#   1. Redistributions of source code must retain the above copyright
8
#      notice, this list of conditions and the following disclaimer.
9
#
10
#  2. Redistributions in binary form must reproduce the above copyright
11
#     notice, this list of conditions and the following disclaimer in the
12
#     documentation and/or other materials provided with the distribution.
13
#
14
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
# SUCH DAMAGE.
25
#
26
# The views and conclusions contained in the software and documentation are
27
# those of the authors and should not be interpreted as representing official
28
# policies, either expressed or implied, of GRNET S.A.
29
#
30

    
31
from django.core.management.base import BaseCommand, CommandError
32
from synnefo.management import common
33
from synnefo.logic import backend as backend_mod
34
from synnefo.db.models import Backend
35
from django.db import transaction, models
36

    
37

    
38
HELP_MSG = """\
39
Remove a backend from the Database. Backend should be set to drained before
40
trying to remove it, in order to avoid the allocation of a new instances in
41
this Backend.  Removal of a backend will fail if the backend hosts any
42
non-deleted instances."""
43

    
44

    
45
class Command(BaseCommand):
46
    help = HELP_MSG
47

    
48
    def handle(self, *args, **options):
49
        write = self.stdout.write
50
        if len(args) < 1:
51
            raise CommandError("Please provide a backend ID")
52

    
53
        backend = common.get_resource("backend", args[0], for_update=True)
54

    
55
        write("Trying to remove backend: %s\n" % backend.clustername)
56

    
57
        if backend.virtual_machines.filter(deleted=False).exists():
58
            raise CommandError('Backend hosts non-deleted vms. Cannot delete')
59

    
60
        # Get networks before deleting backend, because after deleting the
61
        # backend, all BackendNetwork objects are deleted!
62
        networks = [bn.network for bn in backend.networks.all()]
63

    
64
        try:
65
            delete_backend(backend)
66
        except models.ProtectedError as e:
67
            msg = ("Cannot delete backend because it contains"
68
                   "non-deleted VMs:\n%s" % e)
69
            raise CommandError(msg)
70

    
71
        write('Successfully removed backend from DB.\n')
72

    
73
        if networks:
74
            write("Clearing networks from %s..\n" % backend.clustername)
75
            for network in networks:
76
                backend_mod.delete_network(network=network, backend=backend)
77
            write("Successfully issued jobs to remove all networks.\n")
78

    
79

    
80
@transaction.commit_on_success
81
def delete_backend(backend):
82
    # Get X-Lock
83
    backend = Backend.objects.select_for_update().get(id=backend.id)
84
    # Clear 'backend' field of 'deleted' VirtualMachines
85
    backend.virtual_machines.filter(deleted=True).update(backend=None)
86
    # Delete all BackendNetwork objects of this backend
87
    backend.networks.all().delete()
88
    backend.delete()