Nodes

Nodes

Search for a node

nodes.Nodes.search()

Search for a node by name.

Parameters:

name (str) – The name of the node to search for.

Returns:

The Node object if found, otherwise raise an exception.

Return type:

Node

Raises:

JenkinsNotFound – If the node with the specified name is not found.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").url)

The above code will output:

https://JenkinsDNS/computer/sdk_test

Create a node

nodes.Nodes.create()

Create a new node with the given name and configuration.

Parameters:
  • name (str) – The name of the node to create.

  • json (dict or json or Builder.Node) – The JSON configuration of the node, either as a dictionary, an json object, or a Builder.Node object.

Returns:

JenkinsActionObject representing the create action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")

from jenkins_pysdk.builders import Builder
config = Builder.Node(description="test 2", remote_fs="/jen")
print(jenkins.nodes.create("docs_node", config))

The above code will output:

request=<Request object at 1660092329280> content='[200] Successfully created node (docs_node).' status_code=200

Iterate all nodes

nodes.Nodes.iter()

Iterate over the nodes.

Returns:

A generator yielding Node objects.

Return type:

Generator[jenkins_pysdk.nodes.Node]

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
for node in jenkins.nodes.iter():
   print(node.url)

The above code will output:

https://JenkinsDNS/computer/(built-in)
https://JenkinsDNS/computer/docs_node
https://JenkinsDNS/computer/sdk_test
https://JenkinsDNS/computer/te-st
https://JenkinsDNS/computer/testing

List all nodes

nodes.Nodes.list()

Get a list of all nodes.

Returns:

A list of Node objects.

Return type:

List[jenkins_pysdk.nodes.Node]

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.list())

The above code will output:

[<jenkins_pysdk.nodes.Node object at 0x000002000BC05D20>, <jenkins_pysdk.nodes.Node object at 0x000002000BC05BD0>, <truncated...>]

Get total nodes

nodes.Nodes.total()

Get the total number of nodes.

Returns:

The total number of nodes.

Return type:

int

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.total)

The above code will output:

5

Node

Get the node name

nodes.Node.name()

The name of the node.

Returns:

The name of the node.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").name)

The above code will output:

sdk_test

Get the node URL

nodes.Node.url()

The URL of the node.

Returns:

The URL of the node.

Return type:

str

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").url)

The above code will output:

https://JenkinsDNS/computer/sdk_test

Check if the node is idle

nodes.Node.idle()

Whether the node is idle or not.

Returns:

1 if the node is idle, 0 otherwise.

Return type:

int

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
node = jenkins.nodes.search("sdk_test")
print(node.idle)

The above code will output:

True

Get the node config

nodes.Node.config()

Get the configuration of the node.

Returns:

The configuration of the node as a string.

Return type:

str

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
node = jenkins.nodes.search("sdk_test")
print(node.config)

The above code will output:

<?xml version="1.1" encoding="UTF-8"?>
<slave>
  <name>sdk_test</name>
  <description>test 2</description>
  <remoteFS>/jen</remoteFS>
  <numExecutors>1</numExecutors>
  <mode>NORMAL</mode>
  <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
  <launcher class="hudson.slaves.JNLPLauncher">
    <workDirSettings>
      <disabled>false</disabled>
      <internalDir>remoting</internalDir>
      <failIfWorkDirIsMissing>false</failIfWorkDirIsMissing>
    </workDirSettings>
    <webSocket>false</webSocket>
  </launcher>
  <label></label>
  <nodeProperties/>
</slave>

Reconfigure the node

nodes.Node.reconfig()

Reconfigure the node with the provided XML configuration.

Parameters:

xml (str) – The XML configuration to apply to the node.

Returns:

JenkinsActionObject representing the reconfiguration action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
node = jenkins.nodes.search("sdk_test")
config = """
   <slave>
     <name>sdk_test</name>
     <description>my new description</description>
     <remoteFS>/jenkins/new</remoteFS>
     <numExecutors>1</numExecutors>
     <mode>NORMAL</mode>
     <retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
     <launcher class="hudson.slaves.JNLPLauncher">
       <workDirSettings>
         <disabled>false</disabled>
         <internalDir>remoting</internalDir>
         <failIfWorkDirIsMissing>false</failIfWorkDirIsMissing>
       </workDirSettings>
       <webSocket>false</webSocket>
     </launcher>
     <label></label>
     <nodeProperties/>
   </slave>
   """
print(jenkins.nodes.search("sdk_test").reconfig(config))

The above code will output:

request=<Request object at 1423581162816> content='[200] Successfully reconfigured sdk_test.' status_code=200

Disable the node

nodes.Node.disable()

Disable the node with an optional message.

Parameters:

message (str) – Optional message explaining the reason for disabling the node.

Returns:

JenkinsActionObject representing the disable action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").disable("disabling for test"))

The above code will output:

request=<Request object at 2424196147344> content='[200] Successfully marked node (sdk_test) as offline.' status_code=200

Enable the node

nodes.Node.enable()

Enable action for the node.

Returns:

JenkinsActionObject representing the delete action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

Raises:

JenkinsGeneralException – If a general exception occurs.

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").enable())

The above code will output:

request=<Request object at 2289828053616> content='[200] Successfully marked node (sdk_test) as online.' status_code=200

Delete the node

nodes.Node.delete()

Delete action for the node.

Returns:

JenkinsActionObject representing the delete action.

Return type:

jenkins_pysdk.objects.JenkinsActionObject

from jenkins_pysdk.jenkins import Jenkins
jenkins = Jenkins(host="JenkinsDNS", username="admin", token="11e8e294cee85ee88b60d99328284d7608")
print(jenkins.nodes.search("sdk_test").delete())

The above code will output:

request=<Request object at 2401019053696> content='[200] Successfully deleted node (sdk_test).' status_code=200