diff --git a/common-powervc/powervc/common/client/extensions/nova.py b/common-powervc/powervc/common/client/extensions/nova.py index 25e077e..f20992a 100644 --- a/common-powervc/powervc/common/client/extensions/nova.py +++ b/common-powervc/powervc/common/client/extensions/nova.py @@ -40,6 +40,23 @@ class PVCHypervisorManager(hypervisors.HypervisorManager): feature to get/set the hypervisor status and maintenance mode. """ + def get_hypervisor_state(self, hostname): + """Get the hypervisor_state by hostname + """ + hypervisors = self.search(hostname) + + if not hypervisors[0] or not self.get(hypervisors[0]): + raise exc.HTTPNotFound(_("No hypervisor matching '%s' could be" + " found.") % hostname) + + try: + hypervisor = self.get(hypervisors[0]) + except Exception as ex: + raise exc.HTTPNotFound(explanation=six.text_type(ex)) + + hypervisor_state = getattr(hypervisor, "hypervisor_state", "operating") + return hypervisor_state + def get_host_maintenance_mode(self, hostname): """Get host maintenance mode by host name from PowerVC driver """ diff --git a/nova-powervc/powervc/nova/driver/compute/computes.py b/nova-powervc/powervc/nova/driver/compute/computes.py index 28e657b..48ad32f 100644 --- a/nova-powervc/powervc/nova/driver/compute/computes.py +++ b/nova-powervc/powervc/nova/driver/compute/computes.py @@ -188,16 +188,27 @@ class ComputeServiceManager(object): to the remote_service. This method assumes the local service already exists. """ - local_service = self.services[remote_service.host] + hostname = remote_service.host + local_service = self.services[hostname] if local_service is None: - LOG.debug("local service not found for %s" % remote_service.host) + LOG.debug("local service not found for %s" % hostname) return - if remote_service.state == "down" and local_service.started: - LOG.debug("Stopping remote service %s" % local_service.host) + hypervisor_state = self._get_hypervisor_state_by_hostname(hostname) + if (remote_service.state == "down" or hypervisor_state != "operating")\ + and local_service.started: + LOG.info("Stopping remote service %s, as hypervisor state is %s," + "service state is %s." % (local_service.host, + hypervisor_state, + remote_service.state)) local_service.stop() return - if remote_service.state == "up" and not local_service.started: + if (remote_service.state == "up" and hypervisor_state == "operating")\ + and not local_service.started: LOG.debug("Starting remote service %s" % local_service.host) + LOG.info("Starting remote service %s, as hypervisor state is %s," + "service state is %s." % (local_service.host, + hypervisor_state, + remote_service.state)) local_service.start() def _stop_local_services(self): @@ -238,3 +249,10 @@ class ComputeServiceManager(object): filtered_services.append(remote_service) return filtered_services + + def _get_hypervisor_state_by_hostname(self, hostname): + hypervisor_state = self.driver._service._client.\ + hypervisors.get_hypervisor_state(hostname) + LOG.info("Get hypervisor state of '%s' is : '%s'." % + (hostname, hypervisor_state)) + return hypervisor_state