Chee 0.2.0 → 0.3.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/README.md CHANGED
@@ -20,9 +20,38 @@ Installation
20
20
 
21
21
  gem install Chee
22
22
 
23
- Usage
23
+ Usage: DSL
24
24
  ------
25
25
 
26
+ You could include the DSL into your own object:
27
+
28
+ require "Chee"
29
+ Class My_SSH
30
+
31
+ include Chee::DSL
32
+
33
+ def ssh cmd
34
+ super cmd.strip
35
+ end
36
+
37
+ end # === Class My_SSH
38
+
39
+ Or you could use Chee directly:
40
+
41
+ require "Chee"
42
+ Chee.server 'my_server'
43
+ Chee.ssh "uptime"
44
+
45
+ `:ssh` returns a `Chee::Result` object:
46
+
47
+ result = Chee.ssh( "uptime" )
48
+
49
+ result.out # ==> output from STDOUT
50
+ result.exit_status
51
+
52
+ Usage: Single Server
53
+ -----
54
+
26
55
  require "Chee"
27
56
 
28
57
  # If you configured server using ~/.ssh/config:
@@ -39,21 +68,43 @@ Usage
39
68
 
40
69
  # Send a command
41
70
  Chee.ssh %^ sudo add-apt-repository ppa:nginx/stable ^
42
- <!-- sudo apt-get install nginx -->
43
- <!-- ^ -->
44
71
 
45
- Or you could include the DSL into your own object:
46
72
 
47
- Class My_SSH
73
+ Usage: Multiple Servers
74
+ ------
48
75
 
49
- include Chee::DSL
76
+ Chee.server 'server_1'
77
+ Chee.server 'server_2', 'my_username'
50
78
 
51
- def ssh cmd
52
- super cmd.strip
53
- end
79
+ Chee.ssh_to_all "uptime"
80
+ # ---> [ Chee::Result, Chee::Result ]
54
81
 
55
- end # === Class My_SSH
82
+ Usage: Multiple Line Command
83
+ ----
84
+
85
+ Single Server, multiple commands:
86
+
87
+ Chee.server 'localhost'
88
+
89
+ Chee.ssh %^
90
+
91
+ sudo add-apt-repository ppa:nginx/stable
92
+ sudo apt-get install nginx
93
+
94
+ ^
95
+ # ---> [ Chee::Result, Chee::Result ]
96
+
97
+ Multiple servers, multiple commands:
56
98
 
99
+ Chee.server 'localhost'
100
+ Chee.server 'my_other_host'
101
+
102
+ Chee.ssh_to_all %^
103
+ echo "a"
104
+ echo "b"
105
+ ^
106
+ # ---> [ Chee::Result, Chee::Result, Chee::Result, Chee::Result ]
107
+
57
108
  Run Tests
58
109
  ---------
59
110
 
data/lib/Chee/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Chee
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/Chee.rb CHANGED
@@ -18,21 +18,41 @@ class Chee
18
18
 
19
19
  module DSL
20
20
 
21
+ def server_array
22
+ @server_array ||= []
23
+ end
24
+
21
25
  def server *args
22
26
  return @server if args.empty?
27
+ server_array << args
28
+ server_array.uniq!
23
29
  @server = args
24
30
  end
25
31
 
26
32
  def print_data &blok
27
- return @print_data if blok.nil?
33
+ @print_data ||= proc { |d| print d; STDOUT.flush; }
34
+ return(@print_data) if blok.nil?
28
35
  @print_data = blok
29
36
  end
30
37
 
38
+ def ssh_to_all command
39
+ @server_array.map { |opts|
40
+ server *opts
41
+ ssh command
42
+ }.flatten
43
+ end
44
+
31
45
  #
32
46
  # Thread technique came from:
33
47
  # http://stackoverflow.com/questions/6942279/ruby-net-ssh-channel-dies
34
48
  #
35
- def ssh command
49
+ def ssh raw_cmd
50
+ command = raw_cmd.strip
51
+ if command["\n"]
52
+ return command.split("\n").map(&:strip).map { |s|
53
+ ssh s
54
+ }
55
+ end
36
56
  stdout = ""
37
57
  stderr = ""
38
58
  t = nil # used to store a Thread
@@ -144,7 +164,7 @@ class Chee
144
164
  result.out stdout
145
165
 
146
166
  if !result.err.empty? || result.exit_status != 0
147
- e = Exit_Error.new("Exit: #{result.exit_status}, STDERR: #{result.err}")
167
+ e = Exit_Error.new("Exit: #{result.exit_status}, COMMAND: #{command}")
148
168
  e.exit_status result.exit_status
149
169
  e.out result.out
150
170
  e.err result.err
@@ -158,9 +178,5 @@ class Chee
158
178
  end # === module DSL
159
179
 
160
180
  extend DSL
161
- print_data { |d|
162
- print d
163
- STDOUT.flush
164
- }
165
181
 
166
182
  end # === class Chee
