Search in sources :

Example 6 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class OvmResourceBase method execute.

protected PrepareForMigrationAnswer execute(PrepareForMigrationCommand cmd) {
    VirtualMachineTO vm = cmd.getVirtualMachine();
    if (s_logger.isDebugEnabled()) {
        s_logger.debug("Preparing host for migrating " + vm);
    }
    NicTO[] nics = vm.getNics();
    try {
        for (NicTO nic : nics) {
            getNetwork(nic);
        }
        return new PrepareForMigrationAnswer(cmd);
    } catch (Exception e) {
        s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e);
        return new PrepareForMigrationAnswer(cmd, e);
    }
}
Also used : VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException) NicTO(com.cloud.agent.api.to.NicTO) PrepareForMigrationAnswer(com.cloud.agent.api.PrepareForMigrationAnswer)

Example 7 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class OvmResourceBase method execute.

@Override
public synchronized StartAnswer execute(StartCommand cmd) {
    VirtualMachineTO vmSpec = cmd.getVirtualMachine();
    String vmName = vmSpec.getName();
    OvmVm.Details vmDetails = null;
    try {
        vmDetails = new OvmVm.Details();
        applySpecToVm(vmDetails, vmSpec);
        createVbds(vmDetails, vmSpec);
        createVifs(vmDetails, vmSpec);
        startVm(vmDetails);
        // Add security group rules
        NicTO[] nics = vmSpec.getNics();
        for (NicTO nic : nics) {
            if (nic.isSecurityGroupEnabled()) {
                if (vmSpec.getType().equals(VirtualMachine.Type.User)) {
                    defaultNetworkRulesForUserVm(vmName, vmSpec.getId(), nic);
                }
            }
        }
        return new StartAnswer(cmd);
    } catch (Exception e) {
        s_logger.debug("Start vm " + vmName + " failed", e);
        cleanup(vmDetails);
        return new StartAnswer(cmd, e.getMessage());
    }
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) OvmVm(com.cloud.ovm.object.OvmVm) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException) NicTO(com.cloud.agent.api.to.NicTO)

Example 8 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class LibvirtComputingResourceTest method testPrepareForMigrationCommandInternalErrorException.

@SuppressWarnings("unchecked")
@Test
public void testPrepareForMigrationCommandInternalErrorException() {
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final VirtualMachineTO vm = Mockito.mock(VirtualMachineTO.class);
    final KVMStoragePoolManager storagePoolManager = Mockito.mock(KVMStoragePoolManager.class);
    final NicTO nicTO = Mockito.mock(NicTO.class);
    final DiskTO volume = Mockito.mock(DiskTO.class);
    final PrepareForMigrationCommand command = new PrepareForMigrationCommand(vm);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(vm.getName())).thenReturn(conn);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    when(vm.getNics()).thenReturn(new NicTO[] { nicTO });
    when(nicTO.getType()).thenReturn(TrafficType.Guest);
    when(libvirtComputingResource.getVifDriver(nicTO.getType())).thenThrow(InternalErrorException.class);
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolManager);
    try {
        when(libvirtComputingResource.getVolumePath(conn, volume)).thenReturn("/path");
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(vm.getName());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(vm, times(1)).getNics();
}
Also used : AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) LibvirtException(org.libvirt.LibvirtException) PrepareForMigrationCommand(com.cloud.agent.api.PrepareForMigrationCommand) Connect(org.libvirt.Connect) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) NicTO(com.cloud.agent.api.to.NicTO) DiskTO(com.cloud.agent.api.to.DiskTO) Test(org.junit.Test)

Example 9 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class LibvirtComputingResourceTest method testStartCommandFailedConnect.

