Search in sources :

Example 26 with PBD

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

the class CitrixResourceBase method checkSR.

protected boolean checkSR(final Connection conn, final SR sr) {
    try {
        final SR.Record srr = sr.getRecord(conn);
        final Set<PBD> pbds = sr.getPBDs(conn);
        if (pbds.size() == 0) {
            final String msg = "There is no PBDs for this SR: " + srr.nameLabel + " on host:" + _host.getUuid();
            s_logger.warn(msg);
            return false;
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Checking " + srr.nameLabel + " or SR " + srr.uuid + " on " + _host);
        }
        if (srr.shared) {
            if (SRType.NFS.equals(srr.type)) {
                final Map<String, String> smConfig = srr.smConfig;
                if (!smConfig.containsKey("nosubdir")) {
                    smConfig.put("nosubdir", "true");
                    sr.setSmConfig(conn, smConfig);
                }
            }
            final Host host = Host.getByUuid(conn, _host.getUuid());
            boolean found = false;
            for (final PBD pbd : pbds) {
                final PBD.Record pbdr = pbd.getRecord(conn);
                if (host.equals(pbdr.host)) {
                    if (!pbdr.currentlyAttached) {
                        pbdPlug(conn, pbd, pbdr.uuid);
                    }
                    found = true;
                    break;
                }
            }
            if (!found) {
                final PBD.Record pbdr = srr.PBDs.iterator().next().getRecord(conn);
                pbdr.host = host;
                pbdr.uuid = "";
                final PBD pbd = PBD.create(conn, pbdr);
                pbdPlug(conn, pbd, pbd.getUuid(conn));
            }
        } else {
            for (final PBD pbd : pbds) {
                final PBD.Record pbdr = pbd.getRecord(conn);
                if (!pbdr.currentlyAttached) {
                    pbdPlug(conn, pbd, pbdr.uuid);
                }
            }
        }
    } catch (final Exception e) {
        final String msg = "checkSR failed host:" + _host + " due to " + e.toString();
        s_logger.warn(msg, e);
        return false;
    }
    return true;
}
Also used : PBD(com.xensource.xenapi.PBD) 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 27 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 28 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 29 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 30 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)

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