Search in sources :

Example 51 with SR

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

the class CitrixResourceBase method attachConfigDriveToMigratedVm.

public boolean attachConfigDriveToMigratedVm(final Connection conn, final String vmName, final String ipAddr) {
    try {
        s_logger.debug("Attaching config drive iso device for the VM " + vmName + " In host " + ipAddr);
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final SR sr = getSRByNameLabel(conn, _configDriveSRName + ipAddr);
        // Here you will find only two vdis with the <vmname>.iso.
        // one is from source host and second from dest host
        final Set<VDI> vdis = VDI.getByNameLabel(conn, vmName + ".iso");
        if (vdis.isEmpty()) {
            s_logger.debug("Could not find config drive ISO: " + vmName);
            return false;
        }
        VDI configdriveVdi = null;
        for (final VDI vdi : vdis) {
            final SR vdiSr = vdi.getSR(conn);
            if (vdiSr.getUuid(conn).equals(sr.getUuid(conn))) {
                // get this vdi to attach to vbd
                configdriveVdi = vdi;
                s_logger.debug("VDI for the config drive ISO  " + vdi);
            } else {
                // delete the vdi in source host so that the <vmname>.iso file is get removed
                s_logger.debug("Removing the source host VDI for the config drive ISO  " + vdi);
                vdi.destroy(conn);
            }
        }
        if (configdriveVdi == null) {
            s_logger.debug("Config drive ISO VDI is not found ");
            return false;
        }
        for (final VM vm : vms) {
            // create vbd
            final VBD.Record cfgDriveVbdr = new VBD.Record();
            cfgDriveVbdr.VM = vm;
            cfgDriveVbdr.empty = true;
            cfgDriveVbdr.bootable = false;
            cfgDriveVbdr.userdevice = "autodetect";
            cfgDriveVbdr.mode = Types.VbdMode.RO;
            cfgDriveVbdr.type = Types.VbdType.CD;
            final VBD cfgDriveVBD = VBD.create(conn, cfgDriveVbdr);
            s_logger.debug("Inserting vbd " + configdriveVdi);
            cfgDriveVBD.insert(conn, configdriveVdi);
            break;
        }
        return true;
    } catch (final BadServerResponse e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a bad server response.", e);
        return false;
    } catch (final XenAPIException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a xapi problem.", e);
        return false;
    } catch (final XmlRpcException e) {
        s_logger.warn("Failed to attach config drive ISO to the VM  " + vmName + " In host " + ipAddr + " due to a problem in a remote call.", e);
        return false;
    }
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 52 with SR

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

the class XenServerStorageProcessor method createVolume.

@Override
public Answer createVolume(final CreateObjectCommand cmd) {
    final DataTO data = cmd.getData();
    final VolumeObjectTO volume = (VolumeObjectTO) data;
    try {
        final Connection conn = hypervisorResource.getConnection();
        final SR poolSr = hypervisorResource.getStorageRepository(conn, data.getDataStore().getUuid());
        VDI.Record vdir = new VDI.Record();
        vdir.nameLabel = volume.getName();
        vdir.SR = poolSr;
        vdir.type = Types.VdiType.USER;
        vdir.virtualSize = volume.getSize();
        final VDI vdi;
        vdi = VDI.create(conn, vdir);
        vdir = vdi.getRecord(conn);
        final VolumeObjectTO newVol = new VolumeObjectTO();
        newVol.setName(vdir.nameLabel);
        newVol.setSize(vdir.virtualSize);
        newVol.setPath(vdir.uuid);
        return new CreateObjectAnswer(newVol);
    } catch (final Exception e) {
        s_logger.debug("create volume failed: " + e.toString());
        return new CreateObjectAnswer(e.toString());
    }
}
Also used : DataTO(com.cloud.agent.api.to.DataTO) Connection(com.xensource.xenapi.Connection) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) 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 53 with SR

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

the class XenServerStorageProcessor method introduceObject.

