Search in sources :

Example 31 with PBD

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

the class CitrixResourceBase method introduceAndPlugIscsiSr.

private SR introduceAndPlugIscsiSr(Connection conn, String pooluuid, String srNameLabel, String type, Map<String, String> smConfig, Map<String, String> deviceConfig, boolean ignoreIntroduceException) throws XmlRpcException, XenAPIException {
    SR sr = null;
    try {
        sr = SR.introduce(conn, pooluuid, srNameLabel, srNameLabel, type, "user", true, smConfig);
    } catch (final XenAPIException ex) {
        if (ignoreIntroduceException) {
            return sr;
        }
        throw ex;
    }
    final Set<Host> setHosts = Host.getAll(conn);
    if (setHosts == null) {
        final String msg = "Unable to create iSCSI SR " + deviceConfig + " due to hosts not available.";
        s_logger.warn(msg);
        throw new CloudRuntimeException(msg);
    }
    for (final Host currentHost : setHosts) {
        final PBD.Record rec = new PBD.Record();
        rec.deviceConfig = deviceConfig;
        rec.host = currentHost;
        rec.SR = sr;
        final PBD pbd = PBD.create(conn, rec);
        pbd.plug(conn);
    }
    return sr;
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) SR(com.xensource.xenapi.SR)

Example 32 with PBD

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

the class CitrixResourceBase method prepareISO.

