gofer 0.3.1 → 0.4.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZWNlZjlhNDIyYjAyMDAxMWMwNTRiMmFlN2ZjMjk4NGYzYmU4NDRlMQ==
4
+ NjZkNzBmMjZlMWI5ODk4MDE0ZDNmNTFlYzAxNjU4NTFlNzgyMDQ4NQ==
5
5
  data.tar.gz: !binary |-
6
- YzA2ZmJkNGUzY2U5NmQwN2I4YTRmODIwMmY5OTJiOTk1ZmZjNjdmZQ==
6
+ ZjMwYjNmMTAwMWQ2NDJlOWUzZTE1MTdmODBkNjg2ZWVlMTQ2OTZiOA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZTBmYWFhOGEwODAzMzZhNGVhNWJmOTMyNDQ0N2FjMDc5ZjkwZjI3OWU2YmQx
10
- ZjZkYTZlZmQ3MWYyMGE0MWFlM2FkODFjNTI1ZjE0MGMzYjY3MTEwMzc5ZGFk
11
- OGQyYTZkZmE0OWQ3NjgzN2ExYmIxMTBkZjliZDE2YzM2ZWNmMTQ=
9
+ N2MwNTRkYzQzZDM4MGIyN2Q0ZTJmNmY3MDY2NWFhZTgwNTg2ZmFhOWU1OGRk
10
+ NWE3ZGRjZWJkMjM2OGFkOTA0Mjk4NTIyMDA4YTcwYjlhYWM0NmE1OWU2Mzdm
11
+ YzNlZjU1MzYxY2ExYWJkOGY5NTVjYmUxZGMzOGVlNzIzMzMwNzQ=
12
12
  data.tar.gz: !binary |-
13
- MzU4MGRhMmM1ZWNlNWE4MWFiNzZkMmViZjk1MTNiYjJmMDI1ZmZjODM4NGM3
14
- NGM3YjNhOTUzYmEyYjEwYjRiZGYyN2E2ZWRmM2E0YWEyMzdiOTQ1NTYyMWUz
15
- M2Q4M2U4M2Q0YmVlYmZhYTVmMDJkMzA1MGUyYTMyNzM3YzE5NjM=
13
+ ZTYyNDAxYTU2MTdjNTQ1YWRjMjNmODFkYWI4MTA4YTVkYmQxZDlmMWIxMTE0
14
+ NTU2NmQ1MTJiOThkMTBkMjNhNGZhYzBjNTIwY2U2M2UzMGI0ZmU1MjVjYTAw
15
+ MDA1NmUxYWZlNzcxY2U5M2M2MTZmMzFmNGQzNjM1Zjc4OWI1YWE=
@@ -1,5 +1,10 @@
1
1
  # Revision History
2
2
 
3
+ ### v0.4.0
4
+
5
+ * Rework `Gofer::Cluster` to be a direct proxy, rather than requiring a block
6
+ * Add all `Gofer::Host` methods to the `Gofer::Cluster` proxy (@rich0h)
7
+
3
8
  ### v0.3.1
4
9
 
5
10
  * Prefix stderr/stdout per host with `:output_prefix`
data/README.md CHANGED
@@ -90,13 +90,20 @@ cluster = Gopher::Cluster.new
90
90
  cluster << Gofer::Host.new('my.host.com', 'ubuntu', :keys => ['key.pem'], :output_prefix => " my")
91
91
  cluster << Gofer::Host.new('other.host.com', 'ubuntu', :keys => ['key.pem'], :output_prefix => "other")
92
92
 
93
- cluster.run do |c|
94
- c.run("hostname") # This will run on both hosts at once
95
- end
93
+ # Run on all the hosts at once
94
+ cluster.run "hostname"
96
95
 
