Add expect version of APC controller
[pcucontrol.git] / pcucontrol / util / command.py
1 import os
2 from select import select 
3 import subprocess
4 import signal
5 import time
6 import traceback
7 import fcntl
8
9 DEBUG= 0
10
11 class ExceptionTimeout(Exception): pass
12 class ExceptionReadTimeout(Exception): pass
13 COMMAND_TIMEOUT = 60
14 ssh_options = { 'StrictHostKeyChecking':'no', 
15                                 'BatchMode':'yes', 
16                                 'PasswordAuthentication':'no',
17                                 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
18
19 class Sopen(subprocess.Popen):
20         def kill(self, sig = signal.SIGTERM):
21                 try:
22                         # NOTE: this also kills parent... so doesn't work like I want.
23                         # NOTE: adding 'exec' before the cmd removes the extra sh, and
24                         #               partially addresses this problem.
25                         #os.killpg(os.getpgid(self.pid), signal.SIGKILL)
26                         os.kill(self.pid, sig)
27                 except OSError:
28                         # no such process, due to it already exiting...
29                         pass
30
31
32 def read_t(stream, count=1, timeout=COMMAND_TIMEOUT*2):
33         if count == 1:
34                 retstr = ""
35
36                 while True:
37                         lin, lout, lerr = select([stream], [], [], timeout)
38                         if len(lin) == 0:
39                                 #print "timeout!"
40                                 raise ExceptionReadTimeout("TIMEOUT while reading from command")
41
42                         try:
43                                 outbytes = stream.read(count)
44                         except IOError, err:
45                                 print 'no content yet.'
46                                 # due to no content.
47                                 # the select timeout should catch this.
48                                 continue
49
50                         if not outbytes:
51                                 break
52                         retstr += outbytes
53
54                 return retstr
55         else:
56                 lin, lout, lerr = select([stream], [], [], timeout)
57                 if len(lin) == 0:
58                         raise ExceptionReadTimeout("TIMEOUT while reading from command")
59
60                 return stream.read(count)
61
62 class CMD:
63         def __init__(self):
64                 pass
65
66         def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
67
68                 try:
69                         return CMD.run(self,cmd,timeout)
70                 except ExceptionTimeout:
71                         #print traceback.print_exc()
72                         return ("", "ScriptTimeout")
73                 except ExceptionReadTimeout:
74                         #print traceback.print_exc()
75                         return ("", "RunningScriptTimeout")
76                 except KeyboardInterrupt:
77                         print "Interrupted, exiting..."
78                         sys.exit(1)
79                 except Exception, err:
80                         #from monitor.common import email_exception
81                         #email_exception()
82                         return ("", str(err))
83                         
84         def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
85                 (o,e) = self.run(cmd, timeout)
86                 self.output = o
87                 self.error = e
88                 if self.s.returncode is None:
89                         self.s.wait()
90                 return self.s.returncode
91
92         def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
93
94                 s = Sopen(cmd, shell=True, 
95                     stdin=subprocess.PIPE, 
96                     stdout=subprocess.PIPE, 
97                     stderr=subprocess.PIPE, 
98                     close_fds=True)
99                 self.s = s
100                 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
101                 lout, lin, lerr = select([f_out], [], [f_err], timeout)
102                 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
103                         # Reached a timeout!  Nuke process so it does not hang.
104                         #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
105                         s.kill(signal.SIGKILL)
106                         raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
107                 else:
108                         #print "RETURNING"
109                         #print len(lin), len(lout), len(lerr)
110                         pass
111
112                 o_value = ""
113                 e_value = ""
114
115                 #o_value = f_out.read()
116                 flags = fcntl.fcntl(f_out, fcntl.F_GETFL)
117                 fcntl.fcntl(f_out, fcntl.F_SETFL, flags | os.O_NONBLOCK)
118
119                 try:
120                         o_value = read_t(f_out,1,30)
121                 except ExceptionReadTimeout:
122                         s.kill(signal.SIGKILL)
123                         raise ExceptionReadTimeout("TIMEOUT: failed to read from cmd: %s" % cmd)
124                         
125                 e_value = f_err.read()
126
127                 o_value = o_value.strip()
128                 e_value = e_value.strip()
129
130                 f_out.close()
131                 f_in.close()
132                 f_err.close()
133                 s.poll()
134                 s.kill(signal.SIGKILL)
135
136                 return (o_value, e_value)
137
138         def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
139
140                 #print "CMD.run(%s)" % " ".join(args)
141                 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
142                 self.s = s
143                 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
144                 lout, lin, lerr = select([f_out], [], [f_err], timeout)
145                 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
146                         # Reached a timeout!  Nuke process so it does not hang.
147                         s.kill(signal.SIGKILL)
148                         raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
149                 o_value = f_out.read()
150                 e_value = ""
151                 if o_value == "":       # An error has occured
152                         e_value = f_err.read()
153
154                 o_value = o_value.strip()
155                 e_value = e_value.strip()
156
157                 f_out.close()
158                 f_in.close()
159                 f_err.close()
160                 s.kill(signal.SIGKILL)
161
162                 return (o_value, e_value)
163
164
165 class SSH(CMD):
166         def __init__(self, user, host, port=22, options = ssh_options):
167                 self.options = options
168                 self.user = user
169                 self.host = host
170                 self.port = port
171                 return
172
173         def __options_to_str(self):
174                 options = ""
175                 for o,v in self.options.iteritems():
176                         options = options + "-o %s=%s " % (o,v)
177                 return options
178
179         def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
180                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
181                                                                         self.user, self.host, cmd)
182                 #print "SSH.run(%s)" % cmd
183                 return CMD.run(self, cmd, timeout)
184
185         def get_file(self, rmt_filename, local_filename=None):
186                 if local_filename == None:
187                         local_filename = "./"
188                 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(), 
189                                                                         self.user, self.host, 
190                                                                         rmt_filename, local_filename)
191                 # output :
192                 #       errors will be on stderr,
193                 #   success will have a blank stderr...
194                 return CMD.run_noexcept(self, cmd)
195
196         def run_noexcept(self, cmd):
197                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
198                                                                         self.user, self.host, cmd)
199                 #print "SSH.run_noexcept(%s)" % cmd
200                 return CMD.run_noexcept(self, cmd)
201
202         def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
203                 cmd = "exec ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(), 
204                                                                         self.user, self.host, cmd)
205                 #print cmd
206                 r = CMD.run_noexcept(self, cmd, timeout)
207                 self.ret = -1
208
209                 return r
210
211         def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
212                 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(), 
213                                                                         self.user, self.host, cmd)
214                 #print "SSH.system2(%s)" % cmd
215                 return CMD.system(self, cmd, timeout)
216
217         def runE(self, cmd):
218                 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(), 
219                                                                         self.user, self.host, cmd)
220                 if ( DEBUG == 1 ):
221                         print cmd,
222                 (f_in, f_out, f_err) = os.popen3(cmd)
223
224                 value = f_out.read()
225                 if value == "": # An error has occured
226                         value = f_err.read()
227                         value = value.strip()
228
229                 if ( DEBUG == 1 ):
230                         print " == %s" % value
231                 f_out.close()
232                 f_in.close()
233                 f_err.close()
234                 return value.strip()
235                 
236 class MyTimer:
237         def __init__(self):
238                 self.start = time.time()
239
240         def end(self):
241                 self.end = time.time()
242                 t = self.end-self.start
243                 return t
244
245         def diff(self):
246                 self.end = time.time()
247                 t = self.end-self.start
248                 self.start = self.end
249                 return t