abh1sar commented on code in PR #9208:
URL: https://github.com/apache/cloudstack/pull/9208#discussion_r1713174995


##########
plugins/storage/fileshare/storagefsvm/src/main/java/org/apache/cloudstack/storage/fileshare/lifecycle/StorageFsVmFileShareLifeCycle.java:
##########
@@ -171,15 +171,56 @@ private UserVm createFileShareVM(Long zoneId, Account 
owner, List<Long> networkI
         }
         List<String> keypairs = new ArrayList<String>();
 
-        UserVm vm;
-        String fsVmConfig = 
getStorageFsVmConfig(fileSystem.toString().toLowerCase(), 
availableHypervisor.toString().toLowerCase());
-        String base64UserData = 
Base64.encodeBase64String(fsVmConfig.getBytes(com.cloud.utils.StringUtils.getPreferredCharset()));
-        vm = userVmService.createAdvancedVirtualMachine(zone, serviceOffering, 
template, networkIds, owner, hostName, hostName,
-                diskOfferingId, size, null, Hypervisor.HypervisorType.None, 
BaseCmd.HTTPMethod.POST, base64UserData,
-                null, null, keypairs, null, addrs, null, null, null,
-                customParameterMap, null, null, null, null,
-                true, UserVmManager.STORAGEFSVM, null);
-        return vm;
+        for (final Iterator<Hypervisor.HypervisorType> iter = 
hypervisors.iterator(); iter.hasNext();) {
+            final Hypervisor.HypervisorType hypervisor = iter.next();
+            VMTemplateVO template = 
templateDao.findSystemVMReadyTemplate(zoneId, hypervisor);
+            if (template == null && !iter.hasNext()) {
+                throw new CloudRuntimeException(String.format("Unable to find 
the systemvm template for %s or it was not downloaded in %s.", 
hypervisor.toString(), zone.toString()));
+            }
+
+            LaunchPermissionVO existingPermission = 
launchPermissionDao.findByTemplateAndAccount(template.getId(), owner.getId());
+            if (existingPermission == null) {
+                LaunchPermissionVO launchPermission = new 
LaunchPermissionVO(template.getId(), owner.getId());
+                launchPermissionDao.persist(launchPermission);
+            }
+
+            UserVm vm;
+            String fsVmConfig = 
getStorageFsVmConfig(fileSystem.toString().toLowerCase(), 
hypervisor.toString().toLowerCase());
+            String base64UserData = 
Base64.encodeBase64String(fsVmConfig.getBytes(com.cloud.utils.StringUtils.getPreferredCharset()));
+            CallContext vmContext = 
CallContext.register(CallContext.current(), 
ApiCommandResourceType.VirtualMachine);
+            try {
+                vm = userVmService.createAdvancedVirtualMachine(zone, 
serviceOffering, template, networkIds, owner, hostName, hostName,
+                        diskOfferingId, size, null, 
Hypervisor.HypervisorType.None, BaseCmd.HTTPMethod.POST, base64UserData,
+                        null, null, keypairs, null, addrs, null, null, null,
+                        customParameterMap, null, null, null, null,
+                        true, UserVmManager.STORAGEFSVM, null);
+            } catch (InsufficientCapacityException ex) {
+                if (iter.hasNext()) {
+                    continue;
+                } else {
+                    throw ex;
+                }
+            } finally {
+                CallContext.unregister();
+            }
+
+            vmContext = CallContext.register(CallContext.current(), 
ApiCommandResourceType.VirtualMachine);
+            vmContext.setEventResourceId(vm.getId());
+            try {
+                userVmService.startVirtualMachine(vm);
+            } catch (InsufficientCapacityException ex) {
+                expungeVm(vm.getId());
+                if (iter.hasNext()) {
+                    continue;
+                } else {
+                    throw ex;
+                }
+            } finally {
+                CallContext.unregister();
+            }

Review Comment:
   yes, this looks much better.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to