1 from pcucontrol.reboot import *
3 class APCControl(PCUControl):
4 supported_ports = [22,23,80,443]
7 def run(self, node_port, dryrun):
8 print "RUNNING!!!!!!!!!!!!"
9 if self.type == Transport.HTTPS or self.type == Transport.HTTP:
10 print "APC via http...."
11 return self.run_http_or_https(node_port, dryrun)
13 print "APC via telnet/ssh...."
14 return self.run_telnet_or_ssh(node_port, dryrun)
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)
21 def run_telnet_or_ssh(self, node_port, dryrun):
22 self.transport.open(self.host, self.username)
23 self.transport.sendPassword(self.password)
26 for val in self.reboot_sequence:
28 self.transport.ifThenSend("\r\n> ", val, ExceptionPassword)
31 self.transport.ifThenSend("\r\n> ", val)
34 self.transport.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel",
38 self.transport.ifThenSend("Enter 'YES' to continue or <ENTER> to cancel",
39 "", ExceptionSequence)
40 self.transport.ifThenSend("Press <ENTER> to continue...", "", ExceptionSequence)
42 self.transport.close()
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)
50 def run_http_or_https(self, node_port, dryrun):
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"
60 # TODO: also send message for https, since that doesn't work this way...
61 if self.type == Transport.HTTPS:
62 cmd = self.get_https_cmd()
63 elif self.type == Transport.HTTP:
64 cmd = self.get_http_cmd()
66 raise ExceptionNoTransport("Unsupported transport for http command")
68 cmd = cmd % ( self.username, self.password, self.host)
73 if len(result.split('\n')) > 2:
77 # NOTE: an error has occurred, so no need to log out.
78 print "RESULT: ", result
81 def get_https_cmd(self):
82 version = self.get_version()
83 print "VERSION: %s" % version
85 cmd = "curl -s --insecure --user '%s:%s' https://%s/outlets.htm " + \
86 " | grep -E '^[^<]+' " + \
87 " | grep -v 'Protected Object' "
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' "
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 '^[^<]+' "
105 print "USING DEFAULT"
106 cmd = "curl -s --anyauth --user '%s:%s' http://%s/ms3out.htm | grep -E '^[^<]+' "
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/ //g" -e "/^$/d" """ +
114 # """ | awk '{line=$0 ; if ( ! /:/ && length(pline) > 0 ) \
115 # { print pline, line } else { pline=line} }' """ +
116 # """ | grep Model """
118 # NOTE: we may need to return software version, no model version to
119 # know which file to request on the server.
121 if self.type == Transport.HTTP:
122 cmd = """curl -s --anyauth --user '%s:%s' http://%s/about.htm """ + \
123 """ | sed -e "s/<[^>]*>//g" -e "s/ //g" -e "/^$/d" """ + \
124 """ | grep -E "AP[[:digit:]]+" """
125 #""" | grep -E "v[[:digit:]].*" """
126 elif self.type == Transport.HTTPS:
127 cmd = """curl -s --insecure --user '%s:%s' https://%s/about.htm """ + \
128 """ | sed -e "s/<[^>]*>//g" -e "s/ //g" -e "/^$/d" """ + \
129 """ | grep -E "AP[[:digit:]]+" """
130 #""" | grep -E "v[[:digit:]].*" """
132 raise ExceptionNoTransport("Unsupported transport to get version")
134 cmd = cmd % ( self.username, self.password, self.host)
137 return result.strip()
140 # NOTE: log out again, to allow other uses to access the machine.
141 if self.type == Transport.HTTP:
142 cmd = """curl -s --anyauth --user '%s:%s' http://%s/logout.htm """ + \
143 """ | grep -E '^[^<]+' """
144 elif self.type == Transport.HTTPS:
145 cmd = """curl -s --insecure --user '%s:%s' http://%s/logout.htm """ + \
146 """ | grep -E '^[^<]+' """
148 raise ExceptionNoTransport("Unsupported transport to logout")
150 cmd = cmd % ( self.username, self.password, self.host)
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)
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)
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)
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)
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)