Search in sources :

Example 86 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class CitrixResourceBase method getLocalEXTSR.

protected SR getLocalEXTSR(final Connection conn) {
    try {
        final Map<SR, SR.Record> map = SR.getAllRecords(conn);
        if (map != null && !map.isEmpty()) {
            for (final Map.Entry<SR, SR.Record> entry : map.entrySet()) {
                final SR.Record srRec = entry.getValue();
                if (SRType.FILE.equals(srRec.type) || SRType.EXT.equals(srRec.type)) {
                    final Set<PBD> pbds = srRec.PBDs;
                    if (pbds == null) {
                        continue;
                    }
                    for (final PBD pbd : pbds) {
                        final Host host = pbd.getHost(conn);
                        if (!isRefNull(host) && host.getUuid(conn).equals(_host.getUuid())) {
                            if (!pbd.getCurrentlyAttached(conn)) {
                                pbd.plug(conn);
                            }
                            final SR sr = entry.getKey();
                            sr.scan(conn);
                            return sr;
                        }
                    }
                }
            }
        }
    } catch (final XenAPIException e) {
        final String msg = "Unable to get local EXTSR in host:" + _host.getUuid() + e.toString();
        s_logger.warn(msg);
    } catch (final XmlRpcException e) {
        final String msg = "Unable to get local EXTSR in host:" + _host.getUuid() + e.getCause();
        s_logger.warn(msg);
    }
    return null;
}
Also used : PBD(com.xensource.xenapi.PBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) Map(java.util.Map) HashMap(java.util.HashMap) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 87 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createTemplateFromVolume.

@Override
public Answer createTemplateFromVolume(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO volume = (VolumeObjectTO) cmd.getSrcTO();
    final TemplateObjectTO template = (TemplateObjectTO) cmd.getDestTO();
    final NfsTO destStore = (NfsTO) cmd.getDestTO().getDataStore();
    final int wait = cmd.getWait();
    final String secondaryStoragePoolURL = destStore.getUrl();
    final String volumeUUID = volume.getPath();
    final String userSpecifiedName = template.getName();
    String details = null;
    SR tmpltSR = null;
    boolean result = false;
    String secondaryStorageMountPath = null;
    String installPath = null;
    try {
        final URI uri = new URI(secondaryStoragePoolURL);
        secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath();
        installPath = template.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath)) {
            details = " Filed to create folder " + installPath + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final VDI vol = getVDIbyUuid(conn, volumeUUID);
        // create template SR
        final URI tmpltURI = new URI(secondaryStoragePoolURL + "/" + installPath);
        tmpltSR = hypervisorResource.createNfsSRbyURI(conn, tmpltURI, false);
        // copy volume to template SR
        final VDI tmpltVDI = hypervisorResource.cloudVDIcopy(conn, vol, tmpltSR, wait);
        // scan makes XenServer pick up VDI physicalSize
        tmpltSR.scan(conn);
        if (userSpecifiedName != null) {
            tmpltVDI.setNameLabel(conn, userSpecifiedName);
        }
        final String tmpltUUID = tmpltVDI.getUuid(conn);
        final String tmpltFilename = tmpltUUID + ".vhd";
        final long virtualSize = tmpltVDI.getVirtualSize(conn);
        final long physicalSize = tmpltVDI.getPhysicalUtilisation(conn);
        // create the template.properties file
        final String templatePath = secondaryStorageMountPath + "/" + installPath;
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, tmpltFilename, tmpltUUID, userSpecifiedName, null, physicalSize, virtualSize, template.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + tmpltURI);
        }
        installPath = installPath + "/" + tmpltFilename;
        hypervisorResource.removeSR(conn, tmpltSR);
        tmpltSR = null;
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(installPath);
        newTemplate.setFormat(ImageFormat.VHD);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(tmpltUUID);
        final CopyCmdAnswer answer = new CopyCmdAnswer(newTemplate);
        return answer;
    } catch (final Exception e) {
        if (tmpltSR != null) {
            hypervisorResource.removeSR(conn, tmpltSR);
        }
        if (secondaryStorageMountPath != null) {
            hypervisorResource.deleteSecondaryStorageFolder(conn, secondaryStorageMountPath, installPath);
        }
        details = "Creating template from volume " + volumeUUID + " failed due to " + e.toString();
        s_logger.error(details, e);
    }
    return new CopyCmdAnswer(details);
}
Also used : Connection(com.xensource.xenapi.Connection) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 88 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method copyVolumeFromPrimaryToSecondary.

