Class: JenkinsApi::Client::Node

Inherits:
Object
  • Object
show all
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 collapse

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

Constructor Details

#initialize(client) ⇒ Node

Initializes a new node object

Parameters:

  • client (Object)

    reference to Client



91
92
93
# File 'lib/jenkins_api_client/node.rb', line 91

def initialize(client)
  @client = client
end

Instance Method Details

#change_mode(node_name, mode) ⇒ Object

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



258
259
260
261
262
263
264
265
266
# File 'lib/jenkins_api_client/node.rb', line 258

def change_mode(node_name, 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

#create_dump_slave(params) ⇒ Object

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



123
124
125
126
127
128
129
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
# File 'lib/jenkins_api_client/node.rb', line 123

def create_dump_slave(params)

  if list.include?(params[:name])
    raise "The specified slave '#{params[:name]}' already exists."
  end

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

  default_params = {
    :description => "Automatically created through jenkins_api_client",
    :executors => 2,
    :remote_fs => "/var/jenkins",
    :labels => params[:name],
    :slave_port => 22,
    :mode => "normal"
  }

  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],
      }
    }.to_json
  }

  @client.api_post_request("/computer/doCreateItem", post_params)
end

#delete(node_name) ⇒ Object

Deletes the specified node

Parameters:

  • node_name (String)

    Name of the node to delete



181
182
183
184
185
186
187
# File 'lib/jenkins_api_client/node.rb', line 181

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

#delete_all!Object

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.



195
196
197
# File 'lib/jenkins_api_client/node.rb', line 195

def delete_all!
  list.each { |node| delete(node) unless node == "master" }
end

#get_config(node_name) ⇒ Object

Obtains the configuration of node from Jenkins server

Parameters:

  • node_name (String)

    name of the node



272
273
274
275
# File 'lib/jenkins_api_client/node.rb', line 272

def get_config(node_name)
  node_name = "(master)" if node_name == "master"
  @client.get_config("/computer/#{node_name}")
end

#index(node_name) ⇒ Object

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

Parameters:

  • node_name (String)

    name of the node



219
220
221
222
223
224
# File 'lib/jenkins_api_client/node.rb', line 219

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

#list(filter = nil, ignorecase = true) ⇒ Object

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



204
205
206
207
208
209
210
211
212
213
# File 'lib/jenkins_api_client/node.rb', line 204

def list(filter = nil, ignorecase = true)
  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

#post_config(node_name, xml) ⇒ Object

Posts the given config.xml to the Jenkins node

Parameters:

  • node_name (String)

    name of the node

  • xml (String)

    Config.xml of the node



282
283
284
285
# File 'lib/jenkins_api_client/node.rb', line 282

def post_config(node_name, xml)
  node_name = "(master)" if node_name == "master"
  @client.post_config("/computer/#{node_name}/config.xml", xml)
end

#to_sObject

Gives the string representation of the Object



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

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