api_key_expire_mail.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #!/usr/bin/env python
  2. import os
  3. import argparse
  4. from datetime import datetime, timedelta
  5. from sqlalchemy.exc import SQLAlchemyError
  6. if 'PAGURE_CONFIG' not in os.environ \
  7. and os.path.exists('/etc/pagure/pagure.cfg'):
  8. print 'Using configuration file `/etc/pagure/pagure.cfg`'
  9. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  10. import pagure
  11. from pagure import SESSION
  12. from pagure.lib import model
  13. def main(debug=False):
  14. ''' The function that actually sends the email
  15. in case the expiration date is near'''
  16. current_time = datetime.utcnow()
  17. day_diff_for_mail = [5, 3, 1]
  18. email_dates = [email_day.date() for email_day in \
  19. [current_time + timedelta(days=i) for i in day_diff_for_mail]]
  20. tokens = SESSION.query(model.Token).all()
  21. for token in tokens:
  22. if token.expiration.date() in email_dates:
  23. user = token.user
  24. user_email = user.default_email
  25. project = token.project
  26. days_left = token.expiration.day - datetime.utcnow().day
  27. subject = 'Pagure API key expiration date is near!'
  28. text = '''Hi %s, \nYour Pagure API key for the project %s will expire
  29. in %s day(s). Please get a new key for non-interrupted service. \n
  30. Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left)
  31. msg = pagure.lib.notify.send_email(text, subject, user_email)
  32. if debug:
  33. print 'Sent mail to %s' % user.fullname
  34. if debug:
  35. print 'Done'
  36. if __name__ == '__main__':
  37. parser = argparse.ArgumentParser(
  38. description='Script to send email before the api token expires')
  39. parser.add_argument(
  40. '--debug', dest='debug', action='store_true', default=False,
  41. help='Print the debugging output')
  42. args = parser.parse_args()
  43. main(debug=args.debug)