diff --git a/lib/vagrant/provisioners/chef.rb b/lib/vagrant/provisioners/chef.rb index 675a450d1..09f38193d 100644 --- a/lib/vagrant/provisioners/chef.rb +++ b/lib/vagrant/provisioners/chef.rb @@ -18,6 +18,7 @@ module Vagrant # Shared config attr_accessor :provisioning_path + attr_accessor :log_level attr_accessor :json def initialize @@ -27,6 +28,7 @@ module Vagrant @cookbooks_path = "cookbooks" @provisioning_path = "/tmp/vagrant-chef" + @log_level = :info @json = { :instance_role => "vagrant", :run_list => ["recipe[vagrant_main]"] @@ -79,6 +81,15 @@ module Vagrant end end + def setup_config(template, filename, template_vars) + config_file = TemplateRenderer.render(template, { + :log_level => env.config.chef.log_level.to_sym + }.merge(template_vars)) + + logger.info "Uploading chef configuration script..." + env.ssh.upload!(StringIO.new(config_file), File.join(env.config.chef.provisioning_path, filename)) + end + def setup_json logger.info "Generating chef JSON and uploading..." diff --git a/lib/vagrant/provisioners/chef_server.rb b/lib/vagrant/provisioners/chef_server.rb index 595b8e55d..6ae70b221 100644 --- a/lib/vagrant/provisioners/chef_server.rb +++ b/lib/vagrant/provisioners/chef_server.rb @@ -20,7 +20,7 @@ module Vagrant create_client_key_folder upload_validation_key setup_json - setup_config + setup_server_config run_chef_client end @@ -38,17 +38,14 @@ module Vagrant env.ssh.upload!(validation_key_path, guest_validation_key_path) end - def setup_config - solo_file = TemplateRenderer.render("chef_server_client", { + def setup_server_config + setup_config("chef_server_client", "client.rb", { :node_name => env.config.chef.node_name, :chef_server_url => env.config.chef.chef_server_url, :validation_client_name => env.config.chef.validation_client_name, :validation_key => guest_validation_key_path, :client_key => env.config.chef.client_key_path }) - - logger.info "Uploading chef-client configuration script..." - env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "client.rb")) end def run_chef_client diff --git a/lib/vagrant/provisioners/chef_solo.rb b/lib/vagrant/provisioners/chef_solo.rb index 35c185a49..e3b905c56 100644 --- a/lib/vagrant/provisioners/chef_solo.rb +++ b/lib/vagrant/provisioners/chef_solo.rb @@ -20,13 +20,10 @@ module Vagrant end def setup_solo_config - solo_file = TemplateRenderer.render("chef_solo_solo", { + setup_config("chef_solo_solo", "solo.rb", { :provisioning_path => env.config.chef.provisioning_path, :cookbooks_path => cookbooks_path }) - - logger.info "Uploading chef-solo configuration script..." - env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "solo.rb")) end def run_chef_solo diff --git a/templates/chef_server_client.erb b/templates/chef_server_client.erb index 0c5f12b94..94fd11965 100644 --- a/templates/chef_server_client.erb +++ b/templates/chef_server_client.erb @@ -1,4 +1,4 @@ -log_level :info +log_level <%= log_level.inspect %> log_location STDOUT node_name "<%= node_name %>" ssl_verify_mode :verify_none diff --git a/templates/chef_solo_solo.erb b/templates/chef_solo_solo.erb index a615a2960..55fbad275 100644 --- a/templates/chef_solo_solo.erb +++ b/templates/chef_solo_solo.erb @@ -1,2 +1,3 @@ file_cache_path "<%= provisioning_path %>" cookbook_path <%= cookbooks_path %> +log_level <%= log_level.inspect %> \ No newline at end of file diff --git a/test/test_helper.rb b/test/test_helper.rb index 98c3dc5b5..b12dc282c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -54,6 +54,7 @@ class Test::Unit::TestCase config.chef.node_name = "baz" config.chef.cookbooks_path = "cookbooks" config.chef.provisioning_path = "/tmp/vagrant-chef" + config.chef.log_level = :info config.chef.json = { :recipes => ["vagrant_main"] } diff --git a/test/vagrant/provisioners/chef_server_test.rb b/test/vagrant/provisioners/chef_server_test.rb index 940a404a0..b28b2b64c 100644 --- a/test/vagrant/provisioners/chef_server_test.rb +++ b/test/vagrant/provisioners/chef_server_test.rb @@ -15,7 +15,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase @action.expects(:create_client_key_folder).once.in_sequence(prov_seq) @action.expects(:upload_validation_key).once.in_sequence(prov_seq) @action.expects(:setup_json).once.in_sequence(prov_seq) - @action.expects(:setup_config).once.in_sequence(prov_seq) + @action.expects(:setup_server_config).once.in_sequence(prov_seq) @action.expects(:run_chef_client).once.in_sequence(prov_seq) @action.provision! end @@ -141,40 +141,18 @@ class ChefServerProvisionerTest < Test::Unit::TestCase context "generating and uploading chef client configuration file" do setup do @action.stubs(:guest_validation_key_path).returns("foo") - - @env.ssh.stubs(:upload!) end - should "upload properly generate the configuration file using configuration data" do - expected_config = <<-config -log_level :info -log_location STDOUT -node_name "#{@env.config.chef.node_name}" -ssl_verify_mode :verify_none -chef_server_url "#{@env.config.chef.chef_server_url}" + should "call setup_config with proper variables" do + @action.expects(:setup_config).with("chef_server_client", "client.rb", { + :node_name => @env.config.chef.node_name, + :chef_server_url => @env.config.chef.chef_server_url, + :validation_client_name => @env.config.chef.validation_client_name, + :validation_key => @action.guest_validation_key_path, + :client_key => @env.config.chef.client_key_path + }) -validation_client_name "#{@env.config.chef.validation_client_name}" -validation_key "#{@action.guest_validation_key_path}" -client_key "#{@env.config.chef.client_key_path}" - -file_store_path "/srv/chef/file_store" -file_cache_path "/srv/chef/cache" - -pid_file "/var/run/chef/chef-client.pid" - -Mixlib::Log::Formatter.show_time = true -config - - StringIO.expects(:new).with(expected_config).once - @action.setup_config - end - - should "upload this file as client.rb to the provisioning folder" do - Vagrant::Util::TemplateRenderer.expects(:render).returns("foo") - StringIO.expects(:new).returns("foo") - File.expects(:join).with(@env.config.chef.provisioning_path, "client.rb").once.returns("bar") - @env.ssh.expects(:upload!).with("foo", "bar").once - @action.setup_config + @action.setup_server_config end end diff --git a/test/vagrant/provisioners/chef_solo_test.rb b/test/vagrant/provisioners/chef_solo_test.rb index 6b6f53aad..6c8d20e10 100644 --- a/test/vagrant/provisioners/chef_solo_test.rb +++ b/test/vagrant/provisioners/chef_solo_test.rb @@ -92,22 +92,12 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase @env.ssh.stubs(:upload!) end - should "upload properly generate the configuration file using configuration data" do - expected_config = <<-config -file_cache_path "#{@env.config.chef.provisioning_path}" -cookbook_path #{@action.cookbooks_path} -config + should "call setup_config with proper variables" do + @action.expects(:setup_config).with("chef_solo_solo", "solo.rb", { + :provisioning_path => @env.config.chef.provisioning_path, + :cookbooks_path => @action.cookbooks_path + }) - StringIO.expects(:new).with(expected_config).once - @action.setup_solo_config - end - - should "upload this file as solo.rb to the provisioning folder" do - Vagrant::Util::TemplateRenderer.expects(:render).returns("foo") - @action.expects(:cookbooks_path).returns("cookbooks") - StringIO.expects(:new).returns("foo") - File.expects(:join).with(@env.config.chef.provisioning_path, "solo.rb").once.returns("bar") - @env.ssh.expects(:upload!).with("foo", "bar").once @action.setup_solo_config end end diff --git a/test/vagrant/provisioners/chef_test.rb b/test/vagrant/provisioners/chef_test.rb index 8b3057a77..34b00f7c6 100644 --- a/test/vagrant/provisioners/chef_test.rb +++ b/test/vagrant/provisioners/chef_test.rb @@ -77,6 +77,56 @@ class ChefProvisionerTest < Test::Unit::TestCase end end + context "generating and uploading chef configuration file" do + setup do + @env.ssh.stubs(:upload!) + + @template = "template" + @filename = "foo.rb" + + Vagrant::Util::TemplateRenderer.stubs(:render).returns("foo") + end + + should "render and upload file" do + template_data = mock("data") + string_io = mock("string_io") + Vagrant::Util::TemplateRenderer.expects(:render).with(@template, anything).returns(template_data) + StringIO.expects(:new).with(template_data).returns(string_io) + File.expects(:join).with(@env.config.chef.provisioning_path, @filename).once.returns("bar") + @env.ssh.expects(:upload!).with(string_io, "bar") + + @action.setup_config(@template, @filename, {}) + end + + should "provide log level by default" do + Vagrant::Util::TemplateRenderer.expects(:render).returns("foo").with() do |template, vars| + assert vars.has_key?(:log_level) + assert_equal @env.config.chef.log_level.to_sym, vars[:log_level] + true + end + + @action.setup_config(@template, @filename, {}) + end + + should "allow custom template variables" do + custom = { + :foo => "bar", + :int => 7 + } + + Vagrant::Util::TemplateRenderer.expects(:render).returns("foo").with() do |template, vars| + custom.each do |key, value| + assert vars.has_key?(key) + assert_equal value, vars[key] + end + + true + end + + @action.setup_config(@template, @filename, custom) + end + end + context "generating and uploading json" do def assert_json @env.ssh.expects(:upload!).with do |json, path|