From 55b7213cb6425d42eb84a3b260747fd3aec7f909 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 11 Mar 2015 10:08:26 +0100 Subject: [PATCH 01/33] initial serverspec libs --- .rspec | 1 + bin/bunka | 15 +++++++-- bunka.gemspec | 1 + lib/bunka.rb | 18 +++++++++-- lib/bunka/helpers.rb | 11 ++++--- lib/bunka/printers.rb | 18 ++++++----- lib/bunka/rspecinitializer.rb | 53 +++++++++++++++++++++++++++++++ lib/bunka/serverspec.rb | 57 ++++++++++++++++++++++++++++++++++ lib/bunka/serverspecprinter.rb | 37 ++++++++++++++++++++++ 9 files changed, 195 insertions(+), 16 deletions(-) create mode 100644 .rspec create mode 100644 lib/bunka/rspecinitializer.rb create mode 100644 lib/bunka/serverspec.rb create mode 100644 lib/bunka/serverspecprinter.rb diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..4e1e0d2 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color diff --git a/bin/bunka b/bin/bunka index ccdf384..384372f 100755 --- a/bin/bunka +++ b/bin/bunka @@ -1,6 +1,5 @@ #!/usr/bin/env ruby require 'bunka' -require 'rubygems' require 'thor' class BunkaCommand < Thor @@ -13,9 +12,21 @@ class BunkaCommand < Thor option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil - def test(command, query='name:*') + def test(command, query = 'name:*') Bunka.test(command, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + map '-ts' => :testserverspec + + desc 'testserverspec SERVERSPECFILE', 'test nodes with serverspec' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: '/.bunka/servers' + def testserverspec(serverspecfile) + Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/bunka.gemspec b/bunka.gemspec index b2ffe19..bb2f86b 100644 --- a/bunka.gemspec +++ b/bunka.gemspec @@ -17,4 +17,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'parallel' spec.add_dependency 'rake' spec.add_dependency 'thor' + spec.add_dependency 'process_shared' end diff --git a/lib/bunka.rb b/lib/bunka.rb index 57a6b9b..5b84f10 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -1,14 +1,15 @@ require 'parallel' - require 'bunka/bunka' require 'bunka/chef' require 'bunka/helpers' require 'bunka/printers' require 'bunka/ssh' +require 'bunka/serverspec' +require 'pry' class Bunka class << self - def test command, query, timeout_interval, verbose_success, invert, sequential, threads, file=nil + def test command, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil @command = command @invert = invert @query = query @@ -24,5 +25,18 @@ def test command, query, timeout_interval, verbose_success, invert, sequential, print_summary end + + def testserverspec serverspecfile, timeout_interval, verbose_success, invert, sequential, threads, file = '/.bunka/servers' + @serverspecfile = serverspecfile + @invert = invert + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file + + serverspecsetup + print_summary + end end end diff --git a/lib/bunka/helpers.rb b/lib/bunka/helpers.rb index 4183ff5..8055d6f 100644 --- a/lib/bunka/helpers.rb +++ b/lib/bunka/helpers.rb @@ -1,7 +1,10 @@ require 'bunka/printers' +require 'pry' +require 'process_shared' class Bunka class << self + def failed(reason) failed_output_stream.push reason print_fail @@ -16,19 +19,19 @@ def timed_out(reason) timeout_output_stream.push reason print_timeout end - + def timeout_output_stream @timeout_output_stream ||= Array.new end def failed_output_stream - @failed_output_stream ||= Array.new - end + @failed_output_stream ||= ProcessShared::SharedArray.new + end def success_output_stream @success_output_stream ||= Array.new end - + def verbose_success? @verbose_success end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index a2cbb7d..bba34c5 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -14,17 +14,17 @@ def print_timeout print '*'.yellow end - def print_timeout_stream - timeout_output_stream.each do |output| - puts output.yellow - end - end - def print_failed_stream failed_output_stream.each do |output| puts output.red end end + + def print_timeout_stream + timeout_output_stream.each do |output| + puts output.yellow + end + end def print_success_stream success_output_stream.each do |output| @@ -35,8 +35,9 @@ def print_success_stream def print_summary print "\n" print_timeout_stream - print_failed_stream - print_success_stream if verbose_success? + puts "\nErrors: ".red if @serverspecfile + print_failed_stream + print_success_stream if verbose_success? puts "\n---------------------------------------\n" @@ -44,6 +45,7 @@ def print_summary puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" puts "#{'Failed'.red}: #{failed_output_stream.count}" puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" + puts "\n" end end end diff --git a/lib/bunka/rspecinitializer.rb b/lib/bunka/rspecinitializer.rb new file mode 100644 index 0000000..0464fb5 --- /dev/null +++ b/lib/bunka/rspecinitializer.rb @@ -0,0 +1,53 @@ +require 'pry' +require 'bunka/helpers' + +class Bunka + class << self + def initializerspec + + # Can be done with bunka.nodes if also include knife search + if File.exist?(ENV['HOME']+@file) + @hosts = File.readlines(ENV['HOME']+@file).each {|l| l.chomp!} + else + puts 'Serverfile not found' + exit + end + + RSpec.configure do |c| + c.output_stream = nil + end + threads = @threads + @hosts.each_slice(threads).to_a.each do |h| + Parallel.map(h, in_processes: threads) do |hostx| + ENV['TARGET_HOST'] = hostx + config = RSpec.configuration + formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) + # create reporter with json formatter + reporter = RSpec::Core::Reporter.new(config) + config.instance_variable_set(:@reporter, reporter) + # internal hack + # api may not be stable, make sure lock down Rspec version + loader = config.send(:formatter_loader) + notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) + reporter.register_listener(formatter, *notifications) + begin + RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) + rescue RuntimeError + puts 'Serverspec failed' + end + @hash = formatter.output_hash + RSpec.clear_examples + @hash[:examples].each do |x| + if x[:status] == 'failed' + failed x[:full_description] + elsif x[:status] == 'passed' + succeeded x[:full_description] + else + timed_out x[:full_description] + end + end + end + end + end + end +end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb new file mode 100644 index 0000000..d84ecce --- /dev/null +++ b/lib/bunka/serverspec.rb @@ -0,0 +1,57 @@ +require 'rspec' +require 'colorize' +require 'pry' +require 'bunka/helpers' +require 'thread' + +class Bunka + class << self + def serverspecsetup + + # Can be done with bunka.nodes if also include knife search + if File.exist?(ENV['HOME']+@file) + @hosts = File.readlines(ENV['HOME']+@file).each {|l| l.chomp!} + else + puts 'Serverfile not found' + exit + end + + RSpec.configure do |c| + c.output_stream = nil + end + threads = @threads + @hosts.each_slice(threads).to_a.each do |h| + Parallel.map(h, in_processes: threads) do |hostx| + ENV['TARGET_HOST'] = hostx + config = RSpec.configuration + formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) + # create reporter with json formatter + reporter = RSpec::Core::Reporter.new(config) + config.instance_variable_set(:@reporter, reporter) + # internal hack + # api may not be stable, make sure lock down Rspec version + loader = config.send(:formatter_loader) + notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) + reporter.register_listener(formatter, *notifications) + begin + RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) + rescue RuntimeError + puts 'Serverspec failed' + end + @hash = formatter.output_hash + RSpec.clear_examples + @hash[:examples].each do |x| + if x[:status] == 'failed' + failed x[:full_description] + elsif x[:status] == 'passed' + succeeded x[:full_description] + else + timed_out x[:full_description] + end + end + end + end + end + end +end + diff --git a/lib/bunka/serverspecprinter.rb b/lib/bunka/serverspecprinter.rb new file mode 100644 index 0000000..07768d0 --- /dev/null +++ b/lib/bunka/serverspecprinter.rb @@ -0,0 +1,37 @@ + +class Bunka + class << self + def print_symbol_results + puts "\n" + @h[:examples].each do |x| + if x[:status] == 'failed' + @status << 'F'.red + @failed = @failed + 1 + end + if x[:status] == 'passed' + @status << '.'.green + @passed = @passed + 1 + end + if x[:status] == 'pending' + @status << 'P'.yellow + @pending = @pending + 1 + end + end + end + def print_number_results + @status.each do |x| + print x + end + @totaltest = @failed + @passed + @pending + puts "\n" + puts "\n"+'Total tests: '.blue + @totaltest.to_s + puts 'Failed: '.red + @failed.to_s + puts 'Passed: '.green + @passed.to_s + puts 'Pending: '.yellow + @pending.to_s + puts "\n"+'Errors:'.red + puts @errors + puts "\n"+'Duration:' + @h[:summary][:duration].to_s + end + end +end + From ca48cd175deedb1ed31c61936fbb528b06596ef7 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 26 Mar 2015 16:53:53 +0100 Subject: [PATCH 02/33] Back to the threads --- .gitignore | 1 + lib/bunka/helpers.rb | 8 +++----- lib/bunka/rspecinitializer.rb | 4 +++- lib/bunka/serverspec.rb | 14 ++++---------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index cec3cb5..a6af7bb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.gem Gemfile.lock +*.swp diff --git a/lib/bunka/helpers.rb b/lib/bunka/helpers.rb index 8055d6f..1ef218b 100644 --- a/lib/bunka/helpers.rb +++ b/lib/bunka/helpers.rb @@ -1,10 +1,8 @@ require 'bunka/printers' -require 'pry' -require 'process_shared' class Bunka class << self - + def failed(reason) failed_output_stream.push reason print_fail @@ -25,8 +23,8 @@ def timeout_output_stream end def failed_output_stream - @failed_output_stream ||= ProcessShared::SharedArray.new - end + @failed_output_stream ||= Array.new + end def success_output_stream @success_output_stream ||= Array.new diff --git a/lib/bunka/rspecinitializer.rb b/lib/bunka/rspecinitializer.rb index 0464fb5..a1db050 100644 --- a/lib/bunka/rspecinitializer.rb +++ b/lib/bunka/rspecinitializer.rb @@ -12,7 +12,7 @@ def initializerspec puts 'Serverfile not found' exit end - + @pid = 0 RSpec.configure do |c| c.output_stream = nil end @@ -40,12 +40,14 @@ def initializerspec @hash[:examples].each do |x| if x[:status] == 'failed' failed x[:full_description] + puts @pid elsif x[:status] == 'passed' succeeded x[:full_description] else timed_out x[:full_description] end end + @pid = @pid+1 end end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index d84ecce..34cbc92 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -1,8 +1,5 @@ require 'rspec' -require 'colorize' -require 'pry' require 'bunka/helpers' -require 'thread' class Bunka class << self @@ -15,13 +12,11 @@ def serverspecsetup puts 'Serverfile not found' exit end - RSpec.configure do |c| c.output_stream = nil end - threads = @threads - @hosts.each_slice(threads).to_a.each do |h| - Parallel.map(h, in_processes: threads) do |hostx| + threads = @threads + Parallel.map(@hosts, in_threads: 1) do |hostx| ENV['TARGET_HOST'] = hostx config = RSpec.configuration formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) @@ -40,9 +35,10 @@ def serverspecsetup end @hash = formatter.output_hash RSpec.clear_examples + binding.pry @hash[:examples].each do |x| if x[:status] == 'failed' - failed x[:full_description] + failed hostx +': '+x[:full_description] elsif x[:status] == 'passed' succeeded x[:full_description] else @@ -53,5 +49,3 @@ def serverspecsetup end end end -end - From 7c3141884fdcb0367a495eb563c63d1b316b7f5d Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 26 Mar 2015 16:56:00 +0100 Subject: [PATCH 03/33] delete unused files --- lib/bunka/rspecinitializer.rb | 55 ---------------------------------- lib/bunka/serverspec.rb | 1 - lib/bunka/serverspecprinter.rb | 37 ----------------------- 3 files changed, 93 deletions(-) delete mode 100644 lib/bunka/rspecinitializer.rb delete mode 100644 lib/bunka/serverspecprinter.rb diff --git a/lib/bunka/rspecinitializer.rb b/lib/bunka/rspecinitializer.rb deleted file mode 100644 index a1db050..0000000 --- a/lib/bunka/rspecinitializer.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'pry' -require 'bunka/helpers' - -class Bunka - class << self - def initializerspec - - # Can be done with bunka.nodes if also include knife search - if File.exist?(ENV['HOME']+@file) - @hosts = File.readlines(ENV['HOME']+@file).each {|l| l.chomp!} - else - puts 'Serverfile not found' - exit - end - @pid = 0 - RSpec.configure do |c| - c.output_stream = nil - end - threads = @threads - @hosts.each_slice(threads).to_a.each do |h| - Parallel.map(h, in_processes: threads) do |hostx| - ENV['TARGET_HOST'] = hostx - config = RSpec.configuration - formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) - # create reporter with json formatter - reporter = RSpec::Core::Reporter.new(config) - config.instance_variable_set(:@reporter, reporter) - # internal hack - # api may not be stable, make sure lock down Rspec version - loader = config.send(:formatter_loader) - notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) - reporter.register_listener(formatter, *notifications) - begin - RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) - rescue RuntimeError - puts 'Serverspec failed' - end - @hash = formatter.output_hash - RSpec.clear_examples - @hash[:examples].each do |x| - if x[:status] == 'failed' - failed x[:full_description] - puts @pid - elsif x[:status] == 'passed' - succeeded x[:full_description] - else - timed_out x[:full_description] - end - end - @pid = @pid+1 - end - end - end - end -end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 34cbc92..e9af3c7 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -35,7 +35,6 @@ def serverspecsetup end @hash = formatter.output_hash RSpec.clear_examples - binding.pry @hash[:examples].each do |x| if x[:status] == 'failed' failed hostx +': '+x[:full_description] diff --git a/lib/bunka/serverspecprinter.rb b/lib/bunka/serverspecprinter.rb deleted file mode 100644 index 07768d0..0000000 --- a/lib/bunka/serverspecprinter.rb +++ /dev/null @@ -1,37 +0,0 @@ - -class Bunka - class << self - def print_symbol_results - puts "\n" - @h[:examples].each do |x| - if x[:status] == 'failed' - @status << 'F'.red - @failed = @failed + 1 - end - if x[:status] == 'passed' - @status << '.'.green - @passed = @passed + 1 - end - if x[:status] == 'pending' - @status << 'P'.yellow - @pending = @pending + 1 - end - end - end - def print_number_results - @status.each do |x| - print x - end - @totaltest = @failed + @passed + @pending - puts "\n" - puts "\n"+'Total tests: '.blue + @totaltest.to_s - puts 'Failed: '.red + @failed.to_s - puts 'Passed: '.green + @passed.to_s - puts 'Pending: '.yellow + @pending.to_s - puts "\n"+'Errors:'.red - puts @errors - puts "\n"+'Duration:' + @h[:summary][:duration].to_s - end - end -end - From e8b6efd6c76decf8555df07f6f4d0ff662504107 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 27 Mar 2015 15:18:41 +0100 Subject: [PATCH 04/33] add socket --- lib/bunka/socket.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/bunka/socket.rb diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb new file mode 100644 index 0000000..2be9379 --- /dev/null +++ b/lib/bunka/socket.rb @@ -0,0 +1,15 @@ +require 'socket' + +class Bunka + class << self + def create_socket + server = TCPServer.open('localhost', 2000) # Socket to listen on port 2000 + loop { # Servers run forever + Thread.start(server.accept) do |client| + puts 'test geslaagd' + client.close + end + } + end + end +end From 65b3089f88b1fb91957746f6377c47f04c68c9bb Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 27 Mar 2015 16:55:17 +0100 Subject: [PATCH 05/33] Add socket in masterprocess - WIP --- lib/bunka.rb | 4 +++- lib/bunka/serverspec.rb | 14 ++++++++++++-- lib/bunka/socket.rb | 5 ++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 5b84f10..1463ab2 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -5,6 +5,7 @@ require 'bunka/printers' require 'bunka/ssh' require 'bunka/serverspec' +require 'bunka/socket' require 'pry' class Bunka @@ -34,7 +35,8 @@ def testserverspec serverspecfile, timeout_interval, verbose_success, invert, se @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file - + + create_socket serverspecsetup print_summary end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index e9af3c7..3563938 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -1,5 +1,7 @@ require 'rspec' require 'bunka/helpers' +require 'pry' +require 'socket' class Bunka class << self @@ -16,7 +18,8 @@ def serverspecsetup c.output_stream = nil end threads = @threads - Parallel.map(@hosts, in_threads: 1) do |hostx| + Parallel.map(@hosts, in_processes: threads) do |hostx| + RSpec::Core::Configuration#reset ENV['TARGET_HOST'] = hostx config = RSpec.configuration formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) @@ -35,15 +38,22 @@ def serverspecsetup end @hash = formatter.output_hash RSpec.clear_examples + + s = TCPSocket.open(hostname, port) + @hash[:examples].each do |x| if x[:status] == 'failed' - failed hostx +': '+x[:full_description] + puts 'sending...' + s.send(+ ': ' + x[:full_description]) + ng.pry + puts 'voorbij send' elsif x[:status] == 'passed' succeeded x[:full_description] else timed_out x[:full_description] end end + s.close # Close the socket when done end end end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 2be9379..8601f5f 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -3,12 +3,15 @@ class Bunka class << self def create_socket + puts 'creating socket' server = TCPServer.open('localhost', 2000) # Socket to listen on port 2000 + puts 'start loop' loop { # Servers run forever Thread.start(server.accept) do |client| - puts 'test geslaagd' + puts 'test geslaagd' client.close end + puts 'voorbij server' } end end From 30b4089fa4cff601b7a4548d497a8de6fc3865a5 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Tue, 31 Mar 2015 16:12:33 +0200 Subject: [PATCH 06/33] socketstransport --- lib/bunka.rb | 17 +++++++++++++---- lib/bunka/helpers.rb | 8 ++++++++ lib/bunka/printers.rb | 28 ++++++++++++++++++++++++++-- lib/bunka/serverspec.rb | 20 ++++++++++++-------- lib/bunka/socket.rb | 34 +++++++++++++++++++++++++++------- 5 files changed, 86 insertions(+), 21 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 1463ab2..9c3e800 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -35,10 +35,19 @@ def testserverspec serverspecfile, timeout_interval, verbose_success, invert, se @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file - - create_socket - serverspecsetup - print_summary + + @failedarray = Array.new + @successarray = Array.new + @timeoutarray = Array.new + + thread_1 = Thread.new do + create_failed_socket + end + thread_2 = Thread.new do + create_success_socket + end + serverspecsetup + print_summary end end end diff --git a/lib/bunka/helpers.rb b/lib/bunka/helpers.rb index 1ef218b..3282e35 100644 --- a/lib/bunka/helpers.rb +++ b/lib/bunka/helpers.rb @@ -7,6 +7,14 @@ def failed(reason) failed_output_stream.push reason print_fail end + + def failedspec + print_fail + end + + def successspec + print_success + end def succeeded(reason) success_output_stream.push reason diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index bba34c5..c1375b5 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -1,4 +1,5 @@ require 'colorize' +require 'pry' class Bunka class << self @@ -32,19 +33,42 @@ def print_success_stream end end + def print_specfailed_stream + @failedarray.reject! { |c| c.empty? } + @successarray.reject! { |c| c.empty? } + @failedarray.each do |output| + puts output.red + end + end + + def print_summary print "\n" print_timeout_stream puts "\nErrors: ".red if @serverspecfile + print_specfailed_stream if @serverspecfile print_failed_stream print_success_stream if verbose_success? puts "\n---------------------------------------\n" - + + if @serverspecfile + puts "#{'Success'.green}: " + @successarray.count.to_s + else puts "#{'Success'.green}: #{success_output_stream.count}" + end puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" - puts "#{'Failed'.red}: #{failed_output_stream.count}" + if @serverspecfile + puts "#{'Failed: '.red}" + @failedarray.count.to_s + else + puts "#{'Failed'.red}: #{failed_output_stream.count}" + end + if @serverspecfile + puts "#{'Total'.blue}: " + (@failedarray.count + @successarray.count).to_s + binding.pry + else puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" + end puts "\n" end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 3563938..86cceae 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -39,21 +39,25 @@ def serverspecsetup @hash = formatter.output_hash RSpec.clear_examples - s = TCPSocket.open(hostname, port) + f = TCPSocket.open('localhost', 2000) + s = TCPSocket.open('localhost', 2001) + @hash[:examples].each do |x| if x[:status] == 'failed' - puts 'sending...' - s.send(+ ': ' + x[:full_description]) - ng.pry - puts 'voorbij send' + failedspec + f.write("\n"+hostx + ': ' + x[:full_description]) + elsif x[:status] == 'passed' - succeeded x[:full_description] + successspec + s.write("\n" + hostx + ': ' + x[:full_description]) else - timed_out x[:full_description] + puts 'timeout' end end - s.close # Close the socket when done + f.close + s.close + end end end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 8601f5f..8c8d887 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -1,17 +1,37 @@ require 'socket' +require 'pry' class Bunka class << self - def create_socket - puts 'creating socket' - server = TCPServer.open('localhost', 2000) # Socket to listen on port 2000 - puts 'start loop' + def create_failed_socket + server1 = TCPServer.open('localhost', 2000) + loop{ + Thread.start(server1.accept) do |client| + @failedarray.push client.read + binding.pry + puts 'wrote failure to array' + client.close + end + } + end + def create_success_socket + server2 = TCPServer.open('localhost', 2001) + loop{ + Thread.start(server2.accept) do |client| + @successarray.push client.read + binding.pry + puts 'wrote success to array' + client.close + end + } + end + def create_timeout_socket + server3 = TCPServer.open('localhost', 2002) # Socket to listen on port 2000 loop { # Servers run forever - Thread.start(server.accept) do |client| - puts 'test geslaagd' + Thread.start(server3.accept) do |client| + @timeoutarray.push client.read client.close end - puts 'voorbij server' } end end From 3cd4a8b2f7671f5d2a3d440ed7637613a26bacbb Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Tue, 31 Mar 2015 16:33:18 +0200 Subject: [PATCH 07/33] output works (still randomly) --- lib/bunka/printers.rb | 7 +++---- lib/bunka/socket.rb | 4 ---- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index c1375b5..473c28d 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -51,9 +51,9 @@ def print_summary print_success_stream if verbose_success? puts "\n---------------------------------------\n" - + @success = @hosts.count - @failedarray.count if @serverspecfile - puts "#{'Success'.green}: " + @successarray.count.to_s + puts "#{'Success'.green}: " + @success.to_s else puts "#{'Success'.green}: #{success_output_stream.count}" end @@ -64,8 +64,7 @@ def print_summary puts "#{'Failed'.red}: #{failed_output_stream.count}" end if @serverspecfile - puts "#{'Total'.blue}: " + (@failedarray.count + @successarray.count).to_s - binding.pry + puts "#{'Total'.blue}: " + (@failedarray.count + @success).to_s else puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 8c8d887..acce93d 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -8,8 +8,6 @@ def create_failed_socket loop{ Thread.start(server1.accept) do |client| @failedarray.push client.read - binding.pry - puts 'wrote failure to array' client.close end } @@ -19,8 +17,6 @@ def create_success_socket loop{ Thread.start(server2.accept) do |client| @successarray.push client.read - binding.pry - puts 'wrote success to array' client.close end } From 5bf5cc37109327532e810da967d975a323973d5d Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 1 Apr 2015 09:48:33 +0200 Subject: [PATCH 08/33] Invert added --- lib/bunka.rb | 16 ++++++++-------- lib/bunka/helpers.rb | 22 +++++++++++++--------- lib/bunka/printers.rb | 24 +++++++++++++++++++++--- lib/bunka/serverspec.rb | 16 ++++++++++++---- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 9c3e800..f7a3a14 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -40,14 +40,14 @@ def testserverspec serverspecfile, timeout_interval, verbose_success, invert, se @successarray = Array.new @timeoutarray = Array.new - thread_1 = Thread.new do - create_failed_socket - end - thread_2 = Thread.new do - create_success_socket - end - serverspecsetup - print_summary + thread_1 = Thread.new do + create_failed_socket + end + thread_2 = Thread.new do + create_success_socket + end + serverspecsetup + print_summary end end end diff --git a/lib/bunka/helpers.rb b/lib/bunka/helpers.rb index 3282e35..444f2b5 100644 --- a/lib/bunka/helpers.rb +++ b/lib/bunka/helpers.rb @@ -8,15 +8,7 @@ def failed(reason) print_fail end - def failedspec - print_fail - end - - def successspec - print_success - end - - def succeeded(reason) + def succeeded(reason) success_output_stream.push reason print_success end @@ -45,5 +37,17 @@ def verbose_success? def invert? @invert end + + def failedspec + print_fail + end + + def successspec + print_success + end + + def timeoutspec + print_timeout + end end end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 473c28d..a5b6932 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -36,11 +36,17 @@ def print_success_stream def print_specfailed_stream @failedarray.reject! { |c| c.empty? } @successarray.reject! { |c| c.empty? } + specinvert @failedarray.each do |output| puts output.red end end + def specinvert + if invert? + @failedarray = @successarray + end + end def print_summary print "\n" @@ -51,20 +57,32 @@ def print_summary print_success_stream if verbose_success? puts "\n---------------------------------------\n" - @success = @hosts.count - @failedarray.count + + if @serverspecfile + @failed = @failedarray.count + @success = @hosts.count - @failed + @total = @failedarray.count + @success + @timedout = @total - @failed - @success + end + if @serverspecfile puts "#{'Success'.green}: " + @success.to_s else puts "#{'Success'.green}: #{success_output_stream.count}" end + + if @serverspecfile + puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s + else puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" + end if @serverspecfile - puts "#{'Failed: '.red}" + @failedarray.count.to_s + puts "#{'Failed: '.red}" + @failed.to_s else puts "#{'Failed'.red}: #{failed_output_stream.count}" end if @serverspecfile - puts "#{'Total'.blue}: " + (@failedarray.count + @success).to_s + puts "#{'Total'.blue}: " + @total.to_s else puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 86cceae..245dd42 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -45,14 +45,22 @@ def serverspecsetup @hash[:examples].each do |x| if x[:status] == 'failed' - failedspec + if !invert? + failedspec + else + successspec + end + f.write("\n"+hostx + ': ' + x[:full_description]) elsif x[:status] == 'passed' - successspec + if !invert? + successspec + else + failedspec + end + s.write("\n" + hostx + ': ' + x[:full_description]) - else - puts 'timeout' end end f.close From 1292fa7f44fcbfd3bd47b342e6a535084b9bc6f7 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 1 Apr 2015 10:23:37 +0200 Subject: [PATCH 09/33] verbose_success added --- lib/bunka/printers.rb | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index a5b6932..3e393f5 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -33,28 +33,49 @@ def print_success_stream end end - def print_specfailed_stream + def print_spec_streams @failedarray.reject! { |c| c.empty? } @successarray.reject! { |c| c.empty? } specinvert + + if verbose_success? + print_successspec_stream + else + print_failedspec_stream + end + end + + def print_failedspec_stream @failedarray.each do |output| puts output.red end end + + def print_successspec_stream + @successarray.each do |output| + puts output.green + end + end def specinvert if invert? + @dummy = @failedarray @failedarray = @successarray + @successarray = @dummy end end def print_summary print "\n" print_timeout_stream - puts "\nErrors: ".red if @serverspecfile - print_specfailed_stream if @serverspecfile + if @serverspecfile && !verbose_success? + puts "\nErrors: ".red + else + puts "\nSuccesses: ".green + end + print_spec_streams if @serverspecfile print_failed_stream - print_success_stream if verbose_success? + print_success_stream if verbose_success? puts "\n---------------------------------------\n" From 2d2180812689676956fd165a7710c74abfae36b0 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 1 Apr 2015 10:51:04 +0200 Subject: [PATCH 10/33] print nodes instead of tests --- lib/bunka.rb | 1 + lib/bunka/serverspec.rb | 26 ++++++++++++-------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index f7a3a14..9c715c8 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -46,6 +46,7 @@ def testserverspec serverspecfile, timeout_interval, verbose_success, invert, se thread_2 = Thread.new do create_success_socket end + sleep(2) serverspecsetup print_summary end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 245dd42..47ee0d9 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -41,28 +41,26 @@ def serverspecsetup f = TCPSocket.open('localhost', 2000) s = TCPSocket.open('localhost', 2001) - + @failstatus = false + @successstatus = false @hash[:examples].each do |x| if x[:status] == 'failed' - if !invert? - failedspec - else - successspec - end - + @failstatus = true f.write("\n"+hostx + ': ' + x[:full_description]) - elsif x[:status] == 'passed' - if !invert? - successspec - else - failedspec - end - s.write("\n" + hostx + ': ' + x[:full_description]) end end + + if !invert? && @failstatus == true + failedspec + elsif invert? && @failstatus == false + failedspec + else + successspec + end + f.close s.close From 3cd6c7b743ee0aea55568787eeaf09d6744429d1 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 1 Apr 2015 13:46:48 +0200 Subject: [PATCH 11/33] Rubocop and serverspecfile exist control --- bin/bunka | 2 +- lib/bunka.rb | 20 ++++----- lib/bunka/bunka.rb | 4 +- lib/bunka/helpers.rb | 19 ++++---- lib/bunka/printers.rb | 46 +++++++++---------- lib/bunka/serverspec.rb | 97 +++++++++++++++++++++-------------------- 6 files changed, 94 insertions(+), 94 deletions(-) diff --git a/bin/bunka b/bin/bunka index 384372f..14b31de 100755 --- a/bin/bunka +++ b/bin/bunka @@ -25,7 +25,7 @@ class BunkaCommand < Thor option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: '/.bunka/servers' def testserverspec(serverspecfile) - Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end end diff --git a/lib/bunka.rb b/lib/bunka.rb index 9c715c8..14e9afc 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -10,7 +10,7 @@ class Bunka class << self - def test command, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil + def test(command, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) @command = command @invert = invert @query = query @@ -27,7 +27,7 @@ def test command, query, timeout_interval, verbose_success, invert, sequential, print_summary end - def testserverspec serverspecfile, timeout_interval, verbose_success, invert, sequential, threads, file = '/.bunka/servers' + def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, sequential, threads, file = '/.bunka/servers') @serverspecfile = serverspecfile @invert = invert @sequential = sequential @@ -36,17 +36,17 @@ def testserverspec serverspecfile, timeout_interval, verbose_success, invert, se @verbose_success = verbose_success @file = file - @failedarray = Array.new - @successarray = Array.new - @timeoutarray = Array.new + @failedarray = [] + @successarray = [] + @timeoutarray = [] - thread_1 = Thread.new do - create_failed_socket + Thread.new do + create_failed_socket end - thread_2 = Thread.new do - create_success_socket + Thread.new do + create_success_socket end - sleep(2) + sleep(1) serverspecsetup print_summary end diff --git a/lib/bunka/bunka.rb b/lib/bunka/bunka.rb index 0e249c3..d9463b8 100644 --- a/lib/bunka/bunka.rb +++ b/lib/bunka/bunka.rb @@ -10,7 +10,7 @@ def nodes end end - def execute_query fqdn + def execute_query(fqdn) begin timeout @timeout_interval do Net::SSH.start(fqdn, 'root', paranoid: false, forward_agent: true) do |ssh| @@ -25,7 +25,7 @@ def execute_query fqdn end end - def parse_output output, fqdn + def parse_output(output, fqdn) if output[2] == 0 && !invert? succeeded "#{fqdn}: #{output[0]}" elsif output[2] != 0 && invert? diff --git a/lib/bunka/helpers.rb b/lib/bunka/helpers.rb index 444f2b5..06083b4 100644 --- a/lib/bunka/helpers.rb +++ b/lib/bunka/helpers.rb @@ -2,13 +2,12 @@ class Bunka class << self - def failed(reason) failed_output_stream.push reason print_fail end - - def succeeded(reason) + + def succeeded(reason) success_output_stream.push reason print_success end @@ -17,19 +16,19 @@ def timed_out(reason) timeout_output_stream.push reason print_timeout end - + def timeout_output_stream - @timeout_output_stream ||= Array.new + @timeout_output_stream ||= [] end def failed_output_stream - @failed_output_stream ||= Array.new + @failed_output_stream ||= [] end def success_output_stream - @success_output_stream ||= Array.new + @success_output_stream ||= [] end - + def verbose_success? @verbose_success end @@ -41,11 +40,11 @@ def invert? def failedspec print_fail end - + def successspec print_success end - + def timeoutspec print_timeout end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 3e393f5..7d32d0e 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -20,7 +20,7 @@ def print_failed_stream puts output.red end end - + def print_timeout_stream timeout_output_stream.each do |output| puts output.yellow @@ -44,13 +44,13 @@ def print_spec_streams print_failedspec_stream end end - + def print_failedspec_stream @failedarray.each do |output| puts output.red end end - + def print_successspec_stream @successarray.each do |output| puts output.green @@ -59,9 +59,9 @@ def print_successspec_stream def specinvert if invert? - @dummy = @failedarray - @failedarray = @successarray - @successarray = @dummy + @dummy = @failedarray + @failedarray = @successarray + @successarray = @dummy end end @@ -71,41 +71,41 @@ def print_summary if @serverspecfile && !verbose_success? puts "\nErrors: ".red else - puts "\nSuccesses: ".green - end + puts "\nSuccesses: ".green if @serverspecfile + end print_spec_streams if @serverspecfile - print_failed_stream + print_failed_stream print_success_stream if verbose_success? puts "\n---------------------------------------\n" - + if @serverspecfile - @failed = @failedarray.count - @success = @hosts.count - @failed - @total = @failedarray.count + @success - @timedout = @total - @failed - @success + @failed = @failedarray.count + @success = @hosts.count - @failed + @total = @failedarray.count + @success + @timedout = @total - @failed - @success end if @serverspecfile - puts "#{'Success'.green}: " + @success.to_s + puts "#{'Success'.green}: " + @success.to_s else - puts "#{'Success'.green}: #{success_output_stream.count}" + puts "#{'Success'.green}: #{success_output_stream.count}" end if @serverspecfile - puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s + puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s else - puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" + puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" end if @serverspecfile - puts "#{'Failed: '.red}" + @failed.to_s - else - puts "#{'Failed'.red}: #{failed_output_stream.count}" + puts "#{'Failed: '.red}" + @failed.to_s + else + puts "#{'Failed'.red}: #{failed_output_stream.count}" end if @serverspecfile - puts "#{'Total'.blue}: " + @total.to_s + puts "#{'Total'.blue}: " + @total.to_s else - puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" + puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" end puts "\n" end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 47ee0d9..60ba484 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -6,65 +6,66 @@ class Bunka class << self def serverspecsetup - - # Can be done with bunka.nodes if also include knife search - if File.exist?(ENV['HOME']+@file) - @hosts = File.readlines(ENV['HOME']+@file).each {|l| l.chomp!} + if File.exist?(ENV['HOME'] + @file) + @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } else puts 'Serverfile not found' exit end + + if File.exist?(ENV['HOME'] + @serverspecfile) == false + puts 'Serverspecfile not found' + exit + end + RSpec.configure do |c| - c.output_stream = nil + c.output_stream = nil end - threads = @threads - Parallel.map(@hosts, in_processes: threads) do |hostx| - RSpec::Core::Configuration#reset - ENV['TARGET_HOST'] = hostx - config = RSpec.configuration - formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) - # create reporter with json formatter - reporter = RSpec::Core::Reporter.new(config) - config.instance_variable_set(:@reporter, reporter) - # internal hack - # api may not be stable, make sure lock down Rspec version - loader = config.send(:formatter_loader) - notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) - reporter.register_listener(formatter, *notifications) - begin - RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) - rescue RuntimeError - puts 'Serverspec failed' - end - @hash = formatter.output_hash - RSpec.clear_examples + Parallel.map(@hosts, in_processes: @threads) do |hostx| + ENV['TARGET_HOST'] = hostx + config = RSpec.configuration + formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) + # create reporter with json formatter + reporter = RSpec::Core::Reporter.new(config) + config.instance_variable_set(:@reporter, reporter) + # internal hack + # api may not be stable, make sure lock down Rspec version + loader = config.send(:formatter_loader) + notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) + reporter.register_listener(formatter, *notifications) + begin - f = TCPSocket.open('localhost', 2000) - s = TCPSocket.open('localhost', 2001) - @failstatus = false - @successstatus = false + RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) + rescue RuntimeError + puts 'Serverspec failed' + end + @hash = formatter.output_hash + RSpec.clear_examples - @hash[:examples].each do |x| - if x[:status] == 'failed' - @failstatus = true - f.write("\n"+hostx + ': ' + x[:full_description]) - elsif x[:status] == 'passed' - s.write("\n" + hostx + ': ' + x[:full_description]) - end - end + f = TCPSocket.open('localhost', 2000) + s = TCPSocket.open('localhost', 2001) + @failstatus = false + @successstatus = false - if !invert? && @failstatus == true - failedspec - elsif invert? && @failstatus == false - failedspec - else - successspec + @hash[:examples].each do |x| + if x[:status] == 'failed' + @failstatus = true + f.write("\n" + hostx + ': ' + x[:full_description]) + elsif x[:status] == 'passed' + s.write("\n" + hostx + ': ' + x[:full_description]) end + end - f.close - s.close - - end + if !invert? && @failstatus == true + failedspec + elsif invert? && @failstatus == false + failedspec + else + successspec + end + f.close + s.close end end end +end From 19d57a4925f60f85672f2b12c4f0da97cd4c2780 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 2 Apr 2015 09:41:46 +0200 Subject: [PATCH 12/33] possible fix processes --- lib/bunka.rb | 6 ++- lib/bunka/serverspec.rb | 82 ++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 14e9afc..bbabd7c 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -6,7 +6,6 @@ require 'bunka/ssh' require 'bunka/serverspec' require 'bunka/socket' -require 'pry' class Bunka class << self @@ -39,7 +38,8 @@ def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, se @failedarray = [] @successarray = [] @timeoutarray = [] - + + start = Time.now Thread.new do create_failed_socket end @@ -49,6 +49,8 @@ def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, se sleep(1) serverspecsetup print_summary + finish = Time.now + puts finish - start end end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 60ba484..2e5e8c8 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -6,23 +6,16 @@ class Bunka class << self def serverspecsetup - if File.exist?(ENV['HOME'] + @file) - @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } - else - puts 'Serverfile not found' - exit - end - - if File.exist?(ENV['HOME'] + @serverspecfile) == false - puts 'Serverspecfile not found' - exit - end + file_control RSpec.configure do |c| c.output_stream = nil end - Parallel.map(@hosts, in_processes: @threads) do |hostx| + + @hosts.each_slice(@threads).to_a.each do |h| + Parallel.map(h, in_processes: @threads) do |hostx| ENV['TARGET_HOST'] = hostx + @hostx = hostx config = RSpec.configuration formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) # create reporter with json formatter @@ -31,41 +24,62 @@ def serverspecsetup # internal hack # api may not be stable, make sure lock down Rspec version loader = config.send(:formatter_loader) - notifications = loader.send(:notifications_for, RSpec::Core::Formatters::JsonFormatter) + notifications = loader.send( + :notifications_for, + RSpec::Core::Formatters::JsonFormatter + ) reporter.register_listener(formatter, *notifications) begin - - RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) + RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) rescue RuntimeError puts 'Serverspec failed' end @hash = formatter.output_hash RSpec.clear_examples - f = TCPSocket.open('localhost', 2000) - s = TCPSocket.open('localhost', 2001) - @failstatus = false - @successstatus = false - - @hash[:examples].each do |x| - if x[:status] == 'failed' - @failstatus = true - f.write("\n" + hostx + ': ' + x[:full_description]) - elsif x[:status] == 'passed' - s.write("\n" + hostx + ': ' + x[:full_description]) - end + parse_spec_output end + end + end + + def file_control + if File.exist?(ENV['HOME'] + @file) + @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } + else + puts 'Serverfile not found' + exit + end - if !invert? && @failstatus == true - failedspec - elsif invert? && @failstatus == false - failedspec - else - successspec + if File.exist?(ENV['HOME'] + @serverspecfile) == false + puts 'Serverspecfile not found' + exit + end + end + + def parse_spec_output + f = TCPSocket.open('localhost', 2000) + s = TCPSocket.open('localhost', 2001) + @failstatus = false + @successstatus = false + + @hash[:examples].each do |x| + if x[:status] == 'failed' + @failstatus = true + f.write("\n" + @hostx + ': ' + x[:full_description]) + elsif x[:status] == 'passed' + s.write("\n" + @hostx + ': ' + x[:full_description]) end + end + + if !invert? && @failstatus == true + failedspec + elsif invert? && @failstatus == false + failedspec + else + successspec + end f.close s.close - end end end end From 4385b7f3e685ce90f9546d9ebc724968cd947b1d Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 3 Apr 2015 11:42:04 +0200 Subject: [PATCH 13/33] singlesocket --- bin/bunka | 10 +++++----- lib/bunka.rb | 10 ++++------ lib/bunka/serverspec.rb | 17 +++++++++-------- lib/bunka/socket.rb | 18 +++++++++++++++++- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/bin/bunka b/bin/bunka index 14b31de..ac215ad 100755 --- a/bin/bunka +++ b/bin/bunka @@ -15,17 +15,17 @@ class BunkaCommand < Thor def test(command, query = 'name:*') Bunka.test(command, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end - map '-ts' => :testserverspec + map '-s' => :serverspec - desc 'testserverspec SERVERSPECFILE', 'test nodes with serverspec' + desc 'serverspec SERVERSPECFILE', 'test nodes with serverspec' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :processes, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: '/.bunka/servers' - def testserverspec(serverspecfile) - Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + def serverspec(serverspecfile) + Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) end end diff --git a/lib/bunka.rb b/lib/bunka.rb index bbabd7c..32ca0c8 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -26,11 +26,11 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, print_summary end - def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, sequential, threads, file = '/.bunka/servers') + def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, sequential, processes, file = '/.bunka/servers') @serverspecfile = serverspecfile @invert = invert @sequential = sequential - @threads = sequential ? 1 : threads + @processes = sequential ? 1 : processes @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file @@ -41,14 +41,12 @@ def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, se start = Time.now Thread.new do - create_failed_socket - end - Thread.new do - create_success_socket + create_unix_socket end sleep(1) serverspecsetup print_summary + # @server4.close finish = Time.now puts finish - start end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 2e5e8c8..06f3c22 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -12,8 +12,8 @@ def serverspecsetup c.output_stream = nil end - @hosts.each_slice(@threads).to_a.each do |h| - Parallel.map(h, in_processes: @threads) do |hostx| + @hosts.each_slice(@processes).each do |h| + Parallel.map(h, in_processes: @processes) do |hostx| ENV['TARGET_HOST'] = hostx @hostx = hostx config = RSpec.configuration @@ -57,20 +57,23 @@ def file_control end def parse_spec_output - f = TCPSocket.open('localhost', 2000) - s = TCPSocket.open('localhost', 2001) + c = UNIXSocket.open("/tmp/sock") + @failstatus = false @successstatus = false @hash[:examples].each do |x| if x[:status] == 'failed' @failstatus = true - f.write("\n" + @hostx + ': ' + x[:full_description]) + c.write('failedtest' + "\n" + @hostx + ': ' + x[:full_description]) + puts @failedarray elsif x[:status] == 'passed' - s.write("\n" + @hostx + ': ' + x[:full_description]) + c.write('successtest' + "\n" + @hostx + ': ' + x[:full_description]) end end + c.close + if !invert? && @failstatus == true failedspec elsif invert? && @failstatus == false @@ -78,8 +81,6 @@ def parse_spec_output else successspec end - f.close - s.close end end end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index acce93d..0ab1c1a 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -30,5 +30,21 @@ def create_timeout_socket end } end - end + def create_unix_socket + server4 = UNIXServer.open('/tmp/sock') # Socket to listen + loop{ # Servers run forever + Thread.start(server4.accept) do |client| + string = client.read + if string.start_with?('failedtest') + string.gsub!('failedtest','') + @failedarray.push string + else + string.gsub!('successtest', '') + @successarray.push string + end + client.close + end + } + end + end end From 940d5c75cf0e9e67e06590eabffe5fa688d4ca23 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 3 Apr 2015 16:08:26 +0200 Subject: [PATCH 14/33] unixsocket solved --- lib/bunka.rb | 3 ++- lib/bunka/socket.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 32ca0c8..daa6eca 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -6,6 +6,7 @@ require 'bunka/ssh' require 'bunka/serverspec' require 'bunka/socket' +require 'pry' class Bunka class << self @@ -46,7 +47,7 @@ def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, se sleep(1) serverspecsetup print_summary - # @server4.close + File.delete('/tmp/sock') finish = Time.now puts finish - start end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 0ab1c1a..2f52d91 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -31,8 +31,8 @@ def create_timeout_socket } end def create_unix_socket - server4 = UNIXServer.open('/tmp/sock') # Socket to listen - loop{ # Servers run forever + server4 = UNIXServer.new('/tmp/sock') # Socket to listen + loop { # Servers run forever Thread.start(server4.accept) do |client| string = client.read if string.start_with?('failedtest') From 89d625dcccd61f1f843107c7477caaf992b5f4f6 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 3 Apr 2015 17:05:51 +0200 Subject: [PATCH 15/33] query implementation --- bin/bunka | 6 +++--- lib/bunka.rb | 3 ++- lib/bunka/serverspec.rb | 13 ++++++++++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/bin/bunka b/bin/bunka index ac215ad..037aad1 100755 --- a/bin/bunka +++ b/bin/bunka @@ -23,9 +23,9 @@ class BunkaCommand < Thor option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 option :processes, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: '/.bunka/servers' - def serverspec(serverspecfile) - Bunka.testserverspec(serverspecfile, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) + option :'from-file', type: :string, desc: 'path to file with list of servers' + def serverspec(serverspecfile, query='name:*') + Bunka.testserverspec(serverspecfile,query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) end end diff --git a/lib/bunka.rb b/lib/bunka.rb index daa6eca..5c6cdd6 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -27,8 +27,9 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, print_summary end - def testserverspec(serverspecfile, timeout_interval, verbose_success, invert, sequential, processes, file = '/.bunka/servers') + def testserverspec(serverspecfile, query, timeout_interval, verbose_success, invert, sequential, processes, file) @serverspecfile = serverspecfile + @query = query @invert = invert @sequential = sequential @processes = sequential ? 1 : processes diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 06f3c22..15b049f 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -43,10 +43,17 @@ def serverspecsetup end def file_control - if File.exist?(ENV['HOME'] + @file) - @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } + if @file + if File.exist?(ENV['HOME'] + @file) + @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } + else + puts 'Serverfile not found' + exit + end + elsif @query + @hosts = knife_search @query else - puts 'Serverfile not found' + puts 'Wrong querry' exit end From 353787a9f0b9adbdc747b983b05bb03c6af56f7e Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 3 Apr 2015 17:10:04 +0200 Subject: [PATCH 16/33] bug fix where sockfile still existed --- lib/bunka.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/bunka.rb b/lib/bunka.rb index 5c6cdd6..1abbdf7 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -41,6 +41,10 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv @successarray = [] @timeoutarray = [] + if File.exist?('/tmp/sock') + File.delete('/tmp/sock') + end + start = Time.now Thread.new do create_unix_socket From 57391b13900dc55b0aca22a117fc5278378b296e Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Tue, 7 Apr 2015 10:55:16 +0200 Subject: [PATCH 17/33] ENV HOME to path_expand --- lib/bunka/serverspec.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 15b049f..718be08 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -6,12 +6,12 @@ class Bunka class << self def serverspecsetup + file_control RSpec.configure do |c| c.output_stream = nil end - @hosts.each_slice(@processes).each do |h| Parallel.map(h, in_processes: @processes) do |hostx| ENV['TARGET_HOST'] = hostx @@ -30,7 +30,7 @@ def serverspecsetup ) reporter.register_listener(formatter, *notifications) begin - RSpec::Core::Runner.run([ENV['HOME'] + @serverspecfile]) + RSpec::Core::Runner.run([@serverspecfile]) rescue RuntimeError puts 'Serverspec failed' end @@ -43,9 +43,12 @@ def serverspecsetup end def file_control + + @file = File.expand_path(@file) + @serverspecfile = File.expand_path(@serverspecfile) if @file - if File.exist?(ENV['HOME'] + @file) - @hosts = File.readlines(ENV['HOME'] + @file).each { |l| l.chomp! } + if File.exist?(@file) + @hosts = File.readlines(@file).each { |l| l.chomp! } else puts 'Serverfile not found' exit @@ -57,7 +60,7 @@ def file_control exit end - if File.exist?(ENV['HOME'] + @serverspecfile) == false + if File.exist?(@serverspecfile) == false puts 'Serverspecfile not found' exit end From e92d30bded0c0d6263983dc0f480bfeb78d4634d Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Tue, 7 Apr 2015 14:54:54 +0200 Subject: [PATCH 18/33] time out added --- lib/bunka/printers.rb | 19 ++++++++++++++++--- lib/bunka/serverspec.rb | 14 +++++++++++++- lib/bunka/socket.rb | 32 ++++---------------------------- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 7d32d0e..1bd1fac 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -56,6 +56,12 @@ def print_successspec_stream puts output.green end end + + def print_timeoutspec_stream + @timeoutarray.each do |output| + puts output.yellow + end + end def specinvert if invert? @@ -67,6 +73,9 @@ def specinvert def print_summary print "\n" + + puts @timeoutarray.inspect + print_timeout_stream if @serverspecfile && !verbose_success? puts "\nErrors: ".red @@ -74,6 +83,10 @@ def print_summary puts "\nSuccesses: ".green if @serverspecfile end print_spec_streams if @serverspecfile + if @serverspecfile && @timeoutarray.count > 0 + puts "\n" + 'Timed out or unresolved nodes: '.yellow + print_timeoutspec_stream + end print_failed_stream print_success_stream if verbose_success? @@ -81,9 +94,9 @@ def print_summary if @serverspecfile @failed = @failedarray.count - @success = @hosts.count - @failed - @total = @failedarray.count + @success - @timedout = @total - @failed - @success + @success = @successarray.count + @timedout = @timeoutarray.count + @total = @failed + @success + @timedout end if @serverspecfile diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 718be08..ec155e2 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -30,14 +30,21 @@ def serverspecsetup ) reporter.register_listener(formatter, *notifications) begin + timeout @timeout_interval do + Net::SSH.start(hostx, 'root', paranoid: false, forward_agent: true) + end RSpec::Core::Runner.run([@serverspecfile]) + rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH => e + timeoutspec + @timedoutbool = true + fill_timeout_array rescue RuntimeError puts 'Serverspec failed' end @hash = formatter.output_hash RSpec.clear_examples - parse_spec_output + parse_spec_output unless @timedoutbool == true end end end @@ -92,5 +99,10 @@ def parse_spec_output successspec end end + + def fill_timeout_array + c = UNIXSocket.open("/tmp/sock") + c.write('timeouttest' + ' - ' + @hostx) + end end end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 2f52d91..c7aad1d 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -3,33 +3,6 @@ class Bunka class << self - def create_failed_socket - server1 = TCPServer.open('localhost', 2000) - loop{ - Thread.start(server1.accept) do |client| - @failedarray.push client.read - client.close - end - } - end - def create_success_socket - server2 = TCPServer.open('localhost', 2001) - loop{ - Thread.start(server2.accept) do |client| - @successarray.push client.read - client.close - end - } - end - def create_timeout_socket - server3 = TCPServer.open('localhost', 2002) # Socket to listen on port 2000 - loop { # Servers run forever - Thread.start(server3.accept) do |client| - @timeoutarray.push client.read - client.close - end - } - end def create_unix_socket server4 = UNIXServer.new('/tmp/sock') # Socket to listen loop { # Servers run forever @@ -38,9 +11,12 @@ def create_unix_socket if string.start_with?('failedtest') string.gsub!('failedtest','') @failedarray.push string - else + elsif string.start_with?('successtest') string.gsub!('successtest', '') @successarray.push string + else + string.gsub!('timeouttest', '') + @timeoutarray.push string end client.close end From 1615c99378aba272dbffce718185b66468dacd4f Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Tue, 7 Apr 2015 16:36:55 +0200 Subject: [PATCH 19/33] cleanup code WIP --- lib/bunka.rb | 17 +++--- lib/bunka/printers.rb | 19 +++---- lib/bunka/serverspec.rb | 114 ++++++++++++++++++++++++---------------- lib/bunka/socket.rb | 24 +++++---- lib/bunka/ssh.rb | 10 ++-- 5 files changed, 103 insertions(+), 81 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 1abbdf7..2a5b6bc 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -18,7 +18,7 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, @threads = sequential ? 1 : threads @timeout_interval = timeout_interval @verbose_success = verbose_success - @file = file + @file = File.expand_path(file) Parallel.map(nodes, in_threads: @threads) do |fqdn| execute_query fqdn @@ -28,23 +28,19 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, end def testserverspec(serverspecfile, query, timeout_interval, verbose_success, invert, sequential, processes, file) - @serverspecfile = serverspecfile + @serverspecfile = File.expand_path(serverspecfile) @query = query @invert = invert @sequential = sequential @processes = sequential ? 1 : processes @timeout_interval = timeout_interval @verbose_success = verbose_success - @file = file - + @file = File.expand_path(file) @failedarray = [] @successarray = [] @timeoutarray = [] - if File.exist?('/tmp/sock') - File.delete('/tmp/sock') - end - + socket_delete start = Time.now Thread.new do create_unix_socket @@ -52,9 +48,8 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv sleep(1) serverspecsetup print_summary - File.delete('/tmp/sock') - finish = Time.now - puts finish - start + socket_delete + puts Time.now - start end end end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 1bd1fac..fadfa27 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -56,7 +56,7 @@ def print_successspec_stream puts output.green end end - + def print_timeoutspec_stream @timeoutarray.each do |output| puts output.yellow @@ -72,10 +72,13 @@ def specinvert end def print_summary - print "\n" - - puts @timeoutarray.inspect + print_output + puts "\n---------------------------------------\n" + print_counts + end + def print_output + print "\n" print_timeout_stream if @serverspecfile && !verbose_success? puts "\nErrors: ".red @@ -84,27 +87,25 @@ def print_summary end print_spec_streams if @serverspecfile if @serverspecfile && @timeoutarray.count > 0 - puts "\n" + 'Timed out or unresolved nodes: '.yellow + puts "\nTimed out or unresolved nodes: \n".yellow print_timeoutspec_stream end print_failed_stream print_success_stream if verbose_success? + end - puts "\n---------------------------------------\n" - + def print_counts if @serverspecfile @failed = @failedarray.count @success = @successarray.count @timedout = @timeoutarray.count @total = @failed + @success + @timedout end - if @serverspecfile puts "#{'Success'.green}: " + @success.to_s else puts "#{'Success'.green}: #{success_output_stream.count}" end - if @serverspecfile puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s else diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index ec155e2..a68fbf5 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -6,53 +6,72 @@ class Bunka class << self def serverspecsetup - file_control - - RSpec.configure do |c| - c.output_stream = nil - end @hosts.each_slice(@processes).each do |h| - Parallel.map(h, in_processes: @processes) do |hostx| - ENV['TARGET_HOST'] = hostx - @hostx = hostx - config = RSpec.configuration - formatter = RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) - # create reporter with json formatter - reporter = RSpec::Core::Reporter.new(config) - config.instance_variable_set(:@reporter, reporter) - # internal hack - # api may not be stable, make sure lock down Rspec version - loader = config.send(:formatter_loader) - notifications = loader.send( - :notifications_for, - RSpec::Core::Formatters::JsonFormatter + Parallel.map(h, in_processes: @processes) do |hostx| + rspec_config + ENV['TARGET_HOST'] = hostx + @hostx = hostx + config + formatter + # create reporter with json formatter + reporter + config.instance_variable_set(:@reporter, reporter) + # internal hack + # api may not be stable, make sure lock down Rspec version + loader + notifications + reporter.register_listener(formatter, *notifications) + run_tests + @hash = formatter.output_hash + parse_spec_output_to_socket unless @timedoutbool == true + end + end + end + + def hash + @hash ||= formatter.output_hash + end + + def config + @config ||= RSpec.configuration + end + + def formatter + @formatter ||= RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) + end + + def reporter + @reporter ||= RSpec::Core::Reporter.new(config) + end + + def loader + @loader ||= config.send(:formatter_loader) + end + + def notifications + @notifications ||= loader.send( + :notifications_for, + RSpec::Core::Formatters::JsonFormatter ) - reporter.register_listener(formatter, *notifications) - begin + end + + def run_tests + begin timeout @timeout_interval do - Net::SSH.start(hostx, 'root', paranoid: false, forward_agent: true) - end - RSpec::Core::Runner.run([@serverspecfile]) - rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH => e - timeoutspec + Net::SSH.start(@hostx, 'root', paranoid: false, forward_agent: true) + end + RSpec::Core::Runner.run([@serverspecfile]) + rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH + timeoutspec @timedoutbool = true fill_timeout_array rescue RuntimeError puts 'Serverspec failed' end - @hash = formatter.output_hash - RSpec.clear_examples - - parse_spec_output unless @timedoutbool == true - end - end end def file_control - - @file = File.expand_path(@file) - @serverspecfile = File.expand_path(@serverspecfile) if @file if File.exist?(@file) @hosts = File.readlines(@file).each { |l| l.chomp! } @@ -66,31 +85,28 @@ def file_control puts 'Wrong querry' exit end - if File.exist?(@serverspecfile) == false puts 'Serverspecfile not found' exit end - end + end - def parse_spec_output - c = UNIXSocket.open("/tmp/sock") - + def parse_spec_output_to_socket + c = UNIXSocket.open('/tmp/sock') @failstatus = false @successstatus = false - - @hash[:examples].each do |x| + hash[:examples].each do |x| if x[:status] == 'failed' @failstatus = true c.write('failedtest' + "\n" + @hostx + ': ' + x[:full_description]) - puts @failedarray elsif x[:status] == 'passed' c.write('successtest' + "\n" + @hostx + ': ' + x[:full_description]) end end + check_invert + end - c.close - + def check_invert if !invert? && @failstatus == true failedspec elsif invert? && @failstatus == false @@ -101,8 +117,14 @@ def parse_spec_output end def fill_timeout_array - c = UNIXSocket.open("/tmp/sock") + c = UNIXSocket.open('/tmp/sock') c.write('timeouttest' + ' - ' + @hostx) end + + def rspec_config + RSpec.configure do |c| + c.output_stream = nil + end + end end end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index c7aad1d..34031a0 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -3,13 +3,13 @@ class Bunka class << self - def create_unix_socket - server4 = UNIXServer.new('/tmp/sock') # Socket to listen - loop { # Servers run forever - Thread.start(server4.accept) do |client| + def create_unix_socket + server4 = UNIXServer.new('/tmp/sock') # Socket to listen + loop do # Servers run forever + Thread.start(server4.accept) do |client| string = client.read if string.start_with?('failedtest') - string.gsub!('failedtest','') + string.gsub!('failedtest', '') @failedarray.push string elsif string.start_with?('successtest') string.gsub!('successtest', '') @@ -17,10 +17,14 @@ def create_unix_socket else string.gsub!('timeouttest', '') @timeoutarray.push string - end - client.close end - } - end - end + client.close + end + end + end + + def socket_delete + File.delete('/tmp/sock') if File.exist?('/tmp/sock') + end + end end diff --git a/lib/bunka/ssh.rb b/lib/bunka/ssh.rb index 8f378a3..e482060 100644 --- a/lib/bunka/ssh.rb +++ b/lib/bunka/ssh.rb @@ -12,15 +12,15 @@ def ssh_exec!(ssh, command) unless success abort "FAILED: couldn't execute command (ssh.channel.exec)" end - channel.on_data do |ch,data| - stdout_data+=data + channel.on_data do |ch, data| + stdout_data += data end - channel.on_extended_data do |ch,type,data| - stderr_data+=data + channel.on_extended_data do |ch, type, data| + stderr_data += data end - channel.on_request('exit-status') do |ch,data| + channel.on_request('exit-status') do |ch, data| exit_code = data.read_long end end From fef70c34b00df54d5df33025efd761e5b26f3270 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 8 Apr 2015 14:19:42 +0200 Subject: [PATCH 20/33] Bug fix for multiple tests + code cleanup WIP --- lib/bunka.rb | 8 ++-- lib/bunka/printers.rb | 85 ++++++++++++++++++----------------------- lib/bunka/serverspec.rb | 28 +++++++++----- lib/bunka/socket.rb | 51 ++++++++++++++++--------- 4 files changed, 94 insertions(+), 78 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 2a5b6bc..67d69d4 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -1,12 +1,9 @@ require 'parallel' require 'bunka/bunka' require 'bunka/chef' -require 'bunka/helpers' -require 'bunka/printers' require 'bunka/ssh' require 'bunka/serverspec' require 'bunka/socket' -require 'pry' class Bunka class << self @@ -43,7 +40,10 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv socket_delete start = Time.now Thread.new do - create_unix_socket + create_failed_unix_socket + end + Thread.new do + create_success_unix_socket end sleep(1) serverspecsetup diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index fadfa27..844dca2 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -1,5 +1,4 @@ require 'colorize' -require 'pry' class Bunka class << self @@ -34,15 +33,14 @@ def print_success_stream end def print_spec_streams - @failedarray.reject! { |c| c.empty? } - @successarray.reject! { |c| c.empty? } + @failedarray.reject! {|c| c.empty?} + @successarray.reject! {|c| c.empty?} + @failed = @failedarray.count + @success = @successarray.count - @failed + @timedout = @timeoutarray.count + @total = @failed + @success + @timedout specinvert - - if verbose_success? - print_successspec_stream - else - print_failedspec_stream - end + verbose_success? ? print_successspec_stream : print_failedspec_stream end def print_failedspec_stream @@ -65,63 +63,56 @@ def print_timeoutspec_stream def specinvert if invert? - @dummy = @failedarray + @dummyarray = @failedarray @failedarray = @successarray - @successarray = @dummy + @successarray = @dummyarray + @dummyint = @failed + @failed = @success + @success = @dummyint end end def print_summary - print_output + @serverspecfile ? print_spec_output : print_output puts "\n---------------------------------------\n" - print_counts + @serverspecfile ? print_spec_counts : print_counts end def print_output print "\n" print_timeout_stream - if @serverspecfile && !verbose_success? - puts "\nErrors: ".red + print_failed_stream + print_success_stream if verbose_success? + end + + def print_spec_output + if !verbose_success? + puts "\n\nErrors: ".red else - puts "\nSuccesses: ".green if @serverspecfile + puts "\n\nSuccesses: ".green end - print_spec_streams if @serverspecfile - if @serverspecfile && @timeoutarray.count > 0 + print_spec_streams + if @timeoutarray.count > 0 puts "\nTimed out or unresolved nodes: \n".yellow print_timeoutspec_stream end - print_failed_stream - print_success_stream if verbose_success? end def print_counts - if @serverspecfile - @failed = @failedarray.count - @success = @successarray.count - @timedout = @timeoutarray.count - @total = @failed + @success + @timedout - end - if @serverspecfile - puts "#{'Success'.green}: " + @success.to_s - else - puts "#{'Success'.green}: #{success_output_stream.count}" - end - if @serverspecfile - puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s - else - puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}" - end - if @serverspecfile - puts "#{'Failed: '.red}" + @failed.to_s - else - puts "#{'Failed'.red}: #{failed_output_stream.count}" - end - if @serverspecfile - puts "#{'Total'.blue}: " + @total.to_s - else - puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + failed_output_stream.count}" - end - puts "\n" + puts "#{'Success'.green}: #{success_output_stream.count}" + puts "#{'Timed out or does not resolve'.yellow}: + + #{timeout_output_stream.count}" + puts "#{'Failed'.red}: #{failed_output_stream.count}" + puts "#{'Total'.blue}: #{success_output_stream.count + + timeout_output_stream.count + + failed_output_stream.count} + \n" + end + + def print_spec_counts + puts "#{'Success'.green}: " + @success.to_s + puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s + puts "#{'Failed: '.red}" + @failed.to_s + puts "#{'Total'.blue}: " + @total.to_s + "\n" end end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index a68fbf5..24ece55 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -1,6 +1,5 @@ require 'rspec' require 'bunka/helpers' -require 'pry' require 'socket' class Bunka @@ -24,6 +23,7 @@ def serverspecsetup reporter.register_listener(formatter, *notifications) run_tests @hash = formatter.output_hash + RSpec.clear_examples parse_spec_output_to_socket unless @timedoutbool == true end end @@ -38,7 +38,8 @@ def config end def formatter - @formatter ||= RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) + @formatter ||= + RSpec::Core::Formatters::JsonFormatter.new(config.output_stream) end def reporter @@ -59,16 +60,20 @@ def notifications def run_tests begin timeout @timeout_interval do - Net::SSH.start(@hostx, 'root', paranoid: false, forward_agent: true) + Net::SSH.start(@hostx, 'root') end RSpec::Core::Runner.run([@serverspecfile]) rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH - timeoutspec - @timedoutbool = true - fill_timeout_array + timeout_to_socket rescue RuntimeError puts 'Serverspec failed' - end + end + end + + def timeout_to_socket + timeoutspec + @timedoutbool = true + #fill_timeout_array end def file_control @@ -92,17 +97,20 @@ def file_control end def parse_spec_output_to_socket - c = UNIXSocket.open('/tmp/sock') + failed_sock = UNIXSocket.open('/tmp/failed_sock') + success_sock = UNIXSocket.open('/tmp/success_sock') @failstatus = false @successstatus = false hash[:examples].each do |x| if x[:status] == 'failed' @failstatus = true - c.write('failedtest' + "\n" + @hostx + ': ' + x[:full_description]) + failed_sock.write("\n" + @hostx +': ' + x[:full_description]) elsif x[:status] == 'passed' - c.write('successtest' + "\n" + @hostx + ': ' + x[:full_description]) + success_sock.write("\n" + @hostx +': ' + x[:full_description]) end end + failed_sock.close + success_sock.close check_invert end diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 34031a0..f5b5597 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -1,30 +1,47 @@ require 'socket' -require 'pry' class Bunka class << self - def create_unix_socket - server4 = UNIXServer.new('/tmp/sock') # Socket to listen - loop do # Servers run forever - Thread.start(server4.accept) do |client| - string = client.read - if string.start_with?('failedtest') - string.gsub!('failedtest', '') - @failedarray.push string - elsif string.start_with?('successtest') - string.gsub!('successtest', '') - @successarray.push string - else - string.gsub!('timeouttest', '') - @timeoutarray.push string - end + def create_failed_unix_socket + failed_server = UNIXServer.new('/tmp/failed_sock') # Socket to listen + loop do + Thread.start(failed_server.accept) do |client| + testresult = client.read + @failedarray.push testresult + client.close + end + end + end + + def create_success_unix_socket + success_server = UNIXServer.new('/tmp/success_sock') # Socket to listen + loop do + Thread.start(success_server.accept) do |client| + testresult = client.read + @successarray.push testresult client.close end end end def socket_delete - File.delete('/tmp/sock') if File.exist?('/tmp/sock') + File.delete('/tmp/failed_sock') if File.exist?('/tmp/failed_sock') + File.delete('/tmp/success_sock') if File.exist?('/tmp/success_sock') + end + + def fill_failedarray + @testresult.gsub!('failedtest', '') + @failedarray.push @testresult + end + + def fill_successarray + @testresult.gsub!('successtest', '') + @successarray.push @testresult + end + + def fill_timeoutarray + @testresult.gsub!('timeouttest', '') + @timeoutarray.push @testresult end end end From 00ca0153fe336837436f7006051148b8db8e4421 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 8 Apr 2015 14:27:24 +0200 Subject: [PATCH 21/33] timeout readded with socket --- lib/bunka.rb | 5 ++++- lib/bunka/serverspec.rb | 6 +++--- lib/bunka/socket.rb | 27 ++++++++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 67d69d4..3cd8aa9 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -45,7 +45,10 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv Thread.new do create_success_unix_socket end - sleep(1) + Thread.new do + create_timeout_unix_socket + end + sleep(2) serverspecsetup print_summary socket_delete diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 24ece55..acff865 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -73,7 +73,7 @@ def run_tests def timeout_to_socket timeoutspec @timedoutbool = true - #fill_timeout_array + fill_timeout_array end def file_control @@ -125,8 +125,8 @@ def check_invert end def fill_timeout_array - c = UNIXSocket.open('/tmp/sock') - c.write('timeouttest' + ' - ' + @hostx) + timeout_socket = UNIXSocket.open('/tmp/timeout_sock') + timeout_socket.write(' - ' + @hostx) end def rspec_config diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index f5b5597..93472f6 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -23,25 +23,22 @@ def create_success_unix_socket end end end + + def create_timeout_unix_socket + timeout_server = UNIXServer.new('/tmp/timeout_sock') # Socket to listen + loop do + Thread.start(timeout_server.accept) do |client| + testresult = client.read + @timeoutarray.push testresult + client.close + end + end + end def socket_delete File.delete('/tmp/failed_sock') if File.exist?('/tmp/failed_sock') File.delete('/tmp/success_sock') if File.exist?('/tmp/success_sock') - end - - def fill_failedarray - @testresult.gsub!('failedtest', '') - @failedarray.push @testresult - end - - def fill_successarray - @testresult.gsub!('successtest', '') - @successarray.push @testresult - end - - def fill_timeoutarray - @testresult.gsub!('timeouttest', '') - @timeoutarray.push @testresult + File.delete('/tmp/timeout_sock') if File.exist?('/tmp/timeout_sock') end end end From 167d64eb9c5fd31652bb93e9fe665fa13f6aac50 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 8 Apr 2015 14:44:07 +0200 Subject: [PATCH 22/33] code clean up + file_expand nil if query --- lib/bunka.rb | 4 ++-- lib/bunka/printers.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index 3cd8aa9..a95415d 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -15,7 +15,7 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, @threads = sequential ? 1 : threads @timeout_interval = timeout_interval @verbose_success = verbose_success - @file = File.expand_path(file) + @file ? File.expand_path(file) : nil Parallel.map(nodes, in_threads: @threads) do |fqdn| execute_query fqdn @@ -32,7 +32,7 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv @processes = sequential ? 1 : processes @timeout_interval = timeout_interval @verbose_success = verbose_success - @file = File.expand_path(file) + @file ? File.expand_path(file) : nil @failedarray = [] @successarray = [] @timeoutarray = [] diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 844dca2..954312d 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -100,12 +100,12 @@ def print_spec_output def print_counts puts "#{'Success'.green}: #{success_output_stream.count}" - puts "#{'Timed out or does not resolve'.yellow}: + - #{timeout_output_stream.count}" + puts "#{'Timed out or does not resolve'.yellow}: " + + "#{timeout_output_stream.count}" puts "#{'Failed'.red}: #{failed_output_stream.count}" puts "#{'Total'.blue}: #{success_output_stream.count + timeout_output_stream.count + - failed_output_stream.count} + \n" + failed_output_stream.count}" end def print_spec_counts From d950f75bc2f4c7e3dc8b4c2adaef46dfff77d200 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Wed, 8 Apr 2015 16:17:31 +0200 Subject: [PATCH 23/33] code cleanup --- lib/bunka.rb | 19 ++++---------- lib/bunka/bunka.rb | 18 ++++++-------- lib/bunka/chef.rb | 2 +- lib/bunka/printers.rb | 48 +++++++++++++++++------------------ lib/bunka/serverspec.rb | 55 ++++++++++++++++++++++------------------- lib/bunka/socket.rb | 16 ++++++++++-- lib/bunka/ssh.rb | 8 +++--- 7 files changed, 84 insertions(+), 82 deletions(-) diff --git a/lib/bunka.rb b/lib/bunka.rb index a95415d..2ef2252 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -15,7 +15,7 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, @threads = sequential ? 1 : threads @timeout_interval = timeout_interval @verbose_success = verbose_success - @file ? File.expand_path(file) : nil + @file = file ? File.expand_path(file) : nil Parallel.map(nodes, in_threads: @threads) do |fqdn| execute_query fqdn @@ -32,23 +32,14 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv @processes = sequential ? 1 : processes @timeout_interval = timeout_interval @verbose_success = verbose_success - @file ? File.expand_path(file) : nil + @file = file ? File.expand_path(file) : nil @failedarray = [] @successarray = [] @timeoutarray = [] - - socket_delete + + socket_delete start = Time.now - Thread.new do - create_failed_unix_socket - end - Thread.new do - create_success_unix_socket - end - Thread.new do - create_timeout_unix_socket - end - sleep(2) + create_sockets serverspecsetup print_summary socket_delete diff --git a/lib/bunka/bunka.rb b/lib/bunka/bunka.rb index d9463b8..98ea524 100644 --- a/lib/bunka/bunka.rb +++ b/lib/bunka/bunka.rb @@ -11,18 +11,16 @@ def nodes end def execute_query(fqdn) - begin - timeout @timeout_interval do - Net::SSH.start(fqdn, 'root', paranoid: false, forward_agent: true) do |ssh| - output = ssh_exec!(ssh, @command) - parse_output output, fqdn - end + timeout @timeout_interval do + Net::SSH.start(fqdn, 'root', paranoid: false, forward_agent: true) do |ssh| + output = ssh_exec!(ssh, @command) + parse_output output, fqdn end - rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH => e - timed_out "#{fqdn}: #{e.message}" - rescue Exception => e - timed_out "#{fqdn}: #{e.inspect}" end + rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH => e + timed_out "#{fqdn}: #{e.message}" + rescue Exception => e + timed_out "#{fqdn}: #{e.inspect}" end def parse_output(output, fqdn) diff --git a/lib/bunka/chef.rb b/lib/bunka/chef.rb index d8cc184..ebe4d55 100644 --- a/lib/bunka/chef.rb +++ b/lib/bunka/chef.rb @@ -2,7 +2,7 @@ class Bunka class << self - def knife_search query + def knife_search(query) # Monkey patch Chef::Knife::UI to hide stdout Chef::Knife::UI.class_eval do def stdout diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 954312d..1eaca83 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -33,14 +33,14 @@ def print_success_stream end def print_spec_streams - @failedarray.reject! {|c| c.empty?} - @successarray.reject! {|c| c.empty?} + @failedarray.reject! &:empty? + @successarray.reject! &:empty? @failed = @failedarray.count - @success = @successarray.count - @failed + @success = @successarray.count - @failedarray.count @timedout = @timeoutarray.count @total = @failed + @success + @timedout specinvert - verbose_success? ? print_successspec_stream : print_failedspec_stream + verbose_success? ? print_successspec_stream : print_failedspec_stream end def print_failedspec_stream @@ -62,14 +62,11 @@ def print_timeoutspec_stream end def specinvert - if invert? - @dummyarray = @failedarray - @failedarray = @successarray - @successarray = @dummyarray - @dummyint = @failed - @failed = @success - @success = @dummyint - end + return unless invert? + @dummyarray, @failedarray, @successarray = @failedarray, @successarray, @dummyarray + @dummyint = @failed + @failed = @success + @success = @dummyint end def print_summary @@ -92,27 +89,26 @@ def print_spec_output puts "\n\nSuccesses: ".green end print_spec_streams - if @timeoutarray.count > 0 - puts "\nTimed out or unresolved nodes: \n".yellow - print_timeoutspec_stream - end + return unless @timeoutarray.count > 0 + puts "\nTimed out or unresolved nodes: \n".yellow + print_timeoutspec_stream end def print_counts - puts "#{'Success'.green}: #{success_output_stream.count}" - puts "#{'Timed out or does not resolve'.yellow}: " + + puts "#{'Success:'.green} #{success_output_stream.count}" + puts "#{'Timed out or does not resolve:'.yellow} " \ "#{timeout_output_stream.count}" - puts "#{'Failed'.red}: #{failed_output_stream.count}" - puts "#{'Total'.blue}: #{success_output_stream.count + - timeout_output_stream.count + - failed_output_stream.count}" + puts "#{'Failed:'.red} #{failed_output_stream.count}" + puts "#{'Total:'.blue} #{success_output_stream.count \ + timeout_output_stream.count \ + failed_output_stream.count}" end def print_spec_counts - puts "#{'Success'.green}: " + @success.to_s - puts "#{'Timed out or does not resolve'.yellow}: " + @timedout.to_s - puts "#{'Failed: '.red}" + @failed.to_s - puts "#{'Total'.blue}: " + @total.to_s + "\n" + puts "#{'Success:'.green} " + @success.to_s + puts "#{'Timed out or does not resolve:'.yellow} " + @timedout.to_s + puts "#{'Failed:'.red} " + @failed.to_s + puts "#{'Total:'.blue} " + @total.to_s + "\n" end end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index acff865..8d89f68 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -5,7 +5,8 @@ class Bunka class << self def serverspecsetup - file_control + check_serverfile_existence + check_serverspecfile_existence @hosts.each_slice(@processes).each do |h| Parallel.map(h, in_processes: @processes) do |hostx| rspec_config @@ -24,7 +25,7 @@ def serverspecsetup run_tests @hash = formatter.output_hash RSpec.clear_examples - parse_spec_output_to_socket unless @timedoutbool == true + testresults_to_sockets unless @timedoutbool == true end end end @@ -58,16 +59,14 @@ def notifications end def run_tests - begin - timeout @timeout_interval do - Net::SSH.start(@hostx, 'root') - end - RSpec::Core::Runner.run([@serverspecfile]) - rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH - timeout_to_socket - rescue RuntimeError - puts 'Serverspec failed' + timeout @timeout_interval do + Net::SSH.start(@hostx, 'root') end + RSpec::Core::Runner.run([@serverspecfile]) + rescue TimeoutError, Errno::ETIMEDOUT, SocketError, Errno::EHOSTUNREACH + timeout_to_socket + rescue RuntimeError + puts 'Serverspec failed' end def timeout_to_socket @@ -76,10 +75,10 @@ def timeout_to_socket fill_timeout_array end - def file_control + def check_serverfile_existence if @file if File.exist?(@file) - @hosts = File.readlines(@file).each { |l| l.chomp! } + @hosts = File.readlines(@file).each &:chomp! else puts 'Serverfile not found' exit @@ -90,28 +89,34 @@ def file_control puts 'Wrong querry' exit end - if File.exist?(@serverspecfile) == false - puts 'Serverspecfile not found' - exit - end end - def parse_spec_output_to_socket - failed_sock = UNIXSocket.open('/tmp/failed_sock') - success_sock = UNIXSocket.open('/tmp/success_sock') + def check_serverspecfile_existence + return unless File.exist?(@serverspecfile) == false + puts 'Serverspecfile not found' + exit + end + + def testresults_to_sockets + @failed_sock = UNIXSocket.open('/tmp/failed_sock') + @success_sock = UNIXSocket.open('/tmp/success_sock') @failstatus = false @successstatus = false + loop_testresults_in_sockets + @failed_sock.close + @success_sock.close + check_invert + end + + def loop_testresults_in_sockets hash[:examples].each do |x| if x[:status] == 'failed' @failstatus = true - failed_sock.write("\n" + @hostx +': ' + x[:full_description]) + @failed_sock.write("\n" + @hostx + ': ' + x[:full_description]) elsif x[:status] == 'passed' - success_sock.write("\n" + @hostx +': ' + x[:full_description]) + @success_sock.write("\n" + @hostx + ': ' + x[:full_description]) end end - failed_sock.close - success_sock.close - check_invert end def check_invert diff --git a/lib/bunka/socket.rb b/lib/bunka/socket.rb index 93472f6..bd7b4b7 100644 --- a/lib/bunka/socket.rb +++ b/lib/bunka/socket.rb @@ -12,7 +12,7 @@ def create_failed_unix_socket end end end - + def create_success_unix_socket success_server = UNIXServer.new('/tmp/success_sock') # Socket to listen loop do @@ -23,7 +23,7 @@ def create_success_unix_socket end end end - + def create_timeout_unix_socket timeout_server = UNIXServer.new('/tmp/timeout_sock') # Socket to listen loop do @@ -40,5 +40,17 @@ def socket_delete File.delete('/tmp/success_sock') if File.exist?('/tmp/success_sock') File.delete('/tmp/timeout_sock') if File.exist?('/tmp/timeout_sock') end + + def create_sockets + Thread.new do + create_failed_unix_socket + end + Thread.new do + create_success_unix_socket + end + Thread.new do + create_timeout_unix_socket + end + end end end diff --git a/lib/bunka/ssh.rb b/lib/bunka/ssh.rb index e482060..93cc418 100644 --- a/lib/bunka/ssh.rb +++ b/lib/bunka/ssh.rb @@ -8,19 +8,19 @@ def ssh_exec!(ssh, command) exit_code = nil ssh.open_channel do |channel| - channel.exec(command) do |ch, success| + channel.exec(command) do |_ch, success| unless success abort "FAILED: couldn't execute command (ssh.channel.exec)" end - channel.on_data do |ch, data| + channel.on_data do |_ch, data| stdout_data += data end - channel.on_extended_data do |ch, type, data| + channel.on_extended_data do |_ch, _type, data| stderr_data += data end - channel.on_request('exit-status') do |ch, data| + channel.on_request('exit-status') do |_ch, data| exit_code = data.read_long end end From 48584be95ae395e33e9f1d34b9d3e819b08d29fb Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 09:45:45 +0200 Subject: [PATCH 24/33] add File WIP --- bin/bunka | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bin/bunka b/bin/bunka index 037aad1..73fc30c 100755 --- a/bin/bunka +++ b/bin/bunka @@ -27,6 +27,20 @@ class BunkaCommand < Thor def serverspec(serverspecfile, query='name:*') Bunka.testserverspec(serverspecfile,query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) end + + map '-f' => :file + + desc 'file PATH [QUERY]', 'Test existence of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + + def file(path, query='name:*') + Bunka.testfile(file, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start From 44ec2224c6add97fe6ac126c47943a06368e305a Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 10:38:29 +0200 Subject: [PATCH 25/33] File methode finished --- bin/bunka | 7 ++----- lib/bunka.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bin/bunka b/bin/bunka index 73fc30c..f84b98c 100755 --- a/bin/bunka +++ b/bin/bunka @@ -4,7 +4,6 @@ require 'thor' class BunkaCommand < Thor map '-t' => :test - desc 'test COMMAND [QUERY]', 'Execute command on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false @@ -15,8 +14,8 @@ class BunkaCommand < Thor def test(command, query = 'name:*') Bunka.test(command, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + map '-s' => :serverspec - desc 'serverspec SERVERSPECFILE', 'test nodes with serverspec' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false @@ -29,7 +28,6 @@ class BunkaCommand < Thor end map '-f' => :file - desc 'file PATH [QUERY]', 'Test existence of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false @@ -37,9 +35,8 @@ class BunkaCommand < Thor option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil - def file(path, query='name:*') - Bunka.testfile(file, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.testfile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end end diff --git a/lib/bunka.rb b/lib/bunka.rb index 2ef2252..f178741 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -45,5 +45,22 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv socket_delete puts Time.now - start end + + def testfile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "find . -name '#{path}' | egrep '.*'" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end From 549598db84d6c70d54cb8c7e9497ffe009956e05 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 11:46:15 +0200 Subject: [PATCH 26/33] add remove --- bin/bunka | 22 +++++++++++++++++----- bunka.gemspec | 2 +- lib/bunka.rb | 17 +++++++++++++++++ lib/bunka/printers.rb | 7 +++++-- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/bin/bunka b/bin/bunka index f84b98c..c9f062f 100755 --- a/bin/bunka +++ b/bin/bunka @@ -14,7 +14,7 @@ class BunkaCommand < Thor def test(command, query = 'name:*') Bunka.test(command, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end - + map '-s' => :serverspec desc 'serverspec SERVERSPECFILE', 'test nodes with serverspec' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false @@ -23,21 +23,33 @@ class BunkaCommand < Thor option :processes, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers' - def serverspec(serverspecfile, query='name:*') - Bunka.testserverspec(serverspecfile,query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) + def serverspec(serverspecfile, query = 'name:*') + Bunka.testserverspec(serverspecfile, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) end map '-f' => :file - desc 'file PATH [QUERY]', 'Test existence of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + desc 'file PATH [QUERY]', 'Test existence of a file or directory on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil - def file(path, query='name:*') + def file(path, query = 'name:*') Bunka.testfile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + + map '-r' => :remove + desc 'remove PATH [QUERY]', 'Remove a file or directory on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + def remove(path, query = 'name:*') + Bunka.removefile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/bunka.gemspec b/bunka.gemspec index bb2f86b..de38f62 100644 --- a/bunka.gemspec +++ b/bunka.gemspec @@ -7,7 +7,7 @@ Gem::Specification.new do |spec| spec.description = 'A gem to perform command over parallel ssh connections on multiple chef serverspec. Output is rspec-like.' spec.authors = ['Steven De Coeyer', 'Jeroen Jacobs'] spec.email = 'tech@openminds.be' - spec.files = `git ls-files`.split($\) + spec.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR) spec.homepage = 'https://github.com/openminds/bunka' spec.license = 'MIT' diff --git a/lib/bunka.rb b/lib/bunka.rb index f178741..4d8ddc3 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -62,5 +62,22 @@ def testfile(path, query, timeout_interval, verbose_success, invert, sequential, print_summary end + + def removefile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "rm -r '#{path}'" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 1eaca83..452a4aa 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -15,18 +15,21 @@ def print_timeout end def print_failed_stream + puts "\nFailures: \n".red failed_output_stream.each do |output| puts output.red end end def print_timeout_stream + puts "\nTimed out or unresolved nodes: \n".yellow timeout_output_stream.each do |output| puts output.yellow end end def print_success_stream + puts "\nSuccesses: \n".green success_output_stream.each do |output| puts output.green end @@ -99,8 +102,8 @@ def print_counts puts "#{'Timed out or does not resolve:'.yellow} " \ "#{timeout_output_stream.count}" puts "#{'Failed:'.red} #{failed_output_stream.count}" - puts "#{'Total:'.blue} #{success_output_stream.count \ - timeout_output_stream.count \ + puts "#{'Total:'.blue} #{success_output_stream.count + + timeout_output_stream.count + failed_output_stream.count}" end From 39259d9e0e9f57e1df4d851db487743cd1fdc544 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 14:32:05 +0200 Subject: [PATCH 27/33] add Md5 --- bin/bunka | 12 ++++++++++++ lib/bunka.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bin/bunka b/bin/bunka index c9f062f..f51ead5 100755 --- a/bin/bunka +++ b/bin/bunka @@ -50,6 +50,18 @@ class BunkaCommand < Thor def remove(path, query = 'name:*') Bunka.removefile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + + map '-md5' => :md5sum + desc 'md5sum PATH MD5SUM [QUERY]', 'Compare a MD5sum with a MD5sum of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + def md5sum(path, checksum, query = 'name:*') + Bunka.md5sum(path, checksum, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/lib/bunka.rb b/lib/bunka.rb index 4d8ddc3..0e81d13 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -79,5 +79,22 @@ def removefile(path, query, timeout_interval, verbose_success, invert, sequentia print_summary end + + def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "md5sum -c - <<<'#{checksum} #{path}'" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end From 56978db62192a4bf3e60598cee48a676a46ed77c Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 15:22:01 +0200 Subject: [PATCH 28/33] add chmod --- bin/bunka | 15 +++++++++++++-- lib/bunka.rb | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/bin/bunka b/bin/bunka index f51ead5..5fe4ea1 100755 --- a/bin/bunka +++ b/bin/bunka @@ -16,7 +16,7 @@ class BunkaCommand < Thor end map '-s' => :serverspec - desc 'serverspec SERVERSPECFILE', 'test nodes with serverspec' + desc 'serverspec SERVERSPECFILE [QUERY]', 'Test nodes with serverspec, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 @@ -50,7 +50,7 @@ class BunkaCommand < Thor def remove(path, query = 'name:*') Bunka.removefile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end - + map '-md5' => :md5sum desc 'md5sum PATH MD5SUM [QUERY]', 'Compare a MD5sum with a MD5sum of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false @@ -62,6 +62,17 @@ class BunkaCommand < Thor def md5sum(path, checksum, query = 'name:*') Bunka.md5sum(path, checksum, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + + desc 'chmod PERMISSIONS PATH [QUERY]', 'Change the permissions of a file or folder on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + def chmod(permissions, path, query = 'name:*') + Bunka.chmod(permissions, path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/lib/bunka.rb b/lib/bunka.rb index 0e81d13..080f25c 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -96,5 +96,22 @@ def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, seq print_summary end + + def chmod(permissions, path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "chmod #{permissions} #{path}" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end From f9a25d0135be91c9b0904711bff78afb20850ac6 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 15:22:39 +0200 Subject: [PATCH 29/33] clean up code --- bin/bunka | 4 ++-- lib/bunka.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/bunka b/bin/bunka index 5fe4ea1..1cd150d 100755 --- a/bin/bunka +++ b/bin/bunka @@ -50,7 +50,7 @@ class BunkaCommand < Thor def remove(path, query = 'name:*') Bunka.removefile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end - + map '-md5' => :md5sum desc 'md5sum PATH MD5SUM [QUERY]', 'Compare a MD5sum with a MD5sum of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false @@ -62,7 +62,7 @@ class BunkaCommand < Thor def md5sum(path, checksum, query = 'name:*') Bunka.md5sum(path, checksum, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end - + desc 'chmod PERMISSIONS PATH [QUERY]', 'Change the permissions of a file or folder on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false option :invert, type: :boolean, desc: 'invert matched results', default: false diff --git a/lib/bunka.rb b/lib/bunka.rb index 080f25c..bb2b5b8 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -96,7 +96,7 @@ def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, seq print_summary end - + def chmod(permissions, path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) @command = "chmod #{permissions} #{path}" @invert = invert From df4d3e4aff10fd649a66c879289d8026457374e9 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 16:23:07 +0200 Subject: [PATCH 30/33] add listeningport --- bin/bunka | 14 +++++++++++++- lib/bunka.rb | 19 ++++++++++++++++++- lib/bunka/serverspec.rb | 1 + 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/bin/bunka b/bin/bunka index 1cd150d..6aa911e 100755 --- a/bin/bunka +++ b/bin/bunka @@ -36,7 +36,7 @@ class BunkaCommand < Thor option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil def file(path, query = 'name:*') - Bunka.testfile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.findfile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end map '-r' => :remove @@ -73,6 +73,18 @@ class BunkaCommand < Thor def chmod(permissions, path, query = 'name:*') Bunka.chmod(permissions, path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + + map '-p' => :port + desc 'port PORTNUMBER [QUERY]', 'Test if specific port is listening on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + def port(port, query = 'name:*') + Bunka.port(port, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/lib/bunka.rb b/lib/bunka.rb index bb2b5b8..d2177dd 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -46,7 +46,7 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv puts Time.now - start end - def testfile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def findfile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) @command = "find . -name '#{path}' | egrep '.*'" @invert = invert @query = query @@ -113,5 +113,22 @@ def chmod(permissions, path, query, timeout_interval, verbose_success, invert, s print_summary end + + def port(port, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "netstat -anp |grep ':#{port}'" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 8d89f68..8878448 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -67,6 +67,7 @@ def run_tests timeout_to_socket rescue RuntimeError puts 'Serverspec failed' + exit end def timeout_to_socket From 8845d5f45a13c7937dc37d5da2d31cf3b826a8f8 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Thu, 9 Apr 2015 16:47:43 +0200 Subject: [PATCH 31/33] added servicecheck --- bin/bunka | 12 ++++++++++++ lib/bunka.rb | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/bin/bunka b/bin/bunka index 6aa911e..21ef875 100755 --- a/bin/bunka +++ b/bin/bunka @@ -85,6 +85,18 @@ class BunkaCommand < Thor def port(port, query = 'name:*') Bunka.port(port, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) end + + map '-s' => :service + desc 'service NAME [QUERY]', 'Test if specific service is running on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false + option :invert, type: :boolean, desc: 'invert matched results', default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 + option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 + option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + def service(name, query = 'name:*') + Bunka.service(name, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + end end BunkaCommand.start diff --git a/lib/bunka.rb b/lib/bunka.rb index d2177dd..815595d 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -130,5 +130,22 @@ def port(port, query, timeout_interval, verbose_success, invert, sequential, thr print_summary end + + def service(name, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + @command = "service #{name} status" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + + print_summary + end end end From e5ba3df27c4f7b3bcea4619208e6e3bd056d2f08 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 10 Apr 2015 10:43:54 +0200 Subject: [PATCH 32/33] code cleanup --- bin/bunka | 181 ++++++++++++++++++++++++---------------- bunka.gemspec | 10 +-- lib/bunka.rb | 88 ++++--------------- lib/bunka/bunka.rb | 12 ++- lib/bunka/printers.rb | 5 +- lib/bunka/serverspec.rb | 41 +++++---- 6 files changed, 170 insertions(+), 167 deletions(-) diff --git a/bin/bunka b/bin/bunka index 21ef875..ad5d3a0 100755 --- a/bin/bunka +++ b/bin/bunka @@ -4,98 +4,137 @@ require 'thor' class BunkaCommand < Thor map '-t' => :test - desc 'test COMMAND [QUERY]', 'Execute command on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + desc 'test (-t) COMMAND [QUERY]', 'Execute command on nodes, scoped on' \ + ' the given query if query is given. Query syntax should be' \ + ' the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def test(command, query = 'name:*') - Bunka.test(command, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.test(command, query, options[:timeout], options[:'print-success'], + options[:invert], options[:sequential], options[:threads], + options[:'from-file']) end map '-s' => :serverspec - desc 'serverspec SERVERSPECFILE [QUERY]', 'Test nodes with serverspec, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :processes, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers' + desc 'serverspec (-s) SERVERSPECFILE [QUERY]', 'Test nodes with serverspec,' \ + ' scoped on the given query if query is given. Query syntax' \ + ' should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :processes, type: :numeric, desc: 'number of processes', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def serverspec(serverspecfile, query = 'name:*') - Bunka.testserverspec(serverspecfile, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:processes], options[:'from-file']) + Bunka.testserverspec(serverspecfile, query, options[:timeout], + options[:'print-success'], options[:invert], + options[:sequential], options[:processes], + options[:'from-file']) end map '-f' => :file - desc 'file PATH [QUERY]', 'Test existence of a file or directory on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + desc 'file (-f) PATH [QUERY]', 'Test existence of a file or directory' \ + ' on nodes, scoped on the given query if query is given.' \ + ' Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def file(path, query = 'name:*') - Bunka.findfile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) - end - - map '-r' => :remove - desc 'remove PATH [QUERY]', 'Remove a file or directory on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil - def remove(path, query = 'name:*') - Bunka.removefile(path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.findfile(path, query, options[:timeout], options[:'print-success'], + options[:invert], options[:sequential], options[:threads], + options[:'from-file']) end map '-md5' => :md5sum - desc 'md5sum PATH MD5SUM [QUERY]', 'Compare a MD5sum with a MD5sum of a file on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + desc 'md5sum (-md5) PATH MD5SUM [QUERY]', 'Compare a MD5sum with a' \ + ' MD5sum of a file on nodes, scoped on the given query if query' \ + ' is given. Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def md5sum(path, checksum, query = 'name:*') - Bunka.md5sum(path, checksum, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) - end - - desc 'chmod PERMISSIONS PATH [QUERY]', 'Change the permissions of a file or folder on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil - def chmod(permissions, path, query = 'name:*') - Bunka.chmod(permissions, path, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.md5sum(path, checksum, query, options[:timeout], + options[:'print-success'], options[:invert], + options[:sequential], options[:threads], + options[:'from-file']) end map '-p' => :port - desc 'port PORTNUMBER [QUERY]', 'Test if specific port is listening on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + desc 'port (-p) PORTNUMBER [QUERY]', 'Test if specific port is listening' \ + ' on nodes, scoped on the given query if query is given.' \ + ' Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def port(port, query = 'name:*') - Bunka.port(port, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.port(port, query, options[:timeout], options[:'print-success'], + options[:invert], options[:sequential], options[:threads], + options[:'from-file']) end map '-s' => :service - desc 'service NAME [QUERY]', 'Test if specific service is running on nodes, scoped on the given query if query is given. Query syntax should be the same as `knife search` syntax.' - option :sequential, type: :boolean, desc: 'run over nodes sequantially', default: false - option :invert, type: :boolean, desc: 'invert matched results', default: false - option :timeout, type: :numeric, desc: 'timeout interval per ssh connection (default: 15)', default: 15 - option :threads, type: :numeric, desc: 'number of threads (default: 15)', default: 15 - option :'print-success', type: :boolean, desc: 'prints output of successful commands', default: false - option :'from-file', type: :string, desc: 'path to file with list of servers', default: nil + desc 'service (-s) NAME [QUERY]', 'Test if specific service is running' \ + ' on nodes, scoped on the given query if query is given.' \ + ' Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil def service(name, query = 'name:*') - Bunka.service(name, query, options[:timeout], options[:'print-success'], options[:invert], options[:sequential], options[:threads], options[:'from-file']) + Bunka.service(name, query, options[:timeout], options[:'print-success'], + options[:invert], options[:sequential], options[:threads], + options[:'from-file']) end end diff --git a/bunka.gemspec b/bunka.gemspec index de38f62..282fb79 100644 --- a/bunka.gemspec +++ b/bunka.gemspec @@ -3,9 +3,11 @@ Gem::Specification.new do |spec| spec.version = '1.4.0' spec.executables << 'bunka' spec.date = '2013-11-26' - spec.summary = 'Parallel ssh commands over chef servers with rspec-like output' - spec.description = 'A gem to perform command over parallel ssh connections on multiple chef serverspec. Output is rspec-like.' - spec.authors = ['Steven De Coeyer', 'Jeroen Jacobs'] + spec.summary = 'Parallel ssh commands over' \ + 'chef servers with rspec-like output' + spec.description = 'A gem to perform command over parallel ssh' \ + 'connections on multiple chef serverspec. Output is rspec-like.' + spec.authors = ['Steven De Coeyer', 'Jeroen Jacobs', 'Giel De Bleser'] spec.email = 'tech@openminds.be' spec.files = `git ls-files`.split($OUTPUT_RECORD_SEPARATOR) spec.homepage = 'https://github.com/openminds/bunka' @@ -15,7 +17,5 @@ Gem::Specification.new do |spec| spec.add_dependency 'colorize' spec.add_dependency 'net-ssh' spec.add_dependency 'parallel' - spec.add_dependency 'rake' spec.add_dependency 'thor' - spec.add_dependency 'process_shared' end diff --git a/lib/bunka.rb b/lib/bunka.rb index 815595d..945cb4d 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -7,7 +7,8 @@ class Bunka class << self - def test(command, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def test(command, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) @command = command @invert = invert @query = query @@ -16,15 +17,12 @@ def test(command, query, timeout_interval, verbose_success, invert, sequential, @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary + parallel_exec end - def testserverspec(serverspecfile, query, timeout_interval, verbose_success, invert, sequential, processes, file) + def testserverspec(serverspecfile, query, timeout_interval, + verbose_success, invert, sequential, + processes, file) @serverspecfile = File.expand_path(serverspecfile) @query = query @invert = invert @@ -46,7 +44,8 @@ def testserverspec(serverspecfile, query, timeout_interval, verbose_success, inv puts Time.now - start end - def findfile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def findfile(path, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) @command = "find . -name '#{path}' | egrep '.*'" @invert = invert @query = query @@ -55,32 +54,11 @@ def findfile(path, query, timeout_interval, verbose_success, invert, sequential, @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary - end - - def removefile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) - @command = "rm -r '#{path}'" - @invert = invert - @query = query - @sequential = sequential - @threads = sequential ? 1 : threads - @timeout_interval = timeout_interval - @verbose_success = verbose_success - @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary + parallel_exec end - def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def md5sum(path, checksum, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) @command = "md5sum -c - <<<'#{checksum} #{path}'" @invert = invert @query = query @@ -89,32 +67,11 @@ def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, seq @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary + parallel_exec end - def chmod(permissions, path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) - @command = "chmod #{permissions} #{path}" - @invert = invert - @query = query - @sequential = sequential - @threads = sequential ? 1 : threads - @timeout_interval = timeout_interval - @verbose_success = verbose_success - @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary - end - - def port(port, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def port(port, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) @command = "netstat -anp |grep ':#{port}'" @invert = invert @query = query @@ -123,15 +80,11 @@ def port(port, query, timeout_interval, verbose_success, invert, sequential, thr @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary + parallel_exec end - def service(name, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) + def service(name, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) @command = "service #{name} status" @invert = invert @query = query @@ -140,12 +93,7 @@ def service(name, query, timeout_interval, verbose_success, invert, sequential, @timeout_interval = timeout_interval @verbose_success = verbose_success @file = file ? File.expand_path(file) : nil - - Parallel.map(nodes, in_threads: @threads) do |fqdn| - execute_query fqdn - end - - print_summary + parallel_exec end end end diff --git a/lib/bunka/bunka.rb b/lib/bunka/bunka.rb index 98ea524..eabd9a7 100644 --- a/lib/bunka/bunka.rb +++ b/lib/bunka/bunka.rb @@ -2,6 +2,15 @@ class Bunka class << self + def parallel_exec + start = Time.now + Parallel.map(nodes, in_threads: @threads) do |fqdn| + execute_query fqdn + end + print_summary + puts Time.now - start + end + def nodes if @file File.readlines(@file).collect(&:strip) @@ -12,7 +21,8 @@ def nodes def execute_query(fqdn) timeout @timeout_interval do - Net::SSH.start(fqdn, 'root', paranoid: false, forward_agent: true) do |ssh| + Net::SSH.start(fqdn, 'root', paranoid: false, + forward_agent: true) do |ssh| output = ssh_exec!(ssh, @command) parse_output output, fqdn end diff --git a/lib/bunka/printers.rb b/lib/bunka/printers.rb index 452a4aa..c7d7353 100644 --- a/lib/bunka/printers.rb +++ b/lib/bunka/printers.rb @@ -66,7 +66,8 @@ def print_timeoutspec_stream def specinvert return unless invert? - @dummyarray, @failedarray, @successarray = @failedarray, @successarray, @dummyarray + @dummyarray, @failedarray = @failedarray, @successarray + @successarray = @dummyarray @dummyint = @failed @failed = @success @success = @dummyint @@ -87,7 +88,7 @@ def print_output def print_spec_output if !verbose_success? - puts "\n\nErrors: ".red + puts "\n\nFailures: ".red else puts "\n\nSuccesses: ".green end diff --git a/lib/bunka/serverspec.rb b/lib/bunka/serverspec.rb index 8878448..111fe0e 100644 --- a/lib/bunka/serverspec.rb +++ b/lib/bunka/serverspec.rb @@ -5,31 +5,39 @@ class Bunka class << self def serverspecsetup - check_serverfile_existence - check_serverspecfile_existence + file_existence @hosts.each_slice(@processes).each do |h| Parallel.map(h, in_processes: @processes) do |hostx| - rspec_config ENV['TARGET_HOST'] = hostx @hostx = hostx - config - formatter - # create reporter with json formatter - reporter - config.instance_variable_set(:@reporter, reporter) - # internal hack - # api may not be stable, make sure lock down Rspec version - loader - notifications - reporter.register_listener(formatter, *notifications) - run_tests - @hash = formatter.output_hash + rspecrunner RSpec.clear_examples testresults_to_sockets unless @timedoutbool == true end end end + def file_existence + check_serverfile_existence + check_serverspecfile_existence + end + + def rspecrunner + rspec_config + config + formatter + # create reporter with json formatter + reporter + config.instance_variable_set(:@reporter, reporter) + # internal hack + # api may not be stable, make sure lock down Rspec version + loader + notifications + reporter.register_listener(formatter, *notifications) + run_tests + hash + end + def hash @hash ||= formatter.output_hash end @@ -86,9 +94,6 @@ def check_serverfile_existence end elsif @query @hosts = knife_search @query - else - puts 'Wrong querry' - exit end end From d294170ba5623ddd6104a8e0ba9aa1d165d250b8 Mon Sep 17 00:00:00 2001 From: Giel De Bleser Date: Fri, 24 Apr 2015 15:22:06 +0200 Subject: [PATCH 33/33] changed find to test --- bin/bunka | 24 +++++++++++++++++++++++- lib/bunka.rb | 15 ++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/bin/bunka b/bin/bunka index ad5d3a0..389ceac 100755 --- a/bin/bunka +++ b/bin/bunka @@ -49,7 +49,7 @@ class BunkaCommand < Thor end map '-f' => :file - desc 'file (-f) PATH [QUERY]', 'Test existence of a file or directory' \ + desc 'file (-f) PATH [QUERY]', 'Test existence of a file' \ ' on nodes, scoped on the given query if query is given.' \ ' Query syntax should be the same as `knife search` syntax.' option :sequential, type: :boolean, desc: 'run over nodes sequantially', @@ -70,6 +70,28 @@ class BunkaCommand < Thor options[:'from-file']) end + map '-d' => :dir + desc 'dir (-d) PATH [QUERY]', 'Test existence of a directory' \ + ' on nodes, scoped on the given query if query is given.' \ + ' Query syntax should be the same as `knife search` syntax.' + option :sequential, type: :boolean, desc: 'run over nodes sequantially', + default: false + option :invert, type: :boolean, desc: 'invert matched results', + default: false + option :timeout, type: :numeric, desc: 'timeout interval per ssh connection', + default: 15 + option :threads, type: :numeric, desc: 'number of threads', + default: 15 + option :'print-success', type: :boolean, desc: 'prints output of' \ + 'successful commands', default: false + option :'from-file', type: :string, desc: 'path to file with list of' \ + 'servers', default: nil + def dir(path, query = 'name:*') + Bunka.finddir(path, query, options[:timeout], options[:'print-success'], + options[:invert], options[:sequential], options[:threads], + options[:'from-file']) + end + map '-md5' => :md5sum desc 'md5sum (-md5) PATH MD5SUM [QUERY]', 'Compare a MD5sum with a' \ ' MD5sum of a file on nodes, scoped on the given query if query' \ diff --git a/lib/bunka.rb b/lib/bunka.rb index 945cb4d..1fc259e 100755 --- a/lib/bunka.rb +++ b/lib/bunka.rb @@ -46,7 +46,7 @@ def testserverspec(serverspecfile, query, timeout_interval, def findfile(path, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) - @command = "find . -name '#{path}' | egrep '.*'" + @command = "test -f '#{path}'" @invert = invert @query = query @sequential = sequential @@ -57,6 +57,19 @@ def findfile(path, query, timeout_interval, verbose_success, parallel_exec end + def finddir(path, query, timeout_interval, verbose_success, + invert, sequential, threads, file = nil) + @command = "test -d '#{path}'" + @invert = invert + @query = query + @sequential = sequential + @threads = sequential ? 1 : threads + @timeout_interval = timeout_interval + @verbose_success = verbose_success + @file = file ? File.expand_path(file) : nil + parallel_exec + end + def md5sum(path, checksum, query, timeout_interval, verbose_success, invert, sequential, threads, file = nil) @command = "md5sum -c - <<<'#{checksum} #{path}'"