@Override
public Answer introduceObject(final IntroduceObjectCmd cmd) {
    try {
        final Connection conn = hypervisorResource.getConnection();
        final DataStoreTO store = cmd.getDataTO().getDataStore();
        final SR poolSr = hypervisorResource.getStorageRepository(conn, store.getUuid());
        poolSr.scan(conn);
        return new IntroduceObjectAnswer(cmd.getDataTO());
    } catch (final Exception e) {
        s_logger.debug("Failed to introduce object", e);
        return new Answer(cmd, false, e.toString());
    }
}
Also used : CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) IntroduceObjectAnswer(com.cloud.storage.command.IntroduceObjectAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(com.cloud.storage.command.SnapshotAndCopyAnswer) AttachPrimaryDataStoreAnswer(com.cloud.storage.command.AttachPrimaryDataStoreAnswer) DettachAnswer(com.cloud.storage.command.DettachAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) DataStoreTO(com.cloud.agent.api.to.DataStoreTO) Connection(com.xensource.xenapi.Connection) IntroduceObjectAnswer(com.cloud.storage.command.IntroduceObjectAnswer) 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 54 with SR

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

the class XenServerStorageProcessor method createTemplateFromSnapshot.

@Override
public Answer createTemplateFromSnapshot(final CopyCommand cmd) {
    final Connection conn = hypervisorResource.getConnection();
    final SnapshotObjectTO snapshotObjTO = (SnapshotObjectTO) cmd.getSrcTO();
    final TemplateObjectTO templateObjTO = (TemplateObjectTO) cmd.getDestTO();
    if (!(snapshotObjTO.getDataStore() instanceof PrimaryDataStoreTO) || !(templateObjTO.getDataStore() instanceof NfsTO)) {
        return null;
    }
    final String userSpecifiedTemplateName = templateObjTO.getName();
    NfsTO destStore = null;
    URI destUri = null;
    try {
        destStore = (NfsTO) templateObjTO.getDataStore();
        destUri = new URI(destStore.getUrl());
    } catch (final Exception ex) {
        s_logger.debug("Invalid URI", ex);
        return new CopyCmdAnswer("Invalid URI: " + ex.toString());
    }
    SR srcSr = null;
    SR destSr = null;
    final String destDir = templateObjTO.getPath();
    VDI destVdi = null;
    boolean result = false;
    try {
        final Map<String, String> srcDetails = cmd.getOptions();
        final String iScsiName = srcDetails.get(DiskTO.IQN);
        final String storageHost = srcDetails.get(DiskTO.STORAGE_HOST);
        final String chapInitiatorUsername = srcDetails.get(DiskTO.CHAP_INITIATOR_USERNAME);
        final String chapInitiatorSecret = srcDetails.get(DiskTO.CHAP_INITIATOR_SECRET);
        srcSr = hypervisorResource.getIscsiSR(conn, iScsiName, storageHost, iScsiName, chapInitiatorUsername, chapInitiatorSecret, true);
        final String destNfsPath = destUri.getHost() + ":" + destUri.getPath();
        if (!hypervisorResource.createSecondaryStorageFolder(conn, destNfsPath, destDir)) {
            final String details = " Failed to create folder " + destDir + " in secondary storage";
            s_logger.warn(details);
            return new CopyCmdAnswer(details);
        }
        final URI templateUri = new URI(destStore.getUrl() + "/" + destDir);
        destSr = hypervisorResource.createNfsSRbyURI(conn, templateUri, false);
        // there should only be one VDI in this SR
        final VDI srcVdi = srcSr.getVDIs(conn).iterator().next();
        destVdi = srcVdi.copy(conn, destSr);
        // scan makes XenServer pick up VDI physicalSize
        destSr.scan(conn);
        if (userSpecifiedTemplateName != null) {
            destVdi.setNameLabel(conn, userSpecifiedTemplateName);
        }
        final String templateUuid = destVdi.getUuid(conn);
        final String templateFilename = templateUuid + ".vhd";
        final long virtualSize = destVdi.getVirtualSize(conn);
        final long physicalSize = destVdi.getPhysicalUtilisation(conn);
        // create the template.properties file
        String templatePath = destNfsPath + "/" + destDir;
        templatePath = templatePath.replaceAll("//", "/");
        result = hypervisorResource.postCreatePrivateTemplate(conn, templatePath, templateFilename, templateUuid, userSpecifiedTemplateName, null, physicalSize, virtualSize, templateObjTO.getId());
        if (!result) {
            throw new CloudRuntimeException("Could not create the template.properties file on secondary storage dir: " + templateUri);
        }
        final TemplateObjectTO newTemplate = new TemplateObjectTO();
        newTemplate.setPath(destDir + "/" + templateFilename);
        newTemplate.setFormat(Storage.ImageFormat.VHD);
        newTemplate.setHypervisorType(HypervisorType.XenServer);
        newTemplate.setSize(virtualSize);
        newTemplate.setPhysicalSize(physicalSize);
        newTemplate.setName(templateUuid);
        result = true;
        return new CopyCmdAnswer(newTemplate);
    } catch (final Exception ex) {
        s_logger.error("Failed to create a template from a snapshot", ex);
        return new CopyCmdAnswer("Failed to create a template from a snapshot: " + ex.toString());
    } finally {
        if (!result) {
            if (destVdi != null) {
                try {
                    destVdi.destroy(conn);
                } catch (final Exception e) {
                    s_logger.debug("Cleaned up leftover VDI on destination storage due to failure: ", e);
                }
            }
        }
        if (srcSr != null) {
            hypervisorResource.removeSR(conn, srcSr);
        }
        if (destSr != null) {
            hypervisorResource.removeSR(conn, destSr);
        }
    }
}
Also used : SnapshotObjectTO(com.cloud.storage.to.SnapshotObjectTO) 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) PrimaryDataStoreTO(com.cloud.storage.to.PrimaryDataStoreTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) SR(com.xensource.xenapi.SR)

