1 # Copyright 2011-2013 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
34 from sys import stdout
35 from time import sleep
37 from kamaki.clients.cyclades.rest_api import CycladesRestClient
38 from kamaki.clients import ClientError
41 class CycladesClient(CycladesRestClient):
42 """Synnefo Cyclades Compute API client"""
45 self, name, flavor_id, image_id,
46 metadata=None, personality=None):
47 """Submit request to create a new server
51 :param flavor_id: integer id denoting a preset hardware configuration
53 :param image_id: (str) id denoting the OS image to run on the VM
55 :param metadata: (dict) vm metadata updated by os/users image metadata
57 :param personality: a list of (file path, file contents) tuples,
58 describing files to be injected into VM upon creation.
60 :returns: a dict with the new VMs details
62 :raises ClientError: wraps request errors
64 image = self.get_image_details(image_id)
65 metadata = metadata or dict()
66 for key in ('os', 'users'):
68 metadata[key] = image['metadata'][key]
72 return super(CycladesClient, self).create_server(
73 name, flavor_id, image_id,
74 metadata=metadata, personality=personality)
76 def start_server(self, server_id):
77 """Submit a startup request
79 :param server_id: integer (str or int)
81 :returns: (dict) response headers
84 r = self.servers_action_post(server_id, json_data=req, success=202)
87 def shutdown_server(self, server_id):
88 """Submit a shutdown request
90 :param server_id: integer (str or int)
92 :returns: (dict) response headers
94 req = {'shutdown': {}}
95 r = self.servers_action_post(server_id, json_data=req, success=202)
98 def get_server_console(self, server_id):
100 :param server_id: integer (str or int)
102 :returns: (dict) info to set a VNC connection to VM
104 req = {'console': {'type': 'vnc'}}
105 r = self.servers_action_post(server_id, json_data=req, success=200)
106 return r.json['console']
108 def get_firewall_profile(self, server_id):
110 :param server_id: integer (str or int)
112 :returns: (str) ENABLED | DISABLED | PROTECTED
114 :raises ClientError: 520 No Firewall Profile
116 r = self.get_server_details(server_id)
118 return r['attachments'][0]['firewallProfile']
121 'No Firewall Profile',
122 details='Server %s is missing a firewall profile' % server_id)
124 def set_firewall_profile(self, server_id, profile):
125 """Set the firewall profile for the public interface of a server
127 :param server_id: integer (str or int)
129 :param profile: (str) ENABLED | DISABLED | PROTECTED
131 :returns: (dict) response headers
133 req = {'firewallProfile': {'profile': profile}}
134 r = self.servers_action_post(server_id, json_data=req, success=202)
137 def list_server_nics(self, server_id):
139 :param server_id: integer (str or int)
141 :returns: (dict) network interface connections
143 r = self.servers_ips_get(server_id)
144 return r.json['attachments']
146 def get_server_stats(self, server_id):
148 :param server_id: integer (str or int)
150 :returns: (dict) auto-generated graphs of statistics (urls)
152 r = self.servers_stats_get(server_id)
153 return r.json['stats']
155 def list_networks(self, detail=False):
157 :param detail: (bool)
159 :returns: (list) id,name if not detail else full info per network
161 detail = 'detail' if detail else ''
162 r = self.networks_get(command=detail)
163 return r.json['networks']
165 def list_network_nics(self, network_id):
167 :param network_id: integer (str or int)
171 r = self.networks_get(network_id=network_id)
172 return r.json['network']['attachments']
176 cidr=None, gateway=None, type=None, dhcp=False):
182 :param geteway: (str)
184 :param type: (str) if None, will use MAC_FILTERED as default
185 Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
189 :returns: (dict) network detailed info
191 net = dict(name=name)
195 net['gateway'] = gateway
196 net['type'] = type or 'MAC_FILTERED'
197 net['dhcp'] = True if dhcp else False
198 req = dict(network=net)
199 r = self.networks_post(json_data=req, success=202)
200 return r.json['network']
202 def get_network_details(self, network_id):
204 :param network_id: integer (str or int)
208 r = self.networks_get(network_id=network_id)
209 return r.json['network']
211 def update_network_name(self, network_id, new_name):
213 :param network_id: integer (str or int)
215 :param new_name: (str)
217 :returns: (dict) response headers
219 req = {'network': {'name': new_name}}
220 r = self.networks_put(network_id=network_id, json_data=req)
223 def delete_network(self, network_id):
225 :param network_id: integer (str or int)
227 :returns: (dict) response headers
229 :raises ClientError: 421 Network in use
232 r = self.networks_delete(network_id)
234 except ClientError as err:
235 if err.status == 421:
237 'Network may be still connected to at least one server']
240 def connect_server(self, server_id, network_id):
241 """ Connect a server to a network
243 :param server_id: integer (str or int)
245 :param network_id: integer (str or int)
247 :returns: (dict) response headers
249 req = {'add': {'serverRef': server_id}}
250 r = self.networks_post(network_id, 'action', json_data=req)
253 def disconnect_server(self, server_id, nic_id):
255 :param server_id: integer (str or int)
259 :returns: (int) the number of nics disconnected
261 vm_nets = self.list_server_nics(server_id)
262 num_of_disconnections = 0
263 for (nic_id, network_id) in [(
265 net['network_id']) for net in vm_nets if nic_id == net['id']]:
266 req = {'remove': {'attachment': '%s' % nic_id}}
267 self.networks_post(network_id, 'action', json_data=req)
268 num_of_disconnections += 1
269 return num_of_disconnections
271 def disconnect_network_nics(self, netid):
273 :param netid: integer (str or int)
275 for nic in self.list_network_nics(netid):
276 req = dict(remove=dict(attachment=nic))
277 self.networks_post(netid, 'action', json_data=req)
280 self, item_id, current_status, get_status,
281 delay=1, max_wait=100, wait_cb=None):
282 """Wait for item while its status is current_status
284 :param server_id: integer (str or int)
286 :param current_status: (str)
288 :param get_status: (method(self, item_id)) if called, returns
289 (status, progress %) If no way to tell progress, return None
291 :param delay: time interval between retries
293 :param wait_cb: if set a progress bar is used to show progress
295 :returns: (str) the new mode if successful, (bool) False if timed out
297 status, progress = get_status(self, item_id)
298 if status != current_status:
300 old_wait = total_wait = 0
303 wait_gen = wait_cb(1 + max_wait // delay)
306 while status == current_status and total_wait <= max_wait:
309 for i in range(total_wait - old_wait):
316 old_wait = total_wait
317 total_wait = progress or (total_wait + 1)
319 status, progress = get_status(self, item_id)
321 if total_wait < max_wait:
324 for i in range(max_wait):
328 return status if status != current_status else False
332 current_status='BUILD',
333 delay=1, max_wait=100, wait_cb=None):
334 """Wait for server while its status is current_status
336 :param server_id: integer (str or int)
338 :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
340 :param delay: time interval between retries
342 :param wait_cb: if set a progressbar is used to show progress
344 :returns: (str) the new mode if succesfull, (bool) False if timed out
347 def get_status(self, server_id):
348 r = self.get_server_details(server_id)
349 return r['status'], (r.get('progress', None) if (
350 current_status in ('BUILD', )) else None)
353 server_id, current_status, get_status, delay, max_wait, wait_cb)
357 current_status='LALA', delay=1, max_wait=100, wait_cb=None):
358 """Wait for network while its status is current_status
360 :param net_id: integer (str or int)
362 :param current_status: (str) PENDING | ACTIVE | DELETED
364 :param delay: time interval between retries
366 :param wait_cb: if set a progressbar is used to show progress
368 :returns: (str) the new mode if succesfull, (bool) False if timed out
371 def get_status(self, net_id):
372 r = self.get_network_details(net_id)
373 return r['status'], None
376 net_id, current_status, get_status, delay, max_wait, wait_cb)
378 def get_floating_ip_pools(self):
380 :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
382 r = self.floating_ip_pools_get()
385 def get_floating_ips(self):
387 :returns: (dict) {floating_ips:[
388 {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
391 r = self.floating_ips_get()
394 def alloc_floating_ip(self, pool=None, address=None):
396 :param pool: (str) pool of ips to allocate from
398 :param address: (str) ip address to request
401 fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...}
405 json_data['pool'] = pool
407 json_data['address'] = address
408 r = self.floating_ips_post(json_data)
409 return r.json['floating_ip']
411 def get_floating_ip(self, fip_id):
413 :param fip_id: (str) floating ip id
416 {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
418 :raises AssertionError: if fip_id is emtpy
420 assert fip_id, 'floating ip id is needed for get_floating_ip'
421 r = self.floating_ips_get(fip_id)
422 return r.json['floating_ip']
424 def delete_floating_ip(self, fip_id=None):
426 :param fip_id: (str) floating ip id (if None, all ips are deleted)
428 :returns: (dict) request headers
430 :raises AssertionError: if fip_id is emtpy
432 assert fip_id, 'floating ip id is needed for delete_floating_ip'
433 r = self.floating_ips_delete(fip_id)
436 def attach_floating_ip(self, server_id, address):
437 """Associate the address ip to server with server_id
439 :param server_id: (int)
441 :param address: (str) the ip address to assign to server (vm)
443 :returns: (dict) request headers
445 :raises ValueError: if server_id cannot be converted to int
447 :raises ValueError: if server_id is not of a int-convertable type
449 :raises AssertionError: if address is emtpy
451 server_id = int(server_id)
452 assert address, 'address is needed for attach_floating_ip'
453 req = dict(addFloatingIp=dict(address=address))
454 r = self.servers_action_post(server_id, json_data=req)
457 def detach_floating_ip(self, server_id, address):
458 """Disassociate an address ip from the server with server_id
460 :param server_id: (int)
462 :param address: (str) the ip address to assign to server (vm)
464 :returns: (dict) request headers
466 :raises ValueError: if server_id cannot be converted to int
468 :raises ValueError: if server_id is not of a int-convertable type
470 :raises AssertionError: if address is emtpy
472 server_id = int(server_id)
473 assert address, 'address is needed for detach_floating_ip'
474 req = dict(removeFloatingIp=dict(address=address))
475 r = self.servers_action_post(server_id, json_data=req)