Search code examples
jenkinsgroovyjenkins-pipelinejenkins-groovycloudbees

How to print AmazonEC2 Slave details via groovy script in Jenkins Script Console


I am using CloudBees Jenkins. We are configuring dynamic runners (or slaves) via AmazonEC2 Plugin where we mention runner details like AMI ID, Key, Region, Labels etc and the runners are created based on that information.

I want to write a Groovy Script that I want to run in Jenkins Script Console to see the details of all such runner details configured.

I can see that all the information I need is stored in hudson.plugins.ec2.SlaveTemplate class.

But I cant see any method to get this class instance. I have managed to write this one which only prints the labels of the configured slaves

Jenkins.instance.clouds
Jenkins.instance.clouds.each {
 
println it.name
}

Can anyone help to print all (or atleast some like AMI ID, region etc) via groovy?


Solution

  • I have found the answer myself. Sharing here if this is helpful to other people:

    import hudson.plugins.ec2.AmazonEC2Cloud
    import hudson.plugins.ec2.SlaveTemplate
    import hudson.slaves.Cloud
    import com.cloudbees.opscenter.client.cloud.CloudImpl
    
    
    Jenkins.instance.clouds.each {
      
      if(!(it instanceof CloudImpl)){
        
        
        Cloud cloud = it
        AmazonEC2Cloud ac = cloud
        
        List<SlaveTemplate>  stL = ac.getTemplates()
        
        for (SlaveTemplate st : stL){
          
          if(st == null){
           println "SlaveTemplate is null" 
          } else{
           
            println st.ami
            println st.labels 
          }
          
        }
        
        
      }
    
    

    If you want to print more details about your configured slaves, the class attribute names can be found at https://javadoc.jenkins.io/plugin/ec2/hudson/plugins/ec2/SlaveTemplate.html