Search in sources :

Example 31 with SR

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

the class CitrixAttachIsoCommandWrapper method execute.

@Override
public Answer execute(final AttachIsoCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final boolean attach = command.isAttach();
    final String vmName = command.getVmName();
    final String isoURL = command.getIsoPath();
    String errorMsg;
    if (attach) {
        errorMsg = "Failed to attach ISO";
    } else {
        errorMsg = "Failed to detach ISO";
    }
    try {
        if (attach) {
            VBD isoVBD = null;
            // Find the VM
            final VM vm = citrixResourceBase.getVM(conn, vmName);
            // Find the ISO VDI
            final VDI isoVDI = citrixResourceBase.getIsoVDIByURL(conn, vmName, isoURL);
            // Find the VM's CD-ROM VBD
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final String userDevice = vbd.getUserdevice(conn);
                final Types.VbdType type = vbd.getType(conn);
                if (userDevice.equals("3") && type == Types.VbdType.CD) {
                    isoVBD = vbd;
                    break;
                }
            }
            if (isoVBD == null) {
                throw new CloudRuntimeException("Unable to find CD-ROM VBD for VM: " + vmName);
            } else {
                // If an ISO is already inserted, eject it
                if (isoVBD.getEmpty(conn) == false) {
                    isoVBD.eject(conn);
                }
                // Insert the new ISO
                isoVBD.insert(conn, isoVDI);
            }
            return new Answer(command);
        } else {
            // Find the VM
            final VM vm = citrixResourceBase.getVM(conn, vmName);
            final String vmUUID = vm.getUuid(conn);
            // Find the ISO VDI
            final VDI isoVDI = citrixResourceBase.getIsoVDIByURL(conn, vmName, 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))) {
                citrixResourceBase.removeSR(conn, sr);
            }
            return new Answer(command);
        }
    } catch (final XenAPIException e) {
        s_logger.warn(errorMsg + ": " + e.toString(), e);
        return new Answer(command, false, e.toString());
    } catch (final Exception e) {
        s_logger.warn(errorMsg + ": " + e.toString(), e);
        return new Answer(command, false, e.getMessage());
    }
}
Also used : Types(com.xensource.xenapi.Types) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Answer(com.cloud.agent.api.Answer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) SR(com.xensource.xenapi.SR)

Example 32 with SR

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

the class CitrixCreateCommandWrapper method execute.

@Override
public Answer execute(final CreateCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final StorageFilerTO pool = command.getPool();
    final DiskProfile dskch = command.getDiskCharacteristics();
    VDI vdi = null;
    try {
        final SR poolSr = citrixResourceBase.getStorageRepository(conn, CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath()));
        if (command.getTemplateUrl() != null) {
            VDI tmpltvdi = null;
            tmpltvdi = citrixResourceBase.getVDIbyUuid(conn, command.getTemplateUrl());
            vdi = tmpltvdi.createClone(conn, new HashMap<String, String>());
            vdi.setNameLabel(conn, dskch.getName());
        } else {
            final VDI.Record vdir = new VDI.Record();
            vdir.nameLabel = dskch.getName();
            vdir.SR = poolSr;
            vdir.type = Types.VdiType.USER;
            vdir.virtualSize = dskch.getSize();
            vdi = VDI.create(conn, vdir);
        }
        VDI.Record vdir;
        vdir = vdi.getRecord(conn);
        s_logger.debug("Succesfully created VDI for " + command + ".  Uuid = " + vdir.uuid);
        final VolumeTO vol = new VolumeTO(command.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), vdir.nameLabel, pool.getPath(), vdir.uuid, vdir.virtualSize, null);
        return new CreateAnswer(command, vol);
    } catch (final Exception e) {
        s_logger.warn("Unable to create volume; Pool=" + pool + "; Disk: " + dskch, e);
        return new CreateAnswer(command, e);
    }
}
Also used : CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) VolumeTO(com.cloud.agent.api.to.VolumeTO) HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) DiskProfile(com.cloud.vm.DiskProfile) SR(com.xensource.xenapi.SR)

Example 33 with SR

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

the class CitrixDeleteStoragePoolCommandWrapper method execute.

@Override
public Answer execute(final DeleteStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final StorageFilerTO poolTO = command.getPool();
    try {
        final SR sr;
        // getRemoveDatastore being true indicates we are using managed storage and need to pull the SR name out of a Map
        // instead of pulling it out using getUuid of the StorageFilerTO instance.
        String srNameLabel;
        if (command.getRemoveDatastore()) {
            Map<String, String> details = command.getDetails();
            srNameLabel = details.get(DeleteStoragePoolCommand.DATASTORE_NAME);
        } else {
            srNameLabel = CitrixHelper.getSRNameLabel(poolTO.getUuid(), poolTO.getType(), poolTO.getPath());
        }
        sr = citrixResourceBase.getStorageRepository(conn, srNameLabel);
        citrixResourceBase.removeSR(conn, sr);
        final Answer answer = new Answer(command, true, "success");
        return answer;
    } catch (final Exception e) {
        final String msg = "DeleteStoragePoolCommand XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + poolTO.getHost() + poolTO.getPath();
        s_logger.error(msg, e);
        return new Answer(command, false, msg);
    }
}
Also used : Answer(com.cloud.agent.api.Answer) Connection(com.xensource.xenapi.Connection) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) SR(com.xensource.xenapi.SR)

