diff --git a/config/host-list.json b/config/host-list.json new file mode 100755 index 00000000..ce02f54c --- /dev/null +++ b/config/host-list.json @@ -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" + ] +} \ No newline at end of file diff --git a/config/login.yml.example b/config/login.yml.example index ab3b72a3..217f2bbb 100644 --- a/config/login.yml.example +++ b/config/login.yml.example @@ -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 diff --git a/lib/jenkins_api_client/cli/node.rb b/lib/jenkins_api_client/cli/node.rb index 9872f58e..40190e98 100644 --- a/lib/jenkins_api_client/cli/node.rb +++ b/lib/jenkins_api_client/cli/node.rb @@ -21,6 +21,7 @@ # require 'thor' +require 'json' require 'thor/group' require 'terminal-table' @@ -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 # diff --git a/lib/jenkins_api_client/node.rb b/lib/jenkins_api_client/node.rb index fc13b323..a6048944 100644 --- a/lib/jenkins_api_client/node.rb +++ b/lib/jenkins_api_client/node.rb @@ -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 @@ -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 diff --git a/scripts/login_with_irb.rb b/scripts/login_with_irb.rb index aff8f0cc..171ac29d 100644 --- a/scripts/login_with_irb.rb +++ b/scripts/login_with_irb.rb @@ -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) @@ -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