本文整理汇总了Python中nova.cells.opts.get_cell_type函数的典型用法代码示例。如果您正苦于以下问题:Python get_cell_type函数的具体用法?Python get_cell_type怎么用?Python get_cell_type使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_cell_type函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _save_helper
def _save_helper(self, cell_type, update_cells):
obj = instance_info_cache.InstanceInfoCache()
cells_api = cells_rpcapi.CellsAPI()
self.mox.StubOutWithMock(db, 'instance_info_cache_update')
self.mox.StubOutWithMock(cells_opts, 'get_cell_type')
self.mox.StubOutWithMock(cells_rpcapi, 'CellsAPI',
use_mock_anything=True)
self.mox.StubOutWithMock(cells_api,
'instance_info_cache_update_at_top')
nwinfo = network_model.NetworkInfo.hydrate([{'address': 'foo'}])
new_info_cache = fake_info_cache.copy()
new_info_cache['network_info'] = nwinfo.json()
db.instance_info_cache_update(
self.context, 'fake-uuid',
{'network_info': nwinfo.json()}).AndReturn(new_info_cache)
if update_cells:
cells_opts.get_cell_type().AndReturn(cell_type)
if cell_type == 'compute':
cells_rpcapi.CellsAPI().AndReturn(cells_api)
cells_api.instance_info_cache_update_at_top(
self.context, 'foo')
self.mox.ReplayAll()
obj._context = self.context
obj.instance_uuid = 'fake-uuid'
obj.network_info = nwinfo
obj.save(update_cells=update_cells)
开发者ID:Milstein,项目名称:nova,代码行数:27,代码来源:test_instance_info_cache.py
示例2: destroy
def destroy(self):
if not self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='destroy',
reason='already destroyed')
if not self.obj_attr_is_set('uuid'):
raise exception.ObjectActionError(action='destroy',
reason='no uuid')
if not self.obj_attr_is_set('host') or not self.host:
# NOTE(danms): If our host is not set, avoid a race
constraint = db.constraint(host=db.equal_any(None))
else:
constraint = None
cell_type = cells_opts.get_cell_type()
if cell_type is not None:
stale_instance = self.obj_clone()
try:
db_inst = db.instance_destroy(self._context, self.uuid,
constraint=constraint)
self._from_db_object(self._context, self, db_inst)
except exception.ConstraintNotMet:
raise exception.ObjectActionError(action='destroy',
reason='host changed')
if cell_type == 'compute':
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_destroy_at_top(self._context, stale_instance)
delattr(self, base.get_attrname('id'))
开发者ID:amatuerone,项目名称:nova,代码行数:28,代码来源:instance.py
示例3: _info_cache_cells_update
def _info_cache_cells_update(ctxt, info_cache):
cell_type = cells_opts.get_cell_type()
if cell_type != "compute":
return
cells_api = cells_rpcapi.CellsAPI()
try:
cells_api.instance_info_cache_update_at_top(ctxt, info_cache)
except Exception:
LOG.exception(_LE("Failed to notify cells of instance info " "cache update"))
开发者ID:mathslinux,项目名称:nova,代码行数:9,代码来源:instance_info_cache.py
示例4: _metadata_as_json
def _metadata_as_json(self, version, path):
metadata = {'uuid': self.uuid}
if self.launch_metadata:
metadata['meta'] = self.launch_metadata
if self.files:
metadata['files'] = self.files
if self.extra_md:
metadata.update(self.extra_md)
if self.network_config:
metadata['network_config'] = self.network_config
if self.instance.key_name:
if cells_opts.get_cell_type() == 'compute':
cells_api = cells_rpcapi.CellsAPI()
keypair = cells_api.get_keypair_at_top(
context.get_admin_context(), self.instance.user_id,
self.instance.key_name)
else:
keypairs = self.instance.keypairs
# NOTE(mriedem): It's possible for the keypair to be deleted
# before it was migrated to the instance_extra table, in which
# case lazy-loading instance.keypairs will handle the 404 and
# just set an empty KeyPairList object on the instance.
keypair = keypairs[0] if keypairs else None
if keypair:
metadata['public_keys'] = {
keypair.name: keypair.public_key,
}
metadata['keys'] = [
{'name': keypair.name,
'type': keypair.type,
'data': keypair.public_key}
]
else:
LOG.debug("Unable to find keypair for instance with "
"key name '%s'.", self.instance.key_name,
instance=self.instance)
metadata['hostname'] = self._get_hostname()
metadata['name'] = self.instance.display_name
metadata['launch_index'] = self.instance.launch_index
metadata['availability_zone'] = self.availability_zone
if self._check_os_version(GRIZZLY, version):
metadata['random_seed'] = base64.b64encode(os.urandom(512))
if self._check_os_version(LIBERTY, version):
metadata['project_id'] = self.instance.project_id
if self._check_os_version(NEWTON_ONE, version):
metadata['devices'] = self._get_device_metadata()
self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return jsonutils.dump_as_bytes(metadata)
开发者ID:2020human,项目名称:nova,代码行数:56,代码来源:base.py
示例5: _metadata_as_json
def _metadata_as_json(self, version, path):
metadata = {'uuid': self.uuid}
if self.launch_metadata:
metadata['meta'] = self.launch_metadata
if self.files:
metadata['files'] = self.files
if self.extra_md:
metadata.update(self.extra_md)
if self.network_config:
metadata['network_config'] = self.network_config
if self.instance.key_name:
metadata['public_keys'] = {
self.instance.key_name: self.instance.key_data
}
if cells_opts.get_cell_type() == 'compute':
cells_api = cells_rpcapi.CellsAPI()
keypair = cells_api.get_keypair_at_top(
context.get_admin_context(), self.instance.user_id,
self.instance.key_name)
else:
try:
keypair = keypair_obj.KeyPair.get_by_name(
context.get_admin_context(), self.instance.user_id,
self.instance.key_name)
except exception.KeypairNotFound:
# NOTE(mriedem): If the keypair was deleted from under us
# don't totally fail the request, just treat it as if the
# instance.key_name wasn't set.
keypair = None
if keypair:
metadata['keys'] = [
{'name': keypair.name,
'type': keypair.type,
'data': keypair.public_key}
]
else:
LOG.debug("Unable to find keypair for instance with "
"key name '%s'.", self.instance.key_name,
instance=self.instance)
metadata['hostname'] = self._get_hostname()
metadata['name'] = self.instance.display_name
metadata['launch_index'] = self.instance.launch_index
metadata['availability_zone'] = self.availability_zone
if self._check_os_version(GRIZZLY, version):
metadata['random_seed'] = base64.b64encode(os.urandom(512))
if self._check_os_version(LIBERTY, version):
metadata['project_id'] = self.instance.project_id
self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return jsonutils.dump_as_bytes(metadata)
开发者ID:Snergster,项目名称:virl-salt,代码行数:55,代码来源:nova+api+metadata+base.py
示例6: _save_helper
def _save_helper(self, cell_type, update_cells):
obj = instance_info_cache.InstanceInfoCache()
cells_api = cells_rpcapi.CellsAPI()
self.mox.StubOutWithMock(db, "instance_info_cache_update")
self.mox.StubOutWithMock(cells_opts, "get_cell_type")
self.mox.StubOutWithMock(cells_rpcapi, "CellsAPI", use_mock_anything=True)
self.mox.StubOutWithMock(cells_api, "instance_info_cache_update_at_top")
nwinfo = network_model.NetworkInfo.hydrate([{"address": "foo"}])
db.instance_info_cache_update(self.context, "fake-uuid", {"network_info": nwinfo.json()}).AndReturn("foo")
if update_cells:
cells_opts.get_cell_type().AndReturn(cell_type)
if cell_type == "compute":
cells_rpcapi.CellsAPI().AndReturn(cells_api)
cells_api.instance_info_cache_update_at_top(self.context, "foo")
self.mox.ReplayAll()
obj._context = self.context
obj.instance_uuid = "fake-uuid"
obj.network_info = nwinfo
obj.save(update_cells=update_cells)
开发者ID:jettang,项目名称:icehouse,代码行数:20,代码来源:test_instance_info_cache.py
示例7: save
def save(self, context):
updates = self.obj_get_changes()
if "instance" in updates:
raise exception.ObjectActionError(action="save", reason="instance changed")
updates.pop("id", None)
updated = db.block_device_mapping_update(self._context, self.id, updates, legacy=False)
self._from_db_object(context, self, updated)
cell_type = cells_opts.get_cell_type()
if cell_type == "compute":
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(context, self)
开发者ID:dtroyer,项目名称:nova,代码行数:11,代码来源:block_device.py
示例8: destroy
def destroy(self):
if not self.obj_attr_is_set("id"):
raise exception.ObjectActionError(action="destroy", reason="already destroyed")
db.block_device_mapping_destroy(self._context, self.id)
delattr(self, base.get_attrname("id"))
cell_type = cells_opts.get_cell_type()
if cell_type == "compute":
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_destroy_at_top(
self._context, self.instance_uuid, device_name=self.device_name, volume_id=self.volume_id
)
开发者ID:EnKalvi,项目名称:nova,代码行数:12,代码来源:block_device.py
示例9: save
def save(self):
updates = self.obj_get_changes()
if 'instance' in updates:
raise exception.ObjectActionError(action='save',
reason='instance changed')
updates.pop('id', None)
updated = db.block_device_mapping_update(self._context, self.id,
updates, legacy=False)
self._from_db_object(self._context, self, updated)
cell_type = cells_opts.get_cell_type()
if cell_type == 'compute':
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(self._context, self)
开发者ID:drunkensailor2010,项目名称:nova,代码行数:13,代码来源:block_device.py
示例10: _create
def _create(self, context, update_or_create=False):
"""Create the block device record in the database.
In case the id field is set on the object, and if the instance is set
raise an ObjectActionError. Resets all the changes on the object.
Returns None
:param context: security context used for database calls
:param update_or_create: consider existing block devices for the
instance based on the device name and swap, and only update
the ones that match. Normally only used when creating the
instance for the first time.
"""
cell_type = cells_opts.get_cell_type()
if cell_type == 'api':
raise exception.ObjectActionError(
action='create',
reason='BlockDeviceMapping cannot be '
'created in the API cell.')
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
updates = self.obj_get_changes()
if 'instance' in updates:
raise exception.ObjectActionError(action='create',
reason='instance assigned')
cells_create = update_or_create or None
if update_or_create:
db_bdm = db.block_device_mapping_update_or_create(
context, updates, legacy=False)
else:
db_bdm = db.block_device_mapping_create(
context, updates, legacy=False)
self._from_db_object(context, self, db_bdm)
# NOTE(alaski): bdms are looked up by instance uuid and device_name
# so if we sync up with no device_name an entry will be created that
# will not be found on a later update_or_create call and a second bdm
# create will occur.
if cell_type == 'compute' and db_bdm.get('device_name') is not None:
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(
context, self, create=cells_create)
开发者ID:ruslanloman,项目名称:nova,代码行数:46,代码来源:block_device.py
示例11: _metadata_as_json
def _metadata_as_json(self, version, path):
metadata = {'uuid': self.uuid}
if self.launch_metadata:
metadata['meta'] = self.launch_metadata
if self.files:
metadata['files'] = self.files
if self.extra_md:
metadata.update(self.extra_md)
if self.network_config:
metadata['network_config'] = self.network_config
if self.instance.key_name:
metadata['public_keys'] = {
self.instance.key_name: self.instance.key_data
}
if cells_opts.get_cell_type() == 'compute':
cells_api = cells_rpcapi.CellsAPI()
keypair = cells_api.get_keypair_at_top(
context.get_admin_context(), self.instance.user_id,
self.instance.key_name)
else:
keypair = keypair_obj.KeyPair.get_by_name(
context.get_admin_context(), self.instance.user_id,
self.instance.key_name)
metadata['keys'] = [
{'name': keypair.name,
'type': keypair.type,
'data': keypair.public_key}
]
metadata['hostname'] = self._get_hostname()
metadata['name'] = self.instance.display_name
metadata['launch_index'] = self.instance.launch_index
metadata['availability_zone'] = self.availability_zone
if self._check_os_version(GRIZZLY, version):
metadata['random_seed'] = base64.b64encode(os.urandom(512))
if self._check_os_version(LIBERTY, version):
metadata['project_id'] = self.instance.project_id
self.set_mimetype(MIME_TYPE_APPLICATION_JSON)
return jsonutils.dump_as_bytes(metadata)
开发者ID:chenglinlee,项目名称:nova,代码行数:43,代码来源:base.py
示例12: save
def save(self):
updates = self.obj_get_changes()
if "instance" in updates:
raise exception.ObjectActionError(action="save", reason="instance changed")
updates.pop("id", None)
updated = db.block_device_mapping_update(self._context, self.id, updates, legacy=False)
if not updated:
raise exception.BDMNotFound(id=self.id)
self._from_db_object(self._context, self, updated)
cell_type = cells_opts.get_cell_type()
if cell_type == "compute":
create = False
# NOTE(alaski): If the device name has just been set this bdm
# likely does not exist in the parent cell and we should create it.
# If this is a modification of the device name we should update
# rather than create which is why None is used here instead of True
if "device_name" in updates:
create = None
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(self._context, self, create=create)
开发者ID:EnKalvi,项目名称:nova,代码行数:20,代码来源:block_device.py
示例13: create
def create(self, context):
cell_type = cells_opts.get_cell_type()
if cell_type == 'api':
raise exception.ObjectActionError(
action='create',
reason='BlockDeviceMapping cannot be '
'created in the API cell.')
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
updates = self.obj_get_changes()
if 'instance' in updates:
raise exception.ObjectActionError(action='create',
reason='instance assigned')
db_bdm = db.block_device_mapping_create(context, updates, legacy=False)
self._from_db_object(context, self, db_bdm)
if cell_type == 'compute':
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(context, self, create=True)
开发者ID:HybridCloud-dew,项目名称:hws,代码行数:21,代码来源:block_device.py
示例14: create
def create(self, context):
if self.obj_attr_is_set("id"):
raise exception.ObjectActionError(action="create", reason="already created")
values = {
"instance_uuid": self.instance_uuid,
"code": self.code,
"message": self.message,
"details": self.details,
"host": self.host,
}
db_fault = db.instance_fault_create(context, values)
self._from_db_object(context, self, db_fault)
self.obj_reset_changes()
# Cells should only try sending a message over to nova-cells
# if cells is enabled and we're not the API cell. Otherwise,
# if the API cell is calling this, we could end up with
# infinite recursion.
if cells_opts.get_cell_type() == "compute":
try:
cells_rpcapi.CellsAPI().instance_fault_create_at_top(context, db_fault)
except Exception:
LOG.exception(_LE("Failed to notify cells of instance fault"))
开发者ID:rcosma-gd,项目名称:openstack-nova,代码行数:22,代码来源:instance_fault.py
示例15: _create
def _create(self, context, update_or_create=False):
"""Create the block device record in the database.
In case the id field is set on the object, and if the instance is set
raise an ObjectActionError. Resets all the changes on the object.
Returns None
:param context: security context used for database calls
:param update_or_create: consider existing block devices for the
instance based on the device name and swap, and only update
the ones that match. Normally only used when creating the
instance for the first time.
"""
cell_type = cells_opts.get_cell_type()
if cell_type == "api":
raise exception.ObjectActionError(
action="create", reason="BlockDeviceMapping cannot be " "created in the API cell."
)
if self.obj_attr_is_set("id"):
raise exception.ObjectActionError(action="create", reason="already created")
updates = self.obj_get_changes()
if "instance" in updates:
raise exception.ObjectActionError(action="create", reason="instance assigned")
cells_create = update_or_create or None
if update_or_create:
db_bdm = db.block_device_mapping_update_or_create(context, updates, legacy=False)
else:
db_bdm = db.block_device_mapping_create(context, updates, legacy=False)
self._from_db_object(context, self, db_bdm)
if cell_type == "compute":
cells_api = cells_rpcapi.CellsAPI()
cells_api.bdm_update_or_create_at_top(context, self, create=cells_create)
开发者ID:dtroyer,项目名称:nova,代码行数:36,代码来源:block_device.py
示例16: create
def create(self):
if self.obj_attr_is_set('id'):
raise exception.ObjectActionError(action='create',
reason='already created')
values = {
'instance_uuid': self.instance_uuid,
'code': self.code,
'message': self.message,
'details': self.details,
'host': self.host,
}
db_fault = db.instance_fault_create(self._context, values)
self._from_db_object(self._context, self, db_fault)
self.obj_reset_changes()
# Cells should only try sending a message over to nova-cells
# if cells is enabled and we're not the API cell. Otherwise,
# if the API cell is calling this, we could end up with
# infinite recursion.
if cells_opts.get_cell_type() == 'compute':
try:
cells_rpcapi.CellsAPI().instance_fault_create_at_top(
self._context, db_fault)
except Exception:
LOG.exception("Failed to notify cells of instance fault")
开发者ID:sapcc,项目名称:nova,代码行数:24,代码来源:instance_fault.py
示例17: save
def save(self, context, expected_vm_state=None,
expected_task_state=None, admin_state_reset=False):
"""Save updates to this instance
Column-wise updates will be made based on the result of
self.what_changed(). If expected_task_state is provided,
it will be checked against the in-database copy of the
instance before updates are made.
:param context: Security context
:param expected_task_state: Optional tuple of valid task states
for the instance to be in.
:param expected_vm_state: Optional tuple of valid vm states
for the instance to be in.
:param admin_state_reset: True if admin API is forcing setting
of task_state/vm_state.
"""
cell_type = cells_opts.get_cell_type()
if cell_type == 'api' and self.cell_name:
# NOTE(comstud): We need to stash a copy of ourselves
# before any updates are applied. When we call the save
# methods on nested objects, we will lose any changes to
# them. But we need to make sure child cells can tell
# what is changed.
#
# We also need to nuke any updates to vm_state and task_state
# unless admin_state_reset is True. compute cells are
# authoritative for their view of vm_state and task_state.
stale_instance = copy.deepcopy(self)
def _handle_cell_update_from_api():
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_from_api(context, stale_instance,
expected_vm_state,
expected_task_state,
admin_state_reset)
else:
stale_instance = None
updates = {}
changes = self.obj_what_changed()
for field in self.fields:
if (hasattr(self, base.get_attrname(field)) and
isinstance(self[field], base.NovaObject)):
getattr(self, '_save_%s' % field)(context)
elif field in changes:
updates[field] = self[field]
if not updates:
if stale_instance:
_handle_cell_update_from_api()
return
# Cleaned needs to be turned back into an int here
if 'cleaned' in updates:
if updates['cleaned']:
updates['cleaned'] = 1
else:
updates['cleaned'] = 0
if expected_task_state is not None:
updates['expected_task_state'] = expected_task_state
if expected_vm_state is not None:
updates['expected_vm_state'] = expected_vm_state
old_ref, inst_ref = db.instance_update_and_get_original(
context, self.uuid, updates, update_cells=False)
if stale_instance:
_handle_cell_update_from_api()
elif cell_type == 'compute':
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_at_top(context, inst_ref)
expected_attrs = []
for attr in INSTANCE_OPTIONAL_FIELDS:
if hasattr(self, base.get_attrname(attr)):
expected_attrs.append(attr)
self._from_db_object(context, self, inst_ref, expected_attrs)
if 'vm_state' in changes or 'task_state' in changes:
notifications.send_update(context, old_ref, inst_ref)
self.obj_reset_changes()
开发者ID:DrZaarlon,项目名称:nova,代码行数:82,代码来源:instance.py
示例18: save
def save(self, context, expected_vm_state=None,
expected_task_state=None, admin_state_reset=False):
"""Save updates to this instance
Column-wise updates will be made based on the result of
self.what_changed(). If expected_task_state is provided,
it will be checked against the in-database copy of the
instance before updates are made.
:param:context: Security context
:param:expected_task_state: Optional tuple of valid task states
for the instance to be in
:param:expected_vm_state: Optional tuple of valid vm states
for the instance to be in
:param admin_state_reset: True if admin API is forcing setting
of task_state/vm_state
"""
cell_type = cells_opts.get_cell_type()
if cell_type == 'api' and self.cell_name:
# NOTE(comstud): We need to stash a copy of ourselves
# before any updates are applied. When we call the save
# methods on nested objects, we will lose any changes to
# them. But we need to make sure child cells can tell
# what is changed.
#
# We also need to nuke any updates to vm_state and task_state
# unless admin_state_reset is True. compute cells are
# authoritative for their view of vm_state and task_state.
stale_instance = self.obj_clone()
def _handle_cell_update_from_api():
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_from_api(context, stale_instance,
expected_vm_state,
expected_task_state,
admin_state_reset)
else:
stale_instance = None
updates = {}
changes = self.obj_what_changed()
for field in self.fields:
if (self.obj_attr_is_set(field) and
isinstance(self[field], base.NovaObject)):
try:
getattr(self, '_save_%s' % field)(context)
except AttributeError:
LOG.exception(_('No save handler for %s') % field,
instance=self)
elif field in changes:
updates[field] = self[field]
if not updates:
if stale_instance:
_handle_cell_update_from_api()
return
# Cleaned needs to be turned back into an int here
if 'cleaned' in updates:
if updates['cleaned']:
updates['cleaned'] = 1
else:
updates['cleaned'] = 0
if expected_task_state is not None:
if (self.VERSION == '1.9' and
expected_task_state == 'image_snapshot'):
# NOTE(danms): Icehouse introduced a pending state which
# Havana doesn't know about. If we're an old instance,
# tolerate the pending state as well
expected_task_state = [
expected_task_state, 'image_snapshot_pending']
updates['expected_task_state'] = expected_task_state
if expected_vm_state is not None:
updates['expected_vm_state'] = expected_vm_state
expected_attrs = [attr for attr in _INSTANCE_OPTIONAL_JOINED_FIELDS
if self.obj_attr_is_set(attr)]
# NOTE(alaski): We need to pull system_metadata for the
# notification.send_update() below. If we don't there's a KeyError
# when it tries to extract the flavor.
if 'system_metadata' not in expected_attrs:
expected_attrs.append('system_metadata')
old_ref, inst_ref = db.instance_update_and_get_original(
context, self.uuid, updates, update_cells=False,
columns_to_join=_expected_cols(expected_attrs))
if stale_instance:
_handle_cell_update_from_api()
elif cell_type == 'compute':
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_at_top(context, inst_ref)
self._from_db_object(context, self, inst_ref, expected_attrs)
notifications.send_update(context, old_ref, inst_ref)
self.obj_reset_changes()
开发者ID:bigloupe,项目名称:nova,代码行数:98,代码来源:instance.py
示例19: save
def save(self, expected_vm_state=None,
expected_task_state=None, admin_state_reset=False):
"""Save updates to this instance
Column-wise updates will be made based on the result of
self.what_changed(). If expected_task_state is provided,
it will be checked against the in-database copy of the
instance before updates are made.
:param:context: Security context
:param:expected_task_state: Optional tuple of valid task states
for the instance to be in
:param:expected_vm_state: Optional tuple of valid vm states
for the instance to be in
:param admin_state_reset: True if admin API is forcing setting
of task_state/vm_state
"""
# Store this on the class because _cell_name_blocks_sync is useless
# after the db update call below.
self._sync_cells = not self._cell_name_blocks_sync()
context = self._context
cell_type = cells_opts.get_cell_type()
if cell_type is not None:
# NOTE(comstud): We need to stash a copy of ourselves
# before any updates are applied. When we call the save
# methods on nested objects, we will lose any changes to
# them. But we need to make sure child cells can tell
# what is changed.
#
# We also need to nuke any updates to vm_state and task_state
# unless admin_state_reset is True. compute cells are
# authoritative for their view of vm_state and task_state.
stale_instance = self.obj_clone()
cells_update_from_api = (cell_type == 'api' and self.cell_name and
self._sync_cells)
if cells_update_from_api:
def _handle_cell_update_from_api():
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_from_api(context, stale_instance,
expected_vm_state,
expected_task_state,
admin_state_reset)
updates = {}
changes = self.obj_what_changed()
for field in self.fields:
# NOTE(danms): For object fields, we construct and call a
# helper method like self._save_$attrname()
if (self.obj_attr_is_set(field) and
isinstance(self.fields[field], fields.ObjectField)):
try:
getattr(self, '_save_%s' % field)(context)
except AttributeError:
LOG.exception(_LE('No save handler for %s'), field,
instance=self)
except db_exc.DBReferenceError as exp:
if exp.key != 'instance_uuid':
raise
# NOTE(melwitt): This will happen if we instance.save()
# before an instance.create() and FK constraint fails.
# In practice, this occurs in cells during a delete of
# an unscheduled instance. Otherwise, it could happen
# as a result of bug.
raise exception.InstanceNotFound(instance_id=self.uuid)
elif field in changes:
if (field == 'cell_name' and self[field] is not None and
self[field].startswith(cells_utils.BLOCK_SYNC_FLAG)):
updates[field] = self[field].replace(
cells_utils.BLOCK_SYNC_FLAG, '', 1)
else:
updates[field] = self[field]
if not updates:
if cells_update_from_api:
_handle_cell_update_from_api()
return
# Cleaned needs to be turned back into an int here
if 'cleaned' in updates:
if updates['cleaned']:
updates['cleaned'] = 1
else:
updates['cleaned'] = 0
if expected_task_state is not None:
updates['expected_task_state'] = expected_task_state
if expected_vm_state is not None:
updates['expected_vm_state'] = expected_vm_state
expected_attrs = [attr for attr in _INSTANCE_OPTIONAL_JOINED_FIELDS
if self.obj_attr_is_set(attr)]
if 'pci_devices' in expected_attrs:
# NOTE(danms): We don't refresh pci_devices on save right now
expected_attrs.remove('pci_devices')
#.........这里部分代码省略.........
开发者ID:amatuerone,项目名称:nova,代码行数:101,代码来源:instance.py
示例20: save
def save(self, context, expected_vm_state=None, expected_task_state=None, admin_state_reset=False):
"""Save updates to this instance
Column-wise updates will be made based on the result of
self.what_changed(). If expected_task_state is provided,
it will be checked against the in-database copy of the
instance before updates are made.
:param context: Security context
:param expected_task_state: Optional tuple of valid task states
for the instance to be in.
:param expected_vm_state: Optional tuple of valid vm states
for the instance to be in.
:param admin_state_reset: True if admin API is forcing setting
of task_state/vm_state.
"""
cell_type = cells_opts.get_cell_type()
if cell_type == "api" and self.cell_name:
# NOTE(comstud): We need to stash a copy of ourselves
# before any updates are applied. When we call the save
# methods on nested objects, we will lose any changes to
# them. But we need to make sure child cells can tell
# what is changed.
#
# We also need to nuke any updates to vm_state and task_state
# unless admin_state_reset is True. compute cells are
# authoritative for their view of vm_state and task_state.
stale_instance = self.obj_clone()
def _handle_cell_update_from_api():
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_from_api(
context, stale_instance, expected_vm_state, expected_task_state, admin_state_reset
)
else:
stale_instance = None
updates = {}
changes = self.obj_what_changed()
for field in self.fields:
if self.obj_attr_is_set(field) and isinstance(self[field], base.NovaObject):
try:
getattr(self, "_save_%s" % field)(context)
except AttributeError:
LOG.exception(_("No save handler for %s") % field, instance=self)
elif field in changes:
updates[field] = self[field]
if not updates:
if stale_instance:
_handle_cell_update_from_api()
return
# Cleaned needs to be turned back into an int here
if "cleaned" in updates:
if updates["cleaned"]:
updates["cleaned"] = 1
else:
updates["cleaned"] = 0
if expected_task_state is not None:
updates["expected_task_state"] = expected_task_state
if expected_vm_state is not None:
updates["expected_vm_state"] = expected_vm_state
expected_attrs = [attr for attr in _INSTANCE_OPTIONAL_JOINED_FIELDS if self.obj_attr_is_set(attr)]
old_ref, inst_ref = db.instance_update_and_get_original(
context, self.uuid, updates, update_cells=False, columns_to_join=_expected_cols(expected_attrs)
)
if stale_instance:
_handle_cell_update_from_api()
elif cell_type == "compute":
cells_api = cells_rpcapi.CellsAPI()
cells_api.instance_update_at_top(context, inst_ref)
self._from_db_object(context, self, inst_ref, expected_attrs)
notifications.send_update(context, old_ref, inst_ref)
self.obj_reset_changes()
|
请发表评论