0f00f55199db25cc4ef4729c4a25f76114584b76
[pcucontrol.git] / pcucontrol / models / HPiLO.py
1 from pcucontrol.reboot import *
2 from distutils.sysconfig import get_python_lib; 
3 import subprocess
4
5 class HPiLO(PCUControl):
6     supported_ports = [22,443]
7     def run(self, node_port, dryrun):
8         if self.type == Transport.SSH:
9             return self.run_ssh(node_port, dryrun)
10         elif self.type == Transport.HTTP or self.type == Transport.HTTPS:
11             return self.run_https(node_port, dryrun)
12         else:
13             raise ExceptionNoTransport("Unimplemented Transport for HPiLO %s" % self.type)
14
15     def run_ssh(self, node_port, dryrun):
16
17         self.transport.open(self.host, self.username)
18         self.transport.sendPassword(self.password)
19
20         # </>hpiLO-> 
21         self.transport.ifThenSend("</>hpiLO->", "cd system1")
22
23         # Reboot Outlet  N      (Y/N)?
24         if dryrun:
25             self.transport.ifThenSend("</system1>hpiLO->", "POWER")
26         else:
27             # Reset this machine
28             self.transport.ifThenSend("</system1>hpiLO->", "reset")
29
30         self.transport.ifThenSend("</system1>hpiLO->", "exit")
31
32         self.transport.close()
33
34         # NOTE: if an error occurs earlier, an exception should be thrown
35         return 0
36         
37     def run_https(self, node_port, dryrun):
38
39         locfg = command.CMD()
40
41         cmd_str = get_python_lib(1) + "/pcucontrol/models/hpilo/"
42         
43         cmd = cmd_str + "locfg.pl -s %s -f %s -u %s -p '%s' "  % (
44                     self.host, cmd_str+"iloxml/Get_Network.xml", 
45                     self.username, self.password)
46         cmd_out, cmd_err = locfg.run_noexcept(cmd)
47
48         if locfg.s.returncode != 0:
49             return cmd_out.strip() + cmd_err.strip()
50
51         cmd = "grep 'MESSAGE' | grep -v 'No error'"
52         p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
53                             stderr=subprocess.STDOUT, close_fds=True)
54         (grep_in, grep_out ) = (p.stdin, p.stdout)
55         grep_in.write(cmd_out)
56         grep_in.close()         # close so read does not block
57         output = grep_out.read()
58         if output.strip() != "":
59             print "grep_out: %s" % output.strip()
60             return output.strip()
61
62         # NOTE: if an error occurs earlier, an exception should be thrown
63         return 0