-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathcontext_processors.py
More file actions
163 lines (140 loc) · 5.66 KB
/
context_processors.py
File metadata and controls
163 lines (140 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright 2013-2025 Marcus Furlong <furlongm@gmail.com>
#
# This file is part of Patchman.
#
# Patchman is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 only.
#
# Patchman is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Patchman. If not, see <http://www.gnu.org/licenses/>
import subprocess
from datetime import timedelta
from importlib.metadata import version as get_pkg_version
from pathlib import Path
from django.db.models import F
from django.utils import timezone
from hosts.models import Host
from operatingsystems.models import OSRelease, OSVariant
from reports.models import Report
from repos.models import Repository
from util import get_setting_of_type
def get_git_ref():
"""Get current git ref if in a git repo."""
git_dir = Path(__file__).parent.parent / '.git'
if not git_dir.exists():
return None
try:
result = subprocess.run(
['git', 'rev-parse', '--short', 'HEAD'],
capture_output=True,
text=True,
timeout=5,
cwd=git_dir.parent,
)
if result.returncode == 0:
return result.stdout.strip()
except (subprocess.TimeoutExpired, FileNotFoundError):
pass
return None
def get_version():
"""Get version from package metadata or VERSION.txt."""
# Try importlib.metadata first (for installed packages)
try:
return get_pkg_version('patchman')
except Exception:
pass
# Fall back to VERSION.txt (for development)
version_file = Path(__file__).parent.parent / 'VERSION.txt'
try:
return version_file.read_text().strip()
except FileNotFoundError:
return 'unknown'
# Cache version info at module load time (once per process)
_PATCHMAN_VERSION = get_version()
_PATCHMAN_GIT_REF = get_git_ref()
if _PATCHMAN_GIT_REF:
_PATCHMAN_VERSION_DISPLAY = f'v{_PATCHMAN_VERSION} ({_PATCHMAN_GIT_REF})'
else:
_PATCHMAN_VERSION_DISPLAY = f'v{_PATCHMAN_VERSION}'
def patchman_version(request):
"""Context processor to provide version info for navbar."""
return {
'patchman_version': _PATCHMAN_VERSION,
'patchman_git_ref': _PATCHMAN_GIT_REF,
'patchman_version_display': _PATCHMAN_VERSION_DISPLAY,
}
def issues_count(request):
"""Context processor to provide issues count for navbar."""
if not request.user.is_authenticated:
return {'issues_count': 0}
hosts = Host.objects.all()
osvariants = OSVariant.objects.all()
osreleases = OSRelease.objects.all()
repos = Repository.objects.all()
# host issues
days = get_setting_of_type(
setting_name='DAYS_WITHOUT_REPORT',
setting_type=int,
default=14,
)
last_report_delta = timezone.now() - timedelta(days=days)
stale_hosts = hosts.filter(lastreport__lt=last_report_delta)
norepo_hosts = hosts.filter(repos__isnull=True, osvariant__osrelease__repos__isnull=True)
reboot_hosts = hosts.filter(reboot_required=True)
secupdate_hosts = hosts.filter(updates__security=True, updates__isnull=False).distinct()
bugupdate_hosts = hosts.exclude(
updates__security=True, updates__isnull=False
).distinct().filter(
updates__security=False, updates__isnull=False
).distinct()
diff_rdns_hosts = hosts.exclude(reversedns=F('hostname')).filter(check_dns=True)
# os variant issues
noosrelease_osvariants = osvariants.filter(osrelease__isnull=True)
nohost_osvariants = osvariants.filter(host__isnull=True)
# os release issues
norepo_osreleases_count = 0
if hosts.filter(host_repos_only=False).exists():
norepo_osreleases_count = osreleases.filter(repos__isnull=True).count()
# mirror issues
failed_mirrors = repos.filter(
auth_required=False, mirror__last_access_ok=False
).filter(mirror__last_access_ok=True).distinct()
disabled_mirrors = repos.filter(
auth_required=False, mirror__enabled=False, mirror__mirrorlist=False
).distinct()
norefresh_mirrors = repos.filter(auth_required=False, mirror__refresh=False).distinct()
# repo issues
failed_repos = repos.filter(
auth_required=False, mirror__last_access_ok=False
).exclude(id__in=[x.id for x in failed_mirrors]).distinct()
unused_repos = repos.filter(host__isnull=True, osrelease__isnull=True)
nomirror_repos = repos.filter(mirror__isnull=True)
nohost_repos = repos.filter(host__isnull=True)
# report issues
unprocessed_reports = Report.objects.filter(processed=False)
count = (
(1 if stale_hosts.exists() else 0) +
(1 if norepo_hosts.exists() else 0) +
(1 if reboot_hosts.exists() else 0) +
(1 if secupdate_hosts.exists() else 0) +
(1 if bugupdate_hosts.exists() else 0) +
(1 if diff_rdns_hosts.exists() else 0) +
(1 if noosrelease_osvariants.exists() else 0) +
(1 if nohost_osvariants.exists() else 0) +
(1 if norepo_osreleases_count > 0 else 0) +
(1 if failed_mirrors.exists() else 0) +
(1 if disabled_mirrors.exists() else 0) +
(1 if norefresh_mirrors.exists() else 0) +
(1 if failed_repos.exists() else 0) +
(1 if unused_repos.exists() else 0) +
(1 if nomirror_repos.exists() else 0) +
(1 if nohost_repos.exists() else 0) +
(1 if unprocessed_reports.exists() else 0)
)
return {'issues_count': count}