Created
October 9, 2015 19:29
-
-
Save looprock/c83df16d9f8b3ea93693 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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