root / kamaki / clients / cyclades / __init__.py @ e51c7d5b
History | View | Annotate | Download (15.5 kB)
1 |
# Copyright 2011-2013 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 |
from sys import stdout |
35 |
from time import sleep |
36 |
|
37 |
from kamaki.clients.cyclades.rest_api import CycladesRestClient |
38 |
from kamaki.clients import ClientError |
39 |
|
40 |
|
41 |
class CycladesClient(CycladesRestClient): |
42 |
"""Synnefo Cyclades Compute API client"""
|
43 |
|
44 |
def create_server( |
45 |
self, name, flavor_id, image_id,
|
46 |
metadata=None, personality=None): |
47 |
"""Submit request to create a new server
|
48 |
|
49 |
:param name: (str)
|
50 |
|
51 |
:param flavor_id: integer id denoting a preset hardware configuration
|
52 |
|
53 |
:param image_id: (str) id denoting the OS image to run on the VM
|
54 |
|
55 |
:param metadata: (dict) vm metadata updated by os/users image metadata
|
56 |
|
57 |
:param personality: a list of (file path, file contents) tuples,
|
58 |
describing files to be injected into VM upon creation.
|
59 |
|
60 |
:returns: a dict with the new VMs details
|
61 |
|
62 |
:raises ClientError: wraps request errors
|
63 |
"""
|
64 |
image = self.get_image_details(image_id)
|
65 |
metadata = metadata or dict() |
66 |
for key in ('os', 'users'): |
67 |
try:
|
68 |
metadata[key] = image['metadata'][key]
|
69 |
except KeyError: |
70 |
pass
|
71 |
|
72 |
return super(CycladesClient, self).create_server( |
73 |
name, flavor_id, image_id, |
74 |
metadata=metadata, personality=personality) |
75 |
|
76 |
def start_server(self, server_id): |
77 |
"""Submit a startup request
|
78 |
|
79 |
:param server_id: integer (str or int)
|
80 |
|
81 |
:returns: (dict) response headers
|
82 |
"""
|
83 |
req = {'start': {}}
|
84 |
r = self.servers_action_post(server_id, json_data=req, success=202) |
85 |
return r.headers
|
86 |
|
87 |
def shutdown_server(self, server_id): |
88 |
"""Submit a shutdown request
|
89 |
|
90 |
:param server_id: integer (str or int)
|
91 |
|
92 |
:returns: (dict) response headers
|
93 |
"""
|
94 |
req = {'shutdown': {}}
|
95 |
r = self.servers_action_post(server_id, json_data=req, success=202) |
96 |
return r.headers
|
97 |
|
98 |
def get_server_console(self, server_id): |
99 |
"""
|
100 |
:param server_id: integer (str or int)
|
101 |
|
102 |
:returns: (dict) info to set a VNC connection to VM
|
103 |
"""
|
104 |
req = {'console': {'type': 'vnc'}} |
105 |
r = self.servers_action_post(server_id, json_data=req, success=200) |
106 |
return r.json['console'] |
107 |
|
108 |
def get_firewall_profile(self, server_id): |
109 |
"""
|
110 |
:param server_id: integer (str or int)
|
111 |
|
112 |
:returns: (str) ENABLED | DISABLED | PROTECTED
|
113 |
|
114 |
:raises ClientError: 520 No Firewall Profile
|
115 |
"""
|
116 |
r = self.get_server_details(server_id)
|
117 |
try:
|
118 |
return r['attachments'][0]['firewallProfile'] |
119 |
except KeyError: |
120 |
raise ClientError(
|
121 |
'No Firewall Profile',
|
122 |
details='Server %s is missing a firewall profile' % server_id)
|
123 |
|
124 |
def set_firewall_profile(self, server_id, profile): |
125 |
"""Set the firewall profile for the public interface of a server
|
126 |
|
127 |
:param server_id: integer (str or int)
|
128 |
|
129 |
:param profile: (str) ENABLED | DISABLED | PROTECTED
|
130 |
|
131 |
:returns: (dict) response headers
|
132 |
"""
|
133 |
req = {'firewallProfile': {'profile': profile}} |
134 |
r = self.servers_action_post(server_id, json_data=req, success=202) |
135 |
return r.headers
|
136 |
|
137 |
def list_servers(self, detail=False, changes_since=None): |
138 |
"""
|
139 |
:param detail: (bool) append full server details to each item if true
|
140 |
|
141 |
:param changes_since: (date)
|
142 |
|
143 |
:returns: list of server ids and names
|
144 |
"""
|
145 |
r = self.servers_get(detail=bool(detail), changes_since=changes_since) |
146 |
return r.json['servers'] |
147 |
|
148 |
def list_server_nics(self, server_id): |
149 |
"""
|
150 |
:param server_id: integer (str or int)
|
151 |
|
152 |
:returns: (dict) network interface connections
|
153 |
"""
|
154 |
r = self.servers_ips_get(server_id)
|
155 |
return r.json['attachments'] |
156 |
|
157 |
def get_server_stats(self, server_id): |
158 |
"""
|
159 |
:param server_id: integer (str or int)
|
160 |
|
161 |
:returns: (dict) auto-generated graphs of statistics (urls)
|
162 |
"""
|
163 |
r = self.servers_stats_get(server_id)
|
164 |
return r.json['stats'] |
165 |
|
166 |
def list_networks(self, detail=False): |
167 |
"""
|
168 |
:param detail: (bool)
|
169 |
|
170 |
:returns: (list) id,name if not detail else full info per network
|
171 |
"""
|
172 |
detail = 'detail' if detail else '' |
173 |
r = self.networks_get(command=detail)
|
174 |
return r.json['networks'] |
175 |
|
176 |
def list_network_nics(self, network_id): |
177 |
"""
|
178 |
:param network_id: integer (str or int)
|
179 |
|
180 |
:returns: (list)
|
181 |
"""
|
182 |
r = self.networks_get(network_id=network_id)
|
183 |
return r.json['network']['attachments'] |
184 |
|
185 |
def create_network( |
186 |
self, name,
|
187 |
cidr=None, gateway=None, type=None, dhcp=False): |
188 |
"""
|
189 |
:param name: (str)
|
190 |
|
191 |
:param cidr: (str)
|
192 |
|
193 |
:param geteway: (str)
|
194 |
|
195 |
:param type: (str) if None, will use MAC_FILTERED as default
|
196 |
Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
|
197 |
|
198 |
:param dhcp: (bool)
|
199 |
|
200 |
:returns: (dict) network detailed info
|
201 |
"""
|
202 |
net = dict(name=name)
|
203 |
if cidr:
|
204 |
net['cidr'] = cidr
|
205 |
if gateway:
|
206 |
net['gateway'] = gateway
|
207 |
net['type'] = type or 'MAC_FILTERED' |
208 |
net['dhcp'] = True if dhcp else False |
209 |
req = dict(network=net)
|
210 |
r = self.networks_post(json_data=req, success=202) |
211 |
return r.json['network'] |
212 |
|
213 |
def get_network_details(self, network_id): |
214 |
"""
|
215 |
:param network_id: integer (str or int)
|
216 |
|
217 |
:returns: (dict)
|
218 |
"""
|
219 |
r = self.networks_get(network_id=network_id)
|
220 |
return r.json['network'] |
221 |
|
222 |
def update_network_name(self, network_id, new_name): |
223 |
"""
|
224 |
:param network_id: integer (str or int)
|
225 |
|
226 |
:param new_name: (str)
|
227 |
|
228 |
:returns: (dict) response headers
|
229 |
"""
|
230 |
req = {'network': {'name': new_name}} |
231 |
r = self.networks_put(network_id=network_id, json_data=req)
|
232 |
return r.headers
|
233 |
|
234 |
def delete_network(self, network_id): |
235 |
"""
|
236 |
:param network_id: integer (str or int)
|
237 |
|
238 |
:returns: (dict) response headers
|
239 |
|
240 |
:raises ClientError: 421 Network in use
|
241 |
"""
|
242 |
try:
|
243 |
r = self.networks_delete(network_id)
|
244 |
return r.headers
|
245 |
except ClientError as err: |
246 |
if err.status == 421: |
247 |
err.details = [ |
248 |
'Network may be still connected to at least one server']
|
249 |
raise
|
250 |
|
251 |
def connect_server(self, server_id, network_id): |
252 |
""" Connect a server to a network
|
253 |
|
254 |
:param server_id: integer (str or int)
|
255 |
|
256 |
:param network_id: integer (str or int)
|
257 |
|
258 |
:returns: (dict) response headers
|
259 |
"""
|
260 |
req = {'add': {'serverRef': server_id}} |
261 |
r = self.networks_post(network_id, 'action', json_data=req) |
262 |
return r.headers
|
263 |
|
264 |
def disconnect_server(self, server_id, nic_id): |
265 |
"""
|
266 |
:param server_id: integer (str or int)
|
267 |
|
268 |
:param nic_id: (str)
|
269 |
|
270 |
:returns: (int) the number of nics disconnected
|
271 |
"""
|
272 |
vm_nets = self.list_server_nics(server_id)
|
273 |
num_of_disconnections = 0
|
274 |
for (nic_id, network_id) in [( |
275 |
net['id'],
|
276 |
net['network_id']) for net in vm_nets if nic_id == net['id']]: |
277 |
req = {'remove': {'attachment': '%s' % nic_id}} |
278 |
self.networks_post(network_id, 'action', json_data=req) |
279 |
num_of_disconnections += 1
|
280 |
return num_of_disconnections
|
281 |
|
282 |
def disconnect_network_nics(self, netid): |
283 |
"""
|
284 |
:param netid: integer (str or int)
|
285 |
"""
|
286 |
for nic in self.list_network_nics(netid): |
287 |
req = dict(remove=dict(attachment=nic)) |
288 |
self.networks_post(netid, 'action', json_data=req) |
289 |
|
290 |
def _wait( |
291 |
self, item_id, current_status, get_status,
|
292 |
delay=1, max_wait=100, wait_cb=None): |
293 |
"""Wait for item while its status is current_status
|
294 |
|
295 |
:param server_id: integer (str or int)
|
296 |
|
297 |
:param current_status: (str)
|
298 |
|
299 |
:param get_status: (method(self, item_id)) if called, returns
|
300 |
(status, progress %) If no way to tell progress, return None
|
301 |
|
302 |
:param delay: time interval between retries
|
303 |
|
304 |
:param wait_cb: if set a progress bar is used to show progress
|
305 |
|
306 |
:returns: (str) the new mode if successful, (bool) False if timed out
|
307 |
"""
|
308 |
status, progress = get_status(self, item_id)
|
309 |
if status != current_status:
|
310 |
return status
|
311 |
old_wait = total_wait = 0
|
312 |
|
313 |
if wait_cb:
|
314 |
wait_gen = wait_cb(1 + max_wait // delay)
|
315 |
wait_gen.next() |
316 |
|
317 |
while status == current_status and total_wait <= max_wait: |
318 |
if wait_cb:
|
319 |
try:
|
320 |
for i in range(total_wait - old_wait): |
321 |
wait_gen.next() |
322 |
except Exception: |
323 |
break
|
324 |
else:
|
325 |
stdout.write('.')
|
326 |
stdout.flush() |
327 |
old_wait = total_wait |
328 |
total_wait = progress or (total_wait + 1) |
329 |
sleep(delay) |
330 |
status, progress = get_status(self, item_id)
|
331 |
|
332 |
if total_wait < max_wait:
|
333 |
if wait_cb:
|
334 |
try:
|
335 |
for i in range(max_wait): |
336 |
wait_gen.next() |
337 |
except:
|
338 |
pass
|
339 |
return status if status != current_status else False |
340 |
|
341 |
def wait_server( |
342 |
self, server_id,
|
343 |
current_status='BUILD',
|
344 |
delay=1, max_wait=100, wait_cb=None): |
345 |
"""Wait for server while its status is current_status
|
346 |
|
347 |
:param server_id: integer (str or int)
|
348 |
|
349 |
:param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
|
350 |
|
351 |
:param delay: time interval between retries
|
352 |
|
353 |
:param wait_cb: if set a progressbar is used to show progress
|
354 |
|
355 |
:returns: (str) the new mode if succesfull, (bool) False if timed out
|
356 |
"""
|
357 |
|
358 |
def get_status(self, server_id): |
359 |
r = self.get_server_details(server_id)
|
360 |
return r['status'], (r.get('progress', None) if ( |
361 |
current_status in ('BUILD', )) else None) |
362 |
|
363 |
return self._wait( |
364 |
server_id, current_status, get_status, delay, max_wait, wait_cb) |
365 |
|
366 |
def wait_network( |
367 |
self, net_id,
|
368 |
current_status='LALA', delay=1, max_wait=100, wait_cb=None): |
369 |
"""Wait for network while its status is current_status
|
370 |
|
371 |
:param net_id: integer (str or int)
|
372 |
|
373 |
:param current_status: (str) PENDING | ACTIVE | DELETED
|
374 |
|
375 |
:param delay: time interval between retries
|
376 |
|
377 |
:param wait_cb: if set a progressbar is used to show progress
|
378 |
|
379 |
:returns: (str) the new mode if succesfull, (bool) False if timed out
|
380 |
"""
|
381 |
|
382 |
def get_status(self, net_id): |
383 |
r = self.get_network_details(net_id)
|
384 |
return r['status'], None |
385 |
|
386 |
return self._wait( |
387 |
net_id, current_status, get_status, delay, max_wait, wait_cb) |
388 |
|
389 |
def get_floating_ip_pools(self): |
390 |
"""
|
391 |
:returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
|
392 |
"""
|
393 |
r = self.floating_ip_pools_get()
|
394 |
return r.json
|
395 |
|
396 |
def get_floating_ips(self): |
397 |
"""
|
398 |
:returns: (dict) {floating_ips:[
|
399 |
{fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
|
400 |
... ]}
|
401 |
"""
|
402 |
r = self.floating_ips_get()
|
403 |
return r.json
|
404 |
|
405 |
def alloc_floating_ip(self, pool=None, address=None): |
406 |
"""
|
407 |
:param pool: (str) pool of ips to allocate from
|
408 |
|
409 |
:param address: (str) ip address to request
|
410 |
|
411 |
:returns: (dict) {
|
412 |
fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...
|
413 |
}
|
414 |
"""
|
415 |
json_data = dict()
|
416 |
if pool:
|
417 |
json_data['pool'] = pool
|
418 |
if address:
|
419 |
json_data['address'] = address
|
420 |
r = self.floating_ips_post(json_data)
|
421 |
return r.json['floating_ip'] |
422 |
|
423 |
def get_floating_ip(self, fip_id): |
424 |
"""
|
425 |
:param fip_id: (str) floating ip id
|
426 |
|
427 |
:returns: (dict)
|
428 |
{fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
|
429 |
|
430 |
:raises AssertionError: if fip_id is emtpy
|
431 |
"""
|
432 |
assert fip_id, 'floating ip id is needed for get_floating_ip' |
433 |
r = self.floating_ips_get(fip_id)
|
434 |
return r.json['floating_ip'] |
435 |
|
436 |
def delete_floating_ip(self, fip_id=None): |
437 |
"""
|
438 |
:param fip_id: (str) floating ip id (if None, all ips are deleted)
|
439 |
|
440 |
:returns: (dict) request headers
|
441 |
|
442 |
:raises AssertionError: if fip_id is emtpy
|
443 |
"""
|
444 |
assert fip_id, 'floating ip id is needed for delete_floating_ip' |
445 |
r = self.floating_ips_delete(fip_id)
|
446 |
return r.headers
|
447 |
|
448 |
def attach_floating_ip(self, server_id, address): |
449 |
"""Associate the address ip to server with server_id
|
450 |
|
451 |
:param server_id: (int)
|
452 |
|
453 |
:param address: (str) the ip address to assign to server (vm)
|
454 |
|
455 |
:returns: (dict) request headers
|
456 |
|
457 |
:raises ValueError: if server_id cannot be converted to int
|
458 |
|
459 |
:raises ValueError: if server_id is not of a int-convertable type
|
460 |
|
461 |
:raises AssertionError: if address is emtpy
|
462 |
"""
|
463 |
server_id = int(server_id)
|
464 |
assert address, 'address is needed for attach_floating_ip' |
465 |
req = dict(addFloatingIp=dict(address=address)) |
466 |
r = self.servers_action_post(server_id, json_data=req)
|
467 |
return r.headers
|
468 |
|
469 |
def detach_floating_ip(self, server_id, address): |
470 |
"""Disassociate an address ip from the server with server_id
|
471 |
|
472 |
:param server_id: (int)
|
473 |
|
474 |
:param address: (str) the ip address to assign to server (vm)
|
475 |
|
476 |
:returns: (dict) request headers
|
477 |
|
478 |
:raises ValueError: if server_id cannot be converted to int
|
479 |
|
480 |
:raises ValueError: if server_id is not of a int-convertable type
|
481 |
|
482 |
:raises AssertionError: if address is emtpy
|
483 |
"""
|
484 |
server_id = int(server_id)
|
485 |
assert address, 'address is needed for detach_floating_ip' |
486 |
req = dict(removeFloatingIp=dict(address=address)) |
487 |
r = self.servers_action_post(server_id, json_data=req)
|
488 |
return r.headers
|