__init__.py 14.7 KB
Newer Older
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
Giorgos Verigakis's avatar
Giorgos Verigakis committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
#   1. Redistributions of source code must retain the above
#      copyright notice, this list of conditions and the following
#      disclaimer.
#
#   2. Redistributions in binary form must reproduce the above
#      copyright notice, this list of conditions and the following
#      disclaimer in the documentation and/or other materials
#      provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.

34
35
from time import sleep

36
from kamaki.clients.cyclades.rest_api import CycladesRestClient
37
from kamaki.clients.network import NetworkClient
38
from kamaki.clients.utils import path4url
39
from kamaki.clients import ClientError
Giorgos Verigakis's avatar
Giorgos Verigakis committed
40

41

42
class CycladesClient(CycladesRestClient):
43
    """Synnefo Cyclades Compute API client"""
44

45
46
    def create_server(
            self, name, flavor_id, image_id,
47
            metadata=None, personality=None, networks=None):
48
49
50
51
52
53
        """Submit request to create a new server

        :param name: (str)

        :param flavor_id: integer id denoting a preset hardware configuration

54
        :param image_id: (str) id denoting the OS image to run on virt. server
55
56
57
58

        :param metadata: (dict) vm metadata updated by os/users image metadata

        :param personality: a list of (file path, file contents) tuples,
59
            describing files to be injected into virtual server upon creation
60

61
62
63
64
65
66
        :param networks: (list of dicts) Networks to connect to, list this:
            "networks": [
                {"network": <network_uuid>},
                {"network": <network_uuid>, "fixed_ip": address},
                {"port": <port_id>}, ...]

67
        :returns: a dict with the new virtual server details
68
69
70
71
72
73
74
75
76
77
78

        :raises ClientError: wraps request errors
        """
        image = self.get_image_details(image_id)
        metadata = metadata or dict()
        for key in ('os', 'users'):
            try:
                metadata[key] = image['metadata'][key]
            except KeyError:
                pass

79
80
81
        return super(CycladesClient, self).create_server(
            name, flavor_id, image_id,
            metadata=metadata, personality=personality)
82

Giorgos Verigakis's avatar
Giorgos Verigakis committed
83
    def start_server(self, server_id):
84
85
86
        """Submit a startup request

        :param server_id: integer (str or int)
87
88

        :returns: (dict) response headers
89
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
90
        req = {'start': {}}
91
        r = self.servers_action_post(server_id, json_data=req, success=202)
92
        return r.headers
93

Giorgos Verigakis's avatar
Giorgos Verigakis committed
94
    def shutdown_server(self, server_id):
95
96
97
        """Submit a shutdown request

        :param server_id: integer (str or int)
98
99

        :returns: (dict) response headers
100
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
101
        req = {'shutdown': {}}
102
        r = self.servers_action_post(server_id, json_data=req, success=202)
103
        return r.headers
104

Giorgos Verigakis's avatar
Giorgos Verigakis committed
105
    def get_server_console(self, server_id):
106
107
108
        """
        :param server_id: integer (str or int)

109
        :returns: (dict) info to set a VNC connection to virtual server
110
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
111
        req = {'console': {'type': 'vnc'}}
112
        r = self.servers_action_post(server_id, json_data=req, success=200)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
113
        return r.json['console']
114
115

    def get_firewall_profile(self, server_id):
116
117
118
119
120
121
122
        """
        :param server_id: integer (str or int)

        :returns: (str) ENABLED | DISABLED | PROTECTED

        :raises ClientError: 520 No Firewall Profile
        """
123
124
        r = self.get_server_details(server_id)
        try:
125
            return r['attachments'][0]['firewallProfile']
126
        except KeyError:
127
            raise ClientError(
128
                'No Firewall Profile',
129
                details='Server %s is missing a firewall profile' % server_id)
130

Giorgos Verigakis's avatar
Giorgos Verigakis committed
131
132
    def set_firewall_profile(self, server_id, profile):
        """Set the firewall profile for the public interface of a server
133
134
135
136

        :param server_id: integer (str or int)

        :param profile: (str) ENABLED | DISABLED | PROTECTED
137
138

        :returns: (dict) response headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
139
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
140
        req = {'firewallProfile': {'profile': profile}}
141
        r = self.servers_action_post(server_id, json_data=req, success=202)
142
        return r.headers
