Class: Capistrano::Command

Inherits:
Object
  • Object
show all
Includes:
Processable
Defined in:
lib/capistrano/command.rb

Overview

This class encapsulates a single command to be executed on a set of remote machines, in parallel.

Defined Under Namespace

Classes: Tree

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processable

#ensure_each_session, #process_iteration

Constructor Details

#initialize(tree, 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.



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/capistrano/command.rb', line 152

def initialize(tree, sessions, options={}, &block)
  if String === tree
    tree = Tree.new(nil) { |t| t.else(tree, &block) }
  elsif block
    raise ArgumentError, "block given with tree argument"
  end

  @tree = tree
  @sessions = sessions
  @options = options
  @channels = open_channels
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



137
138
139
# File 'lib/capistrano/command.rb', line 137

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



137
138
139
# File 'lib/capistrano/command.rb', line 137

def sessions
  @sessions
end

#treeObject (readonly)

Returns the value of attribute tree.



137
138
139
# File 'lib/capistrano/command.rb', line 137

def tree
  @tree
end

Class Method Details

.process(tree, sessions, options = {}) ⇒ Object



139
140
141
# File 'lib/capistrano/command.rb', line 139

def self.process(tree, sessions, options={})
  new(tree, sessions, options).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.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/capistrano/command.rb', line 168

def process!
  elapsed = Benchmark.realtime do
    loop do
      break unless process_iteration { @channels.any? { |ch| !ch[:closed] } }
    end
  end

  logger.trace "command finished in #{(elapsed * 1000).round}ms" if logger

  if (failed = @channels.select { |ch| ch[:status] != 0 }).any?
    commands = failed.inject({}) { |map, ch| (map[ch[:command]] ||= []) << ch[:server]; map }
    message = commands.map { |command, list| "#{command.inspect} on #{list.join(',')}" }.join("; ")
    error = CommandError.new("failed: #{message}")
    error.hosts = commands.values.flatten
    raise error
  end

  self
end

#stop!Object

Force the command to stop processing, by closing all open channels associated with this command.



190
191
192
193
194
# File 'lib/capistrano/command.rb', line 190

def stop!
  @channels.each do |ch|
    ch.close unless ch[:closed]
  end
end