rdeshpande-crow 0.1.0

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.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Rohan Deshpande
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = crow
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Rohan Deshpande. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "crow"
8
+ gem.summary = %Q{helper for ssh+ruby scripts}
9
+ gem.email = "rohan.deshpande@gmail.com"
10
+ gem.homepage = "http://github.com/rdeshpande/crow"
11
+ gem.authors = ["Rohan Deshpande"]
12
+
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "crow #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 1
3
+ :patch: 0
4
+ :major: 0
data/lib/crow.rb ADDED
@@ -0,0 +1,117 @@
1
+ require 'rubygems'
2
+ require 'net/ssh'
3
+ require 'net/ssh/gateway'
4
+ require 'singleton'
5
+
6
+ class Crow
7
+
8
+ STRFTIME_STRING = "%F %T"
9
+
10
+ attr_accessor :commands, :current_host, :gateway, :logfile, :threads, :user
11
+
12
+ include Singleton
13
+
14
+ def initialize(opts = {})
15
+ @commands = []
16
+ @threads = []
17
+ @user = Crow.get_user
18
+ @gateway = opts[:gateway]
19
+ Crow.set_logfile(opts[:logfile])
20
+ end
21
+
22
+ def Crow.get_user
23
+ return ENV['USER'].strip
24
+ end
25
+
26
+ # Exception handling must occur here
27
+ #
28
+ def Crow.go!
29
+ begin
30
+ instance.threads.each do |thread|
31
+ thread.join
32
+ end
33
+ rescue SocketError => e
34
+ puts "ERROR: attempting to connect to #{instance.current_host}:"
35
+ puts "#{e.class}: #{e.to_s}"
36
+ puts e.backtrace
37
+ exit
38
+ end
39
+ end
40
+
41
+ def Crow.log(host, output, direction, logfile)
42
+ msg = "#{timestamp} (#{direction} :: #{host}) #{output}"
43
+ puts msg
44
+ if logfile
45
+ logfile.write("#{msg.strip}\n")
46
+ end
47
+ end
48
+
49
+ def Crow.log_receive(host, data, logfile = nil)
50
+ Crow.log(host, data, 'in', logfile)
51
+ end
52
+
53
+ def Crow.log_send(host, command, logfile = nil)
54
+ Crow.log(host, command, 'out', logfile)
55
+ end
56
+
57
+ def Crow.run(command)
58
+ instance.commands << command
59
+ end
60
+
61
+ def Crow.on_hosts(hosts, &block)
62
+ instance.commands = []
63
+ yield
64
+ if hosts == 'localhost'
65
+ instance.threads << Thread.new { run_local(instance.commands) }
66
+ else
67
+ hosts.to_a.each do |host|
68
+ instance.threads << Thread.new { ssh(host, instance.commands) }
69
+ end
70
+ end
71
+ go!
72
+ if instance.logfile && File.pipe?(instance.logfile)
73
+ instance.logfile.close
74
+ end
75
+ end
76
+
77
+ def Crow.run_local(commands)
78
+ commands.each do |cmd|
79
+ Crow.log_send('localhost', cmd, instance.logfile)
80
+ IO.popen(cmd) do |io|
81
+ Crow.log_receive('localhost', io.read, instance.logfile)
82
+ end
83
+ end
84
+ end
85
+
86
+ def Crow.set_gateway(host)
87
+ instance.gateway = host
88
+ end
89
+
90
+ def Crow.set_logfile(filename)
91
+ if filename
92
+ instance.logfile = File.new(filename, 'w+')
93
+ return true
94
+ end
95
+ return false
96
+ end
97
+
98
+ def Crow.ssh(host, commands)
99
+ instance.current_host = host
100
+ ssh = nil
101
+ if instance.gateway.nil? || (host == instance.gateway)
102
+ ssh = Net::SSH.start(host, instance.user)
103
+ else
104
+ gateway = Net::SSH::Gateway.new(instance.gateway, instance.user)
105
+ ssh = gateway.ssh(host, instance.user)
106
+ end
107
+ commands.each do |cmd|
108
+ ssh.exec!(cmd, host, instance.logfile)
109
+ end
110
+ ssh.close
111
+ end
112
+
113
+ def Crow.timestamp
114
+ return "[#{Time.now.strftime(STRFTIME_STRING)}]"
115
+ end
116
+
117
+ end
data/lib/ssh.rb ADDED
@@ -0,0 +1,47 @@
1
+ # Override the default Net::SSH::Connection::Sesssion#exec to log to Hustler
2
+ #
3
+ module Net
4
+ module SSH
5
+ module Connection
6
+ class Session
7
+
8
+ attr_accessor :current_host
9
+
10
+ def exec(command, host, logfile = nil, &block)
11
+ Crow.log_send(host, command, logfile)
12
+ open_channel do |channel|
13
+ channel.exec(command) do |ch, success|
14
+ raise "could not execute command: #{command.inspect}" unless success
15
+
16
+ channel.on_data do |ch2, data|
17
+ Crow.log_receive(host, data, logfile)
18
+ end
19
+
20
+ channel.on_extended_data do |ch2, type, data|
21
+ $stderr.print(data)
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ # Same as #exec, except this will block until the command finishes. Also,
28
+ # if a block is not given, this will return all output (stdout and stderr)
29
+ # as a single string.
30
+ #
31
+ # matches = ssh.exec!("grep something /some/files")
32
+ def exec!(command, host, logfile = nil, &block)
33
+ block ||= Proc.new do |ch, type, data|
34
+ ch[:result] ||= ""
35
+ ch[:result] << data
36
+ end
37
+
38
+ channel = exec(command, host, logfile, &block)
39
+ channel.wait
40
+
41
+ return channel[:result]
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
data/test/host_test.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class HostTest < Test::Unit::TestCase
4
+
5
+ def test_should_set_gateway
6
+ Crow.set_gateway('qaweb1')
7
+ assert_equal(Crow.instance.gateway, 'qaweb1')
8
+ end
9
+
10
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'crow')
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rdeshpande-crow
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rohan Deshpande
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-25 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: rohan.deshpande@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - LICENSE
27
+ - README.rdoc
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/crow.rb
31
+ - lib/ssh.rb
32
+ - test/host_test.rb
33
+ - test/test_helper.rb
34
+ has_rdoc: true
35
+ homepage: http://github.com/rdeshpande/crow
36
+ post_install_message:
37
+ rdoc_options:
38
+ - --charset=UTF-8
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ requirements: []
54
+
55
+ rubyforge_project:
56
+ rubygems_version: 1.2.0
57
+ signing_key:
58
+ specification_version: 2
59
+ summary: helper for ssh+ruby scripts
60
+ test_files:
61
+ - test/host_test.rb
62
+ - test/test_helper.rb