Enhance, simplify and beautify DRAC & HPiLO support with expect scripts
[pcucontrol.git] / pcucontrol / models / HPiLO.py
index 2037ab7..e0595be 100644 (file)
@@ -2,8 +2,8 @@ from pcucontrol.reboot import *
 from distutils.sysconfig import get_python_lib; 
 import subprocess
 
-class HPiLO(PCUControl):
-    supported_ports = [22,443]
+class HPiLO(BasicPCUControl):
+    supported_ports = [443,22]
     def run(self, node_port, dryrun):
         if self.type == Transport.SSH:
             return self.run_ssh(node_port, dryrun)
@@ -12,67 +12,26 @@ class HPiLO(PCUControl):
         else:
             raise ExceptionNoTransport("Unimplemented Transport for HPiLO %s" % self.type)
 
-    def run_ssh(self, node_port, dryrun):
-
-        self.transport.open(self.host, self.username)
-        self.transport.sendPassword(self.password)
-
-        # </>hpiLO-> 
-        self.transport.ifThenSend("</>hpiLO->", "cd system1")
-
-        # Reboot Outlet  N      (Y/N)?
-        if dryrun:
-            self.transport.ifThenSend("</system1>hpiLO->", "POWER")
-        else:
-            # Reset this machine
-            self.transport.ifThenSend("</system1>hpiLO->", "reset")
+    def pcu_run(self, node_port):
+        r = self.run_https(node_port, dryrun=False)
+        if "No error" in r:
+            return r
+        r2 = self.run_ssh(node_port, dryrun=False)
+        if "No error" in r2:
+            return r2
+        return r + " :: " +r2
+
+    def pcu_test(self, node_port):
+        r = self.run_https(node_port, dryrun=True)
+        if "No error" in r:
+            return r
+        r2 = self.run_ssh(node_port, dryrun=True)
+        if "No error" in r2:
+            return r2
+        return r + " :: " +r2
 
-        self.transport.ifThenSend("</system1>hpiLO->", "exit")
-
-        self.transport.close()
-
-        # NOTE: if an error occurs earlier, an exception should be thrown
-        return 0
+    def run_ssh(self, node_port, dryrun):
+        return self.run_expect_script("HPiLO.exp ssh", dryrun=dryrun, model="None")
         
     def run_https(self, node_port, dryrun):
-
-        locfg = command.CMD()
-
-        cmd_str = get_python_lib(1) + "/pcucontrol/models/hpilo/"
-        
-        cmd = cmd_str + "locfg.pl -s %s -f %s -u %s -p '%s' "  % (
-                    self.host, cmd_str+"iloxml/Get_Network.xml", 
-                    self.username, self.password)
-        cmd_out, cmd_err = locfg.run_noexcept(cmd)
-
-        if locfg.s.returncode != 0:
-            return cmd_out.strip() + cmd_err.strip()
-
-        if dryrun: 
-            return 0
-
-        locfg = command.CMD()
-        cmd = cmd_str + "locfg.pl -s %s -f %s -u %s -p '%s'" % (
-                    self.host, cmd_str+"iloxml/PowerOn_Server.xml", 
-                    self.username, self.password)
-        cmd_out, cmd_err = locfg.run_noexcept(cmd)
-
-        locfg = command.CMD()
-        cmd = cmd_str + "locfg.pl -s %s -f %s -u %s -p '%s'" % (
-                    self.host, cmd_str+"iloxml/Reset_Server.xml", 
-                    self.username, self.password)
-        cmd_out, cmd_err = locfg.run_noexcept(cmd)
-
-        cmd = "grep 'MESSAGE' | grep -v 'No error' | grep -v 'Server being reset.'"
-        p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
-                            stderr=subprocess.STDOUT, close_fds=True)
-        (grep_in, grep_out ) = (p.stdin, p.stdout)
-        grep_in.write(cmd_out)
-        grep_in.close()         # close so read does not block
-        output = grep_out.read()
-        if output.strip() != "":
-            print "grep_out: %s" % output.strip()
-            return output.strip()
-
-        # NOTE: if an error occurs earlier, an exception should be thrown
-        return 0
+        return self.run_expect_script("HPiLO.exp https", dryrun=dryrun, model="None")