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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processable

#ensure_each_session, #process_iteration

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.



26
27
28
29
30
31
32
# File 'lib/capistrano/command.rb', line 26

def initialize(command, sessions, options={}, &block)
  @command = command.strip.gsub(/\r?\n/, "\\\n")
  @sessions = sessions
  @options = options
  @callback = block
  @channels = open_channels
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



11
12
13
# File 'lib/capistrano/command.rb', line 11

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/capistrano/command.rb', line 11

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



11
12
13
# File 'lib/capistrano/command.rb', line 11

def sessions
  @sessions
end

Class Method Details

.process(command, sessions, options = {}, &block) ⇒ Object



13
14
15
# File 'lib/capistrano/command.rb', line 13

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capistrano/command.rb', line 37

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?
    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.



56
57
58
59
60
# File 'lib/capistrano/command.rb', line 56

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