Search in sources :

Example 16 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class XenServerStorageProcessor method snapshotAndCopy.

// if the source SR needs to be attached to, do so
// take a snapshot of the source VDI (on the source SR)
// create an iSCSI SR based on the new back-end volume
// copy the snapshot to the new SR
// delete the snapshot
// detach the new SR
// if we needed to perform an attach to the source SR, detach from it
@Override
public SnapshotAndCopyAnswer snapshotAndCopy(final SnapshotAndCopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    try {
        SR sourceSr = null;
        final Map<String, String> sourceDetails = cmd.getSourceDetails();
        if (sourceDetails != null && sourceDetails.keySet().size() > 0) {
            final String iScsiName = sourceDetails.get(DiskTO.IQN);
            final String storageHost = sourceDetails.get(DiskTO.STORAGE_HOST);
            final String chapInitiatorUsername = sourceDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
            final String chapInitiatorSecret = sourceDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
            sourceSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        }
        final VDI vdiToSnapshot = VDI.getByUuid(conn, cmd.getUuidOfSourceVdi());
        final VDI vdiSnapshot = vdiToSnapshot.snapshot(conn, new HashMap<String, String>());
        final Map<String, String> destDetails = cmd.getDestDetails();
        final String iScsiName = destDetails.get(DiskTO.IQN);
        final String storageHost = destDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = destDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = destDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        final SR newSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, false);
        final VDI vdiCopy = vdiSnapshot.copy(conn, newSr);
        final String vdiUuid = vdiCopy.getUuid(conn);
        vdiSnapshot.destroy(conn);
        if (sourceSr != null) {
            hypervisorResource.removeSR(conn, sourceSr);
        }
        hypervisorResource.removeSR(conn, newSr);
        final SnapshotAndCopyAnswer snapshotAndCopyAnswer = new SnapshotAndCopyAnswer();
        snapshotAndCopyAnswer.setPath(vdiUuid);
        return snapshotAndCopyAnswer;
    } catch (final Exception ex) {
        s_logger.warn("Failed to take and copy snapshot: " + ex.toString(), ex);
        return new SnapshotAndCopyAnswer(ex.getMessage());
    }
}
Also used : SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) Connection(com.xensource.xenapi.Connection) 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)

Example 17 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class XenServerStorageProcessor method dettachIso.

@Override
public Answer dettachIso(final DettachCommand cmd) {
    final DiskTO disk = cmd.getDisk();
    final DataTO data = disk.getData();
    final DataStoreTO store = data.getDataStore();
    String isoURL = null;
    if (store == null) {
        final TemplateObjectTO iso = (TemplateObjectTO) disk.getData();
        isoURL = iso.getName();
    } else {
        if (!(store instanceof NfsTO)) {
            s_logger.debug("Can't detach a iso which is not created on nfs: ");
            return new AttachAnswer("Can't detach a iso which is not created on nfs: ");
        }
        final NfsTO nfsStore = (NfsTO) store;
        isoURL = nfsStore.getUrl() + nfsStore.getPathSeparator() + data.getPath();
    }
    try {
        final Connection conn = hypervisorResource.getConnection();
        // Find the VM
        final VM vm = hypervisorResource.getVM(conn, cmd.getVmName());
        final String vmUUID = vm.getUuid(conn);
        // Find the ISO VDI
        final VDI isoVDI = hypervisorResource.getIsoVDIByURL(conn, cmd.getVmName(), isoURL);
        final SR sr = isoVDI.getSR(conn);
        // Look up all VBDs for this VDI
        final Set<VBD> vbds = isoVDI.getVBDs(conn);
        // the ISO from it
        for (final VBD vbd : vbds) {
            final VM vbdVM = vbd.getVM(conn);
            final String vbdVmUUID = vbdVM.getUuid(conn);
            if (vbdVmUUID.equals(vmUUID)) {
                // If an ISO is already inserted, eject it
                if (!vbd.getEmpty(conn)) {
                    vbd.eject(conn);
                }
                break;
            }
        }
        if (!XenServerUtilitiesHelper.isXenServerToolsSR(sr.getNameLabel(conn))) {
            hypervisorResource.removeSR(conn, sr);
        }
        return new DettachAnswer(disk);
    } catch (final XenAPIException e) {
        final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + "  due to " + e.toString();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    } catch (final Exception e) {
        final String msg = "Failed to detach volume" + " for uuid: " + data.getPath() + "  due to " + e.getMessage();
        s_logger.warn(msg, e);
        return new DettachAnswer(msg);
    }
}
Also used : PrimaryDataStoreTO(org.apache.cloudstack.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) NfsTO(com.cloud.agent.api.to.NfsTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataTO(com.cloud.agent.api.to.DataTO) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(org.apache.cloudstack.storage.to.TemplateObjectTO) DiskTO(com.cloud.agent.api.to.DiskTO) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) SR(com.xensource.xenapi.SR)