143

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

        :returns: (dict) network interface connections
        """
150
        r = self.servers_ips_get(server_id)
151
        return r.json['attachments']
152

Giorgos Verigakis's avatar
Giorgos Verigakis committed
153
    def get_server_stats(self, server_id):
154
155
156
157
158
        """
        :param server_id: integer (str or int)

        :returns: (dict) auto-generated graphs of statistics (urls)
        """
159
        r = self.servers_stats_get(server_id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
160
        return r.json['stats']
161

Giorgos Verigakis's avatar
Giorgos Verigakis committed
162
    def list_networks(self, detail=False):
163
164
165
166
167
        """
        :param detail: (bool)

        :returns: (list) id,name if not detail else full info per network
        """
168
169
        detail = 'detail' if detail else ''
        r = self.networks_get(command=detail)
170
        return r.json['networks']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
171

172
    def list_network_nics(self, network_id):
173
174
175
176
177
        """
        :param network_id: integer (str or int)

        :returns: (list)
        """
178
        r = self.networks_get(network_id=network_id)
179
        return r.json['network']['attachments']
180

181
182
    def create_network(
            self, name,
183
            cidr=None, gateway=None, type=None, dhcp=False):
184
185
186
187
188
189
190
        """
        :param name: (str)

        :param cidr: (str)

        :param geteway: (str)

191
192
        :param type: (str) if None, will use MAC_FILTERED as default
            Valid values: CUSTOM, IP_LESS_ROUTED, MAC_FILTERED, PHYSICAL_VLAN
193

194
        :param dhcp: (bool)
195
196

        :returns: (dict) network detailed info
197
198
199
        """
        net = dict(name=name)
        if cidr:
200
            net['cidr'] = cidr
201
        if gateway:
202
            net['gateway'] = gateway
203
        net['type'] = type or 'MAC_FILTERED'
204
        net['dhcp'] = True if dhcp else False
205
        req = dict(network=net)
206
        r = self.networks_post(json_data=req, success=202)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
207
        return r.json['network']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
208
209

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

        :returns: (dict)
        """
215
        r = self.networks_get(network_id=network_id)
Giorgos Verigakis's avatar
Giorgos Verigakis committed
216
        return r.json['network']
Giorgos Verigakis's avatar
Giorgos Verigakis committed
217
218

    def update_network_name(self, network_id, new_name):
219
220
221
222
        """
        :param network_id: integer (str or int)

        :param new_name: (str)
223
224

        :returns: (dict) response headers
225
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
226
        req = {'network': {'name': new_name}}
227
228
        r = self.networks_put(network_id=network_id, json_data=req)
        return r.headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
229
230

    def delete_network(self, network_id):
231
232
233
        """
        :param network_id: integer (str or int)

234
235
        :returns: (dict) response headers

236
237
        :raises ClientError: 421 Network in use
        """
238
        try:
239
240
            r = self.networks_delete(network_id)
            return r.headers
241
242
        except ClientError as err:
            if err.status == 421:
243
                err.details = [
244
                    'Network may be still connected to at least one server']
245
            raise
Giorgos Verigakis's avatar
Giorgos Verigakis committed
246
247

    def connect_server(self, server_id, network_id):
248
249
250
251
252
        """ Connect a server to a network

        :param server_id: integer (str or int)

        :param network_id: integer (str or int)
253
254

        :returns: (dict) response headers
255
        """
Giorgos Verigakis's avatar
Giorgos Verigakis committed
256
        req = {'add': {'serverRef': server_id}}
257
258
        r = self.networks_post(network_id, 'action', json_data=req)
        return r.headers
Giorgos Verigakis's avatar
Giorgos Verigakis committed
259

260
    def disconnect_server(self, server_id, nic_id):
261
262
263
264
        """
        :param server_id: integer (str or int)

        :param nic_id: (str)
265
266

        :returns: (int) the number of nics disconnected
267
        """
268
        vm_nets = self.list_server_nics(server_id)
269
        num_of_disconnections = 0
270
        for (nic_id, network_id) in [(
271
272
                net['id'],
                net['network_id']) for net in vm_nets if nic_id == net['id']]:
273
            req = {'remove': {'attachment': '%s' % nic_id}}
274
            self.networks_post(network_id, 'action', json_data=req)
275
276
            num_of_disconnections += 1
        return num_of_disconnections
277
278

    def disconnect_network_nics(self, netid):
279
280
281
282
        """
        :param netid: integer (str or int)
        """
        for nic in self.list_network_nics(netid):
283
            req = dict(remove=dict(attachment=nic))
284
            self.networks_post(netid, 'action', json_data=req)
285

286
287
288
289
    def _wait(
            self, item_id, current_status, get_status,
            delay=1, max_wait=100, wait_cb=None):
        """Wait for item while its status is current_status
