def action_create
crontab = String.new
newcron = String.new
cron_found = false
newcron << "# Chef Name: #{new_resource.name}\n"
[ :mailto, :path, :shell, :home ].each do |v|
newcron << "#{v.to_s.upcase}=#{@new_resource.send(v)}\n" if @new_resource.send(v)
end
newcron << "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
if @cron_exists
unless compare_cron
Chef::Log.debug("#{@new_resource} skipping existing cron entry '#{@new_resource.name}'")
return
end
status = popen4("crontab -l #{@new_resource.user}") do |pid, stdin, stdout, stderr|
stdout.each_line do |line|
case line.chomp
when "# Chef Name: #{@new_resource.name}"
cron_found = true
next
when CRON_PATTERN
if cron_found
cron_found = false
crontab << newcron
next
end
else
next if cron_found
end
crontab << line
end
end
status = write_crontab(crontab)
Chef::Log.info("#{@new_resource} updated crontab entry")
else
unless @cron_empty
status = popen4("crontab -l #{@new_resource.user}") do |pid, stdin, stdout, stderr|
stdout.each { |line| crontab << line }
end
end
crontab << newcron
status = write_crontab(crontab)
Chef::Log.info("#{@new_resource} added crontab entry")
end
end