Search in sources :

Example 21 with Host

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

the class XenServer610MigrateWithStorageReceiveCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageReceiveCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, String>> volumeToStorageUuid = command.getVolumeToStorageUuid();
    try {
        // In a cluster management server setup, the migrate with storage receive and send
        // commands and answers may have to be forwarded to another management server. This
        // happens when the host/resource on which the command has to be executed is owned
        // by the second management server. The serialization/deserialization of the command
        // and answers fails as the xapi SR and Network class type isn't understand by the
        // agent attache. Seriliaze the SR and Network objects here to a string and pass in
        // the answer object. It'll be deserialzed and object created in migrate with
        // storage send command execution.
        Gson gson = new Gson();
        // Get a map of all the SRs to which the vdis will be migrated.
        final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<>();
        for (final Pair<VolumeTO, String> entry : volumeToStorageUuid) {
            final String storageUuid = entry.second();
            final SR sr = xenServer610Resource.getStorageRepository(connection, storageUuid);
            volumeToSr.add(new Pair<VolumeTO, Object>(entry.first(), sr));
        }
        // Get the list of networks to which the vifs will attach.
        final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
        for (final NicTO nicTo : vmSpec.getNics()) {
            final Network network = xenServer610Resource.getNetwork(connection, nicTo);
            nicToNetwork.add(new Pair<NicTO, Object>(nicTo, network));
        }
        final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
        final Network network = nativeNetworkForTraffic.getNetwork();
        final XsHost xsHost = xenServer610Resource.getHost();
        final String uuid = xsHost.getUuid();
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        final Host host = Host.getByUuid(connection, uuid);
        final Map<String, String> token = host.migrateReceive(connection, network, other);
        return new MigrateWithStorageReceiveAnswer(command, volumeToSr, nicToNetwork, token);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageReceiveAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageReceiveAnswer(command, e);
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) VolumeTO(com.cloud.agent.api.to.VolumeTO) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) Network(com.xensource.xenapi.Network) Pair(com.cloud.utils.Pair) SR(com.xensource.xenapi.SR) NicTO(com.cloud.agent.api.to.NicTO) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) MigrateWithStorageReceiveAnswer(com.cloud.agent.api.MigrateWithStorageReceiveAnswer)

Example 22 with Host

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

the class CitrixMigrateCommandWrapper method execute.

@Override
public Answer execute(final MigrateCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    final String dstHostIpAddr = command.getDestinationIp();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        final Set<Host> hosts = Host.getAll(conn);
        Host dsthost = null;
        if (hosts != null) {
            for (final Host host : hosts) {
                if (host.getAddress(conn).equals(dstHostIpAddr)) {
                    dsthost = host;
                    break;
                }
            }
        }
        if (dsthost == null) {
            final String msg = "Migration failed due to unable to find host " + dstHostIpAddr + " in XenServer pool " + citrixResourceBase.getHost().getPool();
            s_logger.warn(msg);
            return new MigrateAnswer(command, false, msg, null);
        }
        for (final VM vm : vms) {
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VBD.Record vbdRec = vbd.getRecord(conn);
                if (vbdRec.type.equals(Types.VbdType.CD) && !vbdRec.empty) {
                    vbd.eject(conn);
                    // for config drive vbd destroy the vbd.
                    if (!vbdRec.userdevice.equals(citrixResourceBase._attachIsoDeviceNum)) {
                        if (vbdRec.currentlyAttached) {
                            vbd.destroy(conn);
                        }
                    }
                    continue;
                }
            }
            citrixResourceBase.migrateVM(conn, dsthost, vm, vmName);
            vm.setAffinity(conn, dsthost);
        }
        // Attach the config drive iso device to VM
        if (!citrixResourceBase.attachConfigDriveToMigratedVm(conn, vmName, dstHostIpAddr)) {
            s_logger.debug("Config drive ISO attach failed after migration for vm " + vmName);
        }
        return new MigrateAnswer(command, true, "migration succeeded", null);
    } catch (final Exception e) {
        s_logger.warn(e.getMessage(), e);
        return new MigrateAnswer(command, false, e.getMessage(), null);
    }
}
Also used : MigrateAnswer(com.cloud.agent.api.MigrateAnswer) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VBD(com.xensource.xenapi.VBD) Host(com.xensource.xenapi.Host)

Example 23 with Host

use of com.xensource.xenapi.Host in project OpenAttestation by OpenAttestation.

the class CitrixClient method getQuoteInformationForHost.

