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