Example 18 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

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<String, String>());
        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(org.apache.cloudstack.storage.to.SnapshotObjectTO) Connection(com.xensource.xenapi.Connection) CreateObjectAnswer(org.apache.cloudstack.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)

Example 19 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class CitrixResourceBase method cleanupTemplateSR.

public void cleanupTemplateSR(final Connection conn) {
    Set<PBD> pbds = null;
    try {
        final Host host = Host.getByUuid(conn, _host.getUuid());
        pbds = host.getPBDs(conn);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to get the SRs " + e.toString(), e);
        throw new CloudRuntimeException("Unable to get SRs " + e.toString(), e);
    } catch (final Exception e) {
        throw new CloudRuntimeException("Unable to get SRs " + e.getMessage(), e);
    }
    for (final PBD pbd : pbds) {
        SR sr = null;
        SR.Record srRec = null;
        try {
            sr = pbd.getSR(conn);
            srRec = sr.getRecord(conn);
        } catch (final Exception e) {
            s_logger.warn("pbd.getSR get Exception due to ", e);
            continue;
        }
        final String type = srRec.type;
        if (srRec.shared) {
            continue;
        }
        if (SRType.NFS.equals(type) || SRType.ISO.equals(type) && srRec.nameDescription.contains("template")) {
            try {
                pbd.unplug(conn);
                pbd.destroy(conn);
                sr.forget(conn);
            } catch (final Exception e) {
                s_logger.warn("forget SR catch Exception due to ", e);
            }
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) TimeoutException(java.util.concurrent.TimeoutException) SAXException(org.xml.sax.SAXException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SR(com.xensource.xenapi.SR)

Example 20 with SR

use of com.xensource.xenapi.SR in project cloudstack by apache.

the class CitrixResourceBase method setupHeartbeatSr.

public String setupHeartbeatSr(final Connection conn, final SR sr, final boolean force) throws XenAPIException, XmlRpcException {
    final SR.Record srRec = sr.getRecord(conn);
    final String srUuid = srRec.uuid;
    if (!srRec.shared || !SRType.LVMOHBA.equals(srRec.type) && !SRType.LVMOISCSI.equals(srRec.type) && !SRType.NFS.equals(srRec.type)) {
        return srUuid;
    }
    String result = null;
    final Host host = Host.getByUuid(conn, _host.getUuid());
    final Set<String> tags = host.getTags(conn);
    if (force || !tags.contains("cloud-heartbeat-" + srUuid)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Setting up the heartbeat sr for host " + _host.getIp() + " and sr " + srUuid);
        }
        final Set<PBD> pbds = sr.getPBDs(conn);
        for (final PBD pbd : pbds) {
            final PBD.Record pbdr = pbd.getRecord(conn);
            if (!pbdr.currentlyAttached && pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                pbd.plug(conn);
                break;
            }
        }
        result = callHostPluginThroughMaster(conn, "vmopspremium", "setup_heartbeat_sr", "host", _host.getUuid(), "sr", srUuid);
        if (result == null || !result.split("#")[1].equals("0")) {
            throw new CloudRuntimeException("Unable to setup heartbeat sr on SR " + srUuid + " due to " + result);
        }
        if (!tags.contains("cloud-heartbeat-" + srUuid)) {
            tags.add("cloud-heartbeat-" + srUuid);
            host.setTags(conn, tags);
        }
    }
    result = callHostPluginPremium(conn, "setup_heartbeat_file", "host", _host.getUuid(), "sr", srUuid, "add", "true");
    if (result == null || !result.split("#")[1].equals("0")) {
        throw new CloudRuntimeException("Unable to setup heartbeat file entry on SR " + srUuid + " due to " + result);
    }
    return srUuid;
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.xensource.xenapi.Host) 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