Home > OS >  Get from jenkins list of nodes by label - by REST API
Get from jenkins list of nodes by label - by REST API

Time:05-30

I need to GET list of nodes that contains certain label.

I know how to do that by getting entire nodes list by using Jenkins REST API and then getting node by node also REST API and checking its labels - but its to many API calls. I also can create some job that writing to some place nodes list by label as parameter - but its bad way as Jenkins job that triggered remotely have no return value and I cant know it finished and will need read results from some other place the job saved it there.

I need some way that by one API call I will get nodes list contains a given label.

CodePudding user response:

You can run a single API call to <JENKINS_URL>/computer/api/json (or <JENKINS_URL>/computer/api/python for a python api) which return a list of all nodes and their properties.
One of the properties is the label - so just go over all nodes and extract the ones that contain your needed label.
Here is an example for the returned object:

{
  "_class" : "hudson.model.ComputerSet",
  "busyExecutors" : 0,
  "computer" : [
    {
      "_class" : "hudson.model.Hudson$MasterComputer",
      "actions" : [
        
      ],
      "assignedLabels" : [
        {
          "name" : "built-in"
        }
      ],
      "description" : "the Jenkins controller's built-in node",
      "displayName" : "Built-In Node",
      "executors" : [
        {
          
        },
        {
          
        }
      ],
      "icon" : "symbol-computer",
      "iconClassName" : "symbol-computer",
      "idle" : true,
      "jnlpAgent" : false,
      "launchSupported" : true,
      "loadStatistics" : {
        "_class" : "hudson.model.Label$1"
      },
      "manualLaunchAllowed" : true,
      "monitorData" : {
        "hudson.node_monitors.SwapSpaceMonitor" : {
          "_class" : "hudson.node_monitors.SwapSpaceMonitor$MemoryUsage2",
          "availablePhysicalMemory" : 6938730496,
          "availableSwapSpace" : 6906019840,
          "totalPhysicalMemory" : 16885276672,
          "totalSwapSpace" : 21046026240
        },
        "hudson.node_monitors.TemporarySpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1653907906021,
          "path" : "C:\\Windows\\Temp",
          "size" : 426696622080
        },
        "hudson.node_monitors.DiskSpaceMonitor" : {
          "_class" : "hudson.node_monitors.DiskSpaceMonitorDescriptor$DiskSpace",
          "timestamp" : 1653907905929,
          "path" : "C:\\ProgramData\\Jenkins\\.jenkins",
          "size" : 426696622080
        },
        "hudson.node_monitors.ArchitectureMonitor" : "Windows 10 (amd64)",
        "hudson.node_monitors.ResponseTimeMonitor" : {
          "_class" : "hudson.node_monitors.ResponseTimeMonitor$Data",
          "timestamp" : 1653907905941,
          "average" : 0
        },
        "hudson.node_monitors.ClockMonitor" : {
          "_class" : "hudson.util.ClockDifference",
          "diff" : 0
        }
      },
      "numExecutors" : 2,
      "offline" : false,
      "offlineCause" : null,
      "offlineCauseReason" : "",
      "oneOffExecutors" : [
        
      ],
      "temporarilyOffline" : false
    }
  ],
  "displayName" : "Nodes",
  "totalExecutors" : 2
}

You are interested in the assignedLabels object - notice that it can contain multiple labels.

  • Related