Class: JenkinsApi::Client::Node
- Inherits:
-
Object
- Object
- JenkinsApi::Client::Node
- 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
-
#change_mode(node_name, mode) ⇒ Object
Changes the mode of a slave node in Jenkins.
-
#create_dump_slave(params) ⇒ Object
Creates a new node with the specified parameters.
-
#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) ⇒ Object
This method lists all nodes.
-
#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.
Constructor Details
#initialize(client) ⇒ Node
Initializes a new node object
93 94 95 96 |
# File 'lib/jenkins_api_client/node.rb', line 93 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
265 266 267 268 269 270 271 272 273 274 |
# File 'lib/jenkins_api_client/node.rb', line 265 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_dump_slave(params) ⇒ Object
Creates a new node with the specified parameters
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 176 |
# File 'lib/jenkins_api_client/node.rb', line 126 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" } 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 } @logger.debug "Modified params posted to create slave:" + " #{post_params.inspect}" @client.api_post_request("/computer/doCreateItem", post_params) end |
#delete(node_name) ⇒ Object
Deletes the specified node
182 183 184 185 186 187 188 189 |
# File 'lib/jenkins_api_client/node.rb', line 182 def delete(node_name) @logger.info "Deleting node '#{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
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.
197 198 199 200 |
# File 'lib/jenkins_api_client/node.rb', line 197 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
280 281 282 283 284 |
# File 'lib/jenkins_api_client/node.rb', line 280 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/#{node_name}") end |
#index(node_name) ⇒ Object
Identifies the index of a node name in the array node nodes
223 224 225 226 227 228 |
# File 'lib/jenkins_api_client/node.rb', line 223 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
207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/jenkins_api_client/node.rb', line 207 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 |
#post_config(node_name, xml) ⇒ Object
Posts the given config.xml to the Jenkins node
291 292 293 294 295 |
# File 'lib/jenkins_api_client/node.rb', line 291 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/#{node_name}/config.xml", xml) end |
#to_s ⇒ Object
Gives the string representation of the Object
100 101 102 |
# File 'lib/jenkins_api_client/node.rb', line 100 def to_s "#<JenkinsApi::Client::Node>" end |