@@ -200,22 +200,27 @@ def xo_server_reconnect(self):
200
200
# is not enough to guarantee that the host object exists yet.
201
201
wait_for (lambda : xo_object_exists (self .uuid ), "Wait for XO to know about HOST %s" % self .uuid )
202
202
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
+
203
219
def import_vm (self , uri , sr_uuid = None , use_cache = False ):
204
220
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
219
224
220
225
params = {}
221
226
msg = "Import VM %s" % uri
0 commit comments