root / snf-cyclades-app / synnefo / management / common.py @ 8b3e98f8
History | View | Annotate | Download (8.4 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
|
35 |
import ipaddr |
36 |
from datetime import datetime |
37 |
|
38 |
from django.utils.timesince import timesince, timeuntil |
39 |
|
40 |
from django.core.management import CommandError |
41 |
from synnefo.db.models import Backend, VirtualMachine, Network, Flavor |
42 |
from synnefo.api.util import get_image as backend_get_image |
43 |
from synnefo.api.faults import ItemNotFound |
44 |
from django.core.exceptions import FieldError |
45 |
|
46 |
from synnefo.api.util import validate_network_size |
47 |
from synnefo.settings import MAX_CIDR_BLOCK |
48 |
from synnefo.logic.rapi import GanetiApiError, GanetiRapiClient |
49 |
|
50 |
|
51 |
def format_bool(b): |
52 |
return 'YES' if b else 'NO' |
53 |
|
54 |
|
55 |
def parse_bool(string): |
56 |
if string == "True": |
57 |
return True |
58 |
elif string == "False": |
59 |
return False |
60 |
else:
|
61 |
raise Exception("Can not parse string %s to bool" % string) |
62 |
|
63 |
|
64 |
|
65 |
def format_date(d): |
66 |
if not d: |
67 |
return '' |
68 |
|
69 |
if d < datetime.now():
|
70 |
return timesince(d) + ' ago' |
71 |
else:
|
72 |
return 'in ' + timeuntil(d) |
73 |
|
74 |
|
75 |
def format_vm_state(vm): |
76 |
if vm.operstate == "BUILD": |
77 |
return "BUILD(" + str(vm.buildpercentage) + "%)" |
78 |
else:
|
79 |
return vm.operstate
|
80 |
|
81 |
|
82 |
def validate_network_info(options): |
83 |
subnet = options['subnet']
|
84 |
gateway = options['gateway']
|
85 |
subnet6 = options['subnet6']
|
86 |
gateway6 = options['gateway6']
|
87 |
|
88 |
try:
|
89 |
net = ipaddr.IPv4Network(subnet) |
90 |
prefix = net.prefixlen |
91 |
if not validate_network_size(prefix): |
92 |
raise CommandError("Unsupport network mask %d." |
93 |
" Must be in range (%s,29] "
|
94 |
% (prefix, MAX_CIDR_BLOCK)) |
95 |
except ValueError: |
96 |
raise CommandError('Malformed subnet') |
97 |
try:
|
98 |
gateway and ipaddr.IPv4Address(gateway) or None |
99 |
except ValueError: |
100 |
raise CommandError('Malformed gateway') |
101 |
|
102 |
try:
|
103 |
subnet6 and ipaddr.IPv6Network(subnet6) or None |
104 |
except ValueError: |
105 |
raise CommandError('Malformed subnet6') |
106 |
|
107 |
try:
|
108 |
gateway6 and ipaddr.IPv6Address(gateway6) or None |
109 |
except ValueError: |
110 |
raise CommandError('Malformed gateway6') |
111 |
|
112 |
return subnet, gateway, subnet6, gateway6
|
113 |
|
114 |
|
115 |
def get_backend(backend_id): |
116 |
try:
|
117 |
backend_id = int(backend_id)
|
118 |
return Backend.objects.get(id=backend_id)
|
119 |
except ValueError: |
120 |
raise CommandError("Invalid Backend ID: %s" % backend_id) |
121 |
except Backend.DoesNotExist:
|
122 |
raise CommandError("Backend with ID %s not found in DB. " |
123 |
" Use snf-manage backend-list to find"
|
124 |
" out available backend IDs." % backend_id)
|
125 |
|
126 |
|
127 |
def get_image(image_id, user_id): |
128 |
if image_id:
|
129 |
try:
|
130 |
return backend_get_image(image_id, user_id)
|
131 |
except ItemNotFound:
|
132 |
raise CommandError("Image with ID %s not found." |
133 |
" Use snf-manage image-list to find"
|
134 |
" out available image IDs." % image_id)
|
135 |
else:
|
136 |
raise CommandError("image-id is mandatory") |
137 |
|
138 |
|
139 |
def get_vm(server_id): |
140 |
try:
|
141 |
server_id = int(server_id)
|
142 |
return VirtualMachine.objects.get(id=server_id)
|
143 |
except ValueError: |
144 |
raise CommandError("Invalid server ID: %s", server_id) |
145 |
except VirtualMachine.DoesNotExist:
|
146 |
raise CommandError("Server with ID %s not found in DB." |
147 |
" Use snf-manage server-list to find out"
|
148 |
" available server IDs." % server_id)
|
149 |
|
150 |
|
151 |
def get_network(network_id): |
152 |
try:
|
153 |
network_id = int(network_id)
|
154 |
return Network.objects.get(id=network_id)
|
155 |
except ValueError: |
156 |
raise CommandError("Invalid network ID: %s", network_id) |
157 |
except Network.DoesNotExist:
|
158 |
raise CommandError("Network with ID %s not found in DB." |
159 |
" Use snf-manage network-list to find out"
|
160 |
" available network IDs." % network_id)
|
161 |
|
162 |
|
163 |
def get_flavor(flavor_id): |
164 |
try:
|
165 |
flavor_id = int(flavor_id)
|
166 |
return Flavor.objects.get(id=flavor_id)
|
167 |
except ValueError: |
168 |
raise CommandError("Invalid flavor ID: %s", flavor_id) |
169 |
except Flavor.DoesNotExist:
|
170 |
raise CommandError("Flavor with ID %s not found in DB." |
171 |
" Use snf-manage flavor-list to find out"
|
172 |
" available flavor IDs." % flavor_id)
|
173 |
|
174 |
|
175 |
def filter_results(objects, filter_by): |
176 |
filter_list = filter_by.split(",")
|
177 |
filter_dict = {} |
178 |
exclude_dict = {} |
179 |
|
180 |
def map_field_type(query): |
181 |
def fix_bool(val): |
182 |
if val.lower() in ("yes", "true", "t"): |
183 |
return True |
184 |
if val.lower() in ("no", "false", "f"): |
185 |
return False |
186 |
return val
|
187 |
|
188 |
if "!=" in query: |
189 |
key, val = query.split("!=")
|
190 |
exclude_dict[key] = fix_bool(val) |
191 |
return
|
192 |
OP_MAP = { |
193 |
">=": "__gte", |
194 |
"=>": "__gte", |
195 |
">": "__gt", |
196 |
"<=": "__lte", |
197 |
"=<": "__lte", |
198 |
"<": "__lt", |
199 |
"=": "" |
200 |
} |
201 |
for op, new_op in OP_MAP.items(): |
202 |
if op in query: |
203 |
key, val = query.split(op) |
204 |
filter_dict[key + new_op] = fix_bool(val) |
205 |
return
|
206 |
|
207 |
map(lambda x: map_field_type(x), filter_list) |
208 |
|
209 |
try:
|
210 |
objects = objects.filter(**filter_dict) |
211 |
return objects.exclude(**exclude_dict)
|
212 |
except FieldError as e: |
213 |
raise CommandError(e)
|
214 |
except Exception as e: |
215 |
raise CommandError("Can not filter results: %s" % e) |
216 |
|
217 |
|
218 |
def check_backend_credentials(clustername, port, username, password): |
219 |
try:
|
220 |
client = GanetiRapiClient(clustername, port, username, password) |
221 |
# This command will raise an exception if there is no
|
222 |
# write-access
|
223 |
client.ModifyCluster() |
224 |
except GanetiApiError as e: |
225 |
raise CommandError(e)
|
226 |
|
227 |
info = client.GetInfo() |
228 |
info_name = info['name']
|
229 |
if info_name != clustername:
|
230 |
raise CommandError("Invalid clustername value. Please use the" |
231 |
" Ganeti Cluster name: %s" % info_name)
|
232 |
|
233 |
|
234 |
def pprint_table(out, table, headers=None, separator=None): |
235 |
"""Print a pretty, aligned string representation of table.
|
236 |
|
237 |
Works by finding out the max width of each column and padding to data
|
238 |
to this value.
|
239 |
"""
|
240 |
|
241 |
sep = separator if separator else " " |
242 |
|
243 |
if headers:
|
244 |
table.insert(0, headers)
|
245 |
|
246 |
# Find out the max width of each column
|
247 |
widths = [max(map(len, col)) for col in zip(*table)] |
248 |
|
249 |
t_length = sum(widths) + len(sep) * (len(widths) - 1) |
250 |
if headers:
|
251 |
# pretty print the headers
|
252 |
print >> out, sep.join((val.rjust(width) for val, width \ |
253 |
in zip(headers, widths))) |
254 |
print >> out, "-" * t_length |
255 |
# remove headers
|
256 |
table = table[1:]
|
257 |
|
258 |
# print the rest table
|
259 |
for row in table: |
260 |
print >> out, sep.join((val.rjust(width).encode('utf8') \ |
261 |
for val, width in zip(row, widths))) |