Search in sources :

Example 1 with VirtualMachineQuestionInfo

use of com.vmware.vim25.VirtualMachineQuestionInfo in project cloudstack by apache.

the class VirtualMachineMO method detachIso.

public void detachIso(String isoDatastorePath) throws Exception {
    if (s_logger.isTraceEnabled())
        s_logger.trace("vCenter API trace - detachIso(). target MOR: " + _mor.getValue() + ", isoDatastorePath: " + isoDatastorePath);
    VirtualDevice device = getIsoDevice();
    if (device == null) {
        if (s_logger.isTraceEnabled())
            s_logger.trace("vCenter API trace - detachIso() done(failed)");
        throw new Exception("Unable to find a CDROM device");
    }
    VirtualCdromRemotePassthroughBackingInfo backingInfo = new VirtualCdromRemotePassthroughBackingInfo();
    backingInfo.setDeviceName("");
    device.setBacking(backingInfo);
    VirtualMachineConfigSpec reConfigSpec = new VirtualMachineConfigSpec();
    //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
    VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
    deviceConfigSpec.setDevice(device);
    deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT);
    //deviceConfigSpecArray[0] = deviceConfigSpec;
    reConfigSpec.getDeviceChange().add(deviceConfigSpec);
    ManagedObjectReference morTask = _context.getService().reconfigVMTask(_mor, reConfigSpec);
    // Monitor VM questions
    final Boolean[] flags = { false };
    final VirtualMachineMO vmMo = this;
    Future<?> future = MonitorServiceExecutor.submit(new Runnable() {

        @Override
        public void run() {
            s_logger.info("VM Question monitor started...");
            while (!flags[0]) {
                try {
                    VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo();
                    VirtualMachineQuestionInfo question = runtimeInfo.getQuestion();
                    if (question != null) {
                        if (s_logger.isTraceEnabled()) {
                            s_logger.trace("Question id: " + question.getId());
                            s_logger.trace("Question text: " + question.getText());
                        }
                        if (question.getMessage() != null) {
                            for (VirtualMachineMessage msg : question.getMessage()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("msg id: " + msg.getId());
                                    s_logger.trace("msg text: " + msg.getText());
                                }
                                if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msg.getId())) {
                                    s_logger.info("Found that VM has a pending question that we need to answer programmatically, question id: " + msg.getId() + ", for safe operation we will automatically decline it");
                                    vmMo.answerVM(question.getId(), "1");
                                    break;
                                }
                            }
                        } else if (question.getText() != null) {
                            String text = question.getText();
                            String msgId;
                            String msgText;
                            if (s_logger.isDebugEnabled()) {
                                s_logger.debug("question text : " + text);
                            }
                            String[] tokens = text.split(":");
                            msgId = tokens[0];
                            msgText = tokens[1];
                            if ("msg.cdromdisconnect.locked".equalsIgnoreCase(msgId)) {
                                s_logger.info("Found that VM has a pending question that we need to answer programmatically, question id: " + question.getId() + ". Message id : " + msgId + ". Message text : " + msgText + ", for safe operation we will automatically decline it.");
                                vmMo.answerVM(question.getId(), "1");
                            }
                        }
                        ChoiceOption choice = question.getChoice();
                        if (choice != null) {
                            for (ElementDescription info : choice.getChoiceInfo()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("Choice option key: " + info.getKey());
                                    s_logger.trace("Choice option label: " + info.getLabel());
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    s_logger.error("Unexpected exception: ", e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    s_logger.debug("[ignored] interupted while handling vm question about iso detach.");
                }
            }
            s_logger.info("VM Question monitor stopped");
        }
    });
    try {
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (!result) {
            if (s_logger.isDebugEnabled())
                s_logger.trace("vCenter API trace - detachIso() done(failed)");
            throw new Exception("Failed to detachIso due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
        _context.waitForTaskProgressDone(morTask);
        s_logger.trace("vCenter API trace - detachIso() done(successfully)");
    } finally {
        flags[0] = true;
        future.cancel(true);
    }
}
Also used : VirtualMachineQuestionInfo(com.vmware.vim25.VirtualMachineQuestionInfo) VirtualDeviceConfigSpec(com.vmware.vim25.VirtualDeviceConfigSpec) VirtualMachineRuntimeInfo(com.vmware.vim25.VirtualMachineRuntimeInfo) VirtualMachineMessage(com.vmware.vim25.VirtualMachineMessage) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualMachineConfigSpec(com.vmware.vim25.VirtualMachineConfigSpec) VirtualCdromRemotePassthroughBackingInfo(com.vmware.vim25.VirtualCdromRemotePassthroughBackingInfo) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ChoiceOption(com.vmware.vim25.ChoiceOption) ElementDescription(com.vmware.vim25.ElementDescription)

Example 2 with VirtualMachineQuestionInfo

use of com.vmware.vim25.VirtualMachineQuestionInfo in project vsphere-cloud-plugin by jenkinsci.

the class VSphere method startVm.

/**
 * @param name - Name of VM to start
 * @param timeoutInSeconds How long to wait for the VM to be running.
 * @throws VSphereException If an error occurred.
 */
public void startVm(String name, int timeoutInSeconds) throws VSphereException {
    try {
        VirtualMachine vm = getVmByName(name);
        if (vm == null) {
            throw new VSphereNotFoundException("VM", name);
        }
        if (isPoweredOn(vm))
            return;
        if (vm.getConfig().template)
            throw new VSphereException("VM represents a template!");
        Task task = vm.powerOnVM_Task(null);
        int timesToCheck = timeoutInSeconds / 5;
        // add one extra time for remainder
        timesToCheck++;
        LOGGER.log(Level.FINER, "Checking " + timesToCheck + " times for vm to be powered on");
        for (int i = 0; i < timesToCheck; i++) {
            if (task.getTaskInfo().getState() == TaskInfoState.success) {
                LOGGER.log(Level.FINER, "VM was powered up successfully.");
                return;
            }
            if (task.getTaskInfo().getState() == TaskInfoState.running || task.getTaskInfo().getState() == TaskInfoState.queued) {
                Thread.sleep(5000);
            }
            // Check for copied/moved question
            VirtualMachineQuestionInfo q = vm.getRuntime().getQuestion();
            if (q != null && q.getId().equals("_vmx1")) {
                vm.answerVM(q.getId(), q.getChoice().getDefaultIndex().toString());
                return;
            }
        }
    } catch (InterruptedException e) {
        // build aborted
        // pass interrupt upwards
        Thread.currentThread().interrupt();
        throw new VSphereException("VM cannot be started: " + e.getMessage(), e);
    } catch (Exception e) {
        throw new VSphereException("VM cannot be started: " + e.getMessage(), e);
    }
    throw new VSphereException("VM cannot be started");
}
Also used : VirtualMachineQuestionInfo(com.vmware.vim25.VirtualMachineQuestionInfo) Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 3 with VirtualMachineQuestionInfo

use of com.vmware.vim25.VirtualMachineQuestionInfo in project cloudstack by apache.

the class VirtualMachineMO method powerOn.

public boolean powerOn() throws Exception {
    if (getResetSafePowerState() == VirtualMachinePowerState.POWERED_ON)
        return true;
    ManagedObjectReference morTask = _context.getService().powerOnVMTask(_mor, null);
    // Monitor VM questions
    final Boolean[] flags = { false };
    final VirtualMachineMO vmMo = this;
    Future<?> future = MonitorServiceExecutor.submit(new Runnable() {

        @Override
        public void run() {
            s_logger.info("VM Question monitor started...");
            while (!flags[0]) {
                try {
                    VirtualMachineRuntimeInfo runtimeInfo = vmMo.getRuntimeInfo();
                    VirtualMachineQuestionInfo question = runtimeInfo.getQuestion();
                    if (question != null) {
                        s_logger.info("Question id: " + question.getId());
                        s_logger.info("Question text: " + question.getText());
                        if (question.getMessage() != null) {
                            for (VirtualMachineMessage msg : question.getMessage()) {
                                if (s_logger.isInfoEnabled()) {
                                    s_logger.info("msg id: " + msg.getId());
                                    s_logger.info("msg text: " + msg.getText());
                                }
                                String logMsg = "Found that VM has a pending question that we need to answer programmatically, question id: " + msg.getId();
                                if ("msg.uuid.altered".equalsIgnoreCase(msg.getId())) {
                                    s_logger.info(logMsg + ", we will automatically answer as 'moved it' to address out of band HA for the VM");
                                    vmMo.answerVM(question.getId(), "1");
                                    break;
                                } else if (msg.getId().equalsIgnoreCase("msg.cpuid.noVHVQuestion")) {
                                    s_logger.info(logMsg + ", automatically answering 'yes'");
                                    vmMo.answerVM(question.getId(), "0");
                                    break;
                                }
                            }
                        }
                        if (s_logger.isTraceEnabled())
                            s_logger.trace("These are the choices we can have just in case");
                        ChoiceOption choice = question.getChoice();
                        if (choice != null) {
                            for (ElementDescription info : choice.getChoiceInfo()) {
                                if (s_logger.isTraceEnabled()) {
                                    s_logger.trace("Choice option key: " + info.getKey());
                                    s_logger.trace("Choice option label: " + info.getLabel());
                                }
                            }
                        }
                    }
                } catch (Throwable e) {
                    s_logger.error("Unexpected exception: ", e);
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    s_logger.debug("[ignored] interupted while dealing with vm questions.");
                }
            }
            s_logger.info("VM Question monitor stopped");
        }
    });
    try {
        boolean result = _context.getVimClient().waitForTask(morTask);
        if (result) {
            _context.waitForTaskProgressDone(morTask);
            return true;
        } else {
            s_logger.error("VMware powerOnVM_Task failed due to " + TaskMO.getTaskFailureInfo(_context, morTask));
        }
    } finally {
        // make sure to let VM question monitor exit
        flags[0] = true;
    }
    return false;
}
Also used : VirtualMachineQuestionInfo(com.vmware.vim25.VirtualMachineQuestionInfo) VirtualMachineRuntimeInfo(com.vmware.vim25.VirtualMachineRuntimeInfo) VirtualMachineMessage(com.vmware.vim25.VirtualMachineMessage) ArrayOfManagedObjectReference(com.vmware.vim25.ArrayOfManagedObjectReference) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ChoiceOption(com.vmware.vim25.ChoiceOption) ElementDescription(com.vmware.vim25.ElementDescription)

Aggregations

VirtualMachineQuestionInfo (com.vmware.vim25.VirtualMachineQuestionInfo)3 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)2 ChoiceOption (com.vmware.vim25.ChoiceOption)2 ElementDescription (com.vmware.vim25.ElementDescription)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 VirtualMachineMessage (com.vmware.vim25.VirtualMachineMessage)2 VirtualMachineRuntimeInfo (com.vmware.vim25.VirtualMachineRuntimeInfo)2 VirtualCdromRemotePassthroughBackingInfo (com.vmware.vim25.VirtualCdromRemotePassthroughBackingInfo)1 VirtualDevice (com.vmware.vim25.VirtualDevice)1 VirtualDeviceConfigSpec (com.vmware.vim25.VirtualDeviceConfigSpec)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 Task (com.vmware.vim25.mo.Task)1 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)1 MalformedURLException (java.net.MalformedURLException)1 RemoteException (java.rmi.RemoteException)1