def emailFailure(self, bashfile, cmd, exception, to_cellphone=False): hostname = socket.getfqdn() content = """ hostname: %s Bash Wrapper failed to execute a cmd in a bash file. Bash File: %s Cmd: %s Error: %s """ % (hostname, bashfile, cmd, exception) subject = '%s failed' % (hostname) print("emailing traceback to %s" % ', '.join(destination)) sendemail(sender, destination, content, subject, username, password, relay) if to_cellphone: print("emailing msg to phone. phone: %s msg: %s" % (cellphone_dest, cellphone_msg)) sendemail(sender, cellphone_dest, cellphone_msg, '', username, password, relay)
timestart, timeend, stdout, env = \ self.execute(cmd, env, stderr_okay) writer.write(self.report(cmd, timestart, timeend, stdout)) except Exception, e: self.emailFailure(bashfile, cmd, e, to_cellphone=True) # used to page cell phone raise finally: writer.flush() if writer!=sys.stdout: writer.close() if email_on_success: #Email when finished hostname = socket.getfqdn() subject = "Reports Finished %s" % hostname content = "%s successfully completed at %s" % (hostname, datetime.datetime.now().isoformat()) sendemail(sender, destination, content, subject, username, password, relay) def execute(self, cmd, env, stderr_okay=False): """Given bash cmd as string and bash environment, Execute bash cmd and return output, updated env, and start/end time """ marker = "____BASHWRAPPER_SPLITONTHIS____" cmd += " ; echo %s ; set" % (marker) timestart = datetime.datetime.now() shell = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, env=env)