Blame files/api_key_expire_mail.py

Vivek Anand cf2adc
#!/usr/bin/env python
Vivek Anand cf2adc
Pierre-Yves Chibon 67d1cc
from __future__ import print_function, absolute_import
Vivek Anand cf2adc
import os
Vivek Anand 0a1f5d
import argparse
Vivek Anand cf2adc
from datetime import datetime, timedelta
Vivek Anand cf2adc
Vivek Anand cf2adc
from sqlalchemy.exc import SQLAlchemyError
Vivek Anand cf2adc
Pierre-Yves Chibon 250f5d
import pagure.config
Pierre-Yves Chibon 250f5d
import pagure.lib.model as model
Pierre-Yves Chibon cf98be
import pagure.lib.model_base
Pierre-Yves Chibon cf98be
import pagure.lib.notify
Pierre-Yves Chibon cf98be
import pagure.lib.query
Vivek Anand 0a1f5d
Pierre-Yves Chibon 73d120
if "PAGURE_CONFIG" not in os.environ and os.path.exists(
Pierre-Yves Chibon 73d120
    "/etc/pagure/pagure.cfg"
Pierre-Yves Chibon 73d120
):
Pierre-Yves Chibon 73d120
    print("Using configuration file `/etc/pagure/pagure.cfg`")
Pierre-Yves Chibon 73d120
    os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"
Vivek Anand cf2adc
Pierre-Yves Chibon 250f5d
_config = pagure.config.reload_config()
Vivek Anand cf2adc
Vivek Anand cf2adc
Pierre-Yves Chibon 250f5d
def main(check=False, debug=False):
Pierre-Yves Chibon 73d120
    """ The function that actually sends the email
Pierre-Yves Chibon 73d120
    in case the expiration date is near"""
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
    current_time = datetime.utcnow()
Pierre-Yves Chibon 250f5d
    day_diff_for_mail = [10, 5, 1]
Karsten Hopp 6ce2d7
    email_dates = [
Karsten Hopp 6ce2d7
        email_day.date()
Karsten Hopp 6ce2d7
        for email_day in [
Pierre-Yves Chibon 73d120
            current_time + timedelta(days=i) for i in day_diff_for_mail
Karsten Hopp 6ce2d7
        ]
Karsten Hopp 6ce2d7
    ]
Vivek Anand 0a1f5d
Pierre-Yves Chibon 73d120
    session = pagure.lib.model_base.create_session(_config["DB_URL"])
Pierre-Yves Chibon 250f5d
    tokens = session.query(model.Token).all()
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
    for token in tokens:
Pierre-Yves Chibon 250f5d
        if debug:
Pierre-Yves Chibon 250f5d
            print(token.id, token.expiration.date())
Vivek Anand 0a1f5d
        if token.expiration.date() in email_dates:
Vivek Anand 0a1f5d
            user = token.user
Pierre-Yves Chibon 250f5d
            username = user.fullname or user.username
Vivek Anand 0a1f5d
            user_email = user.default_email
Karsten Hopp 9905fb
            days_left = (token.expiration - datetime.utcnow()).days
Pierre-Yves Chibon 73d120
            subject = "Pagure API key expiration date is near!"
Pierre-Yves Chibon 57975e
            if token.project:
Pierre-Yves Chibon 73d120
                text = """Hi %s,
Pierre-Yves Chibon 57975e
Your Pagure API key %s linked to the project %s
Pierre-Yves Chibon 57975e
will expire in %s day(s).
Pierre-Yves Chibon 57975e
Please get a new key for non-interrupted service.
Pierre-Yves Chibon 57975e
Pierre-Yves Chibon 57975e
Thanks,
Pierre-Yves Chibon 73d120
Your Pagure Admin. """ % (
Karsten Hopp 6ce2d7
                    username,
Randy Barlow d18f42
                    token.description,
Karsten Hopp 6ce2d7
                    token.project.fullname,
Pierre-Yves Chibon 73d120
                    days_left,
Karsten Hopp 6ce2d7
                )
Pierre-Yves Chibon 57975e
            else:
Pierre-Yves Chibon 73d120
                text = """Hi %s,
Pierre-Yves Chibon 57975e
Your Pagure API key %s will expire in %s day(s).
Pierre-Yves Chibon 57975e
Please get a new key for non-interrupted service.
Pierre-Yves Chibon 57975e
Pierre-Yves Chibon 57975e
Thanks,
Pierre-Yves Chibon 73d120
Your Pagure Admin. """ % (
Karsten Hopp 6ce2d7
                    username,
Randy Barlow d18f42
                    token.description,
Pierre-Yves Chibon 73d120
                    days_left,
Pierre-Yves Chibon 73d120
                )
Pierre-Yves Chibon 250f5d
            if not check:
Pierre-Yves Chibon 250f5d
                msg = pagure.lib.notify.send_email(text, subject, user_email)
Pierre-Yves Chibon 250f5d
            else:
Pierre-Yves Chibon 73d120
                print(
Pierre-Yves Chibon 73d120
                    "Sending email to %s (%s) about key: %s"
Pierre-Yves Chibon 73d120
                    % (username, user_email, token.id)
Pierre-Yves Chibon 73d120
                )
Vivek Anand 0a1f5d
            if debug:
Pierre-Yves Chibon 73d120
                print("Sent mail to %s" % username)
Pierre-Yves Chibon 250f5d
Pierre-Yves Chibon 250f5d
    session.remove()
Vivek Anand 0a1f5d
    if debug:
Pierre-Yves Chibon 73d120
        print("Done")
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Vivek Anand 0a1f5d
    parser = argparse.ArgumentParser(
Pierre-Yves Chibon 73d120
        description="Script to send email before the api token expires"
Pierre-Yves Chibon 73d120
    )
Vivek Anand 0a1f5d
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "--check",
Pierre-Yves Chibon 73d120
        dest="check",
Pierre-Yves Chibon 73d120
        action="store_true",
Pierre-Yves Chibon 73d120
        default=False,
Pierre-Yves Chibon 73d120
        help="Print the some output but does not send any email",
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon 250f5d
    parser.add_argument(
Pierre-Yves Chibon 73d120
        "--debug",
Pierre-Yves Chibon 73d120
        dest="debug",
Pierre-Yves Chibon 73d120
        action="store_true",
Pierre-Yves Chibon 73d120
        default=False,
Pierre-Yves Chibon 73d120
        help="Print the debugging output",
Pierre-Yves Chibon 73d120
    )
Vivek Anand 0a1f5d
    args = parser.parse_args()
Vivek Anand 0a1f5d
    main(debug=args.debug)