Module: ScaBox::Runner

Included in:
Scanner
Defined in:
lib/scabox_sdk/runner.rb

Instance Method Summary collapse

Instance Method Details

#command?(name) ⇒ Boolean

TODO: find a better way to do this

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/scabox_sdk/runner.rb', line 8

def command?(name)
  `which #{name}`
  $?.success?
end

#run_cmd(cmd) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/scabox_sdk/runner.rb', line 13

def run_cmd(cmd)
  print_debug(cmd) if @opts.verbose
  if command?(cmd[0])
    run_cmd_with_timeout(cmd)
  else
    print_error("Command not found: #{cmd[0]}")
    exit 1
  end
end

#run_cmd_with_timeout(cmd, timeout = 1800, output_file = @tempfile) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scabox_sdk/runner.rb', line 23

def run_cmd_with_timeout(cmd, timeout=1800, output_file=@tempfile)
  begin
    # Start the process in a separate thread
    thread = Thread.new { system(*cmd, out: output_file) }
  
    # Timeout block to limit execution time
    Timeout.timeout(timeout) do
      # Wait for the process to finish
      thread.join
    end
  
    # Check if the process has finished successfully
    if thread.value
      return true
    else
      # Command execution failed
      return false
    end
  rescue Timeout::Error
    # Handle timeout
    print_error('Timed out - Skipping...')
    # Terminate the process
    thread.kill
    return false
  end
end