]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/type/file_line.rb
upgrade to stdlib 4.6.1
[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     **Autorequires:** If Puppet is managing the file that will contain the line
38     being managed, the file_line resource will autorequire that file.
39
40   EOT
41
42   ensurable do
43     defaultvalues
44     defaultto :present
45   end
46
47   newparam(:name, :namevar => true) do
48     desc 'An arbitrary name used as the identity of the resource.'
49   end
50
51   newparam(:match) do
52     desc 'An optional ruby regular expression to run against existing lines in the file.' + 
53          ' If a match is found, we replace that line rather than adding a new line.' +
54          ' A regex comparisson is performed against the line value and if it does not' +
55          ' match an exception will be raised. '
56   end
57
58   newparam(:multiple) do
59     desc 'An optional value to determine if match can change multiple lines.' +
60          ' If set to false, an exception will be raised if more than one line matches'
61     newvalues(true, false)
62   end
63
64   newparam(:after) do
65     desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)'
66   end
67
68   newparam(:line) do
69     desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
70   end
71
72   newparam(:path) do
73     desc 'The file Puppet will ensure contains the line specified by the line parameter.'
74     validate do |value|
75       unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/))
76         raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'")
77       end
78     end
79   end
80
81   # Autorequire the file resource if it's being managed
82   autorequire(:file) do
83     self[:path]
84   end
85
86   validate do
87     unless self[:line] and self[:path]
88       raise(Puppet::Error, "Both line and path are required attributes")
89     end
90   end
91 end