55 lines
1.7 KiB
Python
Executable File
55 lines
1.7 KiB
Python
Executable File
#!/usr/bin/python3
|
|
|
|
import sys
|
|
import subprocess
|
|
import json
|
|
import salt.modules.smtp
|
|
|
|
'''
|
|
For use with salt reactor
|
|
'''
|
|
|
|
def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
|
|
data = eval(data_str)
|
|
error = False
|
|
changes = False
|
|
|
|
try:
|
|
if type(data['return']) is dict:
|
|
for _, result in data['return'].iteritems():
|
|
if not result['result']:
|
|
error = True
|
|
break
|
|
if result['changes']:
|
|
changes = True
|
|
break
|
|
else:
|
|
if not data['success']:
|
|
error = True
|
|
except KeyError:
|
|
sys.exit()
|
|
|
|
if error or changes:
|
|
_js = subprocess.check_output(["salt-run", "--out=json",
|
|
"jobs.lookup_jid", data['jid']])
|
|
body = "JobId is %s\n" % (data['jid'])
|
|
outdata = json.loads(_js)
|
|
nodename = outdata.keys()[0]
|
|
for i in outdata[nodename]:
|
|
if not outdata[nodename][i]["result"]:
|
|
name = outdata[nodename][i]["name"]
|
|
comment = outdata[nodename][i]["comment"].rstrip('\n')
|
|
data = "%s- %s / %s\n" % (body, name, comment)
|
|
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
|
|
subject=subject, sender=fromaddr,
|
|
server=smtp_server, use_ssl=False)
|
|
return True
|
|
|
|
def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server):
|
|
data = eval(data_str)
|
|
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
|
|
subject=subject, sender=fromaddr,
|
|
server=smtp_server, use_ssl=False)
|
|
|
|
return True
|