Revision 81c60832 kamaki/cli/commands/cyclades.py

b/kamaki/cli/commands/cyclades.py
375 375
    arguments = dict(
376 376
        personality=PersonalityArgument(
377 377
            (80 * ' ').join(howto_personality), ('-p', '--personality')),
378
        wait=FlagArgument('Wait server to build', ('-w', '--wait'))
378
        wait=FlagArgument('Wait server to build', ('-w', '--wait')),
379
        cluster_size=IntArgument(
380
            'Create a cluster of servers of this size. In this case, the name'
381
            'parameter is the prefix of each server in the cluster (e.g.,'
382
            'srv1, srv2, etc.',
383
            '--cluster-size')
379 384
    )
380 385

  
386
    @errors.cyclades.cluster_size
387
    def _create_cluster(self, prefix, flavor_id, image_id, size):
388
        servers = [dict(
389
            name='%s%s' % (prefix, i),
390
            flavor_id=flavor_id,
391
            image_id=image_id,
392
            personality=self['personality']) for i in range(size)]
393
        if size == 1:
394
            return [self.client.create_server(**servers[0])]
395
        return self.client.async_run(self.client.create_server, servers)
396

  
381 397
    @errors.generic.all
382 398
    @errors.cyclades.connection
383 399
    @errors.plankton.id
384 400
    @errors.cyclades.flavor_id
385 401
    def _run(self, name, flavor_id, image_id):
386
        r = self.client.create_server(
387
            name, int(flavor_id), image_id, personality=self['personality'])
388
        usernames = self._uuids2usernames([r['user_id'], r['tenant_id']])
389
        r['user_id'] += ' (%s)' % usernames[r['user_id']]
390
        r['tenant_id'] += ' (%s)' % usernames[r['tenant_id']]
391
        self._print(r, self.print_dict)
392
        if self['wait']:
393
            self._wait(r['id'], r['status'])
402
        for r in self._create_cluster(
403
                name, flavor_id, image_id, size=self['cluster_size'] or 1):
404
            print 'HEY I GOT A', r
405
            print 'MKEY?????????????????'
406
            usernames = self._uuids2usernames([r['user_id'], r['tenant_id']])
407
            r['user_id'] += ' (%s)' % usernames[r['user_id']]
408
            r['tenant_id'] += ' (%s)' % usernames[r['tenant_id']]
409
            self._print(r, self.print_dict)
410
            if self['wait']:
411
                self._wait(r['id'], r['status'])
412
            self.error('')
394 413

  
395 414
    def main(self, name, flavor_id, image_id):
396 415
        super(self.__class__, self)._run()
......
421 440
    """Delete a virtual server"""
422 441

  
423 442
    arguments = dict(
424
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait'))
443
        wait=FlagArgument('Wait server to be destroyed', ('-w', '--wait')),
444
        cluster=FlagArgument(
445
            '(DANGEROUS) Delete all virtual servers prefixed with the cluster '
446
            'prefix. In that case, the prefix replaces the server id',
447
            '--cluster')
425 448
    )
426 449

  
450
    def _server_ids(self, server_var):
451
        if self['cluster']:
452
            return [s['id'] for s in self.client.list_servers() if (
453
                s['name'].startswith(server_var))]
454

  
455
        @errors.cyclades.server_id
456
        def _check_server_id(self, server_id):
457
            return server_id
458

  
459
        return [_check_server_id(self, server_id=server_var), ]
460

  
427 461
    @errors.generic.all
428 462
    @errors.cyclades.connection
429
    @errors.cyclades.server_id
430
    def _run(self, server_id):
431
            status = 'DELETED'
463
    def _run(self, server_var):
464
        for server_id in self._server_ids(server_var):
432 465
            if self['wait']:
433 466
                details = self.client.get_server_details(server_id)
434 467
                status = details['status']
435 468

  
436
            r = self.client.delete_server(int(server_id))
469
            r = self.client.delete_server(server_id)
437 470
            self._optional_output(r)
438 471

  
439 472
            if self['wait']:
440 473
                self._wait(server_id, status)
441 474

  
442
    def main(self, server_id):
475
    def main(self, server_id_or_cluster_prefix):
443 476
        super(self.__class__, self)._run()
444
        self._run(server_id=server_id)
477
        self._run(server_id_or_cluster_prefix)
445 478

  
446 479

  
447 480
@command(server_cmds)
......
785 818
        self._run(server_id=server_id, current_status=current_status)
786 819

  
787 820

  
788
@command(server_cmds)
789
class server_cluster_create(_init_cyclades):
790
    """Create a cluster of virtual servers
791
    All new servers will be named as <prefix><increment> e.g.,
792
    mycluster1, mycluster2, etc.
793
    All servers in the cluster will run the same image on the same hardware
794
    flavor.
795
    """
796

  
797
    @errors.generic.all
798
    @errors.cyclades.connection
799
    @errors.plankton.id
800
    @errors.cyclades.flavor_id
801
    @errors.cyclades.cluster_size
802
    def _run(self, prefix, image_id, flavor_id, size):
803
        servers = [dict(
804
            name='%s%s' % (prefix, i),
805
            flavor_id=flavor_id,
806
            image_id=image_id) for i in range(int(size))]
807
        self.client.create_cluster(servers)
808

  
809
    def main(self, prefix, image_id, flavor_id, size):
810
        super(self.__class__, self)._run()
811
        self._run(prefix, image_id=image_id, flavor_id=flavor_id, size=size)
812

  
813

  
814
@command(server_cmds)
815
class server_cluster_delete(_init_cyclades):
816
    """Remove all servers that belong to a virtual cluster
817
    A virtual cluster consists of the virtual servers with the same name prefix
818
    ATTENTION: make sure you want to delete all servers of that prefix
819
    To get a list of your servers:  /server list
820
    """
821

  
822
    @errors.generic.all
823
    @errors.cyclades.connection
824
    def _run(self, prefix):
825
        servers = [s['id'] for s in self.client.list_servers() if (
826
            s['name'].startswith(prefix))]
827
        self.client.delete_cluster(servers)
828

  
829
    def main(self, prefix):
830
        super(self.__class__, self)._run()
831
        self._run(prefix)
832

  
833

  
834 821
@command(flavor_cmds)
835 822
class flavor_list(_init_cyclades, _optional_json, _name_filter, _id_filter):
836 823
    """List available hardware flavors"""

Also available in: Unified diff