summaryrefslogtreecommitdiff
path: root/lib/oxidized/hook.rb
blob: 2eb6f87a624e26db39b095b09e637036d28a7ec7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
module Oxidized
  class HookManager
    class << self
      def from_config cfg
        mgr = new
        cfg.hooks.each do |name, h_cfg|
          h_cfg.events.each do |event|
            mgr.register event.to_sym, name, h_cfg.type, h_cfg
          end
        end
        mgr
      end
    end

    # HookContext is passed to each hook. It can contain anything related to the
    # event in question. At least it contains the event name
    class HookContext < OpenStruct; end

    # RegisteredHook is a container for a Hook instance
    class RegisteredHook < Struct.new(:name, :hook); end

    Events = [
      :node_success,
      :node_fail,
      :post_store,
      :nodes_done
    ]
    attr_reader :registered_hooks

    def initialize
      @registered_hooks = Hash.new { |h, k| h[k] = [] }
    end

    def register event, name, hook_type, cfg
      unless Events.include? event
        raise ArgumentError,
              "unknown event #{event}, available: #{Events.join ','}"
      end

      Oxidized.mgr.add_hook(hook_type) or raise("cannot load hook '#{hook_type}', not found")
      begin
        hook = Oxidized.mgr.hook.fetch(hook_type).new
      rescue KeyError
        raise KeyError, "cannot find hook #{hook_type.inspect}"
      end

      hook.cfg = cfg

      @registered_hooks[event] << RegisteredHook.new(name, hook)
      Oxidized.logger.debug "Hook #{name.inspect} registered #{hook.class} for event #{event.inspect}"
    end

    def handle event, ctx_params = {}
      ctx = HookContext.new ctx_params
      ctx.event = event

      @registered_hooks[event].each do |r_hook|
        begin
          r_hook.hook.run_hook ctx
        rescue => e
          Oxidized.logger.error "Hook #{r_hook.name} (#{r_hook.hook}) failed " +
                                "(#{e.inspect}) for event #{event.inspect}"
        end
      end
    end
  end

  # Hook abstract base class
  class Hook
    attr_reader :cfg

    def initialize
    end

    def cfg=(cfg)
      @cfg = cfg
      validate_cfg! if self.respond_to? :validate_cfg!
    end

    def run_hook ctx
      raise NotImplementedError
    end

    def log(msg, level = :info)
      Oxidized.logger.send(level, "#{self.class.name}: #{msg}")
    end
  end
end