Search in sources :

Example 61 with Connection

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

the class XenServer56FP1FenceCommandWrapper method execute.

@Override
public Answer execute(final FenceCommand command, final XenServer56Resource xenServer56) {
    final Connection conn = xenServer56.getConnection();
    try {
        final Boolean alive = xenServer56.checkHeartbeat(command.getHostGuid());
        if (alive == null) {
            s_logger.debug("Failed to check heartbeat,  so unable to fence");
            return new FenceAnswer(command, false, "Failed to check heartbeat, so unable to fence");
        }
        if (alive) {
            s_logger.debug("Heart beat is still going so unable to fence");
            return new FenceAnswer(command, false, "Heartbeat is still going on unable to fence");
        }
        final Set<VM> vms = VM.getByNameLabel(conn, command.getVmName());
        for (final VM vm : vms) {
            final Set<VDI> vdis = new HashSet<VDI>();
            final Set<VBD> vbds = vm.getVBDs(conn);
            for (final VBD vbd : vbds) {
                final VDI vdi = vbd.getVDI(conn);
                if (!xenServer56.isRefNull(vdi)) {
                    vdis.add(vdi);
                }
            }
            s_logger.info("Fence command for VM " + command.getVmName());
            vm.powerStateReset(conn);
            vm.destroy(conn);
            for (final VDI vdi : vdis) {
                final Map<String, String> smConfig = vdi.getSmConfig(conn);
                for (final String key : smConfig.keySet()) {
                    if (key.startsWith("host_")) {
                        vdi.removeFromSmConfig(conn, key);
                        break;
                    }
                }
            }
        }
        return new FenceAnswer(command);
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    } catch (final Exception e) {
        s_logger.warn("Unable to fence", e);
        return new FenceAnswer(command, false, e.getMessage());
    }
}
Also used : Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) FenceAnswer(com.cloud.agent.api.FenceAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VM(com.xensource.xenapi.VM) VBD(com.xensource.xenapi.VBD) VDI(com.xensource.xenapi.VDI) XmlRpcException(org.apache.xmlrpc.XmlRpcException) HashSet(java.util.HashSet)

Example 62 with Connection

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

the class XenServer610MigrateVolumeCommandWrapper method execute.

@Override
public Answer execute(final MigrateVolumeCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final String volumeUUID = command.getVolumePath();
    final StorageFilerTO poolTO = command.getPool();
    try {
        final String uuid = poolTO.getUuid();
        final SR destinationPool = xenServer610Resource.getStorageRepository(connection, uuid);
        final VDI srcVolume = xenServer610Resource.getVDIbyUuid(connection, volumeUUID);
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        // Live migrate the vdi across pool.
        final Task task = srcVolume.poolMigrateAsync(connection, destinationPool, other);
        final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
        xenServer610Resource.waitForTask(connection, task, 1000, timeout);
        xenServer610Resource.checkForSuccess(connection, task);
        final VDI dvdi = Types.toVDI(task, connection);
        return new MigrateVolumeAnswer(command, true, null, dvdi.getUuid(connection));
    } catch (final Exception e) {
        final String msg = "Catch Exception " + e.getClass().getName() + " due to " + e.toString();
        s_logger.error(msg, e);
        return new MigrateVolumeAnswer(command, false, msg, null);
    }
}
Also used : Task(com.xensource.xenapi.Task) HashMap(java.util.HashMap) MigrateVolumeAnswer(com.cloud.agent.api.storage.MigrateVolumeAnswer) Connection(com.xensource.xenapi.Connection) VDI(com.xensource.xenapi.VDI) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) SR(com.xensource.xenapi.SR)

Example 63 with Connection

use of com.xensource.xenapi.Connection 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 64 with Connection

use of com.xensource.xenapi.Connection 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 65 with Connection

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

the class CitrixModifyStoragePoolCommandWrapper method execute.

@Override
public Answer execute(final ModifyStoragePoolCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final StorageFilerTO pool = command.getPool();
    final boolean add = command.getAdd();
    if (add) {
        try {
            final String srName = command.getStoragePath() != null ? command.getStoragePath() : pool.getUuid();
            final SR sr = citrixResourceBase.getStorageRepository(conn, srName);
            citrixResourceBase.setupHeartbeatSr(conn, sr, false);
            final long capacity = sr.getPhysicalSize(conn);
            final long available = capacity - sr.getPhysicalUtilisation(conn);
            if (capacity == -1) {
                final String msg = "Pool capacity is -1! pool: " + pool.getHost() + pool.getPath();
                s_logger.warn(msg);
                return new Answer(command, false, msg);
            }
            final Map<String, TemplateProp> tInfo = new HashMap<String, TemplateProp>();
            final ModifyStoragePoolAnswer answer = new ModifyStoragePoolAnswer(command, capacity, available, tInfo);
            return answer;
        } catch (final XenAPIException e) {
            final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        } catch (final Exception e) {
            final String msg = "ModifyStoragePoolCommand add XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    } else {
        try {
            final SR sr = citrixResourceBase.getStorageRepository(conn, pool.getUuid());
            final String srUuid = sr.getUuid(conn);
            final String result = citrixResourceBase.callHostPluginPremium(conn, "setup_heartbeat_file", "host", citrixResourceBase.getHost().getUuid(), "sr", srUuid, "add", "false");
            if (result == null || !result.split("#")[1].equals("0")) {
                throw new CloudRuntimeException("Unable to remove heartbeat file entry for SR " + srUuid + " due to " + result);
            }
            return new Answer(command, true, "success");
        } catch (final XenAPIException e) {
            final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.toString() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        } catch (final Exception e) {
            final String msg = "ModifyStoragePoolCommand remove XenAPIException:" + e.getMessage() + " host:" + citrixResourceBase.getHost().getUuid() + " pool: " + pool.getHost() + pool.getPath();
            s_logger.warn(msg, e);
            return new Answer(command, false, msg);
        }
    }
}
Also used : TemplateProp(com.cloud.storage.template.TemplateProp) HashMap(java.util.HashMap) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) XenAPIException(com.xensource.xenapi.Types.XenAPIException) ModifyStoragePoolAnswer(com.cloud.agent.api.ModifyStoragePoolAnswer) Answer(com.cloud.agent.api.Answer) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) SR(com.xensource.xenapi.SR)

Aggregations

Connection (com.xensource.xenapi.Connection)165 XenAPIException (com.xensource.xenapi.Types.XenAPIException)88 XmlRpcException (org.apache.xmlrpc.XmlRpcException)86 Answer (com.cloud.agent.api.Answer)79 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)58 Test (org.junit.Test)53 VDI (com.xensource.xenapi.VDI)47 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)46 SR (com.xensource.xenapi.SR)42 InternalErrorException (com.cloud.exception.InternalErrorException)39 RebootAnswer (com.cloud.agent.api.RebootAnswer)38 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)38 Network (com.xensource.xenapi.Network)35 VM (com.xensource.xenapi.VM)32 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)23 HashMap (java.util.HashMap)23 CopyCmdAnswer (org.apache.cloudstack.storage.command.CopyCmdAnswer)23 BadServerResponse (com.xensource.xenapi.Types.BadServerResponse)20 Host (com.xensource.xenapi.Host)19 URI (java.net.URI)19