Skip to content

Commit

Permalink
Add configurable defaults for chef-solo & chef-client paths.
Browse files Browse the repository at this point in the history
This commit adds 3 new tunables: 1 for all Chef provisioners, 1 for
`chef_solo`, and 1 for `chef_zero` and builds on @sethvargo's previous
commit.

**:chef_omnibus_root** which defaults to `"/opt/chef"` is the location in
which Kitchen will check when determining if a Chef Omnibus package is
installed. Modifying this attribute will allow users to use alternative
Omnibus packages and is most likely always reasonable with its default
value.

**:chef_solo_path** is used with the `chef_solo` provisioner, and is the
full path on the remote instance to the `chef-solo` binary, and by
default is calculated using the `:chef_omnibus_root` attribute as its
base. For example, if the Omnibus root is `"/opt/chef"`, then the
`:chef_solo_path` value will be calculated as
`"/opt/chef/bin/chef-solo"`. If the `chef-solo` command is located
elsewhere, this value can be set in a provisioner block to override the
default. For example, setting this attribute to `"chef-solo"` will
invoke the command without an absolute path and rely on `$PATH` lookup.

**:chef_client_path** is used with the `chef_zero` provisioner, and is
the full path on the remote instance to the `chef-client` binary, and
by default is calculated using the `:chef_omnibus_root` attribute as its
base. For example, if the Omnibus root is `"/opt/chef"`, then the
`:chef_client_path` value will be calculated as
`"/opt/chef/bin/chef-client"`. If the `chef-client` command is located
elsewhere, this value can be set in a provisioner block to override the
default. For example, setting this attribute to `"chef-client"` will
invoke the command without an absolute path an rely on `$PATH` lookup.

Here is a concrete example of overriding `:chef_solo_path` and
`chef_client_path` in a `.kitchen.yml` file:

    ---
    driver:
      name: vagrant

    platforms:
      - name: ubuntu-14.04-solo
        provisioner:
          name: chef_solo
          chef_solo_path: /mnt/alt/bin/chef-solo
      - name: ubuntu-14.04-client
        provisioner:
          name: chef_zero
          chef_client_path: /bin/chef-client

    suites:
      - name: default

**Warning:** previous logic used a non-absolute path to invoke both
`chef-solo` and `chef-zero` which means that this commit has the
potential to cause some Kitchen runs to fail, assuming that the Chef
being invoked before was not actually `/opt/chef/bin/chef-solo` or
`/opt/chef/bin/chef-client`. In this case, users can override the new
computed default with a value of `"chef-solo"` or `"chef-client"` to
exactly preserve previous behavior.
  • Loading branch information
fnichol committed Oct 17, 2014
1 parent e2a508e commit 24f6cab
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 15 deletions.
9 changes: 2 additions & 7 deletions lib/kitchen/provisioner/chef_base.rb
Expand Up @@ -35,7 +35,7 @@ class ChefBase < Base

default_config :require_chef_omnibus, true
default_config :chef_omnibus_url, "https://www.getchef.com/chef/install.sh"
default_config :chef_root, "/opt/chef"
default_config :chef_omnibus_root, "/opt/chef"
default_config :run_list, []
default_config :attributes, {}
default_config :log_file, nil
Expand Down Expand Up @@ -157,7 +157,7 @@ def chef_install_function
install_flags = %w[latest true].include?(version) ? "" : "-v #{version}"

<<-INSTALL.gsub(/^ {10}/, "")
if should_update_chef "#{chef_root}" "#{version}" ; then
if should_update_chef "#{config[:chef_omnibus_root]}" "#{version}" ; then
echo "-----> Installing Chef Omnibus (#{pretty_version})"
do_download #{config[:chef_omnibus_url]} /tmp/install.sh
#{sudo("sh")} /tmp/install.sh #{install_flags}
Expand Down Expand Up @@ -360,11 +360,6 @@ def tmpsitebooks_dir
File.join(sandbox_path, "cookbooks")
end

# @return [Pathname]
def chef_root
@chef_root ||= Pathname.new(config[:chef_root])
end

# Copies a cookbooks/ directory into the sandbox path.
def cp_cookbooks
info("Preparing cookbooks from project directory")
Expand Down
6 changes: 5 additions & 1 deletion lib/kitchen/provisioner/chef_solo.rb
Expand Up @@ -29,6 +29,10 @@ class ChefSolo < ChefBase

default_config :solo_rb, {}

default_config :chef_solo_path do |provisioner|
File.join(provisioner[:chef_omnibus_root], %w[bin chef-solo])
end

# (see Base#create_sandbox)
def create_sandbox
super
Expand All @@ -39,7 +43,7 @@ def create_sandbox
def run_command
level = config[:log_level] == :info ? :auto : config[:log_level]

