From 32b1027815acc52ba2a112f65b2322164a3c781b Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Fri, 30 Nov 2012 11:32:48 -0500 Subject: Split out part of compute's init_host. The init_host() function in the compute manager has grown a good bit. The amount of nesting for instance initialization was pretty deep, making it a bit ugly. This patch splits out the code for initializing an instance during the service initialization and puts it into its own function, making things a bit more readable. Change-Id: I5183baa76304bfc04af3c7347218eefbc3dc84e3 --- nova/compute/manager.py | 107 +++++++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 52 deletions(-) diff --git a/nova/compute/manager.py b/nova/compute/manager.py index 1a844386e..4baeab255 100644 --- a/nova/compute/manager.py +++ b/nova/compute/manager.py @@ -352,69 +352,72 @@ class ComputeManager(manager.SchedulerDependentManager): 'trying to set it to ERROR'), instance_uuid=instance_uuid) - def init_host(self): - """Initialization for a standalone compute service.""" - self.driver.init_host(host=self.host) - context = nova.context.get_admin_context() - instances = self.db.instance_get_all_by_host(context, self.host) + def _init_instance(self, context, instance): + '''Initialize this instance during service init.''' + db_state = instance['power_state'] + drv_state = self._get_power_state(context, instance) + closing_vm_states = (vm_states.DELETED, + vm_states.SOFT_DELETED) + + # instance was supposed to shut down - don't attempt + # recovery in any case + if instance['vm_state'] in closing_vm_states: + return - if CONF.defer_iptables_apply: - self.driver.filter_defer_apply_on() + expect_running = (db_state == power_state.RUNNING and + drv_state != db_state) - try: - for instance in instances: - db_state = instance['power_state'] - drv_state = self._get_power_state(context, instance) - closing_vm_states = (vm_states.DELETED, - vm_states.SOFT_DELETED) - - # instance was supposed to shut down - don't attempt - # recovery in any case - if instance['vm_state'] in closing_vm_states: - continue + LOG.debug(_('Current state is %(drv_state)s, state in DB is ' + '%(db_state)s.'), locals(), instance=instance) - expect_running = (db_state == power_state.RUNNING and - drv_state != db_state) + net_info = compute_utils.get_nw_info_for_instance(instance) - LOG.debug(_('Current state is %(drv_state)s, state in DB is ' - '%(db_state)s.'), locals(), instance=instance) + # We're calling plug_vifs to ensure bridge and iptables + # rules exist. This needs to be called for each instance. + legacy_net_info = self._legacy_nw_info(net_info) + self.driver.plug_vifs(instance, legacy_net_info) - net_info = compute_utils.get_nw_info_for_instance(instance) + if expect_running and CONF.resume_guests_state_on_host_boot: + LOG.info( + _('Rebooting instance after nova-compute restart.'), + locals(), instance=instance) - # We're calling plug_vifs to ensure bridge and iptables - # rules exist. This needs to be called for each instance. - legacy_net_info = self._legacy_nw_info(net_info) - self.driver.plug_vifs(instance, legacy_net_info) + block_device_info = \ + self._get_instance_volume_block_device_info( + context, instance['uuid']) - if expect_running and CONF.resume_guests_state_on_host_boot: - LOG.info( - _('Rebooting instance after nova-compute restart.'), - locals(), instance=instance) + try: + self.driver.resume_state_on_host_boot( + context, + instance, + self._legacy_nw_info(net_info), + block_device_info) + except NotImplementedError: + LOG.warning(_('Hypervisor driver does not support ' + 'resume guests'), instance=instance) - block_device_info = \ - self._get_instance_volume_block_device_info( - context, instance['uuid']) + elif drv_state == power_state.RUNNING: + # VMWareAPI drivers will raise an exception + try: + self.driver.ensure_filtering_rules_for_instance( + instance, + self._legacy_nw_info(net_info)) + except NotImplementedError: + LOG.warning(_('Hypervisor driver does not support ' + 'firewall rules'), instance=instance) - try: - self.driver.resume_state_on_host_boot( - context, - instance, - self._legacy_nw_info(net_info), - block_device_info) - except NotImplementedError: - LOG.warning(_('Hypervisor driver does not support ' - 'resume guests'), instance=instance) + def init_host(self): + """Initialization for a standalone compute service.""" + self.driver.init_host(host=self.host) + context = nova.context.get_admin_context() + instances = self.db.instance_get_all_by_host(context, self.host) - elif drv_state == power_state.RUNNING: - # VMWareAPI drivers will raise an exception - try: - self.driver.ensure_filtering_rules_for_instance( - instance, - self._legacy_nw_info(net_info)) - except NotImplementedError: - LOG.warning(_('Hypervisor driver does not support ' - 'firewall rules'), instance=instance) + if CONF.defer_iptables_apply: + self.driver.filter_defer_apply_on() + try: + for instance in instances: + self._init_instance(context, instance) finally: if CONF.defer_iptables_apply: self.driver.filter_defer_apply_off() -- cgit