santoku 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/santoku +6 -0
  3. data/lib/santoku.rb +108 -0
  4. metadata +89 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0738e1ba13ad70766617662b5e62054064e58183
4
+ data.tar.gz: ade98e3db326a6079ada9faf97ec0b14901882d6
5
+ SHA512:
6
+ metadata.gz: d39af2df669f155aace2331d56080fe847296d868be5511114b30fcd55ee9b4a9ce9ca689fd907f9d9172d75eba44ce7905d3426a38af71f7ab16f61af4ffeda
7
+ data.tar.gz: b9e80ecff70535e561083d2e777ea269c8e077a5244da9865bfd8ed784adac996bdf2c3bbd9259cbecf2f5cfbcf99e98cb817a61fb1a669325999052dc5a73e2
data/bin/santoku ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ require './lib/santoku'
3
+
4
+ command = ARGV[0]
5
+ query = ARGV[1]
6
+ Santoku.run(command, query)
data/lib/santoku.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'colorize'
2
+ require 'net/ssh'
3
+ require 'peach'
4
+ require 'ridley'
5
+ require 'yaml'
6
+
7
+ class Santoku
8
+ def self.run(command='uptime', query)
9
+ output_stream = Array.new
10
+ failed_output_stream = Array.new
11
+ timeout_output_stream = Array.new
12
+
13
+ ridley.search(:node, query).peach(5) do |node|
14
+ begin
15
+ timeout 10 do
16
+ Net::SSH.start(node.chef_id, 'root', paranoid: false, forward_agent: true) do |ssh|
17
+ output = ssh_exec!(ssh, command)
18
+ if output[2] != 0
19
+ failed_output_stream.push "#{node.chef_id} error #{output[2]}: #{output[1]}"
20
+ print 'F'.red
21
+ else
22
+ output_stream.push output[0]
23
+ print '.'.green
24
+ end
25
+ end
26
+ end
27
+
28
+ rescue TimeoutError
29
+ timeout_output_stream.push "#{node.chef_id}: connection timed out"
30
+ print '*'.yellow
31
+ rescue SocketError
32
+ timeout_output_stream.push "#{node.chef_id}: node does not resolve"
33
+ print '*'.yellow
34
+ rescue Errno::EHOSTUNREACH
35
+ timeout_output_stream.push "#{node.chef_id}: no route to host"
36
+ print '*'.yellow
37
+ rescue Exception => e
38
+ failed_output_stream.push "#{node.chef_id}: #{e.inspect}"
39
+ print 'F'.red
40
+ end
41
+ end
42
+
43
+ print "\n"
44
+
45
+ timeout_output_stream.each do |output|
46
+ puts output.yellow
47
+ end
48
+
49
+ failed_output_stream.each do |output|
50
+ puts output.red
51
+ end
52
+
53
+ puts "\n---------------------------------------\n"
54
+
55
+ puts "#{'Success'.green}: #{output_stream.count}"
56
+ puts "#{'Timed out or does not resolve'.yellow}: #{timeout_output_stream.count}"
57
+ puts "#{'Failed'.red}: #{failed_output_stream.count}"
58
+ end
59
+
60
+ private
61
+ def self.knife_config
62
+ if ::File.exist?(File.expand_path("../knife.rb", __FILE__))
63
+ Ridley::Chef::Config.from_file(File.expand_path("../knife.rb", __FILE__))
64
+ elsif ::File.exist?("#{ENV['HOME']}/.chef/knife.rb")
65
+ Ridley::Chef::Config.from_file("#{ENV['HOME']}/.chef/knife.rb")
66
+ else
67
+ raise 'Could not find knife.rb in current directory or home '
68
+ end
69
+ end
70
+
71
+ def self.ridley
72
+ @ridley ||= Ridley.new(
73
+ server_url: knife_config.chef_server_url,
74
+ client_name: knife_config.node_name,
75
+ client_key: knife_config.client_key
76
+ )
77
+ end
78
+ end
79
+
80
+ def ssh_exec!(ssh, command)
81
+ # I am not awesome enough to have made this method myself
82
+ # I've just modified it a bit
83
+ # Originally submitted by 'flitzwald' over here: http://stackoverflow.com/a/3386375
84
+ stdout_data = ""
85
+ stderr_data = ""
86
+ exit_code = nil
87
+
88
+ ssh.open_channel do |channel|
89
+ channel.exec(command) do |ch, success|
90
+ unless success
91
+ abort "FAILED: couldn't execute command (ssh.channel.exec)"
92
+ end
93
+ channel.on_data do |ch,data|
94
+ stdout_data+=data
95
+ end
96
+
97
+ channel.on_extended_data do |ch,type,data|
98
+ stderr_data+=data
99
+ end
100
+
101
+ channel.on_request("exit-status") do |ch,data|
102
+ exit_code = data.read_long
103
+ end
104
+ end
105
+ end
106
+ ssh.loop
107
+ [stdout_data, stderr_data, exit_code]
108
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: santoku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven De Coeyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: colorize
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: peach
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ridley
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A gem to perform command over parrallel ssh connections on multiple chef
56
+ serverspec. Output is rspec-like.
57
+ email: steven@banteng.be
58
+ executables:
59
+ - santoku
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - lib/santoku.rb
64
+ - bin/santoku
65
+ homepage: https://github.com/zhann/santoku
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 2.0.3
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Parrallel ssh commands over chef servers with rspec-like output
89
+ test_files: []