~ubuntu-branches/ubuntu/lucid/puppet/lucid-security

« back to all changes in this revision

Viewing changes to lib/puppet/type/file/content.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2009-12-23 00:48:10 UTC
  • mfrom: (1.1.10 upstream) (3.1.7 squeeze)
  • Revision ID: james.westby@ubuntu.com-20091223004810-3i4oryds922g5n59
Tags: 0.25.1-3ubuntu1
* Merge from debian testing.  Remaining changes:
  - debian/rules:
    + Don't start puppet when first installing puppet.
  - debian/puppet.conf, lib/puppet/defaults.rb:
    + Move templates to /etc/puppet
  - lib/puppet/defaults.rb:
    + Fix /var/lib/puppet/state ownership.
  - man/man8/puppet.conf.8: 
    + Fix broken URL in manpage.
  - debian/control:
    + Update maintainer accordint to spec.
    + Puppetmaster Recommends -> Suggests
    + Created puppet-testsuite as a seperate. Allow the users to run puppet's 
      testsuite.
  - tests/Rakefile: Fix rakefile so that the testsuite can acutally be ran.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'puppet/util/checksums'
 
2
 
1
3
module Puppet
2
4
    Puppet::Type.type(:file).newproperty(:content) do
3
5
        include Puppet::Util::Diff
 
6
        include Puppet::Util::Checksums
4
7
 
5
8
        desc "Specify the contents of a file as a string.  Newlines, tabs, and
6
9
            spaces can be specified using the escaped syntax (e.g., \\n for a
18
21
                        content => $str
19
22
                    }
20
23
                }
21
 
            
 
24
 
22
25
            This attribute is especially useful when used with
23
26
            `PuppetTemplating templating`:trac:."
24
27
 
25
 
        def string_as_checksum(string)
26
 
            return "absent" if string == :absent
27
 
            "{md5}" + Digest::MD5.hexdigest(string)
28
 
        end
29
 
 
30
 
        def should_to_s(should)
31
 
            string_as_checksum(should)
32
 
        end
33
 
 
34
 
        def is_to_s(is)
35
 
            string_as_checksum(is)
 
28
        # Store a checksum as the value, rather than the actual content.
 
29
        # Simplifies everything.
 
30
        munge do |value|
 
31
            if value == :absent
 
32
                value
 
33
            else
 
34
                @actual_content = value
 
35
                "{#{checksum_type}}" + send(self.checksum_type, value)
 
36
            end
 
37
        end
 
38
 
 
39
        # Checksums need to invert how changes are printed.
 
40
        def change_to_s(currentvalue, newvalue)
 
41
            # Our "new" checksum value is provided by the source.
 
42
            unless source = resource.parameter(:source) and newvalue = source.checksum
 
43
                newvalue = "unknown checksum"
 
44
            end
 
45
            if currentvalue == :absent
 
46
                return "defined content as '%s'" % [newvalue]
 
47
            elsif newvalue == :absent
 
48
                return "undefined content from '%s'" % [currentvalue]
 
49
            else
 
50
                return "content changed '%s' to '%s'" % [currentvalue, newvalue]
 
51
            end
 
52
        end
 
53
 
 
54
        def checksum_type
 
55
            if source = resource.parameter(:source)
 
56
                result = source.checksum
 
57
            elsif checksum = resource.parameter(:checksum)
 
58
                result = checksum.checktype
 
59
            else
 
60
                return :md5
 
61
            end
 
62
            if result =~ /^\{(\w+)\}.+/
 
63
                return $1.to_sym
 
64
            else
 
65
                return result
 
66
            end
 
67
        end
 
68
 
 
69
        # If content was specified, return that; else try to return the source content;
 
70
        # else, return nil.
 
71
        def actual_content
 
72
            if defined?(@actual_content) and @actual_content
 
73
                return @actual_content
 
74
            end
 
75
 
 
76
            if s = resource.parameter(:source)
 
77
                return s.content
 
78
            end
 
79
            return nil
 
80
        end
 
81
 
 
82
        def content
 
83
            self.should || (s = resource.parameter(:source) and s.content)
36
84
        end
37
85
 
38
86
        # Override this method to provide diffs if asked for.
39
87
        # Also, fix #872: when content is used, and replace is true, the file
40
88
        # should be insync when it exists
41
89
        def insync?(is)
42
 
            if ! @resource.replace? and File.exists?(@resource[:path])
43
 
                return true
44
 
            end
45
 
 
46
 
            result = super
47
 
            if ! result and Puppet[:show_diff] and File.exists?(@resource[:path])
48
 
                string_file_diff(@resource[:path], self.should)
 
90
            if resource.should_be_file?
 
91
                return false if is == :absent
 
92
            else
 
93
                return true
 
94
            end
 
95
 
 
96
            return true if ! @resource.replace?
 
97
 
 
98
            if self.should
 
99
                return super
 
100
            elsif source = resource.parameter(:source)
 
101
                fail "Got a remote source with no checksum" unless source.checksum
 
102
                result = (is == source.checksum)
 
103
            else
 
104
                # We've got no content specified, and no source from which to
 
105
                # get content.
 
106
                return true
 
107
            end
 
108
 
 
109
            if ! result and Puppet[:show_diff]
 
110
                string_file_diff(@resource[:path], actual_content)
49
111
            end
50
112
            return result
51
113
        end
53
115
        def retrieve
54
116
            return :absent unless stat = @resource.stat
55
117
 
56
 
            return self.should if stat.ftype == "link" and @resource[:links] == :ignore
57
 
 
58
 
            # Don't even try to manage the content on directories
 
118
            # Don't even try to manage the content on directories or links
59
119
            return nil if stat.ftype == "directory"
60
120
 
61
121
            begin
62
 
                currentvalue = File.read(@resource[:path])
63
 
                return currentvalue
 
122
                return "{#{checksum_type}}" + send(checksum_type.to_s + "_file", resource[:path]).to_s
64
123
            rescue => detail
65
 
                raise Puppet::Error, "Could not read %s: %s" %
66
 
                    [@resource.title, detail]
 
124
                raise Puppet::Error, "Could not read %s: %s" % [@resource.title, detail]
67
125
            end
68
126
        end
69
127
 
70
128
        # Make sure we're also managing the checksum property.
71
129
        def should=(value)
 
130
            @resource.newattr(:checksum) unless @resource.parameter(:checksum)
72
131
            super
73
 
            @resource.newattr(:checksum) unless @resource.property(:checksum)
74
132
        end
75
133
 
76
134
        # Just write our content out to disk.
77
135
        def sync
78
136
            return_event = @resource.stat ? :file_changed : :file_created
79
 
            
80
 
            @resource.write(self.should, :content)
 
137
 
 
138
            # We're safe not testing for the 'source' if there's no 'should'
 
139
            # because we wouldn't have gotten this far if there weren't at least
 
140
            # one valid value somewhere.
 
141
            @resource.write(actual_content, :content)
81
142
 
82
143
            return return_event
83
144
        end