Skip to content

Commit 0cae6ce

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 dffcdac commit 0cae6ce

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

lib/host.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,27 @@ def xo_server_reconnect(self):
200200
# is not enough to guarantee that the host object exists yet.
201201
wait_for(lambda: xo_object_exists(self.uuid), "Wait for XO to know about HOST %s" % self.uuid)
202202

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

220225
params = {}
221226
msg = "Import VM %s" % uri

0 commit comments

Comments
 (0)