Sha256: b254fbce0d632f87f739fd0c1524dc1341420de598bef96e5c69a904b8f5ec8f

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

require 'yaml'

module Rundock
  module Builder
    class HookBuilder < Base
      HookStructureError = Class.new(NotImplementedError)

      def build(enables)
        if enables.blank?
          Logger.error('Empty hook is specified.')
          return []
        elsif @options[:hooks] && FileTest.exist?(@options[:hooks])
          hooks_file = @options[:hooks]
          Logger.info("hooks file is #{hooks_file}")
        else
          Logger.error('Empty hook detected. Please specifiy hook option.')
          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.error('Empty hook detected. Please verify hooks file and scenario file.')
        hooks
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rundock-0.4.4 lib/rundock/builder/hook_builder.rb