| I've seen this issue also with version 2.16 and 2.18 of the vSphere Cloud plugin, however - it seems like it's not a problem in the plugin, but a limitation of the "cloud" Jenkins interface that the plugin implements. If you're trying to ensure a slave is always in a "clean" state when allocated, here's my workaround, after hours of painful google-search, trial and error: 1. Node configuration: fill the "Snapshot Name" field (eg "Clean") 2. Node configuration: Availability: "Take this agent online when in demand, and offline when idle" 3. Node configuration: What to do when the slave is disconnected: "Shutdown" 4. Pipeline job configuration: include the following code:
import jenkins.slaves.*
import jenkins.model.*
import hudson.slaves.*
import hudson.model.*
def SafelyDisposeNode() {
print "Safely disposing node..."
def slave = Jenkins.instance.getNode(env.NODE_NAME) as Slave
if (slave == null) {
error "ERROR: Could not get slave object for node!"
}
try
{
slave.getComputer().setTemporarilyOffline(true, null)
if(isUnix()) {
sh "(sleep 2; poweroff)&"
} else {
bat "shutdown -t 2 -s"
}
slave.getComputer().disconnect(null)
sleep 10
} catch (err) {
print "ERROR: could not safely dispose node!"
} finally {
slave.getComputer().setTemporarilyOffline(false, null)
}
print "...node safely disposed."
slave = null
}
def DisposableNode(String nodeLabel, Closure body) {
node(nodeLabel) {
try {
body()
} catch (err) {
throw err
} finally {
SafelyDisposeNode()
}
}
}
5. When you want to ensure the node will NOT be used by another job (or another run of the same job), use a "DisposableNode" block instead of "node" block:
DisposableNode('MyNodeLabel') {
// run your pipeline code here.
// it will make sure the node is shutdown at the end of the block, even if it fails.
// no other job or build will be able to use the node in its "dirty" state,
// and vSphere plugin will revert to "clean" snapshot before starting the node again.
}
6. If other Jobs are using this node (or node label), they all must use the above workaround, to avoid leaving a "dirty" machine for each other. 7. As of the "why is it so important to have node in a clean state?" question, my use case is integration tests of kernel-mode drivers (both Windows and Linux O/S) that typically "break" the O/S and leave it in an unstable state (BSODs and Kernel Panics are common). 8. If your pipeline job is running under a Groovy sandbox, you will need to permit some classes (The job will fail and offer you to whitelist a class, repeat carefully several times). |