Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright (c) 2013 TrilioData, Inc.
# All Rights Reserved.

"""Implementation of an image service that uses Glance as the backend."""



import copy
import itertools
import random
import shutil
import sys
import time
import urllib.parse
import uuid

import glanceclient
import glanceclient.exc
from oslo_config import cfg
from oslo_utils import encodeutils

from workloadmgr import exception
from workloadmgr.openstack.common import jsonutils
from workloadmgr.openstack.common import log as logging
from workloadmgr.openstack.common import timeutils

glance_opts = [
    cfg.StrOpt('glance_production_host',
               default='$my_ip',
               help='default glance hostname or ip of production'),
    cfg.StrOpt('glance_tvault_host',
               default='$my_ip',
               help='default glance hostname or ip of tvault'),
    cfg.IntOpt('glance_production_port',
               default=9292,
               help='default glance port of production'),
    cfg.IntOpt('glance_tvault_port',
               default=9292,
               help='default glance port of tvault'),
    cfg.ListOpt('glance_production_api_servers',
                default=['$glance_production_host:$glance_production_port'],
                help='A list of the glance api servers available to workloadmgr '
                     '([hostname|ip]:port)'),
    cfg.ListOpt('glance_tvault_api_servers',
                default=['$glance_tvault_host:$glance_tvault_port'],
                help='A list of the glance api servers available to workloadmgr '
                     '([hostname|ip]:port)'),
    cfg.StrOpt('glance_protocol',
               default='http',
               help='Default protocol to use when connecting to glance. '
               'Set to https for SSL.'),
    cfg.BoolOpt('glance_api_insecure',
                default=True,
                help='Allow to perform insecure SSL (https) requests to '
                     'glance'),
    cfg.IntOpt('glance_num_retries',
               default=0,
               help='Number retries when downloading an image from glance'),
    cfg.ListOpt('allowed_direct_url_schemes',
                default=[],
                help='A list of url scheme that can be downloaded directly '
                     'via the direct_url.  Currently supported schemes: '
                     '[file].'),
    cfg.IntOpt('glance_api_version',
                default=2,
                help='Default glance API version.'),
    ]


LOG = logging.getLogger(__name__)
CONF = cfg.CONF
CONF.register_opts(glance_opts)


def generate_glance_url(production):
    """Generate the URL to glance."""
    if production:
        return "%s://%s:%d" % (CONF.glance_protocol,
                               CONF.glance_production_host,
                               CONF.glance_production_port)
    else:
        return "%s://%s:%d" % (CONF.glance_protocol,
                               CONF.glance_tvault_host,
                               CONF.glance_tvault_port)


def generate_image_url(image_ref, production):
    """Generate an image URL from an image_ref."""
    return "%s/images/%s" % (generate_glance_url(production), image_ref)


def _parse_image_ref(image_href):
    """Parse an image href into composite parts.

    :param image_href: href of an image
    :returns: a tuple of the form (image_id, host, port)
    :raises ValueError

    """
    o = urllib.parse.urlparse(image_href)
    use_ssl = (o.scheme == 'https')
    if use_ssl:
        port = o.port or 443
    else:
        port = o.port or 80

    host = o.hostname
    host = '['+host+']' if ":" in host else host
    image_id = o.path.split('/')[-1]
    return (image_id, host, port, use_ssl)


def _create_glance_client(context, host, port, use_ssl, version=1):
    """Instantiate a new glanceclient.Client object."""
    if use_ssl:
        scheme = 'https'
    else:
        scheme = 'http'
    params = {}
    params['insecure'] = CONF.glance_api_insecure
    if CONF.auth_strategy == 'keystone':
        params['token'] = context.auth_token
    endpoint = '%s://%s:%s' % (scheme, host, port)
    return glanceclient.Client(str(version), endpoint, **params)


def get_api_servers(production):
    """
    Shuffle a list of CONF.glance_api_servers and return an iterator
    that will cycle through the list, looping around to the beginning
    if necessary.
    """
    api_servers = []

    if production:
        glance_api_servers = CONF.glance_production_api_servers
    else:
        glance_api_servers = CONF.glance_tvault_api_servers

    for api_server in glance_api_servers:
        if '//' not in api_server:
            api_server = 'http://' + api_server
        o = urllib.parse.urlparse(api_server)
        use_ssl = (o.scheme == 'https')
        if use_ssl:
            port = o.port or 443
        else:
            port = o.port or 80
        host = o.hostname
        host = '['+host+']' if ":" in host else host
        api_servers.append((host, port, use_ssl))
    random.shuffle(api_servers)
    return itertools.cycle(api_servers)


