def send_mail(send_from, send_to, subject, text, files=[], host="localhost", user="", password="", port=25): assert type(send_to) == list assert type(files) == list pretty_print('Preparing message') msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach(MIMEText(text)) pretty_print('Message prepared') pretty_print('Preparing files') for f in files: part = MIMEBase('application', "octet-stream") part.set_payload(open(f, "rb").read()) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) msg.attach(part) smtp = smtplib.SMTP(host=host, port=port) if len(user): smtp.starttls() smtp.login(user, password) smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close()
def validate_config(self): #config = prepare_config(config_f, 'Restart') pretty_print("Validating supervisor config section", 'debug') if not 'supervisor' in self.config: raise NotConfiguredError("Supervisor section does not exists") supervisor_conf = self.config['supervisor'] if not 'host' in supervisor_conf or not len(supervisor_conf['host']): raise NotConfiguredError("Host not set") if not 'port' in supervisor_conf['supervisor']: supervisor_conf['port'] = 9001 if not 'user' in supervisor_conf['supervisor'] or not len(supervisor_conf['user']): supervisor_conf['user'] = '******' if not 'password' in supervisor_conf or not len(supervisor_conf['password']): supervisor_conf['password'] = '' pretty_print('Config is valid!', 'debug')
def run(self, *apps, **kwargs): if not len(apps): if not 'apps' in self.config['supervisor']: raise NotConfiguredError("Apps not set") if not isinstance(self.config['supervisor']['apps'], list): raise Exception("Wrong format of 'apps' section - should be list") apps = self.config['supervisor']['apps'] env.host_string = 'localhost' pretty_print('[+] Supervisor restart procedure start', 'info') supervisor_conf = self.config['supervisor'] server = xmlrpclib.Server('http://%s:%s@%s:%i/RPC2' % ( supervisor_conf['user'], supervisor_conf['password'], supervisor_conf['host'], supervisor_conf['port']) ) env.host_string = "%s:%s" % (supervisor_conf['host'], supervisor_conf['port']) for app in apps: try: pretty_print("Stopping process: %s" % app, "info") server.supervisor.stopProcess(app) pretty_print("Starting process: %s" % app, "info") server.supervisor.startProcess(app) except xmlrpclib.Fault as ex: if ex.faultCode == 70: pretty_print("Process not running, lets start him", 'info') server.supervisor.startProcess(app) else: pretty_print("Something went wrong: %i - %s" % (ex.faultCode, ex.faultString), 'error') except xmlrpclib.ProtocolError as prot: raise Exception("ProtocolError: %s - %s " % (prot.errcode, prot.errmsg)) pretty_print('[+] Supervisor restart procedure finished', 'info')
def run(self, *apps, **kwargs): if not len(apps): if not 'apps' in self.config['supervisor']: raise NotConfiguredError("Apps not set") if not isinstance(self.config['supervisor']['apps'], list): raise Exception( "Wrong format of 'apps' section - should be list") apps = self.config['supervisor']['apps'] env.host_string = 'localhost' pretty_print('[+] Supervisor restart procedure start', 'info') supervisor_conf = self.config['supervisor'] server = xmlrpclib.Server( 'http://%s:%s@%s:%i/RPC2' % (supervisor_conf['user'], supervisor_conf['password'], supervisor_conf['host'], supervisor_conf['port'])) env.host_string = "%s:%s" % (supervisor_conf['host'], supervisor_conf['port']) for app in apps: try: pretty_print("Stopping process: %s" % app, "info") server.supervisor.stopProcess(app) pretty_print("Starting process: %s" % app, "info") server.supervisor.startProcess(app) except xmlrpclib.Fault as ex: if ex.faultCode == 70: pretty_print("Process not running, lets start him", 'info') server.supervisor.startProcess(app) else: pretty_print( "Something went wrong: %i - %s" % (ex.faultCode, ex.faultString), 'error') except xmlrpclib.ProtocolError as prot: raise Exception("ProtocolError: %s - %s " % (prot.errcode, prot.errmsg)) pretty_print('[+] Supervisor restart procedure finished', 'info')