Class: JenkinsApi::Client::Node
- Inherits:
-
Object
- Object
- JenkinsApi::Client::Node
- Includes:
- UriHelper
- Defined in:
- lib/improved_jenkins_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
-
#change_mode(node_name, mode) ⇒ Object
Changes the mode of a slave node in Jenkins.
-
#create_dumb_slave(params) ⇒ Object
Creates a new node with the specified parameters.
- #create_dump_slave(params) ⇒ Object
-
#delete(node_name) ⇒ Object
Deletes the specified node.
-
#delete_all! ⇒ Object
Deletes all slaves from Jenkins.
-
#get_config(node_name) ⇒ Object
Obtains the configuration of node from Jenkins server.
-
#index(node_name) ⇒ Object
Identifies the index of a node name in the array node nodes.
-
#initialize(client) ⇒ Node
constructor
Initializes a new node object.
-
#list(filter = nil, ignorecase = true, slaveonly = false) ⇒ Object
This method lists all nodes.
-
#online_offline_lists(filter = nil, ignorecase = true) ⇒ Object
This method returns two lists 1) nodes online 2) nodes offline.
-
#post_config(node_name, xml) ⇒ Object
Posts the given config.xml to the Jenkins node.
-
#to_s ⇒ Object
Gives the string representation of the Object.
-
#toggle_temporarilyOffline(node_name, reason = "") ⇒ Object
Toggles the temporarily offline state of the Jenkins node.
Methods included from UriHelper
Constructor Details
#initialize(client) ⇒ Node
Initializes a new node object
96 97 98 99 |
# File 'lib/improved_jenkins_client/node.rb', line 96 def initialize(client) @client = client @logger = @client.logger end |
Instance Method Details
#change_mode(node_name, mode) ⇒ Object
Changes the mode of a slave node in Jenkins
303 304 305 306 307 308 309 310 311 312 |
# File 'lib/improved_jenkins_client/node.rb', line 303 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 |
#create_dumb_slave(params) ⇒ Object
Creates a new node with the specified parameters
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/improved_jenkins_client/node.rb', line 130 def create_dumb_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 dumb slave '#{params[:name]}'" @logger.debug "Creating a dumb slave with params: #{params.inspect}" default_params = { :description => "Automatically created through improved_jenkins_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 |
#create_dump_slave(params) ⇒ Object
185 186 187 188 |
# File 'lib/improved_jenkins_client/node.rb', line 185 def create_dump_slave(params) @logger.warn '[DEPRECATED] Please use create_dumb_slave instead.' create_dumb_slave(params) end |
#delete(node_name) ⇒ Object
Deletes the specified node
194 195 196 197 198 199 200 201 |
# File 'lib/improved_jenkins_client/node.rb', line 194 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 |
#delete_all! ⇒ Object
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.
209 210 211 212 |
# File 'lib/improved_jenkins_client/node.rb', line 209 def delete_all! @logger.info "Deleting all nodes (except master) from jenkins" list.each { |node| delete(node) unless node == "master" } end |
#get_config(node_name) ⇒ Object
Obtains the configuration of node from Jenkins server
318 319 320 321 322 |
# File 'lib/improved_jenkins_client/node.rb', line 318 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 |
#index(node_name) ⇒ Object
Identifies the index of a node name in the array node nodes
259 260 261 262 263 264 |
# File 'lib/improved_jenkins_client/node.rb', line 259 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, slaveonly = false) ⇒ Object
This method lists all nodes
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/improved_jenkins_client/node.rb', line 241 def list(filter = nil, ignorecase = true, slaveonly = false) @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 unless slaveonly && computer["displayName"] == "master" node_names << computer["displayName"] end end end node_names end |
#online_offline_lists(filter = nil, ignorecase = true) ⇒ Object
This method returns two lists 1) nodes online 2) nodes offline
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/improved_jenkins_client/node.rb', line 219 def online_offline_lists(filter = nil, ignorecase = true) @logger.info "Obtaining nodes from jenkins matching filter '#{filter}'" offline_node_names = [] online_node_names = [] response_json = @client.api_get_request("/computer") response_json["computer"].each do |computer| if computer["displayName"] =~ /#{filter}/i if computer["offline"] == true offline_node_names << computer["displayName"] else online_node_names << computer["displayName"] end end end return online_node_names, offline_node_names end |
#post_config(node_name, xml) ⇒ Object
Posts the given config.xml to the Jenkins node
329 330 331 332 333 |
# File 'lib/improved_jenkins_client/node.rb', line 329 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 |
#to_s ⇒ Object
Gives the string representation of the Object
103 104 105 |
# File 'lib/improved_jenkins_client/node.rb', line 103 def to_s "#<JenkinsApi::Client::Node>" end |
#toggle_temporarilyOffline(node_name, reason = "") ⇒ Object
Toggles the temporarily offline state of the Jenkins node
340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/improved_jenkins_client/node.rb', line 340 def toggle_temporarilyOffline(node_name, reason="") @logger.info "Toggling the temporarily offline status of of node '#{node_name}' with reason '#{reason}'" node_name = "(master)" if node_name == "master" previous_state = is_temporarilyOffline?(node_name) @client.api_post_request("/computer/#{path_encode node_name}/toggleOffline?offlineMessage=#{path_encode reason}") new_state = is_temporarilyOffline?(node_name) if new_state == previous_state raise "The specified node '#{node_name}' was unable to change offline state." end new_state end |