Search in sources :

Example 16 with StopAnswer

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

the class VmwareResource method execute.

protected Answer execute(StopCommand cmd) {
    if (s_logger.isInfoEnabled()) {
        s_logger.info("Executing resource StopCommand: " + _gson.toJson(cmd));
    }
    // In the stop command, we're passed in the name of the VM as seen by cloudstack,
    // i.e., i-x-y. This is the internal VM name.
    VmwareContext context = getServiceContext();
    VmwareHypervisorHost hyperHost = getHyperHost(context);
    try {
        VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(cmd.getVmName());
        if (vmMo != null) {
            if (cmd.checkBeforeCleanup()) {
                if (getVmPowerState(vmMo) != PowerState.PowerOff) {
                    String msg = "StopCommand is sent for cleanup and VM " + cmd.getVmName() + " is current running. ignore it.";
                    s_logger.warn(msg);
                    return new StopAnswer(cmd, msg, false);
                } else {
                    String msg = "StopCommand is sent for cleanup and VM " + cmd.getVmName() + " is indeed stopped already.";
                    s_logger.info(msg);
                    return new StopAnswer(cmd, msg, true);
                }
            }
            try {
                vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, "0");
                vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_VM_INTERNAL_NAME, cmd.getVmName());
                if (getVmPowerState(vmMo) != PowerState.PowerOff) {
                    if (vmMo.safePowerOff(_shutdownWaitMs)) {
                        return new StopAnswer(cmd, "Stop VM " + cmd.getVmName() + " Succeed", true);
                    } else {
                        String msg = "Have problem in powering off VM " + cmd.getVmName() + ", let the process continue";
                        s_logger.warn(msg);
                        return new StopAnswer(cmd, msg, true);
                    }
                }
                String msg = "VM " + cmd.getVmName() + " is already in stopped state";
                s_logger.info(msg);
                return new StopAnswer(cmd, msg, true);
            } finally {
            }
        } else {
            String msg = "VM " + cmd.getVmName() + " is no longer in vSphere";
            s_logger.info(msg);
            return new StopAnswer(cmd, msg, true);
        }
    } catch (Exception e) {
        if (e instanceof RemoteException) {
            s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
            invalidateServiceContext();
        }
        String msg = "StopCommand failed due to " + VmwareHelper.getExceptionMessage(e);
        s_logger.error(msg);
        return new StopAnswer(cmd, msg, false);
    }
}
Also used : VmwareContext(com.cloud.hypervisor.vmware.util.VmwareContext) VirtualMachineMO(com.cloud.hypervisor.vmware.mo.VirtualMachineMO) VmwareHypervisorHost(com.cloud.hypervisor.vmware.mo.VmwareHypervisorHost) RemoteException(java.rmi.RemoteException) StopAnswer(com.cloud.agent.api.StopAnswer) 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 17 with StopAnswer

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

the class CitrixStopCommandWrapper method execute.