data/spec/files/abc.rb ADDED
@@ -0,0 +1,29 @@
1
+
2
+ def tmp
3
+ "/tmp/abc.txt"
4
+ end
5
+
6
+ def read
7
+ File.read(tmp).strip
8
+ end
9
+
10
+ def write txt
11
+ File.write tmp, txt
12
+ end
13
+
14
+ if File.file?(tmp)
15
+ case read
16
+ when "a"
17
+ write "b"
18
+ when "b"
19
+ write "c"
20
+ else
21
+ write "a"
22
+ end
23
+ else
24
+ `echo "a" > #{tmp}`
25
+ end
26
+
27
+ puts read
28
+
29
+
data/spec/files/input.rb CHANGED
@@ -18,11 +18,10 @@ end
18
18
  require 'open3'
19
19
  Open3.popen3( "#{cmd} Chee") do |i, o, e, w|
20
20
 
21
- sleep 2
22
21
  print o.gets(' ')
23
22
  print o.gets(' ')
24
23
  i.puts 'a'
25
-
24
+ sleep 1
26
25
  while txt = o.gets do
27
26
  puts txt
28
27
  end
data/spec/main.rb CHANGED
@@ -5,6 +5,17 @@ require 'Bacon_Colored'
5
5
  require 'pry'
6
6
  require 'mocha-on-bacon'
7
7
 
8
+ class My_SSH
9
+
10
+ include Chee::DSL
11
+
12
+ def print_data
13
+ @print_data ||= proc {}
14
+ end
15
+
16
+ end # === My_SSH
17
+
18
+ File.write("/tmp/abc.txt", "")
8
19
 
9
20
  # ======== Include the tests.
10
21
  if ARGV.size > 1 && ARGV[1, ARGV.size - 1].detect { |a| File.exists?(a) }
data/spec/tests/Chee.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- describe ":ssh_exec" do
2
+ describe "Chee :ssh" do
3
3
 
4
4
  before do
5
5
  @localhost = 'localhost'
@@ -41,13 +41,19 @@ describe ":ssh_exec" do
41
41
  .should == %@Input text: a\nYou entered: "a"@
42
42
  end
43
43
 
44
- it 'returns a SSH::Results' do
44
+ it 'returns a Chee::Result' do
45
45
  Chee.ssh("hostname").should.be.is_a Chee::Result
46
46
  end
47
+
48
+ it 'returns an Array of Chee::Result if command has multiple lines' do
49
+ cmd = "ruby #{File.expand_path 'spec/files/abc.rb'}"
50
+ Chee.ssh("#{cmd}\n#{cmd}\n#{cmd}")
51
+ .map(&:class).should == [ Chee::Result, Chee::Result, Chee::Result ]
52
+ end
47
53
 
48
54
  it "strips returned data" do
49
- target = `uptime`.strip.gsub(%r!\d+!, '[0-9]{1,2}')
50
- Chee.ssh("uptime").out.should.match %r!#{target}!
55
+ Chee.ssh("cat ~/.bashrc").out.gsub("\r", '')
56
+ .should == `cat ~/.bashrc`.strip
51
57
  end
52
58
 
53
59
  it 'raises Net::SSH::AuthenticationFailed if login/password are incorrect' do
@@ -68,3 +74,46 @@ describe ":ssh_exec" do
68
74
 
69
75
  end # === describe :ssh_exec
70
76
 
77
+ describe "Chee :server" do
78
+
79
+ before { @m = My_SSH.new }
80
+
81
+ it "adds server to server list" do
82
+ @m.server 'local'
83
+ @m.server_array.should.include ['local']
84
+ end
85
+
86
+ it "adds server to server list only once" do
87
+ @m.server 'local'
88
+ @m.server 'local'
89
+ @m.server_array.should == [ ['local'] ]
90
+ end
91
+
92
+ end # === Chee :server
93
+
94
+ describe "Chee :ssh_to_all" do
95
+
96
+ before {
97
+ @m = My_SSH.new
98
+ @m.server "localhost"
99
+ @m.server "localhost", nil
100
+ @m.server "localhost", `whoami`.strip
101
+ }
102
+
103
+ it "sends commands to all servers in :server_array" do
104
+ list = @m.ssh_to_all "ruby #{File.expand_path 'spec/files/abc.rb'}"
105
+ list.map { |o| o.out.strip }.should == %w{ a b c }
106
+ end
107
+
108
+ it "returns an Array with all elements Chee::Result" do
109
+ list = @m.ssh_to_all "uptime"
110
+ list.map(&:class).should == [ Chee::Result, Chee::Result, Chee::Result]
111
+ end
112
+
113
+ it "returns a flatten Array if command had multiple lines" do
114
+ list = @m.ssh_to_all "uptime\nuptime"
115
+ list.map(&:class).should == [ Chee::Result, Chee::Result ] * 3
116
+ end
117
+
118
+ end # === Chee :ssh_to_all
119
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Chee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -172,6 +172,7 @@ files:
172
172
  - Rakefile
173
173
  - lib/Chee.rb
174
174
  - lib/Chee/version.rb
175
+ - spec/files/abc.rb
175
176
  - spec/files/errors_with_exit_0.rb
176
177
  - spec/files/exit_with_2.rb
177
178
  - spec/files/input.rb