Class: Capissh::Command

Inherits:
Object
  • Object
show all
Includes:
Processable
Defined in:
lib/capissh/command.rb,
lib/capissh/command/tree.rb

Overview

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

Defined Under Namespace

Classes: Tree

Class Attribute Summary collapse

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 = {}) ⇒ 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:

  • shell: (optional), the shell (eg. ‘bash’) or false. Default: ‘sh’

  • logger: (optional), a Capissh::Logger instance

  • data: (optional), a string to be sent to the command via it’s stdin

  • eof: (optional), close stdin after sending data

  • env: (optional), a string or hash to be interpreted as environment variables that should be defined for this command invocation.

  • pty: (optional), execute the command in a pty



50
51
52
53
54
55
# File 'lib/capissh/command.rb', line 50

def initialize(tree, sessions, options={})
  @tree = tree
  @options = options
  @sessions = sessions
  @channels = open_channels
end

Class Attribute Details

.default_io_procObject

Returns the value of attribute default_io_proc.



16
17
18
# File 'lib/capissh/command.rb', line 16

def default_io_proc
  @default_io_proc
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



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

def sessions
  @sessions
end

#treeObject (readonly)

Returns the value of attribute tree.



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

def tree
  @tree
end

Class Method Details

.process_string(string, sessions, options = {}, &block) ⇒ Object

Convenience method to process a command given as a string rather than a Command::Tree.



25
26
27
28
# File 'lib/capissh/command.rb', line 25

def process_string(string, sessions, options={}, &block)
  tree = Tree.twig(nil, string, &block)
  process_tree(tree, sessions, options)
end

.process_tree(tree, sessions, options = {}) ⇒ Object Also known as: process



18
19
20
# File 'lib/capissh/command.rb', line 18

def process_tree(tree, sessions, options={})
  new(tree, sessions, options).process!
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/capissh/command.rb', line 85

def active?
  @channels.any? { |ch| !ch[:closed] }
end

#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 Capissh::CommandError.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/capissh/command.rb', line 60

def process!
  elapsed = Benchmark.realtime do
    loop do
      break unless process_iteration { active? }
    end
  end

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

  failed = @channels.select { |ch| ch[:status] != 0 }
  if failed.any?
    commands = failed.inject({}) do |map, ch|
      map[ch[:command]] ||= []
      map[ch[:command]] << ch[:server]
      map
    end
    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.



91
92
93
94
95
# File 'lib/capissh/command.rb', line 91

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