Class: Capistrano::Command
- Inherits:
-
Object
- Object
- Capistrano::Command
- Defined in:
- lib/capistrano/command.rb
Overview
This class encapsulates a single command to be executed on a set of remote machines, in parallel.
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#sessions ⇒ Object
readonly
Returns the value of attribute sessions.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, sessions, options = {}, &block) ⇒ Command
constructor
Instantiates a new command object.
-
#process! ⇒ Object
Processes the command in parallel on all specified hosts.
-
#stop! ⇒ Object
Force the command to stop processing, by closing all open channels associated with this command.
Constructor Details
#initialize(command, sessions, options = {}, &block) ⇒ Command
Instantiates a new command object. The command
must be a string containing the command to execute. sessions
is an array of Net::SSH session instances, and options
must be a hash containing any of the following keys:
-
logger
: (optional), a Capistrano::Logger instance -
data
: (optional), a string to be sent to the command via it’s stdin -
env
: (optional), a string or hash to be interpreted as environment variables that should be defined for this command invocation.
23 24 25 26 27 28 29 |
# File 'lib/capistrano/command.rb', line 23 def initialize(command, sessions, ={}, &block) @command = command.strip.gsub(/\r?\n/, "\\\n") @sessions = sessions @options = @callback = block @channels = open_channels end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
8 9 10 |
# File 'lib/capistrano/command.rb', line 8 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/capistrano/command.rb', line 8 def @options end |
#sessions ⇒ Object (readonly)
Returns the value of attribute sessions.
8 9 10 |
# File 'lib/capistrano/command.rb', line 8 def sessions @sessions end |
Class Method Details
.process(command, sessions, options = {}, &block) ⇒ Object
10 11 12 |
# File 'lib/capistrano/command.rb', line 10 def self.process(command, sessions, ={}, &block) new(command, sessions, , &block).process! end |
Instance Method Details
#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 Capistrano::CommandError.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/capistrano/command.rb', line 34 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 logger if (failed = @channels.select { |ch| ch[:status] != 0 }).any? hosts = failed.map { |ch| ch[:server] } error = CommandError.new("command #{command.inspect} failed on #{hosts.join(',')}") error.hosts = hosts raise error end self end |
#stop! ⇒ Object
Force the command to stop processing, by closing all open channels associated with this command.
66 67 68 69 70 |
# File 'lib/capistrano/command.rb', line 66 def stop! @channels.each do |ch| ch.close unless ch[:closed] end end |