Skip to content

Commit 65aaab5

Browse files
Merge pull request #6717 from netbox-community/6713-release-checking
Closes #6713: Move release checking to the housekeeping routine
2 parents 028c876 + f426ba3 commit 65aaab5

File tree

9 files changed

+52
-237
lines changed

9 files changed

+52
-237
lines changed

docs/configuration/optional-settings.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -478,19 +478,11 @@ When remote user authentication is in use, this is the name of the HTTP header w
478478

479479
---
480480

481-
## RELEASE_CHECK_TIMEOUT
482-
483-
Default: 86,400 (24 hours)
484-
485-
The number of seconds to retain the latest version that is fetched from the GitHub API before automatically invalidating it and fetching it from the API again. This must be set to at least one hour (3600 seconds).
486-
487-
---
488-
489481
## RELEASE_CHECK_URL
490482

491483
Default: None (disabled)
492484

493-
This parameter defines the URL of the repository that will be checked periodically for new NetBox releases. When a new release is detected, a message will be displayed to administrative users on the home page. This can be set to the official repository (`'https://api.github.com/repos/netbox-community/netbox/releases'`) or a custom fork. Set this to `None` to disable automatic update checks.
485+
This parameter defines the URL of the repository that will be checked for new NetBox releases. When a new release is detected, a message will be displayed to administrative users on the home page. This can be set to the official repository (`'https://api.github.com/repos/netbox-community/netbox/releases'`) or a custom fork. Set this to `None` to disable automatic update checks.
494486

