Search in sources :

Example 11 with XsHost

use of com.cloud.hypervisor.xenserver.resource.XsHost in project cloudstack by apache.

the class NotAValidCommand method testSetupCommand.

@Test
public void testSetupCommand() {
    final XsHost xsHost = Mockito.mock(XsHost.class);
    final HostEnvironment env = Mockito.mock(HostEnvironment.class);
    final SetupCommand setupCommand = new SetupCommand(env);
    final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
    assertNotNull(wrapper);
    when(citrixResourceBase.getHost()).thenReturn(xsHost);
    final Answer answer = wrapper.execute(setupCommand, citrixResourceBase);
    verify(citrixResourceBase, times(1)).getConnection();
    assertFalse(answer.getResult());
}
Also used : RebootAnswer(com.cloud.agent.api.RebootAnswer) CreateAnswer(com.cloud.agent.api.storage.CreateAnswer) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) HostEnvironment(com.cloud.host.HostEnvironment) PvlanSetupCommand(com.cloud.agent.api.PvlanSetupCommand) SetupCommand(com.cloud.agent.api.SetupCommand) Test(org.junit.Test)

Example 12 with XsHost

use of com.cloud.hypervisor.xenserver.resource.XsHost 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 13 with XsHost

use of com.cloud.hypervisor.xenserver.resource.XsHost in project cloudstack by apache.

the class XenServer610MigrateWithStorageCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final List<Pair<VolumeTO, StorageFilerTO>> volumeToFiler = command.getVolumeToFilerAsList();
    final String vmName = vmSpec.getName();
    Task task = null;
    final XsHost xsHost = xenServer610Resource.getHost();
    final String uuid = xsHost.getUuid();
    try {
        xenServer610Resource.prepareISO(connection, vmName, null, null);
        // Get the list of networks and recreate VLAN, if required.
        for (final NicTO nicTo : vmSpec.getNics()) {
            xenServer610Resource.getNetwork(connection, nicTo);
        }
        final Map<String, String> other = new HashMap<String, String>();
        other.put("live", "true");
        final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
        final Network networkForSm = nativeNetworkForTraffic.getNetwork();
        // Create the vif map. The  vm stays in the same cluster so we have to pass an empty vif map.
        final Map<VIF, Network> vifMap = new HashMap<VIF, Network>();
        final Map<VDI, SR> vdiMap = new HashMap<VDI, SR>();
        for (final Pair<VolumeTO, StorageFilerTO> entry : volumeToFiler) {
            final StorageFilerTO storageFiler = entry.second();
            final VolumeTO volume = entry.first();
            vdiMap.put(xenServer610Resource.getVDIbyUuid(connection, volume.getPath()), xenServer610Resource.getStorageRepository(connection, storageFiler.getUuid()));
        }
        // Get the vm to migrate.
        final Set<VM> vms = VM.getByNameLabel(connection, vmSpec.getName());
        final VM vmToMigrate = vms.iterator().next();
        // Check migration with storage is possible.
        final Host host = Host.getByUuid(connection, uuid);
        final Map<String, String> token = host.migrateReceive(connection, networkForSm, other);
        task = vmToMigrate.assertCanMigrateAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while checking if vm " + vmName + " can be migrated to the destination host " + host, e);
            throw new CloudRuntimeException("Error while checking if vm " + vmName + " can be migrated to the " + "destination host " + host, e);
        }
        // Migrate now.
        task = vmToMigrate.migrateSendAsync(connection, token, true, vdiMap, vifMap, other);
        try {
            // poll every 1 seconds.
            final long timeout = xenServer610Resource.getMigrateWait() * 1000L;
            xenServer610Resource.waitForTask(connection, task, 1000, timeout);
            xenServer610Resource.checkForSuccess(connection, task);
        } catch (final Types.HandleInvalid e) {
            s_logger.error("Error while migrating vm " + vmName + " to the destination host " + host, e);
            throw new CloudRuntimeException("Error while migrating vm " + vmName + " to the destination host " + host, e);
        }
        // Volume paths would have changed. Return that information.
        final List<VolumeObjectTO> volumeToList = xenServer610Resource.getUpdatedVolumePathsOfMigratedVm(connection, vmToMigrate, vmSpec.getDisks());
        vmToMigrate.setAffinity(connection, host);
        return new MigrateWithStorageAnswer(command, volumeToList);
    } catch (final Exception e) {
        s_logger.warn("Catch Exception " + e.getClass().getName() + ". Storage motion failed due to " + e.toString(), e);
        return new MigrateWithStorageAnswer(command, e);
    } finally {
        if (task != null) {
            try {
                task.destroy(connection);
            } catch (final Exception e) {
                s_logger.debug("Unable to destroy task " + task.toString() + " on host " + uuid + " due to " + e.toString());
            }
        }
    }
}
Also used : Types(com.xensource.xenapi.Types) Task(com.xensource.xenapi.Task) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) HashMap(java.util.HashMap) StorageFilerTO(com.cloud.agent.api.to.StorageFilerTO) 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) Network(com.xensource.xenapi.Network) XsLocalNetwork(com.cloud.hypervisor.xenserver.resource.XsLocalNetwork) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) VDI(com.xensource.xenapi.VDI) Pair(com.cloud.utils.Pair) NicTO(com.cloud.agent.api.to.NicTO) SR(com.xensource.xenapi.SR) MigrateWithStorageAnswer(com.cloud.agent.api.MigrateWithStorageAnswer) Connection(com.xensource.xenapi.Connection) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM)

