root / snf-cyclades-app / synnefo / logic / management / commands / backend-add.py @ 9115d567
History | View | Annotate | Download (5.9 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 |
35 |
from django.db.utils import IntegrityError |
36 |
from synnefo.logic.backend import (get_physical_resources, |
37 |
update_resources, |
38 |
create_network_synced, |
39 |
connect_network_synced) |
40 |
from synnefo.management.common import check_backend_credentials |
41 |
from synnefo.webproject.management.utils import pprint_table |
42 |
|
43 |
|
44 |
HYPERVISORS = [h[0] for h in Backend.HYPERVISORS] |
45 |
|
46 |
|
47 |
class Command(BaseCommand): |
48 |
can_import_settings = True
|
49 |
|
50 |
help = 'Create a new backend.'
|
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('--hypervisor',
|
61 |
dest='hypervisor',
|
62 |
default=None,
|
63 |
choices=HYPERVISORS, |
64 |
metavar="|".join(HYPERVISORS),
|
65 |
help="The hypervisor that the Ganeti backend uses"),
|
66 |
make_option( |
67 |
'--no-init', action='store_false', |
68 |
dest='init', default=True, |
69 |
help="Do not perform initialization of the Backend Model")
|
70 |
) |
71 |
|
72 |
def handle(self, *args, **options): |
73 |
if len(args) > 0: |
74 |
raise CommandError("Command takes no arguments") |
75 |
|
76 |
clustername = options['clustername']
|
77 |
port = options['port']
|
78 |
username = options['username']
|
79 |
password = options['password']
|
80 |
hypervisor = options["hypervisor"]
|
81 |
|
82 |
if not (clustername and username and password): |
83 |
raise CommandError("Clustername, user and pass must be supplied") |
84 |
|
85 |
# Ensure correctness of credentials
|
86 |
if options['check']: |
87 |
check_backend_credentials(clustername, port, username, password) |
88 |
|
89 |
kw = {"clustername": clustername,
|
90 |
"port": port,
|
91 |
"username": username,
|
92 |
"password": password,
|
93 |
"drained": True} |
94 |
|
95 |
if hypervisor:
|
96 |
kw["hypervisor"] = hypervisor
|
97 |
# Create the new backend in database
|
98 |
try:
|
99 |
backend = Backend.objects.create(**kw) |
100 |
except IntegrityError as e: |
101 |
raise CommandError("Cannot create backend: %s\n" % e) |
102 |
|
103 |
self.stdout.write('\nSuccessfully created backend with id %d\n' % |
104 |
backend.id) |
105 |
|
106 |
if not options['check']: |
107 |
return
|
108 |
|
109 |
self.stdout.write('\rRetrieving backend resources:\n') |
110 |
resources = get_physical_resources(backend) |
111 |
attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] |
112 |
|
113 |
table = [[str(resources[x]) for x in attr]] |
114 |
pprint_table(self.stdout, table, attr)
|
115 |
|
116 |
update_resources(backend, resources) |
117 |
|
118 |
if not options['init']: |
119 |
return
|
120 |
|
121 |
networks = Network.objects.filter(deleted=False, floating_ip_pool=True) |
122 |
if not networks: |
123 |
return
|
124 |
|
125 |
self.stdout.write('\nCreating the follow networks:\n') |
126 |
headers = ('Name', 'Subnet', 'Gateway', 'Mac Prefix', 'Public') |
127 |
table = [] |
128 |
|
129 |
for net in networks: |
130 |
table.append((net.backend_id, str(net.subnet), str(net.gateway), |
131 |
str(net.mac_prefix), str(net.public))) |
132 |
pprint_table(self.stdout, table, headers)
|
133 |
|
134 |
for net in networks: |
135 |
net.create_backend_network(backend) |
136 |
result = create_network_synced(net, backend) |
137 |
if result[0] != "success": |
138 |
self.stdout.write('\nError Creating Network %s: %s\n' % |
139 |
(net.backend_id, result[1]))
|
140 |
else:
|
141 |
self.stdout.write('Successfully created Network: %s\n' % |
142 |
net.backend_id) |
143 |
result = connect_network_synced(network=net, backend=backend) |
144 |
if result[0] != "success": |
145 |
self.stdout.write('\nError Connecting Network %s: %s\n' % |
146 |
(net.backend_id, result[1]))
|
147 |
else:
|
148 |
self.stdout.write('Successfully connected Network: %s\n' % |
149 |
net.backend_id) |