@@ -200,22 +200,31 @@ 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
+ @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
+
203
223
def import_vm (self , uri , sr_uuid = None , use_cache = False ):
204
224
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
219
228
220
229
params = {}
221
230
msg = "Import VM %s" % uri
@@ -235,6 +244,7 @@ def import_vm(self, uri, sr_uuid=None, use_cache=False):
235
244
for vif in vm .vifs ():
236
245
vif .move (self .management_network ())
237
246
if use_cache :
247
+ cache_key = self .vm_cache_key (uri )
238
248
logging .info (f"Marking VM { vm .uuid } as cached" )
239
249
vm .param_set ('name-description' , cache_key )
240
250
return vm
0 commit comments