summaryrefslogtreecommitdiffstats
path: root/lib/puppet/provider/cron/crontab.rb
blob: c50bc26802aa8e9d0bc63f1fe8d063b786955066 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'puppet/provider/parsedfile'

tab = case Facter.value(:operatingsystem)
    when "Solaris": :suntab
    else
        :crontab
    end

Puppet::Type.type(:cron).provide(:crontab,
    :parent => Puppet::Provider::ParsedFile,
    :filetype => tab
) do
    commands :crontab => "crontab"

    attr_accessor :target

    text_line :comment, :match => %r{^#}, :post_parse => proc { |hash|
        if hash[:line] =~ /Puppet Name: (.+)\s*$/
            hash[:name] = $1
        end
    }

    text_line :blank, :match => /^\s+/

    text_line :environment, :match => %r{/^\w+=/}

    record_line :crontab, :fields => %w{minute hour weekday month monthday command},
        :optional => %w{minute hour weekday month monthday}, :absent => "*"

    #record_line :freebsd_special, :fields => %w{special command},
    #    :match => %r{^@(\w+)\s+(.+)}
    # Apparently Freebsd will "helpfully" add a new TZ line to every
    # single cron line, but not in all cases (e.g., it doesn't do it
    # on my machine.  This is my attempt to fix it so the TZ lines don't
    # multiply.
    #if tab =~ /^TZ=.+$/
    #    return tab.sub(/\n/, "\n" + self.header)
    #else
    #    return self.header() + tab
    #end


    # Return the header placed at the top of each generated file, warning
    # users that modifying this file manually is probably a bad idea.
    def self.header
%{# HEADER This file was autogenerated at #{Time.now} by puppet.  While it
# HEADER can still be managed manually, it is definitely not recommended.
# HEADER Note particularly that the comments starting with 'Puppet Name' should
# HEADER not be deleted, as doing so could cause duplicate cron jobs.\n}
    end

    # Collapse name and env records.
    def self.prefetch_hook(records)
        name = nil
        envs = []
        records.each { |record|
            case record[:record_type]
            when :comment:
                if record[:name]
                    name = record[:name]
                    record[:skip] = true
                end
            when :environment:
                if name
                    envs << record[:line]
                    record[:skip] = true
                end
            else
                if name
                    record[:name] = name
                    name = nil
                end
                unless envs.empty?
                    record[:environment] = envs
                    envs = []
                end
            end
        }.reject { |record| record[:skip] }
    end

    def user=(user)
        self.target = target
    end

    def user
        self.target
    end
end

# $Id$