Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.6 kB)

1 a6e6fe48 Christos Stavrakakis
# Copyright 2011-2014 GRNET S.A. All rights reserved.
2 3f52dd82 Christos Stavrakakis
#
3 3f52dd82 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 3f52dd82 Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 3f52dd82 Christos Stavrakakis
# are met:
6 3f52dd82 Christos Stavrakakis
#
7 3f52dd82 Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 3f52dd82 Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 3f52dd82 Christos Stavrakakis
#
10 3f52dd82 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 3f52dd82 Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 3f52dd82 Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 3f52dd82 Christos Stavrakakis
#
14 3f52dd82 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 3f52dd82 Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 3f52dd82 Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 3f52dd82 Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 3f52dd82 Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 3f52dd82 Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 3f52dd82 Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 3f52dd82 Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 3f52dd82 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 3f52dd82 Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 3f52dd82 Christos Stavrakakis
# SUCH DAMAGE.
25 3f52dd82 Christos Stavrakakis
#
26 3f52dd82 Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 3f52dd82 Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 3f52dd82 Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 3f52dd82 Christos Stavrakakis
#
30 3f52dd82 Christos Stavrakakis
31 3f52dd82 Christos Stavrakakis
from django.core.management.base import BaseCommand, CommandError
32 a6e6fe48 Christos Stavrakakis
from synnefo.management import common
33 26515bc1 Christos Stavrakakis
from synnefo.logic import backend as backend_mod
34 26515bc1 Christos Stavrakakis
from synnefo.db.models import Backend
35 26515bc1 Christos Stavrakakis
from django.db import transaction, models
36 3f52dd82 Christos Stavrakakis
37 3f52dd82 Christos Stavrakakis
38 26515bc1 Christos Stavrakakis
HELP_MSG = """\
39 26515bc1 Christos Stavrakakis
Remove a backend from the Database. Backend should be set to drained before
40 26515bc1 Christos Stavrakakis
trying to remove it, in order to avoid the allocation of a new instances in
41 26515bc1 Christos Stavrakakis
this Backend.  Removal of a backend will fail if the backend hosts any
42 26515bc1 Christos Stavrakakis
non-deleted instances."""
43 3f52dd82 Christos Stavrakakis
44 3f52dd82 Christos Stavrakakis
45 26515bc1 Christos Stavrakakis
class Command(BaseCommand):
46 26515bc1 Christos Stavrakakis
    help = HELP_MSG
47 3f52dd82 Christos Stavrakakis
48 3f52dd82 Christos Stavrakakis
    def handle(self, *args, **options):
49 cc92b70f Christos Stavrakakis
        write = self.stdout.write
50 3f52dd82 Christos Stavrakakis
        if len(args) < 1:
51 3f52dd82 Christos Stavrakakis
            raise CommandError("Please provide a backend ID")
52 3f52dd82 Christos Stavrakakis
53 a6e6fe48 Christos Stavrakakis
        backend = common.get_resource("backend", args[0], for_update=True)
54 3f52dd82 Christos Stavrakakis
55 26515bc1 Christos Stavrakakis
        write("Trying to remove backend: %s\n" % backend.clustername)
56 3f52dd82 Christos Stavrakakis
57 26515bc1 Christos Stavrakakis
        if backend.virtual_machines.filter(deleted=False).exists():
58 8d5795b4 Christos Stavrakakis
            raise CommandError('Backend hosts non-deleted vms. Cannot delete')
59 3f52dd82 Christos Stavrakakis
60 26515bc1 Christos Stavrakakis
        # Get networks before deleting backend, because after deleting the
61 26515bc1 Christos Stavrakakis
        # backend, all BackendNetwork objects are deleted!
62 26515bc1 Christos Stavrakakis
        networks = [bn.network for bn in backend.networks.all()]
63 3f52dd82 Christos Stavrakakis
64 26515bc1 Christos Stavrakakis
        try:
65 26515bc1 Christos Stavrakakis
            delete_backend(backend)
66 26515bc1 Christos Stavrakakis
        except models.ProtectedError as e:
67 8d5795b4 Christos Stavrakakis
            msg = ("Cannot delete backend because it contains"
68 26515bc1 Christos Stavrakakis
                   "non-deleted VMs:\n%s" % e)
69 26515bc1 Christos Stavrakakis
            raise CommandError(msg)
70 3f52dd82 Christos Stavrakakis
71 26515bc1 Christos Stavrakakis
        write('Successfully removed backend from DB.\n')
72 3f52dd82 Christos Stavrakakis
73 3f52dd82 Christos Stavrakakis
        if networks:
74 26515bc1 Christos Stavrakakis
            write("Clearing networks from %s..\n" % backend.clustername)
75 26515bc1 Christos Stavrakakis
            for network in networks:
76 26515bc1 Christos Stavrakakis
                backend_mod.delete_network(network=network, backend=backend)
77 26515bc1 Christos Stavrakakis
            write("Successfully issued jobs to remove all networks.\n")
78 26515bc1 Christos Stavrakakis
79 26515bc1 Christos Stavrakakis
80 26515bc1 Christos Stavrakakis
@transaction.commit_on_success
81 26515bc1 Christos Stavrakakis
def delete_backend(backend):
82 26515bc1 Christos Stavrakakis
    # Get X-Lock
83 26515bc1 Christos Stavrakakis
    backend = Backend.objects.select_for_update().get(id=backend.id)
84 26515bc1 Christos Stavrakakis
    # Clear 'backend' field of 'deleted' VirtualMachines
85 26515bc1 Christos Stavrakakis
    backend.virtual_machines.filter(deleted=True).update(backend=None)
86 26515bc1 Christos Stavrakakis
    # Delete all BackendNetwork objects of this backend
87 26515bc1 Christos Stavrakakis
    backend.networks.all().delete()
88 26515bc1 Christos Stavrakakis
    backend.delete()