openstack-nova添加镜像预分发功能

来源:互联网 发布:权限控制代码 java 编辑:程序博客网 时间:2024/06/05 10:35

本博客详细介绍在nova中如何添加自定义功能,并以镜像预分发为例详细介绍。

所谓镜像预分发就是在nova创建instance实例之前,将用于创建实例的镜像提前分发到各个计算节点上,以缩短创建实例的时间。尤其是在大规模的创建实例时,可以明显提升创建效率。

1、分析

首先,镜像相关的操作,可以仿照nova image-list,然后看nova image-list的操作流程,其实整个nova组件的调用流程是这样的:

novaclient-------->>控制节点的nova------>>计算节点nova

                 直接API调用                   RPC调用

base.py                             rpcapi.py                 manager.py

1)nova image-list流程:

客户端的入口---->>novaclient/v2/shell.py:

@cliutils.arg(    '--limit',    dest="limit",    metavar="<limit>",    help=_('Number of images to return per request.'))def do_image_list(cs, _args):    """Print a list of available images to boot from."""    limit = _args.limit    image_list = cs.images.list(limit=limit)    def parse_server_name(image):        try:            return image.server['id']        except (AttributeError, KeyError):            return ''    fmts = {'Server': parse_server_name}    utils.print_list(image_list, ['ID', 'Name', 'Status', 'Server'],                     fmts, sortby_index=1)

其中:image_list = cs.images.list(limit=limit) 

调用到novaclient/images.py 的list()

def list(self, detailed=True, limit=None, marker=None):    """    Get a list of all images.    :rtype: list of :class:`Image`    :param limit: maximum number of images to return.    :param marker: Begin returning images that appear later in the image                   list than that represented by this image id (optional).    """    params = {}    detail = ''    if detailed:        detail = '/detail'    if limit:        params['limit'] = int(limit)    if marker:        params['marker'] = str(marker)    params = sorted(params.items(), key=lambda x: x[0])    query = '?%s' % parse.urlencode(params) if params else ''    return self._list('/images%s%s' % (detail, query), 'images')

调用_list——>base.py

def _list(self, url, response_key, obj_class=None, body=None):    if body:        _resp, body = self.api.client.post(url, body=body)    else:        _resp, body = self.api.client.get(url)    if obj_class is None:        obj_class = self.resource_class    data = body[response_key]    # NOTE(ja): keystone returns values as list as {'values': [ ... ]}    #           unlike other services which just return the list...    if isinstance(data, dict):        try:            data = data['values']        except KeyError:            pass    with self.completion_cache('human_id', obj_class, mode="w"):        with self.completion_cache('uuid', obj_class, mode="w"):            return [obj_class(self, res, loaded=True)                    for res in data if res]

_resp, body = self.api.client.get(url)

所有的image信息放在返回的body中,继续代码 api.client.get(url)

现在发出信息将在nova中被捕获并执行———>>nova.api.openstack.computer.images.py

@extensions.expected_errors(400)def index(self, req):    """Return an index listing of images available to the request.    :param req: `wsgi.Request` object    """    context = req.environ['nova.context']    filters = self._get_filters(req)    page_params = common.get_pagination_params(req)    try:        images = self._image_api.get_all(context, filters=filters,                                         **page_params)    except exception.Invalid as e:        raise webob.exc.HTTPBadRequest(explanation=e.format_message())    return self._view_builder.index(req, images)
>>>_image_api.get_all()
nova.image.api.py
def get_all(self, context, **kwargs):    """Retrieves all information records about all disk images available    to show to the requesting user. If the requesting user is an admin,    all images in an ACTIVE status are returned. If the requesting user    is not an admin, the all public images and all private images that    are owned by the requesting user in the ACTIVE status are returned.    :param context: The `nova.context.Context` object for the request    :param kwargs: A dictionary of filter and pagination values that                   may be passed to the underlying image info driver.    """    session = self._get_session(context)    return session.detail(context, **kwargs)

nova.image.glance.py

def detail(self, context, **kwargs):    """Calls out to Glance for a list of detailed image information."""    params = _extract_query_params(kwargs)    try:        images = self._client.call(context, 1, 'list', **params)    except Exception:        _reraise_translated_exception()    _images = []    for image in images:        if _is_image_available(context, image):            _images.append(_translate_from_glance(image))    return _images

在_images中返回全部的image信息。

好了,类似nova image-list的方法也可以实现image预分发了。

不再详述,因为····机密


0 0
原创粉丝点击