Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
# i created a local endpoint to push webhook data to slack and use this via cron to populate notification for every job
import MySQLdb
import logging
import datetime
LOG_FILENAME = '/var/log/add_slack.log'
logging.basicConfig(filename=LOG_FILENAME,
level=logging.INFO,
)
now = datetime.datetime.now()
class dbc(object):
def __init__(self):
self.db_host = "yourhost"
self.db_username = "youruser"
self.db_password = "yourpass"
self.db_name = "rundeckdb"
def insert(self, statement):
conn = MySQLdb.Connection(host=self.db_host, user=self.db_username, passwd=self.db_password, db=self.db_name)
curs = conn.cursor()
curs.execute(statement)
conn.commit()
conn.close()
def select(self, statement):
conn = MySQLdb.Connection(host=self.db_host, user=self.db_username, passwd=self.db_password, db=self.db_name)
curs = conn.cursor()
curs.execute(statement)
conn.commit()
result = curs.fetchall()
return result
conn.close()
dbconn = dbc()
sql = "select id from scheduled_execution;"
result = dbconn.select(sql)
for i in result:
sql = "select count(*) from notification where content='http://localhost:18888/slack' and event_trigger='onfailure' and scheduled_execution_id=%s" % i[0]
test = dbconn.select(sql)[0][0]
if test != 1:
logging.info("%s didn't find result for %s, adding it" % (now, i[0]))
try:
sql = "insert into notification (version,content,event_trigger,scheduled_execution_id,type) values (1,'http://localhost:18888/slack','onfailure',%s,'url');" % i[0]
dbconn.insert(sql)
except MySQLdb.Error, e:
logging.info("%s Unable to add %s to slack notifications: %s" % (now, i[0], e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.