return _ParseXmList(lines, include_node)
+def _IsInstanceRunning(instance_info):
+ return instance_info == "r-----" \
+ or instance_info == "-b----"
+
+
+def _IsInstanceShutdown(instance_info):
+ return instance_info == "---s--"
+
+
def _ParseNodeInfo(info):
"""Return information about the node.
return self._StopInstance(name, force)
+ def _ShutdownInstance(self, name):
+ """Shutdown an instance if the instance is running.
+
+ @type name: string
+ @param name: name of the instance to stop
+
+ The '-w' flag waits for shutdown to complete which avoids the need
+ to poll in the case where we want to destroy the domain
+ immediately after shutdown.
+
+ """
+ instance_info = self.GetInstanceInfo(name)
+
+ if instance_info is None or _IsInstanceShutdown(instance_info[4]):
+ logging.info("Failed to shutdown instance %s, not running", name)
+ return None
+
+ return self._RunXen(["shutdown", "-w", name])
+
+ def _DestroyInstance(self, name):
+ """Destroy an instance if the instance if the instance exists.
+
+ @type name: string
+ @param name: name of the instance to destroy
+
+ """
+ instance_info = self.GetInstanceInfo(name)
+
+ if instance_info is None:
+ logging.info("Failed to destroy instance %s, does not exist", name)
+ return None
+
+ return self._RunXen(["destroy", name])
+
def _StopInstance(self, name, force):
"""Stop an instance.
+ @type name: string
+ @param name: name of the instance to destroy
+
+ @type force: boolean
+ @param force: whether to do a "hard" stop (destroy)
+
"""
if force:
- action = "destroy"
+ result = self._DestroyInstance(name)
else:
- action = "shutdown"
+ self._ShutdownInstance(name)
+ result = self._DestroyInstance(name)
- result = self._RunXen([action, name])
- if result.failed:
+ if result is not None and result.failed and \
+ self.GetInstanceInfo(name) is not None:
raise errors.HypervisorError("Failed to stop instance %s: %s, %s" %
(name, result.fail_reason, result.output))