From 250f5dd6047bd3329afdcdcdd082538a9246ed52 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 07 2018 07:48:35 +0000 Subject: [PATCH 1/3] Update the cron job sending reminder about API key expiration Port it to the new code structure. Include a --check argument allowing to see what the script will do without sending any notification. Increase the information returned on --debug Signed-off-by: Pierre-Yves Chibon --- diff --git a/files/api_key_expire_mail.py b/files/api_key_expire_mail.py index cabaf9b..041885f 100644 --- a/files/api_key_expire_mail.py +++ b/files/api_key_expire_mail.py @@ -7,42 +7,52 @@ from datetime import datetime, timedelta from sqlalchemy.exc import SQLAlchemyError +import pagure.config +import pagure.lib +import pagure.lib.model as model 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' -import pagure -import pagure.config -from pagure.lib import model, create_session +_config = pagure.config.reload_config() -def main(debug=False): +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 = [5, 3, 1] + 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 = create_session(pagure.config.config['DB_URL']) - tokens = SESSION.query(model.Token).all() + session = pagure.lib.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 project = token.project days_left = token.expiration.day - datetime.utcnow().day subject = 'Pagure API key expiration date is near!' text = '''Hi %s, \nYour Pagure API key for the project %s will expire in %s day(s). Please get a new key for non-interrupted service. \n - Thanks, \nYour Pagure Admin. ''' % (user.fullname, project.name, days_left) - msg = pagure.lib.notify.send_email(text, subject, user_email) + Thanks, \nYour Pagure Admin. ''' % (username, project.name, days_left) + 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_emailk, token.id)) if debug: - print('Sent mail to %s' % user.fullname) + print('Sent mail to %s' % username) + + session.remove() if debug: print('Done') @@ -51,7 +61,10 @@ if __name__ == '__main__': parser = argparse.ArgumentParser( description='Script to send email before the api token expires') parser.add_argument( - '--debug', dest='debug', action='store_true', default=False, - help='Print the debugging output') + '--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) From 0e8711dbf1519e9d841fb2e287e593926a9bbce2 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 07 2018 07:48:35 +0000 Subject: [PATCH 2/3] Add some documentation about cron jobs Fixes https://pagure.io/pagure/issue/2053 Signed-off-by: Pierre-Yves Chibon --- diff --git a/doc/index.rst b/doc/index.rst index fba9a4d..08135ac 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -38,6 +38,7 @@ Contents: install_pagure_ci install_pagure_loadjson install_pagure_logcom + install_crons configuration custom_gitolite_conf development diff --git a/doc/install_crons.rst b/doc/install_crons.rst new file mode 100644 index 0000000..288d842 --- /dev/null +++ b/doc/install_crons.rst @@ -0,0 +1,28 @@ +Cron Jobs +========= + +Some actions in pagure are meant to the run via a cron job. + + +API key expiration reminder +--------------------------- + +One of the cron job sending reminder about API keys that are about to expire. +It will send an email 10 days, then 5 days and finally the day before the +key expires to the person who has created. + +The cron job can be found in the sources in: :: + + files/api_key_expire_mail.py + +In the RPM it is installed in: :: + + /usr/share/pagure/api_key_expire_mail.py + +This cron job is meant to be run daily using a syntax similar to: + +:: + + 10 0 * * * root python /usr/share/pagure/api_key_expire_mail.py + +which will make the script run at 00:10 every day. From 7cd7275fb9e6b83a9659ba5a7e41ffdfea21fb84 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Aug 07 2018 07:48:35 +0000 Subject: [PATCH 3/3] Fix installing the API key reminder cron with systemd integration Signed-off-by: Pierre-Yves Chibon --- diff --git a/files/api_key_expire_mail.py b/files/api_key_expire_mail.py old mode 100644 new mode 100755 diff --git a/files/pagure.spec b/files/pagure.spec index a763729..aaac48c 100644 --- a/files/pagure.spec +++ b/files/pagure.spec @@ -158,6 +158,7 @@ in the future pull-requests) git repo. # In case it gets broken in git at least the rpm will be good chmod +x pagure/hooks/files/* +chmod +x files/api_key_expire_mail.py %build @@ -184,7 +185,7 @@ install -p -m 644 files/doc_pagure.wsgi $RPM_BUILD_ROOT/%{_datadir}/pagure/doc_p install -p -m 644 createdb.py $RPM_BUILD_ROOT/%{_datadir}/pagure/pagure_createdb.py # Install the api_key_expire_mail.py script -install -p -m 644 createdb.py $RPM_BUILD_ROOT/%{_datadir}/pagure/api_key_expire_mail.py +install -p -m 755 files/api_key_expire_mail.py $RPM_BUILD_ROOT/%{_datadir}/pagure/api_key_expire_mail.py # Install the alembic configuration file install -p -m 644 files/alembic.ini $RPM_BUILD_ROOT/%{_sysconfdir}/pagure/alembic.ini @@ -217,6 +218,13 @@ install -p -m 644 files/pagure_logcom.service \ install -p -m 644 files/pagure_loadjson.service \ $RPM_BUILD_ROOT/%{_unitdir}/pagure_loadjson.service +# Install the systemd file for the script sending reminder about API key +# expiration +install -p -m 644 files/pagure_api_key_expire_mail.service \ + $RPM_BUILD_ROOT/%{_unitdir}/pagure_api_key_expire_mail.service +install -p -m 644 files/pagure_api_key_expire_mail.timer \ + $RPM_BUILD_ROOT/%{_unitdir}/pagure_api_key_expire_mail.timer + # Install the milter files mkdir -p $RPM_BUILD_ROOT/%{_localstatedir}/run/pagure mkdir -p $RPM_BUILD_ROOT/%{_tmpfilesdir} @@ -237,6 +245,7 @@ install -p -m 644 pagure-ev/pagure_ev.service \ %post %systemd_post pagure_worker.service +%systemd_post pagure_api_key_expire_mail.timer %post milters %systemd_post pagure_milter.service %post ev @@ -252,6 +261,7 @@ install -p -m 644 pagure-ev/pagure_ev.service \ %preun %systemd_post pagure_worker.service +%systemd_post pagure_api_key_expire_mail.timer %preun milters %systemd_preun pagure_milter.service %preun ev @@ -267,6 +277,7 @@ install -p -m 644 pagure-ev/pagure_ev.service \ %postun %systemd_post pagure_worker.service +%systemd_post pagure_api_key_expire_mail.timer %postun milters %systemd_postun_with_restart pagure_milter.service %postun ev @@ -297,6 +308,8 @@ install -p -m 644 pagure-ev/pagure_ev.service \ %{_bindir}/pagure-admin %{_unitdir}/pagure_worker.service %{_unitdir}/pagure_gitolite_worker.service +%{_unitdir}/pagure_api_key_expire_mail.service +%{_unitdir}/pagure_api_key_expire_mail.timer %files milters %license LICENSE diff --git a/files/pagure_api_key_expire_mail.service b/files/pagure_api_key_expire_mail.service new file mode 100644 index 0000000..4b39835 --- /dev/null +++ b/files/pagure_api_key_expire_mail.service @@ -0,0 +1,10 @@ +[Unit] +Description=Send reminder emails about API key expiration +Documentation=https://pagure.io/pagure + +[Service] +ExecStart=/usr/share/pagure/api_key_expire_mail.py +Environment="PAGURE_CONFIG=/etc/pagure/pagure.cfg" +Type=simple +User=git +Group=git diff --git a/files/pagure_api_key_expire_mail.timer b/files/pagure_api_key_expire_mail.timer new file mode 100644 index 0000000..a527029 --- /dev/null +++ b/files/pagure_api_key_expire_mail.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Sends reminder about API key expiration + +[Timer] +OnCalendar=daily +Persistent=true +AccuracySec=1min +RandomizedDelaySec=15 + +[Install] +WantedBy=timers.target