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