class Kafo::HookContext
Attributes
Public Class Methods
# File lib/kafo/hook_context.rb, line 7 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
# File lib/kafo/hook_context.rb, line 18 def initialize(kafo) @kafo = kafo end
Public Instance Methods
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'})
# File lib/kafo/hook_context.rb, line 61 def add_module(module_name, mapping = nil) self.kafo.config.add_mapping(module_name, mapping) if mapping self.kafo.add_module(module_name) end
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
# File lib/kafo/hook_context.rb, line 35 def app_option(*args) self.kafo.class.app_option *args end
examples:
app_value(:log_level)
note the dash to underscore convention
# File lib/kafo/hook_context.rb, line 42 def app_value(option) self.kafo.config.app[option.to_sym] end
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)
# File lib/kafo/hook_context.rb, line 79 def exit(code) self.kafo.exit(code) end
You can load a custom config value that has been saved using #store_custom_config
# File lib/kafo/hook_context.rb, line 84 def get_custom_config(key) self.kafo.config.get_custom(key) end
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"
# File lib/kafo/hook_context.rb, line 26 def logger self.kafo.logger end
Check if a module is enabled in the current configuration. examples:
module_enabled?('example')
# File lib/kafo/hook_context.rb, line 69 def module_enabled?(module_name) self.kafo.module(module_name).enabled? end
examples:
param('foreman', 'interface').value = 'eth0' param('foreman', 'interface').value = app_option('bind_on_interface')
# File lib/kafo/hook_context.rb, line 49 def param(module_name, parameter_name) self.kafo.param(module_name, parameter_name) end
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
# File lib/kafo/hook_context.rb, line 90 def store_custom_config(key, value) self.kafo.config.set_custom(key, value) end