Class: Kitchen::Provisioner::ChefApply

Inherits:
ChefBase show all
Defined in:
lib/kitchen/provisioner/chef_apply.rb

Overview

Chef Apply provisioner.

Author:

Instance Attribute Summary

Attributes included from Configurable

#instance

Instance Method Summary collapse

Methods inherited from ChefBase

#check_license, #doctor, #initialize, #install_command, #license_acceptance_id, #product_version

Methods inherited from Base

#call, #check_license, #cleanup_sandbox, #doctor, #initialize, #install_command, kitchen_provisioner_api_version, #prepare_command, #sandbox_dirs, #sandbox_path

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?

Methods inherited from Kitchen::Plugin::Base

no_parallel_for

Constructor Details

This class inherits a constructor from Kitchen::Provisioner::ChefBase

Instance Method Details

#create_sandboxObject

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:

Examples:

overriding ‘#create_sandbox`


class MyProvisioner < Kitchen::Provisioner::Base
  def create_sandbox
    super
    # any further file copies, preparations, etc.
  end
end


71
72
73
74
75
76
77
78
79
# File 'lib/kitchen/provisioner/chef_apply.rb', line 71

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}")

  prepare_json
  prepare(:apply)
end

#init_commandString

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.

Returns:

  • (String)

    a command string



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kitchen/provisioner/chef_apply.rb', line 82

def init_command
  dirs = %w{
    apply
  }.sort.map { |dir| remote_path_join(config[:root_path], dir) }

  vars = if powershell_shell?
           init_command_vars_for_powershell(dirs)
         else
           init_command_vars_for_bourne(dirs)
         end

  prefix_command(shell_code_from_file(vars, "chef_base_init_command"))
end

#run_commandString

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.

Returns:

  • (String)

    a command string



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/kitchen/provisioner/chef_apply.rb', line 97

def run_command
  level = config[:log_level]
  lines = []
  config[:run_list].map do |recipe|
    cmd = sudo(config[:chef_apply_path]).dup
      .tap { |str| str.insert(0, "& ") if powershell_shell? }
    args = [
      "apply/#{recipe}.rb",
      "--log_level #{level}",
      "--no-color",
    ]
    args << "--logfile #{config[:log_file]}" if config[:log_file]
    args << "--chef-license #{config[:chef_license]}" if config[:chef_license]

    lines << wrap_shell_code(
      [cmd, *args].join(" ")
      .tap { |str| str.insert(0, reload_ps1_path) if windows_os? }
    )
  end

  prefix_command(lines.join("\n"))
end