add support for roles in Chef Solo

This commit is contained in:
Wade Simmons 2010-04-09 12:38:14 -06:00 committed by Mitchell Hashimoto
parent c0c3313fdd
commit 15e4b2f411
4 changed files with 100 additions and 2 deletions

View File

@ -15,6 +15,7 @@ module Vagrant
# Chef solo specific config
attr_accessor :cookbooks_path
attr_accessor :roles_path
# Shared config
attr_accessor :provisioning_path
@ -27,6 +28,7 @@ module Vagrant
@node_name = "client"
@cookbooks_path = "cookbooks"
@roles_path = []
@provisioning_path = "/tmp/vagrant-chef"
@log_level = :info
@json = {

View File

@ -4,6 +4,7 @@ module Vagrant
class ChefSolo < Chef
def prepare
share_cookbook_folders
share_role_folders
end
def provision!
@ -19,10 +20,17 @@ module Vagrant
end
end
def share_role_folders
host_role_paths.each_with_index do |role, i|
env.config.vm.share_folder("vagrant-chef-solo-#{i}", role_path(i), role)
end
end
def setup_solo_config
setup_config("chef_solo_solo", "solo.rb", {
:provisioning_path => env.config.chef.provisioning_path,
:cookbooks_path => cookbooks_path
:cookbooks_path => cookbooks_path,
:roles_path => roles_path
})
end
@ -44,10 +52,21 @@ module Vagrant
return cookbooks
end
def host_role_paths
roles = env.config.chef.roles_path
roles = [roles] unless roles.is_a?(Array)
roles.collect! { |role| File.expand_path(role, env.root_path) }
return roles
end
def cookbook_path(i)
File.join(env.config.chef.provisioning_path, "cookbooks-#{i}")
end
def role_path(i)
File.join(env.config.chef.provisioning_path, "roles-#{i}")
end
def cookbooks_path
result = []
host_cookbook_paths.each_with_index do |host_path, i|
@ -59,6 +78,18 @@ module Vagrant
result = result[0].to_s if result.length == 1
result.to_json
end
def roles_path
result = []
host_role_paths.each_with_index do |host_path, i|
result << role_path(i)
end
# We're lucky that ruby's string and array syntax for strings is the
# same as JSON, so we can just convert to JSON here and use that
result = result[0].to_s if result.length == 1
result.to_json
end
end
end
end

View File

@ -1,3 +1,4 @@
file_cache_path "<%= provisioning_path %>"
cookbook_path <%= cookbooks_path %>
role_path <%= roles_path %>
log_level <%= log_level.inspect %>

View File

@ -11,6 +11,11 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
@action.expects(:share_cookbook_folders).once
@action.prepare
end
should "share role folders" do
@action.expects(:share_role_folders).once
@action.prepare
end
end
context "provisioning" do
@ -40,6 +45,22 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end
end
context "sharing role folders" do
setup do
@host_role_paths = ["foo", "bar"]
@action.stubs(:host_role_paths).returns(@host_role_paths)
end
should "share each role folder" do
share_seq = sequence("share_seq")
@host_role_paths.each_with_index do |role, i|
@env.config.vm.expects(:share_folder).with("vagrant-chef-solo-#{i}", @action.role_path(i), role).in_sequence(share_seq)
end
@action.share_role_folders
end
end
context "host cookbooks paths" do
should "return as an array if was originally a string" do
File.stubs(:expand_path).returns("foo")
@ -59,6 +80,25 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end
end
context "host roles paths" do
should "return as an array if was originally a string" do
File.stubs(:expand_path).returns("foo")
@env.config.chef.roles_path = "foo"
assert_equal ["foo"], @action.host_role_paths
end
should "return the array of roles if its an array" do
roles = ["foo", "bar"]
@env.config.chef.roles_path = roles
expand_seq = sequence('expand_seq')
roles.collect! { |role| File.expand_path(role, @env.root_path) }
assert_equal roles, @action.host_role_paths
end
end
context "cookbooks path" do
should "return a proper path to a single cookbook" do
expected = File.join(@env.config.chef.provisioning_path, "cookbooks-5")
@ -82,6 +122,29 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
end
end
context "roles path" do
should "return a proper path to a single role" do
expected = File.join(@env.config.chef.provisioning_path, "roles-5")
assert_equal expected, @action.role_path(5)
end
should "return array-representation of role paths if multiple" do
@roles = (0..5).inject([]) do |acc, i|
acc << @action.role_path(i)
end
@env.config.chef.roles_path = @roles
assert_equal @roles.to_json, @action.roles_path
end
should "return a single string representation if roles paths is single" do
@roles = @action.role_path(0)
@env.config.chef.roles_path = @roles
assert_equal @roles.to_json, @action.roles_path
end
end
context "generating and uploading chef solo configuration file" do
setup do
@env.ssh.stubs(:upload!)
@ -90,7 +153,8 @@ class ChefSoloProvisionerTest < Test::Unit::TestCase
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
:cookbooks_path => @action.cookbooks_path,
:roles_path => @action.roles_path
})
@action.setup_solo_config