495487
!!! note
496488
The URL provided **must** be compatible with the [GitHub REST API](https://docs.github.com/en/rest).

docs/release-notes/version-3.0.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ CustomValidator can also be subclassed to enforce more complex logic by overridi
6868
* [#6068](https://github.com/netbox-community/netbox/issues/6068) - Drop support for legacy static CSV export
6969
* [#6338](https://github.com/netbox-community/netbox/issues/6338) - Decimal fields are no longer coerced to strings in REST API
7070
* [#6639](https://github.com/netbox-community/netbox/issues/6639) - Drop support for queryset caching (django-cacheops)
71+
* [#6713](https://github.com/netbox-community/netbox/issues/6713) - Checking for new releases is now done as part of the housekeeping routine
7172

7273
### Configuration Changes
7374

7475
* The `CACHE_TIMEOUT` configuration parameter has been removed.
76+
* The `RELEASE_CHECK_TIMEOUT` configuration parameter has been removed.
7577

7678
### REST API Changes
7779

netbox/extras/management/commands/housekeeping.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from datetime import timedelta
22
from importlib import import_module
33

4+
import requests
45
from django.conf import settings
6+
from django.core.cache import cache
57
from django.core.management.base import BaseCommand
68
from django.db import DEFAULT_DB_ALIAS
79
from django.utils import timezone
10+
from packaging import version
811

912
from extras.models import ObjectChange
1013

@@ -48,4 +51,37 @@ def handle(self, *args, **options):
4851
f"\tSkipping: No retention period specified (CHANGELOG_RETENTION = {settings.CHANGELOG_RETENTION})"
4952
)
5053

54+
# Check for new releases (if enabled)
55+
self.stdout.write("[*] Checking for latest release")
56+
if settings.RELEASE_CHECK_URL:
57+
headers = {
58+
'Accept': 'application/vnd.github.v3+json',
59+
}
60+
61+
try:
62+
self.stdout.write(f"\tFetching {settings.RELEASE_CHECK_URL}")
63+
response = requests.get(
64+
url=settings.RELEASE_CHECK_URL,
65+
headers=headers,
66+
proxies=settings.HTTP_PROXIES
67+
)
68+
response.raise_for_status()
69+
70+
releases = []
71+
for release in response.json():
72+
if 'tag_name' not in release or release.get('devrelease') or release.get('prerelease'):
73+
continue
74+
releases.append((version.parse(release['tag_name']), release.get('html_url')))
75+
latest_release = max(releases)
76+
self.stdout.write(f"\tFound {len(response.json())} releases; {len(releases)} usable")
77+
self.stdout.write(f"\tLatest release: {latest_release[0]}")
78+
79+
# Cache the most recent release
80+
cache.set('latest_release', latest_release, None)
81+
82+
except requests.exceptions.RequestException as exc:
83+
self.stdout.write(f"\tRequest error: {exc}")
84+
else:
85+
self.stdout.write(f"\tSkipping: RELEASE_CHECK_URL not set")
86+
5187
self.stdout.write("Finished.", self.style.SUCCESS)

netbox/netbox/configuration.example.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,6 @@
241241
REMOTE_AUTH_DEFAULT_GROUPS = []
242242
REMOTE_AUTH_DEFAULT_PERMISSIONS = {}
243243

244-
# This determines how often the GitHub API is called to check the latest release of NetBox. Must be at least 1 hour.
245-
RELEASE_CHECK_TIMEOUT = 24 * 3600
246-
247244
# This repository is used to check whether there is a new release of NetBox available. Set to None to disable the
248245
# version check or use the URL below to check for release in the official NetBox repository.
249246
RELEASE_CHECK_URL = None

netbox/netbox/releases.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

netbox/netbox/settings.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,13 @@
4747

4848
# Warn on removed config parameters
4949
if hasattr(configuration, 'CACHE_TIMEOUT'):
50-
warnings.warn("The CACHE_TIMEOUT configuration parameter was removed in v3.0.0 and no longer has any effect.")
50+
warnings.warn(
51+
"The CACHE_TIMEOUT configuration parameter was removed in v3.0.0 and no longer has any effect."
52+
)
53+
if hasattr(configuration, 'RELEASE_CHECK_TIMEOUT'):
54+
warnings.warn(
55+
"The RELEASE_CHECK_TIMEOUT configuration parameter was removed in v3.0.0 and no longer has any effect."
56+
)
5157

5258
# Enforce required configuration parameters
5359
for parameter in ['ALLOWED_HOSTS', 'DATABASE', 'SECRET_KEY', 'REDIS']:
@@ -114,7 +120,6 @@
114120
REMOTE_AUTH_ENABLED = getattr(configuration, 'REMOTE_AUTH_ENABLED', False)
115121
REMOTE_AUTH_HEADER = getattr(configuration, 'REMOTE_AUTH_HEADER', 'HTTP_REMOTE_USER')
116122
RELEASE_CHECK_URL = getattr(configuration, 'RELEASE_CHECK_URL', None)
117-
RELEASE_CHECK_TIMEOUT = getattr(configuration, 'RELEASE_CHECK_TIMEOUT', 24 * 3600)
118123
REPORTS_ROOT = getattr(configuration, 'REPORTS_ROOT', os.path.join(BASE_DIR, 'reports')).rstrip('/')
119124
RQ_DEFAULT_TIMEOUT = getattr(configuration, 'RQ_DEFAULT_TIMEOUT', 300)
120125
SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/')
@@ -141,10 +146,6 @@
141146
except ValidationError as err:
142147
raise ImproperlyConfigured(str(err))
143148

144-
# Enforce a minimum cache timeout for update checks
145-
if RELEASE_CHECK_TIMEOUT < 3600:
146-
raise ImproperlyConfigured("RELEASE_CHECK_TIMEOUT has to be at least 3600 seconds (1 hour)")
147-
148149

149150
#
150151
# Database
@@ -545,8 +546,7 @@ def _setting(name, default=None):
545546
}
546547

547548
RQ_QUEUES = {
548-
'default': RQ_PARAMS, # Webhooks
549-
'check_releases': RQ_PARAMS,
549+
'default': RQ_PARAMS,
550550
}
551551

552552

netbox/netbox/tests/test_releases.py

Lines changed: 0 additions & 138 deletions
This file was deleted.

netbox/netbox/views/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from django.conf import settings
55
from django.contrib.contenttypes.models import ContentType
6+
from django.core.cache import cache
67
from django.db.models import F
78
from django.http import HttpResponseServerError
89
from django.shortcuts import redirect, render
@@ -23,7 +24,6 @@
2324
from ipam.models import Aggregate, IPAddress, Prefix, VLAN, VRF
2425
from netbox.constants import SEARCH_MAX_RESULTS, SEARCH_TYPES
2526
from netbox.forms import SearchForm
26-
from netbox.releases import get_latest_release
2727
from tenancy.models import Tenant
2828
from virtualization.models import Cluster, VirtualMachine
2929

@@ -119,10 +119,10 @@ def build_stats():
119119
# Check whether a new release is available. (Only for staff/superusers.)
120120
new_release = None
121121
if request.user.is_staff or request.user.is_superuser:
122-
latest_release, release_url = get_latest_release()
123-
if isinstance(latest_release, version.Version):
124-
current_version = version.parse(settings.VERSION)
125-
if latest_release > current_version:
122+
latest_release = cache.get('latest_release')
123+
if latest_release:
124+
release_version, release_url = latest_release
125+
if release_version > version.parse(settings.VERSION):
126126
new_release = {
127127
'version': str(latest_release),
128128
'url': release_url,

netbox/utilities/background_tasks.py

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)