Skip to content

Commit 49d4405

Browse files
committed
Update the cron provider to not update the crontab if the current cron entry is already there.
1 parent d4a4742 commit 49d4405

2 files changed

Lines changed: 37 additions & 2 deletions

File tree

chef/lib/chef/provider/cron.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ def action_create
6464
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
6565
stdout.each_line do |line|
6666
if cron_found
67-
crontab << "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
67+
cronline = "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
68+
if(line == cronline)
69+
Chef::Log.debug("Skipping existing cron entry '#{@new_resource.name}'")
70+
return
71+
end
72+
crontab << cronline
6873
cron_found = false
6974
next
7075
end
@@ -76,6 +81,7 @@ def action_create
7681
end
7782
end
7883

84+
7985
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
8086
crontab.each { |line| stdin.puts "#{line}" }
8187
stdin.close

chef/spec/unit/provider/cron_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128
@provider.action_create
129129
end
130130

131-
it "should update the cron entry if it exists" do
131+
it "should update the cron entry if it exists and has changed" do
132132
@status = mock("Status", :exitstatus => 0)
133133
@stdin = mock("STDIN", :null_object => true)
134134
@stdout = mock("STDOUT", :null_object => true)
@@ -142,7 +142,36 @@
142142
@provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
143143
Chef::Log.should_receive(:info).with("Updated cron '#{@new_resource.name}'")
144144
@provider.action_create
145+
end
146+
147+
it "should not update the cron entry if it exists and has not changed" do
148+
resource = mock("Chef::Resource::Cron",
149+
:null_object => true,
150+
:name => "foo",
151+
:minute => "30",
152+
:hour => "*",
153+
:day => "*",
154+
:month => "*",
155+
:weekday => "*",
156+
:command => "/bin/true"
157+
)
158+
provider = Chef::Provider::Cron.new(@node, resource)
145159

160+
161+
@status = mock("Status", :exitstatus => 0)
162+
@stdin = mock("STDIN", :null_object => true)
163+
@stdout = mock("STDOUT", :null_object => true)
164+
@stderr = mock("STDERR", :null_object => true)
165+
@pid = mock("PID", :null_object => true)
166+
@stdout.stub!(:each_line).and_yield("# Chef Name: bar").
167+
and_yield("* 10 * * * /bin/false").
168+
and_yield("# Chef Name: foo\n").
169+
and_yield("30 * * * * /bin/true\n")
170+
provider.stub!(:popen4).and_yield(@pid, @stdin, @stdout, @stderr).and_return(@status)
171+
Chef::Log.should_not_receive(:info).with("Updated cron '#{@new_resource.name}'")
172+
Chef::Log.should_receive(:debug).with("Skipping existing cron entry '#{@new_resource.name}'")
173+
provider.cron_exists = true
174+
provider.action_create
146175
end
147176
end
148177

0 commit comments

Comments
 (0)