@Test
public void testStartCommandFailedConnect() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVMDef vmDef = Mockito.mock(LibvirtVMDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    final String vmName = "Test";
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachine.Type.DomainRouter);
    when(vmSpec.getName()).thenReturn(vmName);
    when(libvirtComputingResource.createVMFromSpec(vmSpec)).thenReturn(vmDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        doNothing().when(libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(false);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertFalse(answer.getResult());
    verify(libvirtComputingResource, times(1)).getStoragePoolMgr();
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByType(vmDef.getHvsType());
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.agent.api.StartCommand) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Example 10 with VirtualMachineTO

use of com.cloud.agent.api.to.VirtualMachineTO in project cloudstack by apache.

the class LibvirtComputingResourceTest method testStartCommandHostMemory.

@Test
public void testStartCommandHostMemory() {
    final VirtualMachineTO vmSpec = Mockito.mock(VirtualMachineTO.class);
    final com.cloud.host.Host host = Mockito.mock(com.cloud.host.Host.class);
    final boolean executeInSequence = false;
    final StartCommand command = new StartCommand(vmSpec, host, executeInSequence);
    final KVMStoragePoolManager storagePoolMgr = Mockito.mock(KVMStoragePoolManager.class);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final LibvirtVMDef vmDef = Mockito.mock(LibvirtVMDef.class);
    final NicTO nic = Mockito.mock(NicTO.class);
    final NicTO[] nics = new NicTO[] { nic };
    int vmId = 1;
    final int[] vms = new int[] { vmId };
    final Domain dm = Mockito.mock(Domain.class);
    final String vmName = "Test";
    when(libvirtComputingResource.getStoragePoolMgr()).thenReturn(storagePoolMgr);
    when(vmSpec.getNics()).thenReturn(nics);
    when(vmSpec.getType()).thenReturn(VirtualMachine.Type.User);
    when(vmSpec.getName()).thenReturn(vmName);
    when(vmSpec.getMaxRam()).thenReturn(512L);
    when(libvirtComputingResource.createVMFromSpec(vmSpec)).thenReturn(vmDef);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    try {
        when(libvirtUtilitiesHelper.getConnectionByType(vmDef.getHvsType())).thenReturn(conn);
        when(conn.listDomains()).thenReturn(vms);
        when(conn.domainLookupByID(vmId)).thenReturn(dm);
        when(dm.getMaxMemory()).thenReturn(1024L);
        when(dm.getName()).thenReturn(vmName);
        when(libvirtComputingResource.getTotalMemory()).thenReturn(2048 * 1024L);
        doNothing().when(libvirtComputingResource).createVbd(conn, vmSpec, vmName, vmDef);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    } catch (final InternalErrorException e) {
        fail(e.getMessage());
    } catch (final URISyntaxException e) {
        fail(e.getMessage());
    }
    when(storagePoolMgr.connectPhysicalDisksViaVmSpec(vmSpec)).thenReturn(true);
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
}
Also used : KVMStoragePoolManager(com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager) LibvirtException(org.libvirt.LibvirtException) StartCommand(com.cloud.agent.api.StartCommand) Connect(org.libvirt.Connect) InternalErrorException(com.cloud.exception.InternalErrorException) URISyntaxException(java.net.URISyntaxException) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) AttachAnswer(org.apache.cloudstack.storage.command.AttachAnswer) Answer(com.cloud.agent.api.Answer) CheckRouterAnswer(com.cloud.agent.api.CheckRouterAnswer) LibvirtRequestWrapper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper) Domain(org.libvirt.Domain) NicTO(com.cloud.agent.api.to.NicTO) Test(org.junit.Test)

Aggregations

VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)84 NicTO (com.cloud.agent.api.to.NicTO)42 Test (org.junit.Test)33 Answer (com.cloud.agent.api.Answer)32 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)25 HashMap (java.util.HashMap)18 LibvirtException (org.libvirt.LibvirtException)18 StartAnswer (com.cloud.agent.api.StartAnswer)17 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)17 URISyntaxException (java.net.URISyntaxException)16 Connect (org.libvirt.Connect)16 InternalErrorException (com.cloud.exception.InternalErrorException)15 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)14 KVMStoragePoolManager (com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager)14 Connection (com.xensource.xenapi.Connection)14 ConfigurationException (javax.naming.ConfigurationException)14 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)13 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)13 StartCommand (com.cloud.agent.api.StartCommand)12 ArrayList (java.util.ArrayList)11