Provisioning redone to use new environment

This commit is contained in:
Mitchell Hashimoto 2010-03-19 22:15:20 -07:00
parent 11780fb618
commit fee10c47fd
10 changed files with 113 additions and 109 deletions

View File

@ -19,7 +19,7 @@ module Vagrant
end
if provisioner.is_a?(Class)
@provisioner = provisioner.new
@provisioner = provisioner.new(@runner.env)
raise ActionException.new(:provisioner_invalid_class) unless @provisioner.is_a?(Provisioners::Base)
elsif provisioner.is_a?(Symbol)
# We have a few hard coded provisioners for built-ins
@ -30,7 +30,7 @@ module Vagrant
provisioner_klass = mapping[provisioner]
raise ActionException.new(:provisioner_unknown_type, :provisioner => provisioner.to_s) if provisioner_klass.nil?
@provisioner = provisioner_klass.new
@provisioner = provisioner_klass.new(@runner.env)
end
logger.info "Provisioning enabled with #{@provisioner.class}"

View File

@ -7,6 +7,13 @@ module Vagrant
class Base
include Vagrant::Util
# The environment which this is being provisioned in
attr_reader :env
def initialize(env)
@env = env
end
# This is the method called to "prepare" the provisioner. This is called
# before any actions are run by the action runner (see {Vagrant::Actions::Runner}).
# This can be used to setup shared folders, forward ports, etc. Whatever is

View File

@ -71,9 +71,9 @@ module Vagrant
def chown_provisioning_folder
logger.info "Setting permissions on chef provisioning folder..."
SSH.execute do |ssh|
ssh.exec!("sudo mkdir -p #{Vagrant.config.chef.provisioning_path}")
ssh.exec!("sudo chown #{Vagrant.config.ssh.username} #{Vagrant.config.chef.provisioning_path}")
env.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
@ -82,8 +82,8 @@ module Vagrant
# Set up initial configuration
data = {
:config => Vagrant.config,
:directory => Vagrant.config.vm.project_directory,
:config => env.config,
:directory => env.config.vm.project_directory,
}
# And wrap it under the "vagrant" namespace
@ -91,11 +91,11 @@ module Vagrant
# Merge with the "extra data" which isn't put under the
# vagrant namespace by default
data.merge!(Vagrant.config.chef.json)
data.merge!(env.config.chef.json)
json = data.to_json
SSH.upload!(StringIO.new(json), File.join(Vagrant.config.chef.provisioning_path, "dna.json"))
env.ssh.upload!(StringIO.new(json), File.join(env.config.chef.provisioning_path, "dna.json"))
end
end
end

View File

@ -4,13 +4,13 @@ module Vagrant
# with a chef server.
class ChefServer < Chef
def prepare
if Vagrant.config.chef.validation_key_path.nil?
if env.config.chef.validation_key_path.nil?
raise Actions::ActionException.new(:chef_server_validation_key_required)
elsif !File.file?(Vagrant.config.chef.validation_key_path)
elsif !File.file?(env.config.chef.validation_key_path)
raise Actions::ActionException.new(:chef_server_validation_key_doesnt_exist)
end
if Vagrant.config.chef.chef_server_url.nil?
if env.config.chef.chef_server_url.nil?
raise Actions::ActionException.new(:chef_server_url_required)
end
end
@ -26,16 +26,16 @@ module Vagrant
def create_client_key_folder
logger.info "Creating folder to hold client key..."
path = Pathname.new(Vagrant.config.chef.client_key_path)
path = Pathname.new(env.config.chef.client_key_path)
SSH.execute do |ssh|
env.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!(validation_key_path, guest_validation_key_path)
env.ssh.upload!(validation_key_path, guest_validation_key_path)
end
def setup_config
@ -43,11 +43,11 @@ module Vagrant
log_level :info
log_location STDOUT
ssl_verify_mode :verify_none
chef_server_url "#{Vagrant.config.chef.chef_server_url}"
chef_server_url "#{env.config.chef.chef_server_url}"
validation_client_name "#{Vagrant.config.chef.validation_client_name}"
validation_client_name "#{env.config.chef.validation_client_name}"
validation_key "#{guest_validation_key_path}"
client_key "#{Vagrant.config.chef.client_key_path}"
client_key "#{env.config.chef.client_key_path}"
file_store_path "/srv/chef/file_store"
file_cache_path "/srv/chef/cache"
@ -58,13 +58,13 @@ Mixlib::Log::Formatter.show_time = true
solo
logger.info "Uploading chef-client configuration script..."
SSH.upload!(StringIO.new(solo_file), File.join(Vagrant.config.chef.provisioning_path, "client.rb"))
env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "client.rb"))
end
def run_chef_client
logger.info "Running chef-client..."
SSH.execute do |ssh|
ssh.exec!("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json") do |channel, data, stream|
env.ssh.execute do |ssh|
ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json") do |channel, data, stream|
# TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high
logger.info("#{stream}: #{data}")
@ -73,11 +73,11 @@ solo
end
def validation_key_path
File.expand_path(Vagrant.config.chef.validation_key_path, Env.root_path)
File.expand_path(env.config.chef.validation_key_path, env.root_path)
end
def guest_validation_key_path
File.join(Vagrant.config.chef.provisioning_path, "validation.pem")
File.join(@env.config.chef.provisioning_path, "validation.pem")
end
end
end

