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 930073
import pagure.lib.query
Pierre-Yves Chibon 930073
import pagure.lib.notify
Pierre-Yves Chibon 250f5d
import pagure.lib.model as model
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]
Vivek Anand 0a1f5d
    email_dates = [email_day.date() for email_day in \
Vivek Anand 0a1f5d
            [current_time + timedelta(days=i) for i in day_diff_for_mail]]
Vivek Anand 0a1f5d
Pierre-Yves Chibon 930073
    session = pagure.lib.query.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
Pierre-Yves Chibon 57975e
            api_key = token.id
Vivek Anand 0a1f5d
            days_left = token.expiration.day - datetime.utcnow().day
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,
Pierre-Yves Chibon 57975e
Your Pagure Admin. ''' % (username, api_key, token.project.fullname, days_left)
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,
Pierre-Yves Chibon 57975e
Your Pagure Admin. ''' % (username, api_key, 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' % (
Pierre-Yves Chibon 250f5d
                    username, user_emailk, 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)