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
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
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
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
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
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
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
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
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
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_s ⇒ Object
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 |