2 from select import select
11 class ExceptionTimeout(Exception): pass
12 class ExceptionReadTimeout(Exception): pass
14 ssh_options = { 'StrictHostKeyChecking':'no',
16 'PasswordAuthentication':'no',
17 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
19 class Sopen(subprocess.Popen):
20 def kill(self, sig = signal.SIGTERM):
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)
28 # no such process, due to it already exiting...
32 def read_t(stream, count=1, timeout=COMMAND_TIMEOUT*2):
37 lin, lout, lerr = select([stream], [], [], timeout)
40 raise ExceptionReadTimeout("TIMEOUT reading from command")
43 outbytes = stream.read(count)
45 print 'no content yet.'
47 # the select timeout should catch this.
56 lin, lout, lerr = select([stream], [], [], timeout)
58 raise ExceptionReadTimeout("TIMEOUT reading from command")
60 return stream.read(count)
66 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
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..."
79 except Exception, err:
80 #from monitor.common import email_exception
84 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
85 (o,e) = self.run(cmd, timeout)
88 if self.s.returncode is None:
90 return self.s.returncode
92 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
94 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
96 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
97 lout, lin, lerr = select([f_out], [], [f_err], timeout)
98 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
99 # Reached a timeout! Nuke process so it does not hang.
100 print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
101 s.kill(signal.SIGKILL)
102 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
105 #print len(lin), len(lout), len(lerr)
111 #o_value = f_out.read()
112 flags = fcntl.fcntl(f_out, fcntl.F_GETFL)
113 fcntl.fcntl(f_out, fcntl.F_SETFL, flags | os.O_NONBLOCK)
116 o_value = read_t(f_out,1,30)
117 except ExceptionReadTimeout:
118 s.kill(signal.SIGKILL)
119 raise ExceptionReadTimeout("TIMEOUT: failed to read from cmd: %s" % cmd)
121 e_value = f_err.read()
123 o_value = o_value.strip()
124 e_value = e_value.strip()
130 s.kill(signal.SIGKILL)
132 return (o_value, e_value)
134 def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
136 #print "CMD.run(%s)" % " ".join(args)
137 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
139 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
140 lout, lin, lerr = select([f_out], [], [f_err], timeout)
141 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
142 # Reached a timeout! Nuke process so it does not hang.
143 s.kill(signal.SIGKILL)
144 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
145 o_value = f_out.read()
147 if o_value == "": # An error has occured
148 e_value = f_err.read()
150 o_value = o_value.strip()
151 e_value = e_value.strip()
156 s.kill(signal.SIGKILL)
158 return (o_value, e_value)
162 def __init__(self, user, host, port=22, options = ssh_options):
163 self.options = options
169 def __options_to_str(self):
171 for o,v in self.options.iteritems():
172 options = options + "-o %s=%s " % (o,v)
175 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
176 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
177 self.user, self.host, cmd)
178 #print "SSH.run(%s)" % cmd
179 return CMD.run(self, cmd, timeout)
181 def get_file(self, rmt_filename, local_filename=None):
182 if local_filename == None:
183 local_filename = "./"
184 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(),
185 self.user, self.host,
186 rmt_filename, local_filename)
188 # errors will be on stderr,
189 # success will have a blank stderr...
190 return CMD.run_noexcept(self, cmd)
192 def run_noexcept(self, cmd):
193 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
194 self.user, self.host, cmd)
195 #print "SSH.run_noexcept(%s)" % cmd
196 return CMD.run_noexcept(self, cmd)
198 def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
199 cmd = "exec ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
200 self.user, self.host, cmd)
202 r = CMD.run_noexcept(self, cmd, timeout)
207 def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
208 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
209 self.user, self.host, cmd)
210 #print "SSH.system2(%s)" % cmd
211 return CMD.system(self, cmd, timeout)
214 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
215 self.user, self.host, cmd)
218 (f_in, f_out, f_err) = os.popen3(cmd)
221 if value == "": # An error has occured
223 value = value.strip()
226 print " == %s" % value
234 self.start = time.time()
237 self.end = time.time()
238 t = self.end-self.start
242 self.end = time.time()
243 t = self.end-self.start
244 self.start = self.end