Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Adds ability to configure chef destination directory (/etc/chef) #43

Merged
2 commits merged into from
Dec 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,13 @@ By default, Gusteau uploads and sets Chef Solo up to use cookbooks from `./cookb
cookbooks_path: [ './my-cookbooks', '../something-else' ]
roles_path: './base-roles'
```

### Custom Chef run configuration directory (e.g. `/etc/chef`)

By default, Gusteau uploads the necessary files and folders (i.e. cookbooks and roles directories) to `/etc/chef/`.

You can specify a custom target directory in `.gusteau.yml`:

```
chef_config_dir: /etc/custom_chef_dir
```
6 changes: 0 additions & 6 deletions bootstrap/solo.rb

This file was deleted.

7 changes: 7 additions & 0 deletions bootstrap/solo.rb.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% dest_dir = Gusteau::Config.settings['chef_config_dir'] %>
file_cache_path "/tmp/chef"
cookbook_path *Dir.glob("<%= dest_dir %>/cookbooks-*")
role_path "<%= dest_dir %>/roles"
data_bag_path "<%= dest_dir %>/data_bags"

log_level :info
25 changes: 18 additions & 7 deletions lib/gusteau/chef.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
require 'gusteau/erb'

module Gusteau
class Chef
include Gusteau::ERB

def initialize(server, platform = nil, dest_dir = '/etc/chef')
def initialize(server, platform = nil)
@server = server
@platform = platform || 'omnibus'
@dest_dir = dest_dir
end

def run(dna, opts)
@server.run "rm -rf #{@dest_dir} && mkdir #{@dest_dir} && mkdir -p /tmp/chef"
dest_dir = Gusteau::Config.settings['chef_config_dir']
@server.run "rm -rf #{dest_dir} && mkdir #{dest_dir} && mkdir -p /tmp/chef"

with_gusteau_dir(dna[:path]) do |dir|
@server.upload [dir], @dest_dir, :exclude => '.git/', :strip_c => 2
@server.upload [dir], dest_dir, :exclude => '.git/', :strip_c => 2
end

@server.run "sh /etc/chef/bootstrap.sh #{Gusteau::Config.settings['chef_version']}" if opts['bootstrap']

cmd = "unset GEM_HOME; unset GEM_PATH; chef-solo -c #{@dest_dir}/solo.rb -j #{@dest_dir}/dna.json --color"
cmd = "unset GEM_HOME; unset GEM_PATH; chef-solo -c #{dest_dir}/solo.rb -j #{dest_dir}/dna.json --color"
cmd << " -F #{opts['format']}" if opts['format']
cmd << " -l #{opts['log_level']}" if opts['log_level']
cmd << " -W" if opts['why-run']
Expand All @@ -32,7 +35,7 @@ def files_list(dna_path)
{
dna_path => "dna.json",
bootstrap => "bootstrap.sh",
"#{bootstrap_dir}/solo.rb" => "solo.rb",
"#{bootstrap_dir}/solo.rb.erb" => "solo.rb",
'data_bags' => "data_bags",
Gusteau::Config.settings['roles_path'] => "roles"
}.tap do |f|
Expand All @@ -47,7 +50,15 @@ def with_gusteau_dir(dna_path)
FileUtils.mkdir_p(tmp_dir)

files_list(dna_path).each_pair do |src, dest|
FileUtils.cp_r(src, "#{tmp_dir}/#{dest}") if File.exists?(src)
if File.exists?(src)
if File.extname(src) == '.erb'
File.open("#{tmp_dir}/#{dest}", "w" ) do |f|
f.write read_erb(src)
end
else
FileUtils.cp_r(src, "#{tmp_dir}/#{dest}")
end
end
end

yield tmp_dir
Expand Down
10 changes: 6 additions & 4 deletions lib/gusteau/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Gusteau
class Config
DEFAULT_CHEF_VERSION = '11.4.4'
DEFAULT_CHEF_CONFIG_DIRECTORY = '/etc/chef'

