Verify expect versions of DRAC, HPiLO and updates to APCControl, & OpenIPMI
Stephen Soltesz [Wed, 8 Jun 2011 22:39:15 +0000 (18:39 -0400)]
pcucontrol/models/APCControl.py
pcucontrol/models/DRAC.py
pcucontrol/models/HPiLO.py
pcucontrol/models/OpenIPMI.py
pcucontrol/models/exp/DRAC.exp
pcucontrol/models/exp/HPiLO.exp
pcucontrol/reboot.py
pcucontrol/support/pcuProxy.php

index 59cc649..ebe43e2 100644 (file)
@@ -1,4 +1,5 @@
 from pcucontrol.reboot import *
+import subprocess
 
 class APCControl(PCUControl):
        supported_ports = [22,23,80,443]
@@ -68,7 +69,8 @@ class APCControl(PCUControl):
                cmd = cmd % ( self.username, self.password, self.host)
                print "CMD: %s" % cmd
 
-               p = os.popen(cmd)
+               p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
+
                result = p.read()
                if len(result.split('\n')) > 2:
                        self.logout()
@@ -132,7 +134,7 @@ class APCControl(PCUControl):
                        raise ExceptionNoTransport("Unsupported transport to get version")
 
                cmd = cmd % ( self.username, self.password, self.host)
-               p = os.popen(cmd)
+               p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
                result = p.read()
                return result.strip()
 
@@ -148,7 +150,7 @@ class APCControl(PCUControl):
                        raise ExceptionNoTransport("Unsupported transport to logout")
 
                cmd = cmd % ( self.username, self.password, self.host)
-               p = os.popen(cmd)
+               p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
                print p.read()
 
 class APCControl12p3(APCControl):
index 672d92f..d9e4fe6 100644 (file)
@@ -4,12 +4,12 @@ import time
 class DRAC(BasicPCUControl):
     supported_ports = [22,443,5869]
     def run_drac(self, node_port, dryrun):
-        return self.run_expect_script("DRAC.exp racadm5", dryrun=dryrun, model="None")
+        return self.run_expect_script("DRAC.exp racadm", dryrun=dryrun, model="None")
 
     def run_ssh(self, node_port, dryrun):
         return self.run_expect_script("DRAC.exp ssh", dryrun=dryrun, model="None")
 
-    def pcu_run(self, node_port):
+    def pcu_run(self, node_port, dryrun=False):
         r = self.run_ssh(node_port, dryrun=False)
         if "No error" in r:
             return r
@@ -18,7 +18,7 @@ class DRAC(BasicPCUControl):
             return r2
         return r + " :: " +r2
 
-    def pcu_test(self, node_port):
+    def pcu_test(self, node_port, dryrun=True):
         r = self.run_ssh(node_port, dryrun=True)
         if "No error" in r:
             return r
index e0595be..7a2863a 100644 (file)
@@ -12,7 +12,7 @@ class HPiLO(BasicPCUControl):
         else:
             raise ExceptionNoTransport("Unimplemented Transport for HPiLO %s" % self.type)
 
-    def pcu_run(self, node_port):
+    def pcu_run(self, node_port, dryrun=False):
         r = self.run_https(node_port, dryrun=False)
         if "No error" in r:
             return r
@@ -21,7 +21,7 @@ class HPiLO(BasicPCUControl):
             return r2
         return r + " :: " +r2
 
-    def pcu_test(self, node_port):
+    def pcu_test(self, node_port, dryrun=True):
         r = self.run_https(node_port, dryrun=True)
         if "No error" in r:
             return r
index 38ca7d8..f2bfc2d 100644 (file)
@@ -1,23 +1,32 @@
 
 from pcucontrol.reboot import *
+import subprocess
 
 class OpenIPMI(PCUControl):
 
        supported_ports = [80,443,623]
 
        # TODO: get exit codes to determine success or failure...
+       def run_http(self, node_port, dryrun):
+               return self.run_ipmi(node_port, dryrun)
+       def run_https(self, node_port, dryrun):
+               return self.run_ipmi(node_port, dryrun)
+
        def run_ipmi(self, node_port, dryrun):
 
                if not dryrun:
-                       cmd = "ipmitool -I lanplus -H %s -U %s -P '%s' power cycle  "
-                       (i,p) = os.popen4(cmd % ( self.host, self.username, self.password) )
-                       result = p.read()
-                       print "RESULT: ", result
+                       ipmi_cmd = "power cycle"
                else:
