From 9dcdf28cfa06426db1daa10c6d78f664d92fc870 Mon Sep 17 00:00:00 2001 From: Omer Gazit Date: Mon, 2 Dec 2013 23:27:49 +0200 Subject: [PATCH 1/2] Adds ability to configure chef destination directory Allow to run chef-solo with a config directory other than '/etc/chef' --- bootstrap/solo.rb | 6 ------ bootstrap/solo.rb.erb | 7 +++++++ lib/gusteau/chef.rb | 25 ++++++++++++++++++------- lib/gusteau/config.rb | 10 ++++++---- spec/config/emile.yml | 2 ++ spec/lib/gusteau/chef_spec.rb | 25 +++++++++++++++++++++++++ spec/lib/gusteau/config_spec.rb | 4 +++- 7 files changed, 61 insertions(+), 18 deletions(-) delete mode 100644 bootstrap/solo.rb create mode 100644 bootstrap/solo.rb.erb diff --git a/bootstrap/solo.rb b/bootstrap/solo.rb deleted file mode 100644 index 344f3fe..0000000 --- a/bootstrap/solo.rb +++ /dev/null @@ -1,6 +0,0 @@ -file_cache_path "/tmp/chef" -cookbook_path *Dir.glob("/etc/chef/cookbooks-*") -role_path "/etc/chef/roles" -data_bag_path "/etc/chef/data_bags" - -log_level :info diff --git a/bootstrap/solo.rb.erb b/bootstrap/solo.rb.erb new file mode 100644 index 0000000..47814e1 --- /dev/null +++ b/bootstrap/solo.rb.erb @@ -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 diff --git a/lib/gusteau/chef.rb b/lib/gusteau/chef.rb index 0c909ca..ff055cc 100644 --- a/lib/gusteau/chef.rb +++ b/lib/gusteau/chef.rb @@ -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'] @@ -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| @@ -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 diff --git a/lib/gusteau/config.rb b/lib/gusteau/config.rb index 6a762e7..59f430b 100644 --- a/lib/gusteau/config.rb +++ b/lib/gusteau/config.rb @@ -4,6 +4,7 @@ module Gusteau class Config DEFAULT_CHEF_VERSION = '11.4.4' + DEFAULT_CHEF_CONFIG_DIRECTORY = '/etc/chef' include Gusteau::ERB @@ -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 diff --git a/spec/config/emile.yml b/spec/config/emile.yml index 451d05f..1e6d784 100644 --- a/spec/config/emile.yml +++ b/spec/config/emile.yml @@ -7,3 +7,5 @@ roles_path: basic-roles bootstrap: ./bootstrap/osx.sh chef_version: 10.26.0 + +chef_config_dir: /etc/custom_chef_dir diff --git a/spec/lib/gusteau/chef_spec.rb b/spec/lib/gusteau/chef_spec.rb index 2d2e8df..4f580f9 100644 --- a/spec/lib/gusteau/chef_spec.rb +++ b/spec/lib/gusteau/chef_spec.rb @@ -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 @@ -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 diff --git a/spec/lib/gusteau/config_spec.rb b/spec/lib/gusteau/config_spec.rb index c9503aa..9dbbff1 100644 --- a/spec/lib/gusteau/config_spec.rb +++ b/spec/lib/gusteau/config_spec.rb @@ -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 @@ -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 From 9777ab5c5df0877470421e764dcbd6f1491c34c2 Mon Sep 17 00:00:00 2001 From: Omer Gazit Date: Wed, 4 Dec 2013 13:51:37 +0200 Subject: [PATCH 2/2] Update README.md with custom Chef directory instructions --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index 376c074..609e22f 100644 --- a/README.md +++ b/README.md @@ -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 +```