Search in sources :

Example 41 with SR

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

the class Xenserver625StorageProcessorTest method retrieveAlreadyConfiguredSrWithoutExceptionMethodWorking.

@Test
public void retrieveAlreadyConfiguredSrWithoutExceptionMethodWorking() throws XenAPIException, XmlRpcException {
    SR srMock = Mockito.mock(SR.class);
    Mockito.doReturn(srMock).when(xenserver625StorageProcessor).retrieveAlreadyConfiguredSr(connectionMock, pathMock);
    SR sr = xenserver625StorageProcessor.retrieveAlreadyConfiguredSrWithoutException(connectionMock, pathMock);
    Mockito.verify(xenserver625StorageProcessor).retrieveAlreadyConfiguredSr(connectionMock, pathMock);
    Assert.assertEquals(srMock, sr);
}
Also used : SR(com.xensource.xenapi.SR) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 42 with SR

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

the class CitrixResourceBase method handleSrAndVdiDetach.

public void handleSrAndVdiDetach(final String iqn, final Connection conn) throws Exception {
    final SR sr = getStorageRepository(conn, iqn);
    removeSR(conn, sr);
}
Also used : SR(com.xensource.xenapi.SR)

Example 43 with SR

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

the class CitrixResourceBase method setupHeartbeatSr.

public String setupHeartbeatSr(final Connection conn, final SR sr, final boolean force) throws XenAPIException, XmlRpcException {
    final SR.Record srRec = sr.getRecord(conn);
    final String srUuid = srRec.uuid;
    if (!srRec.shared || !SRType.LVMOHBA.equals(srRec.type) && !SRType.LVMOISCSI.equals(srRec.type) && !SRType.NFS.equals(srRec.type)) {
        return srUuid;
    }
    String result = null;
    final Host host = Host.getByUuid(conn, _host.getUuid());
    final Set<String> tags = host.getTags(conn);
    if (force || !tags.contains("cloud-heartbeat-" + srUuid)) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Setting up the heartbeat sr for host " + _host.getIp() + " and sr " + srUuid);
        }
        final Set<PBD> pbds = sr.getPBDs(conn);
        for (final PBD pbd : pbds) {
            final PBD.Record pbdr = pbd.getRecord(conn);
            if (!pbdr.currentlyAttached && pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                pbd.plug(conn);
                break;
            }
        }
        result = callHostPluginThroughMaster(conn, "vmopspremium", "setup_heartbeat_sr", "host", _host.getUuid(), "sr", srUuid);
        if (result == null || !result.split("#")[1].equals("0")) {
            throw new CloudRuntimeException("Unable to setup heartbeat sr on SR " + srUuid + " due to " + result);
        }
        if (!tags.contains("cloud-heartbeat-" + srUuid)) {
            tags.add("cloud-heartbeat-" + srUuid);
            host.setTags(conn, tags);
        }
    }
    result = callHostPluginPremium(conn, "setup_heartbeat_file", "host", _host.getUuid(), "sr", srUuid, "add", "true");
    if (result == null || !result.split("#")[1].equals("0")) {
        throw new CloudRuntimeException("Unable to setup heartbeat file entry on SR " + srUuid + " due to " + result);
    }
    return srUuid;
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Host(com.xensource.xenapi.Host) SR(com.xensource.xenapi.SR)

Example 44 with SR

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

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) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SR(com.xensource.xenapi.SR)

Example 45 with SR

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

the class CitrixResourceBase method getIscsiSR.

public SR getIscsiSR(final Connection conn, final String srNameLabel, final String target, String path, final String chapInitiatorUsername, final String chapInitiatorPassword, final boolean ignoreIntroduceException) {
    synchronized (srNameLabel.intern()) {
        final Map<String, String> deviceConfig = new HashMap<>();
        try {
            if (path.endsWith("/")) {
                path = path.substring(0, path.length() - 1);
            }
            final String[] tmp = path.split("/");
            if (tmp.length != 3) {
                final String msg = "Wrong iscsi path " + path + " it should be /targetIQN/LUN";
                s_logger.warn(msg);
                throw new CloudRuntimeException(msg);
            }
            final String targetiqn = tmp[1].trim();
            final String lunid = tmp[2].trim();
            String scsiid = "";
            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());
                }
            }
            deviceConfig.put("target", target);
            deviceConfig.put("targetIQN", targetiqn);
            if (StringUtils.isNotBlank(chapInitiatorUsername) && StringUtils.isNotBlank(chapInitiatorPassword)) {
                deviceConfig.put("chapuser", chapInitiatorUsername);
                deviceConfig.put("chappassword", chapInitiatorPassword);
            }
            final Host host = Host.getByUuid(conn, _host.getUuid());
            final Map<String, String> smConfig = new HashMap<>();
            final String type = SRType.LVMOISCSI.toString();
            SR sr = null;
            try {
                sr = SR.create(conn, host, deviceConfig, new Long(0), srNameLabel, srNameLabel, type, "user", true, smConfig);
            } catch (final XenAPIException e) {
                final String errmsg = e.toString();
                if (errmsg.contains("SR_BACKEND_FAILURE_107")) {
                    final String[] lun = errmsg.split("<LUN>");
                    boolean found = false;
                    for (int i = 1; i < lun.length; i++) {
                        final int blunindex = lun[i].indexOf("<LUNid>") + 7;
                        final int elunindex = lun[i].indexOf("</LUNid>");
                        String ilun = lun[i].substring(blunindex, elunindex);
                        ilun = ilun.trim();
                        if (ilun.equals(lunid)) {
                            final int bscsiindex = lun[i].indexOf("<SCSIid>") + 8;
                            final int escsiindex = lun[i].indexOf("</SCSIid>");
                            scsiid = lun[i].substring(bscsiindex, escsiindex);
                            scsiid = scsiid.trim();
                            found = true;
                            break;
                        }
                    }
                    if (!found) {
                        final String msg = "can not find LUN " + lunid + " in " + errmsg;
                        s_logger.warn(msg);
                        throw new CloudRuntimeException(msg);
                    }
                } else {
                    final String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.toString();
                    s_logger.warn(msg, e);
                    throw new CloudRuntimeException(msg, e);
                }
            }
            deviceConfig.put("SCSIid", scsiid);
            final String result = SR.probe(conn, host, deviceConfig, type, smConfig);
            String pooluuid = null;
            if (result.indexOf("<UUID>") != -1) {
                pooluuid = result.substring(result.indexOf("<UUID>") + 6, result.indexOf("</UUID>")).trim();
            }
            if (pooluuid == null || pooluuid.length() != 36) {
                sr = SR.create(conn, host, deviceConfig, new Long(0), srNameLabel, srNameLabel, type, "user", true, smConfig);
            } else {
                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);
                }
            }
            sr.scan(conn);
            return sr;
        } catch (final XenAPIException e) {
            final String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.toString();
            s_logger.warn(msg, e);
            throw new CloudRuntimeException(msg, e);
        } catch (final Exception e) {
            final String msg = "Unable to create Iscsi SR  " + deviceConfig + " due to  " + e.getMessage();
            s_logger.warn(msg, e);
            throw new CloudRuntimeException(msg, e);
        }
    }
}
Also used : HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) 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) ConfigurationException(javax.naming.ConfigurationException) MalformedURLException(java.net.MalformedURLException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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