Added version check based on git

Using local variable to cache the response and only check every 24 hours
This commit is contained in:
TakeMeNL 2018-12-29 23:58:53 +01:00
parent f7f6704fc1
commit 36d5d05b72
7 changed files with 91 additions and 2 deletions

View File

@ -255,6 +255,23 @@ Enable this option to run the webhook backend. See the docs section on the webho
---
## CHECK_UPDATES
Default: False
Enable this option to check automatically for updates.
------
## CHECK_UPDATES_URL
Default: https://github.com/digitalocean/netbox.git
In case if you're using a fork or a self hosted version you can change the version check url.
Because we use git to check version updates based on tags.
---
## Date and Time Formatting
You may define custom formatting for date and times. For detailed instructions on writing format strings, please see [the Django documentation](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#date).

View File

@ -158,3 +158,6 @@ TIME_FORMAT = 'g:i a'
SHORT_TIME_FORMAT = 'H:i:s'
DATETIME_FORMAT = 'N j, Y g:i a'
SHORT_DATETIME_FORMAT = 'Y-m-d H:i'
# Automatically check for updates if enabled. This feature requires a internet connection.
CHECK_UPDATES = False

View File

@ -337,6 +337,10 @@ INTERNAL_IPS = (
'::1',
)
# Automatic update check
CHECK_UPDATES = getattr(configuration, 'CHECK_UPDATES', False)
CHECK_UPDATES_URL = getattr(configuration, 'CHECK_UPDATES_URL', 'https://github.com/digitalocean/netbox.git')
HAS_UPDATES = {'latest_version': '1.0', 'last_updated': None}
try:
HOSTNAME = socket.gethostname()

View File

@ -47,7 +47,7 @@
<div class="container">
<div class="row">
<div class="col-xs-4">
<p class="text-muted">{{ settings.HOSTNAME }} (v{{ settings.VERSION }})</p>
<p class="text-muted">{{ settings.HOSTNAME }} (v{{ settings.VERSION }}) {% check_updates %}</p>
</div>
<div class="col-xs-4 text-center">
<p class="text-muted">{% now 'Y-m-d H:i:s T' %}</p>

View File

@ -0,0 +1,3 @@
{% if latest_version %}
<a href="{{ url }}" target="_blank"><span class="label label-warning">New version: {{ latest_version }}</span></a>
{% endif %}

View File

@ -8,7 +8,7 @@ from markdown import markdown
from utilities.forms import unpack_grouped_choices
from utilities.utils import foreground_color
from utilities.version_check import check_newer_version
register = template.Library()
@ -208,3 +208,11 @@ def tag(tag, url_name=None):
'tag': tag,
'url_name': url_name,
}
@register.inclusion_tag('utilities/templatetags/checkUpdates.html')
def check_updates():
"""
Display if there is a update
"""
return check_newer_version()

View File

@ -0,0 +1,54 @@
import subprocess
import re
from distutils.version import LooseVersion
from django.conf import settings
from datetime import datetime, timedelta
def check_newer_version():
def _git_get_latest_version():
version = '1.0'
try:
ps = subprocess.Popen(('git', 'ls-remote', '--tags', settings.CHECK_UPDATES_URL), stdout=subprocess.PIPE)
output = subprocess.check_output(('tail', '-1'), stdin=ps.stdout)
ps.wait()
except FileNotFoundError:
# Command not found, so git not installed
settings.CHECK_UPDATES = False
return LooseVersion(version)
output = re.search('refs/tags/v(.*)', output.decode())
if output is not None:
version = output[1]
return LooseVersion(version)
# If check_updates enabled?
if not settings.CHECK_UPDATES:
return {}
if type(settings.HAS_UPDATES.get('latest_version')) is str:
latest_version = LooseVersion(settings.HAS_UPDATES.get('latest_version'))
else:
latest_version = settings.HAS_UPDATES.get('latest_version')
# If dev, remove it
cur_version = LooseVersion(settings.VERSION.replace('-dev', ''))
last_updated = settings.HAS_UPDATES.get('last_updated')
# Check every 24 hours for updates
if last_updated is None or ((datetime.utcnow() - last_updated) > timedelta(1)):
latest_version = _git_get_latest_version()
settings.HAS_UPDATES = {'latest_version': latest_version, 'last_updated': datetime.utcnow()}
# Check if this version is older then the newer version
if cur_version >= latest_version:
return {}
return {
'url': settings.CHECK_UPDATES_URL.replace('.git', ''),
'latest_version': latest_version.__str__(),
}