Skip to content

Commit fcee566

Browse files
committed
consolidate and improve homogenous host update logic
1 parent 7b95a51 commit fcee566

File tree

3 files changed

+50
-81
lines changed

3 files changed

+50
-81
lines changed

hosts/tasks.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from celery import shared_task
1818

1919
from hosts.models import Host
20-
from util import get_datetime_now
21-
from util.logging import info_message
20+
from hosts.utils import find_host_updates_homogenous
2221

2322

2423
@shared_task(priority=0)
@@ -41,35 +40,4 @@ def find_all_host_updates():
4140
def find_all_host_updates_homogenous():
4241
""" Task to find updates for all hosts where hosts are expected to be homogenous
4342
"""
44-
updated_host_ids = set()
45-
ts = get_datetime_now()
46-
for host in Host.objects.all().iterator():
47-
if host.id not in updated_host_ids:
48-
host.find_updates()
49-
host.updated_at = ts
50-
host.save()
51-
52-
# only include hosts with the exact same number of packages
53-
filtered_hosts = Host.objects.filter(
54-
packages_count=host.packages_count
55-
)
56-
# and exclude hosts with the current timestamp
57-
filtered_hosts = filtered_hosts.exclude(updated_at=ts)
58-
59-
package_ids = frozenset(host.packages.values_list('id', flat=True))
60-
repo_ids = frozenset(host.repos.values_list('id', flat=True))
61-
updates = list(host.updates.all())
62-
63-
for fhost in filtered_hosts.iterator():
64-
frepo_ids = frozenset(fhost.repos.values_list('id', flat=True))
65-
if repo_ids != frepo_ids:
66-
continue
67-
fpackage_ids = frozenset(fhost.packages.values_list('id', flat=True))
68-
if package_ids != fpackage_ids:
69-
continue
70-
71-
fhost.updates.set(updates)
72-
fhost.updated_at = ts
73-
fhost.save()
74-
updated_host_ids.add(fhost.id)
75-
info_message(text=f'Added the same updates to {fhost}')
43+
find_host_updates_homogenous(Host.objects.all())

hosts/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from django.db import IntegrityError, transaction
2121
from taggit.models import Tag
2222

23+
from util import get_datetime_now
2324
from util.logging import error_message, info_message
2425

2526

@@ -76,6 +77,51 @@ def get_or_create_host(report, arch, osvariant, domain):
7677
return host
7778

7879

80+
def find_host_updates_homogenous(hosts, verbose=False):
81+
""" Find updates for hosts, copying updates to homogenous hosts.
82+
If a host has the same packages and repos as a previously
83+
processed host, it is given the same updates.
84+
"""
85+
from hosts.models import Host
86+
87+
updated_host_ids = set()
88+
ts = get_datetime_now()
89+
host_iter = hosts.iterator() if hasattr(hosts, 'iterator') else iter(hosts)
90+
for host in host_iter:
91+
if verbose:
92+
info_message(text=str(host))
93+
if host.id not in updated_host_ids:
94+
host.find_updates()
95+
if verbose:
96+
info_message(text='')
97+
host.updated_at = ts
98+
host.save()
99+
100+
filtered_hosts = Host.objects.filter(
101+
packages_count=host.packages_count)
102+
filtered_hosts = filtered_hosts.exclude(updated_at=ts)
103+
104+
package_ids = frozenset(host.packages.values_list('id', flat=True))
105+
repo_ids = frozenset(host.repos.values_list('id', flat=True))
106+
updates = list(host.updates.all())
107+
108+
for fhost in filtered_hosts.iterator():
109+
frepo_ids = frozenset(fhost.repos.values_list('id', flat=True))
110+
if repo_ids != frepo_ids:
111+
continue
112+
fpackage_ids = frozenset(fhost.packages.values_list('id', flat=True))
113+
if package_ids != fpackage_ids:
114+
continue
115+
116+
fhost.updates.set(updates)
117+
fhost.updated_at = ts
118+
fhost.save()
119+
updated_host_ids.add(fhost.id)
120+
info_message(text=f'Added the same updates to {fhost}')
121+
elif verbose:
122+
info_message(text='Updates already added in this run')
123+
124+
79125
def clean_tags():
80126
""" Delete Tags that have no Host
81127
"""

sbin/patchman

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ from errata.utils import (
3636
scan_package_updates_for_affected_packages,
3737
)
3838
from hosts.models import Host
39-
from hosts.utils import clean_tags
39+
from hosts.utils import clean_tags, find_host_updates_homogenous
4040
from modules.utils import clean_modules
4141
from packages.utils import (
4242
clean_packagenames, clean_packages, clean_packageupdates,
@@ -46,7 +46,6 @@ from reports.tasks import remove_reports_with_no_hosts
4646
from repos.models import Repository
4747
from repos.utils import clean_repos
4848
from security.utils import update_cves, update_cwes
49-
from util import get_datetime_now
5049
from util.logging import info_message, set_quiet_mode
5150

5251

@@ -161,52 +160,8 @@ def clean_reports(hoststr=None):
161160
def host_updates_alt(host=None):
162161
""" Find updates for all hosts, specify host for a single host
163162
"""
164-
updated_hosts = []
165163
hosts = get_hosts(host, 'Finding updates')
166-
ts = get_datetime_now()
167-
for host in hosts.iterator():
168-
info_message(text=str(host))
169-
if host not in updated_hosts:
170-
host.find_updates()
171-
info_message(text='')
172-
host.updated_at = ts
173-
host.save()
174-
175-
# only include hosts with the same number of packages
176-
filtered_hosts = Host.objects.filter(
177-
packages_count=host.packages_count)
178-
# exclude hosts with the current timestamp
179-
filtered_hosts = filtered_hosts.exclude(updated_at=ts)
180-
181-
packages = set(host.packages.all())
182-
repos = set(host.repos.all())
183-
updates = host.updates.all()
184-
185-
phosts = []
186-
for fhost in filtered_hosts.iterator():
187-
188-
frepos = set(fhost.repos.all())
189-
rdiff = repos.difference(frepos)
190-
if len(rdiff) != 0:
191-
continue
192-
193-
fpackages = set(fhost.packages.all())
194-
pdiff = packages.difference(fpackages)
195-
if len(pdiff) != 0:
196-
continue
197-
198-
phosts.append(fhost)
199-
200-
for phost in phosts:
201-
phost.updates.set(updates)
202-
phost.updated_at = ts
203-
phost.save()
204-
updated_hosts.append(phost)
205-
text = f'Added the same updates to {phost}'
206-
info_message(text=text)
207-
else:
208-
text = 'Updates already added in this run'
209-
info_message(text=text)
164+
find_host_updates_homogenous(hosts, verbose=True)
210165

211166

212167
def host_updates(host=None):

0 commit comments

Comments
 (0)