1 from pcucontrol.reboot import *
4 class APCControl(BasicPCUControl):
5 supported_ports = [22,23,80,443]
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)
14 print "APC via telnet/ssh...."
15 return self.run_telnet_or_ssh(node_port, dryrun)
17 def pcu_run(self, node_port, dryrun=False):
18 return self.run_telnet_or_ssh(node_port, dryrun)
20 def pcu_test(self, node_port, dryrun=True):
21 return self.run_telnet_or_ssh(node_port, dryrun)
23 def run_telnet_or_ssh(self, node_port, dryrun):
24 r = self.run_telnet(node_port, dryrun)
27 r2 = self.run_ssh(node_port, dryrun)
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)
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)
43 def run_http_or_https(self, node_port, dryrun):
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"
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()
59 raise ExceptionNoTransport("Unsupported transport for http command")
61 cmd = cmd % ( self.username, self.password, self.host)
64 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
67 if len(result.split('\n')) > 2:
71 # NOTE: an error has occurred, so no need to log out.
72 print "RESULT: ", result
75 def get_https_cmd(self):
76 version = self.get_version()
77 print "VERSION: %s" % version
79 cmd = "curl -s --insecure --user '%s:%s' https://%s/outlets.htm " + \
80 " | grep -E '^[^<]+' " + \
81 " | grep -v 'Protected Object' "
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' "
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 '^[^<]+' "
100 cmd = "curl -s --anyauth --user '%s:%s' http://%s/ms3out.htm | grep -E '^[^<]+' "
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/ //g" -e "/^$/d" """ +
108 # """ | awk '{line=$0 ; if ( ! /:/ && length(pline) > 0 ) \
109 # { print pline, line } else { pline=line} }' """ +
110 # """ | grep Model """
112 # NOTE: we may need to return software version, no model version to
113 # know which file to request on the server.
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/ //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/ //g" -e "/^$/d" """ + \
123 """ | grep -E "AP[[:digit:]]+" """
124 #""" | grep -E "v[[:digit:]].*" """
126 raise ExceptionNoTransport("Unsupported transport to get version")
128 cmd = cmd % ( self.username, self.password, self.host)
129 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
131 return result.strip()
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 '^[^<]+' """
142 raise ExceptionNoTransport("Unsupported transport to logout")
144 cmd = cmd % ( self.username, self.password, self.host)
145 p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).stdout
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)
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)
158 class APCControl121p3(APCControl):
159 def run_telnet_or_ssh(self, node_port, dryrun):
161 self.reboot_sequence = ["1", "2", "1", str(node_port), "3"]
162 return super(APCControl121p3, self).run_telnet_or_ssh(node_port, dryrun)
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)
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)