[Branch,~linaro-validation/lava-dispatcher/trunk] Rev 634: Add get_power_status function to ipmi api.

Message ID 20130709073813.2358.53362.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

Tyler Baker July 9, 2013, 7:38 a.m.
Merge authors:
  Arthur She (arthur-she)
Related merge proposals:
  https://code.launchpad.net/~arthur-she/lava-dispatcher/add-get_power_status-to-ipmi/+merge/172075
  proposed by: Arthur She (arthur-she)
  review: Approve - Tyler Baker (tyler-baker)
------------------------------------------------------------
revno: 634 [merge]
committer: Tyler Baker <tyler.baker@linaro.org>
branch nick: lava-dispatcher
timestamp: Tue 2013-07-09 00:37:43 -0700
message:
  Add get_power_status function to ipmi api.
modified:
  lava_dispatcher/context.py
  lava_dispatcher/ipmi.py


--
lp:lava-dispatcher
https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk

You are subscribed to branch lp:lava-dispatcher.
To unsubscribe from this branch go to https://code.launchpad.net/~linaro-validation/lava-dispatcher/trunk/+edit-subscription

Patch

=== modified file 'lava_dispatcher/context.py'
--- lava_dispatcher/context.py	2013-06-11 20:27:05 +0000
+++ lava_dispatcher/context.py	2013-06-28 12:34:59 +0000
@@ -140,3 +140,10 @@ 
             rc = subprocess.check_call(command, **output_args)
         return rc
 
+    def run_command_get_output(self, command):
+        """run command 'command' then return the command output"""
+        if isinstance(command, (str, unicode)):
+            command = ['sh', '-c', command]
+        logging.debug("Executing on host : '%r'" % command)
+        return subprocess.check_output(command) 
+

=== modified file 'lava_dispatcher/ipmi.py'
--- lava_dispatcher/ipmi.py	2013-05-29 22:07:56 +0000
+++ lava_dispatcher/ipmi.py	2013-07-09 05:54:26 +0000
@@ -41,6 +41,12 @@ 
             failok=False
         )
 
+    def __ipmi_cmd_output(self, command):
+        return self.context.run_command_get_output(
+            "%s -H %s -U admin -P admin %s" % (
+                self.ipmitool, self.host, command)
+        )
+
     def set_to_boot_from_disk(self):
         self.__ipmi("chassis bootdev disk")
 
@@ -56,6 +62,11 @@ 
     def reset(self):
         self.__ipmi("chassis power reset")
 
+    def get_power_status(self):
+        """ Command 'ipmitool power status' will output 'Chassis Power is on'
+            or 'Chassis Power is off' """
+        return self.__ipmi_cmd_output("power status").split(' ')[-1]
+
 class IpmiPxeBoot(object):
     """
     This class provides a convenient object-oriented API that can be
@@ -68,14 +79,17 @@ 
 
     def power_on_boot_master(self):
         self.ipmitool.set_to_boot_from_pxe()
-        self.ipmitool.power_off()
+        if self.ipmitool.get_power_status() == 'on':
+            self.ipmitool.power_off()
         self.ipmitool.power_on()
 
     def power_on_boot_image(self):
         self.ipmitool.set_to_boot_from_disk()
-        self.ipmitool.power_off()
+        if self.ipmitool.get_power_status() == 'on':
+            self.ipmitool.power_off()
         self.ipmitool.power_on()
 
     def power_off(self):
-        self.ipmitool.power_off()
+        if self.ipmitool.get_power_status() == 'on':
+            self.ipmitool.power_off()