-                       cmd = "ipmitool -I lanplus -H %s -U %s -P '%s' user list  "
-                       (i,p) = os.popen4(cmd % ( self.host, self.username, self.password) )
-                       result = p.read()
-                       print "RESULT: ", result
+                       ipmi_cmd = "user list"
+
+               cmd = "ipmitool -I lanplus -H %s -U %s -P '%s' %s"
+               cmd = cmd % ( self.host, self.username, self.password, ipmi_cmd )
+               p = subprocess.Popen(cmd, shell=True, 
+                                          stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
+                                          stderr=subprocess.STDOUT, close_fds=True)
+               (i,p) = (p.stdin, p.stdout)
+               result = p.read()
+               #print "RESULT: ", result
 
                if "Error" in result:
                        return result
index efa4562..35e9184 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/expect
 
-set timeout 20
+set timeout 60
 set method [lindex $argv 0]
 set host [lindex $argv 1]
 set user [lindex $argv 2]
@@ -17,16 +17,16 @@ if { "$dryrun" == "True" } {
 
 
 if { "$method" == "ssh" } {
-    send_user "spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host\n"
+    #send_user "spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host\n"
     set cont 0
     for { set retry 0 } { $retry < 2 } { incr retry 1 } {
 
         # NOTE: For unknown reason, some DRAC modules work only with
         #       stderr redirected to stdout, others only work without it.
         if { $retry == 0 } {
-            spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 2>&1
-        } else {
             spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 
+        } else {
+            spawn ssh -o StrictHostKeyChecking=no -o PasswordAuthentication=yes -o PubkeyAuthentication=no $user@$host 2>&1
         }
 
         expect {
@@ -34,9 +34,11 @@ if { "$method" == "ssh" } {
             "password: " { send "$password\n" ; exp_continue }
             "Permission denied" { send_user "DRAC ssh: username/password: $expect_out(0,string)"; exit }
             "Could not resolve hostname" { send_user "DRAC ssh: $expect_out(0,string)"; exit }
+            "Connection timed out" { send_user "DRAC ssh: $expect_out(0,string)"; exit }
             -re "Received disconnect.*" { send_user "DRAC ssh: $expect_out(0,string)"; exit}
             -re "\\$|/.*>|\[$user\]#" { send "\r"; } 
-            eof { set cont 1 }
+            timeout { send_user "DRAC ssh: timeout" ; exit; }
+            eof { if { $cont == 1 } { send_user "DRAC ssh failed twice"; exit ; } ; set cont 1 }
         }
         if { $cont == 0 } { break; }
 
@@ -64,7 +66,7 @@ if { "$method" == "ssh" } {
         } else {
             set rac_cmd "serveraction powercycle"
         }
-        send_user "/opt/dell/srvadmin/bin/$method -r $host -u $user -p '$password' $rac_cmd\n";
+        #send_user "/opt/dell/srvadmin/bin/$method -r $host -u $user -p '$password' $rac_cmd\n";
         set cont 0
         for { set retry 0 } { $retry < 2 } { incr retry 1 } {
 
@@ -73,22 +75,27 @@ if { "$method" == "ssh" } {
             # NOTE: For unknown reason, some DRAC modules work only with
             #       stderr redirected to stdout, others only work without it.
             if { $retry == 0 } {
-                set x [ spawn /opt/dell/srvadmin/bin/$method -i -r $host $rac_cmd 2>&1 ]
+                set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd 2>&1 ]
             } else {
-                set x [ spawn /opt/dell/srvadmin/bin/$method -i -r $host $rac_cmd  ]
+                set x [ spawn /opt/dell/srvadmin/rac5/bin/$method -i -r $host $rac_cmd  ]
             }
 
             expect {
                 -re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
                 -re "UserName:|username:" { send "$user\r" ; exp_continue };
                 -re "Password:|password:" { send "$password\r" };
+                timeout { send_user "DRAC: timeout" ; exit }
+                eof { send_user "DRAC: early EOF"; exit ; }
             }
 
             expect {
                 -re "Authentication failed.*" { send_user "DRAC: $expect_out(0,string)"; exit }
                 -re "This.*not support remote RACADM" { send_user "DRAC: $expect_out(0,string)" ; exit }
                 -re "ERROR: The syntax of the command specified is not correct." { set cont 1 }
+                -re "INVALID ARGUMENT" { send_user "DRAC: received 'INVALID ARGUMENT'"; exit }
                 -re "RAC Information:" { sleep .1; }
+                timeout { send_user "DRAC: timeout" ; exit }
+                eof { send_user "DRAC: early EOF"; exit ; }
                 #-re "ERROR:.*" { send_user "DRAC: $expect_out(0,string)"; exit }
             }
             if { $cont == 0 } { break; }
@@ -98,6 +105,7 @@ if { "$method" == "ssh" } {
         expect {
             -re "Power Status.*" { sleep .1; } 
             -re "RAC Firmware.*" { sleep .1; }
+            eof { send_user "DRAC: early EOF"; exit ; }
         } 
         expect eof { send_user "$reset_msg\n" }
     }
index ec064a5..dc637aa 100755 (executable)
@@ -10,21 +10,23 @@ set model [lindex $argv 5]
 
 log_user 0
 
+set path [ file dirname [ file dirname $argv0 ] ]
+
 if { "$method" == "https" } {
     if { "$dryrun" == "True" } {
-        set file $path/iloxml/Get_Network.xml
+        set file $path/hpilo/iloxml/Get_Network.xml
     } else {
-        #set file $path/iloxml/Reset_Server.xml
-        set file $path/iloxml/Get_Network.xml
+        set file $path/hpilo/iloxml/Reset_Server.xml
+        #set file $path/hpilo/iloxml/Get_Network.xml
     }
-    spawn "$path/locfg.pl" -s $host -u $user -p '$password' -f $file
+    spawn "$path/hpilo/locfg.pl" -s $host -u $user -p $password -f $file
 } elseif { "$method" == "ssh" } {
     if { "$dryrun" == "True" } {
         set reset_cmd "POWER\r"
         set reset_msg "Test: No error"
     } else {
-        #set reset_cmd "reset\r"
-        set reset_cmd "POWER\r"
+        #set reset_cmd "POWER\r"
+        set reset_cmd "reset\r"
         set reset_msg "Reset: No error"
     }
     spawn ssh $user@$host
@@ -41,6 +43,7 @@ if { "$method" == "proxy" || "$method" == "https" }  {
         "MESSAGE='Host power is already ON.'" { send_user "$expect_out(0,string)"}
         "MESSAGE='Host power is already OFF.'" { send_user "$expect_out(0,string)"}
         "MESSAGE='Login credentials rejected.'" { send_user "$expect_out(0,string)"}
+        "MESSAGE='User login name was not found'" { send_user "$expect_out(0,string)"}
         "<GET_NETWORK_SETTINGS>" { send_user "Test: No error"}
         -re "ERROR:.*" { send_user "$expect_out(0,string)" }
         eof { send_user "Reached EOF without status report: $expect_out(buffer)" }
@@ -52,7 +55,9 @@ if { "$method" == "proxy" || "$method" == "https" }  {
         "password: " { send "$password\n" ; exp_continue }
         "Permission denied" { send_user "HPiLO username/password: $expect_out(0,string)"; exit }
         "Could not resolve hostname" { send_user "HPiLO hostname: $expect_out(0,string)"; exit }
+        "Connection timed out" { send_user "HPiLO ssh timeout: $expect_out(0,string)"; exit }
         "</>hpiLO-> " { send "cd system1\r" }
+        eof { send_user "HPiLO early EOF\n"; exit }
     }
 
     expect "</system1>hpiLO-> " { send $reset_cmd }
index a6e1934..eef8d2e 100755 (executable)
@@ -88,9 +88,9 @@ class PCUModel(PCU):
 
         raise Exception("No such Node ID: %d" % node_id)
 
-    def catcherror(self, function, node_port):
+    def catcherror(self, function, node_port, dryrun):
         try:
-            return function(node_port)
+            return function(node_port, dryrun)
         except ExceptionNotFound, err:
             return "Not found: " + str(err)
         except ExceptionPassword, err:
@@ -363,6 +363,7 @@ class BasicPCUControl(PCUModel):
         cmd = cmd_str + "%s %s %s '%s' %s %s "  % (
                     scriptname, self.host, self.username, 
                     self.password, args['dryrun'], args['model'])
+        print cmd
         cmd_out, cmd_err = locfg.run_noexcept(cmd)
         return cmd_out.strip() + cmd_err.strip()
 
@@ -378,7 +379,7 @@ class BasicPCUControl(PCUModel):
 
         print "found function %s in model %s" % (looking_for_fxn, self.model)
         reboot_fxn = getattr(self, looking_for_fxn)
-        ret = self.catcherror(reboot_fxn, node_port)
+        ret = self.catcherror(reboot_fxn, node_port, dryrun)
 
         return ret
 
index 3cfcc47..564d0b9 100644 (file)
@@ -1,9 +1,14 @@
 <?php 
 
+$DEBUG=FALSE;
 function logit($string){
+    global $DEBUG;
+    if ( $DEBUG )
+    {
                $f = fopen("output.log", 'a');
                fwrite($f, $string);
                fclose($f);
+    }
 }
 
 function run_hp_script($script, $host, $username, $password)
@@ -52,7 +57,7 @@ function run_hp_script($script, $host, $username, $password)
                        echo system("which ipmitool 2>&1");
                } else 
                {
-                       echo "what do you want me to do with this?";
+                       echo "Unrecognized pcu type: $model";
                }
 
        } else {