Sha256: b1719674eee62db867524525e5b695537c25b88270315bbfa03e7b573ba689a3

Contents?: true

Size: 1.56 KB

Versions: 8

Compression:

Stored size: 1.56 KB

Contents

require 'yaml'

module Rundock
  module Builder
    class HookBuilder < Base
      DEFAULT_HOOKS_FILE_PATH = './hooks.yml'
      HookStructureError = Class.new(NotImplementedError)

      def build(enables)
        if enables.blank?
          Logger.info('Empty hook is specified.')
          return []
        elsif @options[:hooks]
          if FileTest.exist?(@options[:hooks])
            hooks_file = @options[:hooks]
            Logger.info("hooks file is #{hooks_file}")
          else
            Logger.warn("hooks file is not found. use #{DEFAULT_HOOKS_FILE_PATH}")
            hooks_file = DEFAULT_HOOKS_FILE_PATH
          end
        else
          return []
        end

        build_from_file(hooks_file, enables)
      end

      def build_from_attributes(attributes)
        return [] unless attributes.key?(:enable_hooks)
        build_from_file(attributes[:hooks], attributes[:enable_hooks])
      end

      private

      def build_from_file(file, enables)
        hooks = []
        allow_all = enables.include?('all')

        File.open(file) do |f|
          YAML.load_documents(f) do |y|
            y.each do |k, v|
              raise HookStructureError if !v.is_a?(Hash) || !v.key?('hook_type')
              next if !allow_all && !enables.include?(k)
              hook = Rundock::HookFactory.instance(v['hook_type']).create(k, v.deep_symbolize_keys)
              hooks << hook
            end
          end
        end

        Logger.warn('Empty hook is detected. Please verity hooks file and scenario file.') if hooks.empty?
        hooks
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rundock-0.4.15 lib/rundock/builder/hook_builder.rb
rundock-0.4.14 lib/rundock/builder/hook_builder.rb
rundock-0.4.13 lib/rundock/builder/hook_builder.rb
rundock-0.4.12 lib/rundock/builder/hook_builder.rb
rundock-0.4.11 lib/rundock/builder/hook_builder.rb
rundock-0.4.10 lib/rundock/builder/hook_builder.rb
rundock-0.4.8 lib/rundock/builder/hook_builder.rb
rundock-0.4.7 lib/rundock/builder/hook_builder.rb