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.


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

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


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

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions


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

def sessions
  @sessions
end

#treeObject (readonly)

Returns the value of attribute tree


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

def tree
  @tree
end

Class Method Details

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


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

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.


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

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

  logger.trace "command finished" 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.


181
182
183
184
185
# File 'lib/capistrano/command.rb', line 181

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