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
          end
          next
        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
          end
          next
        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
          end
          next
        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
          end
          next
        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
          end
          next
        end
      else
        puts 'WARNING: Encountered a config element that is not command nor command_group, skipping...'
      end
    end
  end
  failure ? 1 : 0
end

.process_post_contents(contents, modifiers = [], failure = false) ⇒ 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

  • failure - a boolean that describes if the deploy process has ended with failure, false by default

Returns:

  • nil



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/mdt/helpers/runner.rb', line 93

def self.process_post_contents(contents, modifiers = [], failure = false)
  contents.each do |elem|
    if elem.has_key?('command')
      cmd_config = elem['command']
      cmd_config['status'] ||= 'all'
      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."
        next
      end
      unless cmd.subkeys.include?(cmd_value)
        puts "ERROR: Command set with key #{cmd_key} does not have a command with key #{cmd_value}!"
        next
      end
      if cmd_config['status'] == 'all'
        cmd = cmd.new
        code = cmd.execute(cmd_value, cmd_modifiers, cmd_options)
        puts "Command exited with code #{code}"
      elsif cmd_config['status'] == 'success'
        unless failure
          cmd = cmd.new
          code = cmd.execute(cmd_value, cmd_modifiers, cmd_options)
          puts "Command exited with code #{code}"
        end
      elsif cmd_config['status'] == 'failure'
        if failure
          cmd = cmd.new
          code = cmd.execute(cmd_value, cmd_modifiers, cmd_options)
          puts "Command exited with code #{code}"
        end
      else
        puts 'status config key should have value all, success or failure, skipping command...'
      end
    elsif elem.has_key?('command_group')
      cg_config = elem['command_group']
      unless cg_config.has_key?('contents')
        puts "ERROR: Command group #{cg_config['name']} without contents!"
        next
      end
      puts "Executing command group: #{cg_config['name']}"
      MDT::Helpers::Runner.process_post_contents(cg_config['contents'], modifiers + (cg_config['command_modifiers'] || []), failure)
      puts "Command group: #{cg_config['name']} - finished"
    else
      puts 'WARNING: Encountered a config element that is not command nor command_group, skipping...'
    end
  end
  nil
end