From 9384917b81db6a05ef400beb8fc4d27bd9d2b141 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 10 Mar 2010 22:42:42 -0800 Subject: [PATCH] Chef server provisioner works. Added the auto-creation of the client key config. --- lib/vagrant/provisioners/chef.rb | 3 ++ lib/vagrant/provisioners/chef_server.rb | 18 +++++++++-- test/test_helper.rb | 1 + test/vagrant/provisioners/chef_server_test.rb | 32 +++++++++++++++++-- 4 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lib/vagrant/provisioners/chef.rb b/lib/vagrant/provisioners/chef.rb index f9f195f39..10c3d5213 100644 --- a/lib/vagrant/provisioners/chef.rb +++ b/lib/vagrant/provisioners/chef.rb @@ -10,6 +10,7 @@ module Vagrant attr_accessor :chef_server_url attr_accessor :validation_key_path attr_accessor :validation_client_name + attr_accessor :client_key_path # Chef solo specific config attr_accessor :cookbooks_path @@ -20,6 +21,8 @@ module Vagrant def initialize @validation_client_name = "chef-validator" + @client_key_path = "/etc/chef/client.pem" + @cookbooks_path = "cookbooks" @provisioning_path = "/tmp/vagrant-chef" @json = { diff --git a/lib/vagrant/provisioners/chef_server.rb b/lib/vagrant/provisioners/chef_server.rb index a40d12bcb..4aa971929 100644 --- a/lib/vagrant/provisioners/chef_server.rb +++ b/lib/vagrant/provisioners/chef_server.rb @@ -23,15 +23,25 @@ msg def provision! chown_provisioning_folder + create_client_key_folder upload_validation_key setup_json setup_config run_chef_client end + def create_client_key_folder + logger.info "Creating folder to hold client key..." + path = Pathname.new(Vagrant.config.chef.client_key_path) + + SSH.execute do |ssh| + ssh.exec!("sudo mkdir -p #{path.dirname}") + end + end + def upload_validation_key logger.info "Uploading chef client validation key..." - SSH.upload!(Vagrant.config.chef.validation_key_path, guest_validation_key_path) + SSH.upload!(validation_key_path, guest_validation_key_path) end def setup_config @@ -43,7 +53,7 @@ chef_server_url "#{Vagrant.config.chef.chef_server_url}" validation_client_name "#{Vagrant.config.chef.validation_client_name}" validation_key "#{guest_validation_key_path}" -client_key "/etc/chef/client.pem" +client_key "#{Vagrant.config.chef.client_key_path}" file_store_path "/srv/chef/file_store" file_cache_path "/srv/chef/cache" @@ -68,6 +78,10 @@ solo end end + def validation_key_path + File.expand_path(Vagrant.config.chef.validation_key_path, Env.root_path) + end + def guest_validation_key_path File.join(Vagrant.config.chef.provisioning_path, "validation.pem") end diff --git a/test/test_helper.rb b/test/test_helper.rb index a29838700..17dc61e40 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -45,6 +45,7 @@ class Test::Unit::TestCase # Chef config.chef.chef_server_url = "http://localhost:4000" config.chef.validation_key_path = "validation.pem" + config.chef.client_key_path = "/zoo/foo/bar.pem" config.chef.cookbooks_path = "cookbooks" config.chef.provisioning_path = "/tmp/hobo-chef" config.chef.json = { diff --git a/test/vagrant/provisioners/chef_server_test.rb b/test/vagrant/provisioners/chef_server_test.rb index 51139bf69..8b8ddd462 100644 --- a/test/vagrant/provisioners/chef_server_test.rb +++ b/test/vagrant/provisioners/chef_server_test.rb @@ -14,6 +14,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase should "run the proper sequence of methods in order" do prov_seq = sequence("prov_seq") @action.expects(:chown_provisioning_folder).once.in_sequence(prov_seq) + @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) @@ -60,14 +61,41 @@ class ChefServerProvisionerTest < Test::Unit::TestCase end end + context "creating the client key folder" do + setup do + @raw_path = "/foo/bar/baz.pem" + mock_config do |config| + config.chef.client_key_path = @raw_path + end + + @path = Pathname.new(@raw_path) + end + + should "create the folder using the dirname of the path" do + ssh = mock("ssh") + ssh.expects(:exec!).with("sudo mkdir -p #{@path.dirname}").once + Vagrant::SSH.expects(:execute).yields(ssh) + @action.create_client_key_folder + end + end + context "uploading the validation key" do should "upload the validation key to the provisioning path" do + @action.expects(:validation_key_path).once.returns("foo") @action.expects(:guest_validation_key_path).once.returns("bar") - Vagrant::SSH.expects(:upload!).with(Vagrant.config.chef.validation_key_path, "bar").once + Vagrant::SSH.expects(:upload!).with("foo", "bar").once @action.upload_validation_key end end + context "the validation key path" do + should "expand the configured key path" do + result = mock("result") + File.expects(:expand_path).with(Vagrant.config.chef.validation_key_path, Vagrant::Env.root_path).once.returns(result) + assert_equal result, @action.validation_key_path + end + end + context "the guest validation key path" do should "be the provisioning path joined with validation.pem" do result = mock("result") @@ -90,7 +118,7 @@ chef_server_url "#{Vagrant.config.chef.chef_server_url}" validation_client_name "#{Vagrant.config.chef.validation_client_name}" validation_key "#{@action.guest_validation_key_path}" -client_key "/etc/chef/client.pem" +client_key "#{Vagrant.config.chef.client_key_path}" file_store_path "/srv/chef/file_store" file_cache_path "/srv/chef/cache"