Search in sources :

Example 76 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class VmwareStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(CopyCommand cmd) {
    VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    String vmName = srcVolume.getVmName();
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        DataStoreTO primaryStorage = srcVolume.getDataStore();
        NfsTO destStore = (NfsTO) destVolume.getDataStore();
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        Pair<String, String> result;
        result = copyVolumeToSecStorage(hostService, hyperHost, cmd, vmName, primaryStorage.getUuid(), srcVolume.getPath(), destVolume.getPath(), destStore.getUrl(), hostService.getWorkerName(context, cmd, 0));
        VolumeObjectTO newVolume = new VolumeObjectTO();
        newVolume.setPath(result.first() + File.separator + result.second());
        return new CopyCmdAnswer(newVolume);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to execute CopyVolumeCommand due to exception";
        s_logger.error(msg, e);
        return new CopyCmdAnswer("copy volume from primary to secondary failed due to exception: " + VmwareHelper.getExceptionMessage(e));
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 77 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class VmwareStorageProcessor method backupSnapshot.

@Override
public Answer backupSnapshot(CopyCommand cmd) {
    SnapshotObjectTO srcSnapshot = (SnapshotObjectTO) cmd.getSrcTO();
    DataStoreTO primaryStore = srcSnapshot.getDataStore();
    SnapshotObjectTO destSnapshot = (SnapshotObjectTO) cmd.getDestTO();
    DataStoreTO destStore = destSnapshot.getDataStore();
    if (!(destStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO destNfsStore = (NfsTO) destStore;
    String secondaryStorageUrl = destNfsStore.getUrl();
    String snapshotUuid = srcSnapshot.getPath();
    String prevSnapshotUuid = srcSnapshot.getParentSnapshotPath();
    String prevBackupUuid = destSnapshot.getParentSnapshotPath();
    VirtualMachineMO workerVm = null;
    String workerVMName = null;
    String volumePath = srcSnapshot.getVolume().getPath();
    ManagedObjectReference morDs = null;
    DatastoreMO dsMo = null;
    // By default assume failure
    String details = null;
    boolean success = false;
    String snapshotBackupUuid = null;
    boolean hasOwnerVm = false;
    Ternary<String, String, String[]> backupResult = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    VirtualMachineMO vmMo = null;
    String vmName = srcSnapshot.getVmName();
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, primaryStore.getUuid());
        CopyCmdAnswer answer = null;
        try {
            if (vmName != null) {
                vmMo = hyperHost.findVmOnHyperHost(vmName);
                if (vmMo == null) {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("Unable to find owner VM for BackupSnapshotCommand on host " + hyperHost.getHyperHostName() + ", will try within datacenter");
                    }
                    vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
                }
            }
            if (vmMo == null) {
                dsMo = new DatastoreMO(hyperHost.getContext(), morDs);
                workerVMName = hostService.getWorkerName(context, cmd, 0);
                vmMo = HypervisorHostHelper.createWorkerVM(hyperHost, dsMo, workerVMName);
                if (vmMo == null) {
                    throw new Exception("Failed to find the newly create or relocated VM. vmName: " + workerVMName);
                }
                workerVm = vmMo;
                // attach volume to worker VM
                String datastoreVolumePath = dsMo.getDatastorePath(volumePath + ".vmdk");
                vmMo.attachDisk(new String[] { datastoreVolumePath }, morDs);
            } else {
                s_logger.info("Using owner VM " + vmName + " for snapshot operation");
                hasOwnerVm = true;
            }
            if (!vmMo.createSnapshot(snapshotUuid, "Snapshot taken for " + srcSnapshot.getName(), false, false)) {
                throw new Exception("Failed to take snapshot " + srcSnapshot.getName() + " on vm: " + vmName);
            }
            backupResult = backupSnapshotToSecondaryStorage(vmMo, destSnapshot.getPath(), srcSnapshot.getVolume().getPath(), snapshotUuid, secondaryStorageUrl, prevSnapshotUuid, prevBackupUuid, hostService.getWorkerName(context, cmd, 1), _nfsVersion);
            snapshotBackupUuid = backupResult.first();
            success = (snapshotBackupUuid != null);
            if (!success) {
                details = "Failed to backUp the snapshot with uuid: " + snapshotUuid + " to secondary storage.";
                answer = new CopyCmdAnswer(details);
            } else {
                details = "Successfully backedUp the snapshot with Uuid: " + snapshotUuid + " to secondary storage.";
                // Get snapshot physical size
                long physicalSize = 0l;
                String secondaryMountPoint = mountService.getMountPoint(secondaryStorageUrl, _nfsVersion);
                String snapshotDir = destSnapshot.getPath() + "/" + snapshotBackupUuid;
                File[] files = new File(secondaryMountPoint + "/" + snapshotDir).listFiles();
                if (files != null) {
                    for (File file : files) {
                        String fileName = file.getName();
                        if (fileName.toLowerCase().startsWith(snapshotBackupUuid) && fileName.toLowerCase().endsWith(".vmdk")) {
                            physicalSize = new File(secondaryMountPoint + "/" + snapshotDir + "/" + fileName).length();
                            break;
                        }
                    }
                }
                SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
                newSnapshot.setPath(snapshotDir + "/" + snapshotBackupUuid);
                newSnapshot.setPhysicalSize(physicalSize);
                answer = new CopyCmdAnswer(newSnapshot);
            }
        } finally {
            if (vmMo != null) {
                ManagedObjectReference snapshotMor = vmMo.getSnapshotMor(snapshotUuid);
                if (snapshotMor != null) {
                    vmMo.removeSnapshot(snapshotUuid, false);
                    // in the middle
                    if (backupResult != null && hasOwnerVm) {
                        s_logger.info("Check if we have disk consolidation after snapshot operation");
                        boolean chainConsolidated = false;
                        for (String vmdkDsFilePath : backupResult.third()) {
                            s_logger.info("Validate disk chain file:" + vmdkDsFilePath);
                            if (vmMo.getDiskDevice(vmdkDsFilePath) == null) {
                                s_logger.info("" + vmdkDsFilePath + " no longer exists, consolidation detected");
                                chainConsolidated = true;
                                break;
                            } else {
                                s_logger.info("" + vmdkDsFilePath + " is found still in chain");
                            }
                        }
                        if (chainConsolidated) {
                            String topVmdkFilePath = null;
                            try {
                                topVmdkFilePath = vmMo.getDiskCurrentTopBackingFileInChain(backupResult.second());
                            } catch (Exception e) {
                                s_logger.error("Unexpected exception", e);
                            }
                            s_logger.info("Disk has been consolidated, top VMDK is now: " + topVmdkFilePath);
                            if (topVmdkFilePath != null) {
                                DatastoreFile file = new DatastoreFile(topVmdkFilePath);
                                SnapshotObjectTO snapshotInfo = (SnapshotObjectTO) answer.getNewData();
                                VolumeObjectTO vol = new VolumeObjectTO();
                                vol.setUuid(srcSnapshot.getVolume().getUuid());
                                vol.setPath(file.getFileBaseName());
                                snapshotInfo.setVolume(vol);
                            } else {
                                s_logger.error("Disk has been consolidated, but top VMDK is not found ?!");
                            }
                        }
                    }
                } else {
                    s_logger.error("Can not find the snapshot we just used ?!");
                }
            }
            try {
                if (workerVm != null) {
                    // detach volume and destroy worker vm
                    workerVm.detachAllDisks();
                    workerVm.destroy();
                }
            } catch (Throwable e) {
                s_logger.warn("Failed to destroy worker VM: " + workerVMName);
            }
        }
        return answer;
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "backup snapshot exception: " + VmwareHelper.getExceptionMessage(e);
        return new CopyCmdAnswer(details);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) NfsTO(com.cloud.agent.api.to.NfsTO) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) RemoteException(java.rmi.RemoteException) DatastoreFile(com.cloud.hypervisor.vmware.mo.DatastoreFile) File(java.io.File) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 78 with CopyCmdAnswer

