paulbsd-salt/states/_runners/process_minion_data.py

63 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
2021-11-14 13:35:25 +01:00
from salt.modules.smtp import send_msg
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
2021-11-14 13:35:25 +01:00
if type(data['return']) is dict:
for _, result in data['return'].items():
if not result['result']:
error = True
break
if result['changes']:
changes = True
break
else:
if "success" in data.keys():
2020-07-10 00:58:55 +02:00
if not data['success']:
error = True
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']])
2021-09-25 16:08:25 +02:00
body = "JobId is data['jid']\n"
2021-01-17 17:44:58 +01: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)
2021-11-14 13:35:25 +01:00
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)
2021-11-14 13:35:25 +01:00
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