Search in sources :

Example 1 with UnPlugNicAnswer

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

the class HypervDirectConnectResource method execute.

private UnPlugNicAnswer execute(final UnPlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource UnPlugNicCommand " + s_gson.toJson(cmd));
    }
    try {
        final String vmName = cmd.getVmName();
        final NicTO nic = cmd.getNic();
        final URI broadcastUri = nic.getBroadcastUri();
        if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
            throw new InternalErrorException("Unable to unassign a public IP to a VIF on network " + nic.getBroadcastUri());
        }
        final String vlanId = BroadcastDomainType.getValue(broadcastUri);
        int publicNicInfo = -1;
        publicNicInfo = getVmNics(vmName, vlanId);
        if (publicNicInfo > 0) {
            modifyNicVlan(vmName, "2", publicNicInfo, false, "");
        }
        return new UnPlugNicAnswer(cmd, true, "success");
    } catch (final Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e.toString());
    }
}
Also used : UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) InternalErrorException(com.cloud.exception.InternalErrorException) URI(java.net.URI) KeyStoreException(java.security.KeyStoreException) KeyManagementException(java.security.KeyManagementException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ClientProtocolException(org.apache.http.client.ClientProtocolException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) ConfigurationException(javax.naming.ConfigurationException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) CertificateException(java.security.cert.CertificateException) NicTO(com.cloud.agent.api.to.NicTO)

Example 2 with UnPlugNicAnswer

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

the class LibvirtUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand 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);
        final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName);
        for (final InterfaceDef pluggedNic : pluggedNics) {
            if (pluggedNic.getMacAddress().equalsIgnoreCase(nic.getMac())) {
                vm.detachDevice(pluggedNic.toString());
                // each interface at this point, so inform all vif drivers
                for (final VifDriver vifDriver : libvirtComputingResource.getAllVifDrivers()) {
                    vifDriver.unplug(pluggedNic);
                }
                return new UnPlugNicAnswer(command, true, "success");
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final LibvirtException e) {
        final String msg = " Unplug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(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) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) VifDriver(com.cloud.hypervisor.kvm.resource.VifDriver) NicTO(com.cloud.agent.api.to.NicTO)

Example 3 with UnPlugNicAnswer

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

the class VirtualMachineManagerImpl method unplugNic.

public boolean unplugNic(final Network network, final NicTO nic, final VirtualMachineTO vm, final ReservationContext context, final DeployDestination dest) throws ConcurrentOperationException, ResourceUnavailableException {
    boolean result = true;
    final VMInstanceVO router = _vmDao.findById(vm.getId());
    if (router.getState() == State.Running) {
        try {
            final Commands cmds = new Commands(Command.OnError.Stop);
            final UnPlugNicCommand unplugNicCmd = new UnPlugNicCommand(nic, vm.getName());
            cmds.addCommand("unplugnic", unplugNicCmd);
            _agentMgr.send(dest.getHost().getId(), cmds);
            final UnPlugNicAnswer unplugNicAnswer = cmds.getAnswer(UnPlugNicAnswer.class);
            if (!(unplugNicAnswer != null && unplugNicAnswer.getResult())) {
                s_logger.warn("Unable to unplug nic from router " + router);
                result = false;
            }
        } catch (final OperationTimedoutException e) {
            throw new AgentUnavailableException("Unable to unplug nic from rotuer " + router + " from network " + network, dest.getHost().getId(), e);
        }
    } else if (router.getState() == State.Stopped || router.getState() == State.Stopping) {
        s_logger.debug("Vm " + router.getInstanceName() + " is in " + router.getState() + ", so not sending unplug nic command to the backend");
    } else {
        s_logger.warn("Unable to apply unplug nic, Vm " + router + " is not in the right state " + router.getState());
        throw new ResourceUnavailableException("Unable to apply unplug nic on the backend," + " vm " + router + " is not in the right state", DataCenter.class, router.getDataCenterId());
    }
    return result;
}
Also used : OperationTimedoutException(com.cloud.exception.OperationTimedoutException) DataCenter(com.cloud.dc.DataCenter) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) AgentUnavailableException(com.cloud.exception.AgentUnavailableException) Commands(com.cloud.agent.manager.Commands) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) UnPlugNicCommand(com.cloud.agent.api.UnPlugNicCommand)