use of org.apache.cloudstack.storage.command.CopyCmdAnswer in project cloudstack by apache.

the class LocalNfsSecondaryStorageResourceTest method testExecuteRequest.

@Test
public void testExecuteRequest() throws Exception {
    TemplateObjectTO template = Mockito.mock(TemplateObjectTO.class);
    NfsTO cacheStore = Mockito.mock(NfsTO.class);
    Mockito.when(cacheStore.getUrl()).thenReturn("nfs://nfs2.lab.vmops.com/export/home/edison/");
    SwiftTO swift = Mockito.mock(SwiftTO.class);
    Mockito.when(swift.getEndPoint()).thenReturn("https://objects.dreamhost.com/auth");
    Mockito.when(swift.getAccount()).thenReturn("cloudstack");
    Mockito.when(swift.getUserName()).thenReturn("images");
    Mockito.when(swift.getKey()).thenReturn("oxvELQaOD1U5_VyosGfA-wpZ7uBWEff-CUBGCM0u");
    Mockito.when(template.getDataStore()).thenReturn(swift);
    Mockito.when(template.getPath()).thenReturn("template/1/1/");
    Mockito.when(template.isRequiresHvm()).thenReturn(true);
    Mockito.when(template.getId()).thenReturn(1L);
    Mockito.when(template.getFormat()).thenReturn(Storage.ImageFormat.VHD);
    Mockito.when(template.getOrigUrl()).thenReturn("http://nfs1.lab.vmops.com/templates/test.bz2");
    Mockito.when(template.getName()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(template.getObjectType()).thenReturn(DataObjectType.TEMPLATE);
    DownloadCommand cmd = new DownloadCommand(template, 100000L);
    cmd.setCacheStore(cacheStore);
    DownloadAnswer answer = (DownloadAnswer) resource.executeRequest(cmd);
    Assert.assertTrue(answer.getResult());
    Mockito.when(template.getPath()).thenReturn(answer.getInstallPath());
    Mockito.when(template.getDataStore()).thenReturn(swift);
    //download swift:
    Mockito.when(cacheStore.getRole()).thenReturn(DataStoreRole.ImageCache);
    TemplateObjectTO destTemplate = Mockito.mock(TemplateObjectTO.class);
    Mockito.when(destTemplate.getPath()).thenReturn("template/1/2");
    Mockito.when(destTemplate.getDataStore()).thenReturn(cacheStore);
    Mockito.when(destTemplate.getObjectType()).thenReturn(DataObjectType.TEMPLATE);
    CopyCommand cpyCmd = new CopyCommand(template, destTemplate, 10000, true);
    CopyCmdAnswer copyCmdAnswer = (CopyCmdAnswer) resource.executeRequest(cpyCmd);
    Assert.assertTrue(copyCmdAnswer.getResult());
    //list template
    ListTemplateCommand listCmd = new ListTemplateCommand(swift);
    ListTemplateAnswer listAnswer = (ListTemplateAnswer) resource.executeRequest(listCmd);
    Assert.assertTrue(listAnswer.getTemplateInfo().size() > 0);
}
Also used : SwiftTO(com.cloud.agent.api.to.SwiftTO) DownloadCommand(org.apache.cloudstack.storage.command.DownloadCommand) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) ListTemplateAnswer(com.cloud.agent.api.storage.ListTemplateAnswer) ListTemplateCommand(com.cloud.agent.api.storage.ListTemplateCommand) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DownloadAnswer(com.cloud.agent.api.storage.DownloadAnswer) NfsTO(com.cloud.agent.api.to.NfsTO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Test(org.junit.Test)

Aggregations

CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)78 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)49 NfsTO (com.cloud.agent.api.to.NfsTO)40 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)38 DataTO (com.cloud.agent.api.to.DataTO)37 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)37 TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)34 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)30 InternalErrorException (com.cloud.exception.InternalErrorException)27 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)25 Connection (com.xensource.xenapi.Connection)18 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 XmlRpcException (org.apache.xmlrpc.XmlRpcException)18 SR (com.xensource.xenapi.SR)17 VDI (com.xensource.xenapi.VDI)17 URI (java.net.URI)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)10 ConfigurationException (javax.naming.ConfigurationException)10 IOException (java.io.IOException)9 CopyCommand (org.apache.cloudstack.storage.command.CopyCommand)9