Getting an IP Address through Jenkins REST API?

别说谁变了你拦得住时间么 提交于 2020-01-02 04:35:07

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31687139/getting-an-ip-address-through-jenkins-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!