Module: Aruba::Api::Commands

Included in:
Aruba::Api
Defined in:
lib/aruba/api/commands.rb

Overview

Command module

Instance Method Summary collapse

Instance Method Details

#all_commandsArray

Return all commands

Returns:

  • (Array)

    List of commands



47
48
49
# File 'lib/aruba/api/commands.rb', line 47

def all_commands
  aruba.command_monitor.registered_commands
end

#all_outputString

Get stderr and stdout of all processes

Returns:

  • (String)

    The stderr and stdout of all processes which have run before



115
116
117
# File 'lib/aruba/api/commands.rb', line 115

def all_output
  aruba.command_monitor.all_output
end

#all_stderrString

Get stderr of all processes

Returns:

  • (String)

    The stderr of all processes which have run before



107
108
109
# File 'lib/aruba/api/commands.rb', line 107

def all_stderr
  aruba.command_monitor.all_stderr
end

#all_stdoutString

Get stdout of all processes

Returns:

  • (String)

    The stdout of all processes which have run before



99
100
101
# File 'lib/aruba/api/commands.rb', line 99

def all_stdout
  aruba.command_monitor.all_stdout
end

#close_inputObject

Close stdin



217
218
219
# File 'lib/aruba/api/commands.rb', line 217

def close_input
  last_command_started.close_io(:stdin)
end

#find_command(commandline) ⇒ Object

Find a started command

Parameters:

  • commandline (String, Command)

    The commandline



123
124
125
# File 'lib/aruba/api/commands.rb', line 123

def find_command(commandline)
  aruba.command_monitor.find(commandline)
end

#last_command_startedObject

Last command started



52
53
54
# File 'lib/aruba/api/commands.rb', line 52

def last_command_started
  aruba.command_monitor.last_command_started
end

#last_command_stoppedObject

Last command stopped



57
58
59
# File 'lib/aruba/api/commands.rb', line 57

def last_command_stopped
  aruba.command_monitor.last_command_stopped
end

#pipe_in_file(file_name) ⇒ Object

Pipe data in file

Parameters:

  • file_name (String)

    The file which should be used to pipe in data



35
36
37
38
39
40
41
# File 'lib/aruba/api/commands.rb', line 35

def pipe_in_file(file_name)
  file_name = expand_path(file_name)

  File.open(file_name, "r").each_line do |line|
    last_command_started.write(line)
  end
end

#run_command(cmd, opts = {}) {|SpawnProcess| ... } ⇒ Object

Run given command and stop it if timeout is reached

Parameters:

  • cmd (String)

    The command which should be executed

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • exit_timeout (Numeric)

    If the timeout is reached the command will be killed

  • io_wait_timeout (Numeric)

    Wait for IO to finish

  • startup_wait_time (Numeric)

    Wait for a command to start

  • stop_signal (String)

    Use signal to stop command

Yields:

  • (SpawnProcess)

    Run block with process



150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/aruba/api/commands.rb', line 150

def run_command(cmd, opts = {})
  command = prepare_command(cmd, opts)

  unless command.interactive?
    raise NotImplementedError,
          "Running interactively is not supported with this process launcher."
  end

  start_command(command)

  block_given? ? yield(command) : command
end

#run_command_and_stop(cmd, opts = {}) ⇒ Object

Run a command with aruba

Checks for error during command execution and checks the output to detect an timeout error.

Parameters:

  • cmd (String)

    The command to be executed

  • opts (Hash) (defaults to: {})

    Options for aruba

Options Hash (opts):

  • :fail_on_error (Boolean)

    Should aruba fail on error?

  • :exit_timeout (Numeric)

    Timeout for execution

  • :io_wait_timeout (Numeric)

    Timeout for IO - STDERR, STDOUT



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/aruba/api/commands.rb', line 183

def run_command_and_stop(cmd, opts = {})
  fail_on_error = if opts.key?(:fail_on_error)
                    opts.delete(:fail_on_error) == true
                  else
                    true
                  end

  command = prepare_command(cmd, opts)
  start_command(command)
  command.stop

  return unless fail_on_error

  begin
    expect(command).to have_finished_in_time
    expect(command).to be_successfully_executed
  rescue ::RSpec::Expectations::ExpectationNotMetError => e
    aruba.announcer.activate(aruba.config.activate_announcer_on_command_failure)
    aruba.event_bus.notify Events::CommandStopped.new(command)
    raise e
  end
end

#stop_all_commands {|Command| ... } ⇒ Object

Stop all commands

Yields:

  • (Command)

    If block is given use it to filter the commands which should be stoppend.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aruba/api/commands.rb', line 66

def stop_all_commands(&block)
  cmds = if block
           all_commands.select(&block)
         else
           all_commands
         end

  cmds.each(&:stop)

  self
end

#terminate_all_commands {|Command| ... } ⇒ Object

Terminate all commands

Yields:

  • (Command)

    If block is given use it to filter the commands which should be terminated.



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aruba/api/commands.rb', line 83

def terminate_all_commands(&block)
  cmds = if block
           all_commands.select(&block)
         else
           all_commands
         end

  cmds.each(&:terminate)

  self
end

#type(input) ⇒ Object

Provide data to command via stdin

Parameters:

  • input (String)

    The input for the command



210
211
212
213
214
# File 'lib/aruba/api/commands.rb', line 210

def type(input)
  return close_input if input == "\u0004"

  last_command_started.write(input << "\n")
end

#which(program, path = nil) ⇒ Object

Resolve path for command using the PATH-environment variable

Parameters:

  • program (#to_s)

    The name of the program which should be resolved

  • path (String) (defaults to: nil)

    The PATH, a string concatenated with “:”, e.g. /usr/bin/:/bin on a UNIX-system



22
23
24
25
26
27
28
29
# File 'lib/aruba/api/commands.rb', line 22

def which(program, path = nil)
  with_environment do
    # ENV is set within this block
    path = ENV["PATH"] if path.nil?

    Aruba.platform.which(program, path)
  end
end