From 32bde8ee001fba6f6f2ec627a5307e00990d40d4 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Thu, 31 Jan 2019 14:26:28 +0000 Subject: [PATCH] Prepare for superuser Git revision display --- manager/cronjobs.py | 11 +++++++++++ manager/templatetags/repo.py | 15 +++++++++++++++ setup.sh | 2 +- vpnmanager/settings.py | 7 +++++++ 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 manager/cronjobs.py create mode 100644 manager/templatetags/repo.py diff --git a/manager/cronjobs.py b/manager/cronjobs.py new file mode 100644 index 0000000..696f7f2 --- /dev/null +++ b/manager/cronjobs.py @@ -0,0 +1,11 @@ +from git import Repo +from django_cron import CronJobBase, Schedule +from django.conf import settings + +class FetchRemoteCron(CronJobBase): + schedule = Schedule(run_every_mins=5) + code = "manager.fetchremotecron" + + def do(self): + repo = Repo(settings.BASE_DIR) + repo.remotes[0].fetch() diff --git a/manager/templatetags/repo.py b/manager/templatetags/repo.py new file mode 100644 index 0000000..18e7982 --- /dev/null +++ b/manager/templatetags/repo.py @@ -0,0 +1,15 @@ +from django import template +from django.conf import settings +from git import Repo + +register = template.Library() + +@register.simple_tag +def getCommit(): + repo = Repo(settings.BASE_DIR) + return "%s (%s)" % (str(repo.head.commit)[:7], str(repo.head.commit.committed_datetime)) + +def isLatestCommit(): + repo = Repo(settings.BASE_DIR) + origin = repo.remotes[0].refs[0] + return origin.commit.committed_datetime <= repo.head.commit.committed_datetime diff --git a/setup.sh b/setup.sh index d8ed1d9..d9b744e 100644 --- a/setup.sh +++ b/setup.sh @@ -2,7 +2,7 @@ sudo apt update sudo apt install -y python3-pip npm openvpn easy-rsa postfix -sudo pip3 install -U django uwsgi django-two-factor-auth django-bootstrap-form twilio argon2_cffi +sudo pip3 install -U django django-cron uwsgi django-two-factor-auth django-bootstrap-form twilio argon2_cffi GitPython sudo useradd vpn git submodule init git submodule update diff --git a/vpnmanager/settings.py b/vpnmanager/settings.py index 491cba5..78aca5e 100644 --- a/vpnmanager/settings.py +++ b/vpnmanager/settings.py @@ -33,6 +33,7 @@ INSTALLED_APPS = [ 'django.contrib.messages', 'django.contrib.staticfiles', 'manager.apps.ManagerConfig', + 'django_cron', ] MIDDLEWARE = [ @@ -134,3 +135,9 @@ PASSWORD_HASHERS = [ 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ] + +# Cron Jobs + +CRON_CLASSES = [ + "manager.cronjobs.FetchRemoteCron", + ]