315
315
block_device_info = self._prep_block_device(context, instance)
316
316
instance = self._spawn(context, instance, image_meta,
317
317
network_info, block_device_info,
318
injected_files, admin_pass)
318
injected_files, admin_password)
320
self._deallocate_network(context, instance)
320
with utils.save_and_reraise_exception():
321
self._deallocate_network(context, instance)
322
322
self._notify_about_instance_usage(instance)
323
323
except exception.InstanceNotFound:
324
324
LOG.exception(_("Instance %s not found.") % instance_uuid)
1078
1081
disk_info = self.driver.migrate_disk_and_power_off(
1079
context, instance_ref, migration_ref['dest_host'])
1080
except exception.MigrationError, error:
1081
LOG.error(_('%s. Setting instance vm_state to ERROR') % (error,))
1082
self._instance_update(context,
1084
vm_state=vm_states.ERROR)
1082
context, instance_ref, migration_ref['dest_host'],
1084
except Exception, error:
1085
with utils.save_and_reraise_exception():
1086
msg = _('%s. Setting instance vm_state to ERROR')
1087
LOG.error(msg % error)
1088
self._instance_update(context,
1090
vm_state=vm_states.ERROR)
1087
1092
self.db.migration_update(context,
1133
1138
# Have to look up image here since we depend on disk_format later
1134
1139
image_meta = _get_image_meta(context, instance_ref['image_ref'])
1136
self.driver.finish_migration(context, migration_ref, instance_ref,
1137
disk_info, network_info, image_meta,
1142
self.driver.finish_migration(context, migration_ref, instance_ref,
1143
disk_info, network_info, image_meta,
1145
except Exception, error:
1146
with utils.save_and_reraise_exception():
1147
msg = _('%s. Setting instance vm_state to ERROR')
1148
LOG.error(msg % error)
1149
self._instance_update(context,
1151
vm_state=vm_states.ERROR)
1140
1153
self._instance_update(context,
1794
1807
self.driver.destroy(instance_ref, network_info,
1795
1808
block_device_info, True)
1797
def periodic_tasks(self, context=None):
1798
"""Tasks to be run at a periodic interval."""
1799
error_list = super(ComputeManager, self).periodic_tasks(context)
1800
if error_list is None:
1804
if FLAGS.reboot_timeout > 0:
1805
self.driver.poll_rebooting_instances(FLAGS.reboot_timeout)
1806
except Exception as ex:
1807
LOG.warning(_("Error during poll_rebooting_instances: %s"),
1809
error_list.append(ex)
1812
if FLAGS.rescue_timeout > 0:
1813
self.driver.poll_rescued_instances(FLAGS.rescue_timeout)
1814
except Exception as ex:
1815
LOG.warning(_("Error during poll_rescued_instances: %s"),
1817
error_list.append(ex)
1820
if FLAGS.resize_confirm_window > 0:
1821
self.driver.poll_unconfirmed_resizes(
1822
FLAGS.resize_confirm_window)
1823
except Exception as ex:
1824
LOG.warning(_("Error during poll_unconfirmed_resizes: %s"),
1826
error_list.append(ex)
1829
self._report_driver_status()
1830
except Exception as ex:
1831
LOG.warning(_("Error during report_driver_status(): %s"),
1833
error_list.append(ex)
1836
self._sync_power_states(context)
1837
except Exception as ex:
1838
LOG.warning(_("Error during power_state sync: %s"), unicode(ex))
1839
error_list.append(ex)
1842
self._reclaim_queued_deletes(context)
1843
except Exception as ex:
1844
LOG.warning(_("Error during reclamation of queued deletes: %s"),
1846
error_list.append(ex)
1848
start = utils.current_audit_period()[1]
1849
self._update_bandwidth_usage(context, start)
1850
except NotImplementedError:
1851
# Not all hypervisors have bandwidth polling implemented yet.
1852
# If they don't id doesn't break anything, they just don't get the
1853
# info in the usage events. (mdragon)
1855
except Exception as ex:
1856
LOG.warning(_("Error updating bandwidth usage: %s"),
1858
error_list.append(ex)
1862
def _update_bandwidth_usage(self, context, start_time, stop_time=None):
1810
@manager.periodic_task
1811
def _poll_rebooting_instances(self, context):
1812
if FLAGS.reboot_timeout > 0:
1813
self.driver.poll_rebooting_instances(FLAGS.reboot_timeout)
1815
@manager.periodic_task
1816
def _poll_rescued_instances(self, context):
1817
if FLAGS.rescue_timeout > 0:
1818
self.driver.poll_rescued_instances(FLAGS.rescue_timeout)
1820
@manager.periodic_task
1821
def _poll_unconfirmed_resizes(self, context):
1822
if FLAGS.resize_confirm_window > 0:
1823
self.driver.poll_unconfirmed_resizes(FLAGS.resize_confirm_window)
1825
@manager.periodic_task
1826
def _poll_bandwidth_usage(self, context, start_time=None, stop_time=None):
1828
start_time = utils.current_audit_period()[1]
1863
1830
curr_time = time.time()
1864
1831
if curr_time - self._last_bw_usage_poll > FLAGS.bandwith_poll_interval:
1865
1832
self._last_bw_usage_poll = curr_time
1866
1833
LOG.info(_("Updating bandwidth usage cache"))
1867
bw_usage = self.driver.get_all_bw_usage(start_time, stop_time)
1836
bw_usage = self.driver.get_all_bw_usage(start_time, stop_time)
1837
except NotImplementedError:
1838
# NOTE(mdragon): Not all hypervisors have bandwidth polling
1839
# implemented yet. If they don't it doesn't break anything,
1840
# they just don't get the info in the usage events.
1868
1843
for usage in bw_usage:
1869
1844
vif = usage['virtual_interface']
1870
1845
self.db.bw_usage_update(context,
1921
1898
db_instance["id"],
1922
1899
power_state=vm_power_state)
1901
@manager.periodic_task
1924
1902
def _reclaim_queued_deletes(self, context):
1925
1903
"""Reclaim instances that are queued for deletion."""
1904
if FLAGS.reclaim_instance_interval <= 0:
1905
LOG.debug(_("FLAGS.reclaim_instance_interval <= 0, skipping..."))
1927
1908
instances = self.db.instance_get_all_by_host(context, self.host)
1929
queue_time = datetime.timedelta(
1930
seconds=FLAGS.reclaim_instance_interval)
1931
curtime = utils.utcnow()
1932
1909
for instance in instances:
1933
if instance['vm_state'] == vm_states.SOFT_DELETE and \
1934
(curtime - instance['deleted_at']) >= queue_time:
1935
LOG.info('Deleting %s' % instance['name'])
1910
old_enough = (not instance.deleted_at or utils.is_older_than(
1911
instance.deleted_at,
1912
FLAGS.reclaim_instance_interval))
1913
soft_deleted = instance.vm_state == vm_states.SOFT_DELETE
1915
if soft_deleted and old_enough:
1916
instance_id = instance.id
1917
LOG.info(_("Reclaiming deleted instance %(instance_id)s"),
1936
1919
self._delete_instance(context, instance)