Class: Kitchen::Provisioner::Chef::Policyfile

Inherits:
Object
  • Object
show all
Includes:
Logging, ShellOut
Defined in:
lib/kitchen/provisioner/chef/policyfile.rb

Overview

Chef cookbook resolver that uses Policyfiles to calculate dependencies.

Author:

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellOut

#run_command

Methods included from Logging

#banner, #debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(policyfile, path, logger: Kitchen.logger, always_update: false) ⇒ Policyfile

Creates a new cookbook resolver.

Parameters:

  • policyfile (String)

    path to a Policyfile

  • path (String)

    path in which to vendor the resulting cookbooks

  • logger (Kitchen::Logger) (defaults to: Kitchen.logger)

    a logger to use for output, defaults to `Kitchen.logger`


42
43
44
45
46
47
# File 'lib/kitchen/provisioner/chef/policyfile.rb', line 42

def initialize(policyfile, path, logger: Kitchen.logger, always_update: false)
  @policyfile = policyfile
  @path       = path
  @logger     = logger
  @always_update = always_update
end

Class Method Details

.load!(logger: Kitchen.logger) ⇒ Object

Loads the library code required to use the resolver.

Parameters:

  • logger (Kitchen::Logger) (defaults to: Kitchen.logger)

    a logger to use for output, defaults to `Kitchen.logger`


53
54
55
# File 'lib/kitchen/provisioner/chef/policyfile.rb', line 53

def self.load!(logger: Kitchen.logger)
  detect_chef_command!(logger)
end

Instance Method Details

#compileObject

Runs `chef install` to determine the correct cookbook set and generate the policyfile lock.


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/kitchen/provisioner/chef/policyfile.rb', line 66

def compile
  if File.exist?(lockfile)
    info("Installing cookbooks for Policyfile #{policyfile} using `chef install`")
  else
    info("Policy lock file doesn't exist, running `chef install` for "\
         "Policyfile #{policyfile}...")
  end
  run_command("chef install #{escape_path(policyfile)}")

  if always_update
    info("Updating policy lock using `chef update`")
    run_command("chef update #{escape_path(policyfile)}")
  end
end

#lockfileString

Return the path to the lockfile corresponding to this policyfile.

Returns:

  • (String)

84
85
86
# File 'lib/kitchen/provisioner/chef/policyfile.rb', line 84

def lockfile
  policyfile.gsub(/\.rb\Z/, ".lock.json")
end

#resolveObject

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


59
60
61
62
# File 'lib/kitchen/provisioner/chef/policyfile.rb', line 59

def resolve
  info("Exporting cookbook dependencies from Policyfile #{path}...")
  run_command("chef export #{escape_path(policyfile)} #{escape_path(path)} --force")
end