Module: Kitchen::Configurable
- Included in:
- Driver::Base, LifecycleHooks, Provisioner::Base, Transport::Base, Verifier::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
-
.included(base) ⇒ void
private
Extends the including class with ClassMethods so that the
default_config,required_config, and related DSL is available at the class level.
Instance Method Summary collapse
-
#[](attr) ⇒ Object
Provides hash-like access to configuration keys.
-
#bourne_shell? ⇒ TrueClass, FalseClass
True if
:shell_typeis"bourne"(or unset, for backwards compatibility). -
#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.
-
#diagnose_plugin ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information associated with the plugin itself (such as loaded version, class name, etc.).
-
#finalize_config!(instance) ⇒ self
A lifecycle method that should be invoked when the object is about ready to be used.
-
#name ⇒ String
Returns the name of this plugin, suitable for display in a CLI.
-
#powershell_shell? ⇒ TrueClass, FalseClass
True if
:shell_typeis"powershell". -
#remote_path_join(*parts) ⇒ String
Builds a file path based on the
:os_type("windows"or"unix"). -
#unix_os? ⇒ TrueClass, FalseClass
True if
:os_typeis"unix"(or unset, for backwards compatibility). -
#verify_dependencies ⇒ Object
Performs whatever tests that may be required to ensure that this plugin will be able to function in the current environment.
-
#windows_os? ⇒ TrueClass, FalseClass
True if
:os_typeis"windows".
Instance Attribute Details
#instance ⇒ Kitchen::Instance (readonly)
Returns the associated instance.
59 60 61 |
# File 'lib/kitchen/configurable.rb', line 59 def instance @instance end |
Class Method Details
.included(base) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Extends the including class with ClassMethods so that the
default_config, required_config, and related DSL is available at the
class level.
54 55 56 |
# File 'lib/kitchen/configurable.rb', line 54 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#[](attr) ⇒ Object
Provides hash-like access to configuration keys.
86 87 88 |
# File 'lib/kitchen/configurable.rb', line 86 def [](attr) config[attr] end |
#bourne_shell? ⇒ TrueClass, FalseClass
Returns true if :shell_type is "bourne" (or
unset, for backwards compatibility).
92 93 94 95 |
# File 'lib/kitchen/configurable.rb', line 92 def bourne_shell? shell_type = instance.platform.respond_to?(:shell_type) ? instance.platform.shell_type : nil BOURNE_SHELL_TYPE == shell_type || shell_type.nil? 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 segment of "roles" then the following will be tried
in order (first match that exists wins):
- /a/b/server/roles
- /a/b/roles
- $PWD/roles
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/kitchen/configurable.rb', line 116 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 candidate = File.join(base, instance.suite.name, path) return candidate if path_exists?(candidate, type) candidate = File.join(base, path) return candidate if path_exists?(candidate, type) candidate = File.join(Dir.pwd, path) candidate if path_exists?(candidate, type) end |
#config_keys ⇒ Array
Returns an array of configuration keys.
137 138 139 |
# File 'lib/kitchen/configurable.rb', line 137 def config_keys config.keys end |
#diagnose ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information.
144 145 146 147 148 |
# File 'lib/kitchen/configurable.rb', line 144 def diagnose result = {} config_keys.sort.each { |k| result[k] = config[k] } result end |
#diagnose_plugin ⇒ Hash
Returns a Hash of configuration and other useful diagnostic information associated with the plugin itself (such as loaded version, class name, etc.).
155 156 157 158 159 160 |
# File 'lib/kitchen/configurable.rb', line 155 def diagnose_plugin result = {} result[:name] = name result.merge!(self.class.diagnose) 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 dependent data may be accessed 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.
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/kitchen/configurable.rb', line 70 def finalize_config!(instance) raise ClientError, "Instance must be provided to #{self}" if instance.nil? @instance = instance deprecate_config! validate_config! load_needed_dependencies! self end |
#name ⇒ String
Returns the name of this plugin, suitable for display in a CLI.
165 166 167 |
# File 'lib/kitchen/configurable.rb', line 165 def name self.class.name.split("::").last end |
#powershell_shell? ⇒ TrueClass, FalseClass
Returns true if :shell_type is "powershell".
170 171 172 173 |
# File 'lib/kitchen/configurable.rb', line 170 def powershell_shell? shell_type = instance.platform.respond_to?(:shell_type) ? instance.platform.shell_type : nil POWERSHELL_SHELL_TYPE == shell_type end |
#remote_path_join(*parts) ⇒ String
Builds a file path based on the :os_type ("windows" or "unix").
178 179 180 181 |
# File 'lib/kitchen/configurable.rb', line 178 def remote_path_join(*parts) path = File.join(*parts) windows_os? ? path.tr("/", "\\") : path.tr("\\", "/") end |
#unix_os? ⇒ TrueClass, FalseClass
Returns true if :os_type is "unix" (or
unset, for backwards compatibility).
185 186 187 188 |
# File 'lib/kitchen/configurable.rb', line 185 def unix_os? os_type = instance.platform.respond_to?(:os_type) ? instance.platform.os_type : nil UNIX_OS_TYPE == os_type || os_type.nil? end |
#verify_dependencies ⇒ Object
Performs whatever tests that may be required to ensure that this plugin will be able to function in the current environment. This may involve checking for the presence of certain directories, software installed, etc.
197 198 199 |
# File 'lib/kitchen/configurable.rb', line 197 def verify_dependencies # this method may be left unimplemented if that is applicable end |
#windows_os? ⇒ TrueClass, FalseClass
Returns true if :os_type is "windows".
202 203 204 205 |
# File 'lib/kitchen/configurable.rb', line 202 def windows_os? os_type = instance.platform.respond_to?(:os_type) ? instance.platform.os_type : nil WINDOWS_OS_TYPE == os_type end |