backend-add.py 4.38 KB
Newer Older
1
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
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# 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 THE REGENTS AND CONTRIBUTORS ``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 THE REGENTS 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

34
from synnefo.db.models import Backend, Network
35
from django.db.utils import IntegrityError
36
37
from synnefo.logic.backend import (get_physical_resources,
                                   update_resources,
38
39
                                   create_network_synced,
                                   connect_network_synced)
40
from synnefo.management.common import check_backend_credentials
41
from synnefo.webproject.management.utils import pprint_table
42
43


44
45
46
HYPERVISORS = [h[0] for h in Backend.HYPERVISORS]


47
48
49
50
51
52
53
class Command(BaseCommand):
    can_import_settings = True

    help = 'Create a new backend.'
    option_list = BaseCommand.option_list + (
        make_option('--clustername', dest='clustername'),
        make_option('--port', dest='port', default=5080),
54
55
        make_option('--user', dest='username'),
        make_option('--pass', dest='password'),
Christos Stavrakakis's avatar
Christos Stavrakakis committed
56
57
        make_option(
            '--no-check', action='store_false',
58
59
            dest='check', default=True,
            help="Do not perform credentials check and resources update"),
60
61
62
63
64
65
       make_option('--hypervisor',
            dest='hypervisor',
            default=None,
            choices=HYPERVISORS,
            metavar="|".join(HYPERVISORS),
            help="The hypervisor that the Ganeti backend uses"),
Christos Stavrakakis's avatar
Christos Stavrakakis committed
66
    )
67

68
69
70
71
    def handle(self, *args, **options):
        if len(args) > 0:
            raise CommandError("Command takes no arguments")

72
        clustername = options['clustername']
73
        port = options['port']
Christos Stavrakakis's avatar
Christos Stavrakakis committed
74
75
        username = options['username']
        password = options['password']
76
        hypervisor = options["hypervisor"]
77
78

        if not (clustername and username and password):
79
            raise CommandError("Clustername, user and pass must be supplied")
80
81
82

        # Ensure correctness of credentials
        if options['check']:
83
            check_backend_credentials(clustername, port, username, password)
84

85
86
87
88
89
90
91
92
        kw = {"clustername": clustername,
              "port": port,
              "username": username,
              "password": password,
              "drained": True}

        if hypervisor:
            kw["hypervisor"] = hypervisor
93
94
        # Create the new backend in database
        try:
95
            backend = Backend.objects.create(**kw)
96
        except IntegrityError as e:
97
            raise CommandError("Cannot create backend: %s\n" % e)
98

99
100
101
102
103
104
105
106
107
        self.stdout.write('\nSuccessfully created backend with id %d\n' %
                          backend.id)

        if not options['check']:
            return

        self.stdout.write('\rRetrieving backend resources:\n')
        resources = get_physical_resources(backend)
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
108
109
110
111

        table = [[str(resources[x]) for x in attr]]
        pprint_table(self.stdout, table, attr)

112
        update_resources(backend, resources)