Search in sources :

Example 1 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class VolumeServiceImpl method managedCopyBaseImageCallback.

protected Void managedCopyBaseImageCallback(AsyncCallbackDispatcher<VolumeServiceImpl, CopyCommandResult> callback, ManagedCreateBaseImageContext<VolumeApiResult> context) {
    CopyCommandResult result = callback.getResult();
    VolumeInfo volumeInfo = context.getVolumeInfo();
    VolumeApiResult res = new VolumeApiResult(volumeInfo);
    if (result.isSuccess()) {
        // volumeInfo.processEvent(Event.OperationSuccessed, result.getAnswer());
        VolumeVO volume = volDao.findById(volumeInfo.getId());
        CopyCmdAnswer answer = (CopyCmdAnswer) result.getAnswer();
        TemplateObjectTO templateObjectTo = (TemplateObjectTO) answer.getNewData();
        volume.setPath(templateObjectTo.getPath());
        if (templateObjectTo.getFormat() != null) {
            volume.setFormat(templateObjectTo.getFormat());
        }
        volDao.update(volume.getId(), volume);
    } else {
        volumeInfo.processEvent(Event.DestroyRequested);
        res.setResult(result.getResult());
    }
    AsyncCallFuture<VolumeApiResult> future = context.getFuture();
    future.complete(res);
    return null;
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) CopyCommandResult(org.apache.cloudstack.engine.subsystem.api.storage.CopyCommandResult) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Example 2 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class OvmResourceBase method createVbds.

protected void createVbds(OvmVm.Details vm, VirtualMachineTO spec) throws URISyntaxException {
    for (DiskTO volume : spec.getDisks()) {
        if (volume.getType() == Volume.Type.ROOT) {
            VolumeObjectTO vol = (VolumeObjectTO) volume.getData();
            OvmDisk.Details root = new OvmDisk.Details();
            root.path = vol.getPath();
            root.type = OvmDisk.WRITE;
            root.isIso = false;
            vm.rootDisk = root;
        } else if (volume.getType() == Volume.Type.ISO) {
            DataTO isoTO = volume.getData();
            if (isoTO.getPath() != null) {
                TemplateObjectTO template = (TemplateObjectTO) isoTO;
                DataStoreTO store = template.getDataStore();
                if (!(store instanceof NfsTO)) {
                    throw new CloudRuntimeException("unsupported protocol");
                }
                NfsTO nfsStore = (NfsTO) store;
                String isoPath = nfsStore.getUrl() + File.separator + template.getPath();
                OvmDisk.Details iso = new OvmDisk.Details();
                URI path = new URI(isoPath);
                iso.path = path.getHost() + ":" + path.getPath();
                iso.type = OvmDisk.READ;
                iso.isIso = true;
                vm.disks.add(iso);
            }
        } else if (volume.getType() == Volume.Type.DATADISK) {
            OvmDisk.Details data = new OvmDisk.Details();
            data.path = volume.getData().getPath();
            data.type = OvmDisk.SHAREDWRITE;
            data.isIso = false;
            vm.disks.add(data);
        } else {
            throw new CloudRuntimeException("Unknow volume type: " + volume.getType());
        }
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) OvmDisk(com.cloud.ovm.object.OvmDisk) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) DiskTO(com.cloud.agent.api.to.DiskTO)

Example 3 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class VmwareStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(CopyCommand cmd) {
    VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    DataStoreTO imageStore = template.getDataStore();
    if (!(imageStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) imageStore;
    String secondaryStoragePoolURL = nfsImageStore.getUrl();
    String volumePath = volume.getPath();
    String details = null;
    VmwareContext context = hostService.getServiceContext(cmd);
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(volume.getVmName());
        if (vmMo == null) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Unable to find the owner VM for CreatePrivateTemplateFromVolumeCommand on host " + hyperHost.getHyperHostName() + ", try within datacenter");
            }
            vmMo = hyperHost.findVmOnPeerHyperHost(volume.getVmName());
            if (vmMo == null) {
                // This means either the volume is on a zone wide storage pool or VM is deleted by external entity.
                // Look for the VM in the datacenter.
                ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
                DatacenterMO dcMo = new DatacenterMO(context, dcMor);
                vmMo = dcMo.findVm(volume.getVmName());
            }
            if (vmMo == null) {
                String msg = "Unable to find the owner VM for volume operation. vm: " + volume.getVmName();
                s_logger.error(msg);
                throw new Exception(msg);
            }
        }
        Ternary<String, Long, Long> result = createTemplateFromVolume(vmMo, template.getPath(), template.getId(), template.getName(), secondaryStoragePoolURL, volumePath, hostService.getWorkerName(context, cmd, 0), _nfsVersion);
        TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(result.first());
        newTemplate.setFormat(ImageFormat.OVA);
        newTemplate.setSize(result.third());
        newTemplate.setPhysicalSize(result.second());
        return new CopyCmdAnswer(newTemplate);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        s_logger.error("Unexpecpted exception ", e);
        details = "create template from volume exception: " + VmwareHelper.getExceptionMessage(e);
        return new CopyCmdAnswer(details);
    }
}
Also used : 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) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO)

