class Kitchen::LifecycleHooks

A helper object used by {Instance} to coordinate lifecycle hook calls from the `lifecycle:` configuration section.

@api internal @since 1.22

Attributes

state_file[R]

@return [Kitchen::StateFile]

Public Class Methods

new(config, state_file) click to toggle source
# File lib/kitchen/lifecycle_hooks.rb, line 34
def initialize(config, state_file)
  init_config(config)
  @state_file = state_file
end

Public Instance Methods

run_with_hooks(phase, state_file) { || ... } click to toggle source

Run a lifecycle phase with the pre and post hooks.

@param phase [String] Lifecycle phase which is being executed. @param state_file [StateFile] Instance state file object. @param block [Proc] Block of code implementing the lifecycle phase. @return [void]

# File lib/kitchen/lifecycle_hooks.rb, line 45
def run_with_hooks(phase, state_file, &block)
  run(phase, :pre)
  yield
  run(phase, :post)
end

Private Instance Methods

generate_hook(phase, hook) click to toggle source

@param phase [String] @param hook [Hash] @return [Kitchen::LifecycleHook::Local, Kitchen::LifecycleHook::Remote]

# File lib/kitchen/lifecycle_hooks.rb, line 80
def generate_hook(phase, hook)
  if hook.include?(:local)
    # Local command execution on the workstation.
    Kitchen::LifecycleHook::Local.new(self, phase, hook)
  elsif hook.include?(:remote)
    # Remote command execution on the test instance.
    Kitchen::LifecycleHook::Remote.new(self, phase, hook)
  else
    raise UserError, "Unknown lifecycle hook target #{hook.inspect}"
  end
end
run(phase, hook_timing) click to toggle source

Execute a specific lifecycle hook.

@param phase [String] Lifecycle phase which is being executed. @param hook_timing [Symbol] `:pre` or `:post` to indicate which hook to run. @return [void]

# File lib/kitchen/lifecycle_hooks.rb, line 61
def run(phase, hook_timing)
  # Yes this has to be a symbol because of how data munger works.
  hook_key = :"#{hook_timing}_#{phase}"
  # No hooks? We're outta here.
  hook_data = Array(config[hook_key])
  return if hook_data.empty?

  hook_data.each do |hook|
    # Coerce the common case of a bare string to be a local command. This
    # is to match the behavior of the old `pre_create_command` semi-hook.
    hook = { local: hook } if hook.is_a?(String)
    hook = generate_hook(phase, hook)
    hook.run if hook.should_run?
  end
end