@Override
public Answer copyVolumeFromPrimaryToSecondary(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final VolumeObjectTO srcVolume = (VolumeObjectTO) cmd.getSrcTO();
    final VolumeObjectTO destVolume = (VolumeObjectTO) cmd.getDestTO();
    final int wait = cmd.getWait();
    final DataStoreTO destStore = destVolume.getDataStore();
    if (destStore instanceof NfsTO) {
        SR secondaryStorage = null;
        try {
            final NfsTO nfsStore = (NfsTO) destStore;
            final URI uri = new URI(nfsStore.getUrl());
            // Create the volume folder
            if (!hypervisorResource.createSecondaryStorageFolder(conn, uri.getHost() + ":" + uri.getPath(), destVolume.getPath())) {
                throw new InternalErrorException("Failed to create the volume folder.");
            }
            // Create a SR for the volume UUID folder
            secondaryStorage = hypervisorResource.createNfsSRbyURI(conn, new URI(nfsStore.getUrl() + nfsStore.getPathSeparator() + destVolume.getPath()), false);
            // Look up the volume on the source primary storage pool
            final VDI srcVdi = getVDIbyUuid(conn, srcVolume.getPath());
            // Copy the volume to secondary storage
            final VDI destVdi = hypervisorResource.cloudVDIcopy(conn, srcVdi, secondaryStorage, wait);
            final String destVolumeUUID = destVdi.getUuid(conn);
            final VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(destVolume.getPath() + nfsStore.getPathSeparator() + destVolumeUUID + ".vhd");
            newVol.setSize(srcVolume.getSize());
            return new CopyCmdAnswer(newVol);
        } catch (final Exception e) {
            s_logger.debug("Failed to copy volume to secondary: " + e.toString());
            return new CopyCmdAnswer("Failed to copy volume to secondary: " + e.toString());
        } finally {
            hypervisorResource.removeSR(conn, secondaryStorage);
        }
    }
    return new CopyCmdAnswer("unsupported protocol");
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) InternalErrorException(com.cloud.exception.InternalErrorException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 89 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method copyVolumeFromImageCacheToPrimary.

@Override
public Answer copyVolumeFromImageCacheToPrimary(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final DataTO srcData = cmd.getSrcTO();
    final DataTO destData = cmd.getDestTO();
    final int wait = cmd.getWait();
    final VolumeObjectTO srcVolume = (VolumeObjectTO) srcData;
    final VolumeObjectTO destVolume = (VolumeObjectTO) destData;
    final DataStoreTO srcStore = srcVolume.getDataStore();
    if (srcStore instanceof NfsTO) {
        final NfsTO nfsStore = (NfsTO) srcStore;
        try {
            final SR primaryStoragePool = hypervisorResource.getStorageRepository(conn, destVolume.getDataStore().getUuid());
            final String srUuid = primaryStoragePool.getUuid(conn);
            final URI uri = new URI(nfsStore.getUrl());
            final String volumePath = uri.getHost() + ":" + uri.getPath() + nfsStore.getPathSeparator() + srcVolume.getPath();
            final String uuid = copy_vhd_from_secondarystorage(conn, volumePath, srUuid, wait);
            final VolumeObjectTO newVol = new VolumeObjectTO();
            newVol.setPath(uuid);
            newVol.setSize(srcVolume.getSize());
            return new CopyCmdAnswer(newVol);
        } catch (final Exception e) {
            final String msg = "Catch Exception " + e.getClass().getName() + " due to " + e.toString();
            s_logger.warn(msg, e);
            return new CopyCmdAnswer(e.toString());
        }
    }
    s_logger.debug("unsupported protocol");
    return new CopyCmdAnswer("unsupported protocol");
}
Also used : PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DataTO(com.cloud.agent.api.to.DataTO) Connection(com.xensource.xenapi.Connection) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 90 with SR

use of com.xensource.xenapi.SR in project cosmic by MissionCriticalCloud.

the class XenServerStorageProcessor method createSnapshot.

@Override
public Answer createSnapshot(final CreateObjectCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotTO = (SnapshotObjectTO) cmd.getData();
    final long snapshotId = snapshotTO.getId();
    final String snapshotName = snapshotTO.getName();
    String details = "create snapshot operation Failed for snapshotId: " + snapshotId;
    String snapshotUUID = null;
    try {
        final String volumeUUID = snapshotTO.getVolume().getPath();
        final VDI volume = VDI.getByUuid(conn, volumeUUID);
        final VDI snapshot = volume.snapshot(conn, new HashMap<>());
        if (snapshotName != null) {
            snapshot.setNameLabel(conn, snapshotName);
        }
        snapshotUUID = snapshot.getUuid(conn);
        final String preSnapshotUUID = snapshotTO.getParentSnapshotPath();
        // check if it is a empty snapshot
        if (preSnapshotUUID != null) {
            final SR sr = volume.getSR(conn);
            final String srUUID = sr.getUuid(conn);
            final String type = sr.getType(conn);
            final Boolean isISCSI = IsISCSI(type);
            final String snapshotParentUUID = getVhdParent(conn, srUUID, snapshotUUID, isISCSI);
            try {
                final String preSnapshotParentUUID = getVhdParent(conn, srUUID, preSnapshotUUID, isISCSI);
                if (snapshotParentUUID != null && snapshotParentUUID.equals(preSnapshotParentUUID)) {
                    // this is empty snapshot, remove it
                    snapshot.destroy(conn);
                    snapshotUUID = preSnapshotUUID;
                }
            } catch (final Exception e) {
                s_logger.debug("Failed to get parent snapshot", e);
            }
        }
        final SnapshotObjectTO newSnapshot = new SnapshotObjectTO();
        newSnapshot.setPath(snapshotUUID);
        return new CreateObjectAnswer(newSnapshot);
    } catch (final XenAPIException e) {
        details += ", reason: " + e.toString();
        s_logger.warn(details, e);
    } catch (final Exception e) {
        details += ", reason: " + e.toString();
        s_logger.warn(details, e);
    }
    return new CreateObjectAnswer(details);
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) Connection(com.xensource.xenapi.Connection) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Aggregations

SR (com.xensource.xenapi.SR)165 XenAPIException (com.xensource.xenapi.Types.XenAPIException)105 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)102 XmlRpcException (org.apache.xmlrpc.XmlRpcException)97 Connection (com.xensource.xenapi.Connection)92 VDI (com.xensource.xenapi.VDI)81 InternalErrorException (com.cloud.exception.InternalErrorException)64 HashMap (java.util.HashMap)45 Host (com.xensource.xenapi.Host)40 PBD (com.xensource.xenapi.PBD)37 URI (java.net.URI)36 NfsTO (com.cloud.agent.api.to.NfsTO)34 Test (org.junit.Test)27 DataStoreTO (com.cloud.agent.api.to.DataStoreTO)26 Task (com.xensource.xenapi.Task)26 DataTO (com.cloud.agent.api.to.DataTO)25 Answer (com.cloud.agent.api.Answer)22 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)21 IOException (java.io.IOException)20 MalformedURLException (java.net.MalformedURLException)20