Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.7 kB)

1 1a894bfe Christos Stavrakakis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 1a894bfe Christos Stavrakakis
#
3 1a894bfe Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
4 1a894bfe Christos Stavrakakis
# modification, are permitted provided that the following conditions
5 1a894bfe Christos Stavrakakis
# are met:
6 1a894bfe Christos Stavrakakis
#
7 1a894bfe Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
8 1a894bfe Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
9 1a894bfe Christos Stavrakakis
#
10 1a894bfe Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
11 1a894bfe Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
12 1a894bfe Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
13 1a894bfe Christos Stavrakakis
#
14 1a894bfe Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 1a894bfe Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 1a894bfe Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 1a894bfe Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 1a894bfe Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 1a894bfe Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 1a894bfe Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 1a894bfe Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 1a894bfe Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 1a894bfe Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 1a894bfe Christos Stavrakakis
# SUCH DAMAGE.
25 1a894bfe Christos Stavrakakis
#
26 1a894bfe Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
27 1a894bfe Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
28 1a894bfe Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
29 1a894bfe Christos Stavrakakis
#
30 1a894bfe Christos Stavrakakis
31 1a894bfe Christos Stavrakakis
from optparse import make_option
32 1a894bfe Christos Stavrakakis
from django.core.management.base import BaseCommand, CommandError
33 1a894bfe Christos Stavrakakis
34 88b8f176 Christos Stavrakakis
from django.db import transaction
35 3524241a Christos Stavrakakis
from synnefo.db.models import Backend, Network
36 1a894bfe Christos Stavrakakis
from django.db.utils import IntegrityError
37 b3d28af2 Christos Stavrakakis
from synnefo.logic.backend import (get_physical_resources,
38 b3d28af2 Christos Stavrakakis
                                   update_resources,
39 90b29b33 Christos Stavrakakis
                                   create_network_synced,
40 90b29b33 Christos Stavrakakis
                                   connect_network_synced)
41 225cea18 Christos Stavrakakis
from synnefo.management.common import check_backend_credentials
42 225cea18 Christos Stavrakakis
from synnefo.webproject.management.util import pprint_table
43 1a894bfe Christos Stavrakakis
44 1a894bfe Christos Stavrakakis
45 1a894bfe Christos Stavrakakis
class Command(BaseCommand):
46 1a894bfe Christos Stavrakakis
    can_import_settings = True
47 1a894bfe Christos Stavrakakis
48 1a894bfe Christos Stavrakakis
    help = 'Create a new backend.'
49 1a894bfe Christos Stavrakakis
    output_transaction = True  # The management command runs inside
50 1a894bfe Christos Stavrakakis
                               # an SQL transaction
51 1a894bfe Christos Stavrakakis
    option_list = BaseCommand.option_list + (
52 1a894bfe Christos Stavrakakis
        make_option('--clustername', dest='clustername'),
53 1a894bfe Christos Stavrakakis
        make_option('--port', dest='port', default=5080),
54 b3d28af2 Christos Stavrakakis
        make_option('--user', dest='username'),
55 b3d28af2 Christos Stavrakakis
        make_option('--pass', dest='password'),
56 cc92b70f Christos Stavrakakis
        make_option(
57 cc92b70f Christos Stavrakakis
            '--no-check', action='store_false',
58 b3d28af2 Christos Stavrakakis
            dest='check', default=True,
59 b3d28af2 Christos Stavrakakis
            help="Do not perform credentials check and resources update"),
60 cc92b70f Christos Stavrakakis
        make_option(
61 cc92b70f Christos Stavrakakis
            '--no-init', action='store_false',
62 b3d28af2 Christos Stavrakakis
            dest='init', default=True,
63 b3d28af2 Christos Stavrakakis
            help="Do not perform initialization of the Backend Model")
64 cc92b70f Christos Stavrakakis
    )
65 1a894bfe Christos Stavrakakis
66 88b8f176 Christos Stavrakakis
    @transaction.commit_on_success
67 1a894bfe Christos Stavrakakis
    def handle(self, **options):
68 1a894bfe Christos Stavrakakis
        clustername = options['clustername']
69 b3d28af2 Christos Stavrakakis
        port = options['port']
70 e41977e2 Christos Stavrakakis
        username = options['username']
71 e41977e2 Christos Stavrakakis
        password = options['password']
72 1a894bfe Christos Stavrakakis
73 1a894bfe Christos Stavrakakis
        if not (clustername and username and password):
74 b3d28af2 Christos Stavrakakis
            raise CommandError("Clustername, user and pass must be supplied")
75 1a894bfe Christos Stavrakakis
76 1a894bfe Christos Stavrakakis
        # Ensure correctness of credentials
