Class: MDT::Helpers::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/mdt/helpers/runner.rb

Overview

A helper class used in the MDT executable

Class Method Summary collapse

Class Method Details

.process_contents(contents, modifiers = []) ⇒ Object

A method that processes an array of contents that includes configurations for commands and command groups. Arguments:

  • contents - an array of configurations for commands and command groups. Refer to example configuration file in the code repository for structure description

  • modifiers - an array of configurations for command modifiers that have to be applied to every command defined in contents

Returns:

  • 1 on failure, otherwise 0



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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mdt/helpers/runner.rb', line 12

def self.process_contents(contents, modifiers = [])
  failure = false
  contents.each do |elem|
    unless failure
      if elem.has_key?('command')
        cmd_config = elem['command']
        cmd_config['break_on_failure'] ||= true
        cmd_config['success_codes'] ||= [0]
        unless cmd_config.has_key?('type')
          puts 'ERROR: command.type must be defined!'
          if cmd_config['break_on_failure']
            failure = true
            next
          end
        end
        cmd_key = cmd_config['type'].split('.').first
        cmd_value = cmd_config['type'].split('.').last
        cmd_options = cmd_config['options'] || {}
        cmd_modifiers = modifiers + (cmd_config['command_modifiers'] || [])
        cmd = MDT::Commands::Base.descendants.select { |c| c.key == cmd_key }.first
        if cmd == nil
          puts "ERROR: Could not find a command set with key #{cmd_key}. Check its correctness or install needed MDT modules."
          if cmd_config['break_on_failure']
            failure = true
            next
          end
        end
        unless cmd.subkeys.include?(cmd_value)
          puts "ERROR: Command set with key #{cmd_key} does not have a command with key #{cmd_value}!"
          if cmd_config['break_on_failure']
            failure = true
            next
          end
        end
        cmd = cmd.new
        code = cmd.execute(cmd_value, cmd_modifiers, cmd_options)
        if cmd_config['success_codes'].include?(code)
          puts "Command exited with success code #{code}"
        else
          puts "Command exited with error code #{code}"
          if cmd_config['break_on_failure']
            failure = true
            next
          end
        end
      elsif elem.has_key?('command_group')
        cg_config = elem['command_group']
        cg_config['break_on_failure'] ||= true
        unless cg_config.has_key?('contents')
          puts "ERROR: Command group #{cg_config['name']} without contents!"
          if cg_config['break_on_failure']
            failure = true
            next
          end
        end
        puts "Executing command group: #{cg_config['name']}"
        code = MDT::Helpers::Runner.process_contents(cg_config['contents'], modifiers + (cg_config['command_modifiers'] || []))
        if code == 0
          puts "Command group: #{cg_config['name']} - finished with success"
        else
          puts "Command group: #{cg_config['name']} - finished with failure"
          if cg_config['break_on_failure']
            failure = true
            next
          end
        end
      else
        puts 'WARNING: Encountered a config element that is not command nor command_group, skipping...'
      end
    end
  end
  failure ? 1 : 0
end