Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions python/kumar_asshole.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import gmail
import sys
import re
import os
from datetime import timedelta
import datetime

GMAIL_USERNAME = ENV['GMAIL_USERNAME']
GMAIL_PASSWORD = ENV['GMAIL_PASSWORD']

GMAIL_USERNAME = os.environ['GMAIL_USERNAME']
GMAIL_PASSWORD = os.environ['GMAIL_PASSWORD']

g = gmail.login(GMAIL_USERNAME, GMAIL_PASSWORD)

Expand All @@ -14,9 +18,21 @@

msgs = g.inbox().mail(sender="[email protected]", unread=True, prefetch=True)

pattern = re.compile("\bsorry\b | \bhelp\b | \bwrong\b ", flags=re.I)
pattern = r'\bhelp\b|\bwrong\b|\bsorry\b'
db_pattern = r'\S+_staging'

for msg in msgs:
if pattern.match(msg.body):
msg.label("Database fixes")
msg.reply("No problem. I've fixed it. \n\n Please be careful next time.")
if re.search(pattern, msg, flags=re.I):
db_name = re.search(db_pattern, msg, flags=re.I)
if db_name:
dt_today = datetime.date.today()
dt_lastday = (dt_today - timedelta(days=1)).strftime ("%Y%m%d")
backup_file = '/home/backups/databases/%s-.gz'%(dt_lastday)
if os.path.isfile(backup_file):
exec_string = 'gunzip -c %s | psql %s'%(backup_file, db_name)
os.system(exec_string)
msg.label("Database fixes")
msg.reply("No problem. I've fixed it. \n\n Please be careful next time.")
else:
print 'ERROR: Backup file not found'
exit(1)