290
291
292

        :param server_id: integer (str or int)

293
294
295
296
        :param current_status: (str)

        :param get_status: (method(self, item_id)) if called, returns
            (status, progress %) If no way to tell progress, return None
297
298
299

        :param delay: time interval between retries

300
        :param wait_cb: if set a progress bar is used to show progress
301

302
        :returns: (str) the new mode if successful, (bool) False if timed out
303
        """
304
        status, progress = get_status(self, item_id)
305

306
        if wait_cb:
307
            wait_gen = wait_cb(max_wait // delay)
308
            wait_gen.next()
309

310
311
312
313
314
315
316
317
318
        if status != current_status:
            if wait_cb:
                try:
                    wait_gen.next()
                except Exception:
                    pass
            return status
        old_wait = total_wait = 0

319
320
321
322
        while status == current_status and total_wait <= max_wait:
            if wait_cb:
                try:
                    for i in range(total_wait - old_wait):
323
                        wait_gen.next()
324
325
326
                except Exception:
                    break
            old_wait = total_wait
327
            total_wait = progress or total_wait + 1
328
            sleep(delay)
329
            status, progress = get_status(self, item_id)
330

331
        if total_wait < max_wait:
332
333
            if wait_cb:
                try:
334
                    for i in range(max_wait):
335
336
337
                        wait_gen.next()
                except:
                    pass
338
339
340
341
342
343
344
345
346
347
348
349
350
351
        return status if status != current_status else False

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

        :param server_id: integer (str or int)

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

        :param delay: time interval between retries

352
353
        :max_wait: (int) timeout in secconds

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
        :param wait_cb: if set a progressbar is used to show progress

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

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

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

    def wait_network(
            self, net_id,
369
            current_status='PENDING', delay=1, max_wait=100, wait_cb=None):
370
371
372
373
374
375
376
377
        """Wait for network while its status is current_status

        :param net_id: integer (str or int)

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

        :param delay: time interval between retries

378
379
        :max_wait: (int) timeout in secconds

380
381
382
383
384
385
386
387
388
389
390
        :param wait_cb: if set a progressbar is used to show progress

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

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

        return self._wait(
            net_id, current_status, get_status, delay, max_wait, wait_cb)
391

392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
    def wait_firewall(
            self, server_id,
            current_status='DISABLED', delay=1, max_wait=100, wait_cb=None):
        """Wait while the public network firewall status is current_status

        :param server_id: integer (str or int)

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

        :param delay: time interval between retries

        :max_wait: (int) timeout in secconds

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

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

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

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

416
417
418
419
420
421
422

class CycladesNetworkClient(NetworkClient):
    """Cyclades Network API extentions"""

    network_types = (
        'CUSTOM', 'MAC_FILTERED', 'IP_LESS_ROUTED', 'PHYSICAL_VLAN')

423
424
425
426
427
    def list_networks(self, detail=None):
        path = path4url('networks', 'detail' if detail else '')
        r = self.get(path, success=200)
        return r.json['networks']

428
429
430
431
432
433
434
435
    def create_network(self, type, name=None, shared=None):
        req = dict(network=dict(type=type, admin_state_up=True))
        if name:
            req['network']['name'] = name
        if shared not in (None, ):
            req['network']['shared'] = bool(shared)
        r = self.networks_post(json_data=req, success=201)
        return r.json['network']
436

Stavros Sachtouris's avatar
Stavros Sachtouris committed
437
    def create_port(
438
439
            self, network_id, device_id,
            security_groups=None, name=None, fixed_ips=None):
440
441
442
        port = dict(network_id=network_id, device_id=device_id)
        if security_groups:
            port['security_groups'] = security_groups
Stavros Sachtouris's avatar
Stavros Sachtouris committed
443
444
        if name:
            port['name'] = name
445
446
        for fixed_ip in fixed_ips:
            diff = set(['subnet_id', 'ip_address']).difference(fixed_ip)
447
448
449
            if diff:
                raise ValueError(
                    'Invalid format for "fixed_ips", %s missing' % diff)
450
        if fixed_ips:
451
            port['fixed_ips'] = fixed_ips
452
453
        r = self.ports_post(json_data=dict(port=port), success=201)
        return r.json['port']