class Kitchen::Provisioner::PolicyfileZero

@author Daniel DeLeo <dan@chef.io>
Policyfile + Chef Zero provisioner.

def create_sandbox

(see Base#create_sandbox)
def create_sandbox
  create_sandbox_directory
  PolicyfileSandbox.new(config, sandbox_path, instance).populate
  prepare_cookbooks
  prepare_validation_pem
  prepare_client_rb
end

def finalize_config!(*args)

(see Base#finalize_config!)
def finalize_config!(*args)
  super
  banner("Using policyfile mode for chef-client")
end

def load_needed_dependencies!

dependencies, so these can only cause trouble.
We don't want to load Berkshelf or Librarian; Policyfile is managing
def load_needed_dependencies!
end

def policy_exporter

Other tags:
    Api: - private
def policy_exporter
  # Must force this because TK by default copies the current cookbook to the sandbox
  # See ChefCLI::PolicyfileServices::ExportRepo#assert_export_dir_clean!
  @policy_exporter ||= ChefCLI::PolicyfileServices::ExportRepo.new(policyfile: config[:policyfile],
                                                                  export_dir: sandbox_path,
                                                                  force: true)
end

def prepare_client_rb

Other tags:
    Api: - private
def prepare_client_rb
  data = default_config_rb.merge(config[:client_rb])
  data["use_policyfile"] = true
  data["versioned_cookbooks"] = true
  data["policy_name"] = policy_exporter.policy_name
  data["policy_group"] = "local"
  data["policy_document_native_api"] = true
  info("Preparing client.rb")
  debug("Creating client.rb from #{data.inspect}")
  File.open(File.join(sandbox_path, "client.rb"), "wb") do |file|
    file.write(format_config_file(data))
  end
end

def prepare_cookbooks

Other tags:
    Api: - private
def prepare_cookbooks
  Kitchen.mutex.synchronize do
    policy_exporter.run
  end
end

def prepare_json

Other tags:
    Api: - private
def prepare_json
end

def prepare_validation_pem

Other tags:
    Api: - private
def prepare_validation_pem
  info("Preparing validation.pem")
  debug("Using a dummy validation.pem")
  source = File.join(Kitchen.source_root, %w{support dummy-validation.pem})
  FileUtils.cp(source, File.join(sandbox_path, "validation.pem"))
end

def run_command

(see Base#run_command)
def run_command
  level = config[:log_level] == :info ? :auto : config[:log_level]
  cmd = "#{sudo(config[:chef_client_path])} --local-mode"
    .tap { |str| str.insert(0, "& ") if powershell_shell? }
  args = [
    "--config #{config[:root_path]}/client.rb",
    "--log_level #{level}",
    "--force-formatter",
    "--no-color",
  ]
  if config[:chef_zero_port]
    args << "--chef-zero-port #{config[:chef_zero_port]}"
  end
  if config[:log_file]
    args << "--logfile #{config[:log_file]}"
  end
  if config[:named_run_list]
    args << "--named-run-list #{config[:named_run_list]}"
  end
  wrap_shell_code(
    [cmd, *args].join(" ")
    .tap { |str| str.insert(0, reload_ps1_path) if windows_os? }
  )
end