Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node labels from jenkins api

Tags:

jenkins

Is there any way I can extract node labels from jenkins API? The standard:

{base_url}/computer/{node}/api

did not seem to have any label information. Is it in some other place?

like image 976
Oleksiy Avatar asked Jan 25 '13 23:01

Oleksiy


People also ask

What is Jenkins node label?

When creating a new slave node, Jenkins allows us to tag a slave node with a label. Labels represent a way of naming one or more slaves. We leverage this labeling system to tie the execution of a job directly to one or more slave nodes.

How do I add multiple labels in Jenkins?

For example: agent { label 'my-label1 && my-label2' } or agent { label 'my-label1 || my-label2' } .


2 Answers

Apparently, node labels are part of node configuration, so they live in

{base_url}/computer/{node_str}/config.xml

Here is my hack to access that through python jenkinsapi (similar to job configuration), from node_str

import xml.etree.ElementTree as ET
from jenkinsapi.jenkins import Jenkins

j = Jenkins(...)
n = j.get_node(node_str)
response = n.jenkins.requester.get_and_confirm_status( "%(baseurl)s/config.xml" % n.__dict__)
_element_tree = ET.fromstring(response.text)
node_labels = _element_tree.find('label').text
like image 113
Oleksiy Avatar answered Nov 15 '22 11:11

Oleksiy


The ruby client offers a way to obtain the configuration XML file through a call. That file can then be processed to extract the label information.

require "rubygems"
require "jenkins_api_client"

# Initialize the client by passing in the server information
# and credentials to communicate with the server
client = JenkinsApi::Client.new(
  :server_ip => "127.0.0.1",
  :username => "awesomeuser",
  :password => "awesomepassword"
)

# Obtain the XML of the desired node
xml = client.node.get_config("nodename")

# Extract label information
xml =~ /<label>(.*)<\/label)/

# As we can have multiple space-separated labels, we need to split them
labels = []
$1.split(" ").each { |label| labels << label }
like image 25
arangamani Avatar answered Nov 15 '22 11:11

arangamani