paulbsd-salt/states/_runners/process_minion_data.py

59 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
2021-01-17 17:44:58 +01:00
2020-07-10 00:58:55 +02:00
'''
2021-01-17 17:44:58 +01:00
For use with salt reactor
2020-07-10 00:58:55 +02:00
'''
2021-01-17 17:44:58 +01:00
2020-07-10 00:58:55 +02:00
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:
2021-01-17 17:44:58 +01:00
js = subprocess.check_output(["salt-run", "--out=json",
"jobs.lookup_jid", data['jid']])
body = f"JobId is data['jid']\n"
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)
2021-01-17 17:44:58 +01:00
2020-07-10 00:58:55 +02:00
return True
2021-01-17 17:44:58 +01:00
2020-07-10 00:58:55 +02:00
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