File: api_key_expire_mail.py

package info (click to toggle)
pagure 5.11.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,640 kB
  • sloc: python: 113,281; javascript: 23,100; makefile: 194; sh: 66
file content (117 lines) | stat: -rwxr-xr-x 3,411 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python3

from __future__ import print_function, absolute_import
import os
import argparse
from datetime import datetime, timedelta

from sqlalchemy.exc import SQLAlchemyError

import pagure.config
import pagure.lib.model as model
import pagure.lib.model_base
import pagure.lib.notify
import pagure.lib.query

if "PAGURE_CONFIG" not in os.environ and os.path.exists(
    "/etc/pagure/pagure.cfg"
):
    print("Using configuration file `/etc/pagure/pagure.cfg`")
    os.environ["PAGURE_CONFIG"] = "/etc/pagure/pagure.cfg"

_config = pagure.config.reload_config()


def main(check=False, debug=False):
    """ The function that actually sends the email
    in case the expiration date is near"""

    current_time = datetime.utcnow()
    day_diff_for_mail = [10, 5, 1]
    email_dates = [
        email_day.date()
        for email_day in [
            current_time + timedelta(days=i) for i in day_diff_for_mail
        ]
    ]

    session = pagure.lib.model_base.create_session(_config["DB_URL"])
    tokens = session.query(model.Token).all()

    for token in tokens:
        if debug:
            print(token.id, token.expiration.date())
        if token.expiration.date() in email_dates:
            user = token.user
            username = user.fullname or user.username
            user_email = user.default_email
            days_left = (token.expiration - datetime.utcnow()).days
            subject = "Pagure API key expiration date is near!"
            base_url = _config["APP_URL"].rstrip('/')
            if token.project:
                url = "%s/%s/settings#apikeys-tab" % (
                    base_url, token.project.fullname)
                text = """Hi %s,
Your Pagure API key %s linked to the project %s
will expire in %s day(s).
Please get a new key at: %s
for non-interrupted service.

Thanks,
Your Pagure Admin. """ % (
                    username,
                    token.description,
                    token.project.fullname,
                    days_left,
                    url,
                )
            else:
                url = "%s/settings#nav-api-tab" % (base_url)
                text = """Hi %s,
Your Pagure API key %s will expire in %s day(s).
Please get a new key at: %s
for non-interrupted service.


Thanks,
Your Pagure Admin. """ % (
                    username,
                    token.description,
                    days_left,
                    url,
                )
            if not check:
                msg = pagure.lib.notify.send_email(text, subject, user_email)
            else:
                print(
                    "Sending email to %s (%s) about key: %s"
                    % (username, user_email, token.id)
                )
            if debug:
                print("Sent mail to %s" % username)

    session.remove()
    if debug:
        print("Done")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Script to send email before the api token expires"
    )
    parser.add_argument(
        "--check",
        dest="check",
        action="store_true",
        default=False,
        help="Print the some output but does not send any email",
    )
    parser.add_argument(
        "--debug",
        dest="debug",
        action="store_true",
        default=False,
        help="Print the debugging output",
    )
    args = parser.parse_args()
    main(debug=args.debug)