Revision e6ce9ae1 docs/developers/showcase.rst

b/docs/developers/showcase.rst
49 49

  
50 50
    from sys import stderr
51 51
    from kamaki.cli.config import Config, CONFIG_PATH
52
    from kamaki.clients.astakos import AstakosClient, ClientError
52
    from kamaki.clients import ClientError
53
    from kamaki.clients.astakos import AstakosClient
53 54

  
54 55
    #  Initialize Config with default values.
55 56
    cnf = Config()
56 57

  
57 58
    #  1. Get the credentials
58
    #  Get default cloud name 
59
    try:
60
        cloud_name = cnf.get('global', 'default_cloud')
61
    except KeyError:
62
        stderr.write('No default cloud set in file %s\n' % CONFIG_PATH)
63
        raise
59
    #  Get default cloud name
60
    cloud_name = cnf.get('global', 'default_cloud')
61
    assert cloud_name, 'No default_cloud in file %s\n' % CONFIG_PATH
64 62

  
65 63
    #  Get cloud authentication URL and TOKEN
66 64
    try:
......
86 84

  
87 85
    #  3. Get the endpoints
88 86
    #  Identity, Account --> astakos
89
    #  Compute --> cyclades
87
    #  Compute, Network --> cyclades
90 88
    #  Object-store --> pithos
91 89
    #  Image --> plankton
92 90
    try:
93 91
        endpoints = dict(
94 92
            astakos=AUTH_URL,
95 93
            cyclades=auth.get_service_endpoints('compute')['publicURL'],
94
            network=auth.get_service_endpoints('network')['publicURL'],
96 95
            pithos=auth.get_service_endpoints('object-store')['publicURL'],
97 96
            plankton=auth.get_service_endpoints('image')['publicURL']
98 97
            )
99
        user_id = auth.user_info()['id']
98
        user_id = auth.user_info['id']
100 99
    except ClientError:
101 100
        stderr.write(
102 101
            'Failed to get user id and endpoints from the identity server\n')
103 102
        raise
104 103

  
104
    #  4. Pretty print the results
105
    stderr.write('Endpoints for user with id %s\n' % user_id)
106
    for k, v in endpoints.items():
107
        stderr.write('\t%s:\t%s\n' % (k, v))
108

  
109
The output of this script should look similar to this::
110

  
111
    Endpoints for user with id my-us3r-1d-asdf-1234-fd324rt
112
        pithos:     https://pithos.example.com/object-store/v1
113
        plankton:   https://cyclades.example.com/image/v1.0
114
        network:    https://cyclades.example.com/network/v2.0
115
        cyclades:   https://cyclades.example.com/compute/v2.0
116
        astakos:    https://accounts.example.com/identity/v2.0
117

  
118

  
119

  
105 120
Upload the image
106 121
----------------
107 122

  
......
123 138
    CONTAINER = 'images'
124 139
    IMAGE_FILE = 'my_image.diskdump'
125 140

  
141

  
126 142
    #  1. Initialize Pithos+ client and set account to current user
127 143
    try:
128 144
        pithos = PithosClient(endpoints['pithos'], AUTH_TOKEN)
......
133 149

  
134 150
    #  2. Create the container "images" and let pithos client work with that
135 151
    try:
136
        pithos.create_container('images')
152
        pithos.create_container(CONTAINER)
137 153
    except ClientError:
138
        stderr.write('Failed to create container "image"\n')
154
        stderr.write('Failed to create container %s\n' % CONTAINER)
139 155
        raise
140 156
    pithos.container = CONTAINER
141 157

  
142 158
    #  3. Upload
143 159
    with open(abspath(IMAGE_FILE)) as f:
144 160
        try:
161
            stderr.write('This may take a while ...')
145 162
            pithos.upload_object(IMAGE_FILE, f)
146 163
        except ClientError:
