1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Automatically check for new versions

This commit is contained in:
Sander Steffann
2020-01-24 00:15:32 +01:00
parent faf676e6e0
commit 2fcdc90d3f
6 changed files with 94 additions and 1 deletions

View File

@@ -124,6 +124,14 @@ EXEMPT_VIEW_PERMISSIONS = [
# 'ipam.prefix',
]
# This repository is used to check whether there is a new release of NetBox available. Set to None to disable the
# version check.
GITHUB_REPOSITORY = 'netbox-community/netbox'
# This determines how often the GitHub API is called to check the latest release of NetBox. Set to 0 to disable the
# version check.
GITHUB_VERSION_TIMEOUT = 8 * 3600
# Enable custom logging. Please see the Django documentation for detailed guidance on configuring custom logs:
# https://docs.djangoproject.com/en/stable/topics/logging/
LOGGING = {}

View File

@@ -1,6 +1,7 @@
import logging
import os
import platform
import re
import socket
import warnings
@@ -78,6 +79,8 @@ DEVELOPER = getattr(configuration, 'DEVELOPER', False)
EMAIL = getattr(configuration, 'EMAIL', {})
ENFORCE_GLOBAL_UNIQUE = getattr(configuration, 'ENFORCE_GLOBAL_UNIQUE', False)
EXEMPT_VIEW_PERMISSIONS = getattr(configuration, 'EXEMPT_VIEW_PERMISSIONS', [])
GITHUB_REPOSITORY = getattr(configuration, 'GITHUB_REPOSITORY', 'netbox-community/netbox')
GITHUB_VERSION_TIMEOUT = getattr(configuration, 'GITHUB_VERSION_TIMEOUT', 8 * 3600)
LOGGING = getattr(configuration, 'LOGGING', {})
LOGIN_REQUIRED = getattr(configuration, 'LOGIN_REQUIRED', False)
LOGIN_TIMEOUT = getattr(configuration, 'LOGIN_TIMEOUT', None)
@@ -292,6 +295,7 @@ TEMPLATES = [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'utilities.context_processors.settings',
'utilities.context_processors.latest_version',
],
},
},
@@ -302,6 +306,12 @@ AUTHENTICATION_BACKENDS = [
'utilities.auth_backends.ViewExemptModelBackend',
]
# GitHub repository for version check
if GITHUB_REPOSITORY and not re.fullmatch(r'[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+', GITHUB_REPOSITORY):
raise ImproperlyConfigured(
"GITHUB_REPOSITORY must contain the name of a GitHub repository in the form '<owner>/<repository>'"
)
# Internationalization
LANGUAGE_CODE = 'en-us'
USE_I18N = True

View File

@@ -50,7 +50,16 @@
<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 }})
{% if latest_version %}
{% if latest_version_url %}<a target="_blank" href="{{ latest_version_url }}">{% endif %}
<span class="label label-default" style="color: #ffffff; background-color: #ff9800">
New version: {{ latest_version }}
</span>
{% if latest_version_url %}</a>{% endif %}
{% endif %}
</p>
</div>
<div class="col-xs-4 text-center">
<p class="text-muted">{% now 'Y-m-d H:i:s T' %}</p>

View File

@@ -1,4 +1,7 @@
from django.conf import settings as django_settings
from packaging import version
from utilities.versions import get_latest_version
def settings(request):
@@ -8,3 +11,23 @@ def settings(request):
return {
'settings': django_settings,
}
def latest_version(request):
"""
Get the latest version from the GitHub repository
"""
github_latest_version, github_url = get_latest_version()
latest_version_str = None
latest_version_url = None
if isinstance(github_latest_version, version.Version):
current_version = version.parse(django_settings.VERSION)
if github_latest_version > current_version:
latest_version_str = str(github_latest_version)
latest_version_url = github_url
return {
'latest_version': latest_version_str,
'latest_version_url': latest_version_url
}

View File

@@ -0,0 +1,27 @@
import requests
from cacheops import cached
from django.conf import settings
from packaging import version
if settings.GITHUB_VERSION_TIMEOUT and settings.GITHUB_REPOSITORY:
@cached(timeout=settings.GITHUB_VERSION_TIMEOUT)
def get_latest_version():
url = 'https://api.github.com/repos/{}/releases'.format(settings.GITHUB_REPOSITORY)
headers = {
'Accept': 'application/vnd.github.v3+json',
}
try:
response = requests.get(url, headers=headers)
versions = [(version.parse(release['tag_name']), release.get('html_url'))
for release in response.json()
if 'tag_name' in release]
if versions:
return max(versions)
except:
pass
return 'unknown', None
else:
def get_latest_version():
return None