misc updates in states

This commit is contained in:
Paul 2020-09-30 00:44:06 +02:00
parent 4b9cf3759a
commit be4f389ee6
6 changed files with 30 additions and 43 deletions

View File

@ -1,10 +1,9 @@
#!/usr/pkg/bin/python2 #!/usr/bin/python3
#-*- coding: utf-8 -*-
import os import sys
import subprocess import subprocess
import salt.modules.smtp
import json import json
import salt.modules.smtp
''' '''
For use with salt reactor For use with salt reactor
@ -17,7 +16,7 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
try: try:
if type(data['return']) is dict: if type(data['return']) is dict:
for state, result in data['return'].iteritems(): for _, result in data['return'].iteritems():
if not result['result']: if not result['result']:
error = True error = True
break break
@ -27,25 +26,29 @@ def email_errors(fromaddr, toaddrs, subject, data_str, smtp_server):
else: else:
if not data['success']: if not data['success']:
error = True error = True
except KeyError as e: except KeyError:
exit() sys.exit()
#if error or changes: if error or changes:
if error: _js = subprocess.check_output(["salt-run", "--out=json",
js = subprocess.check_output(["salt-run", "--out=json", "jobs.lookup_jid", data['jid']]) "jobs.lookup_jid", data['jid']])
body = "JobId is %s\n" % (data['jid']) body = "JobId is %s\n" % (data['jid'])
outdata = json.loads(js) outdata = json.loads(_js)
nodename = outdata.keys()[0] nodename = outdata.keys()[0]
for i in outdata[nodename]: for i in outdata[nodename]:
if not outdata[nodename][i]["result"]: if not outdata[nodename][i]["result"]:
name = outdata[nodename][i]["name"] name = outdata[nodename][i]["name"]
comment = outdata[nodename][i]["comment"].rstrip('\n') comment = outdata[nodename][i]["comment"].rstrip('\n')
data = "%s- %s / %s\n" % (body, name, comment) 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) salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
subject=subject, sender=fromaddr,
server=smtp_server, use_ssl=False)
return True return True
def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server): def email_auth(fromaddr, toaddrs, subject, data_str, smtp_server):
data = eval(data_str) data = eval(data_str)
salt.modules.smtp.send_msg(recipient=toaddrs, message=data, subject=subject, sender=fromaddr, server=smtp_server, use_ssl=False) salt.modules.smtp.send_msg(recipient=toaddrs, message=data,
subject=subject, sender=fromaddr,
server=smtp_server, use_ssl=False)
return True return True

View File

@ -33,7 +33,7 @@ def enforce_custom_thing(name, foo, bar=True):
# Start with basic error-checking. Do all the passed parameters make sense # Start with basic error-checking. Do all the passed parameters make sense
# and agree with each-other? # and agree with each-other?
if bar == True and foo.startswith('Foo'): if bar and foo.startswith('Foo'):
raise salt.exceptions.SaltInvocationError( raise salt.exceptions.SaltInvocationError(
'Argument "foo" cannot start with "Foo" if argument "bar" is True.') 'Argument "foo" cannot start with "Foo" if argument "bar" is True.')

View File

@ -4,7 +4,7 @@ from __future__ import absolute_import, print_function, unicode_literals
import salt.utils.dictupdate import salt.utils.dictupdate
import salt.utils.dictdiffer import salt.utils.dictdiffer
def jobs(name, url="http://localhost:8080", verify=False, jobs=[]): def jobs(name, url="http://localhost:8080", verify=False, job_list=[]):
ret = {'name': name, ret = {'name': name,
'changes': {}, 'changes': {},
'result': True, 'result': True,
@ -13,7 +13,7 @@ def jobs(name, url="http://localhost:8080", verify=False, jobs=[]):
#dk_jobs = [] #dk_jobs = []
#dk_jobs = __salt__['dkron.get_jobs'](url, verify) #dk_jobs = __salt__['dkron.get_jobs'](url, verify)
for job in jobs: for job in job_list:
res = __salt__['dkron.set_jobs'](url, verify, job) res = __salt__['dkron.set_jobs'](url, verify, job)
if res is not None: if res is not None:
ret['changes'][job['name']] = res ret['changes'][job['name']] = res

View File

@ -1,16 +0,0 @@
[Unit]
Description=The plugin-driven server agent for reporting metrics into InfluxDB
Documentation=https://github.com/influxdata/telegraf
After=network.target
[Service]
EnvironmentFile=-/etc/default/telegraf
#User=telegraf
ExecStart=/usr/bin/telegraf --config $INFLUX_CONFIG
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartForceExitStatus=SIGPIPE
KillMode=control-group
[Install]
WantedBy=multi-user.target

View File

@ -5,4 +5,4 @@ dkron-jobs:
- name: dkron-jobs - name: dkron-jobs
- url: {{ dkron.url }} - url: {{ dkron.url }}
- verify: {{ dkron.verify }} - verify: {{ dkron.verify }}
- jobs: {{ dkron.jobs }} - job_list: {{ dkron.job_list }}