Search in sources :

Example 1 with VSphere

use of org.jenkinsci.plugins.vsphere.tools.VSphere in project vsphere-cloud-plugin by jenkinsci.

the class vSphereCloudLauncher method afterDisconnect.

@Override
public synchronized void afterDisconnect(SlaveComputer slaveComputer, TaskListener taskListener) {
    final vSphereCloudSlave vsSlave = (vSphereCloudSlave) slaveComputer.getNode();
    if (vsSlave == null) {
        vSphereCloud.Log(slaveComputer, taskListener, "Slave is null.");
        return;
    }
    if (vsSlave.slaveIsStarting == Boolean.TRUE) {
        vSphereCloud.Log(slaveComputer, taskListener, "Ignoring disconnect attempt because a connect attempt is in progress.");
        return;
    }
    if (vsSlave.slaveIsDisconnecting == Boolean.TRUE) {
        vSphereCloud.Log(slaveComputer, taskListener, "Already disconnecting on a separate thread");
        return;
    }
    if (slaveComputer.isTemporarilyOffline()) {
        if (!(slaveComputer.getOfflineCause() instanceof VSphereOfflineCause)) {
            vSphereCloud.Log(slaveComputer, taskListener, "Not disconnecting VM because it's not accepting tasks");
            return;
        }
    }
    vsSlave.slaveIsDisconnecting = Boolean.TRUE;
    VSphere v = null;
    boolean reconnect = false;
    try {
        vSphereCloud.Log(slaveComputer, taskListener, "Running disconnect procedure...");
        super.afterDisconnect(slaveComputer, taskListener);
        MACHINE_ACTION localIdle = idleAction;
        if (localIdle == null) {
            localIdle = MACHINE_ACTION.SHUTDOWN;
        }
        vSphereCloud.Log(slaveComputer, taskListener, "Disconnect done.  Performing idle action %s...", localIdle);
        final vSphereCloud vsC = findOurVsInstance();
        vsC.markVMOffline(slaveComputer.getDisplayName(), vmName);
        final VirtualMachine vm;
        if (!MACHINE_ACTION.NOTHING.equals(localIdle)) {
            v = vsC.vSphereInstance();
            vm = v.getVmByName(vmName);
        } else {
            vm = null;
        }
        if (vm != null) {
            // VirtualMachinePowerState power = vm.getRuntime().getPowerState();
            VirtualMachinePowerState power = vm.getSummary().getRuntime().powerState;
            if (power == VirtualMachinePowerState.poweredOn) {
                switch(localIdle) {
                    case SHUTDOWN:
                    case REVERT:
                    case REVERT_AND_RESET:
                    case REVERT_AND_RESTART:
                        shutdownVM(vm, slaveComputer, taskListener);
                        break;
                    case SUSPEND:
                        suspendVM(vm, slaveComputer, taskListener);
                        break;
                    case RESET:
                        resetVM(vm, slaveComputer, taskListener);
                        break;
                    default:
                        break;
                }
                switch(localIdle) {
                    case REVERT:
                        revertVM(vm, vsC, slaveComputer, taskListener);
                        break;
                    case REVERT_AND_RESTART:
                        revertVM(vm, vsC, slaveComputer, taskListener);
                        if (power == VirtualMachinePowerState.poweredOn) {
                            // Some time is needed for the VMWare Tools to reactivate
                            Thread.sleep(60000);
                            shutdownVM(vm, slaveComputer, taskListener);
                        }
                        powerOnVM(vm, slaveComputer, taskListener);
                        break;
                    case REVERT_AND_RESET:
                        revertVM(vm, vsC, slaveComputer, taskListener);
                        resetVM(vm, slaveComputer, taskListener);
                        break;
                    case RECONNECT_AND_REVERT:
                        reconnect = true;
                        break;
                    case NOTHING:
                    case SUSPEND:
                    case SHUTDOWN:
                    case RESET:
                        break;
                }
            } else {
            // VM is already powered down.
            }
        }
        vSphereCloud.Log(slaveComputer, taskListener, "Idle action %s complete.", localIdle);
    } catch (Throwable t) {
        vSphereCloud.Log(slaveComputer, taskListener, t, "Got an exception");
        taskListener.fatalError(t.getMessage(), t);
    } finally {
        if (v != null) {
            v.disconnect();
            v = null;
        }
        vsSlave.slaveIsDisconnecting = Boolean.FALSE;
        vsSlave.slaveIsStarting = Boolean.FALSE;
        if (reconnect) {
            slaveComputer.connect(false);
        }
    }
}
Also used : VirtualMachinePowerState(com.vmware.vim25.VirtualMachinePowerState) VSphere(org.jenkinsci.plugins.vsphere.tools.VSphere) VSphereOfflineCause(org.jenkinsci.plugins.vsphere.VSphereOfflineCause) VirtualMachine(com.vmware.vim25.mo.VirtualMachine)

