paulbsd-salt/states/_runners/process_minion_data.py

55 lines
1.7 KiB
Python
Raw Normal View History

2020-09-30 00:44:06 +02:00
#!/usr/bin/python3
2020-07-10 00:58:55 +02:00
2020-09-30 00:44:06 +02:00
import sys
2020-07-10 00:58:55 +02:00
import subprocess
import json
2020-09-30 00:44:06 +02:00
import salt.modules.smtp
2020-07-10 00:58:55 +02:00
'''
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:
2020-09-30 00:44:06 +02:00
for _, result in data['return'].iteritems():
2020-07-10 00:58:55 +02:00
if not result['result']:
error = True
break
if result['changes']:
changes = True
break
else:
if not data['success']:
error = True
2020-09-30 00:44:06 +02:00
except KeyError:
sys.exit()
2020-07-10 00:58:55 +02:00
2020-09-30 00:44:06 +02:00
if error or changes:
_js = subprocess.check_output(["salt-run", "--out=json",
"jobs.lookup_jid", data['jid']])
2020-07-10 00:58:55 +02:00
body = "JobId is %s\n" % (data['jid'])
2020-09-30 00:44:06 +02:00
outdata = json.loads(_js)
2020-07-10 00:58:55 +02:00
nodename = outdata.keys()[0]
for i in outdata[nodename]:
if not outdata[nodename][i]["result"]:
2020-09-30 00:44:06 +02:00
name = outdata[nodename][i]["name"]
2020-07-10 00:58:55 +02:00
comment = outdata[nodename][i]["comment"].rstrip('\n')
data = "%s- %s / %s\n" % (body, name, comment)
2020-09-30 00:44:06 +02:00
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
subject=subject, sender=fromaddr,
server=smtp_server, use_ssl=False)
2020-07-10 00:58:55 +02:00
return True
def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server):
data = eval(data_str)
2020-09-30 00:44:06 +02:00
salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
subject=subject, sender=fromaddr,
server=smtp_server, use_ssl=False)
2020-07-10 00:58:55 +02:00
2020-09-30 00:44:06 +02:00
return True