OpenStack Compute (Nova)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

instance.py 63KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  1. # Copyright 2013 IBM Corp.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. # not use this file except in compliance with the License. You may obtain
  5. # a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. # License for the specific language governing permissions and limitations
  13. # under the License.
  14. import contextlib
  15. from oslo_config import cfg
  16. from oslo_db import exception as db_exc
  17. from oslo_log import log as logging
  18. from oslo_serialization import jsonutils
  19. from oslo_utils import timeutils
  20. from oslo_utils import versionutils
  21. from sqlalchemy import or_
  22. from sqlalchemy.sql import func
  23. from sqlalchemy.sql import null
  24. from nova import availability_zones as avail_zone
  25. from nova.compute import task_states
  26. from nova.compute import vm_states
  27. from nova.db import api as db
  28. from nova.db.sqlalchemy import api as db_api
  29. from nova.db.sqlalchemy import models
  30. from nova import exception
  31. from nova.i18n import _
  32. from nova.network import model as network_model
  33. from nova import notifications
  34. from nova import objects
  35. from nova.objects import base
  36. from nova.objects import fields
  37. from nova import utils
  38. CONF = cfg.CONF
  39. LOG = logging.getLogger(__name__)
  40. # List of fields that can be joined in DB layer.
  41. _INSTANCE_OPTIONAL_JOINED_FIELDS = ['metadata', 'system_metadata',
  42. 'info_cache', 'security_groups',
  43. 'pci_devices', 'tags', 'services',
  44. 'fault']
  45. # These are fields that are optional but don't translate to db columns
  46. _INSTANCE_OPTIONAL_NON_COLUMN_FIELDS = ['flavor', 'old_flavor',
  47. 'new_flavor', 'ec2_ids']
  48. # These are fields that are optional and in instance_extra
  49. _INSTANCE_EXTRA_FIELDS = ['numa_topology', 'pci_requests',
  50. 'flavor', 'vcpu_model', 'migration_context',
  51. 'keypairs', 'device_metadata', 'trusted_certs']
  52. # These are fields that applied/drooped by migration_context
  53. _MIGRATION_CONTEXT_ATTRS = ['numa_topology', 'pci_requests',
  54. 'pci_devices']
  55. # These are fields that can be specified as expected_attrs
  56. INSTANCE_OPTIONAL_ATTRS = (_INSTANCE_OPTIONAL_JOINED_FIELDS +
  57. _INSTANCE_OPTIONAL_NON_COLUMN_FIELDS +
  58. _INSTANCE_EXTRA_FIELDS)
  59. # These are fields that most query calls load by default
  60. INSTANCE_DEFAULT_FIELDS = ['metadata', 'system_metadata',
  61. 'info_cache', 'security_groups']
  62. # Maximum count of tags to one instance
  63. MAX_TAG_COUNT = 50
  64. def _expected_cols(expected_attrs):
  65. """Return expected_attrs that are columns needing joining.
  66. NB: This function may modify expected_attrs if one
  67. requested attribute requires another.
  68. """
  69. if not expected_attrs:
  70. return expected_attrs
  71. simple_cols = [attr for attr in expected_attrs
  72. if attr in _INSTANCE_OPTIONAL_JOINED_FIELDS]
  73. complex_cols = ['extra.%s' % field
  74. for field in _INSTANCE_EXTRA_FIELDS
  75. if field in expected_attrs]
  76. if complex_cols:
  77. simple_cols.append('extra')
  78. simple_cols = [x for x in simple_cols if x not in _INSTANCE_EXTRA_FIELDS]
  79. expected_cols = simple_cols + complex_cols
  80. # NOTE(pumaranikar): expected_cols list can contain duplicates since
  81. # caller appends column attributes to expected_attr without checking if
  82. # it is already present in the list or not. Hence, we remove duplicates
  83. # here, if any. The resultant list is sorted based on list index to
  84. # maintain the insertion order.
  85. return sorted(list(set(expected_cols)), key=expected_cols.index)
  86. _NO_DATA_SENTINEL = object()
  87. # TODO(berrange): Remove NovaObjectDictCompat
  88. @base.NovaObjectRegistry.register
  89. class Instance(base.NovaPersistentObject, base.NovaObject,
  90. base.NovaObjectDictCompat):
  91. # Version 2.0: Initial version
  92. # Version 2.1: Added services
  93. # Version 2.2: Added keypairs
  94. # Version 2.3: Added device_metadata
  95. # Version 2.4: Added trusted_certs
  96. # Version 2.5: Added hard_delete kwarg in destroy
  97. VERSION = '2.5'
  98. fields = {
  99. 'id': fields.IntegerField(),
  100. 'user_id': fields.StringField(nullable=True),
  101. 'project_id': fields.StringField(nullable=True),
  102. 'image_ref': fields.StringField(nullable=True),
  103. 'kernel_id': fields.StringField(nullable=True),
  104. 'ramdisk_id': fields.StringField(nullable=True),
  105. 'hostname': fields.StringField(nullable=True),
  106. 'launch_index': fields.IntegerField(nullable=True),
  107. 'key_name': fields.StringField(nullable=True),
  108. 'key_data': fields.StringField(nullable=True),
  109. 'power_state': fields.IntegerField(nullable=True),
  110. 'vm_state': fields.StringField(nullable=True),
  111. 'task_state': fields.StringField(nullable=True),
  112. 'services': fields.ObjectField('ServiceList'),
  113. 'memory_mb': fields.IntegerField(nullable=True),
  114. 'vcpus': fields.IntegerField(nullable=True),
  115. 'root_gb': fields.IntegerField(nullable=True),
  116. 'ephemeral_gb': fields.IntegerField(nullable=True),
  117. 'ephemeral_key_uuid': fields.UUIDField(nullable=True),
  118. 'host': fields.StringField(nullable=True),
  119. 'node': fields.StringField(nullable=True),
  120. 'instance_type_id': fields.IntegerField(nullable=True),
  121. 'user_data': fields.StringField(nullable=True),
  122. 'reservation_id': fields.StringField(nullable=True),
  123. 'launched_at': fields.DateTimeField(nullable=True),
  124. 'terminated_at': fields.DateTimeField(nullable=True),
  125. 'availability_zone': fields.StringField(nullable=True),
  126. 'display_name': fields.StringField(nullable=True),
  127. 'display_description': fields.StringField(nullable=True),
  128. 'launched_on': fields.StringField(nullable=True),
  129. 'locked': fields.BooleanField(default=False),
  130. 'locked_by': fields.StringField(nullable=True),
  131. 'os_type': fields.StringField(nullable=True),
  132. 'architecture': fields.StringField(nullable=True),
  133. 'vm_mode': fields.StringField(nullable=True),
  134. 'uuid': fields.UUIDField(),
  135. 'root_device_name': fields.StringField(nullable=True),
  136. 'default_ephemeral_device': fields.StringField(nullable=True),
  137. 'default_swap_device': fields.StringField(nullable=True),
  138. 'config_drive': fields.StringField(nullable=True),
  139. 'access_ip_v4': fields.IPV4AddressField(nullable=True),
  140. 'access_ip_v6': fields.IPV6AddressField(nullable=True),
  141. 'auto_disk_config': fields.BooleanField(default=False),
  142. 'progress': fields.IntegerField(nullable=True),
  143. 'shutdown_terminate': fields.BooleanField(default=False),
  144. 'disable_terminate': fields.BooleanField(default=False),
  145. # TODO(stephenfin): Remove this in version 3.0 of the object
  146. 'cell_name': fields.StringField(nullable=True),
  147. 'metadata': fields.DictOfStringsField(),
  148. 'system_metadata': fields.DictOfNullableStringsField(),
  149. 'info_cache': fields.ObjectField('InstanceInfoCache',
  150. nullable=True),
  151. 'security_groups': fields.ObjectField('SecurityGroupList'),
  152. 'fault': fields.ObjectField('InstanceFault', nullable=True),
  153. 'cleaned': fields.BooleanField(default=False),
  154. 'pci_devices': fields.ObjectField('PciDeviceList', nullable=True),
  155. 'numa_topology': fields.ObjectField('InstanceNUMATopology',
  156. nullable=True),
  157. 'pci_requests': fields.ObjectField('InstancePCIRequests',
  158. nullable=True),
  159. 'device_metadata': fields.ObjectField('InstanceDeviceMetadata',
  160. nullable=True),
  161. 'tags': fields.ObjectField('TagList'),
  162. 'flavor': fields.ObjectField('Flavor'),
  163. 'old_flavor': fields.ObjectField('Flavor', nullable=True),
  164. 'new_flavor': fields.ObjectField('Flavor', nullable=True),
  165. 'vcpu_model': fields.ObjectField('VirtCPUModel', nullable=True),
  166. 'ec2_ids': fields.ObjectField('EC2Ids'),
  167. 'migration_context': fields.ObjectField('MigrationContext',
  168. nullable=True),
  169. 'keypairs': fields.ObjectField('KeyPairList'),
  170. 'trusted_certs': fields.ObjectField('TrustedCerts', nullable=True),
  171. }
  172. obj_extra_fields = ['name']
  173. def obj_make_compatible(self, primitive, target_version):
  174. super(Instance, self).obj_make_compatible(primitive, target_version)
  175. target_version = versionutils.convert_version_to_tuple(target_version)
  176. if target_version < (2, 4) and 'trusted_certs' in primitive:
  177. del primitive['trusted_certs']
  178. if target_version < (2, 3) and 'device_metadata' in primitive:
  179. del primitive['device_metadata']
  180. if target_version < (2, 2) and 'keypairs' in primitive:
  181. del primitive['keypairs']
  182. if target_version < (2, 1) and 'services' in primitive:
  183. del primitive['services']
  184. def __init__(self, *args, **kwargs):
  185. super(Instance, self).__init__(*args, **kwargs)
  186. self._reset_metadata_tracking()
  187. @property
  188. def image_meta(self):
  189. return objects.ImageMeta.from_instance(self)
  190. def _reset_metadata_tracking(self, fields=None):
  191. if fields is None or 'system_metadata' in fields:
  192. self._orig_system_metadata = (dict(self.system_metadata) if
  193. 'system_metadata' in self else {})
  194. if fields is None or 'metadata' in fields:
  195. self._orig_metadata = (dict(self.metadata) if
  196. 'metadata' in self else {})
  197. def obj_clone(self):
  198. """Create a copy of this instance object."""
  199. nobj = super(Instance, self).obj_clone()
  200. # Since the base object only does a deep copy of the defined fields,
  201. # need to make sure to also copy the additional tracking metadata
  202. # attributes so they don't show as changed and cause the metadata
  203. # to always be updated even when stale information.
  204. if hasattr(self, '_orig_metadata'):
  205. nobj._orig_metadata = dict(self._orig_metadata)
  206. if hasattr(self, '_orig_system_metadata'):
  207. nobj._orig_system_metadata = dict(self._orig_system_metadata)
  208. return nobj
  209. def obj_reset_changes(self, fields=None, recursive=False):
  210. super(Instance, self).obj_reset_changes(fields,
  211. recursive=recursive)
  212. self._reset_metadata_tracking(fields=fields)
  213. def obj_what_changed(self):
  214. changes = super(Instance, self).obj_what_changed()
  215. if 'metadata' in self and self.metadata != self._orig_metadata:
  216. changes.add('metadata')
  217. if 'system_metadata' in self and (self.system_metadata !=
  218. self._orig_system_metadata):
  219. changes.add('system_metadata')
  220. return changes
  221. @classmethod
  222. def _obj_from_primitive(cls, context, objver, primitive):
  223. self = super(Instance, cls)._obj_from_primitive(context, objver,
  224. primitive)
  225. self._reset_metadata_tracking()
  226. return self
  227. @property
  228. def name(self):
  229. try:
  230. base_name = CONF.instance_name_template % self.id
  231. except TypeError:
  232. # Support templates like "uuid-%(uuid)s", etc.
  233. info = {}
  234. # NOTE(russellb): Don't use self.iteritems() here, as it will
  235. # result in infinite recursion on the name property.
  236. for key in self.fields:
  237. if key == 'name':
  238. # NOTE(danms): prevent recursion
  239. continue
  240. elif not self.obj_attr_is_set(key):
  241. # NOTE(danms): Don't trigger lazy-loads
  242. continue
  243. info[key] = self[key]
  244. try:
  245. base_name = CONF.instance_name_template % info
  246. except KeyError:
  247. base_name = self.uuid
  248. except (exception.ObjectActionError,
  249. exception.OrphanedObjectError):
  250. # This indicates self.id was not set and/or could not be
  251. # lazy loaded. What this means is the instance has not
  252. # been persisted to a db yet, which should indicate it has
  253. # not been scheduled yet. In this situation it will have a
  254. # blank name.
  255. if (self.vm_state == vm_states.BUILDING and
  256. self.task_state == task_states.SCHEDULING):
  257. base_name = ''
  258. else:
  259. # If the vm/task states don't indicate that it's being booted
  260. # then we have a bug here. Log an error and attempt to return
  261. # the uuid which is what an error above would return.
  262. LOG.error('Could not lazy-load instance.id while '
  263. 'attempting to generate the instance name.')
  264. base_name = self.uuid
  265. return base_name
  266. def _flavor_from_db(self, db_flavor):
  267. """Load instance flavor information from instance_extra."""
  268. # Before we stored flavors in instance_extra, certain fields, defined
  269. # in nova.compute.flavors.system_metadata_flavor_props, were stored
  270. # in the instance.system_metadata for the embedded instance.flavor.
  271. # The "disabled" and "is_public" fields weren't one of those keys,
  272. # however, so really old instances that had their embedded flavor
  273. # converted to the serialized instance_extra form won't have the
  274. # disabled attribute set and we need to default those here so callers
  275. # don't explode trying to load instance.flavor.disabled.
  276. def _default_flavor_values(flavor):
  277. if 'disabled' not in flavor:
  278. flavor.disabled = False
  279. if 'is_public' not in flavor:
  280. flavor.is_public = True
  281. flavor_info = jsonutils.loads(db_flavor)
  282. self.flavor = objects.Flavor.obj_from_primitive(flavor_info['cur'])
  283. _default_flavor_values(self.flavor)
  284. if flavor_info['old']:
  285. self.old_flavor = objects.Flavor.obj_from_primitive(
  286. flavor_info['old'])
  287. _default_flavor_values(self.old_flavor)
  288. else:
  289. self.old_flavor = None
  290. if flavor_info['new']:
  291. self.new_flavor = objects.Flavor.obj_from_primitive(
  292. flavor_info['new'])
  293. _default_flavor_values(self.new_flavor)
  294. else:
  295. self.new_flavor = None
  296. self.obj_reset_changes(['flavor', 'old_flavor', 'new_flavor'])
  297. @staticmethod
  298. def _from_db_object(context, instance, db_inst, expected_attrs=None):
  299. """Method to help with migration to objects.
  300. Converts a database entity to a formal object.
  301. """
  302. instance._context = context
  303. if expected_attrs is None:
  304. expected_attrs = []
  305. # Most of the field names match right now, so be quick
  306. for field in instance.fields:
  307. if field in INSTANCE_OPTIONAL_ATTRS:
  308. continue
  309. elif field == 'deleted':
  310. instance.deleted = db_inst['deleted'] == db_inst['id']
  311. elif field == 'cleaned':
  312. instance.cleaned = db_inst['cleaned'] == 1
  313. else:
  314. instance[field] = db_inst[field]
  315. if 'metadata' in expected_attrs:
  316. instance['metadata'] = utils.instance_meta(db_inst)
  317. if 'system_metadata' in expected_attrs:
  318. instance['system_metadata'] = utils.instance_sys_meta(db_inst)
  319. if 'fault' in expected_attrs:
  320. instance['fault'] = (
  321. objects.InstanceFault.get_latest_for_instance(
  322. context, instance.uuid))
  323. if 'ec2_ids' in expected_attrs:
  324. instance._load_ec2_ids()
  325. if 'info_cache' in expected_attrs:
  326. if db_inst.get('info_cache') is None:
  327. instance.info_cache = None
  328. elif not instance.obj_attr_is_set('info_cache'):
  329. # TODO(danms): If this ever happens on a backlevel instance
  330. # passed to us by a backlevel service, things will break
  331. instance.info_cache = objects.InstanceInfoCache(context)
  332. if instance.info_cache is not None:
  333. instance.info_cache._from_db_object(context,
  334. instance.info_cache,
  335. db_inst['info_cache'])
  336. # TODO(danms): If we are updating these on a backlevel instance,
  337. # we'll end up sending back new versions of these objects (see
  338. # above note for new info_caches
  339. if 'pci_devices' in expected_attrs:
  340. pci_devices = base.obj_make_list(
  341. context, objects.PciDeviceList(context),
  342. objects.PciDevice, db_inst['pci_devices'])
  343. instance['pci_devices'] = pci_devices
  344. if 'security_groups' in expected_attrs:
  345. sec_groups = base.obj_make_list(
  346. context, objects.SecurityGroupList(context),
  347. objects.SecurityGroup, db_inst.get('security_groups', []))
  348. instance['security_groups'] = sec_groups
  349. if 'tags' in expected_attrs:
  350. tags = base.obj_make_list(
  351. context, objects.TagList(context),
  352. objects.Tag, db_inst['tags'])
  353. instance['tags'] = tags
  354. if 'services' in expected_attrs:
  355. services = base.obj_make_list(
  356. context, objects.ServiceList(context),
  357. objects.Service, db_inst['services'])
  358. instance['services'] = services
  359. instance._extra_attributes_from_db_object(instance, db_inst,
  360. expected_attrs)
  361. instance.obj_reset_changes()
  362. return instance
  363. @staticmethod
  364. def _extra_attributes_from_db_object(instance, db_inst,
  365. expected_attrs=None):
  366. """Method to help with migration of extra attributes to objects.
  367. """
  368. if expected_attrs is None:
  369. expected_attrs = []
  370. # NOTE(danms): We can be called with a dict instead of a
  371. # SQLAlchemy object, so we have to be careful here
  372. if hasattr(db_inst, '__dict__'):
  373. have_extra = 'extra' in db_inst.__dict__ and db_inst['extra']
  374. else:
  375. have_extra = 'extra' in db_inst and db_inst['extra']
  376. if 'numa_topology' in expected_attrs:
  377. if have_extra:
  378. instance._load_numa_topology(
  379. db_inst['extra'].get('numa_topology'))
  380. else:
  381. instance.numa_topology = None
  382. if 'pci_requests' in expected_attrs:
  383. if have_extra:
  384. instance._load_pci_requests(
  385. db_inst['extra'].get('pci_requests'))
  386. else:
  387. instance.pci_requests = None
  388. if 'device_metadata' in expected_attrs:
  389. if have_extra:
  390. instance._load_device_metadata(
  391. db_inst['extra'].get('device_metadata'))
  392. else:
  393. instance.device_metadata = None
  394. if 'vcpu_model' in expected_attrs:
  395. if have_extra:
  396. instance._load_vcpu_model(
  397. db_inst['extra'].get('vcpu_model'))
  398. else:
  399. instance.vcpu_model = None
  400. if 'migration_context' in expected_attrs:
  401. if have_extra:
  402. instance._load_migration_context(
  403. db_inst['extra'].get('migration_context'))
  404. else:
  405. instance.migration_context = None
  406. if 'keypairs' in expected_attrs:
  407. if have_extra:
  408. instance._load_keypairs(db_inst['extra'].get('keypairs'))
  409. if 'trusted_certs' in expected_attrs:
  410. if have_extra:
  411. instance._load_trusted_certs(
  412. db_inst['extra'].get('trusted_certs'))
  413. else:
  414. instance.trusted_certs = None
  415. if any([x in expected_attrs for x in ('flavor',
  416. 'old_flavor',
  417. 'new_flavor')]):
  418. if have_extra and db_inst['extra'].get('flavor'):
  419. instance._flavor_from_db(db_inst['extra']['flavor'])
  420. @staticmethod
  421. @db.select_db_reader_mode
  422. def _db_instance_get_by_uuid(context, uuid, columns_to_join,
  423. use_slave=False):
  424. return db.instance_get_by_uuid(context, uuid,
  425. columns_to_join=columns_to_join)
  426. @base.remotable_classmethod
  427. def get_by_uuid(cls, context, uuid, expected_attrs=None, use_slave=False):
  428. if expected_attrs is None:
  429. expected_attrs = ['info_cache', 'security_groups']
  430. columns_to_join = _expected_cols(expected_attrs)
  431. db_inst = cls._db_instance_get_by_uuid(context, uuid, columns_to_join,
  432. use_slave=use_slave)
  433. return cls._from_db_object(context, cls(), db_inst,
  434. expected_attrs)
  435. @base.remotable_classmethod
  436. def get_by_id(cls, context, inst_id, expected_attrs=None):
  437. if expected_attrs is None:
  438. expected_attrs = ['info_cache', 'security_groups']
  439. columns_to_join = _expected_cols(expected_attrs)
  440. db_inst = db.instance_get(context, inst_id,
  441. columns_to_join=columns_to_join)
  442. return cls._from_db_object(context, cls(), db_inst,
  443. expected_attrs)
  444. @base.remotable
  445. def create(self):
  446. if self.obj_attr_is_set('id'):
  447. raise exception.ObjectActionError(action='create',
  448. reason='already created')
  449. if self.obj_attr_is_set('deleted') and self.deleted:
  450. raise exception.ObjectActionError(action='create',
  451. reason='already deleted')
  452. updates = self.obj_get_changes()
  453. # NOTE(danms): We know because of the check above that deleted
  454. # is either unset or false. Since we need to avoid passing False
  455. # down to the DB layer (which uses an integer), we can always
  456. # default it to zero here.
  457. updates['deleted'] = 0
  458. expected_attrs = [attr for attr in INSTANCE_DEFAULT_FIELDS
  459. if attr in updates]
  460. if 'security_groups' in updates:
  461. updates['security_groups'] = [x.name for x in
  462. updates['security_groups']]
  463. if 'info_cache' in updates:
  464. updates['info_cache'] = {
  465. 'network_info': updates['info_cache'].network_info.json()
  466. }
  467. updates['extra'] = {}
  468. numa_topology = updates.pop('numa_topology', None)
  469. expected_attrs.append('numa_topology')
  470. if numa_topology:
  471. updates['extra']['numa_topology'] = numa_topology._to_json()
  472. else:
  473. updates['extra']['numa_topology'] = None
  474. pci_requests = updates.pop('pci_requests', None)
  475. expected_attrs.append('pci_requests')
  476. if pci_requests:
  477. updates['extra']['pci_requests'] = (
  478. pci_requests.to_json())
  479. else:
  480. updates['extra']['pci_requests'] = None
  481. device_metadata = updates.pop('device_metadata', None)
  482. expected_attrs.append('device_metadata')
  483. if device_metadata:
  484. updates['extra']['device_metadata'] = (
  485. device_metadata._to_json())
  486. else:
  487. updates['extra']['device_metadata'] = None
  488. flavor = updates.pop('flavor', None)
  489. if flavor:
  490. expected_attrs.append('flavor')
  491. old = ((self.obj_attr_is_set('old_flavor') and
  492. self.old_flavor) and
  493. self.old_flavor.obj_to_primitive() or None)
  494. new = ((self.obj_attr_is_set('new_flavor') and
  495. self.new_flavor) and
  496. self.new_flavor.obj_to_primitive() or None)
  497. flavor_info = {
  498. 'cur': self.flavor.obj_to_primitive(),
  499. 'old': old,
  500. 'new': new,
  501. }
  502. self._nullify_flavor_description(flavor_info)
  503. updates['extra']['flavor'] = jsonutils.dumps(flavor_info)
  504. keypairs = updates.pop('keypairs', None)
  505. if keypairs is not None:
  506. expected_attrs.append('keypairs')
  507. updates['extra']['keypairs'] = jsonutils.dumps(
  508. keypairs.obj_to_primitive())
  509. vcpu_model = updates.pop('vcpu_model', None)
  510. expected_attrs.append('vcpu_model')
  511. if vcpu_model:
  512. updates['extra']['vcpu_model'] = (
  513. jsonutils.dumps(vcpu_model.obj_to_primitive()))
  514. else:
  515. updates['extra']['vcpu_model'] = None
  516. trusted_certs = updates.pop('trusted_certs', None)
  517. expected_attrs.append('trusted_certs')
  518. if trusted_certs:
  519. updates['extra']['trusted_certs'] = jsonutils.dumps(
  520. trusted_certs.obj_to_primitive())
  521. else:
  522. updates['extra']['trusted_certs'] = None
  523. db_inst = db.instance_create(self._context, updates)
  524. self._from_db_object(self._context, self, db_inst, expected_attrs)
  525. # NOTE(danms): The EC2 ids are created on their first load. In order
  526. # to avoid them being missing and having to be loaded later, we
  527. # load them once here on create now that the instance record is
  528. # created.
  529. self._load_ec2_ids()
  530. self.obj_reset_changes(['ec2_ids'])
  531. @base.remotable
  532. def destroy(self, hard_delete=False):
  533. if not self.obj_attr_is_set('id'):
  534. raise exception.ObjectActionError(action='destroy',
  535. reason='already destroyed')
  536. if not self.obj_attr_is_set('uuid'):
  537. raise exception.ObjectActionError(action='destroy',
  538. reason='no uuid')
  539. if not self.obj_attr_is_set('host') or not self.host:
  540. # NOTE(danms): If our host is not set, avoid a race
  541. constraint = db.constraint(host=db.equal_any(None))
  542. else:
  543. constraint = None
  544. try:
  545. db_inst = db.instance_destroy(self._context, self.uuid,
  546. constraint=constraint,
  547. hard_delete=hard_delete)
  548. self._from_db_object(self._context, self, db_inst)
  549. except exception.ConstraintNotMet:
  550. raise exception.ObjectActionError(action='destroy',
  551. reason='host changed')
  552. delattr(self, base.get_attrname('id'))
  553. def _save_info_cache(self, context):
  554. if self.info_cache:
  555. with self.info_cache.obj_alternate_context(context):
  556. self.info_cache.save()
  557. def _save_security_groups(self, context):
  558. security_groups = self.security_groups or []
  559. for secgroup in security_groups:
  560. with secgroup.obj_alternate_context(context):
  561. secgroup.save()
  562. self.security_groups.obj_reset_changes()
  563. def _save_fault(self, context):
  564. # NOTE(danms): I don't think we need to worry about this, do we?
  565. pass
  566. def _save_pci_requests(self, context):
  567. # TODO(danms): Unfortunately, extra.pci_requests is not a serialized
  568. # PciRequests object (!), so we have to handle it specially here.
  569. # That should definitely be fixed!
  570. self._extra_values_to_save['pci_requests'] = (
  571. self.pci_requests.to_json())
  572. def _save_pci_devices(self, context):
  573. # NOTE(yjiang5): All devices held by PCI tracker, only PCI tracker
  574. # permitted to update the DB. all change to devices from here will
  575. # be dropped.
  576. pass
  577. def _save_tags(self, context):
  578. # NOTE(gibi): tags are not saved through the instance
  579. pass
  580. @staticmethod
  581. def _nullify_flavor_description(flavor_info):
  582. """Helper method to nullify descriptions from a set of primitive
  583. flavors.
  584. Note that we don't remove the flavor description since that would
  585. make the versioned notification FlavorPayload have to handle the field
  586. not being set on the embedded instance.flavor.
  587. :param dict: dict of primitive flavor objects where the values are the
  588. flavors which get persisted in the instance_extra.flavor table.
  589. """
  590. for flavor in flavor_info.values():
  591. if flavor and 'description' in flavor['nova_object.data']:
  592. flavor['nova_object.data']['description'] = None
  593. def _save_flavor(self, context):
  594. if not any([x in self.obj_what_changed() for x in
  595. ('flavor', 'old_flavor', 'new_flavor')]):
  596. return
  597. flavor_info = {
  598. 'cur': self.flavor.obj_to_primitive(),
  599. 'old': (self.old_flavor and
  600. self.old_flavor.obj_to_primitive() or None),
  601. 'new': (self.new_flavor and
  602. self.new_flavor.obj_to_primitive() or None),
  603. }
  604. self._nullify_flavor_description(flavor_info)
  605. self._extra_values_to_save['flavor'] = jsonutils.dumps(flavor_info)
  606. self.obj_reset_changes(['flavor', 'old_flavor', 'new_flavor'])
  607. def _save_old_flavor(self, context):
  608. if 'old_flavor' in self.obj_what_changed():
  609. self._save_flavor(context)
  610. def _save_new_flavor(self, context):
  611. if 'new_flavor' in self.obj_what_changed():
  612. self._save_flavor(context)
  613. def _save_ec2_ids(self, context):
  614. # NOTE(hanlind): Read-only so no need to save this.
  615. pass
  616. def _save_keypairs(self, context):
  617. # NOTE(danms): Read-only so no need to save this.
  618. pass
  619. def _save_extra_generic(self, field):
  620. if field in self.obj_what_changed():
  621. obj = getattr(self, field)
  622. value = None
  623. if obj is not None:
  624. value = jsonutils.dumps(obj.obj_to_primitive())
  625. self._extra_values_to_save[field] = value
  626. # TODO(stephenfin): Remove the 'admin_state_reset' field in version 3.0 of
  627. # the object
  628. @base.remotable
  629. def save(self, expected_vm_state=None,
  630. expected_task_state=None, admin_state_reset=False):
  631. """Save updates to this instance
  632. Column-wise updates will be made based on the result of
  633. self.obj_what_changed(). If expected_task_state is provided,
  634. it will be checked against the in-database copy of the
  635. instance before updates are made.
  636. :param expected_vm_state: Optional tuple of valid vm states
  637. for the instance to be in
  638. :param expected_task_state: Optional tuple of valid task states
  639. for the instance to be in
  640. :param admin_state_reset: True if admin API is forcing setting
  641. of task_state/vm_state
  642. """
  643. context = self._context
  644. self._extra_values_to_save = {}
  645. updates = {}
  646. changes = self.obj_what_changed()
  647. for field in self.fields:
  648. # NOTE(danms): For object fields, we construct and call a
  649. # helper method like self._save_$attrname()
  650. if (self.obj_attr_is_set(field) and
  651. isinstance(self.fields[field], fields.ObjectField)):
  652. try:
  653. getattr(self, '_save_%s' % field)(context)
  654. except AttributeError:
  655. if field in _INSTANCE_EXTRA_FIELDS:
  656. self._save_extra_generic(field)
  657. continue
  658. LOG.exception('No save handler for %s', field,
  659. instance=self)
  660. except db_exc.DBReferenceError as exp:
  661. if exp.key != 'instance_uuid':
  662. raise
  663. # NOTE(melwitt): This will happen if we instance.save()
  664. # before an instance.create() and FK constraint fails.
  665. # In practice, this occurs in cells during a delete of
  666. # an unscheduled instance. Otherwise, it could happen
  667. # as a result of bug.
  668. raise exception.InstanceNotFound(instance_id=self.uuid)
  669. elif field in changes:
  670. updates[field] = self[field]
  671. if self._extra_values_to_save:
  672. db.instance_extra_update_by_uuid(context, self.uuid,
  673. self._extra_values_to_save)
  674. if not updates:
  675. return
  676. # Cleaned needs to be turned back into an int here
  677. if 'cleaned' in updates:
  678. if updates['cleaned']:
  679. updates['cleaned'] = 1
  680. else:
  681. updates['cleaned'] = 0
  682. if expected_task_state is not None:
  683. updates['expected_task_state'] = expected_task_state
  684. if expected_vm_state is not None:
  685. updates['expected_vm_state'] = expected_vm_state
  686. expected_attrs = [attr for attr in _INSTANCE_OPTIONAL_JOINED_FIELDS
  687. if self.obj_attr_is_set(attr)]
  688. if 'pci_devices' in expected_attrs:
  689. # NOTE(danms): We don't refresh pci_devices on save right now
  690. expected_attrs.remove('pci_devices')
  691. # NOTE(alaski): We need to pull system_metadata for the
  692. # notification.send_update() below. If we don't there's a KeyError
  693. # when it tries to extract the flavor.
  694. if 'system_metadata' not in expected_attrs:
  695. expected_attrs.append('system_metadata')
  696. old_ref, inst_ref = db.instance_update_and_get_original(
  697. context, self.uuid, updates,
  698. columns_to_join=_expected_cols(expected_attrs))
  699. self._from_db_object(context, self, inst_ref,
  700. expected_attrs=expected_attrs)
  701. # NOTE(danms): We have to be super careful here not to trigger
  702. # any lazy-loads that will unmigrate or unbackport something. So,
  703. # make a copy of the instance for notifications first.
  704. new_ref = self.obj_clone()
  705. notifications.send_update(context, old_ref, new_ref)
  706. self.obj_reset_changes()
  707. @base.remotable
  708. def refresh(self, use_slave=False):
  709. extra = [field for field in INSTANCE_OPTIONAL_ATTRS
  710. if self.obj_attr_is_set(field)]
  711. current = self.__class__.get_by_uuid(self._context, uuid=self.uuid,
  712. expected_attrs=extra,
  713. use_slave=use_slave)
  714. # NOTE(danms): We orphan the instance copy so we do not unexpectedly
  715. # trigger a lazy-load (which would mean we failed to calculate the
  716. # expected_attrs properly)
  717. current._context = None
  718. for field in self.fields:
  719. if field not in self:
  720. continue
  721. if field not in current:
  722. # If the field isn't in current we should not
  723. # touch it, triggering a likely-recursive lazy load.
  724. # Log it so we can see it happening though, as it
  725. # probably isn't expected in most cases.
  726. LOG.debug('Field %s is set but not in refreshed '
  727. 'instance, skipping', field)
  728. continue
  729. if field == 'info_cache':
  730. self.info_cache.refresh()
  731. elif self[field] != current[field]:
  732. self[field] = current[field]
  733. self.obj_reset_changes()
  734. def _load_generic(self, attrname):
  735. instance = self.__class__.get_by_uuid(self._context,
  736. uuid=self.uuid,
  737. expected_attrs=[attrname])
  738. if attrname not in instance:
  739. # NOTE(danms): Never allow us to recursively-load
  740. raise exception.ObjectActionError(
  741. action='obj_load_attr',
  742. reason=_('loading %s requires recursion') % attrname)
  743. # NOTE(danms): load anything we don't already have from the
  744. # instance we got from the database to make the most of the
  745. # performance hit.
  746. for field in self.fields:
  747. if field in instance and field not in self:
  748. setattr(self, field, getattr(instance, field))
  749. def _load_fault(self):
  750. self.fault = objects.InstanceFault.get_latest_for_instance(
  751. self._context, self.uuid)
  752. def _load_numa_topology(self, db_topology=_NO_DATA_SENTINEL):
  753. if db_topology is None:
  754. self.numa_topology = None
  755. elif db_topology is not _NO_DATA_SENTINEL:
  756. self.numa_topology = \
  757. objects.InstanceNUMATopology.obj_from_db_obj(self.uuid,
  758. db_topology)
  759. else:
  760. try:
  761. self.numa_topology = \
  762. objects.InstanceNUMATopology.get_by_instance_uuid(
  763. self._context, self.uuid)
  764. except exception.NumaTopologyNotFound:
  765. self.numa_topology = None
  766. def _load_pci_requests(self, db_requests=_NO_DATA_SENTINEL):
  767. if db_requests is not _NO_DATA_SENTINEL:
  768. self.pci_requests = objects.InstancePCIRequests.obj_from_db(
  769. self._context, self.uuid, db_requests)
  770. else:
  771. self.pci_requests = \
  772. objects.InstancePCIRequests.get_by_instance_uuid(
  773. self._context, self.uuid)
  774. def _load_device_metadata(self, db_dev_meta=_NO_DATA_SENTINEL):
  775. if db_dev_meta is None:
  776. self.device_metadata = None
  777. elif db_dev_meta is not _NO_DATA_SENTINEL:
  778. self.device_metadata = \
  779. objects.InstanceDeviceMetadata.obj_from_db(
  780. self._context, db_dev_meta)
  781. else:
  782. self.device_metadata = \
  783. objects.InstanceDeviceMetadata.get_by_instance_uuid(
  784. self._context, self.uuid)
  785. def _load_flavor(self):
  786. instance = self.__class__.get_by_uuid(
  787. self._context, uuid=self.uuid,
  788. expected_attrs=['flavor'])
  789. # NOTE(danms): Orphan the instance to make sure we don't lazy-load
  790. # anything below
  791. instance._context = None
  792. self.flavor = instance.flavor
  793. self.old_flavor = instance.old_flavor
  794. self.new_flavor = instance.new_flavor
  795. def _load_vcpu_model(self, db_vcpu_model=_NO_DATA_SENTINEL):
  796. if db_vcpu_model is None:
  797. self.vcpu_model = None
  798. elif db_vcpu_model is _NO_DATA_SENTINEL:
  799. self.vcpu_model = objects.VirtCPUModel.get_by_instance_uuid(
  800. self._context, self.uuid)
  801. else:
  802. db_vcpu_model = jsonutils.loads(db_vcpu_model)
  803. self.vcpu_model = objects.VirtCPUModel.obj_from_primitive(
  804. db_vcpu_model)
  805. def _load_ec2_ids(self):
  806. self.ec2_ids = objects.EC2Ids.get_by_instance(self._context, self)
  807. def _load_security_groups(self):
  808. self.security_groups = objects.SecurityGroupList.get_by_instance(
  809. self._context, self)
  810. def _load_pci_devices(self):
  811. self.pci_devices = objects.PciDeviceList.get_by_instance_uuid(
  812. self._context, self.uuid)
  813. def _load_migration_context(self, db_context=_NO_DATA_SENTINEL):
  814. if db_context is _NO_DATA_SENTINEL:
  815. try:
  816. self.migration_context = (
  817. objects.MigrationContext.get_by_instance_uuid(
  818. self._context, self.uuid))
  819. except exception.MigrationContextNotFound:
  820. self.migration_context = None
  821. elif db_context is None:
  822. self.migration_context = None
  823. else:
  824. self.migration_context = objects.MigrationContext.obj_from_db_obj(
  825. db_context)
  826. def _load_keypairs(self, db_keypairs=_NO_DATA_SENTINEL):
  827. if db_keypairs is _NO_DATA_SENTINEL:
  828. inst = objects.Instance.get_by_uuid(self._context, self.uuid,
  829. expected_attrs=['keypairs'])
  830. if 'keypairs' in inst:
  831. self.keypairs = inst.keypairs
  832. self.keypairs.obj_reset_changes(recursive=True)
  833. self.obj_reset_changes(['keypairs'])
  834. else:
  835. self.keypairs = objects.KeyPairList(objects=[])
  836. # NOTE(danms): We leave the keypairs attribute dirty in hopes
  837. # someone else will save it for us
  838. elif db_keypairs:
  839. self.keypairs = objects.KeyPairList.obj_from_primitive(
  840. jsonutils.loads(db_keypairs))
  841. self.obj_reset_changes(['keypairs'])
  842. def _load_tags(self):
  843. self.tags = objects.TagList.get_by_resource_id(
  844. self._context, self.uuid)
  845. def _load_trusted_certs(self, db_trusted_certs=_NO_DATA_SENTINEL):
  846. if db_trusted_certs is None:
  847. self.trusted_certs = None
  848. elif db_trusted_certs is _NO_DATA_SENTINEL:
  849. self.trusted_certs = objects.TrustedCerts.get_by_instance_uuid(
  850. self._context, self.uuid)
  851. else:
  852. self.trusted_certs = objects.TrustedCerts.obj_from_primitive(
  853. jsonutils.loads(db_trusted_certs))
  854. def apply_migration_context(self):
  855. if self.migration_context:
  856. self._set_migration_context_to_instance(prefix='new_')
  857. else:
  858. LOG.debug("Trying to apply a migration context that does not "
  859. "seem to be set for this instance", instance=self)
  860. def revert_migration_context(self):
  861. if self.migration_context:
  862. self._set_migration_context_to_instance(prefix='old_')
  863. else:
  864. LOG.debug("Trying to revert a migration context that does not "
  865. "seem to be set for this instance", instance=self)
  866. def _set_migration_context_to_instance(self, prefix):
  867. for inst_attr_name in _MIGRATION_CONTEXT_ATTRS:
  868. setattr(self, inst_attr_name, None)
  869. attr_name = prefix + inst_attr_name
  870. if attr_name in self.migration_context:
  871. attr_value = getattr(
  872. self.migration_context, attr_name)
  873. setattr(self, inst_attr_name, attr_value)
  874. @contextlib.contextmanager
  875. def mutated_migration_context(self):
  876. """Context manager to temporarily apply the migration context.
  877. Calling .save() from within the context manager means that the mutated
  878. context will be saved which can cause incorrect resource tracking, and
  879. should be avoided.
  880. """
  881. # First check to see if we even have a migration context set and if not
  882. # we can exit early without lazy-loading other attributes.
  883. if 'migration_context' in self and self.migration_context is None:
  884. yield
  885. return
  886. current_values = {}
  887. for attr_name in _MIGRATION_CONTEXT_ATTRS:
  888. current_values[attr_name] = getattr(self, attr_name)
  889. self.apply_migration_context()
  890. try:
  891. yield
  892. finally:
  893. for attr_name in _MIGRATION_CONTEXT_ATTRS:
  894. setattr(self, attr_name, current_values[attr_name])
  895. @base.remotable
  896. def drop_migration_context(self):
  897. if self.migration_context:
  898. db.instance_extra_update_by_uuid(self._context, self.uuid,
  899. {'migration_context': None})
  900. self.migration_context = None
  901. def clear_numa_topology(self):
  902. numa_topology = self.numa_topology
  903. if numa_topology is not None:
  904. self.numa_topology = numa_topology.clear_host_pinning()
  905. def obj_load_attr(self, attrname):
  906. # NOTE(danms): We can't lazy-load anything without a context and a uuid
  907. if not self._context:
  908. raise exception.OrphanedObjectError(method='obj_load_attr',
  909. objtype=self.obj_name())
  910. if 'uuid' not in self:
  911. raise exception.ObjectActionError(
  912. action='obj_load_attr',
  913. reason=_('attribute %s not lazy-loadable') % attrname)
  914. LOG.debug("Lazy-loading '%(attr)s' on %(name)s uuid %(uuid)s",
  915. {'attr': attrname,
  916. 'name': self.obj_name(),
  917. 'uuid': self.uuid,
  918. })
  919. with utils.temporary_mutation(self._context, read_deleted='yes'):
  920. self._obj_load_attr(attrname)
  921. def _obj_load_attr(self, attrname):
  922. """Internal method for loading attributes from instances.
  923. NOTE: Do not use this directly.
  924. This method contains the implementation of lazy-loading attributes
  925. from Instance object, minus some massaging of the context and
  926. error-checking. This should always be called with the object-local
  927. context set for reading deleted instances and with uuid set. All
  928. of the code below depends on those two things. Thus, this should
  929. only be called from obj_load_attr() itself.
  930. :param attrname: The name of the attribute to be loaded
  931. """
  932. # NOTE(danms): We handle some fields differently here so that we
  933. # can be more efficient
  934. if attrname == 'fault':
  935. self._load_fault()
  936. elif attrname == 'numa_topology':
  937. self._load_numa_topology()
  938. elif attrname == 'device_metadata':
  939. self._load_device_metadata()
  940. elif attrname == 'pci_requests':
  941. self._load_pci_requests()
  942. elif attrname == 'vcpu_model':
  943. self._load_vcpu_model()
  944. elif attrname == 'ec2_ids':
  945. self._load_ec2_ids()
  946. elif attrname == 'migration_context':
  947. self._load_migration_context()
  948. elif attrname == 'keypairs':
  949. # NOTE(danms): Let keypairs control its own destiny for
  950. # resetting changes.
  951. return self._load_keypairs()
  952. elif attrname == 'trusted_certs':
  953. return self._load_trusted_certs()
  954. elif attrname == 'security_groups':
  955. self._load_security_groups()
  956. elif attrname == 'pci_devices':
  957. self._load_pci_devices()
  958. elif 'flavor' in attrname:
  959. self._load_flavor()
  960. elif attrname == 'services' and self.deleted:
  961. # NOTE(mriedem): The join in the data model for instances.services
  962. # filters on instances.deleted == 0, so if the instance is deleted
  963. # don't attempt to even load services since we'll fail.
  964. self.services = objects.ServiceList(self._context)
  965. elif attrname == 'tags':
  966. if self.deleted:
  967. # NOTE(mriedem): Same story as services, the DB API query
  968. # in instance_tag_get_by_instance_uuid will fail if the
  969. # instance has been deleted so just return an empty tag list.
  970. self.tags = objects.TagList(self._context)
  971. else:
  972. self._load_tags()
  973. elif attrname in self.fields and attrname != 'id':
  974. # NOTE(danms): We've never let 'id' be lazy-loaded, and use its
  975. # absence as a sentinel that it hasn't been created in the database
  976. # yet, so refuse to do so here.
  977. self._load_generic(attrname)
  978. else:
  979. # NOTE(danms): This is historically what we did for
  980. # something not in a field that was force-loaded. So, just
  981. # do this for consistency.
  982. raise exception.ObjectActionError(
  983. action='obj_load_attr',
  984. reason=_('attribute %s not lazy-loadable') % attrname)
  985. self.obj_reset_changes([attrname])
  986. def get_flavor(self, namespace=None):
  987. prefix = ('%s_' % namespace) if namespace is not None else ''
  988. attr = '%sflavor' % prefix
  989. try:
  990. return getattr(self, attr)
  991. except exception.FlavorNotFound:
  992. # NOTE(danms): This only happens in the case where we don't
  993. # have flavor information in instance_extra, and doing
  994. # this triggers a lookup based on our instance_type_id for
  995. # (very) legacy instances. That legacy code expects a None here,
  996. # so emulate it for this helper, even though the actual attribute
  997. # is not nullable.
  998. return None
  999. @base.remotable
  1000. def delete_metadata_key(self, key):
  1001. """Optimized metadata delete method.
  1002. This provides a more efficient way to delete a single metadata
  1003. key, instead of just calling instance.save(). This should be called
  1004. with the key still present in self.metadata, which it will update
  1005. after completion.
  1006. """
  1007. db.instance_metadata_delete(self._context, self.uuid, key)
  1008. md_was_changed = 'metadata' in self.obj_what_changed()
  1009. del self.metadata[key]
  1010. self._orig_metadata.pop(key, None)
  1011. notifications.send_update(self._context, self, self)
  1012. if not md_was_changed:
  1013. self.obj_reset_changes(['metadata'])
  1014. def get_network_info(self):
  1015. if self.info_cache is None:
  1016. return network_model.NetworkInfo.hydrate([])
  1017. return self.info_cache.network_info
  1018. def get_bdms(self):
  1019. return objects.BlockDeviceMappingList.get_by_instance_uuid(
  1020. self._context, self.uuid)
  1021. def _make_instance_list(context, inst_list, db_inst_list, expected_attrs):
  1022. get_fault = expected_attrs and 'fault' in expected_attrs
  1023. inst_faults = {}
  1024. if get_fault:
  1025. # Build an instance_uuid:latest-fault mapping
  1026. expected_attrs.remove('fault')
  1027. instance_uuids = [inst['uuid'] for inst in db_inst_list]
  1028. faults = objects.InstanceFaultList.get_by_instance_uuids(
  1029. context, instance_uuids)
  1030. for fault in faults:
  1031. if fault.instance_uuid not in inst_faults:
  1032. inst_faults[fault.instance_uuid] = fault
  1033. inst_cls = objects.Instance
  1034. inst_list.objects = []
  1035. for db_inst in db_inst_list:
  1036. inst_obj = inst_cls._from_db_object(
  1037. context, inst_cls(context), db_inst,
  1038. expected_attrs=expected_attrs)
  1039. if get_fault:
  1040. inst_obj.fault = inst_faults.get(inst_obj.uuid, None)
  1041. inst_list.objects.append(inst_obj)
  1042. inst_list.obj_reset_changes()
  1043. return inst_list
  1044. @db_api.pick_context_manager_writer
  1045. def populate_missing_availability_zones(context, count):
  1046. # instances without host have no reasonable AZ to set
  1047. not_empty_host = models.Instance.host != None # noqa E711
  1048. instances = (context.session.query(models.Instance).
  1049. filter(not_empty_host).
  1050. filter_by(availability_zone=None).limit(count).all())
  1051. count_all = len(instances)
  1052. count_hit = 0
  1053. for instance in instances:
  1054. az = avail_zone.get_instance_availability_zone(context, instance)
  1055. instance.availability_zone = az
  1056. instance.save(context.session)
  1057. count_hit += 1
  1058. return count_all, count_hit
  1059. @base.NovaObjectRegistry.register
  1060. class InstanceList(base.ObjectListBase, base.NovaObject):
  1061. # Version 2.0: Initial Version
  1062. # Version 2.1: Add get_uuids_by_host()
  1063. # Version 2.2: Pagination for get_active_by_window_joined()
  1064. # Version 2.3: Add get_count_by_vm_state()
  1065. # Version 2.4: Add get_counts()
  1066. VERSION = '2.4'
  1067. fields = {
  1068. 'objects': fields.ListOfObjectsField('Instance'),
  1069. }
  1070. @classmethod
  1071. @db.select_db_reader_mode
  1072. def _get_by_filters_impl(cls, context, filters,
  1073. sort_key='created_at', sort_dir='desc', limit=None,
  1074. marker=None, expected_attrs=None, use_slave=False,
  1075. sort_keys=None, sort_dirs=None):
  1076. if sort_keys or sort_dirs:
  1077. db_inst_list = db.instance_get_all_by_filters_sort(
  1078. context, filters, limit=limit, marker=marker,
  1079. columns_to_join=_expected_cols(expected_attrs),
  1080. sort_keys=sort_keys, sort_dirs=sort_dirs)
  1081. else:
  1082. db_inst_list = db.instance_get_all_by_filters(
  1083. context, filters, sort_key, sort_dir, limit=limit,
  1084. marker=marker, columns_to_join=_expected_cols(expected_attrs))
  1085. return db_inst_list
  1086. @base.remotable_classmethod
  1087. def get_by_filters(cls, context, filters,
  1088. sort_key='created_at', sort_dir='desc', limit=None,
  1089. marker=None, expected_attrs=None, use_slave=False,
  1090. sort_keys=None, sort_dirs=None):
  1091. db_inst_list = cls._get_by_filters_impl(
  1092. context, filters, sort_key=sort_key, sort_dir=sort_dir,
  1093. limit=limit, marker=marker, expected_attrs=expected_attrs,
  1094. use_slave=use_slave, sort_keys=sort_keys, sort_dirs=sort_dirs)
  1095. # NOTE(melwitt): _make_instance_list could result in joined objects'
  1096. # (from expected_attrs) _from_db_object methods being called during
  1097. # Instance._from_db_object, each of which might choose to perform
  1098. # database writes. So, we call this outside of _get_by_filters_impl to
  1099. # avoid being nested inside a 'reader' database transaction context.
  1100. return _make_instance_list(context, cls(), db_inst_list,
  1101. expected_attrs)
  1102. @staticmethod
  1103. @db.select_db_reader_mode
  1104. def _db_instance_get_all_by_host(context, host, columns_to_join,
  1105. use_slave=False):
  1106. return db.instance_get_all_by_host(context, host,
  1107. columns_to_join=columns_to_join)
  1108. @base.remotable_classmethod
  1109. def get_by_host(cls, context, host, expected_attrs=None, use_slave=False):
  1110. db_inst_list = cls._db_instance_get_all_by_host(
  1111. context, host, columns_to_join=_expected_cols(expected_attrs),
  1112. use_slave=use_slave)
  1113. return _make_instance_list(context, cls(), db_inst_list,
  1114. expected_attrs)
  1115. @base.remotable_classmethod
  1116. def get_by_host_and_node(cls, context, host, node, expected_attrs=None):
  1117. db_inst_list = db.instance_get_all_by_host_and_node(
  1118. context, host, node,
  1119. columns_to_join=_expected_cols(expected_attrs))
  1120. return _make_instance_list(context, cls(), db_inst_list,
  1121. expected_attrs)
  1122. @base.remotable_classmethod
  1123. def get_by_host_and_not_type(cls, context, host, type_id=None,
  1124. expected_attrs=None):
  1125. db_inst_list = db.instance_get_all_by_host_and_not_type(
  1126. context, host, type_id=type_id)
  1127. return _make_instance_list(context, cls(), db_inst_list,
  1128. expected_attrs)
  1129. @base.remotable_classmethod
  1130. def get_all(cls, context, expected_attrs=None):
  1131. """Returns all instances on all nodes."""
  1132. db_instances = db.instance_get_all(
  1133. context, columns_to_join=_expected_cols(expected_attrs))
  1134. return _make_instance_list(context, cls(), db_instances,
  1135. expected_attrs)
  1136. @base.remotable_classmethod
  1137. def get_hung_in_rebooting(cls, context, reboot_window,
  1138. expected_attrs=None):
  1139. db_inst_list = db.instance_get_all_hung_in_rebooting(context,
  1140. reboot_window)
  1141. return _make_instance_list(context, cls(), db_inst_list,
  1142. expected_attrs)
  1143. @staticmethod
  1144. @db.select_db_reader_mode
  1145. def _db_instance_get_active_by_window_joined(
  1146. context, begin, end, project_id, host, columns_to_join,
  1147. use_slave=False, limit=None, marker=None):
  1148. return db.instance_get_active_by_window_joined(
  1149. context, begin, end, project_id, host,
  1150. columns_to_join=columns_to_join, limit=limit, marker=marker)
  1151. @base.remotable_classmethod
  1152. def _get_active_by_window_joined(cls, context, begin, end=None,
  1153. project_id=None, host=None,
  1154. expected_attrs=None, use_slave=False,
  1155. limit=None, marker=None):
  1156. # NOTE(mriedem): We need to convert the begin/end timestamp strings
  1157. # to timezone-aware datetime objects for the DB API call.
  1158. begin = timeutils.parse_isotime(begin)
  1159. end = timeutils.parse_isotime(end) if end else None
  1160. db_inst_list = cls._db_instance_get_active_by_window_joined(
  1161. context, begin, end, project_id, host,
  1162. columns_to_join=_expected_cols(expected_attrs),
  1163. use_slave=use_slave, limit=limit, marker=marker)
  1164. return _make_instance_list(context, cls(), db_inst_list,
  1165. expected_attrs)
  1166. @classmethod
  1167. def get_active_by_window_joined(cls, context, begin, end=None,
  1168. project_id=None, host=None,
  1169. expected_attrs=None, use_slave=False,
  1170. limit=None, marker=None):
  1171. """Get instances and joins active during a certain time window.
  1172. :param:context: nova request context
  1173. :param:begin: datetime for the start of the time window
  1174. :param:end: datetime for the end of the time window
  1175. :param:project_id: used to filter instances by project
  1176. :param:host: used to filter instances on a given compute host
  1177. :param:expected_attrs: list of related fields that can be joined
  1178. in the database layer when querying for instances
  1179. :param use_slave if True, ship this query off to a DB slave
  1180. :param limit: maximum number of instances to return per page
  1181. :param marker: last instance uuid from the previous page
  1182. :returns: InstanceList
  1183. """
  1184. # NOTE(mriedem): We have to convert the datetime objects to string
  1185. # primitives for the remote call.
  1186. begin = utils.isotime(begin)
  1187. end = utils.isotime(end) if end else None
  1188. return cls._get_active_by_window_joined(context, begin, end,
  1189. project_id, host,
  1190. expected_attrs,
  1191. use_slave=use_slave,
  1192. limit=limit, marker=marker)
  1193. @base.remotable_classmethod
  1194. def get_by_security_group_id(cls, context, security_group_id):
  1195. db_secgroup = db.security_group_get(
  1196. context, security_group_id,
  1197. columns_to_join=['instances.info_cache',
  1198. 'instances.system_metadata'])
  1199. return _make_instance_list(context, cls(), db_secgroup['instances'],
  1200. ['info_cache', 'system_metadata'])
  1201. @classmethod
  1202. def get_by_security_group(cls, context, security_group):
  1203. return cls.get_by_security_group_id(context, security_group.id)
  1204. @base.remotable_classmethod
  1205. def get_by_grantee_security_group_ids(cls, context, security_group_ids):
  1206. db_instances = db.instance_get_all_by_grantee_security_groups(
  1207. context, security_group_ids)
  1208. return _make_instance_list(context, cls(), db_instances, [])
  1209. def fill_faults(self):
  1210. """Batch query the database for our instances' faults.
  1211. :returns: A list of instance uuids for which faults were found.
  1212. """
  1213. uuids = [inst.uuid for inst in self]
  1214. faults = objects.InstanceFaultList.get_latest_by_instance_uuids(
  1215. self._context, uuids)
  1216. faults_by_uuid = {}
  1217. for fault in faults:
  1218. faults_by_uuid[fault.instance_uuid] = fault
  1219. for instance in self:
  1220. if instance.uuid in faults_by_uuid:
  1221. instance.fault = faults_by_uuid[instance.uuid]
  1222. else:
  1223. # NOTE(danms): Otherwise the caller will cause a lazy-load
  1224. # when checking it, and we know there are none
  1225. instance.fault = None
  1226. instance.obj_reset_changes(['fault'])
  1227. return faults_by_uuid.keys()
  1228. @base.remotable_classmethod
  1229. def get_uuids_by_host(cls, context, host):
  1230. return db.instance_get_all_uuids_by_host(context, host)
  1231. @staticmethod
  1232. @db_api.pick_context_manager_reader
  1233. def _get_count_by_vm_state_in_db(context, project_id, user_id, vm_state):
  1234. return context.session.query(models.Instance.id).\
  1235. filter_by(deleted=0).\
  1236. filter_by(project_id=project_id).\
  1237. filter_by(user_id=user_id).\
  1238. filter_by(vm_state=vm_state).\
  1239. count()
  1240. @base.remotable_classmethod
  1241. def get_count_by_vm_state(cls, context, project_id, user_id, vm_state):
  1242. return cls._get_count_by_vm_state_in_db(context, project_id, user_id,
  1243. vm_state)
  1244. @staticmethod
  1245. @db_api.pick_context_manager_reader
  1246. def _get_counts_in_db(context, project_id, user_id=None):
  1247. # NOTE(melwitt): Copied from nova/db/sqlalchemy/api.py:
  1248. # It would be better to have vm_state not be nullable
  1249. # but until then we test it explicitly as a workaround.
  1250. not_soft_deleted = or_(
  1251. models.Instance.vm_state != vm_states.SOFT_DELETED,
  1252. models.Instance.vm_state == null()
  1253. )
  1254. project_query = context.session.query(
  1255. func.count(models.Instance.id),
  1256. func.sum(models.Instance.vcpus),
  1257. func.sum(models.Instance.memory_mb)).\
  1258. filter_by(deleted=0).\
  1259. filter(not_soft_deleted).\
  1260. filter_by(project_id=project_id)
  1261. project_result = project_query.first()
  1262. fields = ('instances', 'cores', 'ram')
  1263. project_counts = {field: int(project_result[idx] or 0)
  1264. for idx, field in enumerate(fields)}
  1265. counts = {'project': project_counts}
  1266. if user_id:
  1267. user_result = project_query.filter_by(user_id=user_id).first()
  1268. user_counts = {field: int(user_result[idx] or 0)
  1269. for idx, field in enumerate(fields)}
  1270. counts['user'] = user_counts
  1271. return counts
  1272. @base.remotable_classmethod
  1273. def get_counts(cls, context, project_id, user_id=None):
  1274. """Get the counts of Instance objects in the database.
  1275. :param context: The request context for database access
  1276. :param project_id: The project_id to count across
  1277. :param user_id: The user_id to count across
  1278. :returns: A dict containing the project-scoped counts and user-scoped
  1279. counts if user_id is specified. For example:
  1280. {'project': {'instances': <count across project>,
  1281. 'cores': <count across project>,
  1282. 'ram': <count across project},
  1283. 'user': {'instances': <count across user>,
  1284. 'cores': <count across user>,
  1285. 'ram': <count across user>}}
  1286. """
  1287. return cls._get_counts_in_db(context, project_id, user_id=user_id)
  1288. @staticmethod
  1289. @db_api.pick_context_manager_reader
  1290. def _get_count_by_hosts(context, hosts):
  1291. return context.session.query(models.Instance).\
  1292. filter_by(deleted=0).\
  1293. filter(models.Instance.host.in_(hosts)).count()
  1294. @classmethod
  1295. def get_count_by_hosts(cls, context, hosts):
  1296. return cls._get_count_by_hosts(context, hosts)