Example 4 with UnPlugNicAnswer

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

the class VmwareResource method execute.

private UnPlugNicAnswer execute(UnPlugNicCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource UnPlugNicCommand " + _gson.toJson(cmd));
    }
    VmwareContext context = getServiceContext();
    try {
        VmwareHypervisorHost hyperHost = getHyperHost(context);
        String vmName = cmd.getVmName();
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
        if (vmMo == null) {
            if (hyperHost instanceof HostMO) {
                ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
                vmMo = clusterMo.findVmOnHyperHost(vmName);
            }
        }
        if (vmMo == null) {
            String msg = "VM " + vmName + " no longer exists to execute UnPlugNic command";
            s_logger.error(msg);
            throw new Exception(msg);
        }
        /*
            if(!isVMWareToolsInstalled(vmMo)){
                String errMsg = "vmware tools not installed or not running, cannot remove nic from vm " + vmName;
                s_logger.debug(errMsg);
                return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + errMsg);
            }
             */
        VirtualDevice nic = findVirtualNicDevice(vmMo, cmd.getNic().getMac());
        if (nic == null) {
            return new UnPlugNicAnswer(cmd, true, "success");
        }
        VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
        //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
        VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
        deviceConfigSpec.setDevice(nic);
        deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
        vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
        if (!vmMo.configureVm(vmConfigSpec)) {
            throw new Exception("Failed to configure devices when running unplugNicCommand");
        }
        return new UnPlugNicAnswer(cmd, true, "success");
    } catch (Exception e) {
        s_logger.error("Unexpected exception: ", e);
        return new UnPlugNicAnswer(cmd, false, "Unable to execute unPlugNicCommand due to " + e.toString());
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VirtualDevice(com.vmware.vim25.VirtualDevice) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) ClusterMO(com.cloud.hypervisor.vmware.mo.ClusterMO) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException)

Example 5 with UnPlugNicAnswer

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

the class CitrixUnPlugNicCommandWrapper method execute.

@Override
public Answer execute(final UnPlugNicCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        if (vms == null || vms.isEmpty()) {
            return new UnPlugNicAnswer(command, false, "Can not find VM " + vmName);
        }
        final VM vm = vms.iterator().next();
        final NicTO nic = command.getNic();
        final String mac = nic.getMac();
        final VIF vif = citrixResourceBase.getVifByMac(conn, vm, mac);
        if (vif != null) {
            vif.unplug(conn);
            final Network network = vif.getNetwork(conn);
            vif.destroy(conn);
            try {
                if (network.getNameLabel(conn).startsWith("VLAN")) {
                    citrixResourceBase.disableVlanNetwork(conn, network);
                }
            } catch (final Exception e) {
            }
        }
        return new UnPlugNicAnswer(command, true, "success");
    } catch (final Exception e) {
        final String msg = " UnPlug Nic failed due to " + e.toString();
        s_logger.warn(msg, e);
        return new UnPlugNicAnswer(command, false, msg);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) UnPlugNicAnswer(com.cloud.agent.api.UnPlugNicAnswer) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) Connection(com.xensource.xenapi.Connection) NicTO(com.cloud.agent.api.to.NicTO)

Aggregations

UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)6 NicTO (com.cloud.agent.api.to.NicTO)3 Commands (com.cloud.agent.manager.Commands)2 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)2 InternalErrorException (com.cloud.exception.InternalErrorException)2 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)2 IOException (java.io.IOException)2 ConnectException (java.net.ConnectException)2 RemoteException (java.rmi.RemoteException)2 ConfigurationException (javax.naming.ConfigurationException)2 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)1 UnPlugNicCommand (com.cloud.agent.api.UnPlugNicCommand)1 DataCenter (com.cloud.dc.DataCenter)1 CloudException (com.cloud.exception.CloudException)1 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)1 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)1 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)1 ClusterMO (com.cloud.hypervisor.vmware.mo.ClusterMO)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 VirtualMachineMO (com.cloud.hypervisor.vmware.mo.VirtualMachineMO)1