Search in sources :

Example 1 with VSphereOfflineCause

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

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

the class vSphereCloudSlave method _terminate.

@Override
protected void _terminate(final TaskListener listener) throws IOException, InterruptedException {
    try {
        Computer computer = toComputer();
        if (computer != null) {
            final VSphereOfflineCause cause = new VSphereOfflineCause(Messages._vSphereCloudSlave_OfflineReason_ShuttingDown());
            computer.disconnect(cause);
            vSphereCloud.Log(this, listener, "Disconnected computer %s", vmName);
        } else {
            vSphereCloud.Log(this, listener, "Can't disconnect computer for %s as there was no Computer node for it.", vmName);
        }
    } catch (Exception e) {
        vSphereCloud.Log(this, listener, e, "Can't disconnect %s", vmName);
    }
}
Also used : Computer(hudson.model.Computer) AbortException(hudson.AbortException) FormException(hudson.model.Descriptor.FormException) IOException(java.io.IOException) VSphereOfflineCause(org.jenkinsci.plugins.vsphere.VSphereOfflineCause)

Example 3 with VSphereOfflineCause

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

the class vSphereCloudSlave method EndLimitedTestRun.

public boolean EndLimitedTestRun(Run r) {
    boolean ret = true;
    // See if the run maps to an existing computer; remove if found.
    Computer slave = RunToSlaveMapper.get(r);
    if (slave != null) {
        RunToSlaveMapper.remove(r);
    }
    if (LimitedTestRunCount > 0) {
        if (NumberOfLimitedTestRuns >= LimitedTestRunCount) {
            ret = false;
            NumberOfLimitedTestRuns = 0;
            try {
                if (slave != null) {
                    vSphereCloud.Log(this, "Disconnecting the slave agent on %s due to limited build threshold", slave.getName());
                    final VSphereOfflineCause tempOffline = new VSphereOfflineCause(Messages._vSphereCloudSlave_LimitedBuild_TemporarilyOnline());
                    slave.setTemporarilyOffline(true, tempOffline);
                    slave.waitUntilOffline();
                    final VSphereOfflineCause disconnect = new VSphereOfflineCause(Messages._vSphereCloudSlave_LimitedBuild_Disconnect());
                    slave.disconnect(disconnect);
                    final VSphereOfflineCause tempOnline = new VSphereOfflineCause(Messages._vSphereCloudSlave_LimitedBuild_TemporarilyOnline());
                    slave.setTemporarilyOffline(false, tempOnline);
                } else {
                    vSphereCloud.Log(this, "Attempting to shutdown slave due to limited build threshold, but cannot determine slave");
                }
            } catch (NullPointerException ex) {
                vSphereCloud.Log(this, ex, "NullPointerException thrown while retrieving the slave agent");
            } catch (InterruptedException ex) {
                vSphereCloud.Log(this, ex, "InterruptedException thrown while marking the slave as online or offline");
            }
        }
    } else {
        ret = true;
    }
    return ret;
}
Also used : Computer(hudson.model.Computer) VSphereOfflineCause(org.jenkinsci.plugins.vsphere.VSphereOfflineCause)

Aggregations

VSphereOfflineCause (org.jenkinsci.plugins.vsphere.VSphereOfflineCause)3 Computer (hudson.model.Computer)2 VirtualMachinePowerState (com.vmware.vim25.VirtualMachinePowerState)1 VirtualMachine (com.vmware.vim25.mo.VirtualMachine)1 AbortException (hudson.AbortException)1 FormException (hudson.model.Descriptor.FormException)1 IOException (java.io.IOException)1 VSphere (org.jenkinsci.plugins.vsphere.tools.VSphere)1