class C implements Serializable {
def stuff(steps) {
steps.node {
steps.sh 'echo hello'
}
}
}
def c = new C()
c.stuff(steps)
class C implements Serializable {
def stuff(script) {
script.node {
script.echo "running in ${script.env.JENKINS_URL}"
}
}
}
def c = new C()
c.stuff(this)
import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import hudson.slaves.EnvironmentVariablesNodeProperty.Entry
/**
* INSERT "Launch Method" SNIPPET HERE
**/
// define a "Permanent Agent"
Slave agent = new DumbSlave (
"agent-node",
"/home/jenkins",
launcher
)
agent.nodeDescription = "Agent node description"
agent.numExecutors = 1
agent.labelString = "agent-node-label"
agent.mode = Node.Mode.NORMAL
agent.retentionStrategy = new RetentionStrategy.Always()
List<Entry> env = new ArrayList<Entry>()
env.add( new Entry('key1','value1') )
env.add( new Entry('key2','value2') )
EnvironmentVariablesNodeProperty envPro = new EnvironmentVariablesNodeProperty(env)
agent.getNodeProperties().add( envPro )
// create a "Permanent Agent"
jenkins.model.Jenkins.instance.addNode( agent )
return "Node has been created successfully."
import com.cloudbees.jenkins.plugins.sshslaves.verification.*
import com.cloudbees.jenkins.plugins.sshslaves.SSHConnectionDetails
// Pick one of the strategies from the comments below this line
ServerKeyVerificationStrategy serverKeyVerificationStrategy = new TrustInitialConnectionVerificationStrategy(false)
// = new TrustInitialConnectionVerificationStrategy(false /* "Require manual verification of initial connection" */) // "Manually trusted key verification Strategy"
// = new ManuallyConnectionVerificationStrategy("<your-key-here>") // "Manually provided key verification Strategy"
// = new KnownHostsConnectionVerificationStrategy() // "~/.ssh/known_hosts file Verification Strategy"
// = new BlindTrustConnectionVerificationStrategy() // "Non-verifying Verification Strategy"
// define a "Launch method": "Launch agents via SSH"
ComputerLauncher launcher = new com.cloudbees.jenkins.plugins.sshslaves.SSHLauncher(
"host", // Host
new SSHConnectionDetails(
"credentialsId", // Credentials ID
22, // port
(String)null, // JavaPath
(String)null, // JVM Options
(String)null, // Prefix Start Agent Command
(String)null, // Suffix Start Agent Command
(boolean)false, // Log environment on initial connect
(ServerKeyVerificationStrategy) serverKeyVerificationStrategy // Host Key Verification Strategy
)
)
import hudson.node_monitors.*
import hudson.slaves.*
import java.util.concurrent.*
jenkins = jenkins.model.Jenkins.instance
import javax.mail.internet.*
import javax.mail.*
import javax.activation.*
def sendMail ( agent, cause ) {
message = agent + " agent is down. Check http://JENKINS_HOSTNAME:JENKINS_PORT/computer/" + agent + "\nBecause " + cause
subject = agent + " agent is offline"
toAddress = "JENKINS_ADMIN@YOUR_DOMAIN"
fromAddress = "JENKINS@YOUR_DOMAIN"
host = "SMTP_SERVER"
port = "SMTP_PORT"
Properties mprops = new Properties()
mprops.setProperty( "mail.transport.protocol", "smtp" )
mprops.setProperty( "mail.host", host )
mprops.setProperty( "mail.smtp.port", port )
Session lSession = Session.getDefaultInstance(mprops,null)
MimeMessage msg = new MimeMessage( lSession )
//tokenize out the recipients in case they came in as a list
StringTokenizer tok = new StringTokenizer(toAddress,";")
ArrayList emailTos = new ArrayList()
while( tok.hasMoreElements() ) {
emailTos.add( new InternetAddress(tok.nextElement().toString()) )
}
InternetAddress[] to = new InternetAddress[emailTos.size()]
to = (InternetAddress[]) emailTos.toArray(to)
msg.setRecipients(MimeMessage.RecipientType.TO,to)
InternetAddress fromAddr = new InternetAddress(fromAddress)
msg.setFrom( fromAddr )
msg.setFrom( new InternetAddress(fromAddress) )
msg.setSubject( subject )
msg.setText( message )
Transport transporter = lSession.getTransport("smtp")
transporter.connect()
transporter.send( msg )
}
def getEnviron( computer ) {
def env
def thread = Thread.start( "Getting env from ${computer.name}", { env = computer.environment } )
thread.join(2000)
if ( thread.isAlive() ) thread.interrupt()
env
}
def agentAccessible( computer ) {
getEnviron(computer)?.get('PATH') != null
}
def numberOfflineNodes = 0
def numberNodes = 0
for ( agent in jenkins.getNodes() ) {
def computer = agent.computer
numberNodes ++
println "\nChecking computer ${computer.name}:"
def isOK = ( agentAccessible(computer) && !computer.offline )
if ( isOK ) {
println "\t\tOK, got PATH back from slave ${computer.name}."
println( '\tcomputer.isOffline: ' + slave.getComputer().isOffline() )
println( '\tcomputer.isTemporarilyOffline: ' + slave.getComputer().isTemporarilyOffline() )
println( '\tcomputer.getOfflineCause: ' + slave.getComputer().getOfflineCause() )
println( '\tcomputer.offline: ' + computer.offline )
} else {
numberOfflineNodes ++
println " ERROR: can't get PATH from agent ${computer.name}."
println( '\tcomputer.isOffline: ' + agent.getComputer().isOffline() )
println( '\tcomputer.isTemporarilyOffline: ' + agent.getComputer().isTemporarilyOffline() )
println( '\tcomputer.getOfflineCause: ' + agent.getComputer().getOfflineCause() )
println( '\tcomputer.offline: ' + computer.offline )
sendMail( computer.name, agent.getComputer().getOfflineCause().toString() )
if ( agent.getComputer().isTemporarilyOffline() ) {
if ( ! agent.getComputer().getOfflineCause().toString().contains("Disconnected by") ) {
computer.setTemporarilyOffline( false, agent.getComputer().getOfflineCause() )
}
} else {
computer.connect( true )
}
}
}
println ( "Number of Offline Nodes: " + numberOfflineNodes )
println ( "Number of Nodes: " + numberNodes )