~ubuntu-cloud-archive/ubuntu/precise/nova/trunk

« back to all changes in this revision

Viewing changes to nova/image/glance.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, Adam Gandelman, Chuck Short
  • Date: 2012-07-06 10:18:33 UTC
  • mfrom: (1.1.58)
  • Revision ID: package-import@ubuntu.com-20120706101833-wp2nv392mpe9re8p
Tags: 2012.2~f2-0ubuntu1
[ Adam Gandelman ]
* Use new rootwrap configuration structure:
  - debian/nova-{compute, network, volume}.{pyinstall, pyremove}: Dropped.
  - debian/nova-common.dirs: Add /etc/nova/rootwrap.d/.
  - debian/nova-common.install: Install /etc/nova/rootwrap.conf.
  - debian/debian/nova.conf: Reference rootwrap.conf in calls to
    nova-rootwrap.
  - debian/nova-{compute, network, volume}.install: Install corresponding
    filter in /etc/nova/rootwrap.d/
* debian/rules: Install logging_sample.conf to /etc/nova/logging.conf
  as part of nova-common.
* debian/pydist-overrides: Add setuptools-git.
* debian/control: Add python-setuptools-git as a Build-Depends.
* debian/rules: Do not remove nova.egg-info during auto_clean.  Now that
  upstream has moved to setuptools-git, doing so results in missing files
  from built package.

[ Chuck Short ]
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
import time
26
26
import urlparse
27
27
 
 
28
import glance.client
28
29
from glance.common import exception as glance_exception
29
30
 
30
31
from nova import exception
31
32
from nova import flags
32
 
from nova import log as logging
33
 
from nova.openstack.common import importutils
34
33
from nova.openstack.common import jsonutils
 
34
from nova.openstack.common import log as logging
35
35
from nova.openstack.common import timeutils
36
36
from nova import utils
37
37
 
38
38
 
39
39
LOG = logging.getLogger(__name__)
40
 
 
41
 
 
42
40
FLAGS = flags.FLAGS
43
41
 
44
42
 
45
 
GlanceClient = importutils.import_class('glance.client.Client')
46
 
 
47
 
 
48
43
def _parse_image_ref(image_href):
49
44
    """Parse an image href into composite parts.
50
45
 
61
56
 
62
57
 
63
58
def _create_glance_client(context, host, port):
 
59
    params = {}
64
60
    if FLAGS.auth_strategy == 'keystone':
65
 
        # NOTE(dprince): Glance client just needs auth_tok right? Should we
66
 
        # add username and tenant to the creds below?
67
 
        creds = {'strategy': 'keystone',
68
 
                 'username': context.user_id,
69
 
                 'tenant': context.project_id}
70
 
        glance_client = GlanceClient(host, port, auth_tok=context.auth_token,
71
 
                                     creds=creds)
72
 
    else:
73
 
        glance_client = GlanceClient(host, port)
74
 
    return glance_client
 
61
        params['creds'] = {
 
62
            'strategy': 'keystone',
 
63
            'username': context.user_id,
 
64
            'tenant': context.project_id,
 
65
        }
 
66
        params['auth_tok'] = context.auth_token
 
67
 
 
68
    return glance.client.Client(host, port, **params)
75
69
 
76
70
 
77
71
def pick_glance_api_server():
89
83
    return host, port
90
84
 
91
85
 
92
 
def get_glance_client(context, image_href):
 
86
def _get_glance_client(context, image_href):
93
87
    """Get the correct glance client and id for the given image_href.
94
88
 
95
89
    The image_href param can be an href of the form
97
91
    image_href is an int, then flags are used to create the default
98
92
    glance client.
99
93
 
 
94
    NOTE: Do not use this or glance.client directly, all other code
 
95
    should be using GlanceImageService.
 
96
 
100
97
    :param image_href: image ref/id for an image
101
98
    :returns: a tuple of the form (glance_client, image_id)
102
99
 
153
150
        raise exception.GlanceConnectionFailed(
154
151
                reason=_('Maximum attempts reached'))
155
152
 
156
 
    def index(self, context, **kwargs):
157
 
        """Calls out to Glance for a list of images available."""
158
 
        params = self._extract_query_params(kwargs)