cmd = sudo(chef_root.join("bin", "chef-solo"))
cmd = sudo(config[:chef_solo_path])
args = [
"--config #{config[:root_path]}/solo.rb",
"--log_level #{level}",
Expand Down
8 changes: 6 additions & 2 deletions lib/kitchen/provisioner/chef_zero.rb
Expand Up @@ -32,6 +32,10 @@ class ChefZero < ChefBase
default_config :json_attributes, true
default_config :chef_zero_port, 8889

default_config :chef_client_path do |provisioner|
File.join(provisioner[:chef_omnibus_root], %w[bin chef-client])
end

# (see Base#create_sandbox)
def create_sandbox
super
Expand Down Expand Up @@ -64,9 +68,9 @@ def prepare_command
# (see Base#run_command)
def run_command
level = config[:log_level] == :info ? :auto : config[:log_level]
chef_client_bin = chef_root.join("bin", "chef-client")
chef_client_bin = sudo(config[:chef_client_path])

cmd = modern? ? "#{sudo(chef_client_bin)} --local-mode" : shim_command
cmd = modern? ? "#{chef_client_bin} --local-mode" : shim_command
args = [
"--config #{config[:root_path]}/client.rb",
"--log_level #{level}",
Expand Down
4 changes: 4 additions & 0 deletions spec/kitchen/provisioner/chef_base_spec.rb
Expand Up @@ -57,6 +57,10 @@ def calculate_path(path, _opts = {})
must_equal "https://www.getchef.com/chef/install.sh"
end

it ":chef_omnibus_root has a default" do
provisioner[:chef_omnibus_root].must_equal "/opt/chef"
end

it ":run_list defaults to an empty array" do
provisioner[:run_list].must_equal []
end
Expand Down
12 changes: 10 additions & 2 deletions spec/kitchen/provisioner/chef_solo_spec.rb
Expand Up @@ -47,6 +47,12 @@
it "sets :solo_rb to an empty Hash" do
provisioner[:solo_rb].must_equal Hash.new
end

it "sets :chef_solo_path to a path using :chef_omnibus_root" do
config[:chef_omnibus_root] = "/nice/place"

provisioner[:chef_solo_path].must_equal "/nice/place/bin/chef-solo"
end
end

describe "#create_sandbox" do
Expand Down Expand Up @@ -229,16 +235,18 @@ def sandbox_path(path)
end

it "uses sudo for chef-solo when configured" do
config[:chef_omnibus_root] = "/c"
config[:sudo] = true

cmd.must_match regexify("sudo -E /opt/chef/bin/chef-solo ", :partial_line)
cmd.must_match regexify("sudo -E /c/bin/chef-solo ", :partial_line)
end

it "does not use sudo for chef-solo when configured" do
config[:chef_omnibus_root] = "/c"
config[:sudo] = false

cmd.must_match regexify("chef-solo ", :partial_line)
cmd.wont_match regexify("sudo -E /opt/chef/bin/chef-solo ", :partial_line)
cmd.wont_match regexify("sudo -E /c/bin/chef-solo ", :partial_line)
end

it "sets config flag on chef-solo" do
Expand Down
14 changes: 11 additions & 3 deletions spec/kitchen/provisioner/chef_zero_spec.rb
Expand Up @@ -48,6 +48,12 @@
provisioner[:client_rb].must_equal Hash.new
end

it "sets :chef_client_path to a path using :chef_omnibus_root" do
config[:chef_omnibus_root] = "/nice/place"

provisioner[:chef_client_path].must_equal "/nice/place/bin/chef-client"
end

it "sets :ruby_bindir to use an Omnibus Ruby" do
provisioner[:ruby_bindir].must_equal "/opt/chef/embedded/bin"
end
Expand Down Expand Up @@ -374,16 +380,18 @@ def sandbox_path(path)
end

it "uses sudo for chef-client when configured" do
config[:chef_omnibus_root] = "/c"
config[:sudo] = true

cmd.must_match regexify("sudo -E /opt/chef/bin/chef-client ", :partial_line)
cmd.must_match regexify("sudo -E /c/bin/chef-client ", :partial_line)
end

it "does not use sudo for chef-client when configured" do
config[:chef_omnibus_root] = "/c"
config[:sudo] = false

cmd.must_match regexify("/opt/chef/bin/chef-client ", :partial_line)
cmd.wont_match regexify("sudo -E /opt/chef/bin/chef-client ", :partial_line)
cmd.must_match regexify("/c/bin/chef-client ", :partial_line)
cmd.wont_match regexify("sudo -E /c/bin/chef-client ", :partial_line)
end

it "sets local mode flag on chef-client" do
Expand Down

0 comments on commit 24f6cab

Please sign in to comment.