Module: Thales::Pse::Cli

Includes:
TR::CliUtils, TR::CondUtils
Included in:
HSM, Token, Config
Defined in:
lib/thales/pse/cli.rb,
lib/thales/pse/hsm.rb,
lib/thales/pse/token.rb

Defined Under Namespace

Modules: HSM, Token Classes: ExecutableNotFoundError

Instance Method Summary collapse

Instance Method Details

#cli_exec(exec, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
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
49
50
51
52
53
54
55
56
57
58
# File 'lib/thales/pse/cli.rb', line 14

def cli_exec(exec, &block)

  exePath = which(exec)
  raise ExecutableNotFoundError, "#{exec} cannot be found" if is_empty?(exePath)
  raise Error, "Block is required" if not block

  params = block.call(:params) || []
  expect_list = block.call(:expect_list)

  output = block.call(:output) || TR::NullOut.new

  cmd = "#{exePath} #{params.join(" ")}"

  logger.tdebug :cmd, "cli_exec command : #{cmd}"

  PTY.spawn(exePath, *params) do |read, write, pid|

    begin

      if not is_empty?(expect_list)
        expect_list.each do |ex|
          to = ex[:timeout] || 1
          cont = read.expect(/#{ex[:matcher]}/, to)
          output.puts cont
          write.puts block.call(ex[:block_key], cont)
        end
      end

      Timeout.timeout(1) do
        read.each do |l|
          output.puts l if not_empty?(l)
        end
      end

    rescue Timeout::Error => e
      read.close
      write.close
      Process.kill('TERM',pid)
      output.puts "Process #{cmd} ('#{pid}') killed due to timeout"
    rescue Errno::EIO
      output.puts "[Done] #{cmd}"
    end
  end
  
end

#loggerObject



60
61
62
63
64
65
# File 'lib/thales/pse/cli.rb', line 60

def logger
  if @logger.nil?
    @logger = Tlogger.new
  end
  @logger
end