Skip to content

Commit ce5e37c

Browse files
committed
host: split cached_vm identification from import_vm
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]>
1 parent 623fd89 commit ce5e37c

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/host.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -204,22 +204,27 @@ def xo_server_reconnect(self):
204204
def vm_cache_key(uri):
205205
return f"[Cache for {uri}]"
206206

207+
def cached_vm(uri, sr_uuid):
208+
assert sr_uuid, "A SR UUID is necessary to use import cache"
209+
cache_key = self.vm_cache_key(uri)
210+
# Look for an existing cache VM
211+
vm_uuids = safe_split(self.xe('vm-list', {'name-description': cache_key}, minimal=True), ',')
212+
213+
for vm_uuid in vm_uuids:
214+
vm = VM(vm_uuid, self)
215+
# Make sure the VM is on the wanted SR.
216+
# Assumption: if the first disk is on the SR, the VM is.
217+
# If there's no VDI at all, then it is virtually on any SR.
218+
if not vm.vdi_uuids() or vm.get_sr().uuid == sr_uuid:
219+
logging.info(f"Reusing cached VM {vm.uuid} for {uri}")
220+
return vm
221+
logging.info("Not found vm in cache with key %r", cache_key)
222+
207223
def import_vm(self, uri, sr_uuid=None, use_cache=False):
208224
if use_cache:
209-
assert sr_uuid, "A SR UUID is necessary to use import cache"
210-
cache_key = self.vm_cache_key(uri)
211-
# Look for an existing cache VM
212-
vm_uuids = safe_split(self.xe('vm-list', {'name-description': cache_key}, minimal=True), ',')
213-
214-
for vm_uuid in vm_uuids:
215-
vm = VM(vm_uuid, self)
216-
# Make sure the VM is on the wanted SR.
217-
# Assumption: if the first disk is on the SR, the VM is.
218-
# If there's no VDI at all, then it is virtually on any SR.
219-
if not vm.vdi_uuids() or vm.get_sr().uuid == sr_uuid:
220-
logging.info(f"Reusing cached VM {vm.uuid} for {uri}")
221-
return vm
222-
logging.info("Not found vm in cache with key %r", cache_key)
225+
vm = cached_vm(uri, sr_uuid)
226+
if vm:
227+
return vm
223228

224229
params = {}
225230
msg = "Import VM %s" % uri

0 commit comments

Comments
 (0)