Blame files/api_key_expire_mail.py

Vivek Anand cf2adc
#!/usr/bin/env python
Vivek Anand cf2adc
Vadim Rutkovsky 9b6a62
from __future__ import print_function
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
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
Vivek Anand cf2adc
import pagure
Aurélien Bompard 831553
import pagure.config
Aurélien Bompard 831553
from pagure.lib import model, create_session
Vivek Anand cf2adc
Vivek Anand cf2adc
Vivek Anand 0a1f5d
def main(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()
Vivek Anand 0a1f5d
    day_diff_for_mail = [5, 3, 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
Aurélien Bompard 831553
    SESSION = create_session(pagure.config.config['DB_URL'])
Vivek Anand 0a1f5d
    tokens = SESSION.query(model.Token).all()
Vivek Anand 0a1f5d
Vivek Anand 0a1f5d
    for token in tokens:
Vivek Anand 0a1f5d
        if token.expiration.date() in email_dates:
Vivek Anand 0a1f5d
            user = token.user
Vivek Anand 0a1f5d
            user_email = user.default_email
Vivek Anand 0a1f5d
            project = token.project
Vivek Anand 0a1f5d
            days_left = token.expiration.day - datetime.utcnow().day
Vivek Anand 0a1f5d
            subject = 'Pagure API key expiration date is near!'
Vivek Anand 0a1f5d
            text = '''Hi %s, \nYour Pagure API key for the project %s will expire
Vivek Anand 0a1f5d
    in %s day(s). Please get a new key for non-interrupted service. \n
Vivek Anand 0a1f5d
    Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left)
Vivek Anand 0a1f5d
            msg = pagure.lib.notify.send_email(text, subject, user_email)
Vivek Anand 0a1f5d
            if debug:
Vadim Rutkovsky 9b6a62
                print('Sent mail to %s' % user.fullname)
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(
Vivek Anand 0a1f5d
            '--debug', dest='debug', action='store_true', default=False,
Vivek Anand 0a1f5d
            help='Print the debugging output')
Vivek Anand 0a1f5d
    args = parser.parse_args()
Vivek Anand 0a1f5d
    main(debug=args.debug)