You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2020/01/08 08:09:11 UTC

[GitHub] [cloudstack] anuragaw commented on a change in pull request #3350: Get Diagnostics: Download logs and diagnostics data from SSVM, CPVM, Router

anuragaw commented on a change in pull request #3350: Get Diagnostics: Download logs and diagnostics data from SSVM, CPVM, Router
URL: https://github.com/apache/cloudstack/pull/3350#discussion_r364104541
 
 

 ##########
 File path: server/src/main/java/org/apache/cloudstack/diagnostics/DiagnosticsServiceImpl.java
 ##########
 @@ -126,10 +185,336 @@ protected String prepareShellCmd(String cmdType, String ipAddress, String option
         }
     }
 
+    private String zipFilesInSystemVm(VMInstanceVO vmInstance, List<String> optionalFilesList) {
+        List<String> fileList = getFileListToBeRetrieved(optionalFilesList, vmInstance);
+
+        if (CollectionUtils.isEmpty(fileList)) {
+            throw new CloudRuntimeException("Failed to generate diagnostics file list for retrieval.");
+        }
+
+        final Answer zipFilesAnswer = prepareDiagnosticsFilesInSystemVm(vmInstance, fileList);
+
+        if (zipFilesAnswer == null) {
+            throw new CloudRuntimeException(String.format("Failed to generate diagnostics zip file in the system VM %s", vmInstance.getUuid()));
+        }
+
+        if (!zipFilesAnswer.getResult()) {
+            throw new CloudRuntimeException(String.format("Failed to generate diagnostics zip file in VM %s due to: %s", vmInstance.getUuid(), zipFilesAnswer.getDetails()));
+        }
+
+        return zipFilesAnswer.getDetails().replace("\n", "");
+    }
+
+    @Override
+    @ActionEvent(eventType = EventTypes.EVENT_SYSTEM_VM_DIAGNOSTICS, eventDescription = "getting diagnostics files on system vm", async = true)
+    public String getDiagnosticsDataCommand(GetDiagnosticsDataCmd cmd) {
+        final Long vmId = cmd.getId();
+        final List<String> optionalFilesList = cmd.getFilesList();
+        final VMInstanceVO vmInstance = getSystemVMInstance(vmId);
+        final DataStore store = getImageStore(vmInstance.getDataCenterId());
+
+        final String zipFileInSystemVm = zipFilesInSystemVm(vmInstance, optionalFilesList);
+        final Long vmHostId = vmInstance.getHostId();
+        copyZipFileToSecondaryStorage(vmInstance, vmHostId, zipFileInSystemVm, store);
+        deleteDiagnosticsZipFileInsystemVm(vmInstance, zipFileInSystemVm);
+
+        // Now we need to create the file download URL
+        // Find ssvm of store
+        final long zoneId = vmInstance.getDataCenterId();
+        VMInstanceVO ssvm = getSecondaryStorageVmInZone(zoneId);
+        if (ssvm == null) {
+            throw new CloudRuntimeException("No SSVM found in zone with ID: " + zoneId);
+        }
+
+        // Secondary Storage install path = "diagnostics_data/diagnostics_files_xxxx.tar
+        String installPath = DIAGNOSTICS_DIRECTORY + File.separator + zipFileInSystemVm.replace("/root", "");
+        return createFileDownloadUrl(store, ssvm.getHypervisorType(), installPath);
+    }
+
+    /**
+     * Copy retrieved diagnostics zip file from system vm to secondary storage
+     * For VMware use the mgmt server, and for Xen/KVM use the hyperhost of the target VM
+     * The strategy is to mount secondary storage on mgmt server or host and scp directly to /mnt/SecStorage/diagnostics_data
+     *
+     * @param fileToCopy zip file in system vm to be copied
+     * @param store      secondary storage to copy zip file to
+     */
+    private Pair<Boolean, String> copyZipFileToSecondaryStorage(VMInstanceVO vmInstance, Long vmHostId, String fileToCopy, DataStore store) {
+        String vmControlIp = getVMSshIp(vmInstance);
+        if (StringUtils.isBlank(vmControlIp)) {
+            return new Pair<>(false, "Unable to find system vm ssh/control IP for  vm with ID: " + vmInstance.getId());
+        }
+        Pair<Boolean, String> copyResult;
+        if (vmInstance.getHypervisorType() == Hypervisor.HypervisorType.VMware) {
+            copyResult = orchestrateCopyToSecondaryStorageVMware(store, vmControlIp, fileToCopy);
+        } else {
+            copyResult = orchestrateCopyToSecondaryStorageNonVMware(store, vmControlIp, fileToCopy, vmHostId);
+        }
+
+        if (!copyResult.first()) {
+            throw new CloudRuntimeException(String.format("Failed to copy %s to secondary storage %s due to: %s.", fileToCopy, store.getUri(), copyResult.second()));
+        }
+
+        return copyResult;
+    }
+
+    private void configureNetworkElementCommand(NetworkElementCommand cmd, VMInstanceVO vmInstance) {
+        Map<String, String> accessDetails = networkManager.getSystemVMAccessDetails(vmInstance);
+        if (StringUtils.isBlank(accessDetails.get(NetworkElementCommand.ROUTER_IP))) {
+            throw new CloudRuntimeException("Unable to set system vm ControlIP for system vm with ID: " + vmInstance.getId());
+        }
+        cmd.setAccessDetail(accessDetails);
+    }
+
+    private Answer prepareDiagnosticsFilesInSystemVm(VMInstanceVO vmInstance, List<String> fileList) {
+        final PrepareFilesCommand cmd = new PrepareFilesCommand(fileList, DataRetrievalTimeout.value());
+        configureNetworkElementCommand(cmd, vmInstance);
+        Answer answer = agentManager.easySend(vmInstance.getHostId(), cmd);
+        return answer;
+    }
+
+    private Answer deleteDiagnosticsZipFileInsystemVm(VMInstanceVO vmInstance, String zipFileName) {
+        final DeleteFileInVrCommand cmd = new DeleteFileInVrCommand(zipFileName);
+        configureNetworkElementCommand(cmd, vmInstance);
+        final Answer fileCleanupAnswer = agentManager.easySend(vmInstance.getHostId(), cmd);
+        if (fileCleanupAnswer == null) {
+            LOGGER.error(String.format("Failed to cleanup diagnostics zip file on vm: %s", vmInstance.getUuid()));
+        } else {
+            if (!fileCleanupAnswer.getResult()) {
+                LOGGER.error(String.format("Zip file cleanup for vm %s has failed with: %s", vmInstance.getUuid(), fileCleanupAnswer.getDetails()));
+            }
+        }
+
+        return fileCleanupAnswer;
+    }
+
+    /**
+     * Generate a list of diagnostics file to be retrieved depending on the system VM type
+     *
+     * @param optionalFileList Optional list of files that user may want to retrieve, empty by default
+     * @param vmInstance       system VM instance, either SSVM, CPVM or VR
+     * @return a list of files to be retrieved for system VM, either generated from defaults depending on the VM type, or specified
+     * by the optional list param
+     */
+    private List<String> getFileListToBeRetrieved(List<String> optionalFileList, VMInstanceVO vmInstance) {
+        DiagnosticsFilesList fileListObject = DiagnosticsFilesListFactory.getDiagnosticsFilesList(optionalFileList, vmInstance);
+        List<String> fileList = new ArrayList<>();
+
+        if (fileListObject != null) {
+            fileList = fileListObject.generateFileList();
+        }
+        return fileList;
+    }
+
+    private Pair<Boolean, String> orchestrateCopyToSecondaryStorageNonVMware(final DataStore store, final String vmControlIp, String fileToCopy, Long vmHostId) {
+        CopyToSecondaryStorageCommand toSecondaryStorageCommand = new CopyToSecondaryStorageCommand(store.getUri(), vmControlIp, fileToCopy);
+        Answer copyToSecondaryAnswer = agentManager.easySend(vmHostId, toSecondaryStorageCommand);
+        Pair<Boolean, String> copyAnswer;
+        if (copyToSecondaryAnswer != null) {
+            copyAnswer = new Pair<>(copyToSecondaryAnswer.getResult(), copyToSecondaryAnswer.getDetails());
+        } else {
+            copyAnswer = new Pair<>(false, "Diagnostics Zip file to secondary storage failed");
+        }
+        return copyAnswer;
+    }
 
 Review comment:
   Changing.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services