Skip to content

Commit

Permalink
Fix regression in VM selection.
Browse files Browse the repository at this point in the history
kevoreilly#1020 introduced a regression
in selection of VM's for x86 tasks where it would no longer allow
selection of an x64 VM. This reintroduces that ability so that x86 tasks
will select x86 OR x64 machines, preferring x86 over x64.
  • Loading branch information
Tommy Beadle committed Aug 1, 2022
1 parent 663ab7d commit 346a3ab
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/cuckoo/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,21 @@ def guest_stop(self, guest_id):
finally:
session.close()

@staticmethod
def filter_machines_by_arch(machines, arch):
""" Add a filter to the given query for the architecture of the machines.
Allow x64 machines to be returned when requesting x86.
"""
if arch:
if arch == "x86":
# Prefer x86 machines over x64 if x86 is what was requested.
machines = machines.filter(Machine.arch.in_(("x64", "x86"))).order_by(
Machine.arch.desc()
)
else:
machines = machines.filter_by(arch=arch)
return machines

@classlock
def list_machines(self, locked=None, label=None, platform=None, tags=[], arch=None):
"""Lists virtual machines.
Expand All @@ -961,8 +976,7 @@ def list_machines(self, locked=None, label=None, platform=None, tags=[], arch=No
machines = machines.filter_by(label=label)
if platform:
machines = machines.filter_by(platform=platform)
if arch:
machines = machines.filter_by(arch=arch)
machines = self.filter_machines_by_arch(machines, arch)
if tags:
for tag in tags:
machines = machines.filter(Machine.tags.any(name=tag))
Expand Down Expand Up @@ -1002,8 +1016,7 @@ def lock_machine(self, label=None, platform=None, tags=None, arch=None):
machines = machines.filter_by(label=label)
if platform:
machines = machines.filter_by(platform=platform)
if arch:
machines = machines.filter_by(arch=arch)
machines = self.filter_machines_by_arch(machines, arch)
if tags:
for tag in tags:
machines = machines.filter(Machine.tags.any(name=tag))
Expand Down

0 comments on commit 346a3ab

Please sign in to comment.