oenstack nova 源码分析

来源:互联网 发布:numbers for mac 编辑:程序博客网 时间:2024/05/17 09:23

1.创建虚拟机

  def create(self, req, body):        """Creates a new server for a given user."""        context = req.environ['nova.context']        server_dict = body['server']        password = self._get_server_admin_password(server_dict)        name = common.normalize_name(server_dict['name'])        if api_version_request.is_supported(req, min_version='2.19'):            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):            self.create_extension_manager.map(self._create_extension_point,                                              server_dict, create_kwargs, body)        availability_zone = create_kwargs.pop("availability_zone", None)<span style="white-space:pre"></span># 将<span style="white-space:pre"></span><span style="font-family: Arial, Helvetica, sans-serif;">server_dict 里的一些参数转入</span><span style="font-family: Arial, Helvetica, sans-serif;">create_kwargs中,还涉及到注入文件</span>
<span style="white-space:pre"></span>helpers.translate_attributes(helpers.CREATE,                                     server_dict, create_kwargs)        target = {            'project_id': context.project_id,            'user_id': context.user_id,            'availability_zone': availability_zone}
<span style="white-space:pre"></span># can 是?        context.can(server_policies.SERVERS % 'create', 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)        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')        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:            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)
<span style="white-space:pre"></span>#<span style="font-family: Arial, Helvetica, sans-serif;">image_uuid </span><span style="font-family: Arial, Helvetica, sans-serif;"> </span>        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)        requested_networks = None        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):            context.can(server_policies.SERVERS % 'create:attach_network',                        target)        try:            <strong>flavor_id</strong> = self._flavor_id_from_req_data(body)        except ValueError:            msg = _("Invalid flavorRef provided.")            raise exc.HTTPBadRequest(explanation=msg)        try:            inst_type = flavors.get_flavor_by_flavor_id(                    flavor_id, ctxt=context, read_deleted="no")<span style="white-space:pre"></span>    # 调用compute组件的api.py            (instances, resv_id) = <strong><em>self.compute_api.create</em></strong>(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,                            **create_kwargs)        except ..........<span style="white-space:pre"></span>.......        # If the caller wanted a reservation_id, return it        if return_reservation_id:            # NOTE(cyeoh): In v3 reservation_id was wrapped in            # servers_reservation but this is reverted for V2 API            # compatibility. In the long term with the tasks API we            # will probably just drop the concept of reservation_id            return wsgi.ResponseObject({'reservation_id': resv_id})        req.cache_db_instances(instances)        server = self._view_builder.create(req, instances[0])        if CONF.enable_instance_password:            server['server']['adminPass'] = password        robj = wsgi.ResponseObject(server)        return self._add_location(robj)


0 0