Class: Kafo::ExitHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/exit_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExitHandler

Returns a new instance of ExitHandler.



5
6
7
8
9
# File 'lib/kafo/exit_handler.rb', line 5

def initialize
  @cleanup_paths = []
  @exit_code = 0
  @logger = KafoConfigure.logger
end

Instance Attribute Details

#cleanup_pathsObject

Returns the value of attribute cleanup_paths.



3
4
5
# File 'lib/kafo/exit_handler.rb', line 3

def cleanup_paths
  @cleanup_paths
end

#exit_codeObject

Returns the value of attribute exit_code.



3
4
5
# File 'lib/kafo/exit_handler.rb', line 3

def exit_code
  @exit_code
end

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/kafo/exit_handler.rb', line 3

def logger
  @logger
end

Instance Method Details

#cleanupObject



40
41
42
43
44
45
46
# File 'lib/kafo/exit_handler.rb', line 40

def cleanup
  # make sure default values are removed from /tmp
  (self.cleanup_paths + ['/tmp/default_values.yaml']).each do |file|
    logger.debug "Cleaning #{file}"
    FileUtils.rm_rf(file)
  end
end

#error_codesObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/kafo/exit_handler.rb', line 11

def error_codes
  @error_codes ||= {
      :invalid_system => 20,
      :invalid_values => 21,
      :manifest_error => 22,
      :no_answer_file => 23,
      :unknown_module => 24,
      :defaults_error => 25
  }
end

#exit(code, &block) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/kafo/exit_handler.rb', line 22

def exit(code, &block)
  @exit_code = translate_exit_code(code)
  block.call if block
  KafoConfigure.logger.debug "Exit with status code: #{@exit_code} (signal was #{code})"
  KafoConfigure.logger.dump_errors
  cleanup
  Kernel.exit(@exit_code)
end

#register_cleanup_path(path) ⇒ Object



48
49
50
# File 'lib/kafo/exit_handler.rb', line 48

def register_cleanup_path(path)
  self.cleanup_paths<< path
end

#translate_exit_code(code) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/kafo/exit_handler.rb', line 31

def translate_exit_code(code)
  return code if code.is_a?(Fixnum)
  if error_codes.has_key?(code)
    return error_codes[code]
  else
    raise "Unknown code #{code}"
  end
end