class GlanceClientWrapper(object):
    """Glance client wrapper class that implements retries."""

    def __init__( self, production, context=None,
                  host=None, port=None, use_ssl=False,
                  version=None):
        if host is not None:
            self.client = self._create_static_client(context,
                                                     host, port,
                                                     use_ssl, version)
        else:
            self.client = None

        self.api_servers = None
        self._production = production
        self.version = version or CONF.glance_api_version

    def _create_static_client(self, context, host, port, use_ssl, version):
        """Create a client that we'll use for every call."""
        self.host = host
        self.port = port
        self.use_ssl = use_ssl
        self.version = version
        return _create_glance_client(context,
                                     self.host, self.port,
                                     self.use_ssl, self.version)

    def _create_onetime_client(self, context, version):
        """Create a client that will be used for one call."""
        if self.api_servers is None:
            self.api_servers = get_api_servers(self._production)
        self.host, self.port, self.use_ssl = next(self.api_servers)
        return _create_glance_client(context,
                                     self.host, self.port,
                                     self.use_ssl, version)

    def call(self, context, method, *args, **kwargs):
        """
        Call a glance client method.  If we get a connection error,
        retry the request according to CONF.glance_num_retries.
        """
        retry_excs = (glanceclient.exc.ServiceUnavailable,
                      glanceclient.exc.InvalidEndpoint,
                      glanceclient.exc.CommunicationError)
        num_attempts = 1 + CONF.glance_num_retries

        for attempt in range(1, num_attempts + 1):
            client = self.client or \
                self._create_onetime_client(context,
                                            self.version)
            try:
                return getattr(client.images, method)(*args, **kwargs)
            except retry_excs as e:
                host = self.host
                port = self.port
                extra = "retrying"
                error_msg = _(
                    "Error contacting glance server "
                    "'%(host)s:%(port)s' for '%(method)s', %(extra)s.")
                if attempt == num_attempts:
                    extra = 'done trying'
                    LOG.exception(error_msg, locals())
                    raise exception.GlanceConnectionFailed(
                        host=host, port=port, reason=str(e))
                LOG.exception(error_msg, locals())
                time.sleep(1)


class GlanceImageService(object):
    """Provides storage and retrieval of disk image objects within Glance."""

    def __init__(self, client=None, production=True):
        self._client = client or GlanceClientWrapper(production)

    @staticmethod
    def _translate_to_glance(image_meta):
        image_meta = _convert_to_string(image_meta)
        image_meta = _remove_read_only(image_meta)
        return image_meta

    @staticmethod
    def _translate_from_glance(image):
        image_meta = _extract_attributes(image)
        image_meta = _convert_timestamps_to_datetimes(image_meta)
        image_meta = _convert_from_string(image_meta)
        return image_meta

    @staticmethod
    def _is_image_available(context, image):
        """Check image availability.

        This check is needed in case Nova and Glance are deployed
        without authentication turned on.
        """
        # The presence of an auth token implies this is an authenticated
        # request and we need not handle the noauth use-case.
        if hasattr(context, 'auth_token') and context.auth_token:
            return True

        if image.is_public or context.is_admin:
            return True

        properties = image.properties

        if context.project_id and ('owner_id' in properties):
            return str(properties['owner_id']) == str(context.project_id)

        if context.project_id and ('project_id' in properties):
            return str(properties['project_id']) == str(context.project_id)

        try:
            user_id = properties['user_id']
        except KeyError:
            return False

        return str(user_id) == str(context.user_id)

    def _extract_query_params(self, params):
        _params = {}
        accepted_params = ('filters', 'marker', 'limit',
                           'sort_key', 'sort_dir')
        for param in accepted_params:
            if params.get(param):
                _params[param] = params.get(param)

        # ensure filters is a dict
        _params.setdefault('filters', {})
        _params['filters'].setdefault('is_public', 'none')

        return _params

    def get_location(self, context, image_id):
        """Returns the direct url representing the backend storage location,
           or None if this attribute is not shown by Glance."""
        try:
            client = GlanceClientWrapper()
            image_meta = client.call(context, 'get', image_id)
        except Exception:
            _reraise_translated_image_exception(image_id)

        if not self._is_image_available(context, image_meta):
            raise exception.ImageNotFound(image_id=image_id)

        return getattr(image_meta, 'direct_url', None)

    def detail(self, context, **kwargs):
        """Calls out to Glance for a list of detailed image information."""
        params = self._extract_query_params(kwargs)
        try:
            images = self._client.call(context, 'list', **params)
        except Exception:
            _reraise_translated_exception()

        _images = []
        for image in images:
            if self._is_image_available(context, image):
                _images.append(self._translate_from_glance(image))

        return _images

    def show(self, context, image_id):
        """Returns a dict with image data for the given opaque 
           image id or image name."""

        image = None
        try:
            # see if the image_id is image name
            uuid.UUID(encodeutils.safe_decode(image_id))
        except (ValueError, glanceclient.exc.NotFound):
            # try to find the image by name
            matches = self.detail(context, filters={'name': image_id})
            num_matches = len(matches)
            if num_matches:
                image = matches[0]
        if not image:
            try:
                image = self._client.call(context, 'get', image_id)
            except Exception:
                _reraise_translated_image_exception(image_id)

            if not self._is_image_available(context, image):
                raise exception.ImageNotFound(image_id=image_id)

        base_image_meta = self._translate_from_glance(image)
        return base_image_meta

    def delete(self, context, image_id):
        """Delete the given image.

        :raises: ImageNotFound if the image does not exist.
        :raises: NotAuthorized if the user is not an owner.
        :raises: ImageNotAuthorized if the user is not authorized.

        """
        try:
            self._client.call(context, 'delete', image_id)
        except glanceclient.exc.NotFound:
            raise exception.ImageNotFound(image_id=image_id)
        except glanceclient.exc.HTTPForbidden:
            raise exception.ImageNotAuthorized(image_id=image_id)

        return True


