Class: JenkinsApi::Client::Node

Inherits:
Object
  • Object
show all
Includes:
UriHelper
Defined in:
lib/jenkins_api_client/node.rb

Overview

This class communicates with Jenkins "/computer" API to obtain details about nodes or slaves connected to the Jenkins.

Constant Summary

GENERAL_ATTRIBUTES =

General attributes of a node. This allows the following methods to be called on this node object. These methods are defined using define_method and are prefixed with get_.

def get_busyExecutors
def get_displayName
def get_totalExecutors
[
  "busyExecutors",
  "displayName",
  "totalExecutors"
].freeze
NODE_PROPERTIES =

Properties of a node. The following methods are defined to be called on the node object and are prefixed with is_ and end with ? as they return true or false.

def is_idle?(node_name)
def is_jnlpAgent?(node_name)
def is_launchSupported?(node_name)
def is_manualLaunchAllowed?(node_name)
def is_offline?(node_name)
def is_temporarilyOffline?(node_name)
[
  "idle",
  "jnlpAgent",
  "launchSupported",
  "manualLaunchAllowed",
  "offline",
  "temporarilyOffline"
].freeze
NODE_ATTRIBUTES =

Node specific attributes. The following methods are defined using define_method. These methods are prefixed with get_node_.

def get_node_numExecutors(node_name)
def get_node_icon(node_name)
def get_node_displayName(node_name)
def get_node_loadStatistics(node_name)
def get_node_monitorData(node_name)
def get_node_offlineCause(node_name)
def get_node_oneOffExecutors(node_name)
[
  "numExecutors",
  "icon",
  "displayName",
  "loadStatistics",
  "monitorData",
  "offlineCause",
  "oneOffExecutors"
].freeze

Instance Method Summary (collapse)

Methods included from UriHelper

#form_encode, #path_encode

Constructor Details

- (Node) initialize(client)

Initializes a new node object

Parameters:

  • client (Client)

    the client object



96
97
98
99
# File 'lib/jenkins_api_client/node.rb', line 96

def initialize(client)
  @client = client
  @logger = @client.logger
end

Instance Method Details

- (Object) change_mode(node_name, mode)

Changes the mode of a slave node in Jenkins

Parameters:

  • node_name (String)

    name of the node to change mode for

  • mode (String)

    mode to change to



272
273
274
275
276
277
278
279
280
281
# File 'lib/jenkins_api_client/node.rb', line 272

def change_mode(node_name, mode)
  @logger.info "Changing the mode of '#{node_name}' to '#{mode}'"
  mode = mode.upcase
  xml = get_config(node_name)
  n_xml = Nokogiri::XML(xml)
  desc = n_xml.xpath("//mode").first
  desc.content = "#{mode.upcase}"
  xml_modified = n_xml.to_xml
  post_config(node_name, xml_modified)
end

- (Object) create_dump_slave(params)

Creates a new node with the specified parameters

Examples:

Create a Dump Slave

create_dump_slave(
  :name => "slave1",
  :slave_host => "10.10.10.10",
  :private_key_file => "/root/.ssh/id_rsa",
  :executors => 10,
  :labels => "slave, ruby"
)

Parameters:

  • params (Hash)

    parameters for creating a dump slave

    • :name name of the slave

    • :description description of the new slave

    • :executors number of executors

    • :remote_fs Remote FS root

    • :labels comma separated list of labels

    • :mode mode of the slave: normal, exclusive

    • :slave_host Hostname/IP of the slave

    • :slave_port Slave port

    • :private_key_file Private key file of master

    • :credentials_id Id for credential in Jenkins



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/jenkins_api_client/node.rb', line 130

