Update stdlib
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / type / file_line.rb
1 Puppet::Type.newtype(:file_line) do
2
3   desc <<-EOT
4     Ensures that a given line is contained within a file.  The implementation
5     matches the full line, including whitespace at the beginning and end.  If
6     the line is not contained in the given file, Puppet will append the line to
7     the end of the file to ensure the desired state.  Multiple resources may
8     be declared to manage multiple lines in the same file.
9
10     Example:
11
12         file_line { 'sudo_rule':
13           path => '/etc/sudoers',
14           line => '%sudo ALL=(ALL) ALL',
15         }
16
17         file_line { 'sudo_rule_nopw':
18           path => '/etc/sudoers',
19           line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
20         }
21
22     In this example, Puppet will ensure both of the specified lines are
23     contained in the file /etc/sudoers.
24
25     Match Example:
26
27         file_line { 'bashrc_proxy':
28           ensure => present,
29           path   => '/etc/bashrc',
30           line   => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
31           match  => '^export\ HTTP_PROXY\=',
32         }
33
34     In this code example match will look for a line beginning with export
35     followed by HTTP_PROXY and replace it with the value in line.
36
37     Match Example With `ensure => absent`:
38
39         file_line { 'bashrc_proxy':
40           ensure            => absent,
41           path              => '/etc/bashrc',
42           line              => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
43           match             => '^export\ HTTP_PROXY\=',
44           match_for_absence => true,
45         }
46
47     In this code example match will look for a line beginning with export
48     followed by HTTP_PROXY and delete it.  If multiple lines match, an
49     error will be raised unless the `multiple => true` parameter is set.
50
51     Encoding example:
52
53         file_line { "XScreenSaver":
54           ensure   => present,
55           path     => '/root/XScreenSaver'
56           line     => "*lock: 10:00:00",
57           match    => '^*lock:',
58           encoding => "iso-8859-1",
59         }
60
61     Files with special characters that are not valid UTF-8 will give the
62     error message "invalid byte sequence in UTF-8".  In this case, determine
63     the correct file encoding and specify the correct encoding using the
64     encoding attribute, the value of which needs to be a valid Ruby character
65     encoding.
66
67     **Autorequires:** If Puppet is managing the file that will contain the line
68     being managed, the file_line resource will autorequire that file.
69   EOT
70
71   ensurable do
72     defaultvalues
73     defaultto :present
74   end
75
76   newparam(:name, :namevar => true) do
77     desc 'An arbitrary name used as the identity of the resource.'
78   end
79
80   newparam(:match) do
81     desc 'An optional ruby regular expression to run against existing lines in the file.' +
82          ' If a match is found, we replace that line rather than adding a new line.' +
83          ' A regex comparison is performed against the line value and if it does not' +
84          ' match an exception will be raised.'
85   end
86
87   newparam(:match_for_absence) do
88     desc 'An optional value to determine if match should be applied when ensure => absent.' +
89          ' If set to true and match is set, the line that matches match will be deleted.' +
90          ' If set to false (the default), match is ignored when ensure => absent.' +
91          ' When `ensure => present`, match_for_absence is ignored.'
92     newvalues(true, false)
93     defaultto false
94   end
95
96   newparam(:multiple) do
97     desc 'An optional value to determine if match can change multiple lines.' +
98          ' If set to false, an exception will be raised if more than one line matches'
99     newvalues(true, false)
100   end
101
102   newparam(:after) do
103     desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' +
104          ' This is also takes a regex.'
105   end
106
107   # The line property never changes; the type only ever performs a create() or
108   # destroy(). line is a property in order to allow it to correctly handle
109   # Sensitive type values. Because it is a property which will never change,
110   # it should never be considered out of sync.
111   newproperty(:line) do
112     desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
113
114     def retrieve
115       @resource[:line]
116     end
117   end
118
119   newparam(:path) do
120     desc 'The file Puppet will ensure contains the line specified by the line parameter.'
121     validate do |value|
122       unless Puppet::Util.absolute_path?(value)
123         raise Puppet::Error, "File paths must be fully qualified, not '#{value}'"
124       end
125     end
126   end
127
128   newparam(:replace) do
129     desc 'If true, replace line that matches. If false, do not write line if a match is found'
130     newvalues(true, false)
131     defaultto true
132   end
133
134   newparam(:encoding) do
135     desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
136     defaultto 'UTF-8'
137   end
138
139   newparam(:append_on_no_match) do
140     desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
141     newvalues(true, false)
142     defaultto true
143   end
144
145   # Autorequire the file resource if it's being managed
146   autorequire(:file) do
147     self[:path]
148   end
149
150   validate do
151     unless self[:line]
152       unless (self[:ensure].to_s == 'absent') and (self[:match_for_absence].to_s == 'true') and self[:match]
153         raise(Puppet::Error, "line is a required attribute")
154       end
155     end
156     unless self[:path]
157       raise(Puppet::Error, "path is a required attribute")
158     end
159   end
160 end