Class: Kafo::HookContext

Inherits:
BaseContext show all
Defined in:
lib/kafo/hook_context.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseContext

#facts

Constructor Details

#initialize(kafo) ⇒ HookContext

Returns a new instance of HookContext.



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

def initialize(kafo)
  @kafo = kafo
end

Instance Attribute Details

#kafoObject (readonly)

Returns the value of attribute kafo.



6
7
8
# File 'lib/kafo/hook_context.rb', line 6

def kafo
  @kafo
end

Class Method Details

.execute(kafo, &hook) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/kafo/hook_context.rb', line 8

def self.execute(kafo, &hook)
  # TODO can be removed in 0.6, is DEPRECATED since 0.5
  # instance_exec can be later changed to instance eval when people stop using |kafo| in their hooks
  # and rely only on hook context DSL
  if hook.arity > 0
    kafo.logger.warn "Hook '#{name}' is using block with arguments which is DEPRECATED, access to kafo instance is " +
                    "provided by hook DSL, please remove |kafo| from your hook block"
  end
  new(kafo).instance_exec(kafo, &hook)
end

Instance Method Details

#add_module(module_name, mapping = nil) ⇒ Object

You can add custom modules not explicitly enabled in answer file. This is especially useful if you want to add your plugin to existing installer. This module will become part of answer file so it also preserves parameter values between runs. It also list its options in help output. You can also specify mapping for this module as a second parameter. examples:

add_module('my_module')
add_module('foreman::plugin::staypuft', {:dir_name => 'foreman', :manifest_name => 'plugin/staypuft'})


62
63
64
65
# File 'lib/kafo/hook_context.rb', line 62

def add_module(module_name, mapping = nil)
  self.kafo.config.add_mapping(module_name, mapping) if mapping
  self.kafo.add_module(module_name)
end

#app_option(*args) ⇒ Object

if you want to add new app_option be sure to do as soon as possible (usually boot hook) otherwise it may be to late (e.g. when displaying help) examples:

app_option '--log-level', 'LEVEL', 'Log level for log file output', :default => config.app[:log_level]:
app_option ['-n', '--noop'], :flag, 'Run puppet in noop mode?', :default => false


36
37
38
# File 'lib/kafo/hook_context.rb', line 36

def app_option(*args)
  self.kafo.class.app_option(*args)
end

#app_value(option) ⇒ Object

examples:

app_value(:log_level)

note the dash to underscore convention



43
44
45
# File 'lib/kafo/hook_context.rb', line 43

def app_value(option)
  self.kafo.config.app[option.to_sym]
end

#exit(code) ⇒ Object

You can trigger installer exit by this method. You must specify exit code as a first argument. You can also specify a symbol alias which is built-in (see exit_handler.rb for more details). examples:

exit(0)
exit(:manifest_error)


81
82
83
# File 'lib/kafo/hook_context.rb', line 81

def exit(code)
  self.kafo.class.exit(code)
end

#get_custom_config(key) ⇒ Object

You can load a custom config value that has been saved using store_custom_config



86
87
88
# File 'lib/kafo/hook_context.rb', line 86

def get_custom_config(key)
  self.kafo.config.get_custom(key)
end

#get_custom_fact(key) ⇒ Object

Load a custom fact from the custom fact storage as saved by store_custom_fact



97
98
99
# File 'lib/kafo/hook_context.rb', line 97

def get_custom_fact(key)
  self.kafo.config.get_custom_fact(key)
end

#loggerObject

some of hooks won’t print any message because logger is not yet configured configuration of logger depends on application configration (log level etc.) examples:

logger.warn "this combindation of parameters is untested"


27
28
29
# File 'lib/kafo/hook_context.rb', line 27

def logger
  self.kafo.logger
end

#module_enabled?(module_name) ⇒ Boolean

Check if a module is enabled in the current configuration. examples:

module_enabled?('example')

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/kafo/hook_context.rb', line 70

def module_enabled?(module_name)
  mod = self.kafo.module(module_name)
  !mod.nil? && mod.enabled?
end

#param(module_name, parameter_name) ⇒ Object

examples:

param('foreman', 'interface').value = 'eth0'
param('foreman', 'interface').value = app_option('bind_on_interface')


50
51
52
# File 'lib/kafo/hook_context.rb', line 50

def param(module_name, parameter_name)
  self.kafo.param(module_name, parameter_name)
end

#scenario_dataObject

Return the actual data in the current scenario



121
122
123
# File 'lib/kafo/hook_context.rb', line 121

def scenario_data
  self.kafo.config.app
end

#scenario_idObject

Return the id of the current scenario



111
112
113
# File 'lib/kafo/hook_context.rb', line 111

def scenario_id
  self.kafo.config.scenario_id
end

#scenario_pathObject

Return the path to the current scenario



116
117
118
# File 'lib/kafo/hook_context.rb', line 116

def scenario_path
  self.kafo.config.config_file
end

#store_custom_config(key, value) ⇒ Object

You can save any value into kafo configuration file, this is useful if you need to share a value between more hooks and persist the values for next run



92
93
94
# File 'lib/kafo/hook_context.rb', line 92

def store_custom_config(key, value)
  self.kafo.config.set_custom(key, value)
end

#store_custom_fact(key, value) ⇒ Object

Store a any custom fact. This will show up as kafo.scenario.custom.your_fact. It is possible to use structures such as arrays and hashes besides the obvious ones such as strings, integers, booleans.

These facts can also be used in Hiera hierachy definitions.



106
107
108
# File 'lib/kafo/hook_context.rb', line 106

def store_custom_fact(key, value)
  self.kafo.config.set_custom_fact(key, value)
end