Tuesday, July 24, 2018

#Throwback: A script that will launch Jenkins slave to a remote Windows PC

Background:There are times when our Master Jenkins would go offline due to various reasons (network issues usually). Our Slave machines will then end up with their Jenkins agents in a dead state basically having to manually intervene to recover the connection. 


Assumptions:

  • Windows-to-Windows only since it uses Windows specific tool (PsExec.exe).
  • Target Slave machine has svn command-line client installed because the script would make slave PC to checkout the scripts first from an svn repo.
  • Will only work with lower versions of Jenkins (specifically, Jenkins versions that are built against Java 7 and below)
-- Modify as per your need --


Approach:

  1. The main script (slave_launcher.bat) would connect to the remote Slave Windows machine via PsExec.exe
  • Checks out the tool in the remote slave server
  • Executes kill.bat - which kills any running "javaw.exe" process. Our machines launch javaw.exe for Jenkins only. If your machine is using javaw.exe for other purposes, you have to modify this part.
  • Executes generate_slave.bat - generates the new slave bat file with values supplied in main script then runs it.

Main Script: slave_launcher.bat
Sample command:  slave_launcher.bat http://localhost:8080 10.69.243.14 user1 password123 svn://svnhost:5800/CI_Script/batch/ci_slave_launcher

Final notes: 

  • PsExec.exe is glitchy. It may not run at first attempt but could become successful on the second try. Test it out part by part first.
  • Creating a job in your Master jenkins for this script can be helpful. 
  • Again, modify as per your need, ex replacing "svn checout" with "FTP transfer", etc. This was just an impromptu script that we had to do because of the sheer number of slaves that we had to maintain.