remove unnecessary debug messages
[pcucontrol.git] / pcucontrol / models / ePowerSwitch.py
1 from pcucontrol.reboot import *
2
3 class ePowerSwitchNew(PCUControl):
4         supported_ports = [80]
5         # NOTE:
6         #               The old code used Python's HTTPPasswordMgrWithDefaultRealm()
7         #               For some reason this both doesn't work and in some cases, actually
8         #               hangs the PCU.  Definitely not what we want.
9         #               
10         #               The code below is much simpler.  Just letting things fail first,
11         #               and then, trying again with authentication string in the header.
12         #               
13         def run_http(self, node_port, dryrun):
14                 self.transport = None
15                 self.url = "http://%s:%d/" % (self.host,80)
16                 uri = "%s:%d" % (self.host,80)
17
18                 req = urllib2.Request(self.url)
19                 try:
20                         handle = urllib2.urlopen(req)
21                 except IOError, e:
22                         # NOTE: this is expected to fail initially
23                         pass
24                 else:
25                         #print self.url
26                         #print "-----------"
27                         r = handle.read()
28                         #print "-----------"
29                         return "ERROR: not protected by HTTP authentication: %s" % r
30
31                 if not hasattr(e, 'code') or e.code != 401:
32                         return "ERROR: failed for: %s" % str(e)
33
34                 base64data = base64.encodestring("%s:%s" % (self.username, self.password))[:-1]
35                 # NOTE: assuming basic realm authentication.
36                 authheader = "Basic %s" % base64data
37                 req.add_header("Authorization", authheader)
38
39                 try:
40                         f = urllib2.urlopen(req)
41                 except IOError, e:
42                         # failing here means the User/passwd is wrong (hopefully)
43                         raise ExceptionPassword("Incorrect username/password")
44
45                 # NOTE: after verifying that the user/password is correct, 
46                 #               actually reboot the given node.
47                 if not dryrun:
48                         try:
49                                 data = urllib.urlencode({'P%d' % node_port : "r"})
50                                 req = urllib2.Request(self.url + "cmd.html")
51                                 req.add_header("Authorization", authheader)
52                                 # add data to handler,
53                                 f = urllib2.urlopen(req, data)
54                                 #if self.transport.verbose: print f.read()
55                         except:
56                                 import traceback; traceback.print_exc()
57
58                                 # fetch url one more time on cmd.html, econtrol.html or whatever.
59                                 # pass
60                 else:
61                         #if self.transport.verbose: print f.read()
62                         pass
63
64                 return 0
65
66 class ePowerSwitchOld(PCUControl):
67         supported_ports = [80]
68         def run_http(self, node_port, dryrun):
69                 self.url = "http://%s" % (self.host)
70                 
71                 authinfo = { "pwd" : self.password }
72                 data = urllib.urlencode(authinfo)
73                 req = urllib2.Request(self.url + "/elogin.html", data)
74                 #print self.url
75                 #print data
76                 response = urllib2.urlopen(req)
77                 reply = response.read()
78
79                 if not dryrun:
80                         try:
81                                 data = urllib.urlencode({'P%d' % node_port : "r"})
82                                 req = urllib2.Request(self.url + "econtrol.html")
83                                 f = urllib2.urlopen(req, data)
84                         except:
85                                 import traceback; traceback.print_exc()
86
87                 return 0