Search in sources :

Example 71 with SR

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

the class Xenserver625StorageProcessor method createFileSR.

protected SR createFileSR(final Connection conn, final String path) {
    SR sr = null;
    PBD pbd = null;
    try {
        final String srname = path.trim();
        synchronized (srname.intern()) {
            final Set<SR> srs = SR.getByNameLabel(conn, srname);
            if (srs != null && !srs.isEmpty()) {
                return srs.iterator().next();
            }
            final Map<String, String> smConfig = new HashMap<String, String>();
            final Host host = Host.getByUuid(conn, hypervisorResource.getHost().getUuid());
            final String uuid = UUID.randomUUID().toString();
            sr = SR.introduce(conn, uuid, srname, srname, "file", "file", false, smConfig);
            final PBD.Record record = new PBD.Record();
            record.host = host;
            record.SR = sr;
            smConfig.put("location", path);
            record.deviceConfig = smConfig;
            pbd = PBD.create(conn, record);
            pbd.plug(conn);
            sr.scan(conn);
        }
        return sr;
    } catch (final Exception ex) {
        try {
            if (pbd != null) {
                pbd.destroy(conn);
            }
        } catch (final Exception e1) {
            s_logger.debug("Failed to destroy PBD", ex);
        }
        try {
            if (sr != null) {
                sr.forget(conn);
            }
        } catch (final Exception e2) {
            s_logger.error("Failed to forget SR", ex);
        }
        final String msg = "createFileSR failed! due to the following: " + ex.toString();
        s_logger.warn(msg, ex);
        throw new CloudRuntimeException(msg, ex);
    }
}
Also used : PBD(com.xensource.xenapi.PBD) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.xensource.xenapi.Host) InternalErrorException(com.cloud.exception.InternalErrorException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) SR(com.xensource.xenapi.SR)

Example 72 with SR

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

the class CitrixResourceBase method initializeLocalSR.

protected StartupStorageCommand initializeLocalSR(final Connection conn) {
    final SR lvmsr = getLocalLVMSR(conn);
    if (lvmsr != null) {
        try {
            _host.setLocalSRuuid(lvmsr.getUuid(conn));
            final String lvmuuid = lvmsr.getUuid(conn);
            final long cap = lvmsr.getPhysicalSize(conn);
            if (cap > 0) {
                final long avail = cap - lvmsr.getPhysicalUtilisation(conn);
                lvmsr.setNameLabel(conn, lvmuuid);
                final String name = "Cloud Stack Local LVM Storage Pool for " + _host.getUuid();
                lvmsr.setNameDescription(conn, name);
                final Host host = Host.getByUuid(conn, _host.getUuid());
                final String address = host.getAddress(conn);
                final StoragePoolInfo pInfo = new StoragePoolInfo(lvmuuid, address, SRType.LVM.toString(), SRType.LVM.toString(), StoragePoolType.LVM, cap, avail);
                final StartupStorageCommand cmd = new StartupStorageCommand();
                cmd.setPoolInfo(pInfo);
                cmd.setGuid(_host.getUuid());
                cmd.setDataCenter(Long.toString(_dcId));
                cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
                return cmd;
            }
        } catch (final XenAPIException e) {
            final String msg = "build local LVM info err in host:" + _host.getUuid() + e.toString();
            s_logger.warn(msg);
        } catch (final XmlRpcException e) {
            final String msg = "build local LVM info err in host:" + _host.getUuid() + e.getMessage();
            s_logger.warn(msg);
        }
    }
    final SR extsr = getLocalEXTSR(conn);
    if (extsr != null) {
        try {
            final String extuuid = extsr.getUuid(conn);
            _host.setLocalSRuuid(extuuid);
            final long cap = extsr.getPhysicalSize(conn);
            if (cap > 0) {
                final long avail = cap - extsr.getPhysicalUtilisation(conn);
                extsr.setNameLabel(conn, extuuid);
                final String name = "Cloud Stack Local EXT Storage Pool for " + _host.getUuid();
                extsr.setNameDescription(conn, name);
                final Host host = Host.getByUuid(conn, _host.getUuid());
                final String address = host.getAddress(conn);
                final StoragePoolInfo pInfo = new StoragePoolInfo(extuuid, address, SRType.EXT.toString(), SRType.EXT.toString(), StoragePoolType.EXT, cap, avail);
                final StartupStorageCommand cmd = new StartupStorageCommand();
                cmd.setPoolInfo(pInfo);
                cmd.setGuid(_host.getUuid());
                cmd.setDataCenter(Long.toString(_dcId));
                cmd.setResourceType(Storage.StorageResourceType.STORAGE_POOL);
                return cmd;
            }
        } catch (final XenAPIException e) {
            final String msg = "build local EXT info err in host:" + _host.getUuid() + e.toString();
            s_logger.warn(msg);
        } catch (final XmlRpcException e) {
            final String msg = "build local EXT info err in host:" + _host.getUuid() + e.getMessage();
            s_logger.warn(msg);
        }
    }
    return null;
}
Also used : StartupStorageCommand(com.cloud.agent.api.StartupStorageCommand) StoragePoolInfo(com.cloud.agent.api.StoragePoolInfo) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 73 with SR

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

