#3463 Update the cron job sending reminder about API key expiration
Merged 5 years ago by pingou. Opened 5 years ago by pingou.

file modified
+1
@@ -38,6 +38,7 @@ 

     install_pagure_ci

     install_pagure_loadjson

     install_pagure_logcom

+    install_crons

     configuration

     custom_gitolite_conf

     development

@@ -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.

file modified
+25 -12
@@ -7,42 +7,52 @@ 

  

  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 @@ 

      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)

file modified
+14 -1
@@ -158,6 +158,7 @@ 

  

  # 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 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_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 @@ 

  

  %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 @@ 

  

  %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 @@ 

  

  %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 @@ 

  %{_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

@@ -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

@@ -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

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 pingou@pingoured.fr

rebased onto 825a6e58f9c7ceb7231c64eb12c107388ca7dc66

5 years ago

Why don't we just ship systemd timers for these?

Metadata Update from @pingou:
- Request assigned

5 years ago

Metadata Update from @pingou:
- Request reset

5 years ago

Metadata Update from @pingou:
- Pull-request tagged with: RFE

5 years ago

Metadata Update from @pingou:
- Pull-request untagged with: RFE

5 years ago

We could but I don't have much experience with them, would you have a good doc for them? Do we install them via the spec file? If not, then I guess we could just mention them in the doc no?

systemd ships with a good doc on it: https://www.freedesktop.org/software/systemd/man/systemd.timer.html

It's pretty easy to write one that works for this.

rebased onto 8d3aaf6b370bf5fb5bee47ba508628b0c3e1c13b

5 years ago

3 new commits added

  • Fix installing the API key reminder cron with systemd integration
  • Add some documentation about cron jobs
  • Update the cron job sending reminder about API key expiration
5 years ago

This needs to be pagure_api_key_expire_mail.timer

This needs to be pagure_api_key_expire_mail.timer

This needs to be pagure_api_key_expire_mail.timer

Also, you're missing file list entries for the pagure_api_key_expire_mail.{service,timer} files.

3 new commits added

  • Fix installing the API key reminder cron with systemd integration
  • Add some documentation about cron jobs
  • Update the cron job sending reminder about API key expiration
5 years ago

3 new commits added

  • Fix installing the API key reminder cron with systemd integration
  • Add some documentation about cron jobs
  • Update the cron job sending reminder about API key expiration
5 years ago

Wrong entries.

It should be:

%{_unitdir}/pagure_api_key_expire_mail.service
%{_unitdir}/pagure_api_key_expire_mail.timer

This needs to be .timer.

This needs to be .timer.

This needs to be .timer.

This needs to be installed as executable, or it won't run.

Wouldn't this be https://docs.pagure.org/pagure/?

rebased onto b4b0c30613e1c8c9e2dce7911a00abc4e654c38c

5 years ago

Your chmod +x earlier does not matter, since you're forcefully changing the mode to 644 here on install.

4 new commits added

  • Disable tests that is no longer supported
  • Fix installing the API key reminder cron with systemd integration
  • Update the cron job sending reminder about API key expiration
  • Add some documentation about cron jobs
5 years ago

4 new commits added

  • Disable tests that is no longer supported
  • Fix installing the API key reminder cron with systemd integration
  • Update the cron job sending reminder about API key expiration
  • Add some documentation about cron jobs
5 years ago

4 new commits added

  • Properly install the api_key_expire_mail script with systemd integration
  • Fix installing the API key reminder cron with systemd integration
  • Update the cron job sending reminder about API key expiration
  • Add some documentation about cron jobs
5 years ago

rebased onto 8d3aaf6b370bf5fb5bee47ba508628b0c3e1c13b

5 years ago

rebased onto 250f5dd

5 years ago

Many thanks for your help, that PR was not my best work ^_^

Pull-Request has been merged by pingou

5 years ago