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

from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
36
from synnefo.db.models import Backend
37
from snf_django.management.utils import parse_bool
38
from synnefo.management import common
39

40 41
HYPERVISORS = [h[0] for h in Backend.HYPERVISORS]

42 43

class Command(BaseCommand):
44
    output_transaction = True
45 46 47 48 49
    args = "<backend ID>"
    help = "Modify a backend"

    option_list = BaseCommand.option_list + (
        make_option('--clustername',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
50 51
                    dest='clustername',
                    help="Set backend's clustername"),
52
        make_option('--port',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
53 54
                    dest='port',
                    help="Set backend's port"),
55
        make_option('--username',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
56 57
                    dest='username',
                    help="Set backend'username"),
58
        make_option('--password',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
59 60
                    dest='password',
                    help="Set backend's password"),
61
        make_option('--drained',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
62 63 64 65 66
                    dest='drained',
                    choices=["True", "False"],
                    metavar="True|False",
                    help="Set the backend as drained to exclude from"
                         " allocation operations"),
67 68 69 70 71 72
        make_option('--hypervisor',
                    dest='hypervisor',
                    default=None,
                    choices=HYPERVISORS,
                    metavar="|".join(HYPERVISORS),
                    help="The hypervisor that the Ganeti backend uses"),
73
        make_option('--offline',
Christos Stavrakakis's avatar
Christos Stavrakakis committed
74 75 76 77 78 79
                    dest='offline',
                    choices=["True", "False"],
                    metavar="True|False",
                    help="Set the backend as offline to not communicate in"
                         " order to avoid delays"),
    )
80 81 82 83 84

    def handle(self, *args, **options):
        if len(args) != 1:
            raise CommandError("Please provide a backend ID")

85
        backend = common.get_resource("backend", args[0], for_update=True)
86 87

        # Ensure fields correspondence with options and Backend model
88 89
        credentials_changed = False
        fields = ('clustername', 'port', 'username', 'password')
90 91 92 93
        for field in fields:
            value = options.get(field)
            if value is not None:
                backend.__setattr__(field, value)
94
                credentials_changed = True
95

96
        if credentials_changed:
97
                # check credentials, if any of them changed!
98 99 100 101
                common.check_backend_credentials(backend.clustername,
                                                 backend.port,
                                                 backend.username,
                                                 backend.password)
102
        if options['drained']:
103
            backend.drained = parse_bool(options['drained'], strict=True)
104
        if options['offline']:
105
            backend.offline = parse_bool(options['offline'], strict=True)
106 107 108
        hypervisor = options["hypervisor"]
        if hypervisor:
            backend.hypervisor = hypervisor
109

110
        backend.save()