Search in sources :

Example 1 with StopAnswer

use of com.cloud.agent.api.StopAnswer in project CloudStack-archive by CloudStack-extras.

the class LibvirtComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        List<DiskDef> disks = getDisks(conn, vmName);
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        if (result == null) {
            for (DiskDef disk : disks) {
                if (disk.getDeviceType() == DiskDef.deviceType.CDROM && disk.getDiskPath() != null)
                    cleanupDisk(conn, disk);
            }
        }
        final String result2 = cleanupVnet(conn, cmd.getVnet());
        if (result != null && result2 != null) {
            result = result2 + result;
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : DiskDef(com.cloud.agent.resource.computing.LibvirtVMDef.DiskDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 2 with StopAnswer

use of com.cloud.agent.api.StopAnswer in project CloudStack-archive by CloudStack-extras.

the class CloudZonesComputingResource method execute.

protected Answer execute(StopCommand cmd) {
    final String vmName = cmd.getVmName();
    Long bytesReceived = new Long(0);
    Long bytesSent = new Long(0);
    State state = null;
    synchronized (_vms) {
        state = _vms.get(vmName);
        _vms.put(vmName, State.Stopping);
    }
    try {
        Connect conn = LibvirtConnection.getConnection();
        try {
            Domain dm = conn.domainLookupByUUID(UUID.nameUUIDFromBytes(vmName.getBytes()));
        } catch (LibvirtException e) {
            state = State.Stopped;
            return new StopAnswer(cmd, null, 0, bytesSent, bytesReceived);
        }
        String macAddress = null;
        if (vmName.startsWith("i-")) {
            List<InterfaceDef> nics = getInterfaces(conn, vmName);
            if (!nics.isEmpty()) {
                macAddress = nics.get(0).getMacAddress();
            }
        }
        destroy_network_rules_for_vm(conn, vmName);
        String result = stopVM(conn, vmName, defineOps.UNDEFINE_VM);
        try {
            cleanupVnet(conn, cmd.getVnet());
            _dhcpSnooper.cleanup(macAddress, vmName);
            _vmDataServer.handleVmStopped(cmd.getVmName());
        } catch (Exception e) {
        }
        state = State.Stopped;
        return new StopAnswer(cmd, result, 0, bytesSent, bytesReceived);
    } catch (LibvirtException e) {
        return new StopAnswer(cmd, e.getMessage());
    } finally {
        synchronized (_vms) {
            if (state != null) {
                _vms.put(vmName, state);
            } else {
                _vms.remove(vmName);
            }
        }
    }
}
Also used : InterfaceDef(com.cloud.agent.resource.computing.LibvirtVMDef.InterfaceDef) LibvirtException(org.libvirt.LibvirtException) State(com.cloud.vm.VirtualMachine.State) Connect(org.libvirt.Connect) Domain(org.libvirt.Domain) StopAnswer(com.cloud.agent.api.StopAnswer) ConfigurationException(javax.naming.ConfigurationException) LibvirtException(org.libvirt.LibvirtException)

Example 3 with StopAnswer

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

the class VirtualMachineManagerImplTest method testSendStopWithOkAnswer.

@Test
public void testSendStopWithOkAnswer() throws Exception {
    VirtualMachineGuru guru = mock(VirtualMachineGuru.class);
    VirtualMachine vm = mock(VirtualMachine.class);
    VirtualMachineProfile profile = mock(VirtualMachineProfile.class);
    StopAnswer answer = new StopAnswer(new StopCommand(vm, false, false), "ok", true);
    when(profile.getVirtualMachine()).thenReturn(vm);
    when(vm.getHostId()).thenReturn(1L);
    when(_agentMgr.send(anyLong(), (Command) any())).thenReturn(answer);
    boolean actual = _vmMgr.sendStop(guru, profile, false, false);
    Assert.assertTrue(actual);
}
Also used : StopCommand(com.cloud.agent.api.StopCommand) StopAnswer(com.cloud.agent.api.StopAnswer) Test(org.junit.Test)

Example 4 with StopAnswer

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

the class OvmResourceBase method execute.

@Override
public StopAnswer execute(StopCommand cmd) {
    String vmName = cmd.getVmName();
    try {
        OvmVm.Details vm = null;
        try {
            vm = OvmVm.getDetails(_conn, vmName);
        } catch (XmlRpcException e) {
            s_logger.debug("Unable to get details of vm: " + vmName + ", treating it as stopped", e);
            return new StopAnswer(cmd, "success", true);
        }
        deleteAllNetworkRulesForVm(vmName);
        OvmVm.stop(_conn, vmName);
        cleanup(vm);
        return new StopAnswer(cmd, "success", true);
    } catch (Exception e) {
        s_logger.debug("Stop " + vmName + "failed", e);
        return new StopAnswer(cmd, e.getMessage(), false);
    }
}
Also used : OvmVm(com.cloud.ovm.object.OvmVm) StopAnswer(com.cloud.agent.api.StopAnswer) XmlRpcException(org.apache.xmlrpc.XmlRpcException) XmlRpcException(org.apache.xmlrpc.XmlRpcException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(javax.naming.ConfigurationException)

Example 5 with StopAnswer

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

the class HypervDirectConnectResourceTest method testStartStartCommand.

@Test
public final void testStartStartCommand() {
    String sample = getSampleStartCommand();
    StartAnswer sans = simpleVmStart(sample);
    Assert.assertTrue(sans.getDetails(), sans.getResult());
    simpleVmStart(sample);
    StopAnswer ans = simpleVmStop();
    Assert.assertTrue(ans.getDetails(), ans.getResult());
}
Also used : StartAnswer(com.cloud.agent.api.StartAnswer) StopAnswer(com.cloud.agent.api.StopAnswer) Test(org.junit.Test)

Aggregations

StopAnswer (com.cloud.agent.api.StopAnswer)21 StartAnswer (com.cloud.agent.api.StartAnswer)7 StopCommand (com.cloud.agent.api.StopCommand)6 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 State (com.cloud.vm.VirtualMachine.State)6 ConfigurationException (javax.naming.ConfigurationException)6 Answer (com.cloud.agent.api.Answer)5 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)5 GPUDeviceTO (com.cloud.agent.api.to.GPUDeviceTO)4 Test (org.junit.Test)4 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)3 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)3 PlugNicAnswer (com.cloud.agent.api.PlugNicAnswer)3 RebootAnswer (com.cloud.agent.api.RebootAnswer)3 RestoreVMSnapshotAnswer (com.cloud.agent.api.RestoreVMSnapshotAnswer)3 UnPlugNicAnswer (com.cloud.agent.api.UnPlugNicAnswer)3 AgentUnavailableException (com.cloud.exception.AgentUnavailableException)3 OperationTimedoutException (com.cloud.exception.OperationTimedoutException)3 Connect (org.libvirt.Connect)3 LibvirtException (org.libvirt.LibvirtException)3