@Override
public Answer execute(final StopCommand command, final CitrixResourceBase citrixResourceBase) {
    final String vmName = command.getVmName();
    String platformstring = null;
    try {
        final Connection conn = citrixResourceBase.getConnection();
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        // stop vm which is running on this host or is in halted state
        final Iterator<VM> iter = vms.iterator();
        while (iter.hasNext()) {
            final VM vm = iter.next();
            final VM.Record vmr = vm.getRecord(conn);
            if (vmr.powerState != VmPowerState.RUNNING) {
                continue;
            }
            if (citrixResourceBase.isRefNull(vmr.residentOn)) {
                continue;
            }
            if (vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                continue;
            }
            iter.remove();
        }
        if (vms.size() == 0) {
            return new StopAnswer(command, "VM does not exist", true);
        }
        for (final VM vm : vms) {
            final VM.Record vmr = vm.getRecord(conn);
            platformstring = StringUtils.mapToString(vmr.platform);
            if (vmr.isControlDomain) {
                final String msg = "Tring to Shutdown control domain";
                s_logger.warn(msg);
                return new StopAnswer(command, msg, false);
            }
            if (vmr.powerState == VmPowerState.RUNNING && !citrixResourceBase.isRefNull(vmr.residentOn) && !vmr.residentOn.getUuid(conn).equals(citrixResourceBase.getHost().getUuid())) {
                final String msg = "Stop Vm " + vmName + " failed due to this vm is not running on this host: " + citrixResourceBase.getHost().getUuid() + " but host:" + vmr.residentOn.getUuid(conn);
                s_logger.warn(msg);
                return new StopAnswer(command, msg, platformstring, false);
            }
            if (command.checkBeforeCleanup() && vmr.powerState == VmPowerState.RUNNING) {
                final String msg = "Vm " + vmName + " is running on host and checkBeforeCleanup flag is set, so bailing out";
                s_logger.debug(msg);
                return new StopAnswer(command, msg, false);
            }
            s_logger.debug("9. The VM " + vmName + " is in Stopping state");
            try {
                if (vmr.powerState == VmPowerState.RUNNING) {
                    /* when stop a vm, set affinity to current xenserver */
                    vm.setAffinity(conn, vm.getResidentOn(conn));
                    if (citrixResourceBase.canBridgeFirewall()) {
                        final String result = citrixResourceBase.callHostPlugin(conn, "vmops", "destroy_network_rules_for_vm", "vmName", command.getVmName());
                        if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
                            s_logger.warn("Failed to remove  network rules for vm " + command.getVmName());
                        } else {
                            s_logger.info("Removed  network rules for vm " + command.getVmName());
                        }
                    }
                    citrixResourceBase.shutdownVM(conn, vm, vmName);
                }
            } catch (final Exception e) {
                final String msg = "Catch exception " + e.getClass().getName() + " when stop VM:" + command.getVmName() + " due to " + e.toString();
                s_logger.debug(msg);
                return new StopAnswer(command, msg, platformstring, false);
            } finally {
                try {
                    if (vm.getPowerState(conn) == VmPowerState.HALTED) {
                        Set<VGPU> vGPUs = null;
                        // Get updated GPU details
                        try {
                            vGPUs = vm.getVGPUs(conn);
                        } catch (final XenAPIException e2) {
                            s_logger.debug("VM " + vmName + " does not have GPU support.");
                        }
                        if (vGPUs != null && !vGPUs.isEmpty()) {
                            final HashMap<String, HashMap<String, VgpuTypesInfo>> groupDetails = citrixResourceBase.getGPUGroupDetails(conn);
                            command.setGpuDevice(new GPUDeviceTO(null, null, groupDetails));
                        }
                        final Set<VIF> vifs = vm.getVIFs(conn);
                        final List<Network> networks = new ArrayList<Network>();
                        for (final VIF vif : vifs) {
                            networks.add(vif.getNetwork(conn));
                        }
                        vm.destroy(conn);
                        final SR sr = citrixResourceBase.getISOSRbyVmName(conn, command.getVmName());
                        citrixResourceBase.removeSR(conn, sr);
                        // anymore
                        for (final Network network : networks) {
                            try {
                                if (network.getNameLabel(conn).startsWith("VLAN")) {
                                    citrixResourceBase.disableVlanNetwork(conn, network);
                                }
                            } catch (final Exception e) {
                            // network might be destroyed by other host
                            }
                        }
                        return new StopAnswer(command, "Stop VM " + vmName + " Succeed", platformstring, true);
                    }
                } catch (final Exception e) {
                    final String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage();
                    s_logger.warn(msg, e);
                } finally {
                    s_logger.debug("10. The VM " + vmName + " is in Stopped state");
                }
            }
        }
    } catch (final Exception e) {
        final String msg = "Stop Vm " + vmName + " fail due to " + e.toString();
        s_logger.warn(msg, e);
        return new StopAnswer(command, msg, platformstring, false);
    }
    return new StopAnswer(command, "Stop VM failed", platformstring, false);
}
Also used : HashMap(java.util.HashMap) Connection(com.xensource.xenapi.Connection) ArrayList(java.util.ArrayList) XenAPIException(com.xensource.xenapi.Types.XenAPIException) GPUDeviceTO(com.cloud.agent.api.to.GPUDeviceTO) XenAPIException(com.xensource.xenapi.Types.XenAPIException) VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Network(com.xensource.xenapi.Network) StopAnswer(com.cloud.agent.api.StopAnswer) VGPU(com.xensource.xenapi.VGPU) SR(com.xensource.xenapi.SR)

Example 18 with StopAnswer

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

the class BareMetalResourceBase method execute.

protected StopAnswer execute(final StopCommand cmd) {
    boolean success = false;
    int count = 0;
    Script powerOff = _powerOffCommand;
    while (count < 10) {
        if (!doScript(powerOff)) {
            break;
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            break;
        }
        OutputInterpreter.AllLinesParser interpreter = new OutputInterpreter.AllLinesParser();
        if (!doScript(_getStatusCommand, interpreter)) {
            success = true;
            s_logger.warn("Cannot get power status of " + getName() + ", assume VM state changed successfully");
            break;
        }
        if (!isPowerOn(interpreter.getLines())) {
            success = true;
            break;
        } else {
            powerOff = _forcePowerOffCommand;
        }
        count++;
    }
    return success ? new StopAnswer(cmd, "Success", true) : new StopAnswer(cmd, "IPMI power off failed", false);
}
Also used : Script(com.cloud.utils.script.Script) OutputInterpreter(com.cloud.utils.script.OutputInterpreter) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 19 with StopAnswer

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

the class HypervDirectConnectResourceTest method simpleVmStop.

private StopAnswer simpleVmStop() {
    String sampleStop = "{\"isProxy\":false,\"vmName\":\"i-2-17-VM\"," + "\"contextMap\":{},\"wait\":0}";
    StopCommand cmd = s_gson.fromJson(sampleStop, StopCommand.class);
    StopAnswer ans = (StopAnswer) s_hypervresource.executeRequest(cmd);
    return ans;
}
Also used : StopCommand(com.cloud.agent.api.StopCommand) StopAnswer(com.cloud.agent.api.StopAnswer)

Example 20 with StopAnswer

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

the class HypervDirectConnectResourceTest method testStartStopCommand.

@Test
public final void testStartStopCommand() {
    String sample = getSampleStartCommand();
    StartAnswer sans = simpleVmStart(sample);
    Assert.assertTrue(sans.getDetails(), sans.getResult());
    StopAnswer stopAns = simpleVmStop();
    Assert.assertTrue(stopAns.getDetails(), stopAns.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