Skip to content

Commit c9c0e5e

Browse files
committed
Adding ignore_failure and epic_fail, to allow selective ignorance of provider failures.
1 parent 39dd646 commit c9c0e5e

7 files changed

Lines changed: 50 additions & 45 deletions

File tree

chef/lib/chef/resource.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def initialize(name, collection=nil, node=nil)
4545
@action = :nothing
4646
@updated = false
4747
@supports = {}
48+
@ignore_failure = false
4849
sline = caller(4).shift
4950
if sline
5051
@source_line = sline.gsub!(/^(.+):(.+):.+$/, '\1 line \2')
@@ -102,6 +103,18 @@ def noop(tf=nil)
102103
end
103104
end
104105

106+
def ignore_failure(arg=nil)
107+
set_or_return(
108+
:ignore_failure,
109+
arg,
110+
:kind_of => [ TrueClass, FalseClass ]
111+
)
112+
end
113+
114+
def epic_fail(arg=nil)
115+
ignore_failure(arg)
116+
end
117+
105118
def notifies(action, resources, timing=:delayed)
106119
timing = check_timing(timing)
107120
rarray = resources.kind_of?(Array) ? resources : [ resources ]

chef/lib/chef/runner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def converge
8585
end
8686
rescue => e
8787
Chef::Log.error("#{resource} (#{resource.source_line}) had an error:")
88-
raise
88+
raise e unless resource.ignore_failure
8989
end
9090
end
9191

chef/spec/unit/resource_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,19 @@
150150
it "should return the current value of supports" do
151151
@resource.supports.should == {}
152152
end
153+
154+
it "should default to throwing an error if a provider fails for a resource" do
155+
@resource.ignore_failure.should == false
156+
end
157+
158+
it "should allow you to set whether a provider should throw exceptions with ignore_failure" do
159+
@resource.ignore_failure(true)
160+
@resource.ignore_failure.should == true
161+
end
162+
163+
it "should allow you to epic_fail" do
164+
@resource.epic_fail(true)
165+
@resource.epic_fail.should == true
166+
end
167+
153168
end

chef/spec/unit/runner_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@
6565
@runner.converge
6666
end
6767

68+
it "should raise exceptions as thrown by a provider" do
69+
Chef::Platform.stub!(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
70+
provider = Chef::Provider::SnakeOil.new(@node, @collection[0])
71+
Chef::Provider::SnakeOil.stub!(:new).once.and_return(provider)
72+
provider.stub!(:action_sell).once.and_raise(ArgumentError)
73+
lambda { @runner.converge }.should raise_error(ArgumentError)
74+
end
75+
76+
it "should not raise exceptions thrown by providers if the resource has ignore_failure set to true" do
77+
Chef::Platform.stub!(:find_provider_for_node).once.and_return(Chef::Provider::SnakeOil)
78+
@collection[0].stub!(:ignore_failure).and_return(true)
79+
provider = Chef::Provider::SnakeOil.new(@node, @collection[0])
80+
Chef::Provider::SnakeOil.stub!(:new).once.and_return(provider)
81+
provider.stub!(:action_sell).once.and_raise(ArgumentError)
82+
lambda { @runner.converge }.should_not raise_error(ArgumentError)
83+
end
84+
6885
it "should execute immediate actions on changed resources" do
6986
Chef::Platform.should_receive(:find_provider_for_node).exactly(3).times.and_return(Chef::Provider::SnakeOil)
7087
provider = Chef::Provider::SnakeOil.new(@node, @collection[0])

example-repository/Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ desc "Test your cookbooks for syntax errors"
5555
task :test do
5656
puts "** Testing your cookbooks for syntax errors"
5757
Dir[ File.join(TOPDIR, "cookbooks", "**", "*.rb") ].each do |recipe|
58+
print "Testing recipe #{recipe}: "
5859
sh %{ruby -c #{recipe}} do |ok, res|
5960
if ! ok
6061
raise "Syntax error in #{recipe}"

example-repository/config/client.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
#
2-
# Cookbook Name:: chef
3-
# Attribute File:: client.rb
2+
# Chef Client Config File
43
#
5-
# Copyright 2008, Engine Yard, Inc.
6-
#
7-
# All rights reserved - Do Not Redistribute
4+
# Will be overwritten
85
#
96

107
log_level :info
@@ -19,3 +16,4 @@
1916
search_url "http://127.0.0.1:4000"
2017

2118

19+

example-repository/cookbooks/fakefile/recipes/default.rb

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
bork
22

3-
list_of_users = []
4-
list_of_users << { :user => "aj", :uid => 2000, :gid => 110, :home => "/tmp/home/aj", :comment => "AJ Christensen", :shell => "/bin/bash" }.to_mash
5-
list_of_users << { :user => "adam", :uid => 2001, :gid => 110, :home => "/tmp/home/adam", :comment => "Adam Jacobs", :shell => "/bin/zsh" }.to_mash
6-
7-
list_of_users.each do |u|
8-
u.each { |k,v| Chef::Log.info("#{k}: #{v}") }
9-
user "#{u['user']}-test" do
10-
uid u['uid']
11-
gid u['gid']
12-
home u['home']
13-
shell u['shell']
14-
comment u['comment']
15-
shell u['shell']
16-
action [ :create, :lock ]
17-
end
18-
end
19-
203
execute "write-foolio" do
214
command <<-EOH
225
echo 'monkeypants #{node[:ipaddress]} #{node[:friends]}' > /tmp/foolio
@@ -157,26 +140,4 @@
157140

158141
monkey "snack"
159142

160-
user "katie" do
161-
uid 9999
162-
gid 100
163-
home "/tmp/home/katie"
164-
shell "/bin/bash"
165-
comment "Katie Bethell"
166-
action :create
167-
end
168-
169-
user "katie" do
170-
gid 101
171-
action :modify
172-
end
173-
174-
user "katie" do
175-
shell "/home/katie"
176-
action :manage
177-
end
178-
179-
user "katie" do
180-
action [ :lock, :unlock, :remove ]
181-
end
182143

0 commit comments

Comments
 (0)