View File

@ -15,24 +15,24 @@ module Vagrant
def share_cookbook_folders
host_cookbook_paths.each_with_index do |cookbook, i|
Vagrant.config.vm.share_folder("vagrant-chef-solo-#{i}", cookbook_path(i), cookbook)
env.config.vm.share_folder("vagrant-chef-solo-#{i}", cookbook_path(i), cookbook)
end
end
def setup_solo_config
solo_file = <<-solo
file_cache_path "#{Vagrant.config.chef.provisioning_path}"
file_cache_path "#{env.config.chef.provisioning_path}"
cookbook_path #{cookbooks_path}
solo
logger.info "Uploading chef-solo configuration script..."
SSH.upload!(StringIO.new(solo_file), File.join(Vagrant.config.chef.provisioning_path, "solo.rb"))
env.ssh.upload!(StringIO.new(solo_file), File.join(env.config.chef.provisioning_path, "solo.rb"))
end
def run_chef_solo
logger.info "Running chef-solo..."
SSH.execute do |ssh|
ssh.exec!("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream|
env.ssh.execute do |ssh|
ssh.exec!("cd #{env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json") do |channel, data, stream|
# TODO: Very verbose. It would be easier to save the data and only show it during
# an error, or when verbosity level is set high
logger.info("#{stream}: #{data}")
@ -41,14 +41,14 @@ solo
end
def host_cookbook_paths
cookbooks = Vagrant.config.chef.cookbooks_path
cookbooks = env.config.chef.cookbooks_path
cookbooks = [cookbooks] unless cookbooks.is_a?(Array)
cookbooks.collect! { |cookbook| File.expand_path(cookbook, Env.root_path) }
cookbooks.collect! { |cookbook| File.expand_path(cookbook, env.root_path) }
return cookbooks
end
def cookbook_path(i)
File.join(Vagrant.config.chef.provisioning_path, "cookbooks-#{i}")
File.join(env.config.chef.provisioning_path, "cookbooks-#{i}")
end
def cookbooks_path

View File

@ -47,7 +47,7 @@ class ProvisionActionTest < Test::Unit::TestCase
@instance.stubs(:prepare)
@klass = mock("klass")
@klass.stubs(:is_a?).with(Class).returns(true)
@klass.stubs(:new).returns(@instance)
@klass.stubs(:new).with(@runner.env).returns(@instance)
mock_config do |config|
config.vm.provisioner = @klass
@ -55,7 +55,7 @@ class ProvisionActionTest < Test::Unit::TestCase
end
should "set the provisioner to an instantiation of the class" do
@klass.expects(:new).once.returns(@instance)
@klass.expects(:new).with(@runner.env).once.returns(@instance)
assert_nothing_raised { @action.prepare }
assert_equal @instance, @action.provisioner
end
@ -81,7 +81,7 @@ class ProvisionActionTest < Test::Unit::TestCase
instance = mock("instance")
instance.expects(:prepare).once
provisioner.expects(:new).returns(instance)
provisioner.expects(:new).with(@runner.env).returns(instance)
assert_nothing_raised { @action.prepare }
assert_equal instance, @action.provisioner
end

View File

@ -7,7 +7,13 @@ class BaseProvisionerTest < Test::Unit::TestCase
context "base instance" do
setup do
@base = Vagrant::Provisioners::Base.new
@env = mock_environment
@base = Vagrant::Provisioners::Base.new(@env)
end
should "set the environment" do
base = Vagrant::Provisioners::Base.new(@env)
assert_equal @env, base.env
end
should "implement provision! which does nothing" do

View File

@ -2,10 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefServerProvisionerTest < Test::Unit::TestCase
setup do
@action = Vagrant::Provisioners::ChefServer.new
Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!)
@env = mock_environment
@action = Vagrant::Provisioners::ChefServer.new(@env)
mock_config
end
@ -29,56 +27,68 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
end
should "not raise an exception if validation_key_path is set" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.validation_key_path = "7"
end
@action.stubs(:env).returns(@env)
assert_nothing_raised { @action.prepare }
end
should "raise an exception if validation_key_path is nil" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.validation_key_path = nil
end
@action.stubs(:env).returns(@env)
assert_raises(Vagrant::Actions::ActionException) {
@action.prepare
}
end
should "not raise an exception if validation_key_path does exist" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.validation_key_path = "7"
end
File.expects(:file?).with(Vagrant.config.chef.validation_key_path).returns(true)
@action.stubs(:env).returns(@env)
File.expects(:file?).with(@env.config.chef.validation_key_path).returns(true)
assert_nothing_raised { @action.prepare }
end
should "raise an exception if validation_key_path doesn't exist" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.validation_key_path = "7"
end
File.expects(:file?).with(Vagrant.config.chef.validation_key_path).returns(false)
@action.stubs(:env).returns(@env)
File.expects(:file?).with(@env.config.chef.validation_key_path).returns(false)
assert_raises(Vagrant::Actions::ActionException) {
@action.prepare
}
end
should "not raise an exception if chef_server_url is set" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.chef_server_url = "7"
end
@action.stubs(:env).returns(@env)
assert_nothing_raised { @action.prepare }
end
should "raise an exception if chef_server_url is nil" do
mock_config do |config|
@env = mock_environment do |config|
config.chef.chef_server_url = nil
end
@action.stubs(:env).returns(@env)
assert_raises(Vagrant::Actions::ActionException) {
@action.prepare
}
@ -88,9 +98,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
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
@env.config.chef.client_key_path = @raw_path
@path = Pathname.new(@raw_path)
end
@ -98,7 +106,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
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)
@env.ssh.expects(:execute).yields(ssh)
@action.create_client_key_folder
end
end
@ -107,7 +115,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
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("foo", "bar").once
@env.ssh.expects(:upload!).with("foo", "bar").once
@action.upload_validation_key
end
end
@ -115,7 +123,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
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)
File.expects(:expand_path).with(@env.config.chef.validation_key_path, @env.root_path).once.returns(result)
assert_equal result, @action.validation_key_path
end
end
@ -123,7 +131,7 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
context "the guest validation key path" do
should "be the provisioning path joined with validation.pem" do
result = mock("result")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "validation.pem").once.returns(result)
File.expects(:join).with(@env.config.chef.provisioning_path, "validation.pem").once.returns(result)
assert_equal result, @action.guest_validation_key_path
end
end
@ -131,6 +139,8 @@ 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
@ -138,11 +148,11 @@ class ChefServerProvisionerTest < Test::Unit::TestCase
log_level :info
log_location STDOUT
ssl_verify_mode :verify_none
chef_server_url "#{Vagrant.config.chef.chef_server_url}"
chef_server_url "#{@env.config.chef.chef_server_url}"
validation_client_name "#{Vagrant.config.chef.validation_client_name}"
validation_client_name "#{@env.config.chef.validation_client_name}"
validation_key "#{@action.guest_validation_key_path}"
client_key "#{Vagrant.config.chef.client_key_path}"
client_key "#{@env.config.chef.client_key_path}"
file_store_path "/srv/chef/file_store"
file_cache_path "/srv/chef/cache"
@ -158,8 +168,8 @@ config
should "upload this file as client.rb to the provisioning folder" do
StringIO.expects(:new).returns("foo")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "client.rb").once.returns("bar")
Vagrant::SSH.expects(:upload!).with("foo", "bar").once
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
end
end
@ -167,8 +177,8 @@ config
context "running chef client" do
should "cd into the provisioning directory and run chef client" do
ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once
Vagrant::SSH.expects(:execute).yields(ssh)
ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-client -c client.rb -j dna.json").once
@env.ssh.expects(:execute).yields(ssh)
@action.run_chef_client
end
end

