Search in sources :

Example 46 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtReplugNicCommandWrapper method execute.

@Override
public Answer execute(final ReplugNicCommand command, final LibvirtComputingResource libvirtComputingResource) {
    final NicTO nic = command.getNic();
    final String vmName = command.getVmName();
    Domain vm = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
        vm = libvirtComputingResource.getDomain(conn, vmName);
        InterfaceDef oldPluggedNic = findPluggedNic(libvirtComputingResource, nic, vmName, conn);
        final VifDriver newVifDriver = libvirtComputingResource.getVifDriver(nic.getType(), nic.getName());
        final InterfaceDef interfaceDef = newVifDriver.plug(nic, "Other PV", oldPluggedNic.getModel().toString(), null);
        interfaceDef.setSlot(oldPluggedNic.getSlot());
        interfaceDef.setDevName(oldPluggedNic.getDevName());
        interfaceDef.setLinkStateUp(false);
        oldPluggedNic.setSlot(null);
        int i = 0;
        do {
            i++;
            s_logger.debug("ReplugNic: Detaching interface" + oldPluggedNic + " (Attempt: " + i + ")");
            vm.detachDevice(oldPluggedNic.toString());
        } while (findPluggedNic(libvirtComputingResource, nic, vmName, conn) != null && i <= 10);
        s_logger.debug("ReplugNic: Attaching interface" + interfaceDef);
        vm.attachDevice(interfaceDef.toString());
        interfaceDef.setLinkStateUp(true);
        s_logger.debug("ReplugNic: Updating interface" + interfaceDef);
        vm.updateDeviceFlags(interfaceDef.toString(), DomainAffect.LIVE.getValue());
        // each interface at this point, so inform all vif drivers
        for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
            vifDriver.unplug(oldPluggedNic, true);
        }
        return new ReplugNicAnswer(command, true, "success");
    } catch (final LibvirtException | InternalErrorException e) {
        final String msg = " Plug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new ReplugNicAnswer(command, false, msg);
    } finally {
        if (vm != null) {
            try {
                vm.free();
            } catch (final LibvirtException l) {
                s_logger.trace("Ignoring libvirt error.", l);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ReplugNicAnswer(com.cloud.agent.api.ReplugNicAnswer) InternalErrorException(com.cloud.exception.InternalErrorException) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Example 47 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtReplugNicCommandWrapper method findPluggedNic.

private InterfaceDef findPluggedNic(LibvirtComputingResource libvirtComputingResource, NicTO nic, String vmName, Connect conn) {
    InterfaceDef oldPluggedNic = null;
    final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
    for (final InterfaceDef pluggedNic : pluggedNics) {
        if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
            oldPluggedNic = pluggedNic;
        }
    }
    return oldPluggedNic;
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)

Example 48 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtSecurityGroupRulesCommandWrapper method execute.

@Override
public Answer execute(final SecurityGroupRulesCmd command, final LibvirtComputingResource libvirtComputingResource) {
    String vif = null;
    String brname = null;
    try {
        final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
        final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
        final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName());
        vif = nics.get(0).getDevName();
        brname = nics.get(0).getBrName();
        final VirtualMachineTO vm = command.getVmTO();
        if (!libvirtComputingResource.applyDefaultNetworkRules(conn, vm, true)) {
            s_logger.warn("Failed to program default network rules for vm " + command.getVmName());
            return new SecurityGroupRuleAnswer(command, false, "programming default network rules failed");
        }
    } catch (final LibvirtException e) {
        return new SecurityGroupRuleAnswer(command, false, e.toString());
    }
    final boolean result = libvirtComputingResource.addNetworkRules(command.getVmName(), Long.toString(command.getVmId()), command.getGuestIp(), command.getGuestIp6(), command.getSignature(), Long.toString(command.getSeqNum()), command.getGuestMac(), command.stringifyRules(), vif, brname, command.getSecIpsString());
    if (!result) {
        s_logger.warn("Failed to program network rules for vm " + command.getVmName());
        return new SecurityGroupRuleAnswer(command, false, "programming network rules failed");
    } else {
        s_logger.debug("Programmed network rules for vm " + command.getVmName() + " guestIp=" + command.getGuestIp() + ",ingress numrules=" + command.getIngressRuleSet().size() + ",egress numrules=" + command.getEgressRuleSet().size());
        return new SecurityGroupRuleAnswer(command);
    }
}
Also used : InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) SecurityGroupRuleAnswer(com.cloud.agent.api.SecurityGroupRuleAnswer) VirtualMachineTO(com.cloud.agent.api.to.VirtualMachineTO)

