Statistics
| Branch: | Tag: | Revision:

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

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 88b8f176 Christos Stavrakakis
from synnefo.management.common import check_backend_credentials, pprint_table
42 1a894bfe Christos Stavrakakis
43 1a894bfe Christos Stavrakakis
44 1a894bfe Christos Stavrakakis
class Command(BaseCommand):
45 1a894bfe Christos Stavrakakis
    can_import_settings = True
46 1a894bfe Christos Stavrakakis
47 1a894bfe Christos Stavrakakis
    help = 'Create a new backend.'
48 1a894bfe Christos Stavrakakis
    output_transaction = True  # The management command runs inside
49 1a894bfe Christos Stavrakakis
                               # an SQL transaction
50 1a894bfe Christos Stavrakakis
    option_list = BaseCommand.option_list + (
51 1a894bfe Christos Stavrakakis
        make_option('--clustername', dest='clustername'),
52 1a894bfe Christos Stavrakakis
        make_option('--port', dest='port', default=5080),
53 b3d28af2 Christos Stavrakakis
        make_option('--user', dest='username'),
54 b3d28af2 Christos Stavrakakis
        make_option('--pass', dest='password'),
55 cc92b70f Christos Stavrakakis
        make_option(
56 cc92b70f Christos Stavrakakis
            '--no-check', action='store_false',
57 b3d28af2 Christos Stavrakakis
            dest='check', default=True,
58 b3d28af2 Christos Stavrakakis
            help="Do not perform credentials check and resources update"),
59 cc92b70f Christos Stavrakakis
        make_option(
60 cc92b70f Christos Stavrakakis
            '--no-init', action='store_false',
61 b3d28af2 Christos Stavrakakis
            dest='init', default=True,
62 b3d28af2 Christos Stavrakakis
            help="Do not perform initialization of the Backend Model")
63 cc92b70f Christos Stavrakakis
    )
64 1a894bfe Christos Stavrakakis
65 88b8f176 Christos Stavrakakis
    @transaction.commit_on_success
66 1a894bfe Christos Stavrakakis
    def handle(self, **options):
67 1a894bfe Christos Stavrakakis
        clustername = options['clustername']
68 b3d28af2 Christos Stavrakakis
        port = options['port']
69 e41977e2 Christos Stavrakakis
        username = options['username']
70 e41977e2 Christos Stavrakakis
        password = options['password']
71 1a894bfe Christos Stavrakakis
72 1a894bfe Christos Stavrakakis
        if not (clustername and username and password):
73 b3d28af2 Christos Stavrakakis
            raise CommandError("Clustername, user and pass must be supplied")
74 1a894bfe Christos Stavrakakis
75 1a894bfe Christos Stavrakakis
        # Ensure correctness of credentials
76 1a894bfe Christos Stavrakakis
        if options['check']:
77 2333a2c4 Christos Stavrakakis
            check_backend_credentials(clustername, port, username, password)
78 f7759b2d Christos Stavrakakis
79 1a894bfe Christos Stavrakakis
        # Create the new backend in database
80 1a894bfe Christos Stavrakakis
        try:
81 1a894bfe Christos Stavrakakis
            backend = Backend.objects.create(clustername=clustername,
82 1a894bfe Christos Stavrakakis
                                             port=port,
83 1a894bfe Christos Stavrakakis
                                             username=username,
84 1a894bfe Christos Stavrakakis
                                             password=password,
85 88b8f176 Christos Stavrakakis
                                             drained=True)
86 1a894bfe Christos Stavrakakis
        except IntegrityError as e:
87 b3d28af2 Christos Stavrakakis
            raise CommandError("Cannot create backend: %s\n" % e)
88 1a894bfe Christos Stavrakakis
89 b3d28af2 Christos Stavrakakis
        self.stdout.write('\nSuccessfully created backend with id %d\n' %
90 b3d28af2 Christos Stavrakakis
                          backend.id)
91 b3d28af2 Christos Stavrakakis
92 b3d28af2 Christos Stavrakakis
        if not options['check']:
93 b3d28af2 Christos Stavrakakis
            return
94 b3d28af2 Christos Stavrakakis
95 b3d28af2 Christos Stavrakakis
        self.stdout.write('\rRetrieving backend resources:\n')
96 b3d28af2 Christos Stavrakakis
        resources = get_physical_resources(backend)
97 b3d28af2 Christos Stavrakakis
        attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal']
98 88b8f176 Christos Stavrakakis
99 88b8f176 Christos Stavrakakis
        table = [[str(resources[x]) for x in attr]]
100 88b8f176 Christos Stavrakakis
        pprint_table(self.stdout, table, attr)
101 88b8f176 Christos Stavrakakis
102 b3d28af2 Christos Stavrakakis
        update_resources(backend, resources)
103 b3d28af2 Christos Stavrakakis
104 b3d28af2 Christos Stavrakakis
        if not options['init']:
105 b3d28af2 Christos Stavrakakis
            return
106 b3d28af2 Christos Stavrakakis
107 fbb74fff Christos Stavrakakis
        networks = Network.objects.filter(deleted=False, public=False)
108 fbb74fff Christos Stavrakakis
        if not networks:
109 fbb74fff Christos Stavrakakis
            return
110 b3d28af2 Christos Stavrakakis
111 b3d28af2 Christos Stavrakakis
        self.stdout.write('\nCreating the follow networks:\n')
112 88b8f176 Christos Stavrakakis
        headers = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public')
113 88b8f176 Christos Stavrakakis
        table = []
114 b3d28af2 Christos Stavrakakis
115 b3d28af2 Christos Stavrakakis
        for net in networks:
116 88b8f176 Christos Stavrakakis
            table.append((net.backend_id, str(net.subnet), str(net.gateway),
117 88b8f176 Christos Stavrakakis
                         str(net.mac_prefix), str(net.public)))
118 88b8f176 Christos Stavrakakis
        pprint_table(self.stdout, table, headers)
119 b3d28af2 Christos Stavrakakis
120 b3d28af2 Christos Stavrakakis
        for net in networks:
121 7cfbbf32 Christos Stavrakakis
            net.create_backend_network(backend)
122 b3d28af2 Christos Stavrakakis
            result = create_network_synced(net, backend)
123 b3d28af2 Christos Stavrakakis
            if result[0] != "success":
124 cc92b70f Christos Stavrakakis
                self.stdout.write('\nError Creating Network %s: %s\n' %
125 e41977e2 Christos Stavrakakis
                                  (net.backend_id, result[1]))
126 a9ba398e Christos Stavrakakis
            else:
127 a9ba398e Christos Stavrakakis
                self.stdout.write('Successfully created Network: %s\n' %
128 cc92b70f Christos Stavrakakis
                                  net.backend_id)
129 90b29b33 Christos Stavrakakis
            result = connect_network_synced(network=net, backend=backend)
130 90b29b33 Christos Stavrakakis
            if result[0] != "success":
131 cc92b70f Christos Stavrakakis
                self.stdout.write('\nError Connecting Network %s: %s\n' %
132 90b29b33 Christos Stavrakakis
                                  (net.backend_id, result[1]))
133 a9ba398e Christos Stavrakakis
            else:
134 a9ba398e Christos Stavrakakis
                self.stdout.write('Successfully connected Network: %s\n' %
135 cc92b70f Christos Stavrakakis
                                  net.backend_id)