remove unnecessary debug messages
[pcucontrol.git] / pcucontrol / models / APCControl.py
1 from pcucontrol.reboot import *
2 import subprocess
3
4 class APCControl(BasicPCUControl):
5     supported_ports = [22,23,80,443]
6 #    reboot_sequence = []
7
8     def run(self, node_port, dryrun):
9         #print "RUNNING!!!!!!!!!!!!"
10         if self.transport.type == Transport.HTTPS or self.type == Transport.HTTP:
11             #print "APC via http...."
12             return self.run_http_or_https(node_port, dryrun)
13         else:
14             #print "APC via telnet/ssh...."
15             return self.run_telnet_or_ssh(node_port, dryrun)
16
17     def pcu_run(self, node_port, dryrun=False):
18         return self.run_telnet_or_ssh(node_port, dryrun)
19
20     def pcu_test(self, node_port, dryrun=True):
21         return self.run_telnet_or_ssh(node_port, dryrun)
22     
23     def run_telnet_or_ssh(self, node_port, dryrun):
24         r = self.run_telnet(node_port, dryrun)
25         if "No error" in r:
26             return r
27         r2 = self.run_ssh(node_port, dryrun)
28         if "No error" in r2:
29             return r2
30         return r + " :: " +r2
31
32     def run_ssh(self, node_port, dryrun):
33         return self.run_expect_script("APC.exp ssh", dryrun=dryrun, model="None", sequence=self.reboot_sequence)
34     def run_telnet(self, node_port, dryrun):
35         return self.run_expect_script("APC.exp telnet", dryrun=dryrun, model="None", sequence=self.reboot_sequence)
36
37
38     def run_http(self, node_port, dryrun):
39         return self.run_http_or_https(node_port, dryrun)
40     def run_https(self, node_port, dryrun):
41         return self.run_http_or_https(node_port, dryrun)
42
43     def run_http_or_https(self, node_port, dryrun):
44         if not dryrun:
45             # send reboot signal.
46             # TODO: send a ManualPCU() reboot request for this PCU.
47             # NOTE: this model defies automation because, the port numbering
48             #     and the form numbers are not consistent across models.  There is
49             #     not direct mapping from port# to form#.
50             return "Manual Reboot Required"
51
52         else:
53             # TODO: also send message for https, since that doesn't work this way...
54             if self.transport.type == Transport.HTTPS:
55                 cmd = self.get_https_cmd()
56             elif self.transport.type == Transport.HTTP:
57                 cmd = self.get_http_cmd()
58             else:
59                 raise ExceptionNoTransport("Unsupported transport for http command")
60
61         cmd = cmd % ( self.username, self.password, self.host)
62         #print "CMD: %s" % cmd
63
64         p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
65
66         result = p.read()
67         if len(result.split('\n')) > 2:
68             self.logout()
69             return 0
70         else:
71             # NOTE: an error has occurred, so no need to log out.
72             #print "RESULT: ", result
73             return result
74
75     def get_https_cmd(self):
76         version = self.get_version()
77         #print "VERSION: %s" % version
78         if "AP96" in version:
79             cmd = "curl -s --insecure --user '%s:%s' https://%s/outlets.htm " + \
80                   " | grep -E '^[^<]+' " + \
81                   " | grep -v 'Protected Object' "
82         else:
83             # NOTE: no other case known right now...
84             cmd = "curl -s --insecure --user '%s:%s' https://%s/outlets.htm " + \
85                   " | grep -E '^[^<]+' " + \
86                   " | grep -v 'Protected Object' "
87             
88         return cmd
89     
90     def get_http_cmd(self):
91         version = self.get_version()
92         #print "VERSION: %s" % version
93         if "AP7900" in version:
94             cmd = "curl -s --anyauth --user '%s:%s' http://%s/rPDUout.htm | grep -E '^[^<]+'" 
95         elif "AP7920" in version:
96             cmd = "curl -s --anyauth --user '%s:%s' http://%s/ms3out.htm | grep -E '^[^<]+' " 
97         else:
98             # default case...
99             #print "USING DEFAULT"
100             cmd = "curl -s --anyauth --user '%s:%s' http://%s/ms3out.htm | grep -E '^[^<]+' " 
101             
102         return cmd
103
104     def get_version(self):
105         # NOTE: this command returns and formats all data.
106         #cmd = """curl -s --anyauth --user '%s:%s' http://%s/about.htm """ +
107         #      """ | sed -e "s/<[^>]*>//g" -e "s/&nbsp;//g" -e "/^$/d" """ +
108         #      """ | awk '{line=$0 ; if ( ! /:/ && length(pline) > 0 ) \
109         #                   { print pline, line } else { pline=line} }' """ + 
110         #      """ | grep Model """
111
112         # NOTE: we may need to return software version, no model version to
113         #         know which file to request on the server.
114
115         if self.transport.type == Transport.HTTP:
116             cmd = """curl -s --anyauth --user '%s:%s' http://%s/about.htm """ + \
117                   """ | sed -e "s/<[^>]*>//g" -e "s/&nbsp;//g" -e "/^$/d" """ + \
118                   """ | grep -E "AP[[:digit:]]+" """
119                   #""" | grep -E "v[[:digit:]].*" """
120         elif self.transport.type == Transport.HTTPS:
121             cmd = """curl -s --insecure --user '%s:%s' https://%s/about.htm """ + \
122                   """ | sed -e "s/<[^>]*>//g" -e "s/&nbsp;//g" -e "/^$/d" """ + \
123                   """ | grep -E "AP[[:digit:]]+" """
124                   #""" | grep -E "v[[:digit:]].*" """
125         else:
126             raise ExceptionNoTransport("Unsupported transport to get version")
127
128         cmd = cmd % ( self.username, self.password, self.host)
129         p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
130         result = p.read()
131         return result.strip()
132
133     def logout(self):
134         # NOTE: log out again, to allow other uses to access the machine.
135         if self.transport.type == Transport.HTTP:
136             cmd = """curl -s --anyauth --user '%s:%s' http://%s/logout.htm """ + \
137                   """ | grep -E '^[^<]+' """
138         elif self.transport.type == Transport.HTTPS:
139             cmd = """curl -s --insecure --user '%s:%s' http://%s/logout.htm """ + \
140                   """ | grep -E '^[^<]+' """
141         else:
142             raise ExceptionNoTransport("Unsupported transport to logout")
143
144         cmd = cmd % ( self.username, self.password, self.host)
145         p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
146         #print p.read()
147
148 class APCControl12p3(APCControl):
149     def run_telnet_or_ssh(self, node_port, dryrun):
150         self.reboot_sequence = ["1", "2", str(node_port), "3"]
151         return super(APCControl12p3, self).run_telnet_or_ssh(node_port, dryrun)
152
153 class APCControl1p4(APCControl):
154     def run_telnet_or_ssh(self, node_port, dryrun):
155         self.reboot_sequence = ["1", str(node_port), "4"]
156         return super(APCControl1p4, self).run_telnet_or_ssh(node_port, dryrun)
157
158 class APCControl121p3(APCControl):
159     def run_telnet_or_ssh(self, node_port, dryrun):
160         #print "TEST! "
161         self.reboot_sequence = ["1", "2", "1", str(node_port), "3"]
162         return super(APCControl121p3, self).run_telnet_or_ssh(node_port, dryrun)
163
164 class APCControl121p1(APCControl):
165     def run_telnet_or_ssh(self, node_port, dryrun):
166         self.reboot_sequence = ["1", "2", "1", str(node_port), "1", "3"]
167         return super(APCControl121p1, self).run_telnet_or_ssh(node_port, dryrun)
168
169 class APCControl13p13(APCControl):
170     def run_telnet_or_ssh(self, node_port, dryrun):
171         self.reboot_sequence = ["1", "3", str(node_port), "1", "3"]
172         return super(APCControl13p13, self).run_telnet_or_ssh(node_port, dryrun)