2 from select import select
10 class ExceptionTimeout(Exception): pass
12 ssh_options = { 'StrictHostKeyChecking':'no',
14 'PasswordAuthentication':'no',
15 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
17 class Sopen(subprocess.Popen):
18 def kill(self, signal = signal.SIGTERM):
19 os.kill(self.pid, signal)
21 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
22 lin, lout, lerr = select([stream], [], [], timeout)
24 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
26 return stream.read(count)
32 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
34 #print "CMD.run_noexcept(%s)" % cmd
36 return CMD.run(self,cmd,timeout)
37 except ExceptionTimeout:
38 print traceback.print_exc()
39 return ("", "SCRIPTTIMEOUT")
41 from monitor.common import email_exception
44 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
45 (o,e) = self.run(cmd, timeout)
48 if self.s.returncode is None:
50 return self.s.returncode
52 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
54 #print "CMD.run(%s)" % cmd
55 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
57 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
58 #print "calling select(%s)" % timeout
59 lout, lin, lerr = select([f_out], [], [f_err], timeout)
60 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
61 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
62 # Reached a timeout! Nuke process so it does not hang.
64 s.kill(signal.SIGKILL)
65 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
68 #print len(lin), len(lout), len(lerr)
74 o_value = f_out.read()
75 e_value = f_err.read()
77 #print "striping output"
78 o_value = o_value.strip()
79 e_value = e_value.strip()
81 #print "OUTPUT -%s-%s-" % (o_value, e_value)
83 #print "closing files"
90 #print "after s.kill()"
92 # no such process, due to it already exiting...
95 #print o_value, e_value
96 return (o_value, e_value)
98 def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
100 #print "CMD.run(%s)" % " ".join(args)
101 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
103 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
104 lout, lin, lerr = select([f_out], [], [f_err], timeout)
105 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
106 # Reached a timeout! Nuke process so it does not hang.
107 s.kill(signal.SIGKILL)
108 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
109 o_value = f_out.read()
111 if o_value == "": # An error has occured
112 e_value = f_err.read()
114 o_value = o_value.strip()
115 e_value = e_value.strip()
123 # no such process, due to it already exiting...
126 return (o_value, e_value)
130 def __init__(self, user, host, port=22, options = ssh_options):
131 self.options = options
137 def __options_to_str(self):
139 for o,v in self.options.iteritems():
140 options = options + "-o %s=%s " % (o,v)
143 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
144 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
145 self.user, self.host, cmd)
146 #print "SSH.run(%s)" % cmd
147 return CMD.run(self, cmd, timeout)
149 def get_file(self, rmt_filename, local_filename=None):
150 if local_filename == None:
151 local_filename = "./"
152 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(),
153 self.user, self.host,
154 rmt_filename, local_filename)
156 # errors will be on stderr,
157 # success will have a blank stderr...
158 return CMD.run_noexcept(self, cmd)
160 def run_noexcept(self, cmd):
161 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
162 self.user, self.host, cmd)
163 #print "SSH.run_noexcept(%s)" % cmd
164 return CMD.run_noexcept(self, cmd)
166 def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
167 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
168 self.user, self.host, cmd)
169 #print "SSH.run_noexcept2(%s)" % cmd
170 r = CMD.run_noexcept(self, cmd, timeout)
172 # XXX: this may be resulting in deadlocks... not sure.
173 #if self.s.returncode is None:
175 # self.s.kill(signal.SIGKILL)
177 # self.ret = self.s.returncode
182 def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
183 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
184 self.user, self.host, cmd)
185 #print "SSH.system2(%s)" % cmd
186 return CMD.system(self, cmd, timeout)
189 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
190 self.user, self.host, cmd)
193 (f_in, f_out, f_err) = os.popen3(cmd)
196 if value == "": # An error has occured
198 value = value.strip()
201 print " == %s" % value
209 self.start = time.time()
212 self.end = time.time()
213 t = self.end-self.start
217 self.end = time.time()
218 t = self.end-self.start
219 self.start = self.end