本帖最后由 xioaxu790 于 2014-6-12 13:48 编辑
问题导读:
1、建立类型的实例都要调用哪些方法?
2、如何理解run_instances方法?
现在开始,开始分析great这个最重要的方法。我们回到第一篇文章中的run_instances方法:
- def run_instances(self, context, **kwargs):
- """
- 准备实例,并且发送实例的信息和要运行实例的请求消息到远程调度器scheduler;
- 实现实例的简历和运行,由调度器完成;
- """
-
- ......
-
- # create:准备实例,并且发送实例的信息和要运行实例的请求消息到远程调度器scheduler;
- # 实现实例的简历和运行,由调度器完成,这部分代码实际上只是实现请求消息的发送;
- (instances, resv_id) = self.compute_api.create(context,
- # get_instance_type_by_name:通过给定的name检索单个实例类型信息;
- # 以字典的形式返回查询结果;
- instance_type=instance_types.get_instance_type_by_name(kwargs.get('instance_type', None)),
- image_href=image_uuid,
- max_count=int(kwargs.get('max_count', min_count)),
- min_count=min_count,
- kernel_id=kwargs.get('kernel_id'),
- ramdisk_id=kwargs.get('ramdisk_id'),
- key_name=kwargs.get('key_name'),
- user_data=kwargs.get('user_data'),
- security_group=kwargs.get('security_group'),
- availability_zone=kwargs.get('placement', {}).get('availability_zone'),
- block_device_mapping=kwargs.get('block_device_mapping', {}))
- return self._format_run_instances(context, resv_id)
复制代码
这里调用的是/nova/compute/api.py中的creat方法,这是建立所有类型的实例都要调用的方法;
- def create(self, context, instance_type,
- image_href, kernel_id=None, ramdisk_id=None,
- min_count=None, max_count=None,
- display_name=None, display_description=None,
- key_name=None, key_data=None, security_group=None,
- availability_zone=None, user_data=None, metadata=None,
- injected_files=None, admin_password=None,
- block_device_mapping=None, access_ip_v4=None,
- access_ip_v6=None, requested_networks=None, config_drive=None,
- auto_disk_config=None, scheduler_hints=None):
- """
- 准备实例,并且发送实例的信息和要运行实例的请求消息到远程调度器scheduler;
- 实现实例的简历和运行,由调度器完成,这部分代码实际上只是实现请求消息的发送;
- 返回一个元组(实例或者是reservation_id的元组),元组里面的实例可以是“None”或者是实例字典的一个列表,这要取决于是否等待scheduler返回的信息;
- """
-
- self._check_create_policies(context, availability_zone,requested_networks, block_device_mapping)
-
- # 验证所有的输入实例参数;
- # 发送要运行实例('run_instance')的请求消息到远程调度器;
- return self._create_instance(
- context, instance_type,
- image_href, kernel_id, ramdisk_id,
- min_count, max_count,
- display_name, display_description,
- key_name, key_data, security_group,
- availability_zone, user_data, metadata,
- injected_files, admin_password,
- access_ip_v4, access_ip_v6,
- requested_networks, config_drive,
- block_device_mapping, auto_disk_config,
- scheduler_hints=scheduler_hints)
复制代码
这里涉及到的参数太多,想看看具体的参数值都是什么,应用命令行:
- nova boot --flavor 2 --key_name oskey --image 20612b24-c980-4900-b270-8e6b66e5f72f test3
复制代码
调试运行了一下,输出的参数值如下:
- context = <nova.context.RequestContext object at 0x447d050>
- instance_type = {'memory_mb': 2048L, 'root_gb': 20L, 'deleted_at': None, 'name': u'm1.small', 'deleted': 0L, 'created_at': None, 'ephemeral_gb': 0L, 'updated_at': None, 'disabled': False, 'vcpus': 1L, 'extra_specs': {}, 'swap': 0L, 'rxtx_factor': 1.0, 'is_public': True, 'flavorid': u'2', 'vcpu_weight': None, 'id': 5L}
- image_href = 20612b24-c980-4900-b270-8e6b66e5f72f
- kernel_id = None
- ramdisk_id = None
- min_count = 1
- max_count = 1
- display_name = test3
- display_description = test3
- key_name = oskey
- key_data = None
- security_group = ['default']
- availability_zone = None
- user_data = None
- metadata = {}
- injected_files = []
- admin_password = Piu4aSSNmSNk
- block_device_mapping = []
- access_ip_v4 = None
- access_ip_v6 = None
- requested_networks = None
- config_drive = None
- auto_disk_config = None
- scheduler_hints = {}
复制代码
可以看到context这个上下文信息对象,是由类nova.context.RequestContext实例化来的,那我们具体看一下,这个上下文运行环境都包括哪些信息:
- class RequestContext(object):
-
- def __init__(self, user_id, project_id, is_admin=None, read_deleted="no",
- roles=None, remote_address=None, timestamp=None,
- request_id=None, auth_token=None, overwrite=True,
- quota_class=None, user_name=None, project_name=None,
- service_catalog=None, instance_lock_checked=False, **kwargs):
-
- if kwargs:
- LOG.warn(_('Arguments dropped when creating context: %s') %
- str(kwargs))
-
- self.user_id = user_id
- self.project_id = project_id
- self.roles = roles or []
- self.read_deleted = read_deleted
- self.remote_address = remote_address
- if not timestamp:
- timestamp = timeutils.utcnow()
- if isinstance(timestamp, basestring):
- timestamp = timeutils.parse_strtime(timestamp)
- self.timestamp = timestamp
- if not request_id:
- request_id = generate_request_id()
- self.request_id = request_id
- self.auth_token = auth_token
-
- if service_catalog:
- self.service_catalog = [s for s in service_catalog
- if s.get('type') in ('volume')]
- else:
- self.service_catalog = []
-
- self.instance_lock_checked = instance_lock_checked
-
- self.quota_class = quota_class
- self.user_name = user_name
- self.project_name = project_name
- self.is_admin = is_admin
- if self.is_admin is None:
- self.is_admin = policy.check_is_admin(self)
- if overwrite or not hasattr(local.store, 'context'):
- self.update_store()
-
- ......
复制代码
我们在看看在这里context中的信息具体值都是什么,会更加有助我们理解context,调试运行一下:
- user_id = afc380206e2549ad930396d9050d20cf
- project_id = 0e492e86f22e4d19bd523f1e7ca64566
- roles = [u'admin', u'KeystoneAdmin', u'KeystoneServiceAdmin']
- read_deleted = no
- remote_address = 172.21.6.145
- timestamp = 2013-06-23 16:36:37.399405
- request_id = req-f0255b14-833d-4fff-b973-23c35f70ddda
- auth_token = 7e7bb3cf84ab43269010bb55410064b3
- service_catalog = [{u'endpoints': [{u'adminURL': u'http://172.21.5.161:8776/v1/0e492e86f22e4d19bd523f1e7ca64566', u'region': u'RegionOne', u'id': u'753a1ad55e91469794e2eb7ac4c3df92', u'internalURL': u'http://172.21.5.161:8776/v1/0e492e86f22e4d19bd523f1e7ca64566', u'publicURL': u'http://172.21.6.145:8776/v1/0e492e86f22e4d19bd523f1e7ca64566'}], u'endpoints_links': [], u'type': u'volume', u'name': u'cinder'}]
- instance_lock_checked = False
- quota_class = None
- user_name = admin
- project_name = admin
- is_admin = True
复制代码
注意,本文接着上一篇文章:
OpenStack建立实例完整过程源码详细分析(4)
下一篇,内容:OpenStack建立实例完整过程源码详细分析(6)
|