class Kitchen::Provisioner::Chef::Berkshelf

Chef cookbook resolver that uses Berkshelf and a Berksfile to calculate dependencies.

@author Fletcher Nichol <fnichol@nichol.ca>

Attributes

always_update[R]

@return [Boolean] If true, always update cookbooks in Berkshelf. @api private

berksfile[R]

@return [String] path to a Berksfile @api private

logger[R]

@return [Kitchen::Logger] a logger to use for output @api private

path[R]

@return [String] path in which to vendor the resulting cookbooks @api private

Public Class Methods

load!(logger: Kitchen.logger) click to toggle source

Loads the library code required to use the resolver.

@param logger [Kitchen::Logger] a logger to use for output, defaults

to `Kitchen.logger`
# File lib/kitchen/provisioner/chef/berkshelf.rb, line 49
def self.load!(logger: Kitchen.logger)
  load_berkshelf!(logger)
end
new(berksfile, path, logger: Kitchen.logger, always_update: false) click to toggle source

Creates a new cookbook resolver.

@param berksfile [String] path to a Berksfile @param path [String] path in which to vendor the resulting

cookbooks

@param logger [Kitchen::Logger] a logger to use for output, defaults

to `Kitchen.logger`
# File lib/kitchen/provisioner/chef/berkshelf.rb, line 38
def initialize(berksfile, path, logger: Kitchen.logger, always_update: false)
  @berksfile  = berksfile
  @path       = path
  @logger     = logger
  @always_update = always_update
end

Private Class Methods

load_berkshelf!(logger) click to toggle source

Load the Berkshelf-specific libary code.

@param logger [Kitchen::Logger] the logger to use @raise [UserError] if the library couldn't be loaded @api private

# File lib/kitchen/provisioner/chef/berkshelf.rb, line 95
def load_berkshelf!(logger)
  first_load = require "berkshelf"

  version = ::Berkshelf::VERSION
  if first_load
    logger.debug("Berkshelf #{version} library loaded")
  else
    logger.debug("Berkshelf #{version} previously loaded")
  end
rescue LoadError => e
  logger.fatal("The `berkshelf' gem is missing and must be installed" \
    " or cannot be properly activated. Run" \
    " `gem install berkshelf` or add the following to your" \
    " Gemfile if you are using Bundler: `gem 'berkshelf'`.")
  raise UserError,
    "Could not load or activate Berkshelf (#{e.message})"
end

Public Instance Methods

resolve() click to toggle source

Performs the cookbook resolution and vendors the resulting cookbooks in the desired path.

# File lib/kitchen/provisioner/chef/berkshelf.rb, line 55
def resolve
  version = ::Berkshelf::VERSION
  info("Resolving cookbook dependencies with Berkshelf #{version}...")
  debug("Using Berksfile from #{berksfile}")

  ::Berkshelf.ui.mute do
    berksfile_obj = ::Berkshelf::Berksfile.from_file(berksfile)
    berksfile_obj.update if always_update && berksfile_obj.lockfile.present?
    # Berkshelf requires the directory to not exist
    FileUtils.rm_rf(path)
    berksfile_obj.vendor(path)
  end
end