Example 14 with XsHost

use of com.cloud.hypervisor.xenserver.resource.XsHost in project cloudstack by apache.

the class XenServer610MigrateWithStorageCompleteCommandWrapper method execute.

@Override
public Answer execute(final MigrateWithStorageCompleteCommand command, final XenServer610Resource xenServer610Resource) {
    final Connection connection = xenServer610Resource.getConnection();
    final VirtualMachineTO vmSpec = command.getVirtualMachine();
    final String name = vmSpec.getName();
    try {
        final XsHost xsHost = xenServer610Resource.getHost();
        final String uuid = xsHost.getUuid();
        final Set<VM> vms = VM.getByNameLabel(connection, name);
        // Check if VMs can be found by label.
        if (vms == null) {
            throw new CloudRuntimeException("Couldn't find VMs by label " + name + " on the destination host.");
        }
        final VM migratedVm = vms.iterator().next();
        // Check the vm is present on the new host.
        if (migratedVm == null) {
            throw new CloudRuntimeException("Couldn't find the migrated vm " + name + " on the destination host.");
        }
        final Host host = Host.getByUuid(connection, uuid);
        migratedVm.setAffinity(connection, host);
        // Volume paths would have changed. Return that information.
        final List<VolumeObjectTO> volumeToSet = xenServer610Resource.getUpdatedVolumePathsOfMigratedVm(connection, migratedVm, vmSpec.getDisks());
        return new MigrateWithStorageCompleteAnswer(command, volumeToSet);
    } catch (final CloudRuntimeException e) {
        s_logger.error("Migration of vm " + name + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageCompleteAnswer(command, e);
    } catch (final Exception e) {
        s_logger.error("Migration of vm " + name + " with storage failed due to " + e.toString(), e);
        return new MigrateWithStorageCompleteAnswer(command, e);
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) VolumeObjectTO(org.apache.cloudstack.storage.to.VolumeObjectTO) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MigrateWithStorageCompleteAnswer(com.cloud.agent.api.MigrateWithStorageCompleteAnswer) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 15 with XsHost

use of com.cloud.hypervisor.xenserver.resource.XsHost in project cloudstack by apache.

the class CitrixMaintainCommandWrapper method execute.

@Override
public Answer execute(final MaintainCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    try {
        final XsHost xsHost = citrixResourceBase.getHost();
        final String uuid = xsHost.getUuid();
        final Host host = Host.getByUuid(conn, uuid);
        // remove all tags cloud stack
        final Host.Record hr = host.getRecord(conn);
        // Adding this check because could not get the mock to work. Will push the code and fix it afterwards.
        if (hr == null) {
            s_logger.warn("Host.Record is null.");
            return new MaintainAnswer(command, false, "Host.Record is null");
        }
        final Iterator<String> it = hr.tags.iterator();
        while (it.hasNext()) {
            final String tag = it.next();
            if (tag.contains("cloud")) {
                it.remove();
            }
        }
        host.setTags(conn, hr.tags);
        return new MaintainAnswer(command);
    } catch (final XenAPIException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    } catch (final XmlRpcException e) {
        s_logger.warn("Unable to put server in maintainence mode", e);
        return new MaintainAnswer(command, false, e.getMessage());
    }
}
Also used : XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) MaintainAnswer(com.cloud.agent.api.MaintainAnswer) Connection(com.xensource.xenapi.Connection) XenAPIException(com.xensource.xenapi.Types.XenAPIException) Host(com.xensource.xenapi.Host) XsHost(com.cloud.hypervisor.xenserver.resource.XsHost) XmlRpcException(org.apache.xmlrpc.XmlRpcException)

Aggregations

XsHost (com.cloud.hypervisor.xenserver.resource.XsHost)22 Answer (com.cloud.agent.api.Answer)18 Test (org.junit.Test)18 Connection (com.xensource.xenapi.Connection)15 RebootAnswer (com.cloud.agent.api.RebootAnswer)14 CreateAnswer (com.cloud.agent.api.storage.CreateAnswer)14 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)14 XenAPIException (com.xensource.xenapi.Types.XenAPIException)8 XmlRpcException (org.apache.xmlrpc.XmlRpcException)8 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)7 XsLocalNetwork (com.cloud.hypervisor.xenserver.resource.XsLocalNetwork)6 Host (com.xensource.xenapi.Host)6 Network (com.xensource.xenapi.Network)6 NicTO (com.cloud.agent.api.to.NicTO)4 VolumeTO (com.cloud.agent.api.to.VolumeTO)4 SR (com.xensource.xenapi.SR)4 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)4 StorageFilerTO (com.cloud.agent.api.to.StorageFilerTO)3 Pair (com.cloud.utils.Pair)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3