root / snf-cyclades-app / synnefo / logic / management / commands / backend-add.py @ 7cfbbf32
History | View | Annotate | Download (6.5 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 synnefo.db.models import Backend, Network, BackendNetwork |
35 |
from django.db.utils import IntegrityError |
36 |
from synnefo.logic.backend import (get_physical_resources, |
37 |
update_resources, |
38 |
create_client, |
39 |
create_network_synced, |
40 |
connect_network_synced) |
41 |
from synnefo.util.rapi import GanetiApiError |
42 |
|
43 |
|
44 |
class Command(BaseCommand): |
45 |
can_import_settings = True
|
46 |
|
47 |
help = 'Create a new backend.'
|
48 |
output_transaction = True # The management command runs inside |
49 |
# an SQL transaction
|
50 |
option_list = BaseCommand.option_list + ( |
51 |
make_option('--clustername', dest='clustername'), |
52 |
make_option('--port', dest='port', default=5080), |
53 |
make_option('--user', dest='username'), |
54 |
make_option('--pass', dest='password'), |
55 |
make_option('--drained', action='store_true', |
56 |
dest='drained', default=False, |
57 |
help="Set as drained to exclude from allocations"),
|
58 |
make_option('--no-check', action='store_false', |
59 |
dest='check', default=True, |
60 |
help="Do not perform credentials check and resources update"),
|
61 |
make_option('--no-init', action='store_false', |
62 |
dest='init', default=True, |
63 |
help="Do not perform initialization of the Backend Model")
|
64 |
) |
65 |
|
66 |
def handle(self, **options): |
67 |
clustername = options['clustername']
|
68 |
port = options['port']
|
69 |
username = options['username']
|
70 |
password = options['password']
|
71 |
drained = options['drained']
|
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 |
self.stdout.write('Checking connectivity and credentials.\n') |
79 |
try:
|
80 |
client = create_client(clustername, port, username, password) |
81 |
# This command will raise an exception if there is no
|
82 |
# write-access
|
83 |
client.ModifyCluster() |
84 |
except GanetiApiError as e: |
85 |
self.stdout.write('Check failed:\n%s\n' % e) |
86 |
return
|
87 |
else:
|
88 |
self.stdout.write('Check passed.\n') |
89 |
|
90 |
# Create the new backend in database
|
91 |
try:
|
92 |
backend = Backend.objects.create(clustername=clustername, |
93 |
port=port, |
94 |
username=username, |
95 |
password=password, |
96 |
drained=drained) |
97 |
except IntegrityError as e: |
98 |
raise CommandError("Cannot create backend: %s\n" % e) |
99 |
|
100 |
self.stdout.write('\nSuccessfully created backend with id %d\n' % |
101 |
backend.id) |
102 |
|
103 |
if not options['check']: |
104 |
return
|
105 |
|
106 |
self.stdout.write('\rRetrieving backend resources:\n') |
107 |
resources = get_physical_resources(backend) |
108 |
attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] |
109 |
self.stdout.write('----------------------------\n') |
110 |
for a in attr: |
111 |
self.stdout.write(a.ljust(12) + ' : ' + str(resources[a]) + '\n') |
112 |
update_resources(backend, resources) |
113 |
|
114 |
if not options['init']: |
115 |
return
|
116 |
|
117 |
networks = Network.objects.filter(deleted=False)
|
118 |
|
119 |
self.stdout.write('\nCreating the follow networks:\n') |
120 |
fields = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public') |
121 |
columns = (20, 16, 16, 16, 10) |
122 |
line = ' '.join(f.ljust(c) for f, c in zip(fields, columns)) |
123 |
sep = '-' * len(line) |
124 |
self.stdout.write(sep + '\n') |
125 |
self.stdout.write(line + '\n') |
126 |
self.stdout.write(sep + '\n') |
127 |
|
128 |
for net in networks: |
129 |
fields = (net.backend_id, str(net.subnet), str(net.gateway), |
130 |
str(net.mac_prefix), str(net.public)) |
131 |
line = ' '.join(f.ljust(c) for f, c in zip(fields, columns)) |
132 |
self.stdout.write(line + '\n') |
133 |
self.stdout.write(sep + '\n\n') |
134 |
|
135 |
for net in networks: |
136 |
net.create_backend_network(backend) |
137 |
result = create_network_synced(net, backend) |
138 |
if result[0] != "success": |
139 |
self.stdout.write('\nError Creating Network %s: %s\n' %\ |
140 |
(net.backend_id, result[1]))
|
141 |
else:
|
142 |
self.stdout.write('Successfully created Network: %s\n' % |
143 |
net.backend_id) |
144 |
result = connect_network_synced(network=net, backend=backend) |
145 |
if result[0] != "success": |
146 |
self.stdout.write('\nError Connecting Network %s: %s\n' %\ |
147 |
(net.backend_id, result[1]))
|
148 |
else:
|
149 |
self.stdout.write('Successfully connected Network: %s\n' % |
150 |
net.backend_id) |