通过Jenkins REST API获取IP地址? [英] Getting an IP Address through Jenkins REST API?

查看:245
本文介绍了通过Jenkins REST API获取IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直负责对Jenkins的一些工作进行一些健康检查。这个想法是通过Jenkins rest API获得工作状态和相关的IP地址,以便我可以使用该信息与另一个restful API进行交互。我创建了一个groovy脚本,可以成功解析Jenkins作业并获取它们的状态(无论它们是否正在运行),但我还没有找到将这些作业与其IP地址相关联的方法。有没有办法通过其余的API获取Jenkins中奴隶的IP地址,如果没有,还有另一种获得IP地址的方法吗?



这里是代码到目前为止,它的功能就像一个魅力:

  @Grab(group ='org.codehaus.groovy.modules .http-builder',module ='http-builder',version ='0.7')
导入groovyx.net.http.RESTClient
导入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)
}
//我们可以获得IPS列表吗?
$ b $ for(def jobs 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+ job +作业正在运行。
//在此处调用其他Restful API

$ b if(jenkinsSlurp.builds [0] .building == false){
println +作业+作业未运行。




$ b $ p $在标注为//的注释部分中, IPS列表?我想以某种方式使用Jenkins Rest API来获取詹金斯奴隶的IP列表。



我可以通过其他API来完成此操作吗?如果不是,还有另一种方法吗?通过CLI,也许?我在Jenkins API文档中的任何地方都没有看到getIP()方法,但是我对此很新,所以我可能只是缺少一些简单的东西。

解决方案

你可以通过REST API在你的slave上执行groovy脚本,从而获得slave的ip地址。这里有一个卷曲的例子,但你可以调整它在代码中使用:

  $ curl -u username:password  - dscript = println InetAddress.localHost.hostAddressjenkins_url / computer / node_name / scriptText 
#192.168.0.104

节点:要获取特定奴隶的ip地址,您必须知道它的名称。这对于查询节点名称很容易 jenkins_url / computer / api / json


I我将尝试抓取节点页面的HTML以从swarm slave描述中获取IP

这并不总是作为从站可能通过JNLP连接,并且在该HTML页面上不会有IP。

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.

解决方案

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.

这篇关于通过Jenkins REST API获取IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