Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an IP Address through Jenkins REST API?

I've been tasked with instituting some health checking on some Jenkins jobs. The idea is to get the job's status and an associated IP address through the Jenkins rest API, so I can use that information to interface with another restful API. I have created a groovy script that successfully parses through the Jenkins jobs and gets their status (whether or not they are running) but I have yet to find a way to associate these jobs with their IP addresses. Is there any way to get the IP address of a slave in Jenkins through the rest API, and if not, is there another way to get said IP address?

Here's the code I've got so far that works like a charm:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
import groovyx.net.http.RESTClient
import groovy.json.JsonSlurper

def jenkinsClient = new RESTClient( 'myJenkinsURL' )
def monitorClient = new RESTClient( 'myOtherRestfulAPIURL' )
monitorClient.auth.basic "<username>", "<pass>"
jenkinsClient.setHeaders(Accept: 'application/json')
monitorClient.setHeaders(Accept: 'application/json')

def jobs = []
def jenkinsGetJobs = jenkinsClient.get( path: 'view/Events/api/json', contentType: 'text/plain' )
def jenkinsGetJobsSlurp = new JsonSlurper().parse(jenkinsGetJobs.data)
for (def j in jenkinsGetJobsSlurp.jobs ){
    jobs.add(j.name)
}
//Can we get a list of IPS?

for(def job in jobs){
        def jenkinsResp = jenkinsClient.get( path : 'view/Events/job/' + job + '/api/json', contentType: 'text/plain', query: [depth:"1"])
        def jenkinsSlurp = new JsonSlurper().parse(jenkinsResp.data)
       // println slurp
        if (jenkinsSlurp.builds[0].building == true){
            println "The " + job + " job is running."
            //Make a call to other Restful API here

        }
        if (jenkinsSlurp.builds[0].building == false){
            println "The " + job + " job is not running."
        }
}

In the commented section labeled //can we get a list of IPS? I would like to somehow use the Jenkins Rest API to get a list of the IPs of the Jenkins slaves.

Can I do this through the rest API? And if not, is there another way? Through the CLI, perhaps? I haven't seen a getIP() method anywhere in the Jenkins API documentation but I am fairly new to this so I might just be missing something simple.

like image 449
Jake Meacham Avatar asked Jul 28 '15 21:07

Jake Meacham


People also ask

How do I find the IP address of Jenkins?

Through the Script Console (Manage Jenkins -> Nodes -> Select a node -> Script Console) of the node we can execute groovy script. Run the following command to get the IP address. To get the actual IP address (as requested originally) I used the Script Console, but executed println InetAddress. localHost.

How do I access REST API Jenkins?

Jenkins has a link to their REST API in the bottom right of each page. This link appears on every page of Jenkins and points you to an API output for the exact page you are browsing. That should provide some understanding into how to build the API URls.

Does Jenkins have a REST API?

The jenkins-rest library is an object oriented Java project that provides access to the Jenkins REST API programmatically to some remote API Jenkins provides. It is built using the jclouds toolkit and can easily be extended to support more REST endpoints.


1 Answers

You can execute groovy script on your slave via REST API, thus can get slave's ip address. Here is an example with curl, but you can adjust it to use in your code:

$ curl -u username:password -d "script=println InetAddress.localHost.hostAddress" jenkins_url/computer/node_name/scriptText
# 192.168.0.104

Node: to get an ip-address of a particular slave, you have to know it's name. That's easy to grad nodes names querying jenkins_url/computer/api/json

I am going to try scraping the HTML of the node page to grab the IP from the swarm slave description

This will not always work as slave may be connected via JNLP and you will not have an IP on that HTML page.

like image 159
Vitalii Elenhaupt Avatar answered Sep 30 '22 09:09

Vitalii Elenhaupt