Sha256: 96e3b62b32357bfc68d70d3f8dd080fced78545a6068a9d8556fd5adb69efe66

Contents?: true

Size: 1.46 KB

Versions: 18

Compression:

Stored size: 1.46 KB

Contents

module Kafo
  class ExitHandler
    attr_accessor :cleanup_paths, :exit_code

    def initialize
      @cleanup_paths = []
      @exit_code = 0
    end

    def error_codes
      @error_codes ||= {
          :invalid_system => 20,
          :invalid_values => 21,
          :manifest_error => 22,
          :no_answer_file => 23,
          :unknown_module => 24,
          :defaults_error => 25,
          :unset_scenario => 26,
          :scenario_error => 27,
          :missing_argument => 28,
          :insufficient_permissions => 29,
          :puppet_version_error => 30
      }
    end

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

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

    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

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

    private

    def logger
      @logger ||= KafoConfigure.logger
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
kafo-7.6.0 lib/kafo/exit_handler.rb
kafo-7.5.1 lib/kafo/exit_handler.rb
kafo-7.5.0 lib/kafo/exit_handler.rb
kafo-7.4.0 lib/kafo/exit_handler.rb
kafo-7.3.0 lib/kafo/exit_handler.rb
kafo-7.2.0 lib/kafo/exit_handler.rb
kafo-7.1.0 lib/kafo/exit_handler.rb
kafo-7.0.0 lib/kafo/exit_handler.rb
kafo-6.5.0 lib/kafo/exit_handler.rb
kafo-6.4.1 lib/kafo/exit_handler.rb
kafo-6.4.0 lib/kafo/exit_handler.rb
kafo-6.3.0 lib/kafo/exit_handler.rb
kafo-6.2.1 lib/kafo/exit_handler.rb
kafo-6.2.0 lib/kafo/exit_handler.rb
kafo-6.1.2 lib/kafo/exit_handler.rb
kafo-6.1.1 lib/kafo/exit_handler.rb
kafo-6.1.0 lib/kafo/exit_handler.rb
kafo-6.0.0 lib/kafo/exit_handler.rb