5 class ExceptionTimeout(Exception): pass
7 ssh_options = { 'StrictHostKeyChecking':'no',
9 'PasswordAuthentication':'no',
10 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
11 from select import select
15 class Sopen(subprocess.Popen):
16 def kill(self, signal = signal.SIGTERM):
17 os.kill(self.pid, signal)
19 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
20 lin, lout, lerr = select([stream], [], [], timeout)
22 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
24 return stream.read(count)
30 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
32 #print "CMD.run_noexcept(%s)" % cmd
34 return CMD.run(self,cmd,timeout)
35 except ExceptionTimeout:
36 import traceback; print traceback.print_exc()
37 return ("", "SCRIPTTIMEOUT")
39 from nodecommon import email_exception
42 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
43 (o,e) = self.run(cmd, timeout)
46 if self.s.returncode is None:
48 return self.s.returncode
50 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
52 print "CMD.run(%s)" % cmd
53 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
55 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
56 #print "calling select(%s)" % timeout
57 lout, lin, lerr = select([f_out], [], [f_err], timeout)
58 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
59 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
60 # Reached a timeout! Nuke process so it does not hang.
62 s.kill(signal.SIGKILL)
63 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
66 #print len(lin), len(lout), len(lerr)
72 #print "reading from f_out"
73 if len(lout) > 0: o_value = f_out.read()
74 #print "reading from f_err"
75 if len(lerr) > 0: e_value = f_err.read()
77 #print "striping output"
78 o_value = o_value.strip()
79 e_value = e_value.strip()
81 #print "OUTPUT", 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
210 self.start = time.time()
213 self.end = time.time()
214 t = self.end-self.start
218 self.end = time.time()
219 t = self.end-self.start
220 self.start = self.end