2
# Author:: Bryan McLellan (btm@loftninjas.org)
3
# Copyright:: Copyright (c) 2009 Bryan McLellan
4
# License:: Apache License, Version 2.0
6
# Licensed under the Apache License, Version 2.0 (the "License");
7
# you may not use this file except in compliance with the License.
8
# You may obtain a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS,
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
# See the License for the specific language governing permissions and
16
# limitations under the License.
20
require 'chef/mixin/command'
21
require 'chef/provider'
25
class Cron < Chef::Provider
26
include Chef::Mixin::Command
28
def initialize(node, new_resource)
29
super(node, new_resource)
33
attr_accessor :cron_exists, :cron_empty
35
def load_current_resource
37
@current_resource = Chef::Resource::Cron.new(@new_resource.name)
38
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
39
stdout.each { |line| crontab << line }
41
if status.exitstatus > 1
42
raise Chef::Exceptions::Cron, "Error determining state of #{@new_resource.name}, exit: #{status.exitstatus}"
43
elsif status.exitstatus == 0
44
crontab.each do |line|
46
when /^# Chef Name: #{@new_resource.name}/
47
Chef::Log.debug("Found cron '#{@new_resource.name}'")
51
Chef::Log.debug("Cron '#{@new_resource.name}' not found") unless @cron_exists
52
elsif status.exitstatus == 1
53
Chef::Log.debug("Cron empty for '#{@new_resource.user}'")
64
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
65
stdout.each_line do |line|
67
cronline = "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
69
Chef::Log.debug("Skipping existing cron entry '#{@new_resource.name}'")
77
when /^# Chef Name: #{new_resource.name}\n/
85
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
86
crontab.each { |line| stdin.puts "#{line}" }
89
Chef::Log.info("Updated cron '#{@new_resource.name}'")
92
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
93
stdout.each { |line| crontab << line }
97
crontab << "# Chef Name: #{new_resource.name}\n"
98
crontab << "#{@new_resource.minute} #{@new_resource.hour} #{@new_resource.day} #{@new_resource.month} #{@new_resource.weekday} #{@new_resource.command}\n"
100
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
101
crontab.each { |line| stdin.puts "#{line}" }
104
Chef::Log.info("Added cron '#{@new_resource.name}'")
112
status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
113
stdout.each_line do |line|
119
when /^# Chef Name: #{new_resource.name}\n/
127
status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
128
crontab.each { |line| stdin.puts "#{line}" }
131
Chef::Log.debug("Deleted cron '#{@new_resource.name}'")