Example 55 with SR

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

the class XenServerStorageProcessor method directDownloadHttpTemplate.

protected Answer directDownloadHttpTemplate(final CopyCommand cmd, final DecodedDataObject srcObj, final DecodedDataObject destObj) {
    final Connection conn = hypervisorResource.getConnection();
    SR poolsr = null;
    VDI vdi = null;
    boolean result = false;
    try {
        if (destObj.getPath() == null) {
        // need to create volume at first
        }
        vdi = VDI.getByUuid(conn, destObj.getPath());
        if (vdi == null) {
            throw new CloudRuntimeException("can't find volume: " + destObj.getPath());
        }
        final String destStoreUuid = destObj.getStore().getUuid();
        final Set<SR> srs = SR.getByNameLabel(conn, destStoreUuid);
        if (srs.size() != 1) {
            throw new CloudRuntimeException("storage uuid: " + destStoreUuid + " is not unique");
        }
        poolsr = srs.iterator().next();
        final VDI.Record vdir = vdi.getRecord(conn);
        final String vdiLocation = vdir.location;
        String pbdLocation = null;
        if (destObj.getStore().getScheme().equalsIgnoreCase(DataStoreProtocol.NFS.toString())) {
            pbdLocation = "/run/sr-mount/" + poolsr.getUuid(conn);
        } else {
            final Set<PBD> pbds = poolsr.getPBDs(conn);
            if (pbds.size() != 1) {
                throw new CloudRuntimeException("Don't how to handle multiple pbds:" + pbds.size() + " for sr: " + poolsr.getUuid(conn));
            }
            final PBD pbd = pbds.iterator().next();
            final Map<String, String> deviceCfg = pbd.getDeviceConfig(conn);
            pbdLocation = deviceCfg.get("location");
        }
        if (pbdLocation == null) {
            throw new CloudRuntimeException("Can't get pbd location");
        }
        final String vdiPath = pbdLocation + "/" + vdiLocation + ".vhd";
        // download a url into vdipath
        // downloadHttpToLocalFile(vdiPath, template.getPath());
        hypervisorResource.callHostPlugin(conn, "storagePlugin", "downloadTemplateFromUrl", "destPath", vdiPath, "srcUrl", srcObj.getPath());
        result = true;
    // return new CopyCmdAnswer(cmd, vdi.getUuid(conn));
    } catch (final BadServerResponse e) {
        s_logger.debug("Failed to download template", e);
    } catch (final XenAPIException e) {
        s_logger.debug("Failed to download template", e);
    } catch (final XmlRpcException e) {
        s_logger.debug("Failed to download template", e);
    } catch (final Exception e) {
        s_logger.debug("Failed to download template", e);
    } finally {
        if (!result && vdi != null) {
            try {
                vdi.destroy(conn);
            } catch (final BadServerResponse e) {
                s_logger.debug("Failed to cleanup newly created vdi");
            } catch (final XenAPIException e) {
                s_logger.debug("Failed to cleanup newly created vdi");
            } catch (final XmlRpcException e) {
                s_logger.debug("Failed to cleanup newly created vdi");
            }
        }
    }
    return new Answer(cmd, false, "Failed to download template");
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) CreateObjectAnswer(com.cloud.storage.command.CreateObjectAnswer) IntroduceObjectAnswer(com.cloud.storage.command.IntroduceObjectAnswer) Answer(com.cloud.agent.api.Answer) SnapshotAndCopyAnswer(com.cloud.storage.command.SnapshotAndCopyAnswer) AttachPrimaryDataStoreAnswer(com.cloud.storage.command.AttachPrimaryDataStoreAnswer) DettachAnswer(com.cloud.storage.command.DettachAnswer) CopyCmdAnswer(com.cloud.storage.command.CopyCmdAnswer) AttachAnswer(com.cloud.storage.command.AttachAnswer) PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) 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