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

#close_inputObject

Close stdin


227
228
229
# File 'lib/aruba/api/commands.rb', line 227

def close_input
  last_command_started.close_io(:stdin)
end

#find_command(commandline) ⇒ Object

Find a started command

Parameters:


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

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

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/CyclomaticComplexity

Parameters:

  • cmd (String)

    The command which should be executed

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

    Options

Options Hash (opts):

  • exit_timeout (Integer)

    If the timeout is reached the command will be killed

  • io_wait_timeout (Integer)

    Wait for IO to finish

  • startup_wait_time (Integer)

    Wait for a command to start

  • stop_signal (String)

    Use signal to stop command

Yields:

  • (SpawnProcess)

    Run block with process


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/aruba/api/commands.rb', line 128

def run_command(cmd, opts = {})
  exit_timeout      = opts[:exit_timeout].nil? ? aruba.config.exit_timeout : opts[:exit_timeout]
  io_wait_timeout   = opts[:io_wait_timeout].nil? ? aruba.config.io_wait_timeout : opts[:io_wait_timeout]
  stop_signal       = opts[:stop_signal].nil? ? aruba.config.stop_signal : opts[:stop_signal]
  startup_wait_time = opts[:startup_wait_time].nil? ? aruba.config.startup_wait_time : opts[:startup_wait_time]

  cmd = replace_variables(cmd)

  @commands ||= []
  @commands << cmd

  environment       = aruba.environment
  working_directory = expand_path('.')
  event_bus         = aruba.event_bus

  cmd = Aruba.platform.detect_ruby(cmd)

  mode = aruba.config.command_launcher

  main_class = aruba.config.main_class

  command = Command.new(
    cmd,
    mode: mode,
    exit_timeout: exit_timeout,
    io_wait_timeout: io_wait_timeout,
    working_directory: working_directory,
    environment: environment.to_hash,
    main_class: main_class,
    stop_signal: stop_signal,
    startup_wait_time: startup_wait_time,
    event_bus: event_bus
  )

  aruba.config.before(:command, self, command)

  command.start

  aruba.announcer.announce(:stop_signal, command.pid, stop_signal) if stop_signal

  aruba.config.after(:command, self, 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

  • options (Hash)

    Options for aruba


195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/aruba/api/commands.rb', line 195

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 = run_command(cmd, opts)
  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_given?
           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_given?
           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


221
222
223
224
# File 'lib/aruba/api/commands.rb', line 221

def type(input)
  return close_input if input == ""
  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