Example 2 with VSphere

use of org.jenkinsci.plugins.vsphere.tools.VSphere in project vsphere-cloud-plugin by jenkinsci.

the class vSphereCloudSlaveTemplate method provision.

public vSphereCloudProvisionedSlave provision(final String cloneName, final TaskListener listener) throws VSphereException, FormException, IOException, InterruptedException {
    final PrintStream logger = listener.getLogger();
    final Map<String, String> resolvedExtraConfigParameters = calculateExtraConfigParameters(cloneName, listener);
    final VSphere vSphere = getParent().vSphereInstance();
    final vSphereCloudProvisionedSlave slave;
    try {
        slave = provision(cloneName, logger, resolvedExtraConfigParameters, vSphere);
    } finally {
        vSphere.disconnect();
    }
    return slave;
}
Also used : PrintStream(java.io.PrintStream) VSphere(org.jenkinsci.plugins.vsphere.tools.VSphere)

Example 3 with VSphere

use of org.jenkinsci.plugins.vsphere.tools.VSphere in project vsphere-cloud-plugin by jenkinsci.

the class VSphereBuildStepContainer method perform.

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
    VSphere vsphere = null;
    try {
        String expandedServerName = serverName;
        if (run instanceof AbstractBuild) {
            EnvVars env = (run.getEnvironment(listener));
            // Add in matrix axes..
            env.overrideAll(((AbstractBuild) run).getBuildVariables());
            expandedServerName = env.expand(serverName);
        }
        startLogs(listener.getLogger(), expandedServerName);
        // select by hash if we have one
        if (serverHash != null) {
            vsphere = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByHash(serverHash, run.getEnvironment(listener).get("JOB_NAME")).vSphereInstance();
        } else {
            vsphere = VSphereBuildStep.VSphereBuildStepDescriptor.getVSphereCloudByName(expandedServerName, run.getEnvironment(listener).get("JOB_NAME")).vSphereInstance();
        }
        buildStep.setVsphere(vsphere);
        if (run instanceof AbstractBuild) {
            buildStep.perform(((AbstractBuild) run), launcher, (BuildListener) listener);
        } else {
            buildStep.perform(run, filePath, launcher, listener);
        }
    } catch (Exception e) {
        throw new AbortException(e.getMessage());
    } finally {
        if (vsphere != null) {
            vsphere.disconnect();
        }
    }
}
Also used : VSphere(org.jenkinsci.plugins.vsphere.tools.VSphere) VSphereException(org.jenkinsci.plugins.vsphere.tools.VSphereException) IOException(java.io.IOException)

Example 4 with VSphere

use of org.jenkinsci.plugins.vsphere.tools.VSphere 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

VSphere (org.jenkinsci.plugins.vsphere.tools.VSphere)4 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)2 IOException (java.io.IOException)2 VSphereException (org.jenkinsci.plugins.vsphere.tools.VSphereException)2 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)1 VirtualMachineToolsStatus (com.vmware.vim25.VirtualMachineToolsStatus)1 Task (com.vmware.vim25.mo.Task)1 VirtualMachineSnapshot (com.vmware.vim25.mo.VirtualMachineSnapshot)1 ObjectStreamException (java.io.ObjectStreamException)1 PrintStream (java.io.PrintStream)1 RemoteException (java.rmi.RemoteException)1 Calendar (java.util.Calendar)1 VSphereOfflineCause (org.jenkinsci.plugins.vsphere.VSphereOfflineCause)1