159
 
        image_metas = self._get_images(context, **params)
160
 
 
161
 
        images = []
162
 
        for image_meta in image_metas:
163
 
            # NOTE(sirp): We need to use `get_images_detailed` and not
164
 
            # `get_images` here because we need `is_public` and `properties`
165
 
            # included so we can filter by user
166
 
            if self._is_image_available(context, image_meta):
167
 
                meta_subset = utils.subset_dict(image_meta, ('id', 'name'))
168
 
                images.append(meta_subset)
169
 
        return images
170
 
 
171
153
    def detail(self, context, **kwargs):
172
154
        """Calls out to Glance for a list of detailed image information."""
173
155
        params = self._extract_query_params(kwargs)
247
229
        base_image_meta = self._translate_from_glance(image_meta)
248
230
        return base_image_meta
249
231
 
250
 
    def show_by_name(self, context, name):
251
 
        """Returns a dict containing image data for the given name."""
252
 
        image_metas = self.detail(context, filters={'name': name})
253
 
        try:
254
 
            return image_metas[0]
255
 
        except (IndexError, TypeError):
256
 
            raise exception.ImageNotFound(image_id=name)
257
 
 
258
 
    def get(self, context, image_id, data):
 
232
    def download(self, context, image_id, data):
259
233
        """Calls out to Glance for metadata and data and writes data."""
260
234
        try:
261
235
            image_meta, image_chunks = self._call_retry(context, 'get_image',
266
240
        for chunk in image_chunks:
267
241
            data.write(chunk)
268
242
 
269
 
        base_image_meta = self._translate_from_glance(image_meta)
270
 
        return base_image_meta
271
 
 
272
243
    def create(self, context, image_meta, data=None):
273
244
        """Store the image data and return the new image id.
274
245
 
413
384
                       'signatures: %(iso_formats)s') % locals())
414
385
 
415
386
 
416
 
# TODO(yamahata): use block-device-mapping extension to glance
 
387
# NOTE(bcwaldon): used to store non-string data in glance metadata
417
388
def _json_loads(properties, attr):
418
389
    prop = properties[attr]
419
390
    if isinstance(prop, basestring):
430
401
 
431
402
 
432
403
def _convert(method, metadata):
433
 
    metadata = copy.deepcopy(metadata)  # don't touch original metadata
 
404
    metadata = copy.deepcopy(metadata)
434
405
    properties = metadata.get('properties')
435
406
    if properties:
436
407
        for attr in _CONVERT_PROPS:
449
420
 
450
421
 
451
422
def _limit_attributes(image_meta):
452
 
    IMAGE_ATTRIBUTES = ['size', 'disk_format',
 
423
    IMAGE_ATTRIBUTES = ['size', 'disk_format', 'owner',
453
424
                        'container_format', 'checksum', 'id',
454
425
                        'name', 'created_at', 'updated_at',
455
426
                        'deleted_at', 'deleted', 'status',
508
479
    if exc_type is glance_exception.Invalid:
509
480
        return exception.Invalid(exc_value)
510
481
    return exc_value
 
482
 
 
483
 
 
484
def get_remote_image_service(context, image_href):
 
485
    """Create an image_service and parse the id from the given image_href.
 
486
 
 
487
    The image_href param can be an href of the form
 
488
    'http://example.com:9292/v1/images/b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3',
 
489
    or just an id such as 'b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3'. If the
 
490
    image_href is a standalone id, then the default image service is returned.
 
491
 
 
492
    :param image_href: href that describes the location of an image
 
493
    :returns: a tuple of the form (image_service, image_id)
 
494
 
 
495
    """
 
496
    #NOTE(bcwaldon): If image_href doesn't look like a URI, assume its a
 
497
    # standalone image ID
 
498
    if '/' not in str(image_href):
 
499
        image_service = get_default_image_service()
 
500
        image_id = image_href
 
501
    else:
 
502
        (glance_client, image_id) = _get_glance_client(context, image_href)
 
503
        image_service = GlanceImageService(glance_client)
 
504
 
 
505
    return (image_service, image_id)
 
506
 
 
507
 
 
508
def get_default_image_service():
 
509
    return GlanceImageService()