Example 49 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResourceTest method testUnPlugNicCommandMatchMack.

@Test
public void testUnPlugNicCommandMatchMack() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final InterfaceDef interfaceDef = Mockito.mock(InterfaceDef.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final InterfaceDef intDef = Mockito.mock(InterfaceDef.class);
    nics.add(intDef);
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<VifDriver> drivers = new ArrayList<VifDriver>();
    drivers.add(vifDriver);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    when(intDef.getDevName()).thenReturn("eth0");
    when(intDef.getBrName()).thenReturn("br0");
    when(intDef.getMacAddress()).thenReturn("00:00:00:00");
    when(nic.getMac()).thenReturn("00:00:00:00");
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
        when(interfaceDef.toString()).thenReturn("Interface");
        final String interfaceDefStr = interfaceDef.toString();
        doNothing().when(vm).detachDevice(interfaceDefStr);
        when(libvirtComputingResource.getAllVifDrivers()).thenReturn(drivers);
        doNothing().when(vifDriver).unplug(intDef, true);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
        verify(libvirtComputingResource, times(1)).getAllVifDrivers();
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 50 with InterfaceDef

use of com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef in project cloudstack by apache.

the class LibvirtComputingResourceTest method testUnPlugNicCommandNoNics.

@Test
public void testUnPlugNicCommandNoNics() {
    final NicTO nic = Mockito.mock(NicTO.class);
    final String instanceName = "Test";
    final UnPlugNicCommand command = new UnPlugNicCommand(nic, instanceName);
    final LibvirtUtilitiesHelper libvirtUtilitiesHelper = Mockito.mock(LibvirtUtilitiesHelper.class);
    final Connect conn = Mockito.mock(Connect.class);
    final Domain vm = Mockito.mock(Domain.class);
    final List<InterfaceDef> nics = new ArrayList<InterfaceDef>();
    final VifDriver vifDriver = Mockito.mock(VifDriver.class);
    final List<VifDriver> drivers = new ArrayList<VifDriver>();
    drivers.add(vifDriver);
    when(libvirtComputingResource.getLibvirtUtilitiesHelper()).thenReturn(libvirtUtilitiesHelper);
    when(libvirtComputingResource.getInterfaces(conn, command.getVmName())).thenReturn(nics);
    try {
        when(libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName())).thenReturn(conn);
        when(libvirtComputingResource.getDomain(conn, instanceName)).thenReturn(vm);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
    final LibvirtRequestWrapper wrapper = LibvirtRequestWrapper.getInstance();
    assertNotNull(wrapper);
    final Answer answer = wrapper.execute(command, libvirtComputingResource);
    assertTrue(answer.getResult());
    verify(libvirtComputingResource, times(1)).getLibvirtUtilitiesHelper();
    try {
        verify(libvirtUtilitiesHelper, times(1)).getConnectionByVmName(command.getVmName());
        verify(libvirtComputingResource, times(1)).getDomain(conn, instanceName);
    } catch (final LibvirtException e) {
        fail(e.getMessage());
    }
}
Also used : LibvirtException(org.libvirt.LibvirtException) Connect(org.libvirt.Connect) ArrayList(java.util.ArrayList) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand) LibvirtUtilitiesHelper(com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper) InterfaceDef(com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef) UnsupportedAnswer(com.cloud.agent.api.UnsupportedAnswer) 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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)35 Connect (org.libvirt.Connect)32 LibvirtException (org.libvirt.LibvirtException)32 Domain (org.libvirt.Domain)29 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVmDef.InterfaceDef)20 ArrayList (java.util.ArrayList)18 Test (org.junit.Test)18 Answer (com.cloud.agent.api.Answer)17 CheckRouterAnswer (com.cloud.agent.api.CheckRouterAnswer)16 NicTO (com.cloud.agent.api.to.NicTO)16 LibvirtRequestWrapper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper)16 LibvirtUtilitiesHelper (com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper)16 InternalErrorException (com.cloud.exception.InternalErrorException)12 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)10 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 UnsupportedAnswer (com.cloud.agent.api.UnsupportedAnswer)9 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)9 AttachAnswer (org.apache.cloudstack.storage.command.AttachAnswer)9 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)7 AttachAnswer (com.cloud.storage.command.AttachAnswer)7