Provisioners now register themselves with their shortcut name

This commit is contained in:
Mitchell Hashimoto 2011-01-11 19:52:55 -08:00
parent db60e5f426
commit 85b8ccc2ec
7 changed files with 44 additions and 9 deletions

View File

@ -30,15 +30,7 @@ module Vagrant
@provisioner = provisioner.new(@env) @provisioner = provisioner.new(@env)
raise Errors::ProvisionInvalidClass if !@provisioner.is_a?(Provisioners::Base) raise Errors::ProvisionInvalidClass if !@provisioner.is_a?(Provisioners::Base)
elsif provisioner.is_a?(Symbol) elsif provisioner.is_a?(Symbol)
# We have a few hard coded provisioners for built-ins provisioner_klass = Provisioners::Base.registered[provisioner]
mapping = {
:chef_solo => Provisioners::ChefSolo,
:chef_server => Provisioners::ChefServer,
:puppet => Provisioners::Puppet,
:puppet_server => Provisioners::PuppetServer
}
provisioner_klass = mapping[provisioner]
raise Errors::ProvisionUnknownType, :provisioner => provisioner.to_s if provisioner_klass.nil? raise Errors::ProvisionUnknownType, :provisioner => provisioner.to_s if provisioner_klass.nil?
@provisioner = provisioner_klass.new(@env) @provisioner = provisioner_klass.new(@env)
end end

View File

@ -11,6 +11,19 @@ module Vagrant
# {Vagrant::Action::Environment} # {Vagrant::Action::Environment}
attr_reader :action_env attr_reader :action_env
# Registers a provisioner with a given shortcut. This allows that provisioner
# to be referenced with the shortcut.
#
# @param [Symbol] shortcut
def self.register(shortcut)
registered[shortcut] = self
end
# Returns the provisioner associated with the given shortcut.
def self.registered
@@registered ||= {}
end
def initialize(env) def initialize(env)
@action_env = env @action_env = env
end end

View File

@ -5,6 +5,8 @@ module Vagrant
# This class implements provisioning via chef-client, allowing provisioning # This class implements provisioning via chef-client, allowing provisioning
# with a chef server. # with a chef server.
class ChefServer < Chef class ChefServer < Chef
register :chef_server
def prepare def prepare
raise ChefError, :server_validation_key_required if env.config.chef.validation_key_path.nil? raise ChefError, :server_validation_key_required if env.config.chef.validation_key_path.nil?
raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path) raise ChefError, :server_validation_key_doesnt_exist if !File.file?(validation_key_path)

View File

@ -2,6 +2,8 @@ module Vagrant
module Provisioners module Provisioners
# This class implements provisioning via chef-solo. # This class implements provisioning via chef-solo.
class ChefSolo < Chef class ChefSolo < Chef
register :chef_solo
def prepare def prepare
share_cookbook_folders share_cookbook_folders
share_role_folders share_role_folders

View File

@ -21,6 +21,8 @@ module Vagrant
end end
class Puppet < Base class Puppet < Base
register :puppet
def prepare def prepare
check_manifest_dir check_manifest_dir
share_manifests share_manifests

View File

@ -19,6 +19,8 @@ module Vagrant
end end
class PuppetServer < Base class PuppetServer < Base
register :puppet_server
def provision! def provision!
verify_binary("puppetd") verify_binary("puppetd")
run_puppetd_client run_puppetd_client

View File

@ -1,10 +1,32 @@
require "test_helper" require "test_helper"
class BaseProvisionerTest < Test::Unit::TestCase class BaseProvisionerTest < Test::Unit::TestCase
setup do
@klass = Vagrant::Provisioners::Base
end
should "include the util class so subclasses have access to it" do should "include the util class so subclasses have access to it" do
assert Vagrant::Provisioners::Base.include?(Vagrant::Util) assert Vagrant::Provisioners::Base.include?(Vagrant::Util)
end end
context "registering provisioners" do
teardown do
@klass.registered.delete(:zomg)
end
should "not have unregistered provisioners" do
assert_nil @klass.registered[:foo]
end
should "be able to register a provisioner" do
foo = Class.new(@klass) do
register :zomg
end
assert_equal foo, @klass.registered[:zomg]
end
end
context "base instance" do context "base instance" do
setup do setup do
@env = Vagrant::Action::Environment.new(vagrant_env) @env = Vagrant::Action::Environment.new(vagrant_env)