Class: Minestrone::Command

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

Overview

This class encapsulates a single command to be executed on a remote machine.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Processable

#ensure_session, #process_iteration

Constructor Details

#initialize(command, session, options = {}, &block) ⇒ Command

Instantiates a new command object. The command must be a string containing the command to execute. session is a Net::SSH session instance, and options must be a hash containing any of the following keys:

  • logger: (optional), a Minestrone::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.



31
32
33
34
35
36
# File 'lib/minestrone/command.rb', line 31

def initialize(command, session, options = {}, &block)
  @command = command.strip.gsub(/\r?\n/, "\\\n")
  @session = session
  @options = options
  @callback = block || Minestrone::Configuration.default_io_proc
end

Instance Attribute Details

#callbackObject (readonly)

Returns the value of attribute callback.



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

def callback
  @callback
end

#channelObject (readonly)

Returns the value of attribute channel.



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

def channel
  @channel
end

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#sessionObject (readonly)

Returns the value of attribute session.



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

def session
  @session
end

Class Method Details

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



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

def self.process(command, session, options = {}, &block)
  new(command, session, options, &block).process!
end

Instance Method Details

#process!Object

Processes the command. If the command fails (non-zero return code), this will raise a Minestrone::CommandError.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/minestrone/command.rb', line 41

def process!
  elapsed = Benchmark.realtime do
    open_channel(session)

    loop do
      break unless process_iteration { !channel[:closed] }
    end
  end

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

  if channel[:status] != 0
    message = "#{channel[:command].inspect} on #{channel[:server]}"
    error = CommandError.new("failed: #{message}")
    error.host = channel[:server]
    raise error
  end

  self
end

#stop!Object

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



65
66
67
# File 'lib/minestrone/command.rb', line 65

def stop!
  channel.close if channel && !channel[:closed]
end