Class: Minestrone::Command
- Inherits:
-
Object
- Object
- Minestrone::Command
- 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
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command, session, options = {}, &block) ⇒ Command
constructor
Instantiates a new command object.
-
#process! ⇒ Object
Processes the command.
-
#stop! ⇒ Object
Force the command to stop processing, by closing the open channel associated with this command.
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, = {}, &block) @command = command.strip.gsub(/\r?\n/, "\\\n") @session = session @options = @callback = block || Minestrone::Configuration.default_io_proc end |
Instance Attribute Details
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
16 17 18 |
# File 'lib/minestrone/command.rb', line 16 def callback @callback end |
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
16 17 18 |
# File 'lib/minestrone/command.rb', line 16 def channel @channel end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
16 17 18 |
# File 'lib/minestrone/command.rb', line 16 def command @command end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/minestrone/command.rb', line 16 def @options end |
#session ⇒ Object (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, = {}, &block) new(command, session, , &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 = "#{channel[:command].inspect} on #{channel[:server]}" error = CommandError.new("failed: #{}") 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 |