include Gusteau::ERB

Expand Down Expand Up @@ -45,10 +46,11 @@ def nodes

def settings
{
'cookbooks_path' => @config['cookbooks_path'] || ['cookbooks', 'site-cookbooks'],
'roles_path' => @config['roles_path'] || 'roles',
'bootstrap' => @config['bootstrap'],
'chef_version' => @config['chef_version'] || DEFAULT_CHEF_VERSION
'cookbooks_path' => @config['cookbooks_path'] || ['cookbooks', 'site-cookbooks'],
'roles_path' => @config['roles_path'] || 'roles',
'bootstrap' => @config['bootstrap'],
'chef_version' => @config['chef_version'] || DEFAULT_CHEF_VERSION,
'chef_config_dir' => @config['chef_config_dir'] || DEFAULT_CHEF_CONFIG_DIRECTORY
}
end

Expand Down
2 changes: 2 additions & 0 deletions spec/config/emile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ roles_path: basic-roles
bootstrap: ./bootstrap/osx.sh

chef_version: 10.26.0

chef_config_dir: /etc/custom_chef_dir
25 changes: 25 additions & 0 deletions spec/lib/gusteau/chef_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ def expects_run_chef_solo
chef.run({ :path => '/tmp/node.json' }, opts)
end
end

it "removes and creates the chef configuration directory" do
server.expects(:run).with("rm -rf /etc/custom_chef_dir && mkdir /etc/custom_chef_dir && mkdir -p /tmp/chef")
chef.run({ :path => '/tmp/node.json' }, opts)
end
end

describe "#files_list" do
Expand All @@ -48,4 +53,24 @@ def expects_run_chef_solo
subject['./bootstrap/osx.sh'].must_equal 'bootstrap.sh'
end
end

describe "#with_gusteau_dir" do
it "copies files to the tmp directory" do
File.stubs(:exists?).returns(true)
chef.send(:files_list, '/some/dna.json').each_pair do |src, dest|
# Expect to copy the files listed by #files_list (excpet for .erb)
FileUtils.expects(:cp_r).with(src, regexp_matches( %r{^/tmp/gusteau-\d{10}/#{dest}$} )) unless File.extname(src) == '.erb'
end

chef.send(:with_gusteau_dir, '/some/dna.json') {}
end

it "process the solo.rb.erb with ERB template and saves the result to the tmp directory" do
file_mock = mock
file_mock.expects(:write).with(regexp_matches( %r{/etc/custom_chef_dir} )) # Expect the value from emile.yml:chef_config_dir
File.expects(:open).with(regexp_matches( %r{^/tmp/gusteau-\d{10}/solo.rb$} ),"w").yields(file_mock)

chef.send(:with_gusteau_dir, '/some/dna.json') {}
end
end
end
4 changes: 3 additions & 1 deletion spec/lib/gusteau/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@
describe "#settings" do
let(:settings) { Gusteau::Config.settings }

it "should have defaults for cookbooks_path, roles_path, bootstrap, chef_version" do
it "should have defaults for cookbooks_path, roles_path, bootstrap, chef_version, chef_config_dir" do
settings['cookbooks_path'].must_equal ['cookbooks', 'site-cookbooks']
settings['roles_path'].must_equal 'roles'
settings['bootstrap'].must_equal nil
settings['chef_version'].must_equal Gusteau::Config::DEFAULT_CHEF_VERSION
settings['chef_config_dir'].must_equal Gusteau::Config::DEFAULT_CHEF_CONFIG_DIRECTORY
end

context "settings defined in the config yml" do
Expand All @@ -55,6 +56,7 @@
settings['cookbooks_path'].must_equal ['private-cookbooks', '/home/user/.cookbooks']
settings['roles_path'].must_equal 'basic-roles'
settings['chef_version'].must_equal '10.26.0'
settings['chef_config_dir'].must_equal '/etc/custom_chef_dir'
end
end
end
Expand Down