Class: Kitchen::Provisioner::Base
- Inherits:
-
Object
- Object
- Kitchen::Provisioner::Base
- Includes:
- Configurable, Logging
- Defined in:
- lib/kitchen/provisioner/base.rb
Overview
Base class for a provisioner.
Instance Attribute Summary
Attributes included from Configurable
Class Method Summary collapse
-
.kitchen_provisioner_api_version(version) ⇒ Object
Sets the API version for this provisioner.
Instance Method Summary collapse
-
#call(state) ⇒ Object
Runs the provisioner on the instance.
-
#cleanup_sandbox ⇒ Object
Deletes the sandbox path.
-
#create_sandbox ⇒ Object
Creates a temporary directory on the local workstation into which provisioner related files and directories can be copied or created.
-
#init_command ⇒ String
Generates a command string which will perform any data initialization or configuration required after the provisioner software is installed but before the sandbox has been transferred to the instance.
-
#initialize(config = {}) ⇒ Base
constructor
Constructs a new provisioner by providing a configuration hash.
-
#install_command ⇒ String
Generates a command string which will install and configure the provisioner software on an instance.
-
#prepare_command ⇒ String
Generates a command string which will perform any commands or configuration required just before the main provisioner run command but after the sandbox has been transferred to the instance.
-
#run_command ⇒ String
Generates a command string which will invoke the main provisioner command on the prepared instance.
-
#sandbox_path ⇒ String
Returns the absolute path to the sandbox directory or raises an exception if
#create_sandbox
has not yet been called.
Methods included from Logging
#banner, #debug, #error, #fatal, #info, #warn
Methods included from Configurable
#[], #bourne_shell?, #calculate_path, #config_keys, #diagnose, #diagnose_plugin, #finalize_config!, included, #name, #powershell_shell?, #remote_path_join, #unix_os?, #verify_dependencies, #windows_os?
Constructor Details
#initialize(config = {}) ⇒ Base
Constructs a new provisioner by providing a configuration hash.
58 59 60 |
# File 'lib/kitchen/provisioner/base.rb', line 58 def initialize(config = {}) init_config(config) end |
Class Method Details
.kitchen_provisioner_api_version(version) ⇒ Object
Sets the API version for this provisioner. If the provisioner does not
set this value, then nil
will be used and reported.
Sets the API version for this provisioner
191 192 193 |
# File 'lib/kitchen/provisioner/base.rb', line 191 def self.kitchen_provisioner_api_version(version) @api_version = version end |
Instance Method Details
#call(state) ⇒ Object
Runs the provisioner on the instance.
rubocop:disable Metrics/AbcSize
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kitchen/provisioner/base.rb', line 67 def call(state) create_sandbox sandbox_dirs = Dir.glob(File.join(sandbox_path, "*")) instance.transport.connection(state) do |conn| conn.execute(install_command) conn.execute(init_command) info("Transferring files to #{instance.to_str}") conn.upload(sandbox_dirs, config[:root_path]) debug("Transfer complete") conn.execute(prepare_command) conn.execute_with_retry( run_command, config[:retry_on_exit_code], config[:max_retries], config[:wait_for_retry] ) end rescue Kitchen::Transport::TransportFailed => ex raise ActionFailed, ex. ensure cleanup_sandbox end |
#cleanup_sandbox ⇒ Object
Deletes the sandbox path. Without calling this method, the sandbox path will persist after the process terminates. In other words, cleanup is explicit. This method is safe to call multiple times.
165 166 167 168 169 170 |
# File 'lib/kitchen/provisioner/base.rb', line 165 def cleanup_sandbox return if sandbox_path.nil? debug("Cleaning up local sandbox in #{sandbox_path}") FileUtils.rmtree(sandbox_path) end |
#create_sandbox ⇒ Object
Creates a temporary directory on the local workstation into which provisioner related files and directories can be copied or created. The contents of this directory will be copied over to the instance before invoking the provisioner's run command. After this method completes, it is expected that the contents of the sandbox is complete and ready for copy to the remote instance.
Note: any subclasses would be well advised to call super first when overriding this method, for example:
143 144 145 146 147 148 |
# File 'lib/kitchen/provisioner/base.rb', line 143 def create_sandbox @sandbox_path = Dir.mktmpdir("#{instance.name}-sandbox-") File.chmod(0755, sandbox_path) info("Preparing files for transfer") debug("Creating local sandbox in #{sandbox_path}") end |
#init_command ⇒ String
Generates a command string which will perform any data initialization
or configuration required after the provisioner software is installed
but before the sandbox has been transferred to the instance. If no work
is required, then nil
will be returned.
105 106 |
# File 'lib/kitchen/provisioner/base.rb', line 105 def init_command end |
#install_command ⇒ String
Generates a command string which will install and configure the
provisioner software on an instance. If no work is required, then nil
will be returned.
96 97 |
# File 'lib/kitchen/provisioner/base.rb', line 96 def install_command end |
#prepare_command ⇒ String
Generates a command string which will perform any commands or
configuration required just before the main provisioner run command but
after the sandbox has been transferred to the instance. If no work is
required, then nil
will be returned.
114 115 |
# File 'lib/kitchen/provisioner/base.rb', line 114 def prepare_command end |
#run_command ⇒ String
Generates a command string which will invoke the main provisioner
command on the prepared instance. If no work is required, then nil
will be returned.
122 123 |
# File 'lib/kitchen/provisioner/base.rb', line 122 def run_command end |
#sandbox_path ⇒ String
Returns the absolute path to the sandbox directory or raises an
exception if #create_sandbox
has not yet been called.
156 157 158 159 160 |
# File 'lib/kitchen/provisioner/base.rb', line 156 def sandbox_path @sandbox_path || (raise ClientError, "Sandbox directory has not yet " \ "been created. Please run #{self.class}#create_sandox before " \ "trying to access the path.") end |