Skip to content

Commit d3e1fc0

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 d3e1fc0

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

lib/host.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,22 +200,31 @@ 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+
@staticmethod
204+
def vm_cache_key(uri):
205+
return f"[Cache for {strip_suffix(uri, '.xva')}]"
206+
207+
def cached_vm(self, 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+
203223
def import_vm(self, uri, sr_uuid=None, use_cache=False):
204224
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)
225+
vm = self.cached_vm(uri, sr_uuid)
226+
if vm:
227+
return vm
219228

220229
params = {}
221230
msg = "Import VM %s" % uri
@@ -235,6 +244,7 @@ def import_vm(self, uri, sr_uuid=None, use_cache=False):
235244
for vif in vm.vifs():
236245
vif.move(self.management_network())
237246
if use_cache:
247+
cache_key = self.vm_cache_key(uri)
238248
logging.info(f"Marking VM {vm.uuid} as cached")
239249
vm.param_set('name-description', cache_key)
240250
return vm

0 commit comments

Comments
 (0)