class MDT::Helpers::Runner
A helper class used in the MDT
executable
Public Class Methods
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 incontents
Returns:
-
1 on failure, otherwise 0
# File lib/mdt/helpers/runner.rb 12 def self.process_contents(contents, modifiers = []) 13 failure = false 14 contents.each do |elem| 15 unless failure 16 if elem.has_key?('command') 17 cmd_config = elem['command'] 18 cmd_config['break_on_failure'] ||= true 19 cmd_config['success_codes'] ||= [0] 20 unless cmd_config.has_key?('type') 21 puts 'ERROR: command.type must be defined!' 22 if cmd_config['break_on_failure'] 23 failure = true 24 next 25 end 26 end 27 cmd_key = cmd_config['type'].split('.').first 28 cmd_value = cmd_config['type'].split('.').last 29 cmd_options = cmd_config['options'] || {} 30 cmd_modifiers = modifiers + (cmd_config['command_modifiers'] || []) 31 cmd = MDT::Commands::Base.descendants.select { |c| c.key == cmd_key }.first 32 if cmd == nil 33 puts "ERROR: Could not find a command set with key #{cmd_key}. Check its correctness or install needed MDT modules." 34 if cmd_config['break_on_failure'] 35 failure = true 36 end 37 next 38 end 39 unless cmd.subkeys.include?(cmd_value) 40 puts "ERROR: Command set with key #{cmd_key} does not have a command with key #{cmd_value}!" 41 if cmd_config['break_on_failure'] 42 failure = true 43 end 44 next 45 end 46 cmd = cmd.new 47 code = cmd.execute(cmd_value, cmd_modifiers, cmd_options) 48 if cmd_config['success_codes'].include?(code) 49 puts "Command exited with success code #{code}" 50 else 51 puts "Command exited with error code #{code}" 52 if cmd_config['break_on_failure'] 53 failure = true 54 end 55 next 56 end 57 elsif elem.has_key?('command_group') 58 cg_config = elem['command_group'] 59 cg_config['break_on_failure'] ||= true 60 unless cg_config.has_key?('contents') 61 puts "ERROR: Command group #{cg_config['name']} without contents!" 62 if cg_config['break_on_failure'] 63 failure = true 64 end 65 next 66 end 67 puts "Executing command group: #{cg_config['name']}" 68 code = MDT::Helpers::Runner.process_contents(cg_config['contents'], modifiers + (cg_config['command_modifiers'] || [])) 69 if code == 0 70 puts "Command group: #{cg_config['name']} - finished with success" 71 else 72 puts "Command group: #{cg_config['name']} - finished with failure" 73 if cg_config['break_on_failure'] 74 failure = true 75 end 76 next 77 end 78 else 79 puts 'WARNING: Encountered a config element that is not command nor command_group, skipping...' 80 end 81 end 82 end 83 failure ? 1 : 0 84 end
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 incontents
-
failure
- a boolean that describes if the deploy process has ended with failure, false by default
Returns:
-
nil
# File lib/mdt/helpers/runner.rb 93 def self.process_post_contents(contents, modifiers = [], failure = false) 94 contents.each do |elem| 95 if elem.has_key?('command') 96 cmd_config = elem['command'] 97 cmd_config['status'] ||= 'all' 98 cmd_key = cmd_config['type'].split('.').first 99 cmd_value = cmd_config['type'].split('.').last 100 cmd_options = cmd_config['options'] || {} 101 cmd_modifiers = modifiers + (cmd_config['command_modifiers'] || []) 102 cmd = MDT::Commands::Base.descendants.select { |c| c.key == cmd_key }.first 103 if cmd == nil 104 puts "ERROR: Could not find a command set with key #{cmd_key}. Check its correctness or install needed MDT modules." 105 next 106 end 107 unless cmd.subkeys.include?(cmd_value) 108 puts "ERROR: Command set with key #{cmd_key} does not have a command with key #{cmd_value}!" 109 next 110 end 111 if cmd_config['status'] == 'all' 112 cmd = cmd.new 113 code = cmd.execute(cmd_value, cmd_modifiers, cmd_options) 114 puts "Command exited with code #{code}" 115 elsif cmd_config['status'] == 'success' 116 unless failure 117 cmd = cmd.new 118 code = cmd.execute(cmd_value, cmd_modifiers, cmd_options) 119 puts "Command exited with code #{code}" 120 end 121 elsif cmd_config['status'] == 'failure' 122 if failure 123 cmd = cmd.new 124 code = cmd.execute(cmd_value, cmd_modifiers, cmd_options) 125 puts "Command exited with code #{code}" 126 end 127 else 128 puts 'status config key should have value all, success or failure, skipping command...' 129 end 130 elsif elem.has_key?('command_group') 131 cg_config = elem['command_group'] 132 unless cg_config.has_key?('contents') 133 puts "ERROR: Command group #{cg_config['name']} without contents!" 134 next 135 end 136 puts "Executing command group: #{cg_config['name']}" 137 MDT::Helpers::Runner.process_post_contents(cg_config['contents'], modifiers + (cg_config['command_modifiers'] || []), failure) 138 puts "Command group: #{cg_config['name']} - finished" 139 else 140 puts 'WARNING: Encountered a config element that is not command nor command_group, skipping...' 141 end 142 end 143 nil 144 end