From ee2225b63c4356dafffedc16ec0376af58d42dad Mon Sep 17 00:00:00 2001 From: Gerrard Geldenhuis Date: Fri, 24 Oct 2014 15:41:58 +0100 Subject: Clarifying behaviour of attributes and adding an extra example. --- lib/puppet/type/file_line.rb | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index df263e6..29f9538 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -3,9 +3,9 @@ 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 add the line to - ensure the desired state. Multiple resources may be declared to manage - multiple lines in the same file. + 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: @@ -13,6 +13,7 @@ Puppet::Type.newtype(:file_line) do path => '/etc/sudoers', line => '%sudo ALL=(ALL) ALL', } + file_line { 'sudo_rule_nopw': path => '/etc/sudoers', line => '%sudonopw ALL=(ALL) NOPASSWD: ALL', @@ -21,6 +22,18 @@ Puppet::Type.newtype(:file_line) do In this example, Puppet will ensure both of the specified lines are contained in the file /etc/sudoers. + Match Example: + + 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. + **Autorequires:** If Puppet is managing the file that will contain the line being managed, the file_line resource will autorequire that file. @@ -36,12 +49,15 @@ Puppet::Type.newtype(:file_line) do end newparam(:match) do - desc 'An optional regular expression to run against existing lines in the file;\n' + - 'if a match is found, we replace that line rather than adding a new line.' + 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 comparisson is performed against the line value and if it does not' + + ' match an exception will be raised. ' end newparam(:multiple) do - desc 'An optional value to determine if match can change multiple lines.' + 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 @@ -50,7 +66,7 @@ Puppet::Type.newtype(:file_line) do end newparam(:line) do - desc 'The line to be appended to the file located by the path parameter.' + desc 'The line to be appended to the file or used to replace matches found by the match attribute.' end newparam(:path) do -- cgit v1.2.3 From 35e92645f727f02ef9ace8948154079bc0fff05a Mon Sep 17 00:00:00 2001 From: Raymond Maika Date: Thu, 30 Jul 2015 14:05:39 -0400 Subject: (MODULES-2024) Adding replace attribute to file_line --- lib/puppet/type/file_line.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 29f9538..190105c 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -1,3 +1,4 @@ +require 'puppet/parameter/boolean' Puppet::Type.newtype(:file_line) do desc <<-EOT @@ -78,6 +79,11 @@ Puppet::Type.newtype(:file_line) do end end + newparam(:replace, :boolean => true, :parent => Puppet::Parameter::Boolean) do + desc 'If true, replace line that matches. If false, do not write line if a match is found' + defaultto true + end + # Autorequire the file resource if it's being managed autorequire(:file) do self[:path] -- cgit v1.2.3 From a7adcda803abe82e6a16e2410c10d58abedbd82d Mon Sep 17 00:00:00 2001 From: Dominic Cleal Date: Tue, 4 Aug 2015 09:59:53 +0100 Subject: (MODULES-2316) Change file_type boolean parameter to symbols Puppet's boolean parameter type is only available in Puppet 3.3 and higher, so change file_type's new "replace" parameter to a regular parameter with true and false as possible values. This matches the existing "multiple" parameter. --- lib/puppet/type/file_line.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 190105c..4a96ba7 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -1,4 +1,3 @@ -require 'puppet/parameter/boolean' Puppet::Type.newtype(:file_line) do desc <<-EOT @@ -79,8 +78,9 @@ Puppet::Type.newtype(:file_line) do end end - newparam(:replace, :boolean => true, :parent => Puppet::Parameter::Boolean) do + newparam(:replace) do desc 'If true, replace line that matches. If false, do not write line if a match is found' + newvalues(true, false) defaultto true end -- cgit v1.2.3 From 9bacf14ca24283a94883523064603babcd7046d3 Mon Sep 17 00:00:00 2001 From: Johnson Earls Date: Thu, 6 Aug 2015 13:00:11 -0700 Subject: allow `match` parameter to influence `ensure => absent` behavior. Split the `destroy` method of the file_type::ruby provider into two private methods: `handle_destroy_line` which is the same as the previous `destroy` method, and `handle_destroy_with_match` which will destroy any line which matches the `match` parameter, raising an error if multiple lines match and the `multiple` parameter is not `true`. This new behavior is only used if the new boolean parameter `match_for_absence` is `true` (it defaults to `false`). --- lib/puppet/type/file_line.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 4a96ba7..446f103 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -34,6 +34,20 @@ Puppet::Type.newtype(:file_line) do 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`: + + 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, + } + + 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. + **Autorequires:** If Puppet is managing the file that will contain the line being managed, the file_line resource will autorequire that file. @@ -55,6 +69,14 @@ Puppet::Type.newtype(:file_line) do ' 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.' + 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' -- cgit v1.2.3 From ad173f2d0552ad9ed42950aea7df8d2b22677904 Mon Sep 17 00:00:00 2001 From: David Schmitt Date: Tue, 13 Oct 2015 15:02:04 +0100 Subject: (MODULES-2421) improve description of file_line This mostly needed extraction of the existing doc strings from the type. --- lib/puppet/type/file_line.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 446f103..77d3be2 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -4,7 +4,7 @@ Puppet::Type.newtype(:file_line) do 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 + the end of the file to ensure the desired state. Multiple resources may be declared to manage multiple lines in the same file. Example: @@ -31,7 +31,7 @@ Puppet::Type.newtype(:file_line) do match => '^export\ HTTP_PROXY\=', } - In this code example match will look for a line beginning with export + 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`: @@ -50,7 +50,6 @@ 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 ensurable do @@ -63,10 +62,10 @@ Puppet::Type.newtype(:file_line) do end newparam(:match) do - desc 'An optional ruby regular expression to run against existing lines in the file.' + + 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 comparisson is performed against the line value and if it does not' + - ' match an exception will be raised. ' + ' 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 -- cgit v1.2.3 From 7e408ca7970fd172822db02227935798f9ff282f Mon Sep 17 00:00:00 2001 From: Johnson Earls Date: Mon, 28 Mar 2016 20:59:27 +0000 Subject: [MODULES-2370] file_line.rb: Fix `line` attribute validation `file_line` type: During validation, do not require `line` attribute if: * `ensure` is `absent`, * `match` is not empty, * and `match_for_absence` is `true`. Also update `spec` tests to reflect this. --- lib/puppet/type/file_line.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 77d3be2..f2c6937 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -111,8 +111,13 @@ Puppet::Type.newtype(:file_line) do end validate do - unless self[:line] and self[:path] - raise(Puppet::Error, "Both line and path are required attributes") + 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") + end + end + unless self[:path] + raise(Puppet::Error, "path is a required attribute") end end end -- cgit v1.2.3 From 7b1250478c513a3c02c463ec7cdd62d427957400 Mon Sep 17 00:00:00 2001 From: Bryan Jen Date: Tue, 28 Jun 2016 16:45:56 -0700 Subject: (MODULES-3507) Updates file_line path validation --- lib/puppet/type/file_line.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index f2c6937..a02b514 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -93,8 +93,8 @@ Puppet::Type.newtype(:file_line) do newparam(:path) do desc 'The file Puppet will ensure contains the line specified by the line parameter.' validate do |value| - unless (Puppet.features.posix? and value =~ /^\//) or (Puppet.features.microsoft_windows? and (value =~ /^.:\// or value =~ /^\/\/[^\/]+\/[^\/]+/)) - raise(Puppet::Error, "File paths must be fully qualified, not '#{value}'") + unless Puppet::Util.absolute_path?(value) + raise Puppet::Error, "File paths must be fully qualified, not '#{value}'" end end end -- cgit v1.2.3 From 1b36d540fde3f149bd2738384ad9065bd7f6c4bb Mon Sep 17 00:00:00 2001 From: tphoney Date: Tue, 19 Jul 2016 17:02:42 +0100 Subject: (modules-3407) documenting after can take a regex --- lib/puppet/type/file_line.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index a02b514..6f5c188 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -83,7 +83,8 @@ Puppet::Type.newtype(:file_line) do 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)' + 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 newparam(:line) do -- cgit v1.2.3 From bcab71ded8507de4fd9f89b4dcf798b3d98ace59 Mon Sep 17 00:00:00 2001 From: Hailee Kenney Date: Thu, 6 Oct 2016 15:23:35 -0700 Subject: (MODULES-3590) Fix match_for_absence parameter Prior to this commit, due to a bug in the exists? method in the file_line provider, match_for_absence didn't work as described (or at all really). Update the exists? logic so that match_for_absence works as described. Additionally add a unit test to prevent regressions and update the documentation for the parameter to reflect the fact that it is ignored when `ensure => present`. --- lib/puppet/type/file_line.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/type/file_line.rb') diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb index 6f5c188..7b7d44e 100644 --- a/lib/puppet/type/file_line.rb +++ b/lib/puppet/type/file_line.rb @@ -71,7 +71,8 @@ Puppet::Type.newtype(:file_line) do 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.' + ' 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 -- cgit v1.2.3