Search in sources :

Example 1 with ChoiceOption

use of com.vmware.vim25.ChoiceOption 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 ChoiceOption

use of com.vmware.vim25.ChoiceOption 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

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 VirtualMachineQuestionInfo (com.vmware.vim25.VirtualMachineQuestionInfo)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