Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added functionality for add/delete node(s) #166

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions config/host-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"node_label": "label",
"executors": "2",
"template_detail": {
"node_username": "root",
"node_user_credentials_id": "xxxxxxxxxxx"
},
"host_list":[
"xxxx.xxx.com",
"yyyy.yyy.com"
]
}
4 changes: 4 additions & 0 deletions config/login.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

:password: my_password

# jenkins api token

:jenkins_api_token: ""

# The private key file for Jenkins CLI authentication
# remember to upload the public key to http://#{server_ip}:#{server_port}/user/#{my_username}/configure
:identity_file: ~/.ssh/id_rsa
84 changes: 84 additions & 0 deletions lib/jenkins_api_client/cli/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#

require 'thor'
require 'json'
require 'thor/group'
require 'terminal-table'

Expand Down Expand Up @@ -58,6 +59,89 @@ def print_general_attributes
puts table
end

desc "create", "creates new node(s)"
# CLI command that creates new node
option :json
option :name
option :slave_host
option :credentials_id
option :private_key_file
option :executors
option :labels
def create
@client = Helper.setup(parent_options)
if options[:json]
json_file = "#{options[:json]}"
file = File.read(json_file)
node_details = JSON.parse(file)
node_label = node_details['node_label']
executors = node_details['executors']
template_detail_hash = node_details['template_detail']
host_list_array = node_details['host_list']
host_list_array.each_with_index {|val, index|
host_name = "#{val}"
if ! template_detail_hash['node_user_credentials_id'].empty?
credentials_id = template_detail_hash['node_user_credentials_id']
else
raise "failed to find a credentials_id for "+template_detail_hash['node_username']+"\nPlease add this user in jenkins and #{options[:json]}"
end
@client.node.create_dumb_slave(
:name => host_name,
:slave_host => host_name,
:credentials_id => credentials_id,
:private_key_file => "",
:executors => executors.empty? ? (2) : (executors),
:labels => node_label)
}
elsif options[:name] && options[:credentials_id] && options[:labels]
@client.node.create_dumb_slave(
:name => options[:name],
:slave_host => options[:name],
:credentials_id => options[:credentials_id],
:private_key_file => "",
:executors => options[:executors].empty? ? (2) : (options[:executors]),
:labels => options[:labels])
else
@client.logger.info "incorrect usage.\n"

end
end

desc "delete","deletes a given node or a list of nodes in json file"
# CLI command that deletes node
option :json
option :node_name

def delete
@client = Helper.setup(parent_options)
if options[:json]
json_file = "#{options[:json]}"
file = File.read(json_file)
node_details = JSON.parse(file)
template_detail_hash = node_details['template_detail']
host_list_array = node_details['host_list']
host_list_array.each_with_index {|val, index|
host_name = "#{val}"
node_result = @client.node.index(host_name)
if ! node_result.is_a? Enumerable
@client.node.delete(host_name)
else
@client.logger.info "node not found : #{host_name}\n"
end
}
elsif options[:node_name]
node_result = @client.node.index(options[:node_name])
if ! node_result.is_a? Enumerable
@client.node.delete(options[:node_name])
else
@client.logger.info "node not found : #{options[:node_name]}\n"
end
else
@client.logger.info "incorrect usage\n"
end
end


desc "print_node_attributes NODE", "Prints attributes specific to a node"
# CLI command to print the attributes specific to a node
#
Expand Down
3 changes: 1 addition & 2 deletions lib/jenkins_api_client/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def to_s
# )
#
def create_dumb_slave(params)
unless params[:name] && params[:slave_host] && params[:private_key_file]
unless params[:name] && params[:slave_host] && (params[:private_key_file] || params[:credentials_id])
raise ArgumentError, "Name, slave host, and private key file are" +
" required for creating a slave."
end
Expand Down Expand Up @@ -256,7 +256,6 @@ def index(node_name)
@logger.info "Obtaining '#{meth_suffix}' property of '#{node_name}'"
response_json = @client.api_get_request("/computer")
resp = response_json["computer"][index(node_name)]["#{meth_suffix}"]
resp =~ /False/i ? false : true
end
end

Expand Down
8 changes: 7 additions & 1 deletion scripts/login_with_irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def prompt_for_password
get_from_stdin("Password: ", true)
end

def prompt_for_jenkins_api_token
get_from_stdin("Jenkins api token for #{client_opts[:username]}: ", false)
end

def get_from_stdin(prompt, mask = false)
$stdout.write(prompt)

Expand Down Expand Up @@ -45,7 +49,9 @@ def get_from_stdin(prompt, mask = false)
unless client_opts.has_key?(:password) or client_opts.has_key?(:password_base64)
client_opts[:password] = prompt_for_password()
end

unless client_opts.has_key?(:jenkins_api_token)
client_opts[:jenkins_api_token] = prompt_for_jenkins_api_token()
end
@client = JenkinsApi::Client.new(client_opts)
puts "logged-in to the Jenkins API, use the '@client' variable to use the client"
end
Expand Down