Search in sources :

Example 46 with SR

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

the class CitrixResourceBase method cleanupTemplateSR.

public void cleanupTemplateSR(final Connection conn) {
    Set<PBD> pbds = null;
    try {
        final Host host = Host.getByUuid(conn, _host.getUuid());
        pbds = host.getPBDs(conn);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to get the SRs " + e.toString(), e);
        throw new CloudRuntimeException("Unable to get SRs " + e.toString(), e);
    } catch (final Exception e) {
        throw new CloudRuntimeException("Unable to get SRs " + e.getMessage(), e);
    }
    for (final PBD pbd : pbds) {
        SR sr = null;
        SR.Record srRec = null;
        try {
            sr = pbd.getSR(conn);
            srRec = sr.getRecord(conn);
        } catch (final Exception e) {
            s_logger.warn("pbd.getSR get Exception due to ", e);
            continue;
        }
        final String type = srRec.type;
        if (srRec.shared) {
            continue;
        }
        if (SRType.NFS.equals(type) || SRType.ISO.equals(type) && srRec.nameDescription.contains("template")) {
            try {
                pbd.unplug(conn);
                pbd.destroy(conn);
                sr.forget(conn);
            } catch (final Exception e) {
                s_logger.warn("forget SR catch Exception due to ", e);
            }
        }
    }
}
Also used : PBD(com.xensource.xenapi.PBD) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) 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) SR(com.xensource.xenapi.SR)

Example 47 with SR

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

the class CitrixResourceBase method createPatchVbd.

public VBD createPatchVbd(final Connection conn, final String vmName, final VM vm) throws XmlRpcException, XenAPIException {
    if (_host.getSystemvmisouuid() == null) {
        final Set<SR> srs = SR.getByNameLabel(conn, "XenServer Tools");
        if (srs.size() != 1) {
            throw new CloudRuntimeException("There are " + srs.size() + " SRs with name XenServer Tools");
        }
        final SR sr = srs.iterator().next();
        sr.scan(conn);
        final SR.Record srr = sr.getRecord(conn);
        if (_host.getSystemvmisouuid() == null) {
            for (final VDI vdi : srr.VDIs) {
                final VDI.Record vdir = vdi.getRecord(conn);
                if (vdir.nameLabel.contains("systemvm.iso")) {
                    _host.setSystemvmisouuid(vdir.uuid);
                    break;
                }
            }
        }
        if (_host.getSystemvmisouuid() == null) {
            throw new CloudRuntimeException("can not find systemvmiso");
        }
    }
    final VBD.Record cdromVBDR = new VBD.Record();
    cdromVBDR.VM = vm;
    cdromVBDR.empty = true;
    cdromVBDR.bootable = false;
    cdromVBDR.userdevice = "3";
    cdromVBDR.mode = Types.VbdMode.RO;
    cdromVBDR.type = Types.VbdType.CD;
    final VBD cdromVBD = VBD.create(conn, cdromVBDR);
    cdromVBD.insert(conn, VDI.getByUuid(conn, _host.getSystemvmisouuid()));
    return cdromVBD;
}
Also used : CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) SR(com.xensource.xenapi.SR)

Example 48 with SR

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

the class CitrixResourceBase method getSRByNameLabelandHost.

protected SR getSRByNameLabelandHost(final Connection conn, final String name) throws BadServerResponse, XenAPIException, XmlRpcException {
    final Set<SR> srs = SR.getByNameLabel(conn, name);
    SR ressr = null;
    for (final SR sr : srs) {
        final Set<PBD> pbds;
        pbds = sr.getPBDs(conn);
        for (final PBD pbd : pbds) {
            final PBD.Record pbdr = pbd.getRecord(conn);
            if (pbdr.host != null && pbdr.host.getUuid(conn).equals(_host.getUuid())) {
                if (!pbdr.currentlyAttached) {
                    pbd.plug(conn);
                }
                ressr = sr;
                break;
            }
        }
    }
    return ressr;
}
Also used : PBD(com.xensource.xenapi.PBD) SR(com.xensource.xenapi.SR)

Example 49 with SR

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

the class CitrixResourceBase method mount.

