Class: Kafo::HookContext
- Inherits:
-
BaseContext
- Object
- BaseContext
- Kafo::HookContext
- Defined in:
- lib/kafo/hook_context.rb
Instance Attribute Summary collapse
- #kafo ⇒ Kafo::KafoConfigure readonly
-
#logger ⇒ Kafo::Logger
readonly
some of hooks won’t print any message because logger is not yet configured configuration of logger depends on application configration (log level etc.).
Class Method Summary collapse
Instance Method Summary collapse
-
#add_module(module_name, mapping = nil) ⇒ Object
You can add custom modules not explicitly enabled in answer file.
-
#app_option(*args, &block) ⇒ Clamp::Option
if you want to add new app_option be sure to do as soon as possible (usually boot hook) otherwise it may be too late (e.g. when displaying help).
-
#app_option?(option) ⇒ Boolean
Returns whether the given app option exists.
-
#app_value(option) ⇒ Object
note the dash to underscore convention.
-
#exit(code) ⇒ Object
You can trigger installer exit by this method.
-
#exit_code ⇒ Object
Return the current exit code.
-
#get_custom_config(key) ⇒ Object
You can load a custom config value that has been saved using store_custom_config.
-
#get_custom_fact(key) ⇒ Object
Load a custom fact from the custom fact storage as saved by store_custom_fact.
-
#has_custom_fact?(key) ⇒ Boolean
Check whether a custom fact exists, regardless of whether or not it has a value.
-
#initialize(kafo, logger) ⇒ HookContext
constructor
A new instance of HookContext.
-
#module_enabled?(module_name) ⇒ Boolean
Check if a module is enabled in the current configuration.
-
#module_present?(module_name) ⇒ Boolean
Check if a module is present in the current configuration.
-
#param(module_name, parameter_name) ⇒ Kafo::Param?
Return the parameter of a module.
-
#puppet_report ⇒ Optional[Kafo::PuppetReport]
Return the Puppet report, if any.
-
#scenario_data ⇒ Hash
Return the actual data in the current scenario.
-
#scenario_id ⇒ String
Return the id of the current scenario.
-
#scenario_path ⇒ String
Return the path to the current scenario.
-
#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.
-
#store_custom_fact(key, value) ⇒ Object
Store any custom fact.
Methods inherited from BaseContext
Constructor Details
#initialize(kafo, logger) ⇒ HookContext
Returns a new instance of HookContext.
22 23 24 25 |
# File 'lib/kafo/hook_context.rb', line 22 def initialize(kafo, logger) @kafo = kafo @logger = logger end |
Instance Attribute Details
#kafo ⇒ Kafo::KafoConfigure (readonly)
7 8 9 |
# File 'lib/kafo/hook_context.rb', line 7 def kafo @kafo end |
#logger ⇒ Kafo::Logger (readonly)
some of hooks won’t print any message because logger is not yet configured configuration of logger depends on application configration (log level etc.)
16 17 18 |
# File 'lib/kafo/hook_context.rb', line 16 def logger @logger end |
Class Method Details
.execute(kafo, logger, &hook) ⇒ Object
18 19 20 |
# File 'lib/kafo/hook_context.rb', line 18 def self.execute(kafo, logger, &hook) new(kafo, logger).instance_eval(&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.
90 91 92 93 |
# File 'lib/kafo/hook_context.rb', line 90 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, &block) ⇒ Clamp::Option
if you want to add new app_option be sure to do as soon as possible (usually boot hook) otherwise it may be too late (e.g. when displaying help)
37 38 39 |
# File 'lib/kafo/hook_context.rb', line 37 def app_option(*args, &block) self.kafo.class.app_option(*args, &block) end |
#app_option?(option) ⇒ Boolean
Returns whether the given app option exists. This is useful when there’s a conditional option that is determined during boot; this helper can be used in later hooks to determine whether the option exists.
45 46 47 |
# File 'lib/kafo/hook_context.rb', line 45 def app_option?(option) self.kafo.config.app.key?(option.to_sym) end |
#app_value(option) ⇒ Object
note the dash to underscore convention
55 56 57 |
# File 'lib/kafo/hook_context.rb', line 55 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).
128 129 130 |
# File 'lib/kafo/hook_context.rb', line 128 def exit(code) self.kafo.class.exit(code) end |
#exit_code ⇒ Object
Return the current exit code
196 197 198 |
# File 'lib/kafo/hook_context.rb', line 196 def exit_code self.kafo.exit_code end |
#get_custom_config(key) ⇒ Object
You can load a custom config value that has been saved using store_custom_config
135 136 137 |
# File 'lib/kafo/hook_context.rb', line 135 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
151 152 153 |
# File 'lib/kafo/hook_context.rb', line 151 def get_custom_fact(key) self.kafo.config.get_custom_fact(key) end |
#has_custom_fact?(key) ⇒ Boolean
Check whether a custom fact exists, regardless of whether or not it has a value.
170 171 172 |
# File 'lib/kafo/hook_context.rb', line 170 def has_custom_fact?(key) self.kafo.config.has_custom_fact?(key) end |
#module_enabled?(module_name) ⇒ Boolean
Check if a module is enabled in the current configuration.
101 102 103 104 |
# File 'lib/kafo/hook_context.rb', line 101 def module_enabled?(module_name) mod = self.kafo.module(module_name) !mod.nil? && mod.enabled? end |
#module_present?(module_name) ⇒ Boolean
Check if a module is present in the current configuration.
112 113 114 115 |
# File 'lib/kafo/hook_context.rb', line 112 def module_present?(module_name) mod = self.kafo.module(module_name) !mod.nil? end |
#param(module_name, parameter_name) ⇒ Kafo::Param?
Return the parameter of a module. Note that the module may not actually be enabled.
72 73 74 |
# File 'lib/kafo/hook_context.rb', line 72 def param(module_name, parameter_name) self.kafo.param(module_name, parameter_name) end |
#puppet_report ⇒ Optional[Kafo::PuppetReport]
Return the Puppet report, if any. Only available after Puppet actual ran.
204 205 206 |
# File 'lib/kafo/hook_context.rb', line 204 def puppet_report self.kafo.puppet_report end |
#scenario_data ⇒ Hash
Return the actual data in the current scenario
191 192 193 |
# File 'lib/kafo/hook_context.rb', line 191 def scenario_data self.kafo.config.app end |
#scenario_id ⇒ String
Return the id of the current scenario
177 178 179 |
# File 'lib/kafo/hook_context.rb', line 177 def scenario_id self.kafo.config.scenario_id end |
#scenario_path ⇒ String
Return the path to the current scenario
184 185 186 |
# File 'lib/kafo/hook_context.rb', line 184 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
144 145 146 |
# File 'lib/kafo/hook_context.rb', line 144 def store_custom_config(key, value) self.kafo.config.set_custom(key, value) end |
#store_custom_fact(key, value) ⇒ Object
Store 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.
163 164 165 |
# File 'lib/kafo/hook_context.rb', line 163 def store_custom_fact(key, value) self.kafo.config.set_custom_fact(key, value) end |