~ubuntu-branches/ubuntu/karmic/chef/karmic

« back to all changes in this revision

Viewing changes to chef/lib/chef/provider/cron.rb

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Timberman
  • Date: 2009-08-12 12:18:48 UTC
  • Revision ID: james.westby@ubuntu.com-20090812121848-yl38hpd7nfsl51xz
Tags: upstream-0.7.8
ImportĀ upstreamĀ versionĀ 0.7.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# Author:: Bryan McLellan (btm@loftninjas.org)
 
3
# Copyright:: Copyright (c) 2009 Bryan McLellan
 
4
# License:: Apache License, Version 2.0
 
5
#
 
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
 
9
 
10
#     http://www.apache.org/licenses/LICENSE-2.0
 
11
 
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.
 
17
#
 
18
 
 
19
require 'chef/log'
 
20
require 'chef/mixin/command'
 
21
require 'chef/provider'
 
22
 
 
23
class Chef
 
24
  class Provider
 
25
    class Cron < Chef::Provider
 
26
      include Chef::Mixin::Command
 
27
 
 
28
      def initialize(node, new_resource)
 
29
        super(node, new_resource)
 
30
        @cron_exists = false
 
31
        @cron_empty = false
 
32
      end
 
33
      attr_accessor :cron_exists, :cron_empty
 
34
 
 
35
      def load_current_resource
 
36
        crontab = String.new
 
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 }
 
40
        end
 
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|
 
45
            case line
 
46
            when /^# Chef Name: #{@new_resource.name}/
 
47
              Chef::Log.debug("Found cron '#{@new_resource.name}'")
 
48
              @cron_exists = true
 
49
            end
 
50
          end
 
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}'")
 
54
          @cron_empty = true
 
55
        end
 
56
        
 
57
        @current_resource
 
58
      end
 
59
 
 
60
      def action_create
 
61
        crontab = String.new
 
62
        cron_found = false
 
63
        if @cron_exists
 
64
          status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
 
65
            stdout.each_line do |line|
 
66
              if cron_found
 
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
 
73
                cron_found = false
 
74
                next
 
75
              end
 
76
              case line
 
77
              when /^# Chef Name: #{new_resource.name}\n/
 
78
                cron_found = true
 
79
              end
 
80
              crontab << line 
 
81
            end
 
82
          end
 
83
 
 
84
 
 
85
          status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
 
86
            crontab.each { |line| stdin.puts "#{line}" }
 
87
            stdin.close
 
88
          end
 
89
          Chef::Log.info("Updated cron '#{@new_resource.name}'")
 
90
        else
 
91
          unless @cron_empty
 
92
            status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
 
93
              stdout.each { |line| crontab << line }
 
94
            end
 
95
          end
 
96
  
 
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"
 
99
  
 
100
          status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
 
101
            crontab.each { |line| stdin.puts "#{line}" }
 
102
            stdin.close
 
103
          end
 
104
          Chef::Log.info("Added cron '#{@new_resource.name}'")
 
105
        end
 
106
      end
 
107
 
 
108
      def action_delete
 
109
        if @cron_exists
 
110
          crontab = String.new
 
111
          cron_found = false
 
112
          status = popen4("crontab -l -u #{@new_resource.user}") do |pid, stdin, stdout, stderr|
 
113
            stdout.each_line do |line|
 
114
              if cron_found
 
115
                cron_found = false
 
116
                next
 
117
              end
 
118
              case line
 
119
              when /^# Chef Name: #{new_resource.name}\n/
 
120
                cron_found = true
 
121
                next
 
122
              end
 
123
              crontab << line 
 
124
            end
 
125
          end
 
126
 
 
127
          status = popen4("crontab -u #{@new_resource.user} -", :waitlast => true) do |pid, stdin, stdout, stderr|
 
128
            crontab.each { |line| stdin.puts "#{line}" }
 
129
            stdin.close
 
130
          end
 
131
          Chef::Log.debug("Deleted cron '#{@new_resource.name}'")
 
132
        end
 
133
      end
 
134
 
 
135
    end
 
136
  end
 
137
end