Example 34 with SR

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

the class CitrixModifyStoragePoolCommandWrapper method execute.

@Override
public Answer execute(final ModifyStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final StorageFilerTO pool = command.getPool();
    final boolean add = command.getAdd();
    if (add) {
        try {
            String srName = command.getStoragePath();
            if (srName == null) {
                srName = CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath());
            }
            final SR sr = citrixResourceBase.getStorageRepository(conn, srName);
            citrixResourceBase.setupHeartbeatSr(conn, sr, false);
            final long capacity = sr.getPhysicalSize(conn);
            final long available = capacity - sr.getPhysicalUtilisation(conn);
            if (capacity == -1) {
                final String msg = "Pool capacity is -1! pool: " + pool.getHost() + pool.getPath();
                s_logger.warn(msg);
                return new Answer(command, false, msg);
            }
            final Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
            final ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(command, capacity, available, tInfo);
            return answer;
        } catch (final XenAPIException e) {
            final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        } catch (final Exception e) {
            final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    } else {
        try {
            final SR sr = citrixResourceBase.getStorageRepository(conn, CitrixHelper.getSRNameLabel(pool.getUuid(), pool.getType(), pool.getPath()));
            final String srUuid = sr.getUuid(conn);
            final String result = citrixResourceBase.callHostPluginPremium(conn, "setup_heartbeat_file", "host", citrixResourceBase.getHost().getUuid(), "sr", srUuid, "add", "false");
            if (result == null || !result.split("#")[1].equals("0")) {
                throw new CloudRuntimeException("Unable to remove heartbeat file entry for SR " + srUuid + " due to " + result);
            }
            return new Answer(command, true, "success");
        } catch (final XenAPIException e) {
            final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        } catch (final Exception e) {
            final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) HashMap(java.util.HashMap) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) Answer(com.cloud.agent.api.Answer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 35 with SR

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

the class CitrixPrimaryStorageDownloadCommandWrapper method execute.

@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final CitrixResourceBase citrixResourceBase) {
    final String tmplturl = command.getUrl();
    final String poolName = command.getPoolUuid();
    final int wait = command.getWait();
    try {
        final URI uri = new URI(tmplturl);
        final String tmplpath = uri.getHost() + ":" + uri.getPath();
        final Connection conn = citrixResourceBase.getConnection();
        SR poolsr = null;
        final Set<SR> srs = SR.getByNameLabel(conn, poolName);
        if (srs.size() != 1) {
            final String msg = "There are " + srs.size() + " SRs with same name: " + poolName;
            s_logger.warn(msg);
            return new PrimaryStorageDownloadAnswer(msg);
        } else {
            poolsr = srs.iterator().next();
        }
        final String pUuid = poolsr.getUuid(conn);
        final boolean isISCSI = citrixResourceBase.IsISCSI(poolsr.getType(conn));
        final String uuid = citrixResourceBase.copyVhdFromSecondaryStorage(conn, tmplpath, pUuid, wait);
        final VDI tmpl = citrixResourceBase.getVDIbyUuid(conn, uuid);
        final VDI snapshotvdi = tmpl.snapshot(conn, new HashMap<String, String>());
        final String snapshotUuid = snapshotvdi.getUuid(conn);
        snapshotvdi.setNameLabel(conn, "Template " + command.getName());
        final String parentuuid = citrixResourceBase.getVhdParent(conn, pUuid, snapshotUuid, isISCSI);
        final VDI parent = citrixResourceBase.getVDIbyUuid(conn, parentuuid);
        final Long phySize = parent.getPhysicalUtilisation(conn);
        tmpl.destroy(conn);
        poolsr.scan(conn);
        try {
            Thread.sleep(5000);
        } catch (final Exception e) {
        }
        return new PrimaryStorageDownloadAnswer(snapshotvdi.getUuid(conn), phySize);
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " on host:" + citrixResourceBase.getHost().getUuid() + " for template: " + tmplturl + " due to " + e.toString();
        s_logger.warn(msg, e);
        return new PrimaryStorageDownloadAnswer(msg);
    }
}
Also used : PrimaryStorageDownloadAnswer(com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) URI(java.net.URI) 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