diff --git a/lib/vagrant/provisioners/chef.rb b/lib/vagrant/provisioners/chef.rb index b5d372d1d..a9d73718f 100644 --- a/lib/vagrant/provisioners/chef.rb +++ b/lib/vagrant/provisioners/chef.rb @@ -3,6 +3,62 @@ module Vagrant # This class is a base class where the common functinality shared between # chef-solo and chef-client provisioning are stored. This is **not an actual # provisioner**. Instead, {ChefSolo} or {ChefServer} should be used. + class Chef < Base + def prepare + raise ChefError.new(:invalid_provisioner) + end + + def verify_binary(binary) + vm.ssh.execute do |ssh| + # Checks for the existence of chef binary and error if it + # doesn't exist. + ssh.exec!("which #{binary}", :error_key => :chef_not_detected, :error_data => {:binary => binary}) + end + end + + def chown_provisioning_folder + vm.ssh.execute do |ssh| + ssh.exec!("sudo mkdir -p #{env.config.chef.provisioning_path}") + ssh.exec!("sudo chown #{env.config.ssh.username} #{env.config.chef.provisioning_path}") + 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)) + + vm.ssh.upload!(StringIO.new(config_file), File.join(env.config.chef.provisioning_path, filename)) + end + + def setup_json + env.ui.info "vagrant.provisioners.chef.json" + + # Set up initial configuration + data = { + :config => env.config, + :directory => env.config.vm.shared_folders["v-root"][:guestpath], + } + + # And wrap it under the "vagrant" namespace + data = { :vagrant => data } + + # Merge with the "extra data" which isn't put under the + # vagrant namespace by default + data.merge!(env.config.chef.json) + + json = data.to_json + + vm.ssh.upload!(StringIO.new(json), File.join(env.config.chef.provisioning_path, "dna.json")) + end + end + + class Chef < Base + class ChefError < Errors::VagrantError + error_namespace("vagrant.provisioners.chef") + end + end + class Chef < Base # This is the configuration which is available through `config.chef` class ChefConfig < Vagrant::Config::Base @@ -71,54 +127,6 @@ module Vagrant # Tell the Vagrant configure class about our custom configuration Config.configures :chef, ChefConfig - - def prepare - action_env.error!(:chef_base_invalid_provisioner) - end - - def verify_binary(binary) - vm.ssh.execute do |ssh| - # Checks for the existence of chef binary and error if it - # doesn't exist. - ssh.exec!("which #{binary}", :error_key => :chef_not_detected, :error_data => {:binary => binary}) - end - end - - def chown_provisioning_folder - vm.ssh.execute do |ssh| - ssh.exec!("sudo mkdir -p #{env.config.chef.provisioning_path}") - ssh.exec!("sudo chown #{env.config.ssh.username} #{env.config.chef.provisioning_path}") - 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)) - - vm.ssh.upload!(StringIO.new(config_file), File.join(env.config.chef.provisioning_path, filename)) - end - - def setup_json - env.ui.info "vagrant.provisioners.chef.json" - - # Set up initial configuration - data = { - :config => env.config, - :directory => env.config.vm.shared_folders["v-root"][:guestpath], - } - - # And wrap it under the "vagrant" namespace - data = { :vagrant => data } - - # Merge with the "extra data" which isn't put under the - # vagrant namespace by default - data.merge!(env.config.chef.json) - - json = data.to_json - - vm.ssh.upload!(StringIO.new(json), File.join(env.config.chef.provisioning_path, "dna.json")) - end end end end diff --git a/lib/vagrant/provisioners/chef_server.rb b/lib/vagrant/provisioners/chef_server.rb index 37d0aea85..2a67fb5d2 100644 --- a/lib/vagrant/provisioners/chef_server.rb +++ b/lib/vagrant/provisioners/chef_server.rb @@ -6,13 +6,9 @@ module Vagrant # with a chef server. class ChefServer < Chef def prepare - if env.config.chef.validation_key_path.nil? - action_env.error!(:chef_server_validation_key_required) - elsif !File.file?(validation_key_path) - action_env.error!(:chef_server_validation_key_doesnt_exist) - elsif env.config.chef.chef_server_url.nil? - action_env.error!(:chef_server_url_required) - end + raise ChefError.new(:server_validation_key_required) if env.config.chef.validation_key_path.nil? + raise ChefError.new(:server_validation_key_doesnt_exist) if !File.file?(validation_key_path) + raise ChefError.new(:server_url_required) if env.config.chef.chef_server_url.nil? end def provision! diff --git a/templates/locales/en.yml b/templates/locales/en.yml index ecee2a8ea..04eeb63fe 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -302,6 +302,18 @@ en: upload_validation_key: "Uploading chef client validation key..." running_client: "Running chef-client..." running_solo: "Running chef-solo..." + invalid_provisioner: "Vagrant::Provisioners::Chef is not a valid provisioner! Use ChefSolo or ChefServer instead." + server_url_required: |- + Chef server provisioning requires that the `config.chef.chef_server_url` be set to the + URL of your chef server. Examples include "http://12.12.12.12:4000" and + "http://myserver.com:4000" (the port of course can be different, but 4000 is the default) + server_validation_key_required: |- + Chef server provisioning requires that the `config.chef.validation_key_path` configuration + be set to a path on your local machine of the validation key used to register the + VM with the chef server. + server_validation_key_doesnt_exist: |- + The validation key set for `config.chef.validation_key_path` does not exist! This + file needs to exist so it can be uploaded to the virtual machine. systems: linux: diff --git a/templates/strings.yml b/templates/strings.yml index 970ba1acc..4ef5c1d52 100644 --- a/templates/strings.yml +++ b/templates/strings.yml @@ -20,20 +20,6 @@ name you specified and try again. As a reminder, the repackage command is for repackaging boxes which have been added through `vagrant box add` but the box has been lost. -:chef_base_invalid_provisioner: |- - Vagrant::Provisioners::Chef is not a valid provisioner! Use ChefSolo or ChefServer instead. -:chef_server_url_required: |- - Chef server provisioning requires that the `config.chef.chef_server_url` be set to the - URL of your chef server. Examples include "http://12.12.12.12:4000" and - "http://myserver.com:4000" (the port of course can be different, but 4000 is the default) -:chef_server_validation_key_required: |- - Chef server provisioning requires that the `config.chef.validation_key_path` configuration - be set to a path on your local machine of the validation key used to register the - VM with the chef server. -:chef_server_validation_key_doesnt_exist: |- - The validation key set for `config.chef.validation_key_path` does not exist! This - file needs to exist so it can be uploaded to the virtual machine. It is - currently set to "<%= Vagrant.config.chef.validation_key_path %>" :chef_not_detected: |- The `<%= binary %>` binary appears to not be in the PATH of the guest. This could be because the PATH is not properly setup or perhaps chef is not diff --git a/test/vagrant/provisioners/chef_server_test.rb b/test/vagrant/provisioners/chef_server_test.rb index c6bd7411d..a60854c24 100644 --- a/test/vagrant/provisioners/chef_server_test.rb +++ b/test/vagrant/provisioners/chef_server_test.rb @@ -35,21 +35,19 @@ class ChefServerProvisionerTest < Test::Unit::TestCase end @action.stubs(:env).returns(@env) - - @action.prepare - assert !@action_env.error? + assert_nothing_raised { @action.prepare } end - should "eraise an exception if validation_key_path is nil" do + should "raise an exception if validation_key_path is nil" do @env = mock_environment do |config| config.chef.validation_key_path = nil end @action.stubs(:env).returns(@env) - @action.prepare - assert @action_env.error? - assert_equal :chef_server_validation_key_required, @action_env.error.first + assert_raises(Vagrant::Provisioners::Chef::ChefError) { + @action.prepare + } end should "not raise an exception if validation_key_path does exist" do @@ -61,8 +59,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase @action.stubs(:validation_key_path).returns("9") File.expects(:file?).with(@action.validation_key_path).returns(true) - @action.prepare - assert !@action_env.error? + assert_nothing_raised { @action.prepare } end should "raise an exception if validation_key_path doesn't exist" do @@ -74,9 +71,9 @@ class ChefServerProvisionerTest < Test::Unit::TestCase @action.stubs(:validation_key_path).returns("9") File.expects(:file?).with(@action.validation_key_path).returns(false) - @action.prepare - assert @action_env.error? - assert_equal :chef_server_validation_key_doesnt_exist, @action_env.error.first + assert_raises(Vagrant::Provisioners::Chef::ChefError) { + @action.prepare + } end should "not raise an exception if chef_server_url is set" do @@ -85,9 +82,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase end @action.stubs(:env).returns(@env) - - @action.prepare - assert !@action_env.error? + assert_nothing_raised { @action.prepare } end should "raise an exception if chef_server_url is nil" do @@ -97,9 +92,9 @@ class ChefServerProvisionerTest < Test::Unit::TestCase @action.stubs(:env).returns(@env) - @action.prepare - assert @action_env.error? - assert_equal :chef_server_url_required, @action_env.error.first + assert_raises(Vagrant::Provisioners::Chef::ChefError) { + @action.prepare + } end end diff --git a/test/vagrant/provisioners/chef_test.rb b/test/vagrant/provisioners/chef_test.rb index f0786a8e0..4d4e92380 100644 --- a/test/vagrant/provisioners/chef_test.rb +++ b/test/vagrant/provisioners/chef_test.rb @@ -12,9 +12,9 @@ class ChefProvisionerTest < Test::Unit::TestCase context "preparing" do should "error the environment" do - @action.prepare - assert @action_env.error? - assert_equal :chef_base_invalid_provisioner, @action_env.error.first + assert_raises(Vagrant::Provisioners::Chef::ChefError) { + @action.prepare + } end end