root / snf-cyclades-app / synnefo / logic / management / commands / backend-add.py @ 8c911970
History | View | Annotate | Download (6.2 kB)
1 |
# Copyright 2011-2013 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 |
import sys |
31 |
from optparse import make_option |
32 |
from django.core.management.base import CommandError |
33 |
from snf_django.management.commands import SynnefoCommand |
34 |
|
35 |
from synnefo.db.models import Backend, Network |
36 |
from django.db.utils import IntegrityError |
37 |
from synnefo.logic import backend as backend_mod |
38 |
from synnefo.management.common import check_backend_credentials |
39 |
from snf_django.management.utils import pprint_table |
40 |
|
41 |
|
42 |
HYPERVISORS = [h[0] for h in Backend.HYPERVISORS] |
43 |
|
44 |
|
45 |
class Command(SynnefoCommand): |
46 |
can_import_settings = True
|
47 |
|
48 |
help = 'Create a new backend.'
|
49 |
option_list = SynnefoCommand.option_list + ( |
50 |
make_option('--clustername', dest='clustername'), |
51 |
make_option('--port', dest='port', default=5080), |
52 |
make_option('--user', dest='username'), |
53 |
make_option('--pass', dest='password'), |
54 |
make_option( |
55 |
'--no-check',
|
56 |
action='store_false',
|
57 |
dest='check',
|
58 |
default=True,
|
59 |
help="Do not perform credentials check and resources update"),
|
60 |
make_option( |
61 |
'--hypervisor',
|
62 |
dest='hypervisor',
|
63 |
default=None,
|
64 |
choices=HYPERVISORS, |
65 |
metavar="|".join(HYPERVISORS),
|
66 |
help="The hypervisor that the Ganeti backend uses"),
|
67 |
make_option( |
68 |
'--no-init', action='store_false', |
69 |
dest='init', default=True, |
70 |
help="Do not perform initialization of the Backend Model")
|
71 |
) |
72 |
|
73 |
def handle(self, *args, **options): |
74 |
if len(args) > 0: |
75 |
raise CommandError("Command takes no arguments") |
76 |
|
77 |
clustername = options['clustername']
|
78 |
port = options['port']
|
79 |
username = options['username']
|
80 |
password = options['password']
|
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 |
create_backend(clustername, port, username, password, |
90 |
hypervisor=options["hypervisor"],
|
91 |
initialize=options["init"])
|
92 |
|
93 |
|
94 |
def create_backend(clustername, port, username, password, hypervisor=None, |
95 |
initialize=True, stream=sys.stdout):
|
96 |
kw = {"clustername": clustername,
|
97 |
"port": port,
|
98 |
"username": username,
|
99 |
"password": password,
|
100 |
"drained": True} |
101 |
|
102 |
if hypervisor:
|
103 |
kw["hypervisor"] = hypervisor
|
104 |
|
105 |
# Create the new backend in database
|
106 |
try:
|
107 |
backend = Backend.objects.create(**kw) |
108 |
except IntegrityError as e: |
109 |
raise CommandError("Cannot create backend: %s\n" % e) |
110 |
|
111 |
stream.write("Successfully created backend with id %d\n" % backend.id)
|
112 |
|
113 |
if not initialize: |
114 |
return
|
115 |
|
116 |
stream.write("Retrieving backend resources:\n")
|
117 |
resources = backend_mod.get_physical_resources(backend) |
118 |
attr = ['mfree', 'mtotal', 'dfree', 'dtotal', 'pinst_cnt', 'ctotal'] |
119 |
|
120 |
table = [[str(resources[x]) for x in attr]] |
121 |
pprint_table(stream, table, attr) |
122 |
|
123 |
backend_mod.update_backend_resources(backend, resources) |
124 |
backend_mod.update_backend_disk_templates(backend) |
125 |
|
126 |
networks = Network.objects.filter(deleted=False, public=True) |
127 |
if not networks: |
128 |
return
|
129 |
|
130 |
stream.write("Creating the following public:\n")
|
131 |
headers = ("ID", "Name", 'IPv4 Subnet', "IPv6 Subnet", 'Mac Prefix') |
132 |
table = [] |
133 |
|
134 |
for net in networks: |
135 |
subnet4 = net.subnet4.cidr if net.subnet4 else None |
136 |
subnet6 = net.subnet6.cidr if net.subnet6 else None |
137 |
table.append((net.id, net.backend_id, subnet4, |
138 |
subnet6, str(net.mac_prefix)))
|
139 |
pprint_table(stream, table, headers) |
140 |
|
141 |
for net in networks: |
142 |
net.create_backend_network(backend) |
143 |
result = backend_mod.create_network_synced(net, backend) |
144 |
if result[0] != "success": |
145 |
stream.write('\nError Creating Network %s: %s\n' %
|
146 |
(net.backend_id, result[1]))
|
147 |
else:
|
148 |
stream.write('Successfully created Network: %s\n' %
|
149 |
net.backend_id) |
150 |
result = backend_mod.connect_network_synced(network=net, |
151 |
backend=backend) |
152 |
if result[0] != "success": |
153 |
stream.write('\nError Connecting Network %s: %s\n' %
|
154 |
(net.backend_id, result[1]))
|
155 |
else:
|
156 |
stream.write('Successfully connected Network: %s\n' %
|
157 |
net.backend_id) |