Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / commands / cyclades.py @ c75be81a

History | View | Annotate | Download (35.2 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
import cStringIO
34
import codecs
35
from base64 import b64encode
36
from os.path import exists, expanduser
37
from io import StringIO
38
from pydoc import pager
39
from json import dumps
40

    
41
from kamaki.cli import command
42
from kamaki.cli.command_tree import CommandTree
43
from kamaki.cli.utils import remove_from_items, filter_dicts_by_dict
44
from kamaki.cli.errors import (
45
    raiseCLIError, CLISyntaxError, CLIBaseUrlError, CLIInvalidArgument)
46
from kamaki.clients.cyclades import CycladesClient
47
from kamaki.cli.argument import (
48
    FlagArgument, ValueArgument, KeyValueArgument, RepeatableArgument,
49
    ProgressBarArgument, DateArgument, IntArgument, StatusArgument)
50
from kamaki.cli.commands import (
51
    _command_init, errors, addLogSettings, dataModification,
52
    _optional_output_cmd, _optional_json, _name_filter, _id_filter)
53

    
54

    
55
server_cmds = CommandTree('server', 'Cyclades/Compute API server commands')
56
flavor_cmds = CommandTree('flavor', 'Cyclades/Compute API flavor commands')
57
_commands = [server_cmds, flavor_cmds]
58

    
59

    
60
about_authentication = '\nUser Authentication:\
61
    \n* to check authentication: /user authenticate\
62
    \n* to set authentication token: /config set cloud.<cloud>.token <token>'
63

    
64
howto_personality = [
65
    'Defines a file to be injected to virtual servers file system.',
66
    'syntax:  PATH,[SERVER_PATH,[OWNER,[GROUP,[MODE]]]]',
67
    '  [local-path=]PATH: local file to be injected (relative or absolute)',
68
    '  [server-path=]SERVER_PATH: destination location inside server Image',
69
    '  [owner=]OWNER: virtual servers user id for the remote file',
70
    '  [group=]GROUP: virtual servers group id or name for the remote file',
71
    '  [mode=]MODE: permission in octal (e.g., 0777)',
72
    'e.g., -p /tmp/my.file,owner=root,mode=0777']
73

    
74
server_states = ('BUILD', 'ACTIVE', 'STOPPED', 'REBOOT')
75

    
76

    
77
class _service_wait(object):
78

    
79
    wait_arguments = dict(
80
        progress_bar=ProgressBarArgument(
81
            'do not show progress bar', ('-N', '--no-progress-bar'), False)
82
    )
83

    
84
    def _wait(
85
            self, service, service_id, status_method, current_status,
86
            countdown=True, timeout=60):
87
        (progress_bar, wait_cb) = self._safe_progress_bar(
88
            '%s %s: status is still %s' % (
89
                service, service_id, current_status),
90
            countdown=countdown, timeout=timeout)
91

    
92
        try:
93
            new_mode = status_method(
94
                service_id, current_status, max_wait=timeout, wait_cb=wait_cb)
95
            if new_mode:
96
                self.error('%s %s: status is now %s' % (
97
                    service, service_id, new_mode))
98
            else:
99
                self.error('%s %s: status is still %s' % (
100
                    service, service_id, current_status))
101
        except KeyboardInterrupt:
102
            self.error('\n- canceled')
103
        finally:
104
            self._safe_progress_bar_finish(progress_bar)
105

    
106

    
107
class _server_wait(_service_wait):
108

    
109
    def _wait(self, server_id, current_status, timeout=60):
110
        super(_server_wait, self)._wait(
111
            'Server', server_id, self.client.wait_server, current_status,
112
            countdown=(current_status not in ('BUILD', )),
113
            timeout=timeout if current_status not in ('BUILD', ) else 100)
114

    
115

    
116
class _init_cyclades(_command_init):
117
    @errors.generic.all
118
    @addLogSettings
119
    def _run(self, service='compute'):
120
        if getattr(self, 'cloud', None):
121
            base_url = self._custom_url(service) or self._custom_url(
122
                'cyclades')
123
            if base_url:
124
                token = self._custom_token(service) or self._custom_token(
125
                    'cyclades') or self.config.get_cloud('token')
126
                self.client = CycladesClient(base_url=base_url, token=token)
127
                return
128
        else:
129
            self.cloud = 'default'
130
        if getattr(self, 'auth_base', False):
131
            cyclades_endpoints = self.auth_base.get_service_endpoints(
132
                self._custom_type('cyclades') or 'compute',
133
                self._custom_version('cyclades') or '')
134
            base_url = cyclades_endpoints['publicURL']
135
            token = self.auth_base.token
136
            self.client = CycladesClient(base_url=base_url, token=token)
137
        else:
138
            raise CLIBaseUrlError(service='cyclades')
139

    
140
    @dataModification
141
    def _restruct_server_info(self, vm):
142
        if not vm:
143
            return vm
144
        img = vm['image']
145
        try:
146
            img.pop('links', None)
147
            img['name'] = self.client.get_image_details(img['id'])['name']
148
        except Exception:
149
            pass
150
        flv = vm['flavor']
151
        try:
152
            flv.pop('links', None)
153
            flv['name'] = self.client.get_flavor_details(flv['id'])['name']
154
        except Exception:
155
            pass
156
        vm['ports'] = vm.pop('attachments', dict())
157
        for port in vm['ports']:
158
            netid = port.get('network_id')
159
            for k in vm['addresses'].get(netid, []):
160
                k.pop('addr', None)
161
                k.pop('version', None)
162
                port.update(k)
163
        uuids = self._uuids2usernames([vm['user_id'], vm['tenant_id']])
164
        vm['user_id'] += ' (%s)' % uuids[vm['user_id']]
165
        for key in ('addresses', 'tenant_id', 'links'):
166
            vm.pop(key, None)
167
        return vm
168

    
169
    def main(self):
170
        self._run()
171

    
172

    
173
@command(server_cmds)
174
class server_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
175
    """List virtual servers accessible by user
176
    Use filtering arguments (e.g., --name-like) to manage long server lists
177
    """
178

    
179
    PERMANENTS = ('id', 'name')
180

    
181
    arguments = dict(
182
        detail=FlagArgument('show detailed output', ('-l', '--details')),
183
        since=DateArgument(
184
            'show only items since date (\' d/m/Y H:M:S \')',
185
            '--since'),
186
        limit=IntArgument(
187
            'limit number of listed virtual servers', ('-n', '--number')),
188
        more=FlagArgument(
189
            'output results in pages (-n to set items per page, default 10)',
190
            '--more'),
191
        enum=FlagArgument('Enumerate results', '--enumerate'),
192
        flavor_id=ValueArgument('filter by flavor id', ('--flavor-id')),
193
        image_id=ValueArgument('filter by image id', ('--image-id')),
194
        user_id=ValueArgument('filter by user id', ('--user-id')),
195
        user_name=ValueArgument('filter by user name', ('--user-name')),
196
        status=ValueArgument(
197
            'filter by status (ACTIVE, STOPPED, REBOOT, ERROR, etc.)',
198
            ('--status')),
199
        meta=KeyValueArgument('filter by metadata key=values', ('--metadata')),
200
        meta_like=KeyValueArgument(
201
            'print only if in key=value, the value is part of actual value',
202
            ('--metadata-like')),
203
    )
204

    
205
    def _add_user_name(self, servers):
206
        uuids = self._uuids2usernames(list(set(
207
                [srv['user_id'] for srv in servers] +
208
                [srv['tenant_id'] for srv in servers])))
209
        for srv in servers:
210
            srv['user_id'] += ' (%s)' % uuids[srv['user_id']]
211
            srv['tenant_id'] += ' (%s)' % uuids[srv['tenant_id']]
212
        return servers
213

    
214
    def _apply_common_filters(self, servers):
215
        common_filters = dict()
216
        if self['status']:
217
            common_filters['status'] = self['status']
218
        if self['user_id'] or self['user_name']:
219
            uuid = self['user_id'] or self._username2uuid(self['user_name'])
220
            common_filters['user_id'] = uuid
221
        return filter_dicts_by_dict(servers, common_filters)
222

    
223
    def _filter_by_image(self, servers):
224
        iid = self['image_id']
225
        return [srv for srv in servers if srv['image']['id'] == iid]
226

    
227
    def _filter_by_flavor(self, servers):
228
        fid = self['flavor_id']
229
        return [srv for srv in servers if (
230
            '%s' % srv['image']['id'] == '%s' % fid)]
231

    
232
    def _filter_by_metadata(self, servers):
233
        new_servers = []
234
        for srv in servers:
235
            if not 'metadata' in srv:
236
                continue
237
            meta = [dict(srv['metadata'])]
238
            if self['meta']:
239
                meta = filter_dicts_by_dict(meta, self['meta'])
240
            if meta and self['meta_like']:
241
                meta = filter_dicts_by_dict(
242
                    meta, self['meta_like'], exact_match=False)
243
            if meta:
244
                new_servers.append(srv)
245
        return new_servers
246

    
247
    @errors.generic.all
248
    @errors.cyclades.connection
249
    @errors.cyclades.date
250
    def _run(self):
251
        withimage = bool(self['image_id'])
252
        withflavor = bool(self['flavor_id'])
253
        withmeta = bool(self['meta'] or self['meta_like'])
254
        withcommons = bool(
255
            self['status'] or self['user_id'] or self['user_name'])
256
        detail = self['detail'] or (
257
            withimage or withflavor or withmeta or withcommons)
258
        servers = self.client.list_servers(detail, self['since'])
259

    
260
        servers = self._filter_by_name(servers)
261
        servers = self._filter_by_id(servers)
262
        servers = self._apply_common_filters(servers)
263
        if withimage:
264
            servers = self._filter_by_image(servers)
265
        if withflavor:
266
            servers = self._filter_by_flavor(servers)
267
        if withmeta:
268
            servers = self._filter_by_metadata(servers)
269

    
270
        if detail and self['detail']:
271
            servers = [self._restruct_server_info(vm) for vm in servers]
272
        else:
273
            for srv in servers:
274
                for key in set(srv).difference(self.PERMANENTS):
275
                    srv.pop(key)
276

    
277
        kwargs = dict(with_enumeration=self['enum'])
278
        if self['more']:
279
            codecinfo = codecs.lookup('utf-8')
280
            kwargs['out'] = codecs.StreamReaderWriter(
281
                cStringIO.StringIO(),
282
                codecinfo.streamreader,
283
                codecinfo.streamwriter)
284
            kwargs['title'] = ()
285
        if self['limit']:
286
            servers = servers[:self['limit']]
287
        self._print(servers, **kwargs)
288
        if self['more']:
289
            pager(kwargs['out'].getvalue())
290

    
291
    def main(self):
292
        super(self.__class__, self)._run()
293
        self._run()
294

    
295

    
296
@command(server_cmds)
297
class server_info(_init_cyclades, _optional_json):
298
    """Detailed information on a Virtual Machine"""
299

    
300
    arguments = dict(
301
        nics=FlagArgument(
302
            'Show only the network interfaces of this virtual server',
303
            '--nics'),
304
        network_id=ValueArgument(
305
            'Show the connection details to that network', '--network-id'),
306
        stats=FlagArgument('Get URLs for server statistics', '--stats'),
307
        diagnostics=FlagArgument('Diagnostic information', '--diagnostics')
308
    )
309

    
310
    @errors.generic.all
311
    @errors.cyclades.connection
312
    @errors.cyclades.server_id
313
    def _run(self, server_id):
314
        if self['nics']:
315
            self._print(
316
                self.client.get_server_nics(server_id), self.print_dict)
317
        elif self['network_id']:
318
            self._print(
319
                self.client.get_server_network_nics(
320
                    server_id, self['network_id']), self.print_dict)
321
        elif self['stats']:
322
            self._print(
323
                self.client.get_server_stats(server_id), self.print_dict)
324
        elif self['diagnostics']:
325
            self._print(self.client.get_server_diagnostics(server_id))
326
        else:
327
            vm = self.client.get_server_details(server_id)
328
            self._print(self._restruct_server_info(vm), self.print_dict)
329

    
330
    def main(self, server_id):
331
        super(self.__class__, self)._run()
332
        choose_one = ('nics', 'stats', 'diagnostics')
333
        count = len([a for a in choose_one if self[a]])
334
        if count > 1:
335
            raise CLIInvalidArgument('Invalid argument combination', details=[
336
                'Arguments %s cannot be used simultaneously' % ', '.join(
337
                    [self.arguments[a].lvalue for a in choose_one])])
338
        self._run(server_id=server_id)
339

    
340

    
341
class PersonalityArgument(KeyValueArgument):
342

    
343
    terms = (
344
        ('local-path', 'contents'),
345
        ('server-path', 'path'),
346
        ('owner', 'owner'),
347
        ('group', 'group'),
348
        ('mode', 'mode'))
349

    
350
    @property
351
    def value(self):
352
        return getattr(self, '_value', [])
353

    
354
    @value.setter
355
    def value(self, newvalue):
356
        if newvalue == self.default:
357
            return self.value
358
        self._value, input_dict = [], {}
359
        for i, terms in enumerate(newvalue):
360
            termlist = terms.split(',')
361
            if len(termlist) > len(self.terms):
362
                msg = 'Wrong number of terms (1<=terms<=%s)' % len(self.terms)
363
                raiseCLIError(CLISyntaxError(msg), details=howto_personality)
364

    
365
            for k, v in self.terms:
366
                prefix = '%s=' % k
367
                for item in termlist:
368
                    if item.lower().startswith(prefix):
369
                        input_dict[k] = item[len(k) + 1:]
370
                        break
371
                    item = None
372
                if item:
373
                    termlist.remove(item)
374

    
375
            try:
376
                path = input_dict['local-path']
377
            except KeyError:
378
                path = termlist.pop(0)
379
                if not path:
380
                    raise CLIInvalidArgument(
381
                        '--personality: No local path specified',
382
                        details=howto_personality)
383

    
384
            if not exists(path):
385
                raise CLIInvalidArgument(
386
                    '--personality: File %s does not exist' % path,
387
                    details=howto_personality)
388

    
389
            self._value.append(dict(path=path))
390
            with open(expanduser(path)) as f:
391
                self._value[i]['contents'] = b64encode(f.read())
392
            for k, v in self.terms[1:]:
393
                try:
394
                    self._value[i][v] = input_dict[k]
395
                except KeyError:
396
                    try:
397
                        self._value[i][v] = termlist.pop(0)
398
                    except IndexError:
399
                        continue
400
                if k in ('mode', ) and self._value[i][v]:
401
                    try:
402
                        self._value[i][v] = int(self._value[i][v], 8)
403
                    except ValueError as ve:
404
                        raise CLIInvalidArgument(
405
                            'Personality mode must be in octal', details=[
406
                                '%s' % ve])
407

    
408

    
409
class NetworkArgument(RepeatableArgument):
410
    """[id=]NETWORK_ID[,[ip=]IP]"""
411

    
412
    @property
413
    def value(self):
414
        return getattr(self, '_value', self.default)
415

    
416
    @value.setter
417
    def value(self, new_value):
418
        for v in new_value or []:
419
            part1, sep, part2 = v.partition(',')
420
            netid, ip = '', ''
421
            if part1.startswith('id='):
422
                netid = part1[len('id='):]
423
            elif part1.startswith('ip='):
424
                ip = part1[len('ip='):]
425
            else:
426
                netid = part1
427
            if part2:
428
                if (part2.startswith('id=') and netid) or (
429
                        part2.startswith('ip=') and ip):
430
                    raise CLIInvalidArgument(
431
                        'Invalid network argument %s' % v, details=[
432
                        'Valid format: [id=]NETWORK_ID[,[ip=]IP]'])
433
                if part2.startswith('id='):
434
                    netid = part2[len('id='):]
435
                elif part2.startswith('ip='):
436
                    ip = part2[len('ip='):]
437
                elif netid:
438
                    ip = part2
439
                else:
440
                    netid = part2
441
            if not netid:
442
                raise CLIInvalidArgument(
443
                    'Invalid network argument %s' % v, details=[
444
                    'Valid format: [id=]NETWORK_ID[,[ip=]IP]'])
445
            self._value = getattr(self, '_value', [])
446
            self._value.append(dict(uuid=netid))
447
            if ip:
448
                self._value[-1]['fixed_ip'] = ip
449

    
450

    
451
@command(server_cmds)
452
class server_create(_init_cyclades, _optional_json, _server_wait):
453
    """Create a server (aka Virtual Machine)"""
454

    
455
    arguments = dict(
456
        server_name=ValueArgument('The name of the new server', '--name'),
457
        flavor_id=IntArgument('The ID of the flavor', '--flavor-id'),
458
        image_id=ValueArgument('The ID of the image', '--image-id'),
459
        personality=PersonalityArgument(
460
            (80 * ' ').join(howto_personality), ('-p', '--personality')),
461
        wait=FlagArgument('Wait server to build', ('-w', '--wait')),
462
        cluster_size=IntArgument(
463
            'Create a cluster of servers of this size. In this case, the name'
464
            'parameter is the prefix of each server in the cluster (e.g.,'
465
            'srv1, srv2, etc.',
466
            '--cluster-size'),
467
        max_threads=IntArgument(
468
            'Max threads in cluster mode (default 1)', '--threads'),
469
        network_configuration=NetworkArgument(
470
            'Connect server to network: [id=]NETWORK_ID[,[ip=]IP]        . '
471
            'Use only NETWORK_ID for private networks.        . '
472
            'Use NETWORK_ID,[ip=]IP for networks with IP.        . '
473
            'Can be repeated, mutually exclussive with --no-network',
474
            '--network'),
475
        no_network=FlagArgument(
476
            'Do not create any network NICs on the server.        . '
477
            'Mutually exclusive to --network        . '
478
            'If neither --network or --no-network are used, the default '
479
            'network policy is applied. These policies are set on the cloud, '
480
            'so kamaki is oblivious to them',
481
            '--no-network')
482
    )
483
    required = ('server_name', 'flavor_id', 'image_id')
484

    
485
    @errors.cyclades.cluster_size
486
    def _create_cluster(self, prefix, flavor_id, image_id, size):
487
        networks = self['network_configuration'] or (
488
            [] if self['no_network'] else None)
489
        servers = [dict(
490
            name='%s%s' % (prefix, i if size > 1 else ''),
491
            flavor_id=flavor_id,
492
            image_id=image_id,
493
            personality=self['personality'],
494
            networks=networks) for i in range(1, 1 + size)]
495
        if size == 1:
496
            return [self.client.create_server(**servers[0])]
497
        self.client.MAX_THREADS = int(self['max_threads'] or 1)
498
        try:
499
            r = self.client.async_run(self.client.create_server, servers)
500
            return r
501
        except Exception as e:
502
            if size == 1:
503
                raise e
504
            try:
505
                requested_names = [s['name'] for s in servers]
506
                spawned_servers = [dict(
507
                    name=s['name'],
508
                    id=s['id']) for s in self.client.list_servers() if (
509
                        s['name'] in requested_names)]
510
                self.error('Failed to build %s servers' % size)
511
                self.error('Found %s matching servers:' % len(spawned_servers))
512
                self._print(spawned_servers, out=self._err)
513
                self.error('Check if any of these servers should be removed\n')
514
            except Exception as ne:
515
                self.error('Error (%s) while notifying about errors' % ne)
516
            finally:
517
                raise e
518

    
519
    @errors.generic.all
520
    @errors.cyclades.connection
521
    @errors.plankton.id
522
    @errors.cyclades.flavor_id
523
    def _run(self, name, flavor_id, image_id):
524
        for r in self._create_cluster(
525
                name, flavor_id, image_id, size=self['cluster_size'] or 1):
526
            if not r:
527
                self.error('Create %s: server response was %s' % (name, r))
528
                continue
529
            self._print(self._restruct_server_info(r), self.print_dict)
530
            if self['wait']:
531
                self._wait(r['id'], r['status'] or 'BUILD')
532
            self.writeln(' ')
533

    
534
    def main(self):
535
        super(self.__class__, self)._run()
536
        if self['no_network'] and self['network_configuration']:
537
            raise CLIInvalidArgument(
538
                'Invalid argument compination', importance=2, details=[
539
                'Arguments %s and %s are mutually exclusive' % (
540
                    self.arguments['no_network'].lvalue,
541
                    self.arguments['network_configuration'].lvalue)])
542
        self._run(
543
            name=self['server_name'],
544
            flavor_id=self['flavor_id'],
545
            image_id=self['image_id'])
546

    
547

    
548
class FirewallProfileArgument(ValueArgument):
549

    
550
    profiles = ('DISABLED', 'ENABLED', 'PROTECTED')
551

    
552
    @property
553
    def value(self):
554
        return getattr(self, '_value', None)
555

    
556
    @value.setter
557
    def value(self, new_profile):
558
        if new_profile:
559
            new_profile = new_profile.upper()
560
            if new_profile in self.profiles:
561
                self._value = new_profile
562
            else:
563
                raise CLIInvalidArgument(
564
                    'Invalid firewall profile %s' % new_profile,
565
                    details=['Valid values: %s' % ', '.join(self.profiles)])
566

    
567

    
568
@command(server_cmds)
569
class server_modify(_init_cyclades, _optional_output_cmd):
570
    """Modify attributes of a virtual server"""
571

    
572
    arguments = dict(
573
        server_name=ValueArgument('The new name', '--name'),
574
        flavor_id=IntArgument('Resize (set another flavor)', '--flavor-id'),
575
        firewall_profile=FirewallProfileArgument(
576
            'Valid values: %s' % (', '.join(FirewallProfileArgument.profiles)),
577
            '--firewall'),
578
        metadata_to_set=KeyValueArgument(
579
            'Set metadata in key=value form (can be repeated)',
580
            '--metadata-set'),
581
        metadata_to_delete=RepeatableArgument(
582
            'Delete metadata by key (can be repeated)', '--metadata-del'),
583
        public_network_port_id=ValueArgument(
584
            'Connection to set new firewall (* for all)', '--port-id'),
585
    )
586
    required = [
587
        'server_name', 'flavor_id', 'firewall_profile', 'metadata_to_set',
588
        'metadata_to_delete']
589

    
590
    @errors.generic.all
591
    @errors.cyclades.connection
592
    @errors.cyclades.server_id
593
    def _run(self, server_id):
594
        if self['server_name'] is not None:
595
            self.client.update_server_name((server_id), self['server_name'])
596
        if self['flavor_id']:
597
            self.client.resize_server(server_id, self['flavor_id'])
598
        if self['firewall_profile']:
599
            vm = self._restruct_server_info(
600
                self.client.get_server_details(server_id))
601
            ports = [p for p in vm['ports'] if 'firewallProfile' in p]
602
            picked_port = self.arguments['public_network_port_id']
603
            if picked_port.value and picked_port.value != '*':
604
                    ports = [p for p in ports if p['id'] == picked_port.value]
605
            elif len(ports) > 1:
606
                raiseCLIError(
607
                    'Multiple public networks are connected to server %s' % (
608
                        server_id), details=[
609
                            'To select one:',
610
                            '  %s <port id>' % picked_port.lvalue,
611
                            'To set all:',
612
                            '  %s *' % picked_port.lvalue,
613
                            'Ports to public networks on server %s:' % (
614
                                server_id),
615
                            ','.join([' %s' % p['id'] for p in ports])])
616
            if not ports:
617
                pp = picked_port.value
618
                raiseCLIError(
619
                    'No *public* networks attached on server %s%s' % (
620
                        server_id, ' through port %s' % pp if pp else ''),
621
                    details=[
622
                        'To see all networks:',
623
                        '  kamaki network list',
624
                        'To connect to a network:',
625
                        '  kamaki network connect <net id> --device-id %s' % (
626
                            server_id)])
627
            for port in ports:
628
                self.client.set_firewall_profile(
629
                    server_id=server_id,
630
                    profile=self['firewall_profile'],
631
                    port_id=port['id'])
632
        if self['metadata_to_set']:
633
            self.client.update_server_metadata(
634
                server_id, **self['metadata_to_set'])
635
        for key in (self['metadata_to_delete'] or []):
636
            errors.cyclades.metadata(
637
                self.client.delete_server_metadata)(server_id, key=key)
638
        if self['with_output']:
639
            self._optional_output(self.client.get_server_details(server_id))
640

    
641
    def main(self, server_id):
642
        super(self.__class__, self)._run()
643
        pnpid = self.arguments['public_network_port_id']
644
        fp = self.arguments['firewall_profile']
645
        if pnpid.value and not fp.value:
646
            raise CLIInvalidArgument('Invalid argument compination', details=[
647
                'Argument %s should always be combined with %s' % (
648
                    pnpid.lvalue, fp.lvalue)])
649
        self._run(server_id=server_id)
650

    
651

    
652
@command(server_cmds)
653
class server_delete(_init_cyclades, _optional_output_cmd, _server_wait):
654
    """Delete a virtual server"""
655

    
656
    arguments = dict(
657
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')),
658
        cluster=FlagArgument(
659
            '(DANGEROUS) Delete all virtual servers prefixed with the cluster '
660
            'prefix. In that case, the prefix replaces the server id',
661
            '--cluster')
662
    )
663

    
664
    def _server_ids(self, server_var):
665
        if self['cluster']:
666
            return [s['id'] for s in self.client.list_servers() if (
667
                s['name'].startswith(server_var))]
668

    
669
        @errors.cyclades.server_id
670
        def _check_server_id(self, server_id):
671
            return server_id
672

    
673
        return [_check_server_id(self, server_id=server_var), ]
674

    
675
    @errors.generic.all
676
    @errors.cyclades.connection
677
    def _run(self, server_var):
678
        for server_id in self._server_ids(server_var):
679
            if self['wait']:
680
                details = self.client.get_server_details(server_id)
681
                status = details['status']
682

    
683
            r = self.client.delete_server(server_id)
684
            self._optional_output(r)
685

    
686
            if self['wait']:
687
                self._wait(server_id, status)
688

    
689
    def main(self, server_id_or_cluster_prefix):
690
        super(self.__class__, self)._run()
691
        self._run(server_id_or_cluster_prefix)
692

    
693

    
694
@command(server_cmds)
695
class server_reboot(_init_cyclades, _optional_output_cmd, _server_wait):
696
    """Reboot a virtual server"""
697

    
698
    arguments = dict(
699
        hard=FlagArgument(
700
            'perform a hard reboot (deprecated)', ('-f', '--force')),
701
        type=ValueArgument('SOFT or HARD - default: SOFT', ('--type')),
702
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
703
    )
704

    
705
    @errors.generic.all
706
    @errors.cyclades.connection
707
    @errors.cyclades.server_id
708
    def _run(self, server_id):
709
        hard_reboot = self['hard']
710
        if hard_reboot:
711
            self.error(
712
                'WARNING: -f/--force will be deprecated in version 0.12\n'
713
                '\tIn the future, please use --type=hard instead')
714
        if self['type']:
715
            if self['type'].lower() in ('soft', ):
716
                hard_reboot = False
717
            elif self['type'].lower() in ('hard', ):
718
                hard_reboot = True
719
            else:
720
                raise CLISyntaxError(
721
                    'Invalid reboot type %s' % self['type'],
722
                    importance=2, details=[
723
                        '--type values are either SOFT (default) or HARD'])
724

    
725
        r = self.client.reboot_server(int(server_id), hard_reboot)
726
        self._optional_output(r)
727

    
728
        if self['wait']:
729
            self._wait(server_id, 'REBOOT')
730

    
731
    def main(self, server_id):
732
        super(self.__class__, self)._run()
733
        self._run(server_id=server_id)
734

    
735

    
736
@command(server_cmds)
737
class server_start(_init_cyclades, _optional_output_cmd, _server_wait):
738
    """Start an existing virtual server"""
739

    
740
    arguments = dict(
741
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
742
    )
743

    
744
    @errors.generic.all
745
    @errors.cyclades.connection
746
    @errors.cyclades.server_id
747
    def _run(self, server_id):
748
        status = 'ACTIVE'
749
        if self['wait']:
750
            details = self.client.get_server_details(server_id)
751
            status = details['status']
752
            if status in ('ACTIVE', ):
753
                return
754

    
755
        r = self.client.start_server(int(server_id))
756
        self._optional_output(r)
757

    
758
        if self['wait']:
759
            self._wait(server_id, status)
760

    
761
    def main(self, server_id):
762
        super(self.__class__, self)._run()
763
        self._run(server_id=server_id)
764

    
765

    
766
@command(server_cmds)
767
class server_shutdown(_init_cyclades, _optional_output_cmd, _server_wait):
768
    """Shutdown an active virtual server"""
769

    
770
    arguments = dict(
771
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
772
    )
773

    
774
    @errors.generic.all
775
    @errors.cyclades.connection
776
    @errors.cyclades.server_id
777
    def _run(self, server_id):
778
        status = 'STOPPED'
779
        if self['wait']:
780
            details = self.client.get_server_details(server_id)
781
            status = details['status']
782
            if status in ('STOPPED', ):
783
                return
784

    
785
        r = self.client.shutdown_server(int(server_id))
786
        self._optional_output(r)
787

    
788
        if self['wait']:
789
            self._wait(server_id, status)
790

    
791
    def main(self, server_id):
792
        super(self.__class__, self)._run()
793
        self._run(server_id=server_id)
794

    
795

    
796
@command(server_cmds)
797
class server_console(_init_cyclades, _optional_json):
798
    """Create a VMC console and show connection information"""
799

    
800
    @errors.generic.all
801
    @errors.cyclades.connection
802
    @errors.cyclades.server_id
803
    def _run(self, server_id):
804
        self.error('The following credentials will be invalidated shortly')
805
        self._print(
806
            self.client.get_server_console(server_id), self.print_dict)
807

    
808
    def main(self, server_id):
809
        super(self.__class__, self)._run()
810
        self._run(server_id=server_id)
811

    
812

    
813
@command(server_cmds)
814
class server_wait(_init_cyclades, _server_wait):
815
    """Wait for server to change its status (default: BUILD)"""
816

    
817
    arguments = dict(
818
        timeout=IntArgument(
819
            'Wait limit in seconds (default: 60)', '--timeout', default=60),
820
        server_status=StatusArgument(
821
            'Status to wait for (%s, default: %s)' % (
822
                ', '.join(server_states), server_states[0]),
823
            '--status',
824
            valid_states=server_states)
825
    )
826

    
827
    @errors.generic.all
828
    @errors.cyclades.connection
829
    @errors.cyclades.server_id
830
    def _run(self, server_id, current_status):
831
        r = self.client.get_server_details(server_id)
832
        if r['status'].lower() == current_status.lower():
833
            self._wait(server_id, current_status, timeout=self['timeout'])
834
        else:
835
            self.error(
836
                'Server %s: Cannot wait for status %s, '
837
                'status is already %s' % (
838
                    server_id, current_status, r['status']))
839

    
840
    def main(self, server_id):
841
        super(self.__class__, self)._run()
842
        self._run(
843
            server_id=server_id,
844
            current_status=self['server_status'] or 'BUILD')
845

    
846

    
847
@command(flavor_cmds)
848
class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
849
    """List available hardware flavors"""
850

    
851
    PERMANENTS = ('id', 'name')
852

    
853
    arguments = dict(
854
        detail=FlagArgument('show detailed output', ('-l', '--details')),
855
        limit=IntArgument('limit # of listed flavors', ('-n', '--number')),
856
        more=FlagArgument(
857
            'output results in pages (-n to set items per page, default 10)',
858
            '--more'),
859
        enum=FlagArgument('Enumerate results', '--enumerate'),
860
        ram=ValueArgument('filter by ram', ('--ram')),
861
        vcpus=ValueArgument('filter by number of VCPUs', ('--vcpus')),
862
        disk=ValueArgument('filter by disk size in GB', ('--disk')),
863
        disk_template=ValueArgument(
864
            'filter by disk_templace', ('--disk-template'))
865
    )
866

    
867
    def _apply_common_filters(self, flavors):
868
        common_filters = dict()
869
        if self['ram']:
870
            common_filters['ram'] = self['ram']
871
        if self['vcpus']:
872
            common_filters['vcpus'] = self['vcpus']
873
        if self['disk']:
874
            common_filters['disk'] = self['disk']
875
        if self['disk_template']:
876
            common_filters['SNF:disk_template'] = self['disk_template']
877
        return filter_dicts_by_dict(flavors, common_filters)
878

    
879
    @errors.generic.all
880
    @errors.cyclades.connection
881
    def _run(self):
882
        withcommons = self['ram'] or self['vcpus'] or (
883
            self['disk'] or self['disk_template'])
884
        detail = self['detail'] or withcommons
885
        flavors = self.client.list_flavors(detail)
886
        flavors = self._filter_by_name(flavors)
887
        flavors = self._filter_by_id(flavors)
888
        if withcommons:
889
            flavors = self._apply_common_filters(flavors)
890
        if not (self['detail'] or (
891
                self['json_output'] or self['output_format'])):
892
            remove_from_items(flavors, 'links')
893
        if detail and not self['detail']:
894
            for flv in flavors:
895
                for key in set(flv).difference(self.PERMANENTS):
896
                    flv.pop(key)
897
        kwargs = dict(out=StringIO(), title=()) if self['more'] else {}
898
        self._print(
899
            flavors,
900
            with_redundancy=self['detail'], with_enumeration=self['enum'],
901
            **kwargs)
902
        if self['more']:
903
            pager(kwargs['out'].getvalue())
904

    
905
    def main(self):
906
        super(self.__class__, self)._run()
907
        self._run()
908

    
909

    
910
@command(flavor_cmds)
911
class flavor_info(_init_cyclades, _optional_json):
912
    """Detailed information on a hardware flavor
913
    To get a list of available flavors and flavor ids, try /flavor list
914
    """
915

    
916
    @errors.generic.all
917
    @errors.cyclades.connection
918
    @errors.cyclades.flavor_id
919
    def _run(self, flavor_id):
920
        self._print(
921
            self.client.get_flavor_details(int(flavor_id)), self.print_dict)
922

    
923
    def main(self, flavor_id):
924
        super(self.__class__, self)._run()
925
        self._run(flavor_id=flavor_id)
926

    
927

    
928
def _add_name(self, net):
929
        user_id, tenant_id, uuids = net['user_id'], net['tenant_id'], []
930
        if user_id:
931
            uuids.append(user_id)
932
        if tenant_id:
933
            uuids.append(tenant_id)
934
        if uuids:
935
            usernames = self._uuids2usernames(uuids)
936
            if user_id:
937
                net['user_id'] += ' (%s)' % usernames[user_id]
938
            if tenant_id:
939
                net['tenant_id'] += ' (%s)' % usernames[tenant_id]