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;
}
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);
}
}
}
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");
}
}
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");
}
}
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");
}
}
}
Aggregations