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