97
- cluster.run(:max_concurrency => 1) do |c|
98
- c.run("sudo /etc/init.d/apache2 restart") # This will run on one machine at a time
99
- end
96
+ # Run on only one host at a time
97
+ cluster.max_concurrency = 1
98
+ cluster.run("sudo /etc/init.d/apache2 restart")
99
+
100
+ # Run a command on only one host
101
+ host = cluster.shuffle.first
102
+ host.run("rake migrations")
103
+
104
+ # Inspect the results from each host
105
+ results = cluster.run "echo hostname"
106
+ puts results.values.join(", ") # will print "my.host.com, other.host.com"
100
107
  ```
101
108
 
102
109
  ## Testing
@@ -2,72 +2,69 @@ require 'thread'
2
2
 
3
3
  module Gofer
4
4
  class Cluster
5
+
5
6
  attr_reader :hosts
6
7
  attr_accessor :max_concurrency
7
- def initialize(parties=[])
8
+
9
+ def initialize(parties=[], opts={})
8
10
  @hosts = []
9
- @max_concurrency = nil
11
+ @max_concurrency = opts.delete(:max_concurrency)
10
12
 
11
- parties.each do |i|
12
- self << i
13
- end
13
+ parties.each { |i| self << i }
14
+ end
15
+
16
+ def concurrency
17
+ max_concurrency.nil? ? hosts.length : [max_concurrency, hosts.length].min
14
18
  end
15
19
 
16
20
  def <<(other)
17
21
  case other
18
22
  when Cluster
19
- other.hosts.each do |host|
20
- @hosts << host
21
- end
23
+ other.hosts.each { |host| self << host }
22
24
  when Host
23
25
  @hosts << other
24
26
  end
25
27
  end
26
28
 
27
- def run(opts={}, &block)
28
- concurrency = opts[:max_concurrency] || max_concurrency || hosts.length
29
- block.call(ClusterCommandRunner.new(hosts, concurrency))
29
+ %w{run exist? read directory? ls upload read write}.each do |host_method|
30
+ define_method host_method do |*args|
31
+ threaded(host_method, *args)
32
+ end
30
33
  end
31
34
 
32
- class ClusterCommandRunner
33
- def initialize(hosts, concurrency)
34
- @concurrency = concurrency
35
- @hosts = hosts
36
- end
35
+ private
37
36
 
38
- # Spawn +concurrency+ worker threads, each of which pops work off the
39
- # +_in+ queue, and writes values to the +_out+ queue for syncronisation.
40
- def run(cmd, opts={})
41
- _in = run_queue
42
- length = run_queue.length
43
- _out = Queue.new
44
- results = {}
45
- (0...@concurrency).map do
46
- Thread.new do
47
- loop do
48
- host = _in.pop(false) rescue Thread.exit
37
+ # Spawn +concurrency+ worker threads, each of which pops work off the
38
+ # +_in+ queue, and writes values to the +_out+ queue for syncronisation.
39
+ def threaded(meth, *args)
40
+ _in = run_queue
41
+ length = run_queue.length
42
+ _out = Queue.new
43
+ results = {}
44
+ (0...concurrency).map do
45
+ Thread.new do
46
+ loop do
47
+ host = _in.pop(false) rescue Thread.exit
49
48
 
50
- results[host] = host.run(cmd, opts)
51
- _out << true
52
- end
49
+ results[host] = host.send(meth, *args)
50
+ _out << true
53
51
  end
54
52
  end
53
+ end
55
54
 
56
- length.times do
57
- _out.pop
58
- end
59
-
60
- return results
55
+ length.times do
56
+ _out.pop
61
57
  end
62
58
 
63
- def run_queue
64
- Queue.new.tap do |q|
65
- @hosts.each do |h|
66
- q << h
67
- end
59
+ results
60
+ end
61
+
62
+ def run_queue
63
+ Queue.new.tap do |q|
64
+ @hosts.each do |h|
65
+ q << h
68
66
  end
69
67
  end
70
68
  end
71
-
72
69
  end
73
70
  end
@@ -1,3 +1,3 @@
1
1
  module Gofer # :nodoc:
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -251,9 +251,7 @@ describe Gofer do
251
251
  end
252
252
 
253
253
  it "should run commands in parallel" do
254
- results = @cluster.run do |c|
255
- c.run "ruby -e 'puts Time.now.to_f; sleep 0.1; puts Time.now.to_f'"
256
- end
254
+ results = @cluster.run("ruby -e 'puts Time.now.to_f; sleep 0.1; puts Time.now.to_f'")
257
255
 
258
256
  res1 = results[@host1].stdout.lines.to_a
259
257
  res2 = results[@host2].stdout.lines.to_a
@@ -262,9 +260,8 @@ describe Gofer do
262
260
  end
263
261
 
264
262
  it "should respect max_concurrency" do
265
- results = @cluster.run(:max_concurrency => 1) do |c|
266
- c.run "ruby -e 'puts Time.now.to_f; sleep 0.1; puts Time.now.to_f'"
267
- end
263
+ @cluster.max_concurrency = 1
264
+ results = @cluster.run("ruby -e 'puts Time.now.to_f; sleep 0.1; puts Time.now.to_f'")
268
265
 
269
266
  res1 = results[@host1].stdout.lines.to_a
270
267
  res2 = results[@host2].stdout.lines.to_a
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gofer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Pearson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-26 00:00:00.000000000 Z
11
+ date: 2013-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: net-ssh