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?
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.
For example: agent { label 'my-label1 && my-label2' } or agent { label 'my-label1 || my-label2' } .
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
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With