def create_dump_slave(params)
  unless params[:name] && params[:slave_host] && params[:private_key_file]
    raise ArgumentError, "Name, slave host, and private key file are" +
      " required for creating a slave."
  end

  @logger.info "Creating a dump slave '#{params[:name]}'"
  @logger.debug "Creating a dump slave with params: #{params.inspect}"
  default_params = {
    :description => "Automatically created through jenkins_api_client",
    :executors => 2,
    :remote_fs => "/var/jenkins",
    :labels => params[:name],
    :slave_port => 22,
    :mode => "normal",
    :private_key_file => "",
    :credentials_id => ""
  }

  params = default_params.merge(params)
  labels = params[:labels].split(/\s*,\s*/).join(" ")
  mode = params[:mode].upcase

  post_params = {
    "name" => params[:name],
    "type" => "hudson.slaves.DumbSlave$DescriptorImpl",
    "json" => {
      "name" => params[:name],
      "nodeDescription" => params[:description],
      "numExecutors" => params[:executors],
      "remoteFS" => params[:remote_fs],
      "labelString" => labels,
      "mode" => mode,
      "type" => "hudson.slaves.DumbSlave$DescriptorImpl",
      "retentionStrategy" => {
        "stapler-class" => "hudson.slaves.RetentionStrategy$Always"
      },
      "nodeProperties" => {
        "stapler-class-bag" => "true"
      },
      "launcher" => {
        "stapler-class" => "hudson.plugins.sshslaves.SSHLauncher",
        "host" => params[:slave_host],
        "port" => params[:slave_port],
        "username" => params[:slave_user],
        "privatekey" => params[:private_key_file],
        "credentialsId" => params[:credentials_id]
      }
    }.to_json
  }
  @logger.debug "Modified params posted to create slave:" +
    " #{post_params.inspect}"
  @client.api_post_request("/computer/doCreateItem", post_params)
end

- (Object) delete(node_name)

Deletes the specified node

Parameters:

  • node_name (String)

    Name of the node to delete



189
190
191
192
193
194
195
196
# File 'lib/jenkins_api_client/node.rb', line 189

def delete(node_name)
  @logger.info "Deleting node '#{node_name}'"
  if list.include?(node_name)
    @client.api_post_request("/computer/#{path_encode node_name}/doDelete")
  else
    raise "The specified node '#{node_name}' doesn't exist in Jenkins."
  end
end

- (Object) delete_all!

Note:

This method will remove all slaves from Jenkins. Please use with caution.

Deletes all slaves from Jenkins. The master will be the only node alive after the exection of this call.



204
205
206
207
# File 'lib/jenkins_api_client/node.rb', line 204

def delete_all!
  @logger.info "Deleting all nodes (except master) from jenkins"
  list.each { |node| delete(node) unless node == "master" }
end

- (Object) get_config(node_name)

Obtains the configuration of node from Jenkins server

Parameters:

  • node_name (String)

    name of the node



287
288
289
290
291
# File 'lib/jenkins_api_client/node.rb', line 287

def get_config(node_name)
  @logger.info "Obtaining the config.xml of node '#{node_name}'"
  node_name = "(master)" if node_name == "master"
  @client.get_config("/computer/#{ path_encode node_name}")
end

- (Object) index(node_name)

Identifies the index of a node name in the array node nodes

Parameters:

  • node_name (String)

    name of the node



230
231
232
233
234
235
# File 'lib/jenkins_api_client/node.rb', line 230

def index(node_name)
  response_json = @client.api_get_request("/computer")
  response_json["computer"].each_with_index do |computer, index|
    return index if computer["displayName"] == node_name
  end
end

- (Object) list(filter = nil, ignorecase = true)

This method lists all nodes

Parameters:

  • filter (String) (defaults to: nil)

    a regex to filter node names

  • ignorecase (Bool) (defaults to: true)

    whether to be case sensitive or not



214
215
216
217
218
219
220
221
222
223
224
# File 'lib/jenkins_api_client/node.rb', line 214

def list(filter = nil, ignorecase = true)
  @logger.info "Obtaining nodes from jenkins matching filter '#{filter}'"
  node_names = []
  response_json = @client.api_get_request("/computer")
  response_json["computer"].each do |computer|
      if computer["displayName"] =~ /#{filter}/i
        node_names << computer["displayName"]
      end
  end
  node_names
end

- (Object) post_config(node_name, xml)

Posts the given config.xml to the Jenkins node

Parameters:

  • node_name (String)

    name of the node

  • xml (String)

    Config.xml of the node



298
299
300
301
302
# File 'lib/jenkins_api_client/node.rb', line 298

def post_config(node_name, xml)
  @logger.info "Posting the config.xml of node '#{node_name}'"
  node_name = "(master)" if node_name == "master"
  @client.post_config("/computer/#{path_encode node_name}/config.xml", xml)
end

- (Object) to_s

Gives the string representation of the Object



103
104
105
# File 'lib/jenkins_api_client/node.rb', line 103

def to_s
  "#<JenkinsApi::Client::Node>"
end