Example 4 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class VmwareStorageProcessor method copyTemplateToPrimaryStorage.

@Override
public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    TemplateObjectTO template = (TemplateObjectTO) srcData;
    DataStoreTO srcStore = srcData.getDataStore();
    if (!(srcStore instanceof NfsTO)) {
        return new CopyCmdAnswer("unsupported protocol");
    }
    NfsTO nfsImageStore = (NfsTO) srcStore;
    DataTO destData = cmd.getDestTO();
    DataStoreTO destStore = destData.getDataStore();
    DataStoreTO primaryStore = destStore;
    String secondaryStorageUrl = nfsImageStore.getUrl();
    assert (secondaryStorageUrl != null);
    boolean managed = false;
    String storageHost = null;
    int storagePort = Integer.MIN_VALUE;
    String managedStoragePoolName = null;
    String managedStoragePoolRootVolumeName = null;
    String chapInitiatorUsername = null;
    String chapInitiatorSecret = null;
    String chapTargetUsername = null;
    String chapTargetSecret = null;
    if (destStore instanceof PrimaryDataStoreTO) {
        PrimaryDataStoreTO destPrimaryDataStoreTo = (PrimaryDataStoreTO) destStore;
        Map<String, String> details = destPrimaryDataStoreTo.getDetails();
        if (details != null) {
            managed = Boolean.parseBoolean(details.get(PrimaryDataStoreTO.MANAGED));
            if (managed) {
                storageHost = details.get(PrimaryDataStoreTO.STORAGE_HOST);
                try {
                    storagePort = Integer.parseInt(details.get(PrimaryDataStoreTO.STORAGE_PORT));
                } catch (Exception ex) {
                    storagePort = 3260;
                }
                managedStoragePoolName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET);
                managedStoragePoolRootVolumeName = details.get(PrimaryDataStoreTO.MANAGED_STORE_TARGET_ROOT_VOLUME);
                chapInitiatorUsername = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_USERNAME);
                chapInitiatorSecret = details.get(PrimaryDataStoreTO.CHAP_INITIATOR_SECRET);
                chapTargetUsername = details.get(PrimaryDataStoreTO.CHAP_TARGET_USERNAME);
                chapTargetSecret = details.get(PrimaryDataStoreTO.CHAP_TARGET_SECRET);
            }
        }
    }
    String templateUrl = secondaryStorageUrl + "/" + srcData.getPath();
    Pair<String, String> templateInfo = VmwareStorageLayoutHelper.decodeTemplateRelativePathAndNameFromUrl(secondaryStorageUrl, templateUrl, template.getName());
    VmwareContext context = hostService.getServiceContext(cmd);
    if (context == null) {
        return new CopyCmdAnswer("Failed to create a Vmware context, check the management server logs or the ssvm log for details");
    }
    try {
        VmwareHypervisorHost hyperHost = hostService.getHyperHost(context, cmd);
        String storageUuid = managed ? managedStoragePoolName : primaryStore.getUuid();
        String templateUuidName = deriveTemplateUuidOnHost(hyperHost, storageUuid, templateInfo.second());
        DatacenterMO dcMo = new DatacenterMO(context, hyperHost.getHyperHostDatacenter());
        VirtualMachineMO templateMo = VmwareHelper.pickOneVmOnRunningHost(dcMo.findVmByNameAndLabel(templateUuidName), true);
        DatastoreMO dsMo = null;
        Pair<VirtualMachineMO, Long> vmInfo = null;
        if (templateMo == null) {
            if (s_logger.isInfoEnabled()) {
                s_logger.info("Template " + templateInfo.second() + " is not setup yet. Set up template from secondary storage with uuid name: " + templateUuidName);
            }
            final ManagedObjectReference morDs;
            if (managed) {
                morDs = prepareManagedDatastore(context, hyperHost, null, managedStoragePoolName, storageHost, storagePort, chapInitiatorUsername, chapInitiatorSecret, chapTargetUsername, chapTargetSecret);
            } else {
                morDs = HypervisorHostHelper.findDatastoreWithBackwardsCompatibility(hyperHost, storageUuid);
            }
            assert (morDs != null);
            dsMo = new DatastoreMO(context, morDs);
            if (managed) {
                vmInfo = copyTemplateFromSecondaryToPrimary(hyperHost, dsMo, secondaryStorageUrl, templateInfo.first(), templateInfo.second(), managedStoragePoolRootVolumeName, false, _nfsVersion);
                VirtualMachineMO vmMo = vmInfo.first();
                vmMo.unregisterVm();
                String[] vmwareLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, managedStoragePoolRootVolumeName, managedStoragePoolRootVolumeName, VmwareStorageLayoutType.VMWARE, false);
                String[] legacyCloudStackLayoutFilePair = VmwareStorageLayoutHelper.getVmdkFilePairDatastorePath(dsMo, null, managedStoragePoolRootVolumeName, VmwareStorageLayoutType.CLOUDSTACK_LEGACY, false);
                dsMo.moveDatastoreFile(vmwareLayoutFilePair[0], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[0], dcMo.getMor(), true);
                dsMo.moveDatastoreFile(vmwareLayoutFilePair[1], dcMo.getMor(), dsMo.getMor(), legacyCloudStackLayoutFilePair[1], dcMo.getMor(), true);
                String folderToDelete = dsMo.getDatastorePath(managedStoragePoolRootVolumeName, true);
                dsMo.deleteFolder(folderToDelete, dcMo.getMor());
            } else {
                vmInfo = copyTemplateFromSecondaryToPrimary(hyperHost, dsMo, secondaryStorageUrl, templateInfo.first(), templateInfo.second(), templateUuidName, true, _nfsVersion);
            }
        } else {
            s_logger.info("Template " + templateInfo.second() + " has already been setup, skip the template setup process in primary storage");
        }
        TemplateObjectTO newTemplate = new TemplateObjectTO();
        if (managed) {
            if (dsMo != null) {
                String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
                newTemplate.setPath(path);
            }
        } else {
            newTemplate.setPath(templateUuidName);
        }
        newTemplate.setSize((vmInfo != null) ? vmInfo.second() : new Long(0));
        return new CopyCmdAnswer(newTemplate);
    } catch (Throwable e) {
        if (e instanceof RemoteException) {
            hostService.invalidateServiceContext(context);
        }
        String msg = "Unable to copy template to primary storage due to exception:" + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg, e);
        return new CopyCmdAnswer(msg);
    }
}
Also used : 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) RemoteException(java.rmi.RemoteException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DatastoreMO(com.cloud.hypervisor.vmware.mo.DatastoreMO) VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) DataTO(com.cloud.agent.api.to.DataTO) PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) RemoteException(java.rmi.RemoteException) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) DatacenterMO(com.cloud.hypervisor.vmware.mo.DatacenterMO) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 5 with TemplateObjectTO

