X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Ftype%2Ffile_line.rb;h=ead6b103348c739e5a0a862509889489c04894bc;hb=30caaa85aed7015ca0d77216bff175eebd917eb7;hp=b2357b85acbbe7e55af7a9f4849138615a836c0f;hpb=6963202b4b62c2816655ac9532521b018fdf83bd;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/type/file_line.rb b/3rdparty/modules/stdlib/lib/puppet/type/file_line.rb index b2357b85a..ead6b1033 100644 --- a/3rdparty/modules/stdlib/lib/puppet/type/file_line.rb +++ b/3rdparty/modules/stdlib/lib/puppet/type/file_line.rb @@ -1,62 +1,92 @@ Puppet::Type.newtype(:file_line) do - - desc <<-EOT - Ensures that a given line is contained within a file. The implementation - matches the full line, including whitespace at the beginning and end. If - the line is not contained in the given file, Puppet will append the line to - the end of the file to ensure the desired state. Multiple resources may - be declared to manage multiple lines in the same file. - - Example: - - file_line { 'sudo_rule': - path => '/etc/sudoers', - line => '%sudo ALL=(ALL) ALL', - } - - file_line { 'sudo_rule_nopw': - path => '/etc/sudoers', - line => '%sudonopw ALL=(ALL) NOPASSWD: ALL', - } - + desc <<-DOC + @summary + Ensures that a given line is contained within a file. + + The implementation matches the full line, including whitespace at the + beginning and end. If the line is not contained in the given file, Puppet + will append the line to the end of the file to ensure the desired state. + Multiple resources may be declared to manage multiple lines in the same file. + + * Ensure Example + ``` + file_line { 'sudo_rule': + path => '/etc/sudoers', + line => '%sudo ALL=(ALL) ALL', + } + + file_line { 'sudo_rule_nopw': + path => '/etc/sudoers', + line => '%sudonopw ALL=(ALL) NOPASSWD: ALL', + } + ``` In this example, Puppet will ensure both of the specified lines are contained in the file /etc/sudoers. - Match Example: + * Match Example - file_line { 'bashrc_proxy': - ensure => present, - path => '/etc/bashrc', - line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', - match => '^export\ HTTP_PROXY\=', - } + ``` + file_line { 'bashrc_proxy': + ensure => present, + path => '/etc/bashrc', + line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + match => '^export\ HTTP_PROXY\=', + } + ``` In this code example match will look for a line beginning with export followed by HTTP_PROXY and replace it with the value in line. - Match Example With `ensure => absent`: + * Examples With `ensure => absent`: + + This type has two behaviors when `ensure => absent` is set. - file_line { 'bashrc_proxy': - ensure => absent, - path => '/etc/bashrc', - line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', - match => '^export\ HTTP_PROXY\=', - match_for_absence => true, - } + One possibility is to set `match => ...` and `match_for_absence => true`, + as in the following example: + + ``` + file_line { 'bashrc_proxy': + ensure => absent, + path => '/etc/bashrc', + match => '^export\ HTTP_PROXY\=', + match_for_absence => true, + } + ``` In this code example match will look for a line beginning with export followed by HTTP_PROXY and delete it. If multiple lines match, an error will be raised unless the `multiple => true` parameter is set. - Encoding example: - - file_line { "XScreenSaver": - ensure => present, - path => '/root/XScreenSaver' - line => "*lock: 10:00:00", - match => '^*lock:', - encoding => "iso-8859-1", - } + Note that the `line => ...` parameter would be accepted BUT IGNORED in + the above example. + + The second way of using `ensure => absent` is to specify a `line => ...`, + and no match: + + ``` + file_line { 'bashrc_proxy': + ensure => absent, + path => '/etc/bashrc', + line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128', + } + ``` + + > *Note:* + When ensuring lines are absent this way, the default behavior + this time is to always remove all lines matching, and this behavior + can't be disabled. + + * Encoding example: + + ``` + file_line { "XScreenSaver": + ensure => present, + path => '/root/XScreenSaver', + line => "*lock: 10:00:00", + match => '^*lock:', + encoding => "iso-8859-1", + } + ``` Files with special characters that are not valid UTF-8 will give the error message "invalid byte sequence in UTF-8". In this case, determine @@ -66,9 +96,10 @@ Puppet::Type.newtype(:file_line) do **Autorequires:** If Puppet is managing the file that will contain the line being managed, the file_line resource will autorequire that file. - EOT + DOC ensurable do + desc 'Manage the state of this type.' defaultvalues defaultto :present end @@ -78,30 +109,30 @@ Puppet::Type.newtype(:file_line) do end newparam(:match) do - desc 'An optional ruby regular expression to run against existing lines in the file.' + - ' If a match is found, we replace that line rather than adding a new line.' + - ' A regex comparison is performed against the line value and if it does not' + - ' match an exception will be raised.' + desc 'An optional ruby regular expression to run against existing lines in the file. + If a match is found, we replace that line rather than adding a new line. + A regex comparison is performed against the line value and if it does not + match an exception will be raised.' end newparam(:match_for_absence) do - desc 'An optional value to determine if match should be applied when ensure => absent.' + - ' If set to true and match is set, the line that matches match will be deleted.' + - ' If set to false (the default), match is ignored when ensure => absent.' + - ' When `ensure => present`, match_for_absence is ignored.' + desc 'An optional value to determine if match should be applied when ensure => absent. + If set to true and match is set, the line that matches match will be deleted. + If set to false (the default), match is ignored when ensure => absent. + When `ensure => present`, match_for_absence is ignored.' newvalues(true, false) defaultto false end newparam(:multiple) do - desc 'An optional value to determine if match can change multiple lines.' + - ' If set to false, an exception will be raised if more than one line matches' + desc 'An optional value to determine if match can change multiple lines. + If set to false, an exception will be raised if more than one line matches' newvalues(true, false) end newparam(:after) do - desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' + - ' This is also takes a regex.' + desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place) + This is also takes a regex.' end # The line property never changes; the type only ever performs a create() or @@ -131,6 +162,13 @@ Puppet::Type.newtype(:file_line) do defaultto true end + newparam(:replace_all_matches_not_matching_line) do + desc 'Configures the behavior of replacing all lines in a file which match the `match` parameter regular expression, regardless of whether the specified line is already present in the file.' + + newvalues(true, false) + defaultto false + end + newparam(:encoding) do desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1' defaultto 'UTF-8' @@ -146,15 +184,20 @@ Puppet::Type.newtype(:file_line) do autorequire(:file) do self[:path] end - validate do + if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false' + raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true') + end + if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false' + raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true') + end unless self[:line] - unless (self[:ensure].to_s == 'absent') and (self[:match_for_absence].to_s == 'true') and self[:match] - raise(Puppet::Error, "line is a required attribute") + unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match] + raise(Puppet::Error, 'line is a required attribute') end end unless self[:path] - raise(Puppet::Error, "path is a required attribute") + raise(Puppet::Error, 'path is a required attribute') end end end