Search in sources :

Example 26 with Task

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

the class VSphere method suspendVm.

public void suspendVm(VirtualMachine vm) throws VSphereException {
    if (isPoweredOn(vm)) {
        try {
            // TODO is this better?
            // vm.shutdownGuest()
            final Task task = vm.suspendVM_Task();
            final String status = task.waitForTask();
            if (Task.SUCCESS.equals(status)) {
                LOGGER.log(Level.FINER, "VM was suspended successfully.");
                return;
            }
            throw newVSphereException(task.getTaskInfo(), "Machine could not be suspended!");
        } catch (RuntimeException | VSphereException e) {
            throw e;
        } catch (Exception e) {
            throw new VSphereException(e);
        }
    } else {
        LOGGER.log(Level.FINER, "Machine not powered on.");
        return;
    }
}
Also used : Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException)

Example 27 with Task

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

the class VSphere method revertToSnapshot.

public void revertToSnapshot(String vmName, String snapName) throws VSphereException {
    VirtualMachine vm = getVmByName(vmName);
    VirtualMachineSnapshot snap = getSnapshotInTree(vm, snapName);
    if (snap == null) {
        LOGGER.log(Level.SEVERE, "Cannot find snapshot: '" + snapName + "' for virtual machine: '" + vm.getName() + "'");
        throw new VSphereNotFoundException("Snapshot", snapName);
    }
    try {
        Task task = snap.revertToSnapshot_Task(null);
        if (!task.waitForTask().equals(Task.SUCCESS)) {
            final String msg = "Could not revert to snapshot '" + snap.toString() + "' for virtual machine:'" + vm.getName() + "'";
            LOGGER.log(Level.SEVERE, msg);
            throw newVSphereException(task.getTaskInfo(), msg);
        }
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e);
    }
}
Also used : VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 28 with Task

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

the class VSphere method deleteSnapshot.

public void deleteSnapshot(String vmName, String snapName, boolean consolidate, boolean failOnNoExist) throws VSphereException {
    VirtualMachine vm = getVmByName(vmName);
    VirtualMachineSnapshot snap = getSnapshotInTree(vm, snapName);
    if (snap == null && failOnNoExist) {
        throw new VSphereNotFoundException("Snapshot", snapName);
    }
    try {
        Task task;
        if (snap != null) {
            // Does not delete subtree; Implicitly consolidates disk
            task = snap.removeSnapshot_Task(false);
            if (!task.waitForTask().equals(Task.SUCCESS)) {
                throw newVSphereException(task.getTaskInfo(), "Could not delete snapshot");
            }
        }
        if (!consolidate)
            return;
        // This might be redundant, but I think it consolidates all disks,
        // where as the removeSnapshot only consolidates the individual disk
        task = vm.consolidateVMDisks_Task();
        if (!task.waitForTask().equals(Task.SUCCESS)) {
            throw newVSphereException(task.getTaskInfo(), "Could not consolidate VM disks");
        }
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e);
    }
}
Also used : VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) Task(com.vmware.vim25.mo.Task) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 29 with Task

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

the class vSphereCloudLauncher method resetVM.

private void resetVM(VirtualMachine vm, SlaveComputer slaveComputer, TaskListener taskListener) throws RemoteException, InterruptedException {
    vSphereCloud.Log(slaveComputer, taskListener, "Resetting the VM");
    Task taskReset = vm.resetVM_Task();
    if (!taskReset.waitForTask().equals(Task.SUCCESS)) {
        vSphereCloud.Log(slaveComputer, taskListener, "Unable to reset the VM");
    }
}
Also used : Task(com.vmware.vim25.mo.Task)

Example 30 with Task

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

the class vSphereCloudLauncher method revertVM.

private void revertVM(VirtualMachine vm, vSphereCloud vsC, SlaveComputer slaveComputer, TaskListener taskListener) throws IOException, InterruptedException, VSphereException {
    if (!snapName.isEmpty()) {
        VirtualMachineSnapshot snap = vsC.vSphereInstance().getSnapshotInTree(vm, snapName);
        if (snap == null) {
            throw new IOException("Virtual Machine snapshot cannot be found");
        }
        vSphereCloud.Log(slaveComputer, taskListener, "Reverting to snapshot:" + snapName);
        Task task = snap.revertToSnapshot_Task(null);
        if (!task.waitForTask().equals(Task.SUCCESS)) {
            throw new IOException("Error while reverting to virtual machine snapshot");
        }
    } else {
        vSphereCloud.Log(slaveComputer, taskListener, "Reverting to current snapshot");
        Task task = vm.revertToCurrentSnapshot_Task(null);
        if (!task.waitForTask().equals(Task.SUCCESS)) {
            throw new IOException("Error while reverting to virtual machine snapshot");
        }
    }
}
Also used : VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) Task(com.vmware.vim25.mo.Task) IOException(java.io.IOException)

Aggregations

ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)48 Task (com.vmware.vim25.mo.Task)27 TaskInfo (com.vmware.vim25.TaskInfo)23 ArrayList (java.util.ArrayList)21 RemoteException (java.rmi.RemoteException)18 QueryTask (com.vmware.xenon.services.common.QueryTask)14 Operation (com.vmware.xenon.common.Operation)13 List (java.util.List)13 PhotonModelUriUtils (com.vmware.photon.controller.model.util.PhotonModelUriUtils)11 ArrayOfManagedObjectReference (com.vmware.vim25.ArrayOfManagedObjectReference)11 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)10 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 RuntimeFaultFaultMsg (com.vmware.vim25.RuntimeFaultFaultMsg)8 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)7 InvalidPropertyFaultMsg (com.vmware.vim25.InvalidPropertyFaultMsg)7