use of org.apache.cloudstack.storage.to.TemplateObjectTO in project cloudstack by apache.

the class VmwareStorageSubsystemCommandHandler method execute.

@Override
protected Answer execute(CopyCommand cmd) {
    DataTO srcData = cmd.getSrcTO();
    DataTO destData = cmd.getDestTO();
    DataStoreTO srcDataStore = srcData.getDataStore();
    DataStoreTO destDataStore = destData.getDataStore();
    //if copied between s3 and nfs cache, go to resource
    boolean needDelegation = false;
    if (destDataStore instanceof NfsTO && destDataStore.getRole() == DataStoreRole.ImageCache) {
        if (srcDataStore instanceof S3TO || srcDataStore instanceof SwiftTO) {
            needDelegation = true;
        }
    }
    if (srcDataStore.getRole() == DataStoreRole.ImageCache && destDataStore.getRole() == DataStoreRole.Image) {
        //need to take extra processing for vmware, such as packing to ova, before sending to S3
        if (srcData.getObjectType() == DataObjectType.VOLUME) {
            NfsTO cacheStore = (NfsTO) srcDataStore;
            String parentPath = storageResource.getRootDir(cacheStore.getUrl(), _nfsVersion);
            VolumeObjectTO vol = (VolumeObjectTO) srcData;
            String path = vol.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            storageManager.createOva(parentPath + File.separator + path, name);
            vol.setPath(path + File.separator + name + ".ova");
        } else if (srcData.getObjectType() == DataObjectType.TEMPLATE) {
            // sync template from NFS cache to S3 in NFS migration to S3 case
            storageManager.createOvaForTemplate((TemplateObjectTO) srcData);
        } else if (srcData.getObjectType() == DataObjectType.SNAPSHOT) {
            // pack ova first
            // sync snapshot from NFS cache to S3 in NFS migration to S3 case
            String parentPath = storageResource.getRootDir(srcDataStore.getUrl(), _nfsVersion);
            SnapshotObjectTO snap = (SnapshotObjectTO) srcData;
            String path = snap.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            String snapDir = path.substring(0, index);
            storageManager.createOva(parentPath + File.separator + snapDir, name);
            if (destData.getObjectType() == DataObjectType.TEMPLATE) {
                //create template from snapshot on src at first, then copy it to s3
                TemplateObjectTO cacheTemplate = (TemplateObjectTO) destData;
                cacheTemplate.setDataStore(srcDataStore);
                CopyCmdAnswer answer = (CopyCmdAnswer) processor.createTemplateFromSnapshot(cmd);
                if (!answer.getResult()) {
                    return answer;
                }
                cacheTemplate.setDataStore(destDataStore);
                TemplateObjectTO template = (TemplateObjectTO) answer.getNewData();
                template.setDataStore(srcDataStore);
                CopyCommand newCmd = new CopyCommand(template, destData, cmd.getWait(), cmd.executeInSequence());
                Answer result = storageResource.defaultAction(newCmd);
                //clean up template data on staging area
                try {
                    DeleteCommand deleteCommand = new DeleteCommand(template);
                    storageResource.defaultAction(deleteCommand);
                } catch (Exception e) {
                    s_logger.debug("Failed to clean up staging area:", e);
                }
                return result;
            }
        }
        needDelegation = true;
    }
    if (srcData.getObjectType() == DataObjectType.SNAPSHOT && srcData.getDataStore().getRole() == DataStoreRole.Primary) {
        //for back up snapshot, we need to do backup to cache, then to object store if object store is used.
        if (cmd.getCacheTO() != null) {
            cmd.setDestTO(cmd.getCacheTO());
            CopyCmdAnswer answer = (CopyCmdAnswer) processor.backupSnapshot(cmd);
            if (!answer.getResult()) {
                return answer;
            }
            NfsTO cacheStore = (NfsTO) cmd.getCacheTO().getDataStore();
            String parentPath = storageResource.getRootDir(cacheStore.getUrl(), _nfsVersion);
            SnapshotObjectTO newSnapshot = (SnapshotObjectTO) answer.getNewData();
            String path = newSnapshot.getPath();
            int index = path.lastIndexOf(File.separator);
            String name = path.substring(index + 1);
            String dir = path.substring(0, index);
            storageManager.createOva(parentPath + File.separator + dir, name);
            newSnapshot.setPath(newSnapshot.getPath() + ".ova");
            newSnapshot.setDataStore(cmd.getCacheTO().getDataStore());
            CopyCommand newCmd = new CopyCommand(newSnapshot, destData, cmd.getWait(), cmd.executeInSequence());
            Answer result = storageResource.defaultAction(newCmd);
            //clean up data on staging area
            try {
                newSnapshot.setPath(path);
                DeleteCommand deleteCommand = new DeleteCommand(newSnapshot);
                storageResource.defaultAction(deleteCommand);
            } catch (Exception e) {
                s_logger.debug("Failed to clean up staging area:", e);
            }
            return result;
        }
    }
    if (needDelegation) {
        return storageResource.defaultAction(cmd);
    } else {
        return super.execute(cmd);
    }
}
Also used : SnapshotObjectTO(org.apache.cloudstack.storage.to.SnapshotObjectTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) SwiftTO(com.cloud.agent.api.to.SwiftTO) CopyCommand(org.apache.cloudstack.storage.command.CopyCommand) NfsTO(com.cloud.agent.api.to.NfsTO) DeleteCommand(org.apache.cloudstack.storage.command.DeleteCommand) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) Answer(com.cloud.agent.api.Answer) DataTO(com.cloud.agent.api.to.DataTO) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) S3TO(com.cloud.agent.api.to.S3TO) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer)

Aggregations

TemplateObjectTO (org.apache.cloudstack.storage.to.TemplateObjectTO)57 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)40 NfsTO (com.cloud.agent.api.to.NfsTO)34 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)28 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)26 DataTO (com.cloud.agent.api.to.DataTO)22 VolumeObjectTO (org.apache.cloudstack.storage.to.VolumeObjectTO)20 InternalErrorException (com.cloud.exception.InternalErrorException)19 PrimaryDataStoreTO (org.apache.cloudstack.storage.to.PrimaryDataStoreTO)19 DiskTO (com.cloud.agent.api.to.DiskTO)13 XmlRpcException (org.apache.xmlrpc.XmlRpcException)12 XenAPIException (com.xensource.xenapi.Types.XenAPIException)11 SnapshotObjectTO (org.apache.cloudstack.storage.to.SnapshotObjectTO)11 Answer (com.cloud.agent.api.Answer)10 VDI (com.xensource.xenapi.VDI)10 Connection (com.xensource.xenapi.Connection)9 SR (com.xensource.xenapi.SR)9 URI (java.net.URI)9 ConfigurationException (javax.naming.ConfigurationException)9 IOException (java.io.IOException)8