nova_api create,源码跟读

def create(self, req, body):
        """Creates a new server for a given user."""


        return self._create_server(req, body)

    def _create_server(self, req, body, action='create'):
        context = req.environ['nova.context']# 请求参数
        server_dict = body['server'] #虚拟机创建参数,一般会在context里面
        password = self._get_server_admin_password(server_dict) #获取密码,如果未在参数指定则生成随机
        name = common.normalize_name(server_dict['name'])#去除虚拟的名称空格,strip()

        if api_version_request.is_supported(req, min_version='2.19'):#目前环境api是2.64
            if 'description' in server_dict:
                # This is allowed to be None
                description = server_dict['description']
            else:
                # No default description
                description = None
        else:
            description = name

        # Arguments to be passed to instance create function
        create_kwargs = {}# 虚拟机的创建参数


        # Query extensions which want to manipulate the keyword
        # arguments.
        # NOTE(cyeoh): This is the hook that extensions use
        # to replace the extension specific code below.
        # When the extensions are ported this will also result
        # in some convenience function from this class being
        # moved to the extension
        if list(self.create_extension_manager):#stevedon 加载了指定的create extnesion 
            self.create_extension_manager.map(self._create_extension_point,
                                              server_dict, create_kwargs, body)
        #map会运行self._create_extension_point(server_dict,create_kwargs,body) 
        # 调用到nova.avalibility_zone.py:server_create
        # 作用是填充 availability_zone
        availability_zone = create_kwargs.pop("availability_zone", None)

        create_kwargs['action'] = action
        if action == 'import':
            create_kwargs['path'] = server_dict['path']

        if api_version_request.is_supported(req, min_version='2.52'):
            create_kwargs['tags'] = server_dict.get('tags')
        # 搞的translate,就是把server_dict 的参数转到create_kwargs
        helpers.translate_attributes(helpers.CREATE,
                                     server_dict, create_kwargs)
        
        #这个就是我们的身份信息啦
        target = {
            'project_id': context.project_id,
            'user_id': context.user_id,
            'availability_zone': availability_zone}
        # 这个权限这个不好读,就是验证我是否有server:create的权限
        context.can(server_policies.SERVERS % action, target)

        # TODO(Shao He, Feng) move this policy check to os-availability-zone
        # extension after refactor it.
        parse_az = self.compute_api.parse_availability_zone
        try:
            availability_zone, host, node = parse_az(context,
                                                     availability_zone)
            # availability_zone = 默认资源群集 host,node 未指定
        except exception.InvalidInput as err:
            raise exc.HTTPBadRequest(explanation=six.text_type(err))
        if host or node:
            context.can(server_policies.SERVERS % 'create:forced_host', {})
        # 这个如何获取的自己看吧,头疼 
        min_compute_version = objects.Service.get_minimum_version(
            nova_context.get_admin_context(), 'nova-compute')
        # 最小版本得大于tagging最小版本 支持啊
        supports_device_tagging = (min_compute_version >=
                                   DEVICE_TAGGING_MIN_COMPUTE_VERSION)
        #获取参数里的块设备映射信息 有啊
        block_device_mapping = create_kwargs.get("block_device_mapping")
        # TODO(Shao He, Feng) move this policy check to os-block-device-mapping
        # extension after refactor it.
        if block_device_mapping:
            # 看看能不能attach_volume
            context.can(server_policies.SERVERS % 'create:attach_volume',
                        target)
            for bdm in block_device_mapping:
                if bdm.get('tag', None) and not supports_device_tagging:
                    msg = _('Block device tags are not yet supported.')
                    raise exc.HTTPBadRequest(explanation=msg)

        image_uuid = self._image_from_req_data(server_dict, create_kwargs)
        # 返回了''
        # NOTE(cyeoh): Although an extension can set
        # return_reservation_id in order to request that a reservation
        # id be returned to the client instead of the newly created
        # instance information we do not want to pass this parameter
        # to the compute create call which always returns both. We use
        # this flag after the instance create call to determine what
        # to return to the client
        return_reservation_id = create_kwargs.pop('return_reservation_id',
                                                  False)
        # 返回false
        requested_networks = None
        #查看是否加载os-networks 
        if ('os-networks' in self.extension_info.get_extensions()
                or utils.is_neutron()):
            #加载了就把请求网络从请求参数里拿出来
            requested_networks = server_dict.get('networks')

        if requested_networks is not None:
            requested_networks = self._get_requested_networks(
                requested_networks, supports_device_tagging)

        if requested_networks and len(requested_networks):
            #查看能不能attach
            context.can(server_policies.SERVERS % 'create:attach_network',
                        target)
        #从flavorRef 里 提取/分割的最后一个元素就是id
        flavor_id = self._flavor_id_from_req_data(body)
        try:
            # 获取到flavor 信息 
            inst_type = flavors.get_flavor_by_flavor_id(
                    flavor_id, ctxt=context, read_deleted="no")
            #检查是否可以多挂载 2.64>2.60  
            supports_multiattach = common.supports_multiattach_volume(req)
            # 调用compute_api 进入nova/compute/api.py
            (instances, resv_id) = self.compute_api.create(context,
                            inst_type,
                            image_uuid,
                            display_name=name,
                            display_description=description,
                            availability_zone=availability_zone,
                            forced_host=host, forced_node=node,
                            metadata=server_dict.get('metadata', {}),
                            admin_password=password,
                            requested_networks=requested_networks,
                            check_server_group_quota=True,
                            supports_multiattach=supports_multiattach,
                            **create_kwargs)

你可能感兴趣的:(nova_api create,源码跟读)