the class XenServerStorageProcessor method getNfsSR.

protected SR getNfsSR(final Connection conn, final StorageFilerTO pool) {
    final Map<String, String> deviceConfig = new HashMap<String, String>();
    try {
        final String server = pool.getHost();
        String serverpath = pool.getPath();
        serverpath = serverpath.replace("//", "/");
        final Set<SR> srs = SR.getAll(conn);
        for (final SR sr : srs) {
            if (!SRType.NFS.equals(sr.getType(conn))) {
                continue;
            }
            final Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds.isEmpty()) {
                continue;
            }
            final PBD pbd = pbds.iterator().next();
            final Map<String, String> dc = pbd.getDeviceConfig(conn);
            if (dc == null) {
                continue;
            }
            if (dc.get("server") == null) {
                continue;
            }
            if (dc.get("serverpath") == null) {
                continue;
            }
            if (server.equals(dc.get("server")) && serverpath.equals(dc.get("serverpath"))) {
                throw new CloudRuntimeException("There is a SR using the same configuration server:" + dc.get("server") + ", serverpath:" + dc.get("serverpath") + " for pool " + pool.getUuid() + "on host:" + hypervisorResource.getHost().getUuid());
            }
        }
        deviceConfig.put("server", server);
        deviceConfig.put("serverpath", serverpath);
        final Host host = Host.getByUuid(conn, hypervisorResource.getHost().getUuid());
        final Map<String, String> smConfig = new HashMap<String, String>();
        smConfig.put("nosubdir", "true");
        final SR sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), Long.toString(pool.getId()), SRType.NFS.toString(), "user", true, smConfig);
        sr.scan(conn);
        return sr;
    } catch (final XenAPIException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
    } catch (final XmlRpcException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
    }
}
Also used : PBD(com.xensource.xenapi.PBD) HashMap(java.util.HashMap) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XmlRpcException(org.apache.xmlrpc.XmlRpcException) SR(com.xensource.xenapi.SR)

Example 74 with SR

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

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(org.apache.cloudstack.storage.command.CreateObjectAnswer) Answer(com.cloud.agent.api.Answer) ResignatureAnswer(org.apache.cloudstack.storage.command.ResignatureAnswer) IntroduceObjectAnswer(org.apache.cloudstack.storage.command.IntroduceObjectAnswer) AttachPrimaryDataStoreAnswer(org.apache.cloudstack.storage.command.AttachPrimaryDataStoreAnswer) DettachAnswer(org.apache.cloudstack.storage.command.DettachAnswer) SnapshotAndCopyAnswer(org.apache.cloudstack.storage.command.SnapshotAndCopyAnswer) CopyCmdAnswer(org.apache.cloudstack.storage.command.CopyCmdAnswer) AttachAnswer(org.apache.cloudstack.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)

Example 75 with SR

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

the class CitrixResourceBase method getLocalLVMSR.

protected SR getLocalLVMSR(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.LVM.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 LVMSR in host:" + _host.getUuid() + e.toString();
        s_logger.warn(msg);
    } catch (final XmlRpcException e) {
        final String msg = "Unable to get local LVMSR 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)

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