1
0

api_key_expire_mail.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. base_url = _config["APP_URL"].rstrip('/')
  41. if token.project:
  42. url = "%s/%s/settings#apikeys-tab" % (
  43. base_url, token.project.fullname)
  44. text = """Hi %s,
  45. Your Pagure API key %s linked to the project %s
  46. will expire in %s day(s).
  47. Please get a new key at: %s
  48. for non-interrupted service.
  49. Thanks,
  50. Your Pagure Admin. """ % (
  51. username,
  52. token.description,
  53. token.project.fullname,
  54. days_left,
  55. url,
  56. )
  57. else:
  58. url = "%s/settings#nav-api-tab" % (base_url)
  59. text = """Hi %s,
  60. Your Pagure API key %s will expire in %s day(s).
  61. Please get a new key at: %s
  62. for non-interrupted service.
  63. Thanks,
  64. Your Pagure Admin. """ % (
  65. username,
  66. token.description,
  67. days_left,
  68. url,
  69. )
  70. if not check:
  71. msg = pagure.lib.notify.send_email(text, subject, user_email)
  72. else:
  73. print(
  74. "Sending email to %s (%s) about key: %s"
  75. % (username, user_email, token.id)
  76. )
  77. if debug:
  78. print("Sent mail to %s" % username)
  79. session.remove()
  80. if debug:
  81. print("Done")
  82. if __name__ == "__main__":
  83. parser = argparse.ArgumentParser(
  84. description="Script to send email before the api token expires"
  85. )
  86. parser.add_argument(
  87. "--check",
  88. dest="check",
  89. action="store_true",
  90. default=False,
  91. help="Print the some output but does not send any email",
  92. )
  93. parser.add_argument(
  94. "--debug",
  95. dest="debug",
  96. action="store_true",
  97. default=False,
  98. help="Print the debugging output",
  99. )
  100. args = parser.parse_args()
  101. main(debug=args.debug)