Module: DSL

Included in:
Case
Defined in:
lib/teuton/case_manager/case/dsl/log.rb,
lib/teuton/case_manager/case/dsl/goto.rb,
lib/teuton/case_manager/case/dsl/send.rb,
lib/teuton/case_manager/case/dsl/check.rb,
lib/teuton/case_manager/case/dsl/expect.rb,
lib/teuton/case_manager/case/dsl/getset.rb,
lib/teuton/case_manager/case/dsl/target.rb,
lib/teuton/case_manager/case/dsl/unique.rb,
lib/teuton/case_manager/case/dsl/missing.rb,
lib/teuton/case_manager/case/dsl/deprecated.rb

Overview

DSL#request, DSL#target2

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args = {}) ⇒ Object

If a method call is missing, then delegate to concept parent.



6
7
8
9
10
11
# File 'lib/teuton/case_manager/case/dsl/missing.rb', line 6

def method_missing(method, args = {})
  a = method.to_s
  return instance_eval("get(:#{a[0, a.size - 1]})") if a[a.size - 1] == '?'
  return check a[6, a.size], args if a[0,6]=='check_'
  check a, args
end

Instance Method Details

#check(name, input = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/teuton/case_manager/case/dsl/check.rb', line 6

def check(name, input = {})
  checks = Application.instance.checks
  unless checks[name]
    log("Check #{name} not found!", :error)
    return
  end
  input.each_pair { |k, v| set(k, v) }
  errors = []
  checks[name][:args].each do |i|
    errors << i if get(i) == 'NODATA'
  end
  if errors.count > 0
    log("Check #{name} => required params #{errors.join(',')}",:error)
  else
    instance_eval(&checks[name][:block])
  end
  input.each_pair { |k, v| unset(k) }
end

#command(p_command, args = {}) ⇒ Object



10
11
12
13
# File 'lib/teuton/case_manager/case/dsl/deprecated.rb', line 10

def command(p_command, args = {})
  @action[:command] = p_command
  tempfile(args[:tempfile]) if args[:tempfile]
end

#expect(input, args = {}) ⇒ Object

expect <condition>, :weight => <value>



39
40
41
42
43
44
45
46
47
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 39

def expect(input, args = {})
  if input.class == TrueClass || input.class == FalseClass
    expect2(input, args)
  elsif input.class == String || input.class == Regexp || input.class == Array
    expect_any input
  else
    puts "[ERROR] expect #{input} (#{input.class})"
  end
end

#expect2(cond, args = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 49

def expect2(cond, args = {})
  @action_counter += 1
  @action[:id] = @action_counter
  @action[:check] = cond
  @action[:result] = @result.value

  @action[:alterations] = @result.alterations
  @action[:expected] = @result.expected
  @action[:expected] = args[:expected] if args[:expected]

  @report.lines << @action.clone
  weight(1.0)

  app = Application.instance
  c = app.letter[:bad]
  c = app.letter[:good] if cond
  verbose c
end

#expect_any(input) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 29

def expect_any(input)
  if input.class == Array
    input.each { |i| result.find(i) }
  else
    result.find(input)
  end
  expect2 result.count.gt(0)
end

#expect_none(input) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 11

def expect_none(input)
  if input.class == Array
    input.each { |i| result.find(i) }
  else
    result.find(input)
  end
  expect2 result.count.eq(0)
end

#expect_one(input) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 20

def expect_one(input)
  if input.class == Array
    input.each { |i| result.find(i) }
  else
    result.find(input)
  end
  expect2 result.count.eq(1)
end

#get(option) ⇒ Object

Read param option from [running, config or global] Hash data



6
7
8
# File 'lib/teuton/case_manager/case/dsl/getset.rb', line 6

def get(option)
  @config.get(option)
end

#gett(option) ⇒ Object



10
11
12
13
# File 'lib/teuton/case_manager/case/dsl/getset.rb', line 10

def gett(option)
  value = get(option)
  "#{value} (#{option})"
end

#goto(host = :localhost, args = {}) ⇒ Object Also known as: on

Run command from the host identify as pHostname goto :host1, :execute => “command”



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/teuton/case_manager/case/dsl/goto.rb', line 9

def goto(host = :localhost, args = {})
  @result.reset
  @action[:command] = args[:execute] if args[:execute]
  @action[:command] = args[:exec] if args[:exec]
  tempfile(args[:tempfile]) if args[:tempfile]
  @action[:encoding] = args[:encoding] || 'UTF-8'

  protocol = @config.get("#{host}_protocol".to_sym)
  ip = @config.get("#{host}_ip".to_sym)
  start_time = Time.now
  if (protocol == 'NODATA' || protocol.nil?) &&
     (host.to_s == 'localhost' || host.to_s.include?('127.0.0.') || ip.include?('127.0.0.'))
    run_local_cmd
  elsif ip == 'NODATA'
    log("#{host} IP not found!", :error)
  else
    run_remote_cmd host
  end
  @action[:duration] = (Time.now - start_time).round(3)
