Search in sources :

Example 61 with Task

use of com.vmware.vim25.mo.Task in project photon-model by vmware.

the class PowerStateClient method resetVM.

/**
 * @param vm
 *
 * @throws Exception
 */
public void resetVM(ManagedObjectReference vm) throws Exception {
    ManagedObjectReference task = getVimPort().resetVMTask(vm);
    awaitTaskEnd(task);
    return;
}
Also used : ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 62 with Task

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

the class vSphereCloudLauncher method launch.

@Override
public void launch(SlaveComputer slaveComputer, TaskListener taskListener) throws IOException, InterruptedException {
    vSphereCloudSlave vsSlave = (vSphereCloudSlave) slaveComputer.getNode();
    // synchronized(vSphereCloud.class)
    {
        try {
            if (slaveComputer.isTemporarilyOffline()) {
                vSphereCloud.Log(slaveComputer, taskListener, "Not launching VM because it's not accepting tasks; temporarily offline");
                return;
            }
            // requests from Jenkins.
            if (vsSlave.slaveIsStarting == Boolean.TRUE) {
                vSphereCloud.Log(slaveComputer, taskListener, "Ignoring additional attempt to start the slave; it's already being started");
                return;
            }
            // If a slave is disconnecting, don't try to start it up
            if (vsSlave.slaveIsDisconnecting == Boolean.TRUE) {
                vSphereCloud.Log(slaveComputer, taskListener, "Ignoring connect attempt to start the slave; it's being shutdown");
                return;
            }
            vSphereCloudSlave.ProbableLaunchCleanup();
            vSphereCloud vsC = findOurVsInstance();
            vsSlave.slaveIsStarting = Boolean.TRUE;
            VSphere v = null;
            try {
                vSphereCloud.Log(slaveComputer, taskListener, "Starting Virtual Machine...");
                Calendar cal = Calendar.getInstance();
                cal.add(Calendar.MINUTE, 5);
                vSphereCloudSlave.AddProbableLaunch(vsSlave, cal.getTime());
                v = vsC.vSphereInstance();
                VirtualMachine vm = v.getVmByName(vmName);
                if (vm == null) {
                    throw new IOException("Virtual Machine '" + vmName + "' could not be found");
                }
                // Revert to a snapshot - always - if one is specified.
                if (!snapName.isEmpty()) {
                    VirtualMachineSnapshot snap = v.getSnapshotInTree(vm, snapName);
                    if (snap == null) {
                        throw new IOException("Virtual Machine '" + vmName + "' snapshot '" + snapName + "' 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");
                    }
                }
                switch(vm.getRuntime().powerState) {
                    case poweredOn:
                        // Nothing to do.
                        vSphereCloud.Log(slaveComputer, taskListener, "VM already powered on");
                        break;
                    case poweredOff:
                    case suspended:
                        // Power the VM up.
                        vSphereCloud.Log(slaveComputer, taskListener, "Powering on VM");
                        v.startVm(vmName, 60);
                        break;
                }
                if (waitForVMTools) {
                    vSphereCloud.Log(slaveComputer, taskListener, "Waiting for VMTools");
                    Calendar target = Calendar.getInstance();
                    target.add(Calendar.SECOND, 120);
                    while (Calendar.getInstance().before(target)) {
                        VirtualMachineToolsStatus status = vm.getGuest().toolsStatus;
                        if ((status == VirtualMachineToolsStatus.toolsOk) || (status == VirtualMachineToolsStatus.toolsOld)) {
                            vSphereCloud.Log(slaveComputer, taskListener, "VM Tools are running");
                            break;
                        }
                        Thread.sleep(5000);
                    }
                    vSphereCloud.Log(slaveComputer, taskListener, "Finished wait for VMTools");
                }
                /* At this point we have told vSphere to get the VM going.
                     * Now we wait our launch delay amount before trying to connect.
                     */
                if (launcher.isLaunchSupported()) {
                    if (launchDelay > 0) {
                        vSphereCloud.Log(slaveComputer, taskListener, "Waiting for " + launchDelay + " seconds before asking " + launcher + " to launch slave.");
                        // Delegate is going to do launch.
                        Thread.sleep(launchDelay * 1000);
                    }
                    vSphereCloud.Log(slaveComputer, taskListener, "Asking " + launcher.getClass().getSimpleName() + " to launch slave.");
                    super.launch(slaveComputer, taskListener);
                } else {
                    vSphereCloud.Log(slaveComputer, taskListener, "Waiting for up to " + launchDelay + " seconds for slave to come online.");
                    for (int i = 0; i <= launchDelay; i++) {
                        Thread.sleep(1000);
                        if (slaveComputer.isOnline()) {
                            vSphereCloud.Log(slaveComputer, taskListener, "Slave has come online.");
                            break;
                        }
                    }
                    if (!slaveComputer.isOnline()) {
                        vSphereCloud.Log(slaveComputer, taskListener, "Slave did not come online in allowed time");
                        throw new IOException("Slave did not come online in allowed time");
                    }
                }
                vSphereCloud.Log(slaveComputer, taskListener, "Slave online");
            } catch (final Exception e) {
                vSphereCloud.Log(slaveComputer, taskListener, e, "EXCEPTION while starting VM");
                vsC.markVMOffline(slaveComputer.getDisplayName(), vmName);
                throw new RuntimeException(e);
            } finally {
                vSphereCloudSlave.RemoveProbableLaunch(vsSlave);
                vsSlave.slaveIsStarting = Boolean.FALSE;
                if (v != null)
                    v.disconnect();
            }
        } catch (final RuntimeException e) {
            throw e;
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }
}
Also used : VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) Task(com.vmware.vim25.mo.Task) VSphere(org.jenkinsci.plugins.vsphere.tools.VSphere) Calendar(java.util.Calendar) VirtualMachineToolsStatus(com.vmware.vim25.VirtualMachineToolsStatus) IOException(java.io.IOException) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) ObjectStreamException(java.io.ObjectStreamException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 63 with Task

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

the class vSphereCloudLauncher method suspendVM.

private void suspendVM(VirtualMachine vm, SlaveComputer slaveComputer, TaskListener taskListener) throws RemoteException, InterruptedException {
    vSphereCloud.Log(slaveComputer, taskListener, "Suspending the VM");
    Task task = vm.suspendVM_Task();
    if (!task.waitForTask().equals(Task.SUCCESS)) {
        vSphereCloud.Log(slaveComputer, taskListener, "Unable to suspend the VM");
    }
}
Also used : Task(com.vmware.vim25.mo.Task)

Example 64 with Task

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

the class vSphereCloudLauncher method powerOnVM.

private void powerOnVM(VirtualMachine vm, SlaveComputer slaveComputer, TaskListener taskListener) throws RemoteException, InterruptedException {
    vSphereCloud.Log(slaveComputer, taskListener, "Powering on the VM");
    Task taskPowerOn = vm.powerOnVM_Task(null);
    if (!taskPowerOn.waitForTask().equals(Task.SUCCESS)) {
        vSphereCloud.Log(slaveComputer, taskListener, "Unable to power on the VM");
    }
}
Also used : Task(com.vmware.vim25.mo.Task)

Example 65 with Task

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

the class vSphereCloudLauncher method shutdownVM.

private void shutdownVM(VirtualMachine vm, SlaveComputer slaveComputer, TaskListener taskListener) throws RemoteException, InterruptedException {
    // If reverting to shutting down, attempt to shutdown
    // gracefully first, then hard.
    VirtualMachineToolsStatus status = vm.getGuest().toolsStatus;
    if ((status == VirtualMachineToolsStatus.toolsOk) || (status == VirtualMachineToolsStatus.toolsOld)) {
        try {
            vSphereCloud.Log(slaveComputer, taskListener, "Attempting a graceful shutdown");
            vm.shutdownGuest();
            Calendar target = Calendar.getInstance();
            target.add(Calendar.MINUTE, 3);
            while (Calendar.getInstance().before(target)) {
                if (vm.getRuntime().powerState == VirtualMachinePowerState.poweredOff) {
                    vSphereCloud.Log(slaveComputer, taskListener, "Guest shutdown succeeded");
                    break;
                }
                Thread.sleep(5000);
            }
        } catch (Throwable t) {
            vSphereCloud.Log(slaveComputer, taskListener, t, "Got an exception while attempting a graceful shutdown");
            vSphereCloud.Log(slaveComputer, taskListener, "Will now attempt a hard power down");
        }
    }
    // Still powered on or no tools?  Hard power down time.
    if (vm.getRuntime().powerState == VirtualMachinePowerState.poweredOn) {
        vSphereCloud.Log(slaveComputer, taskListener, "Powering down hard");
        Task task = vm.powerOffVM_Task();
        if (!task.waitForTask().equals(Task.SUCCESS)) {
            vSphereCloud.Log(slaveComputer, taskListener, "Unable to power down the VM");
        }
    }
}
Also used : Task(com.vmware.vim25.mo.Task) Calendar(java.util.Calendar) VirtualMachineToolsStatus(com.vmware.vim25.VirtualMachineToolsStatus)

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