From cf2adc1806d78a81fa380309d1f32c5036a99ae7 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Jun 12 2016 10:59:07 +0000 Subject: [PATCH 1/2] Send email before api token expires --- diff --git a/files/api_key_expire_mail.py b/files/api_key_expire_mail.py new file mode 100644 index 0000000..fa81414 --- /dev/null +++ b/files/api_key_expire_mail.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python + +import os +from datetime import datetime, timedelta + +from sqlalchemy.exc import SQLAlchemyError + +if 'PAGURE_CONFIG' not in os.environ \ + and os.path.exists('/etc/pagure/pagure.cfg'): + print 'Using configuration file `/etc/pagure/pagure.cfg`' + os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg' + +import pagure +from pagure import SESSION +from pagure.lib import model + + +current_time = datetime.utcnow() +day_diff_for_mail = [5, 3, 1] +email_dates = [email_day.date() for email_day in [current_time + timedelta(days=i)\ + for i in day_diff_for_mail]] + +tokens = SESSION.query(model.Token).all() + +for token in tokens: + if token.expiration.date() in email_dates: + user = token.user + user_email = user.default_email + project = token.project + days_left = token.expiration.day - datetime.utcnow().day + subject = 'Pagure API key expiration date is near!' + text = '''Hi %s, \nYour Pagure API key for the project %s will expire +in %s day(s). Please get a new key for non-interrupted service. \n +Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left) + msg = pagure.lib.notify.send_email(text, subject, user_email) + print 'Sent mail to %s' % user.fullname + +print 'Done' From 0a1f5d17e05154652121e25e8e940d126ff8f950 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Jun 12 2016 10:59:07 +0000 Subject: [PATCH 2/2] Add debug option in the script for sending email before the api token expires --- diff --git a/files/api_key_expire_mail.py b/files/api_key_expire_mail.py index fa81414..504a758 100644 --- a/files/api_key_expire_mail.py +++ b/files/api_key_expire_mail.py @@ -1,10 +1,12 @@ #!/usr/bin/env python import os +import argparse from datetime import datetime, timedelta from sqlalchemy.exc import SQLAlchemyError + if 'PAGURE_CONFIG' not in os.environ \ and os.path.exists('/etc/pagure/pagure.cfg'): print 'Using configuration file `/etc/pagure/pagure.cfg`' @@ -15,24 +17,39 @@ from pagure import SESSION from pagure.lib import model -current_time = datetime.utcnow() -day_diff_for_mail = [5, 3, 1] -email_dates = [email_day.date() for email_day in [current_time + timedelta(days=i)\ - for i in day_diff_for_mail]] - -tokens = SESSION.query(model.Token).all() - -for token in tokens: - if token.expiration.date() in email_dates: - user = token.user - user_email = user.default_email - project = token.project - days_left = token.expiration.day - datetime.utcnow().day - subject = 'Pagure API key expiration date is near!' - text = '''Hi %s, \nYour Pagure API key for the project %s will expire -in %s day(s). Please get a new key for non-interrupted service. \n -Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left) - msg = pagure.lib.notify.send_email(text, subject, user_email) - print 'Sent mail to %s' % user.fullname - -print 'Done' +def main(debug=False): + ''' The function that actually sends the email + in case the expiration date is near''' + + current_time = datetime.utcnow() + day_diff_for_mail = [5, 3, 1] + email_dates = [email_day.date() for email_day in \ + [current_time + timedelta(days=i) for i in day_diff_for_mail]] + + tokens = SESSION.query(model.Token).all() + + for token in tokens: + if token.expiration.date() in email_dates: + user = token.user + user_email = user.default_email + project = token.project + days_left = token.expiration.day - datetime.utcnow().day + subject = 'Pagure API key expiration date is near!' + text = '''Hi %s, \nYour Pagure API key for the project %s will expire + in %s day(s). Please get a new key for non-interrupted service. \n + Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left) + msg = pagure.lib.notify.send_email(text, subject, user_email) + if debug: + print 'Sent mail to %s' % user.fullname + if debug: + print 'Done' + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Script to send email before the api token expires') + parser.add_argument( + '--debug', dest='debug', action='store_true', default=False, + help='Print the debugging output') + args = parser.parse_args() + main(debug=args.debug)