class GlanceImageServiceV1(GlanceImageService):
    """Provides storage and retrieval of disk image objects within Glance."""

    def create(self, context, image_meta):
        """Store the image data and return the new image object."""
        sent_service_image_meta = self._translate_to_glance(image_meta)

        try:
            recv_service_image_meta = self._client.call(
                context, 'create', **sent_service_image_meta)
        except glanceclient.exc.HTTPException:
            _reraise_translated_exception()

        return self._translate_from_glance(recv_service_image_meta)

    def download(self, context, image_id, data=None):
        """Calls out to Glance for data and writes data."""

        if 'file' in CONF.allowed_direct_url_schemes:
            location = self.get_location(context, image_id)
            o = urllib.parse.urlparse(location)
            if o.scheme == "file":
                with open(o.path, "r") as f:
                    shutil.copyfileobj(f, data)
                return

        try:
            image_chunks = self._client.call(context, 'data', image_id)
        except Exception:
            _reraise_translated_image_exception(image_id)

        if data is None:
            return image_chunks
        else:
            for chunk in image_chunks:
                data.write(chunk)

    def update(self, context, image_id, image_meta, data=None,
               purge_props=True):
        """Modify the given image with the new data."""
        image_meta = self._translate_to_glance(image_meta)
        image_meta['purge_props'] = purge_props
        image_meta.pop('id', None)
        if data:
            image_meta['data'] = data

        try:
            image_meta = self._client.call(context, 'update',
                                           image_id, **image_meta)
        except Exception:
            _reraise_translated_image_exception(image_id)
        else:
            return self._translate_from_glance(image_meta)


class GlanceImageServiceV2(GlanceImageService):

    def create(self, context, image_meta):
        """Store the image data and return the new image object."""
        sent_service_image_meta = self._translate_to_glance(image_meta)

        sent_service_image_meta.update(sent_service_image_meta.pop('properties', {}))
        sent_service_image_meta.pop('is_public', None)
        sent_service_image_meta.pop('virtual_size', None)
        try:
            recv_service_image_meta = self._client.call(
                context, 'create', **sent_service_image_meta)
        except glanceclient.exc.HTTPException:
            _reraise_translated_exception()

        return self._translate_from_glance(recv_service_image_meta)

    def download(self, context, image_id, data=None):
        try:
            image_chunks = self._client.call(context, 'data', image_id)
        except Exception:
            _reraise_translated_image_exception(image_id)

        if data is None:
            return image_chunks
        else:
            for chunk in image_chunks:
                data.write(chunk)

    def update(self, context, image_id, image_meta, data=None,
               purge_props=False):
        """Modify the given image with the new data."""
        image_meta = self._translate_to_glance(image_meta)
        image_meta.pop('id', None)

        if data:
            upload_retvalue = self._client.call(context, 'upload',
                image_id, data)

        if upload_retvalue == -1:
            raise Exception("Cannot upload image data for %s" % image_id)

        if len(image_meta):
            try:
                image_meta.pop('is_public', None)
                image_meta.pop('properties', None)
                if purge_props == True:
                     image_meta = self._client.call(context, 'update',
                         image_id, remove_props=image_meta)
                else:
                     image_meta = self._client.call(context, 'update',
                         image_id, **image_meta)
            except Exception:
                _reraise_translated_image_exception(image_id)
            else:
                return self._translate_from_glance(image_meta)


def _convert_timestamps_to_datetimes(image_meta):
    """Returns image with timestamp fields converted to datetime objects."""
    for attr in ['created_at', 'updated_at', 'deleted_at']:
        if image_meta.get(attr):
            image_meta[attr] = timeutils.parse_isotime(image_meta[attr])
    return image_meta


def _json_loads(properties, attr):
    prop = properties[attr]
    if isinstance(prop, str):
        properties[attr] = jsonutils.loads(prop)


