Module: ForemanHooks

Defined in:
lib/foreman_hooks.rb,
lib/foreman_hooks/engine.rb,
lib/foreman_hooks/as_dependencies_hook.rb

Defined Under Namespace

Modules: ASDependenciesHook, CallbackHooks, OrchestrationHook, Util Classes: Engine, Error

Class Method Summary collapse

Class Method Details

.attach_hook(klass, events) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/foreman_hooks.rb', line 70

def attach_hook(klass, events)
  if events.keys.detect { |event| ['create', 'update', 'destroy', 'postcreate', 'postupdate'].include? event }
    unless klass.ancestors.include?(ForemanHooks::OrchestrationHook)
      logger.debug "Extending #{klass} with foreman_hooks orchestration hooking support"
      klass.send(:include, ForemanHooks::OrchestrationHook)
    end
  end

  unless klass.ancestors.include?(ForemanHooks::CallbackHooks)
    logger.debug "Extending #{klass} with foreman_hooks Rails hooking support"
    klass.send(:include, ForemanHooks::CallbackHooks)
  end
end

.discover_hooksObject

Find all executable hook files under $hook_root/model_name/event_name/



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/foreman_hooks.rb', line 16

def discover_hooks
  hooks = {}
  Dir.glob(File.join(hooks_root, '**', '*')) do |filename|
    next if filename.end_with? '~'
    next if filename.end_with? '.bak'
    next if File.directory? filename
    next unless File.executable? filename

    relative = filename[hooks_root.size..-1]
    next unless relative =~ %r{^/(.+)/([^/]+)/([^/]+)$}
    klass = $1.camelize
    event = $2
    script_name = $3
    hooks[klass] ||= {}
    hooks[klass][event] ||= []
    hooks[klass][event] << filename
    logger.debug "Found hook to #{klass.to_s}##{event}, filename #{script_name}"
  end
  hooks
end

.events(klass = nil) ⇒ Object

‘event1’, ‘event2’


52
53
54
55
56
57
58
59
60
# File 'lib/foreman_hooks.rb', line 52

def events(klass = nil)
  filtered = if klass
               klass = klass.name if klass.kind_of? Class
               Hash[hooks.select { |k,e| k == klass }]
             else
               hooks
             end
  filtered.values.map(&:keys).flatten.uniq.map(&:to_sym)
end

.find_hooks(klass, event) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/foreman_hooks.rb', line 62

def find_hooks(klass, event)
  klass = klass.name if klass.kind_of? Class
  return unless filtered = hooks[klass]
  return unless filtered = filtered[event.to_s]
  return if filtered.empty?
  filtered
end

.hooksObject

=> {‘event_name’ => [‘/path/to/01.sh’, ‘/path/to/02.sh’]}



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/foreman_hooks.rb', line 38

def hooks
  unless @hooks
    @hooks = discover_hooks
    @hooks.each do |klass,events|
      events.each do |event,hooks|
        logger.info "Finished discovering #{hooks.size} hooks for #{klass}##{event}"
        hooks.sort!
      end
    end
  end
  @hooks
end

.hooks_rootObject



11
12
13
# File 'lib/foreman_hooks.rb', line 11

def hooks_root
  File.join(Rails.application.root, 'config', 'hooks')
end

.loggerObject



84
# File 'lib/foreman_hooks.rb', line 84

def logger; Rails.logger; end