public void prepareISO(final Connection conn, final String vmName, List<String[]> vmDataList, String configDriveLabel) throws XmlRpcException, XenAPIException {
    final Set<VM> vms = VM.getByNameLabel(conn, vmName);
    if (vms == null || vms.size() != 1) {
        throw new CloudRuntimeException("There are " + (vms == null ? "0" : vms.size()) + " VMs named " + vmName);
    }
    final VM vm = vms.iterator().next();
    final Set<VBD> vbds = vm.getVBDs(conn);
    for (final VBD vbd : vbds) {
        final VBD.Record vbdr = vbd.getRecord(conn);
        if (vbdr.type == Types.VbdType.CD && vbdr.empty == false && vbdr.userdevice.equals(_attachIsoDeviceNum)) {
            final VDI vdi = vbdr.VDI;
            final SR sr = vdi.getSR(conn);
            final Set<PBD> pbds = sr.getPBDs(conn);
            if (pbds == null) {
                throw new CloudRuntimeException("There is no pbd for sr " + sr);
            }
            for (final PBD pbd : pbds) {
                final PBD.Record pbdr = pbd.getRecord(conn);
                if (pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                    return;
                }
            }
            sr.setShared(conn, true);
            final Host host = Host.getByUuid(conn, _host.getUuid());
            final PBD.Record pbdr = pbds.iterator().next().getRecord(conn);
            pbdr.host = host;
            pbdr.uuid = "";
            final PBD pbd = PBD.create(conn, pbdr);
            pbdPlug(conn, pbd, pbd.getUuid(conn));
            break;
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) Host(com.xensource.xenapi.Host) SR(com.xensource.xenapi.SR)

Example 33 with PBD

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

the class CitrixResourceBase method checkIfIscsiSrExisits.

private void checkIfIscsiSrExisits(Connection conn, String srNameLabel, String target, String targetiqn, String lunid) throws XenAPIException, XmlRpcException {
    final Set<SR> srs = SR.getByNameLabel(conn, srNameLabel);
    for (final SR sr : srs) {
        if (!(SRType.LVMOISCSI.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("target") == null) {
            continue;
        }
        if (dc.get("targetIQN") == null) {
            continue;
        }
        if (dc.get("lunid") == null) {
            continue;
        }
        if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN")) && lunid.equals(dc.get("lunid"))) {
            throw new CloudRuntimeException("There is a SR using the same configuration target:" + dc.get("target") + ",  targetIQN:" + dc.get("targetIQN") + ", lunid:" + dc.get("lunid") + " for pool " + srNameLabel + "on host:" + _host.getUuid());
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Example 34 with PBD

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

the class CitrixResourceBase method getAllLocalSrForType.

/**
 * This  method will return a list of all local SRs.
 * An SR is considered local if it meets all of the following criteria:
 * <ul>
 *  <li> {@link Record#shared} is equal to false
 *  <li> The PBDs of the SR ({@link Record#PBDs}) are connected to host {@link #_host}
 *  <li> SR type is equal to the {@link SRType} sent as parameter
 * </ul>
 */
protected List<SR> getAllLocalSrForType(Connection conn, SRType srType) throws XenAPIException, XmlRpcException {
    List<SR> localSrs = new ArrayList<>();
    Map<SR, SR.Record> allSrRecords = SR.getAllRecords(conn);
    if (MapUtils.isEmpty(allSrRecords)) {
        return localSrs;
    }
    for (Map.Entry<SR, SR.Record> entry : allSrRecords.entrySet()) {
        SR.Record srRec = entry.getValue();
        if (!srType.equals(srRec.type)) {
            continue;
        }
        if (BooleanUtils.toBoolean(srRec.shared)) {
            continue;
        }
        Set<PBD> pbds = srRec.PBDs;
        if (CollectionUtils.isEmpty(pbds)) {
            continue;
        }
        for (PBD pbd : pbds) {
            Host host = pbd.getHost(conn);
            if (!isRefNull(host) && StringUtils.equals(host.getUuid(conn), _host.getUuid())) {
                if (!pbd.getCurrentlyAttached(conn)) {
                    s_logger.debug(String.format("PBD [%s] of local SR [%s] was unplugged, pluggin it now", pbd.getUuid(conn), srRec.uuid));
                    pbd.plug(conn);
                }
                s_logger.debug("Scanning local SR: " + srRec.uuid);
                SR sr = entry.getKey();
                sr.scan(conn);
                localSrs.add(sr);
            }
        }
    }
    s_logger.debug(String.format("Found %d local storage of type [%s] for host [%s]", localSrs.size(), srType.toString(), _host.getUuid()));
    return localSrs;
}
Also used : PBD(com.xensource.xenapi.PBD) ArrayList(java.util.ArrayList) Host(com.xensource.xenapi.Host) Map(java.util.Map) HashMap(java.util.HashMap) SR(com.xensource.xenapi.SR)

Example 35 with PBD

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

the class CitrixResourceBase method getNfsSR.

public SR getNfsSR(final Connection conn, final String poolid, final String uuid, final String server, String serverpath, final String pooldesc) {
    final Map<String, String> deviceConfig = new HashMap<String, String>();
    try {
        serverpath = serverpath.replace("//", "/");
        final Set<SR> srs = SR.getAll(conn);
        if (srs != null && !srs.isEmpty()) {
            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 " + uuid + " on host:" + _host.getUuid());
                }
            }
        }
        deviceConfig.put("server", server);
        deviceConfig.put("serverpath", serverpath);
        final Host host = Host.getByUuid(conn, _host.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), uuid, poolid, SRType.NFS.toString(), "user", true, smConfig);
        sr.scan(conn);
        return sr;
    } catch (final XenAPIException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pooldesc, e);
    } catch (final XmlRpcException e) {
        throw new CloudRuntimeException("Unable to create NFS SR " + pooldesc, 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)

Aggregations

PBD (com.xensource.xenapi.PBD)40 SR (com.xensource.xenapi.SR)35 Host (com.xensource.xenapi.Host)24 XenAPIException (com.xensource.xenapi.Types.XenAPIException)22 XmlRpcException (org.apache.xmlrpc.XmlRpcException)21 HashMap (java.util.HashMap)14 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)9 VDI (com.xensource.xenapi.VDI)6 Test (org.junit.Test)6 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 IOException (java.io.IOException)5 MalformedURLException (java.net.MalformedURLException)5 URISyntaxException (java.net.URISyntaxException)5 Map (java.util.Map)5 TimeoutException (java.util.concurrent.TimeoutException)5 ConfigurationException (javax.naming.ConfigurationException)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 SAXException (org.xml.sax.SAXException)5 InternalErrorException (com.cloud.exception.InternalErrorException)4