147 164
            stderr.write('Failed to upload file %s to container %s\n' % (
......
171 188
    #  3.2 Register the image
172 189
    properties = dict(osfamily='linux', root_partition='1')
173 190
    try:
174
        image = plankton.image_register(IMAGE_NAME, IMAGE_LOCATION)
191
        image = plankton.register(IMAGE_NAME, IMAGE_LOCATION)
175 192
    except ClientError:
176 193
        stderr.write('Failed to register image %s\n' % IMAGE_NAME)
177 194
        raise
......
442 459

  
443 460
.. code-block:: python
444 461

  
445
    #!/usr/bin/env python
446

  
447 462
    from sys import argv
448 463
    from os.path import abspath
449 464
    from base64 import b64encode
450 465
    from kamaki.clients import ClientError
451 466
    from kamaki.cli.logger import get_logger, add_file_logger
467
    from progress.bar import Bar
452 468
    from logging import DEBUG
453 469

  
454 470
    #  Define loggers
......
457 473
    add_file_logger(__name__, DEBUG, '%s.log' % __name__)
458 474

  
459 475
    #  Create progress bar generator
460
    try:
461
        from progress.bar import Bar
462 476

  
463
        def create_pb(msg):
464
            def generator(n):
465
                bar=Bar(msg)
466
                for i in bar.iter(range(int(n))):
467
                    yield
477

  
478
    def create_pb(msg):
479
        def generator(n):
480
            bar = Bar(msg)
481
            for i in bar.iter(range(int(n))):
468 482
                yield
469
            return generator
470
    except ImportError:
471
        log.warning('Suggestion: install python-progress')
472
        def create_pb(msg):
473
            return None
483
            yield
484
        return generator
474 485

  
475 486

  
476 487
    #  kamaki.config
......
519 530
            endpoints = dict(
520 531
                astakos=auth.get_service_endpoints('identity')['publicURL'],
521 532
                cyclades=auth.get_service_endpoints('compute')['publicURL'],
533
                network=auth.get_service_endpoints('network')['publicURL'],
522 534
                pithos=auth.get_service_endpoints('object-store')['publicURL'],
523 535
                plankton=auth.get_service_endpoints('image')['publicURL']
524 536
                )
525
            user_id = auth.user_info()['id']
537
            user_id = auth.user_info['id']
526 538
        except ClientError:
527 539
            print('Failed to get endpoints & user_id from identity server')
528 540
            raise
......
586 598
        image_location = (user_id, container, path)
587 599
        print(' Register the image')
588 600
        try:
589
             return plankton.register(name, image_location, properties)
601
            return plankton.register(name, image_location, properties)
590 602
        except ClientError:
591 603
            log.debug('Failed to register image %s' % name)
592 604
            raise
......
654 666
            for i in range(1, self.size + 1):
655 667
                try:
656 668
                    server_name = '%s%s' % (self.prefix, i)
669

  
657 670
                    servers.append(self.client.create_server(
658 671
                        server_name, self.flavor_id, self.image_id,
672
                        networks=[],
659 673
                        personality=self._personality(ssh_k_path, pub_k_path)))
660 674
                except ClientError:
661 675
                    log.debug('Failed while creating server %s' % server_name)
......
698 712

  
699 713
        print('4.  Create  virtual  cluster')
700 714
        cluster = Cluster(
701
            cyclades = init_cyclades(endpoints['cyclades'], token),
715
            cyclades=init_cyclades(endpoints['cyclades'], token),
702 716
            prefix=opts.prefix,
703 717
            flavor_id=opts.flavorid,
704 718
            image_id=image['id'],
......
738 752
        parser.add_option('--prefix',
739 753
                          action='store', type='string', dest='prefix',
740 754
                          help='The prefix to use for naming cluster nodes',
741
                          default='cluster')
755
                          default='node')
742 756
        parser.add_option('--clustersize',
743 757
                          action='store', type='string', dest='clustersize',
744 758
                          help='Number of virtual cluster nodes to create ',
......
795 809
        opts, args = parser.parse_args(argv[1:])
796 810

  
797 811
        main(opts)
798

  

Also available in: Unified diff