Skip to content

Commit

Permalink
host: split cached_vm identification from import_vm
Browse files Browse the repository at this point in the history
This will be useful to the plugin that allows not rerunning a cached
dependency: it needs to probe the cache.

Signed-off-by: Yann Dirson <[email protected]>
  • Loading branch information
ydirson committed May 30, 2024
1 parent 623fd89 commit ce5e37c
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions lib/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,27 @@ def xo_server_reconnect(self):
def vm_cache_key(uri):
return f"[Cache for {uri}]"

def cached_vm(uri, sr_uuid):
assert sr_uuid, "A SR UUID is necessary to use import cache"
cache_key = self.vm_cache_key(uri)
# Look for an existing cache VM
vm_uuids = safe_split(self.xe('vm-list', {'name-description': cache_key}, minimal=True), ',')

for vm_uuid in vm_uuids:
vm = VM(vm_uuid, self)
# Make sure the VM is on the wanted SR.
# Assumption: if the first disk is on the SR, the VM is.
# If there's no VDI at all, then it is virtually on any SR.
if not vm.vdi_uuids() or vm.get_sr().uuid == sr_uuid:
logging.info(f"Reusing cached VM {vm.uuid} for {uri}")
return vm
logging.info("Not found vm in cache with key %r", cache_key)

def import_vm(self, uri, sr_uuid=None, use_cache=False):
if use_cache:
assert sr_uuid, "A SR UUID is necessary to use import cache"
cache_key = self.vm_cache_key(uri)
# Look for an existing cache VM
vm_uuids = safe_split(self.xe('vm-list', {'name-description': cache_key}, minimal=True), ',')

for vm_uuid in vm_uuids:
vm = VM(vm_uuid, self)
# Make sure the VM is on the wanted SR.
# Assumption: if the first disk is on the SR, the VM is.
# If there's no VDI at all, then it is virtually on any SR.
if not vm.vdi_uuids() or vm.get_sr().uuid == sr_uuid:
logging.info(f"Reusing cached VM {vm.uuid} for {uri}")
return vm
logging.info("Not found vm in cache with key %r", cache_key)
vm = cached_vm(uri, sr_uuid)
if vm:
return vm

params = {}
msg = "Import VM %s" % uri
Expand Down

0 comments on commit ce5e37c

Please sign in to comment.