Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / management / commands / backend-add.py @ 225cea18

History | View | Annotate | Download (5.7 kB)

1
# Copyright 2011-2012 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 optparse import make_option
32
from django.core.management.base import BaseCommand, CommandError
33

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

    
44

    
45
class Command(BaseCommand):
46
    can_import_settings = True
47

    
48
    help = 'Create a new backend.'
49
    output_transaction = True  # The management command runs inside
50
                               # an SQL transaction
51
    option_list = BaseCommand.option_list + (
52
        make_option('--clustername', dest='clustername'),
53
        make_option('--port', dest='port', default=5080),
54
        make_option('--user', dest='username'),
55
        make_option('--pass', dest='password'),
56
        make_option(
57
            '--no-check', action='store_false',
58
            dest='check', default=True,
59
            help="Do not perform credentials check and resources update"),
60
        make_option(
61
            '--no-init', action='store_false',
62
            dest='init', default=True,
63
            help="Do not perform initialization of the Backend Model")
64
    )
65

    
66
    @transaction.commit_on_success
67
    def handle(self, **options):
68
        clustername = options['clustername']
69
        port = options['port']
70
        username = options['username']
71
        password = options['password']
72

    
73
        if not (clustername and username and password):
74
            raise CommandError("Clustername, user and pass must be supplied")
75

    
76
        # Ensure correctness of credentials
77
        if options['check']:
78
            check_backend_credentials(clustername, port, username, password)
79

    
80
        # Create the new backend in database
81
        try:
82
            backend = Backend.objects.create(clustername=clustername,
83
                                             port=port,
84
                                             username=username,
85
                                             password=password,
86
                                             drained=True)
87
        except IntegrityError as e:
88
            raise CommandError("Cannot create backend: %s\n" % e)
89

    
90
        self.stdout.write('\nSuccessfully created backend with id %d\n' %
91
                          backend.id)
92

    
93
        if not options['check']:
94
            return
95

    
96
        self.stdout.write('\rRetrieving backend resources:\n')
97
        resources = get_physical_resources(backend)
98
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
99

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

    
103
        update_resources(backend, resources)
104

    
105
        if not options['init']:
106
            return
107

    
108
        networks = Network.objects.filter(deleted=False, public=False)
109
        if not networks:
110
            return
111

    
112
        self.stdout.write('\nCreating the follow networks:\n')
113
        headers = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public')
114
        table = []
115

    
116
        for net in networks:
117
            table.append((net.backend_id, str(net.subnet), str(net.gateway),
118
                         str(net.mac_prefix), str(net.public)))
119
        pprint_table(self.stdout, table, headers)
120

    
121
        for net in networks:
122
            net.create_backend_network(backend)
123
            result = create_network_synced(net, backend)
124
            if result[0] != "success":
125
                self.stdout.write('\nError Creating Network %s: %s\n' %
126
                                  (net.backend_id, result[1]))
127
            else:
128
                self.stdout.write('Successfully created Network: %s\n' %
129
                                  net.backend_id)
130
            result = connect_network_synced(network=net, backend=backend)
131
            if result[0] != "success":
132
                self.stdout.write('\nError Connecting Network %s: %s\n' %
133
                                  (net.backend_id, result[1]))
134
            else:
135
                self.stdout.write('Successfully connected Network: %s\n' %
136
                                  net.backend_id)