api_key_expire_mail.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python
  2. from __future__ import print_function, absolute_import
  3. import os
  4. import argparse
  5. from datetime import datetime, timedelta
  6. from sqlalchemy.exc import SQLAlchemyError
  7. import pagure.config
  8. import pagure.lib.model as model
  9. import pagure.lib.model_base
  10. import pagure.lib.notify
  11. import pagure.lib.query
  12. if "PAGURE_CONFIG" not in os.environ and os.path.exists(
  13. "/etc/pagure/pagure.cfg"
  14. ):
  15. print("Using configuration file `/etc/pagure/pagure.cfg`")
  16. os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"
  17. _config = pagure.config.reload_config()
  18. def main(check=False, debug=False):
  19. """ The function that actually sends the email
  20. in case the expiration date is near"""
  21. current_time = datetime.utcnow()
  22. day_diff_for_mail = [10, 5, 1]
  23. email_dates = [
  24. email_day.date()
  25. for email_day in [
  26. current_time + timedelta(days=i) for i in day_diff_for_mail
  27. ]
  28. ]
  29. session = pagure.lib.model_base.create_session(_config["DB_URL"])
  30. tokens = session.query(model.Token).all()
  31. for token in tokens:
  32. if debug:
  33. print(token.id, token.expiration.date())
  34. if token.expiration.date() in email_dates:
  35. user = token.user
  36. username = user.fullname or user.username
  37. user_email = user.default_email
  38. days_left = (token.expiration - datetime.utcnow()).days
  39. subject = "Pagure API key expiration date is near!"
  40. if token.project:
  41. text = """Hi %s,
  42. Your Pagure API key %s linked to the project %s
  43. will expire in %s day(s).
  44. Please get a new key for non-interrupted service.
  45. Thanks,
  46. Your Pagure Admin. """ % (
  47. username,
  48. token.description,
  49. token.project.fullname,
  50. days_left,
  51. )
  52. else:
  53. text = """Hi %s,
  54. Your Pagure API key %s will expire in %s day(s).
  55. Please get a new key for non-interrupted service.
  56. Thanks,
  57. Your Pagure Admin. """ % (
  58. username,
  59. token.description,
  60. days_left,
  61. )
  62. if not check:
  63. msg = pagure.lib.notify.send_email(text, subject, user_email)
  64. else:
  65. print(
  66. "Sending email to %s (%s) about key: %s"
  67. % (username, user_email, token.id)
  68. )
  69. if debug:
  70. print("Sent mail to %s" % username)
  71. session.remove()
  72. if debug:
  73. print("Done")
  74. if __name__ == "__main__":
  75. parser = argparse.ArgumentParser(
  76. description="Script to send email before the api token expires"
  77. )
  78. parser.add_argument(
  79. "--check",
  80. dest="check",
  81. action="store_true",
  82. default=False,
  83. help="Print the some output but does not send any email",
  84. )
  85. parser.add_argument(
  86. "--debug",
  87. dest="debug",
  88. action="store_true",
  89. default=False,
  90. help="Print the debugging output",
  91. )
  92. args = parser.parse_args()
  93. main(debug=args.debug)