Class: Kafo::Hooking

Inherits:
Object
  • Object
show all
Defined in:
lib/kafo/hooking.rb

Constant Summary collapse

TYPES =
  • pre_migrations - just after kafo reads its configuration - useful for config file updates. Only in this stage it is posible to request config reload (‘Kafo.request_config_reload`) to get in our changes

  • boot - before kafo is ready to work, useful for adding new app arguments, logger won’t work yet

  • init - just after hooking is initialized and kafo is configured, parameters have no values yet

  • pre_values - just before value from CLI is set to parameters (they already have default values)

  • pre_validations - just after system checks and before validations are executed (and before interactive wizard is started), at this point all parameter values are already set but not yet stored in answer file

  • pre_commit - after validations or interactive wizard have completed, all parameter values are set but not yet stored in the answer file

  • pre - just before puppet is executed to converge system

  • post - just after puppet is executed to converge system

  • pre_exit - happens during exit handling, before exit is completed

[:pre_migrations, :boot, :init, :pre_values, :pre_validations, :pre_commit, :pre, :post, :pre_exit]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHooking

Returns a new instance of Hooking.



19
20
21
22
# File 'lib/kafo/hooking.rb', line 19

def initialize
  self.hooks = Hash.new { |h, k| h[k] = {} }
  @loaded = false
end

Instance Attribute Details

#hooksObject

Returns the value of attribute hooks.



17
18
19
# File 'lib/kafo/hooking.rb', line 17

def hooks
  @hooks
end

#kafoObject

Returns the value of attribute kafo.



17
18
19
# File 'lib/kafo/hooking.rb', line 17

def kafo
  @kafo
end

Instance Method Details

#execute(group, log_stage: true) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/kafo/hooking.rb', line 57

def execute(group, log_stage: true)
  logger = Logger.new(group)
  logger.info "Executing hooks in group #{group}" if log_stage

  sorted_hooks = self.hooks[group].keys.sort do |a, b|
    File.basename(a.to_s) <=> File.basename(b.to_s)
  end

  sorted_hooks.each do |name|
    hook = self.hooks[group][name]
    result = HookContext.execute(self.kafo, logger, &hook)
    logger.debug "Hook #{name} returned #{result.inspect}"
  end

  logger.info "All hooks in group #{group} finished" if log_stage
  @group = nil
end

#loadObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/kafo/hooking.rb', line 28

def load
  base_dirs = [File.join([KafoConfigure.root_dir, 'hooks']), KafoConfigure.config.app[:hook_dirs]].flatten
  base_dirs.each do |base_dir|
    TYPES.each do |hook_type|
      dir = File.join(base_dir, hook_type.to_s)
      Dir.glob(dir + "/*.rb").sort.each do |file|
        logger.debug "Loading hook #{file}"
        hook = File.read(file)
        hook_block = proc { instance_eval(hook, file, 1) }
        register(hook_type, file, &hook_block)
      end
    end

    # Multi stage hooks are special
    Dir.glob(File.join(base_dir, 'multi', '*.rb')).sort.each do |file|
      logger.debug "Loading multi stage hook #{file}"
      hook = File.read(file)
      MultiStageHook.new(file, self, TYPES).instance_eval(hook, file, 1)
    end

    @loaded = true
  end
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/kafo/hooking.rb', line 53

def loaded?
  @loaded
end

#loggerObject



24
25
26
# File 'lib/kafo/hooking.rb', line 24

def logger
  KafoConfigure.logger
end

#register_boot(name, &block) ⇒ Object



79
80
81
# File 'lib/kafo/hooking.rb', line 79

def register_boot(name, &block)
  register(:boot, name, &block)
end

#register_init(name, &block) ⇒ Object



83
84
85
# File 'lib/kafo/hooking.rb', line 83

def register_init(name, &block)
  register(:init, name, &block)
end

#register_post(name, &block) ⇒ Object



103
104
105
# File 'lib/kafo/hooking.rb', line 103

def register_post(name, &block)
  register(:post, name, &block)
end

#register_pre(name, &block) ⇒ Object



99
100
101
# File 'lib/kafo/hooking.rb', line 99

def register_pre(name, &block)
  register(:pre, name, &block)
end

#register_pre_commit(name, &block) ⇒ Object



95
96
97
# File 'lib/kafo/hooking.rb', line 95

def register_pre_commit(name, &block)
  register(:pre_commit, name, &block)
end

#register_pre_exit(name, &block) ⇒ Object



107
108
109
# File 'lib/kafo/hooking.rb', line 107

def register_pre_exit(name, &block)
  register(:pre_exit, name, &block)
end

#register_pre_migrations(name, &block) ⇒ Object



75
76
77
# File 'lib/kafo/hooking.rb', line 75

def register_pre_migrations(name, &block)
  register(:pre_migrations, name, &block)
end

#register_pre_validations(name, &block) ⇒ Object



91
92
93
# File 'lib/kafo/hooking.rb', line 91

def register_pre_validations(name, &block)
  register(:pre_validations, name, &block)
end

#register_pre_values(name, &block) ⇒ Object



87
88
89
# File 'lib/kafo/hooking.rb', line 87

def register_pre_values(name, &block)
  register(:pre_values, name, &block)
end