Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / cyclades / __init__.py @ 7b2e4bf1

History | View | Annotate | Download (14.6 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(
261
            self, item_id, current_status, get_status,
262
            delay=1, max_wait=100, wait_cb=None):
263
        """Wait for item while its status is current_status
264

265
        :param server_id: integer (str or int)
266

267
        :param current_status: (str)
268

269
        :param get_status: (method(self, item_id)) if called, returns
270
            (status, progress %) If no way to tell progress, return None
271

272
        :param delay: time interval between retries
273

274
        :param wait_cb: if set a progress bar is used to show progress
275

276
        :returns: (str) the new mode if successful, (bool) False if timed out
277
        """
278
        status, progress = get_status(self, item_id)
279
        if status != current_status:
280
            return status
281
        old_wait = total_wait = 0
282

    
283
        if wait_cb:
284
            wait_gen = wait_cb(1 + max_wait // delay)
285
            wait_gen.next()
286

    
287
        while status == current_status and total_wait <= max_wait:
288
            if wait_cb:
289
                try:
290
                    for i in range(total_wait - old_wait):
291
                        wait_gen.next()
292
                except Exception:
293
                    break
294
            else:
295
                stdout.write('.')
296
                stdout.flush()
297
            old_wait = total_wait
298
            total_wait = progress or (total_wait + 1)
299
            sleep(delay)
300
            status, progress = get_status(self, item_id)
301

    
302
        if total_wait < max_wait:
303
            if wait_cb:
304
                try:
305
                    for i in range(max_wait):
306
                        wait_gen.next()
307
                except:
308
                    pass
309
        return status if status != current_status else False
310

    
311
    def wait_server(
312
            self, server_id,
313
            current_status='BUILD',
314
            delay=1, max_wait=100, wait_cb=None):
315
        """Wait for server while its status is current_status
316

317
        :param server_id: integer (str or int)
318

319
        :param current_status: (str) BUILD|ACTIVE|STOPPED|DELETED|REBOOT
320

321
        :param delay: time interval between retries
322

323
        :param wait_cb: if set a progressbar is used to show progress
324

325
        :returns: (str) the new mode if succesfull, (bool) False if timed out
326
        """
327

    
328
        def get_status(self, server_id):
329
            r = self.get_server_details(server_id)
330
            return r['status'], (r.get('progress', None) if (
331
                            current_status in ('BUILD', )) else None)
332

    
333
        return self._wait(
334
            server_id, current_status, get_status, delay, max_wait, wait_cb)
335

    
336
    def wait_network(
337
            self, net_id,
338
            current_status='LALA', delay=1, max_wait=100, wait_cb=None):
339
        """Wait for network while its status is current_status
340

341
        :param net_id: integer (str or int)
342

343
        :param current_status: (str) PENDING | ACTIVE | DELETED
344

345
        :param delay: time interval between retries
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, net_id):
353
            r = self.get_network_details(net_id)
354
            return r['status'], None
355

    
356
        return self._wait(
357
            net_id, current_status, get_status, delay, max_wait, wait_cb)
358

    
359
    def get_floating_ip_pools(self):
360
        """
361
        :returns: (dict) {floating_ip_pools:[{name: ...}, ...]}
362
        """
363
        r = self.floating_ip_pools_get()
364
        return r.json
365

    
366
    def get_floating_ips(self):
367
        """
368
        :returns: (dict) {floating_ips:[
369
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
370
            ... ]}
371
        """
372
        r = self.floating_ips_get()
373
        return r.json
374

    
375
    def alloc_floating_ip(self, pool=None, address=None):
376
        """
377
        :param pool: (str) pool of ips to allocate from
378

379
        :param address: (str) ip address to request
380

381
        :returns: (dict) {
382
                fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...
383
            }
384
        """
385
        json_data = dict()
386
        if pool:
387
            json_data['pool'] = pool
388
            if address:
389
                json_data['address'] = address
390
        r = self.floating_ips_post(json_data)
391
        return r.json['floating_ip']
392

    
393
    def get_floating_ip(self, fip_id):
394
        """
395
        :param fip_id: (str) floating ip id
396

397
        :returns: (dict)
398
            {fixed_ip: ..., id: ..., instance_id: ..., ip: ..., pool: ...},
399

400
        :raises AssertionError: if fip_id is emtpy
401
        """
402
        assert fip_id, 'floating ip id is needed for get_floating_ip'
403
        r = self.floating_ips_get(fip_id)
404
        return r.json['floating_ip']
405

    
406
    def delete_floating_ip(self, fip_id=None):
407
        """
408
        :param fip_id: (str) floating ip id (if None, all ips are deleted)
409

410
        :returns: (dict) request headers
411

412
        :raises AssertionError: if fip_id is emtpy
413
        """
414
        assert fip_id, 'floating ip id is needed for delete_floating_ip'
415
        r = self.floating_ips_delete(fip_id)
416
        return r.headers
417

    
418
    def attach_floating_ip(self, server_id, address):
419
        """Associate the address ip to server with server_id
420

421
        :param server_id: (int)
422

423
        :param address: (str) the ip address to assign to server (vm)
424

425
        :returns: (dict) request headers
426

427
        :raises ValueError: if server_id cannot be converted to int
428

429
        :raises ValueError: if server_id is not of a int-convertable type
430

431
        :raises AssertionError: if address is emtpy
432
        """
433
        server_id = int(server_id)
434
        assert address, 'address is needed for attach_floating_ip'
435
        r = self.servers_post(
436
            server_id, 'action',
437
            json_data=dict(addFloatingIp=dict(address=address)))
438
        return r.headers
439

    
440
    def detach_floating_ip(self, server_id, address):
441
        """Disassociate an address ip from the server with server_id
442

443
        :param server_id: (int)
444

445
        :param address: (str) the ip address to assign to server (vm)
446

447
        :returns: (dict) request headers
448

449
        :raises ValueError: if server_id cannot be converted to int
450

451
        :raises ValueError: if server_id is not of a int-convertable type
452

453
        :raises AssertionError: if address is emtpy
454
        """
455
        server_id = int(server_id)
456
        assert address, 'address is needed for detach_floating_ip'
457
        r = self.servers_post(
458
            server_id, 'action',
459
            json_data=dict(removeFloatingIp=dict(address=address)))
460
        return r.headers