6 from PHPSerialize import *
7 from PHPUnserialize import *
9 #print >>sys.stderr, "PHPSerial db type not allowed."
19 PICKLE_PATH=config.MONITOR_DATA_ROOT
21 class ExceptionTimeout(Exception): pass
23 def dbLoad(name, type=None):
24 return SPickle().load(name, type)
26 def dbExists(name, type=None):
27 #if self.config.debug:
28 # name = "debug.%s" % name
29 return SPickle().exists(name, type)
31 def dbDump(name, obj=None, type=None):
32 # depth of the dump is 2 now, since we're redirecting to '.dump'
33 return SPickle().dump(name, obj, type, 2)
35 def if_cached_else_refresh(cond, refresh, name, function, type=None):
38 if not config.debug and s.exists("production.%s" % name, type):
39 s.remove("production.%s" % name, type)
40 if config.debug and s.exists("debug.%s" % name, type):
41 s.remove("debug.%s" % name, type)
43 return if_cached_else(cond, name, function, type)
45 def if_cached_else(cond, name, function, type=None):
47 if (cond and s.exists("production.%s" % name, type)) or \
48 (cond and config.debug and s.exists("debug.%s" % name, type)):
49 o = s.load(name, type)
53 s.dump(name, o, type) # cache the object using 'name'
54 o = s.load(name, type)
55 # TODO: what if 'o' hasn't been converted...
59 def __init__(self, path=PICKLE_PATH):
62 def if_cached_else(self, cond, name, function, type=None):
63 if cond and self.exists("production.%s" % name, type):
64 o = self.load(name, type)
68 self.dump(name, o, type) # cache the object using 'name'
71 def __file(self, name, type=None):
73 return "%s/%s.pkl" % (self.path, name)
76 raise Exception("No PHPSerializer module available")
78 return "%s/%s.phpserial" % (self.path, name)
80 def exists(self, name, type=None):
81 return os.path.exists(self.__file(name, type))
83 def remove(self, name, type=None):
84 return os.remove(self.__file(name, type))
86 def load(self, name, type=None):
88 In debug mode, we should fail if neither file exists.
89 if the debug file exists, reset name
90 elif the original file exists, make a copy, reset name
91 else neither exist, raise an error
92 Otherwise, it's normal mode, if the file doesn't exist, raise error
97 if self.exists("debug.%s" % name, type):
98 name = "debug.%s" % name
99 elif self.exists("production.%s" % name, type):
100 debugname = "debug.%s" % name
101 if not self.exists(debugname, type):
102 name = "production.%s" % name
103 shutil.copyfile(self.__file(name, type), self.__file(debugname, type))
105 else: # neither exist
106 raise Exception, "No such pickle based on %s" % self.__file("debug.%s" % name, type)
108 if self.exists("production.%s" % name, type):
109 name = "production.%s" % name
110 elif self.exists(name, type):
113 raise Exception, "No such file %s" % name
116 #print "loading %s" % self.__file(name, type)
117 f = open(self.__file(name, type), 'r')
122 raise Exception("No PHPSerializer module available")
124 o = s.unserialize(f.read())
129 # use the environment to extract the data associated with the local
131 def dump(self, name, obj=None, type=None, depth=1):
133 o = inspect.getouterframes(inspect.currentframe())
134 up1 = o[depth][0] # get the frame one prior to (up from) this frame
135 argvals = inspect.getargvalues(up1)
136 # TODO: check that 'name' is a local variable; otherwise this would fail.
137 obj = argvals[3][name] # extract the local variable name 'name'
138 if not os.path.isdir("%s/" % self.path):
139 os.mkdir("%s" % self.path)
141 name = "debug.%s" % name
143 name = "production.%s" % name
144 f = open(self.__file(name, type), 'w')
149 raise Exception("No PHPSerializer module available")
151 f.write(s.serialize(obj))
157 ssh_options = { 'StrictHostKeyChecking':'no',
159 'PasswordAuthentication':'no',
160 'ConnectTimeout':'%s' % COMMAND_TIMEOUT}
161 from select import select
165 class Sopen(subprocess.Popen):
166 def kill(self, signal = signal.SIGTERM):
167 os.kill(self.pid, signal)
169 def read_t(stream, count, timeout=COMMAND_TIMEOUT*2):
170 lin, lout, lerr = select([stream], [], [], timeout)
172 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
174 return stream.read(count)
180 def run_noexcept(self, cmd, timeout=COMMAND_TIMEOUT*2):
182 #print "CMD.run_noexcept(%s)" % cmd
184 return CMD.run(self,cmd,timeout)
185 except ExceptionTimeout:
186 import traceback; print traceback.print_exc()
187 from nodecommon import email_exception
189 return ("", "SCRIPTTIMEOUT")
191 def system(self, cmd, timeout=COMMAND_TIMEOUT*2):
192 (o,e) = self.run(cmd, timeout)
195 if self.s.returncode is None:
197 return self.s.returncode
199 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
201 #print "CMD.run(%s)" % cmd
202 s = Sopen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
204 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
205 #print "calling select(%s)" % timeout
206 lout, lin, lerr = select([f_out], [], [f_err], timeout)
207 #print "TIMEOUT!!!!!!!!!!!!!!!!!!!"
208 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
209 # Reached a timeout! Nuke process so it does not hang.
211 s.kill(signal.SIGKILL)
212 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
215 #print len(lin), len(lout), len(lerr)
221 #print "reading from f_out"
222 if len(lout) > 0: o_value = f_out.read()
223 #print "reading from f_err"
224 if len(lerr) > 0: e_value = f_err.read()
226 #print "striping output"
227 o_value = o_value.strip()
228 e_value = e_value.strip()
230 #print "OUTPUT", o_value, e_value
232 #print "closing files"
239 #print "after s.kill()"
241 # no such process, due to it already exiting...
244 #print o_value, e_value
245 return (o_value, e_value)
247 def runargs(self, args, timeout=COMMAND_TIMEOUT*2):
249 #print "CMD.run(%s)" % " ".join(args)
250 s = Sopen(args, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True)
252 (f_in, f_out, f_err) = (s.stdin, s.stdout, s.stderr)
253 lout, lin, lerr = select([f_out], [], [f_err], timeout)
254 if len(lin) == 0 and len(lout) == 0 and len(lerr) == 0:
255 # Reached a timeout! Nuke process so it does not hang.
256 s.kill(signal.SIGKILL)
257 raise ExceptionTimeout("TIMEOUT Running: %s" % cmd)
258 o_value = f_out.read()
260 if o_value == "": # An error has occured
261 e_value = f_err.read()
263 o_value = o_value.strip()
264 e_value = e_value.strip()
272 # no such process, due to it already exiting...
275 return (o_value, e_value)
279 def __init__(self, user, host, port=22, options = ssh_options):
280 self.options = options
286 def __options_to_str(self):
288 for o,v in self.options.iteritems():
289 options = options + "-o %s=%s " % (o,v)
292 def run(self, cmd, timeout=COMMAND_TIMEOUT*2):
293 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
294 self.user, self.host, cmd)
295 #print "SSH.run(%s)" % cmd
296 return CMD.run(self, cmd, timeout)
298 def get_file(self, rmt_filename, local_filename=None):
299 if local_filename == None:
300 local_filename = "./"
301 cmd = "scp -P %s -B %s %s@%s:%s %s" % (self.port, self.__options_to_str(),
302 self.user, self.host,
303 rmt_filename, local_filename)
305 # errors will be on stderr,
306 # success will have a blank stderr...
307 return CMD.run_noexcept(self, cmd)
309 def run_noexcept(self, cmd):
310 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
311 self.user, self.host, cmd)
312 #print "SSH.run_noexcept(%s)" % cmd
313 return CMD.run_noexcept(self, cmd)
315 def run_noexcept2(self, cmd, timeout=COMMAND_TIMEOUT*2):
316 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
317 self.user, self.host, cmd)
318 #print "SSH.run_noexcept2(%s)" % cmd
319 r = CMD.run_noexcept(self, cmd, timeout)
321 # XXX: this may be resulting in deadlocks... not sure.
322 #if self.s.returncode is None:
324 # self.s.kill(signal.SIGKILL)
326 # self.ret = self.s.returncode
331 def system2(self, cmd, timeout=COMMAND_TIMEOUT*2):
332 cmd = "ssh -p %s %s %s@%s %s" % (self.port, self.__options_to_str(),
333 self.user, self.host, cmd)
334 #print "SSH.system2(%s)" % cmd
335 return CMD.system(self, cmd, timeout)
338 cmd = "ssh -p %s %s %s@%s '%s'" % (self.port, self.__options_to_str(),
339 self.user, self.host, cmd)
342 (f_in, f_out, f_err) = os.popen3(cmd)
345 if value == "": # An error has occured
347 value = value.strip()
350 print " == %s" % value
359 self.start = time.time()
362 self.end = time.time()
363 t = self.end-self.start
367 self.end = time.time()
368 t = self.end-self.start
369 self.start = self.end