Module: Kitchen::Configurable
- Included in:
- Driver::Base, Provisioner::Base
- Defined in:
- lib/kitchen/configurable.rb
Overview
A mixin for providing configuration-related behavior such as default config (static, computed, inherited), required config, local path expansion, etc.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#instance ⇒ Kitchen::Instance
readonly
The associated instance.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Provides hash-like access to configuration keys.
-
#calculate_path(path, opts = {}) ⇒ String
Find an appropriate path to a file or directory, based on graceful fallback rules or returns nil if path cannot be determined.
-
#config_keys ⇒ Array
Returns an array of configuration keys.
-
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
-
#finalize_config!(instance) ⇒ self
A lifecycle method that should be invoked when the object is about ready to be used.
Instance Attribute Details
#instance ⇒ Kitchen::Instance (readonly)
Returns the associated instance.
37 38 39 |
# File 'lib/kitchen/configurable.rb', line 37 def instance @instance end |
Class Method Details
.included(base) ⇒ Object
32 33 34 |
# File 'lib/kitchen/configurable.rb', line 32 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#[](attr) ⇒ Object
Provides hash-like access to configuration keys.
64 65 66 |
# File 'lib/kitchen/configurable.rb', line 64 def [](attr) config[attr] end |
#calculate_path(path, opts = {}) ⇒ String
Find an appropriate path to a file or directory, based on graceful fallback rules or returns nil if path cannot be determined.
Given an instance with suite named "server"
, a test_base_path
of
"/a/b"
, and a path segement of "roles"
then following will be tried
in order (first match that exists wins):
- /a/b/server/roles
- /a/b/roles
- $PWD/roles
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/kitchen/configurable.rb', line 87 def calculate_path(path, opts = {}) type = opts.fetch(:type, :directory) base = opts.fetch(:base_path) do config.fetch(:test_base_path) do |key| raise UserError, "#{key} is not found in #{self}" end end [ File.join(base, instance.suite.name, path), File.join(base, path), File.join(Dir.pwd, path) ].find do |candidate| type == :directory ? File.directory?(candidate) : File.file?(candidate) end end |
#config_keys ⇒ Array
Returns an array of configuration keys.
107 108 109 |
# File 'lib/kitchen/configurable.rb', line 107 def config_keys config.keys end |
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
114 115 116 117 118 |
# File 'lib/kitchen/configurable.rb', line 114 def diagnose result = Hash.new config_keys.sort.each { |k| result[k] = config[k] } result end |
#finalize_config!(instance) ⇒ self
A lifecycle method that should be invoked when the object is about ready to be used. A reference to an Instance is required as configuration dependant data may be access through an Instance. This also acts as a hook point where the object may wish to perform other last minute checks, validations, or configuration expansions.
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/kitchen/configurable.rb', line 48 def finalize_config!(instance) if instance.nil? raise ClientError, "Instance must be provided to #{self}" end @instance = instance validate_config! self end |