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
Vivek Anand cf2adc
if 'PAGURE_CONFIG' not in os.environ \
Vivek Anand cf2adc
        and os.path.exists('/etc/pagure/pagure.cfg'):
Vadim Rutkovsky 9b6a62
    print('Using configuration file `/etc/pagure/pagure.cfg`')
Vivek Anand cf2adc
    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):
Vivek Anand 0a1f5d
    ''' The function that actually sends the email
Vivek Anand 0a1f5d
    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 [
Karsten Hopp 6ce2d7
            current_time + timedelta(days=i)
Karsten Hopp 6ce2d7
                   for i in day_diff_for_mail
Karsten Hopp 6ce2d7
        ]
Karsten Hopp 6ce2d7
    ]
Vivek Anand 0a1f5d
Pierre-Yves Chibon cf98be
    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
Vivek Anand 0a1f5d
            subject = 'Pagure API key expiration date is near!'
Pierre-Yves Chibon 57975e
            if token.project:
Pierre-Yves Chibon 57975e
                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,
Karsten Hopp 6ce2d7
Your Pagure Admin. ''' % (
Karsten Hopp 6ce2d7
                    username,
Randy Barlow d18f42
                    token.description,
Karsten Hopp 6ce2d7
                    token.project.fullname,
Karsten Hopp 6ce2d7
                    days_left
Karsten Hopp 6ce2d7
                )
Pierre-Yves Chibon 57975e
            else:
Pierre-Yves Chibon 57975e
                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,
Karsten Hopp 6ce2d7
Your Pagure Admin. ''' % (
Karsten Hopp 6ce2d7
                    username,
Randy Barlow d18f42
                    token.description,
Karsten Hopp 6ce2d7
                    days_left)
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 250f5d
                print('Sending email to %s (%s) about key: %s' % (
Karsten Hopp 6ce2d7
                    username, user_email, token.id))
Vivek Anand 0a1f5d
            if debug:
Pierre-Yves Chibon 250f5d
                print('Sent mail to %s' % username)
Pierre-Yves Chibon 250f5d
Pierre-Yves Chibon 250f5d
    session.remove()
Vivek Anand 0a1f5d
    if debug:
Vadim Rutkovsky 9b6a62
        print('Done')
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
if __name__ == '__main__':
Vivek Anand 0a1f5d
    parser = argparse.ArgumentParser(
Vivek Anand 0a1f5d
            description='Script to send email before the api token expires')
Vivek Anand 0a1f5d
    parser.add_argument(
Pierre-Yves Chibon 250f5d
        '--check', dest='check', action='store_true', default=False,
Pierre-Yves Chibon 250f5d
        help='Print the some output but does not send any email')
Pierre-Yves Chibon 250f5d
    parser.add_argument(
Pierre-Yves Chibon 250f5d
        '--debug', dest='debug', action='store_true', default=False,
Pierre-Yves Chibon 250f5d
        help='Print the debugging output')
Vivek Anand 0a1f5d
    args = parser.parse_args()
Vivek Anand 0a1f5d
    main(debug=args.debug)