Search in sources :

Example 71 with Task

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

the class VSphere method takeSnapshot.

public void takeSnapshot(String vmName, String snapshot, String description, boolean snapMemory) throws VSphereException {
    final String message = "Could not take snapshot";
    VirtualMachine vmToSnapshot = getVmByName(vmName);
    if (vmToSnapshot == null) {
        throw new VSphereNotFoundException("VM", vmName);
    }
    try {
        Task task = vmToSnapshot.createSnapshot_Task(snapshot, description, snapMemory, !snapMemory);
        if (task.waitForTask().equals(Task.SUCCESS)) {
            return;
        }
        throw newVSphereException(task.getTaskInfo(), message);
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(message, e);
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 72 with Task

use of com.vmware.vim25.mo.Task 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 73 with Task

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

the class ReconfigureDisk method addSCSIController.

private VirtualLsiLogicController addSCSIController(VirtualMachine vm) throws Exception {
    VirtualMachineConfigInfo vmConfig = vm.getConfig();
    VirtualPCIController pci = null;
    Set<Integer> scsiBuses = new HashSet<Integer>();
    for (VirtualDevice vmDevice : vmConfig.getHardware().getDevice()) {
        if (vmDevice instanceof VirtualPCIController) {
            pci = (VirtualPCIController) vmDevice;
        } else if (vmDevice instanceof VirtualSCSIController) {
            VirtualSCSIController ctrl = (VirtualSCSIController) vmDevice;
            scsiBuses.add(ctrl.getBusNumber());
        }
    }
    if (pci == null) {
        throw new VSphereException("No PCI controller found");
    }
    VirtualMachineConfigSpec vmSpec = new VirtualMachineConfigSpec();
    VirtualDeviceConfigSpec deviceSpec = new VirtualDeviceConfigSpec();
    deviceSpec.setOperation(VirtualDeviceConfigSpecOperation.add);
    VirtualLsiLogicController scsiCtrl = new VirtualLsiLogicController();
    scsiCtrl.setControllerKey(pci.getKey());
    scsiCtrl.setSharedBus(VirtualSCSISharing.noSharing);
    for (int i = 0; ; ++i) {
        if (!scsiBuses.contains(Integer.valueOf(i))) {
            scsiCtrl.setBusNumber(i);
            break;
        }
    }
    deviceSpec.setDevice(scsiCtrl);
    vmSpec.setDeviceChange(new VirtualDeviceConfigSpec[] { deviceSpec });
    Task task = vm.reconfigVM_Task(vmSpec);
    task.waitForTask();
    return scsiCtrl;
}
Also used : Task(com.vmware.vim25.mo.Task) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) HashSet(java.util.HashSet)

Example 74 with Task

use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.

the class ComputeSystemControllerImpl method isComplete.

/**
 * Checks if the VMWare task has completed
 *
 * @param task
 *            the task to check
 * @return true if the task has completed, otherwise returns false
 * @throws Exception
 *             if an error occurs while monitoring the task
 */
private boolean isComplete(Task task) throws Exception {
    TaskInfo info = task.getTaskInfo();
    TaskInfoState state = info.getState();
    if (state == TaskInfoState.success) {
        return true;
    } else if (state == TaskInfoState.error) {
        return true;
    }
    return false;
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) TaskInfoState(com.vmware.vim25.TaskInfoState)

Example 75 with Task

use of com.vmware.vim25.mo.Task in project coprhd-controller by CoprHD.

the class SetStorageIOControl method execute.

@Override
public void execute() throws Exception {
    debug("Executing: %s", getDetail());
    StorageResourceManager manager = vcenter.getStorageResourceManager();
    StorageIORMConfigSpec spec = new StorageIORMConfigSpec();
    spec.setEnabled(enabled);
    Task task = null;
    try {
        task = manager.configureDatastoreIORM_Task(datastore, spec);
        waitForTask(task);
    } catch (Exception e) {
        logError("SetStorageIOControl.detail.error", datastore.getName());
        if (enabled && failIfErrorDuringEnable) {
            throw e;
        } else {
            cancelTaskNoException(task);
        }
    }
}
Also used : StorageResourceManager(com.vmware.vim25.mo.StorageResourceManager) VMwareTask(com.emc.sa.service.vmware.tasks.VMwareTask) Task(com.vmware.vim25.mo.Task) StorageIORMConfigSpec(com.vmware.vim25.StorageIORMConfigSpec)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)39 Task (com.vmware.vim25.mo.Task)27 TaskInfo (com.vmware.vim25.TaskInfo)21 RemoteException (java.rmi.RemoteException)17 ArrayList (java.util.ArrayList)15 QueryTask (com.vmware.xenon.services.common.QueryTask)14 Operation (com.vmware.xenon.common.Operation)13 List (java.util.List)12 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)11 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)10 ComputeDescriptionService (com.vmware.photon.controller.model.resources.ComputeDescriptionService)9 DiskService (com.vmware.photon.controller.model.resources.DiskService)9 TaskInfoState (com.vmware.vim25.TaskInfoState)9 ComputeService (com.vmware.photon.controller.model.resources.ComputeService)8 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)8 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)8 DeferredResult (com.vmware.xenon.common.DeferredResult)8 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)7 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)7