View File

@ -2,7 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefSoloProvisionerTest < Test::Unit::TestCase
setup do
@action = Vagrant::Provisioners::ChefSolo.new
@env = mock_environment
@action = Vagrant::Provisioners::ChefSolo.new(@env)
Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!)
@ -37,7 +38,7 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
should "share each cookbook folder" do
share_seq = sequence("share_seq")
@host_cookbook_paths.each_with_index do |cookbook, i|
Vagrant.config.vm.expects(:share_folder).with("vagrant-chef-solo-#{i}", @action.cookbook_path(i), cookbook).in_sequence(share_seq)
@env.config.vm.expects(:share_folder).with("vagrant-chef-solo-#{i}", @action.cookbook_path(i), cookbook).in_sequence(share_seq)
end
@action.share_cookbook_folders
@ -45,38 +46,19 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end
context "host cookbooks paths" do
should "expand the path of the cookbooks relative to the environment root path" do
@cookbook = "foo"
@expanded = "bar"
File.expects(:expand_path).with(@cookbook, Vagrant::Env.root_path).returns(@expanded)
mock_config do |config|
config.chef.cookbooks_path = @cookbook
end
assert_equal [@expanded], @action.host_cookbook_paths
end
should "return as an array if was originally a string" do
File.stubs(:expand_path).returns("foo")
mock_config do |config|
config.chef.cookbooks_path = "foo"
end
@env.config.chef.cookbooks_path = "foo"
assert_equal ["foo"], @action.host_cookbook_paths
end
should "return the array of cookbooks if its an array" do
cookbooks = ["foo", "bar"]
mock_config do |config|
config.chef.cookbooks_path = cookbooks
end
@env.config.chef.cookbooks_path = cookbooks
expand_seq = sequence('expand_seq')
cookbooks.each do |cookbook|
File.expects(:expand_path).with(cookbook, Vagrant::Env.root_path).returns(cookbook)
end
cookbooks.collect! { |cookbook| File.expand_path(cookbook, @env.root_path) }
assert_equal cookbooks, @action.host_cookbook_paths
end
@ -84,7 +66,7 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
context "cookbooks path" do
should "return a proper path to a single cookbook" do
expected = File.join(Vagrant.config.chef.provisioning_path, "cookbooks-5")
expected = File.join(@env.config.chef.provisioning_path, "cookbooks-5")
assert_equal expected, @action.cookbook_path(5)
end
@ -93,28 +75,26 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
acc << @action.cookbook_path(i)
end
mock_config do |config|
config.chef.cookbooks_path = @cookbooks
end
@env.config.chef.cookbooks_path = @cookbooks
assert_equal @cookbooks.to_json, @action.cookbooks_path
end
should "return a single string representation if cookbook paths is single" do
@cookbooks = @action.cookbook_path(0)
mock_config do |config|
config.chef.cookbooks_path = @cookbooks
end
@env.config.chef.cookbooks_path = @cookbooks
assert_equal @cookbooks.to_json, @action.cookbooks_path
end
end
context "generating and uploading chef solo configuration file" do
setup do
@env.ssh.stubs(:upload!)
end
should "upload properly generate the configuration file using configuration data" do
expected_config = <<-config
file_cache_path "#{Vagrant.config.chef.provisioning_path}"
file_cache_path "#{@env.config.chef.provisioning_path}"
cookbook_path #{@action.cookbooks_path}
config
@ -125,8 +105,8 @@ config
should "upload this file as solo.rb to the provisioning folder" do
@action.expects(:cookbooks_path).returns("cookbooks")
StringIO.expects(:new).returns("foo")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "solo.rb").once.returns("bar")
Vagrant::SSH.expects(:upload!).with("foo", "bar").once
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
@ -134,8 +114,8 @@ config
context "running chef solo" do
should "cd into the provisioning directory and run chef solo" do
ssh = mock("ssh")
ssh.expects(:exec!).with("cd #{Vagrant.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once
Vagrant::SSH.expects(:execute).yields(ssh)
ssh.expects(:exec!).with("cd #{@env.config.chef.provisioning_path} && sudo chef-solo -c solo.rb -j dna.json").once
@env.ssh.expects(:execute).yields(ssh)
@action.run_chef_solo
end
end

View File

@ -2,7 +2,8 @@ require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
class ChefProvisionerTest < Test::Unit::TestCase
setup do
@action = Vagrant::Provisioners::Chef.new
@env = mock_environment
@action = Vagrant::Provisioners::Chef.new(@env)
Vagrant::SSH.stubs(:execute)
Vagrant::SSH.stubs(:upload!)
@ -69,16 +70,16 @@ class ChefProvisionerTest < Test::Unit::TestCase
should "create and chown the folder to the ssh user" do
ssh_seq = sequence("ssh_seq")
ssh = mock("ssh")
ssh.expects(:exec!).with("sudo mkdir -p #{Vagrant.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
ssh.expects(:exec!).with("sudo chown #{Vagrant.config.ssh.username} #{Vagrant.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
Vagrant::SSH.expects(:execute).yields(ssh)
ssh.expects(:exec!).with("sudo mkdir -p #{@env.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
ssh.expects(:exec!).with("sudo chown #{@env.config.ssh.username} #{@env.config.chef.provisioning_path}").once.in_sequence(ssh_seq)
@env.ssh.expects(:execute).yields(ssh)
@action.chown_provisioning_folder
end
end
context "generating and uploading json" do
def assert_json
Vagrant::SSH.expects(:upload!).with do |json, path|
@env.ssh.expects(:upload!).with do |json, path|
data = JSON.parse(json.read)
yield data
true
@ -88,7 +89,7 @@ class ChefProvisionerTest < Test::Unit::TestCase
end
should "merge in the extra json specified in the config" do
Vagrant.config.chef.json = { :foo => "BAR" }
@env.config.chef.json = { :foo => "BAR" }
assert_json do |data|
assert_equal "BAR", data["foo"]
end
@ -96,20 +97,20 @@ class ChefProvisionerTest < Test::Unit::TestCase
should "add the directory as a special case to the JSON" do
assert_json do |data|
assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["directory"]
assert_equal @env.config.vm.project_directory, data["vagrant"]["directory"]
end
end
should "add the config to the JSON" do
assert_json do |data|
assert_equal Vagrant.config.vm.project_directory, data["vagrant"]["config"]["vm"]["project_directory"]
assert_equal @env.config.vm.project_directory, data["vagrant"]["config"]["vm"]["project_directory"]
end
end
should "upload a StringIO to dna.json" do
StringIO.expects(:new).with(anything).returns("bar")
File.expects(:join).with(Vagrant.config.chef.provisioning_path, "dna.json").once.returns("baz")
Vagrant::SSH.expects(:upload!).with("bar", "baz").once
File.expects(:join).with(@env.config.chef.provisioning_path, "dna.json").once.returns("baz")
@env.ssh.expects(:upload!).with("bar", "baz").once
@action.setup_json
end
end