¡@

Home 

OpenStack Study: store_type.py

OpenStack Index

**** CubicPower OpenStack Study ****

def get_strategy_name():

    """Return strategy module name."""

    return 'store_type'

**** CubicPower OpenStack Study ****

def init():

    """Initialize strategy module."""

    # NOTE(zhiyan): We have a plan to do a reusable glance client library for

    # all clients like Nova and Cinder in near period, it would be able to

    # contains common code to provide uniform image service interface for them,

    # just like Brick in Cinder, this code can be moved to there and shared

    # between Glance and client both side. So this implementation as far as

    # possible to prevent make relationships with Glance(server)-specific code,

    # for example: using functions within store module to validate

    # 'store_type_preference' option.

    mapping = {'filesystem': ['file', 'filesystem'],

               'http': ['http', 'https'],

               'rbd': ['rbd'],

               's3': ['s3', 's3+http', 's3+https'],

               'swift': ['swift', 'swift+https', 'swift+http'],

               'gridfs': ['gridfs'],

               'sheepdog': ['sheepdog'],

               'cinder': ['cinder'],

               'vmware_datastore': ['vsphere']}

    _STORE_TO_SCHEME_MAP.clear()

    _STORE_TO_SCHEME_MAP.update(mapping)

**** CubicPower OpenStack Study ****

def get_ordered_locations(locations, uri_key='url', **kwargs):

    """

    Order image location list.

    :param locations: The original image location list.

    :param uri_key: The key name for location URI in image location dictionary.

    :return: The image location list with preferred store type order.

    """

    def _foreach_store_type_preference():

        store_types = CONF.store_type_location_strategy.store_type_preference

        for preferred_store in store_types:

            preferred_store = str(preferred_store).strip()

            if not preferred_store:

                continue

            yield preferred_store

    if not locations:

        return locations

    preferences = {}

    others = []

    for preferred_store in _foreach_store_type_preference():

        preferences[preferred_store] = []

    for location in locations:

        uri = location.get(uri_key)

        if not uri:

            continue

        pieces = urlparse.urlparse(uri.strip())

        store_name = None

        for store, schemes in six.iteritems(_STORE_TO_SCHEME_MAP):

            if pieces.scheme.strip() in schemes:

                store_name = store

                break

        if store_name in preferences:

            preferences[store_name].append(location)

        else:

            others.append(location)

    ret = []

    # NOTE(zhiyan): While configuration again since py26 does not support

    # ordereddict container.

    for preferred_store in _foreach_store_type_preference():

        ret.extend(preferences[preferred_store])

    ret.extend(others)

    return ret