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.



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/capistrano/command.rb', line 146

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.



131
132
133
# File 'lib/capistrano/command.rb', line 131

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



131
132
133
# File 'lib/capistrano/command.rb', line 131

def sessions
  @sessions
end

#treeObject (readonly)

Returns the value of attribute tree.



131
132
133
# File 'lib/capistrano/command.rb', line 131

def tree
  @tree
end

Class Method Details

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



133
134
135
# File 'lib/capistrano/command.rb', line 133

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.



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/capistrano/command.rb', line 162

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.



184
185
186
187
188
# File 'lib/capistrano/command.rb', line 184

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