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