Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / cyclades / __init__.py @ c788a761

History | View | Annotate | Download (16 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 virt. server
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 virtual server upon creation
59

60
        :returns: a dict with the new virtual server 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 virtual server
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_server_nics(self, server_id):
138
        """
139
        :param server_id: integer (str or int)
140

141
        :returns: (dict) network interface connections
142
        """
143
        r = self.servers_ips_get(server_id)
144
        return r.json['attachments']
145

    
146
    def get_server_stats(self, server_id):
147
        """
148
        :param server_id: integer (str or int)
149

150
        :returns: (dict) auto-generated graphs of statistics (urls)
151
        """
152
        r = self.servers_stats_get(server_id)
153
        return r.json['stats']
154

    
155
    def list_networks(self, detail=False):
156
        """
157
        :param detail: (bool)
158

159
        :returns: (list) id,name if not detail else full info per network
160
        """
161
        detail = 'detail' if detail else ''
162
        r = self.networks_get(command=detail)
163
        return r.json['networks']
164

    
165
    def list_network_nics(self, network_id):
166
        """
167
        :param network_id: integer (str or int)
168

169
        :returns: (list)
170
        """
171
        r = self.networks_get(network_id=network_id)
172
        return r.json['network']['attachments']
173

    
174
    def create_network(
175
            self, name,
176
            cidr=None, gateway=None, type=None, dhcp=False):
177
        """
178
        :param name: (str)
179

180
        :param cidr: (str)
181

182
        :param geteway: (str)
183

184
        :param type: (str) if None, will use MAC_FILTERED as default
185
            Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
186

187
        :param dhcp: (bool)
188

189
        :returns: (dict) network detailed info
190
        """
191
        net = dict(name=name)
192
        if cidr:
193
            net['cidr'] = cidr
194
        if gateway:
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']
201

    
202
    def get_network_details(self, network_id):
203
        """
204
        :param network_id: integer (str or int)
205

206
        :returns: (dict)
207
        """
208
        r = self.networks_get(network_id=network_id)
209
        return r.json['network']
210

    
211
    def update_network_name(self, network_id, new_name):
212
        """
213
        :param network_id: integer (str or int)
214

215
        :param new_name: (str)
216

217
        :returns: (dict) response headers
218
        """
219
        req = {'network': {'name': new_name}}
220
        r = self.networks_put(network_id=network_id, json_data=req)
221
        return r.headers
222

    
223
    def delete_network(self, network_id):
224
        """
225
        :param network_id: integer (str or int)
226

227
        :returns: (dict) response headers
228

229
        :raises ClientError: 421 Network in use
230
        """
231
        try:
232
            r = self.networks_delete(network_id)
233
            return r.headers
234
        except ClientError as err:
235
            if err.status == 421:
236
                err.details = [
237
                    'Network may be still connected to at least one server']
238
            raise
239

    
240
    def connect_server(self, server_id, network_id):
241
        """ Connect a server to a network
242

243
        :param server_id: integer (str or int)
244

245
        :param network_id: integer (str or int)
246

247
        :returns: (dict) response headers
248
        """
249
        req = {'add': {'serverRef': server_id}}
250
        r = self.networks_post(network_id, 'action', json_data=req)
251
        return r.headers
252

    
253
    def disconnect_server(self, server_id, nic_id):
254
        """
255
        :param server_id: integer (str or int)
256

257
        :param nic_id: (str)
258

259
        :returns: (int) the number of nics disconnected
260
        """
261
        vm_nets = self.list_server_nics(server_id)
262
        num_of_disconnections = 0
263
        for (nic_id, network_id) in [(
264
                net['id'],
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
270

    
271
    def disconnect_network_nics(self, netid):
272
        """
273
        :param netid: integer (str or int)
274
        """
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)
278

    
279
    def _wait(
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
283

284
        :param server_id: integer (str or int)
285

286
        :param current_status: (str)
287

288
        :param get_status: (method(self, item_id)) if called, returns
289
            (status, progress %) If no way to tell progress, return None
290

291
        :param delay: time interval between retries
292

293
        :param wait_cb: if set a progress bar is used to show progress
294

295
        :returns: (str) the new mode if successful, (bool) False if timed out
296
        """
297
        status, progress = get_status(self, item_id)
298

    
299
        if wait_cb:
300
            wait_gen = wait_cb(max_wait // delay)
301
            wait_gen.next()
302

    
303
        if status != current_status:
304
            if wait_cb:
305
                try:
306
                    wait_gen.next()
307
                except Exception:
308
                    pass
309
            return status
310
        old_wait = total_wait = 0
311

    
312
        while status == current_status and total_wait <= max_wait:
313
            if wait_cb:
314
                try:
315
                    for i in range(total_wait - old_wait):
316
                        wait_gen.next()
317
                except Exception:
318
                    break
319
            old_wait = total_wait
320
            total_wait = progress or total_wait + 1
321
            sleep(delay)
322
            status, progress = get_status(self, item_id)
323

    
324
        if total_wait < max_wait:
325
            if wait_cb:
326
                try:
327
                    for i in range(max_wait):
328
                        wait_gen.next()
329
                except:
330
                    pass
331
        return status if status != current_status else False
332

    
333
    def wait_server(
334
            self, server_id,
335
            current_status='BUILD',
336
            delay=1, max_wait=100, wait_cb=None):
337
        """Wait for server while its status is current_status
338

339
        :param server_id: integer (str or int)
340

341
        :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
342

343
        :param delay: time interval between retries
344

345
        :max_wait: (int) timeout in secconds
346

347
        :param wait_cb: if set a progressbar is used to show progress
348

349
        :returns: (str) the new mode if succesfull, (bool) False if timed out
350
        """
351

    
352
        def get_status(self, server_id):
353
            r = self.get_server_details(server_id)
354
            return r['status'], (r.get('progress', None) if (
355
                            current_status in ('BUILD', )) else None)
356

    
357
        return self._wait(
358
            server_id, current_status, get_status, delay, max_wait, wait_cb)
359

    
360
    def wait_network(
361
            self, net_id,
362
            current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
363
        """Wait for network while its status is current_status
364

365
        :param net_id: integer (str or int)
366

367
        :param current_status: (str) PENDING | ACTIVE | DELETED
368

369
        :param delay: time interval between retries
370

371
        :max_wait: (int) timeout in secconds
372

373
        :param wait_cb: if set a progressbar is used to show progress
374

375
        :returns: (str) the new mode if succesfull, (bool) False if timed out
376
        """
377

    
378
        def get_status(self, net_id):
379
            r = self.get_network_details(net_id)
380
            return r['status'], None
381

    
382
        return self._wait(
383
            net_id, current_status, get_status, delay, max_wait, wait_cb)
384

    
385
    def wait_firewall(
386
            self, server_id,
387
            current_status='DISABLED', delay=1, max_wait=100, wait_cb=None):
388
        """Wait while the public network firewall status is current_status
389

390
        :param server_id: integer (str or int)
391

392
        :param current_status: (str) DISABLED | ENABLED | PROTECTED
393

394
        :param delay: time interval between retries
395

396
        :max_wait: (int) timeout in secconds
397

398
        :param wait_cb: if set a progressbar is used to show progress
399

400
        :returns: (str) the new mode if succesfull, (bool) False if timed out
401
        """
402

    
403
        def get_status(self, server_id):
404
            return self.get_firewall_profile(server_id), None
405

    
406
        return self._wait(
407
            server_id, current_status, get_status, delay, max_wait, wait_cb)
408

    
409
    def get_floating_ip_pools(self):
410
        """
411
        :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
412
        """
413
        r = self.floating_ip_pools_get()
414
        return r.json
415

    
416
    def get_floating_ips(self):
417
        """
418
        :returns: (dict) {floating_ips: [fixed_ip: , id: , ip: , pool: ]}
419
        """
420
        r = self.floating_ips_get()
421
        return r.json
422

    
423
    def alloc_floating_ip(self, pool=None, address=None):
424
        """
425
        :param pool: (str) pool of ips to allocate from
426

427
        :param address: (str) ip address to request
428

429
        :returns: (dict) {
430
            fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...}
431
        """
432
        json_data = dict()
433
        if pool:
434
            json_data['pool'] = pool
435
        if address:
436
            json_data['address'] = address
437
        r = self.floating_ips_post(json_data)
438
        return r.json['floating_ip']
439

    
440
    def get_floating_ip(self, fip_id):
441
        """
442
        :param fip_id: (str) floating ip id
443

444
        :returns: (dict)
445
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
446

447
        :raises AssertionError: if fip_id is emtpy
448
        """
449
        assert fip_id, 'floating ip id is needed for get_floating_ip'
450
        r = self.floating_ips_get(fip_id)
451
        return r.json['floating_ip']
452

    
453
    def delete_floating_ip(self, fip_id=None):
454
        """
455
        :param fip_id: (str) floating ip id (if None, all ips are deleted)
456

457
        :returns: (dict) request headers
458

459
        :raises AssertionError: if fip_id is emtpy
460
        """
461
        assert fip_id, 'floating ip id is needed for delete_floating_ip'
462
        r = self.floating_ips_delete(fip_id)
463
        return r.headers
464

    
465
    def attach_floating_ip(self, server_id, address):
466
        """Associate the address ip to server with server_id
467

468
        :param server_id: (int)
469

470
        :param address: (str) the ip address to assign to server (vm)
471

472
        :returns: (dict) request headers
473

474
        :raises ValueError: if server_id cannot be converted to int
475

476
        :raises ValueError: if server_id is not of a int-convertable type
477

478
        :raises AssertionError: if address is emtpy
479
        """
480
        server_id = int(server_id)
481
        assert address, 'address is needed for attach_floating_ip'
482
        req = dict(addFloatingIp=dict(address=address))
483
        r = self.servers_action_post(server_id, json_data=req)
484
        return r.headers
485

    
486
    def detach_floating_ip(self, server_id, address):
487
        """Disassociate an address ip from the server with server_id
488

489
        :param server_id: (int)
490

491
        :param address: (str) the ip address to assign to server (vm)
492

493
        :returns: (dict) request headers
494

495
        :raises ValueError: if server_id cannot be converted to int
496

497
        :raises ValueError: if server_id is not of a int-convertable type
498

499
        :raises AssertionError: if address is emtpy
500
        """
501
        server_id = int(server_id)
502
        assert address, 'address is needed for detach_floating_ip'
503
        req = dict(removeFloatingIp=dict(address=address))
504
        r = self.servers_action_post(server_id, json_data=req)
505
        return r.headers