public HashMap<String, PcrManifest> getQuoteInformationForHost(String pcrList) {
    log.debug("getQuoteInformationForHost pcrList == " + pcrList);
    try {
        // We cannot reuse the connections across different calls since they are tied to a particular host.
        if (!isConnected()) {
            connect();
        }
        String nonce = generateNonce();
        String sessionId = generateSessionId();
        // We do not need to connect again. So, commenting it out.
        // System.err.println("stdalex-error connecting with " + userName + " " + password);
        // Session.loginWithPassword(connection, userName, password, APIVersion.latest().toString());
        // System.err.println( "CitrixClient: connected to server ["+hostIpAddress+"]");	
        Map<String, String> myMap = new HashMap<String, String>();
        Set<Host> hostList = Host.getAll(connection);
        Iterator iter = hostList.iterator();
        // hasNext() will always be valid otherwise we will get an exception from the getAll method. So, we not need
        // to throw an exception if the hasNext is false.
        Host h = null;
        if (iter.hasNext()) {
            h = (Host) iter.next();
        }
        String aik = h.callPlugin(connection, "tpm", "tpm_get_attestation_identity", myMap);
        int startP = aik.indexOf("<xentxt:TPM_Attestation_KEY_PEM>");
        int endP = aik.indexOf("</xentxt:TPM_Attestation_KEY_PEM>");
        // 32 is the size of the opening tag  <xentxt:TPM_Attestation_KEY_PEM>
        String cert = aik.substring(startP + "<xentxt:TPM_Attestation_KEY_PEM>".length(), endP);
        keys key = new keys();
        // This is the actual value for AIK!!!!!
        key.tpmAttKeyPEM = cert;
        String aikCertificate = key.tpmAttKeyPEM;
        myMap = new HashMap<String, String>();
        myMap.put("nonce", nonce);
        String quote = h.callPlugin(connection, "tpm", "tpm_get_quote", myMap);
        log.debug("extracted quote from response: " + quote);
        //saveFile(getCertFileName(sessionId), Base64.decodeBase64(aikCertificate));
        saveFile(getCertFileName(sessionId), aikCertificate.getBytes());
        log.debug("saved certificate with session id: " + sessionId);
        saveQuote(quote, sessionId);
        log.debug("saved quote with session id: " + sessionId);
        saveNonce(nonce, sessionId);
        log.debug("saved nonce with session id: " + sessionId);
        //createRSAKeyFile(sessionId);
        log.debug("created RSA key file for session id: " + sessionId);
        HashMap<String, PcrManifest> pcrMap = verifyQuoteAndGetPcr(sessionId, pcrList);
        log.debug("Got PCR map");
        return pcrMap;
    } catch (ASException e) {
        throw e;
    //        } catch(UnknownHostException e) {
    //            throw new ASException(e,ErrorCode.AS_HOST_COMMUNICATION_ERROR, hostIpAddress);
    } catch (Exception e) {
        log.debug("caught exception during login: " + e.toString() + " class: " + e.getClass());
        throw new ASException(e, ErrorCode.AS_CITRIX_ERROR, e.toString());
    }
}
Also used : HashMap(java.util.HashMap) Host(com.xensource.xenapi.Host) ASException(com.intel.mountwilson.as.common.ASException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PcrManifest(com.intel.mountwilson.manifest.data.PcrManifest) Iterator(java.util.Iterator) ASException(com.intel.mountwilson.as.common.ASException)

Example 24 with Host

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

the class CitrixResourceBase method getHostVmStateReport.

protected HashMap<String, HostVmStateReportEntry> getHostVmStateReport(final Connection conn) {
    // TODO : new VM sync model does not require a cluster-scope report, we
    // need to optimize
    // the report accordingly
    final HashMap<String, HostVmStateReportEntry> vmStates = new HashMap<String, HostVmStateReportEntry>();
    Map<VM, VM.Record> vm_map = null;
    for (int i = 0; i < 2; i++) {
        try {
            // USE THIS TO GET ALL VMS FROM
            vm_map = VM.getAllRecords(conn);
            // A CLUSTER
            break;
        } catch (final Throwable e) {
            s_logger.warn("Unable to get vms", e);
        }
        try {
            Thread.sleep(1000);
        } catch (final InterruptedException ex) {
        }
    }
    if (vm_map == null) {
        return vmStates;
    }
    for (final VM.Record record : vm_map.values()) {
        if (record.isControlDomain || record.isASnapshot || record.isATemplate) {
            // Skip DOM0
            continue;
        }
        final VmPowerState ps = record.powerState;
        final Host host = record.residentOn;
        String host_uuid = null;
        if (!isRefNull(host)) {
            try {
                host_uuid = host.getUuid(conn);
            } catch (final BadServerResponse e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            } catch (final XenAPIException e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            } catch (final XmlRpcException e) {
                s_logger.error("Failed to get host uuid for host " + host.toWireString(), e);
            }
            if (host_uuid.equalsIgnoreCase(_host.getUuid())) {
                vmStates.put(record.nameLabel, new HostVmStateReportEntry(convertToPowerState(ps), host_uuid));
            }
        }
    }
    return vmStates;
}
Also used : BadServerResponse(com.xensource.xenapi.Types.BadServerResponse) HostVmStateReportEntry(com.cloud.agent.api.HostVmStateReportEntry) HashMap(java.util.HashMap) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) VM(com.xensource.xenapi.VM) VmPowerState(com.xensource.xenapi.Types.VmPowerState) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Example 25 with Host

use of com.xensource.xenapi.Host 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

Host (com.xensource.xenapi.Host)47 XenAPIException (com.xensource.xenapi.Types.XenAPIException)35 XmlRpcException (org.apache.xmlrpc.XmlRpcException)33 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)32 HashMap (java.util.HashMap)24 Connection (com.xensource.xenapi.Connection)17 SR (com.xensource.xenapi.SR)17 IOException (java.io.IOException)15 ConfigurationException (javax.naming.ConfigurationException)14 InternalErrorException (com.cloud.exception.InternalErrorException)13 MalformedURLException (java.net.MalformedURLException)13 URISyntaxException (java.net.URISyntaxException)12 TimeoutException (java.util.concurrent.TimeoutException)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 SAXException (org.xml.sax.SAXException)12 VM (com.xensource.xenapi.VM)11 PBD (com.xensource.xenapi.PBD)10 XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)7 Map (java.util.Map)7 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)6