protected VDI mount(final Connection conn, final String vmName, final DiskTO volume) throws XmlRpcException, XenAPIException {
    final DataTO data = volume.getData();
    final Volume.Type type = volume.getType();
    if (type == Volume.Type.ISO) {
        final TemplateObjectTO iso = (TemplateObjectTO) data;
        final DataStoreTO store = iso.getDataStore();
        if (store == null) {
            // It's a fake iso
            return null;
        }
        // corer case, xenserver pv driver iso
        final String templateName = iso.getName();
        if (templateName.startsWith("xs-tools")) {
            try {
                final Set<VDI> vdis = VDI.getByNameLabel(conn, templateName);
                if (vdis.isEmpty()) {
                    throw new CloudRuntimeException("Could not find ISO with URL: " + templateName);
                }
                return vdis.iterator().next();
            } catch (final XenAPIException e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            } catch (final Exception e) {
                throw new CloudRuntimeException("Unable to get pv iso: " + templateName + " due to " + e.toString());
            }
        }
        if (!(store instanceof NfsTO)) {
            throw new CloudRuntimeException("only support mount iso on nfs");
        }
        final NfsTO nfsStore = (NfsTO) store;
        final String isoPath = nfsStore.getUrl() + File.separator + iso.getPath();
        final int index = isoPath.lastIndexOf("/");
        final String mountpoint = isoPath.substring(0, index);
        final URI uri;
        try {
            uri = new URI(mountpoint);
        } catch (final URISyntaxException e) {
            throw new CloudRuntimeException("Incorrect uri " + mountpoint, e);
        }
        final SR isoSr = createIsoSRbyURI(conn, uri, vmName, false);
        final String isoname = isoPath.substring(index + 1);
        final VDI isoVdi = getVDIbyLocationandSR(conn, isoname, isoSr);
        if (isoVdi == null) {
            throw new CloudRuntimeException("Unable to find ISO " + isoPath);
        }
        return isoVdi;
    } else {
        final VolumeObjectTO vol = (VolumeObjectTO) data;
        return VDI.getByUuid(conn, vol.getPath());
    }
}
Also used : DataStoreTO(com.cloud.agent.api.to.DataStoreTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) URISyntaxException(java.net.URISyntaxException) NfsTO(com.cloud.agent.api.to.NfsTO) URI(java.net.URI) 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) DataTO(com.cloud.agent.api.to.DataTO) Volume(com.cloud.storage.Volume) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeObjectTO(com.cloud.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) TemplateObjectTO(com.cloud.storage.to.TemplateObjectTO) SR(com.xensource.xenapi.SR)

Example 50 with SR

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

the class CitrixResourceBase method createLocalIsoSR.

public SR createLocalIsoSR(final Connection conn, final String srName) throws XenAPIException, XmlRpcException {
    // if config drive sr already exists then return
    SR sr = getSRByNameLabelandHost(conn, _configDriveSRName + _host.getIp());
    if (sr != null) {
        s_logger.debug("Config drive SR already exist, returing it");
        return sr;
    }
    try {
        final Map<String, String> deviceConfig = new HashMap<>();
        final com.trilead.ssh2.Connection sshConnection = new com.trilead.ssh2.Connection(_host.getIp(), 22);
        try {
            sshConnection.connect(null, 60000, 60000);
            if (!sshConnection.authenticateWithPassword(_username, _password.peek())) {
                throw new CloudRuntimeException("Unable to authenticate");
            }
            final String cmd = "mkdir -p " + _configDriveIsopath;
            if (!SSHCmdHelper.sshExecuteCmd(sshConnection, cmd)) {
                throw new CloudRuntimeException("Cannot create directory configdrive_iso on XenServer hosts");
            }
        } catch (final IOException e) {
            throw new CloudRuntimeException("Unable to create iso folder", e);
        } finally {
            sshConnection.close();
        }
        s_logger.debug("Created the config drive SR " + srName + " folder path " + _configDriveIsopath);
        deviceConfig.put("location", _configDriveIsopath);
        deviceConfig.put("legacy_mode", "true");
        final Host host = Host.getByUuid(conn, _host.getUuid());
        final String type = SRType.ISO.toString();
        sr = SR.create(conn, host, deviceConfig, new Long(0), _configDriveIsopath, "iso", type, "iso", false, new HashMap<>());
        sr.setNameLabel(conn, srName);
        sr.setNameDescription(conn, deviceConfig.get("location"));
        sr.scan(conn);
        s_logger.debug("Config drive ISO SR at the path " + _configDriveIsopath + " got created in host " + _host);
        return sr;
    } catch (final XenAPIException e) {
        final String msg = "createLocalIsoSR failed! mountpoint " + e.toString();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    } catch (final Exception e) {
        final String msg = "createLocalIsoSR failed! mountpoint:  due to " + e.getMessage();
        s_logger.warn(msg, e);
        throw new CloudRuntimeException(msg, e);
    }
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) URLConnection(java.net.URLConnection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) IOException(java.io.IOException) 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) 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