Skip to content

Commit 86f39b1

Browse files
committed
Template and remote file now supports a cookbook attribute, which lets you modify what cookbook you fetch the source from. Fixes CHEF-72
1 parent 93e4526 commit 86f39b1

15 files changed

Lines changed: 196 additions & 23 deletions

File tree

chef/lib/chef/provider/file.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ def backup(file=nil)
169169
end
170170

171171
def generate_url(url, type, args=nil)
172-
generate_cookbook_url(url, @new_resource.cookbook_name, type, @node, args)
172+
cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
173+
generate_cookbook_url(url, cookbook_name, type, @node, args)
173174
end
174175

175176
end

chef/lib/chef/provider/remote_file.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ def get_from_server(source, current_checksum)
107107

108108
def get_from_local_cookbook(source)
109109
if Chef::Config[:solo]
110+
cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
110111
filename = find_preferred_file(
111-
@new_resource.cookbook_name.to_s,
112+
cookbook_name,
112113
:remote_file,
113114
source,
114115
@node[:fqdn],

chef/lib/chef/provider/template.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,15 @@ class Template < Chef::Provider::File
3636
def action_create
3737
Chef::Log.debug(@node.run_state.inspect)
3838
raw_template_file = nil
39-
cache_file_name = "cookbooks/#{@new_resource.cookbook_name}/templates/default/#{@new_resource.source}"
40-
template_cache_name = "#{@new_resource.cookbook_name}_#{@new_resource.source}"
39+
40+
cookbook_name = @new_resource.cookbook || @new_resource.cookbook_name
41+
42+
cache_file_name = "cookbooks/#{cookbook_name}/templates/default/#{@new_resource.source}"
43+
template_cache_name = "#{cookbook_name}_#{@new_resource.source}"
4144

4245
if Chef::Config[:solo]
4346
filename = find_preferred_file(
44-
@new_resource.cookbook_name.to_s,
47+
cookbook_name,
4548
:template,
4649
@new_resource.source,
4750
@node[:fqdn],

chef/lib/chef/resource/remote_file.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def initialize(name, collection=nil, node=nil)
2727
@resource_name = :remote_file
2828
@action = "create"
2929
@source = nil
30+
@cookbook = nil
3031
end
3132

3233
def source(args=nil)
@@ -36,6 +37,14 @@ def source(args=nil)
3637
:kind_of => String
3738
)
3839
end
40+
41+
def cookbook(args=nil)
42+
set_or_return(
43+
:cookbook,
44+
args,
45+
:kind_of => String
46+
)
47+
end
3948

4049
end
4150
end

chef/lib/chef/resource/template.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def initialize(name, collection=nil, node=nil)
2727
@resource_name = :template
2828
@action = "create"
2929
@source = nil
30+
@cookbook = nil
3031
@variables = Hash.new
3132
end
3233

@@ -45,6 +46,14 @@ def variables(args=nil)
4546
:kind_of => [ Hash ]
4647
)
4748
end
49+
50+
def cookbook(args=nil)
51+
set_or_return(
52+
:cookbook,
53+
args,
54+
:kind_of => [ String ]
55+
)
56+
end
4857

4958
end
5059
end

chef/spec/unit/resource/remote_file_spec.rb

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,32 @@
2222

2323
before(:each) do
2424
@resource = Chef::Resource::RemoteFile.new("fakey_fakerton")
25-
end
25+
end
2626

27-
it "should create a new Chef::Resource::RemoteFile" do
28-
@resource.should be_a_kind_of(Chef::Resource)
29-
@resource.should be_a_kind_of(Chef::Resource::File)
30-
@resource.should be_a_kind_of(Chef::Resource::RemoteFile)
27+
describe "initialize" do
28+
it "should create a new Chef::Resource::RemoteFile" do
29+
@resource.should be_a_kind_of(Chef::Resource)
30+
@resource.should be_a_kind_of(Chef::Resource::File)
31+
@resource.should be_a_kind_of(Chef::Resource::RemoteFile)
32+
end
3133
end
3234

33-
it "should accept a string for the remote file source" do
34-
@resource.source "something"
35-
@resource.source.should eql("something")
35+
describe "source" do
36+
it "should accept a string for the remote file source" do
37+
@resource.source "something"
38+
@resource.source.should eql("something")
39+
end
40+
end
41+
42+
describe "cookbook" do
43+
it "should accept a string for the cookbook name" do
44+
@resource.cookbook "something"
45+
@resource.cookbook.should eql("something")
46+
end
47+
48+
it "should default to nil" do
49+
@resource.cookbook.should == nil
50+
end
3651
end
3752

3853
end

chef/spec/unit/resource/template_spec.rb

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,37 @@
2424
@resource = Chef::Resource::Template.new("fakey_fakerton")
2525
end
2626

27-
it "should create a new Chef::Resource::Template" do
28-
@resource.should be_a_kind_of(Chef::Resource)
29-
@resource.should be_a_kind_of(Chef::Resource::File)
30-
@resource.should be_a_kind_of(Chef::Resource::Template)
27+
describe "initialize" do
28+
it "should create a new Chef::Resource::Template" do
29+
@resource.should be_a_kind_of(Chef::Resource)
30+
@resource.should be_a_kind_of(Chef::Resource::File)
31+
@resource.should be_a_kind_of(Chef::Resource::Template)
32+
end
3133
end
3234

33-
it "should accept a string for the template source" do
34-
@resource.source "something"
35-
@resource.source.should eql("something")
35+
describe "source" do
36+
it "should accept a string for the template source" do
37+
@resource.source "something"
38+
@resource.source.should eql("something")
39+
end
3640
end
3741

38-
it "should accept a hash for the variable list" do
39-
@resource.variables({ :reluctance => :awkward })
40-
@resource.variables.should == { :reluctance => :awkward }
42+
describe "variables" do
43+
it "should accept a hash for the variable list" do
44+
@resource.variables({ :reluctance => :awkward })
45+
@resource.variables.should == { :reluctance => :awkward }
46+
end
47+
end
48+
49+
describe "cookbook" do
50+
it "should accept a string for the cookbook name" do
51+
@resource.cookbook("foo")
52+
@resource.cookbook.should == "foo"
53+
end
54+
55+
it "should default to nil" do
56+
@resource.cookbook.should == nil
57+
end
4158
end
4259

4360
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#
2+
# Cookbook Name:: CHEF-72-define
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
define :define_remote_file do
21+
remote_file "/tmp/chef-72-remote-#{params[:name]}.txt" do
22+
source "chef-72.txt"
23+
cookbook "CHEF-72-define"
24+
end
25+
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Cookbook Name:: CHEF-72-define
3+
# Recipe:: default
4+
#
5+
# Copyright 2009, Opscode
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
20+
define :define_template, :sing => 'ahh' do
21+
template "/tmp/chef-72-#{params[:name]}.txt" do
22+
source "chef-72.txt.erb"
23+
variables({:sing => params[:sing]})
24+
cookbook "CHEF-72-define"
25+
end
26+
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I want to give you some good, good love!

0 commit comments

Comments
 (0)