Search in sources :

Example 1 with VirtualMachineSnapshot

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

the class VSphere method renameVmSnapshot.

/**
 * Renames a VM Snapshot
 * @param vmName the name of the VM whose snapshot is being renamed.
 * @param oldName the current name of the VM's snapshot.
 * @param newName the new name of the VM's snapshot.
 * @param newDescription the new description of the VM's snapshot.
 * @throws VSphereException If an error occurred.
 */
public void renameVmSnapshot(String vmName, String oldName, String newName, String newDescription) throws VSphereException {
    try {
        VirtualMachine vm = getVmByName(vmName);
        if (vm == null) {
            throw new VSphereNotFoundException("VM", vmName);
        }
        VirtualMachineSnapshot snapshot = getSnapshotInTree(vm, oldName);
        snapshot.renameSnapshot(newName, newDescription);
        LOGGER.log(Level.FINER, "VM Snapshot was renamed successfully.");
        return;
    } catch (RuntimeException | VSphereException e) {
        throw e;
    } catch (Exception e) {
        throw new VSphereException(e.getMessage(), e);
    }
}
Also used : VirtualMachineSnapshot(com.vmware.vim25.mo.VirtualMachineSnapshot) MalformedURLException(java.net.MalformedURLException) RemoteException(java.rmi.RemoteException) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 2 with VirtualMachineSnapshot

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

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

use of com.vmware.vim25.mo.VirtualMachineSnapshot 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)

Example 5 with VirtualMachineSnapshot

use of com.vmware.vim25.mo.VirtualMachineSnapshot 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)

Aggregations

VirtualMachineSnapshot (com.vmware.vim25.mo.VirtualMachineSnapshot)7 Task (com.vmware.vim25.mo.Task)5 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)5 RemoteException (java.rmi.RemoteException)5 MalformedURLException (java.net.MalformedURLException)4 IOException (java.io.IOException)2 CustomizationSpecItem (com.vmware.vim25.CustomizationSpecItem)1 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)1 VirtualMachineCloneSpec (com.vmware.vim25.VirtualMachineCloneSpec)1 VirtualMachineConfigInfo (com.vmware.vim25.VirtualMachineConfigInfo)1 VirtualMachineConfigSpec (com.vmware.vim25.VirtualMachineConfigSpec)1 VirtualMachineRelocateSpec (com.vmware.vim25.VirtualMachineRelocateSpec)1 VirtualMachineSnapshotInfo (com.vmware.vim25.VirtualMachineSnapshotInfo)1 VirtualMachineSnapshotTree (com.vmware.vim25.VirtualMachineSnapshotTree)1 VirtualMachineToolsStatus (com.vmware.vim25.VirtualMachineToolsStatus)1 Folder (com.vmware.vim25.mo.Folder)1 ObjectStreamException (java.io.ObjectStreamException)1 Calendar (java.util.Calendar)1 VSphere (org.jenkinsci.plugins.vsphere.tools.VSphere)1 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)1