Class: SwitchTower::Command
- Inherits:
-
Object
- Object
- SwitchTower::Command
- Defined in:
- lib/switchtower/command.rb
Overview
This class encapsulates a single command to be executed on a set of remote machines, in parallel.
Instance Attribute Summary collapse
-
#actor ⇒ Object
readonly
Returns the value of attribute actor.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#servers ⇒ Object
readonly
Returns the value of attribute servers.
Instance Method Summary collapse
-
#initialize(servers, command, callback, options, actor) ⇒ Command
constructor
:nodoc:.
-
#logger ⇒ Object
:nodoc:.
-
#process! ⇒ Object
Processes the command in parallel on all specified hosts.
Constructor Details
#initialize(servers, command, callback, options, actor) ⇒ Command
:nodoc:
8 9 10 11 12 13 14 15 |
# File 'lib/switchtower/command.rb', line 8 def initialize(servers, command, callback, , actor) #:nodoc: @servers = servers @command = command.strip.gsub(/\r?\n/, "\\\n") @callback = callback @options = @actor = actor @channels = open_channels end |
Instance Attribute Details
#actor ⇒ Object (readonly)
Returns the value of attribute actor.
6 7 8 |
# File 'lib/switchtower/command.rb', line 6 def actor @actor end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
6 7 8 |
# File 'lib/switchtower/command.rb', line 6 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/switchtower/command.rb', line 6 def @options end |
#servers ⇒ Object (readonly)
Returns the value of attribute servers.
6 7 8 |
# File 'lib/switchtower/command.rb', line 6 def servers @servers end |
Instance Method Details
#logger ⇒ Object
:nodoc:
17 18 19 |
# File 'lib/switchtower/command.rb', line 17 def logger #:nodoc: actor.logger end |
#process! ⇒ Object
Processes the command in parallel on all specified hosts. If the command fails (non-zero return code) on any of the hosts, this will raise a RuntimeError.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/switchtower/command.rb', line 24 def process! since = Time.now loop do active = 0 @channels.each do |ch| next if ch[:closed] active += 1 ch.connection.process(true) end break if active == 0 if Time.now - since >= 1 since = Time.now @channels.each { |ch| ch.connection.ping! } end sleep 0.01 # a brief respite, to keep the CPU from going crazy end logger.trace "command finished" if failed = @channels.detect { |ch| ch[:status] != 0 } raise "command #{@command.inspect} failed on #{failed[:host]}" end self end |