end

#log(text = '', type = :info) ⇒ Object Also known as: msg



5
6
7
8
9
10
11
12
# File 'lib/teuton/case_manager/case/dsl/log.rb', line 5

def log(text = '', type = :info)
  s = ''
  s = Rainbow('WARN!').color(:yellow) if type == :warn
  s = Rainbow('ERROR').bg(:red) if type == :error
  t = Time.now
  f = format('%02d:%02d:%02d', t.hour, t.min, t.sec)
  @report.lines << "[#{f}] #{s}: #{text}"
end

#readme(_text) ⇒ Object



5
6
7
# File 'lib/teuton/case_manager/case/dsl/target.rb', line 5

def readme(_text)
  # Usefull for "teuton reamde" action.
end

#remote_tempdirObject



66
67
68
# File 'lib/teuton/case_manager/case/dsl/send.rb', line 66

def remote_tempdir
  @remote_tmpdir
end

#remote_tempfileObject



61
62
63
64
# File 'lib/teuton/case_manager/case/dsl/send.rb', line 61

def remote_tempfile
  # puts '[WARN] Using DSL.tempfile'
  @action[:remote_tempfile]
end

#request(text) ⇒ Object



5
6
7
8
# File 'lib/teuton/case_manager/case/dsl/deprecated.rb', line 5

def request(text)
  raise "Deprecated request #{text}"
  # do nothing by now
end

#run(command, args = {}) ⇒ Object



31
32
33
34
# File 'lib/teuton/case_manager/case/dsl/goto.rb', line 31

def run(command, args = {})
  args[:exec] = command.to_s
  goto(:localhost, args)
end

#send(args = {}) ⇒ Object



10
11
12
13
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
# File 'lib/teuton/case_manager/case/dsl/send.rb', line 10

def send(args = {})
  return if skip?

  return unless args[:copy_to]

  host = args[:copy_to].to_s
  return unless @conn_status[host].nil?

  ip = get((host + '_ip').to_sym)
  username = get((host + '_username').to_sym).to_s
  password = get((host + '_password').to_sym).to_s

  filename = @report.filename + '.' + @report.format.to_s
  localfilepath = File.join(@report.output_dir, filename)
  filename = args[:prefix].to_s + filename if args[:prefix]

  if args[:remote_dir]
    remotefilepath = File.join(args[:remote_dir], filename)
  else
    # remotefilepath = File.join(remote_tempdir, filename)
    remotefilepath = File.join('.', filename)
  end

  # Upload a file or directory to the remote host
  begin
    Net::SFTP.start(ip, username, password: password) do |sftp|
      sftp.upload!(localfilepath, remotefilepath)
    end
    verboseln("=> [ OK  ] #{(get(:tt_members)[0,15]).ljust(16)} : #{remotefilepath}")
  rescue
    verboseln("=> [ERROR] #{(get(:tt_members)[0,15]).ljust(16)} : scp #{localfilepath} => #{remotefilepath}")
  end
end

#set(key, value) ⇒ Object



15
16
17
# File 'lib/teuton/case_manager/case/dsl/getset.rb', line 15

def set(key, value)
  @config.set(key, value)
end

#target(desc, args = {}) ⇒ Object Also known as: goal



9
10
11
12
13
14
# File 'lib/teuton/case_manager/case/dsl/target.rb', line 9

def target(desc, args = {})
  @action[:description] = desc.to_s
  @action[:asset] = args[:asset].to_s if args[:asset]
  w = args[:weight] || 1.0
  weight(w)
end

#tempdirObject



56
57
58
59
# File 'lib/teuton/case_manager/case/dsl/send.rb', line 56

def tempdir
  # puts '[WARN] Using DSL.tempdir'
  @tmpdir
end

#tempfile(input = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/teuton/case_manager/case/dsl/send.rb', line 44

def tempfile(input = nil)
  return @action[:tempfile] if input.nil?

  name = input
  name = 'teuton.tmp' if input == :default

  @action[:tempfile] = File.join(@tmpdir, name)
  @action[:remote_tempfile] = File.join(@remote_tmpdir, name)

  @action[:tempfile]
end

#unique(key, value) ⇒ Object



5
6
7
8
9
10
# File 'lib/teuton/case_manager/case/dsl/unique.rb', line 5

def unique(key, value)
  return if value.nil?

  k = (key.to_s + '=' + value.to_s).to_sym
  @uniques << k
end

#unset(key) ⇒ Object



19
20
21
# File 'lib/teuton/case_manager/case/dsl/getset.rb', line 19

def unset(key)
  @config.unset(key)
end

#weight(value = nil) ⇒ Object

Set weight value for the action



69
70
71
72
73
74
75
76
77
# File 'lib/teuton/case_manager/case/dsl/expect.rb', line 69

def weight(value = nil)
  if value.nil?
    @action[:weight]
  elsif value == :default
    @action[:weight] = 1.0
  else
    @action[:weight] = value.to_f
  end
end