def _json_dumps(properties, attr):
    prop = properties[attr]
    if not isinstance(prop, str):
        properties[attr] = jsonutils.dumps(prop)


_CONVERT_PROPS = ('block_device_mapping', 'mappings')


def _convert(method, metadata):
    metadata = copy.deepcopy(metadata)
    properties = metadata.get('properties')
    if properties:
        for attr in _CONVERT_PROPS:
            if attr in properties:
                method(properties, attr)

    return metadata


def _convert_from_string(metadata):
    return _convert(_json_loads, metadata)


def _convert_to_string(metadata):
    return _convert(_json_dumps, metadata)


def _extract_attributes(image):
    ATTRIBUTES_TO_REMOVE = ['is_public', 'visibility', 'changes',
                            '__original__', 'resolver', 'schema',
                            'direct_url', 'file', 'virtual_size',
                            'locations',]
    IMAGE_ATTRIBUTES = ['size', 'disk_format', 'owner',
                        'container_format', 'checksum', 'id',
                        'name', 'created_at', 'updated_at',
                        'deleted_at', 'deleted', 'status',
                        'min_disk', 'min_ram',]
    output = {}
    for attr in IMAGE_ATTRIBUTES:
        output[attr] = getattr(image, attr, None)

    
    if CONF.glance_api_version == 1:
        output['properties'] = getattr(image, 'properties', {})
    else:
        output['properties'] = {}
        for attr in list(set(image.keys()) - set(IMAGE_ATTRIBUTES) -\
                            set(ATTRIBUTES_TO_REMOVE)):
            output['properties'][attr] = getattr(image, attr, None)

    return output


def _remove_read_only(image_meta):
    IMAGE_ATTRIBUTES = ['status', 'updated_at', 'created_at', 'deleted_at',
                        'locations']
    output = copy.deepcopy(image_meta)
    for attr in IMAGE_ATTRIBUTES:
        output.pop(attr, None)
        output.get('properties', {}).pop(attr, None)
    return output


def _reraise_translated_image_exception(image_id):
    """Transform the exception for the image but keep its traceback intact."""
    exc_type, exc_value, exc_trace = sys.exc_info()
    new_exc = _translate_image_exception(image_id, exc_value)
    raise new_exc.with_traceback(exc_trace)


def _reraise_translated_exception():
    """Transform the exception but keep its traceback intact."""
    exc_type, exc_value, exc_trace = sys.exc_info()
    new_exc = _translate_plain_exception(exc_value)
    raise new_exc.with_traceback(exc_trace)


def _translate_image_exception(image_id, exc_value):
    if isinstance(exc_value, (glanceclient.exc.Forbidden,
                              glanceclient.exc.Unauthorized)):
        return exception.ImageNotAuthorized(image_id=image_id)
    if isinstance(exc_value, glanceclient.exc.NotFound):
        return exception.ImageNotFound(image_id=image_id)
    if isinstance(exc_value, glanceclient.exc.BadRequest):
        return exception.Invalid(exc_value)
    return exc_value


def _translate_plain_exception(exc_value):
    if isinstance(exc_value, (glanceclient.exc.Forbidden,
                              glanceclient.exc.Unauthorized)):
        return exception.NotAuthorized(exc_value)
    if isinstance(exc_value, glanceclient.exc.NotFound):
        return exception.NotFound(exc_value)
    if isinstance(exc_value, glanceclient.exc.BadRequest):
        return exception.Invalid(exc_value)
    return exc_value


def get_remote_image_service(context, image_href, production=True):
    """Create an image_service and parse the id from the given image_href.

    The image_href param can be an href of the form
    'http://example.com:9292/v1/images/b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3',
    or just an id such as 'b8b2c6f7-7345-4e2f-afa2-eedaba9cbbe3'. If the
    image_href is a standalone id, then the default image service is returned.

    :param image_href: href that describes the location of an image
    :returns: a tuple of the form (image_service, image_id)

    """
    # Calling out to another service may take a while, so lets log this
    LOG.debug(_("fetching image %s from glance") % image_href)
    # NOTE(bcwaldon): If image_href doesn't look like a URI, assume its a
    # standalone image ID
    if '/' not in str(image_href):
        image_service = get_default_image_service(production)
        return image_service, image_href

    try:
        (image_id, glance_host, glance_port, use_ssl) = \
            _parse_image_ref(image_href)
        glance_client = GlanceClientWrapper(
            context=context,
            host=glance_host,
            port=glance_port,
            use_ssl=use_ssl)
    except ValueError:
        raise exception.InvalidImageRef(image_href=image_href)

    image_service = GlanceImageService(
        client=glance_client, production=production)
    return image_service, image_id


def get_default_image_service(production=True):
    if CONF.glance_api_version == 1:
        return GlanceImageServiceV1(production=True)
    else:
        return GlanceImageServiceV2(production=True)