77 1a894bfe Christos Stavrakakis
        if options['check']:
78 2333a2c4 Christos Stavrakakis
            check_backend_credentials(clustername, port, username, password)
79 f7759b2d Christos Stavrakakis
80 1a894bfe Christos Stavrakakis
        # Create the new backend in database
81 1a894bfe Christos Stavrakakis
        try:
82 1a894bfe Christos Stavrakakis
            backend = Backend.objects.create(clustername=clustername,
83 1a894bfe Christos Stavrakakis
                                             port=port,
84 1a894bfe Christos Stavrakakis
                                             username=username,
85 1a894bfe Christos Stavrakakis
                                             password=password,
86 88b8f176 Christos Stavrakakis
                                             drained=True)
87 1a894bfe Christos Stavrakakis
        except IntegrityError as e:
88 b3d28af2 Christos Stavrakakis
            raise CommandError("Cannot create backend: %s\n" % e)
89 1a894bfe Christos Stavrakakis
90 b3d28af2 Christos Stavrakakis
        self.stdout.write('\nSuccessfully created backend with id %d\n' %
91 b3d28af2 Christos Stavrakakis
                          backend.id)
92 b3d28af2 Christos Stavrakakis
93 b3d28af2 Christos Stavrakakis
        if not options['check']:
94 b3d28af2 Christos Stavrakakis
            return
95 b3d28af2 Christos Stavrakakis
96 b3d28af2 Christos Stavrakakis
        self.stdout.write('\rRetrieving backend resources:\n')
97 b3d28af2 Christos Stavrakakis
        resources = get_physical_resources(backend)
98 b3d28af2 Christos Stavrakakis
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
99 88b8f176 Christos Stavrakakis
100 88b8f176 Christos Stavrakakis
        table = [[str(resources[x]) for x in attr]]
101 88b8f176 Christos Stavrakakis
        pprint_table(self.stdout, table, attr)
102 88b8f176 Christos Stavrakakis
103 b3d28af2 Christos Stavrakakis
        update_resources(backend, resources)
104 b3d28af2 Christos Stavrakakis
105 b3d28af2 Christos Stavrakakis
        if not options['init']:
106 b3d28af2 Christos Stavrakakis
            return
107 b3d28af2 Christos Stavrakakis
108 fbb74fff Christos Stavrakakis
        networks = Network.objects.filter(deleted=False, public=False)
109 fbb74fff Christos Stavrakakis
        if not networks:
110 fbb74fff Christos Stavrakakis
            return
111 b3d28af2 Christos Stavrakakis
112 b3d28af2 Christos Stavrakakis
        self.stdout.write('\nCreating the follow networks:\n')
113 88b8f176 Christos Stavrakakis
        headers = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public')
114 88b8f176 Christos Stavrakakis
        table = []
115 b3d28af2 Christos Stavrakakis
116 b3d28af2 Christos Stavrakakis
        for net in networks:
117 88b8f176 Christos Stavrakakis
            table.append((net.backend_id, str(net.subnet), str(net.gateway),
118 88b8f176 Christos Stavrakakis
                         str(net.mac_prefix), str(net.public)))
119 88b8f176 Christos Stavrakakis
        pprint_table(self.stdout, table, headers)
120 b3d28af2 Christos Stavrakakis
121 b3d28af2 Christos Stavrakakis
        for net in networks:
122 7cfbbf32 Christos Stavrakakis
            net.create_backend_network(backend)
123 b3d28af2 Christos Stavrakakis
            result = create_network_synced(net, backend)
124 b3d28af2 Christos Stavrakakis
            if result[0] != "success":
125 cc92b70f Christos Stavrakakis
                self.stdout.write('\nError Creating Network %s: %s\n' %
126 e41977e2 Christos Stavrakakis
                                  (net.backend_id, result[1]))
127 a9ba398e Christos Stavrakakis
            else:
128 a9ba398e Christos Stavrakakis
                self.stdout.write('Successfully created Network: %s\n' %
129 cc92b70f Christos Stavrakakis
                                  net.backend_id)
130 90b29b33 Christos Stavrakakis
            result = connect_network_synced(network=net, backend=backend)
131 90b29b33 Christos Stavrakakis
            if result[0] != "success":
132 cc92b70f Christos Stavrakakis
                self.stdout.write('\nError Connecting Network %s: %s\n' %
133 90b29b33 Christos Stavrakakis
                                  (net.backend_id, result[1]))
134 a9ba398e Christos Stavrakakis
            else:
135 a9ba398e Christos Stavrakakis
                self.stdout.write('Successfully connected Network: %s\n' %
136 cc92b70f Christos Stavrakakis
                                  net.backend_id)