diff --git a/CHANGELOG.md b/CHANGELOG.md index 59204bb..87a0443 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,261 +1,281 @@ +======= +##2015-06-02 - Supported Release 2.0.1 +###Summary + +This is a bugfix release. + +####Bugfixes +- Fixes dependency graphing with concurrent modification of the same file. +- Fixes handling fragment target. +- Fixes the defaulted force behavior to handle empty concats correctly. + ======= ##2015-05-12 - Supported Release 2.0.0 ###Summary This is a major release. Includes re-implementation of concat to use native Type and Providers, resulting in significantly improved performance and testability. Also includes a bugfix to alpha ordering of fragments. ####Features - Re-implementation of concat to use native Type and Providers. ####Bugfixes - Fixes a bug in alpha ordering of fragments. +======= +##2015-06-02 - Supported Release 1.2.3 +###Summary + +This release includes a README fix to document correct behavior of fragment target parameter. + +####Bugfixes + +- README Fix to correctly document how a fragment $target param should work. ##2015-05-12 - Supported Release 1.2.2 ###Summary This release includes a bugfix. ####Bugfixes - Fixes a bug introduced by MODULES-1700, in handling default retrieval of fragment backup parameter. ##2015-04-14 - Supported Release 1.2.1 ###Summary This release includes bugfixes, test improvements, and a rewritten README. ####Bugfixes - Verifies existence of $is_pe before using it. - Adds validation for $order param to not allow restricted characters. - Sets the group id on Fragments for versions of Facter that contain the $gid fact. - Sets the script group to 0 if the script is owned by root. - Explicitly sets script and concat directories to the same owner and group. - Re-adds support for fragment backup, so that static compiler can work with filebucket (MODULES-1700). ##2015-02-17 - Supported Release 1.2.0 ###Summary This release includes a number of bugfixes and adds support for running a validation command when using puppet >= 3.5.0. ####Features - Support running a validation command for Puppet >= 3.5.0 ####Bugfixes - Reset poisoned defaults from Exec - Use concatfragments.rb on AIX since it doesn't support print0 - Make sure ruby is in the path for PE (MODULES-1456) - Fix missing method for check_is_owned_by for windows (MODULES-1764) - Fix sort by numeric ##2014-10-28 - Supported Release 1.1.2 ###Summary This release includes bugfixes and test improvements. The module was tested against SLES10 and SLES12 and found to work against those platforms with no module improvements. Metadata was updated to include those as supported platforms. ####Bugfixes - newline didn't work for Windows and Solaris. This has been fixed. - Install certs on Windows for acceptance tests - Update tests to work with strict variables (no module updates were required) - Update tests to work on Windows - Fix typo in CHANGELOG.md ##2014-09-10 - Supported Release 1.1.1 ###Summary This is a bugfix release, and the first supported release of the 1.1.x series. ####Bugfixes - Make the `$order` parameter default to a string and be validated as an integer or a string - Use the ruby script on Solaris to not break Sol10 support - Add quotes to the ruby script location for Windows - Fix typos in README.md - Make regex in concat::setup case-insensitive to make it work on Windows - Make sure concat fragments are always replaced - Fix validation to allow `$backup` to be a boolean - Remove dependency on stdlib 4.x - Fix for lack of idempotency with `ensure => 'absent'` - Fix tests and spec_helper - Synchronized files for more consistency across modules via modulesync ##2014-05-14 - Release 1.1.0 ###Summary This release is primarily a bugfix release since 1.1.0-rc1. ####Features - Improved testing, with tests moved to beaker ####Bugfixes - No longer attempts to set fragment owner and mode on Windows - Fix numeric sorting - Fix incorrect quoting - Fix newlines ##2014-01-03 - Release 1.1.0-rc1 ###Summary This release of concat was 90% written by Joshua Hoblitt, and the module team would like to thank him for the huge amount of work he put into this release. This module deprecates a bunch of old parameters and usage patterns, modernizes much of the manifest code, simplifies a whole bunch of logic and makes improvements to almost all parts of the module. The other major feature is windows support, courtesy of luisfdez, with an alternative version of the concat bash script in ruby. We've attempted to ensure that there are no backwards incompatible changes, all users of 1.0.0 should be able to use 1.1.0 without any failures, but you may find deprecation warnings and we'll be aggressively moving for a 2.0 to remove those too. For further information on deprecations, please read: https://github.com/puppetlabs/puppetlabs-concat/blob/master/README.md#api-deprecations ####Removed - Puppet 0.24 support. - Filebucket backup of all file resources except the target concatenated file. - Default owner/user/group values. - Purging of long unused /usr/local/bin/concatfragments.sh ###Features - Windows support via a ruby version of the concat bash script. - Huge amount of acceptance testing work added. - Documentation (README) completely rewritten. - New parameters in concat: - `ensure`: Controls if the file should be present/absent at all. - Remove requirement to include concat::setup in manifests. - Made `gnu` parameter deprecated. - Added parameter validation. ###Bugfixes - Ensure concat::setup runs before concat::fragment in all cases. - Pluginsync references updated for modern Puppet. - Fix incorrect group parameter. - Use $owner instead of $id to avoid confusion with $::id - Compatibility fixes for Puppet 2.7/ruby 1.8.7 - Use LC_ALL=C instead of LANG=C - Always exec the concatfragments script as root when running as root. - Syntax and other cleanup changes. ##2014-06-25 - Supported Release 1.0.4 ###Summary This release has test fixes. ####Features - Added test support for OSX. ####Bugfixes ####Known bugs * Not supported on Windows. ##2014-06-04 - Release 1.0.3 ###Summary This release adds compatibility for PE3.3 and fixes tests. ####Features - Added test support for Ubuntu Trusty. ####Bugfixes ####Known bugs *Not supported on Windows. ##2014-03-04 - Supported Release 1.0.2 ###Summary This is a supported release. No functional changes were made from 1.0.1. ####Features - Huge amount of tests backported from 1.1. - Documentation rewrite. ####Bugfixes ####Known Bugs * Not supported on Windows. ##2014-02-12 - 1.0.1 ###Summary Minor bugfixes for sorting of fragments and ordering of resources. ####Bugfixes - LANG => C replaced with LC_ALL => C to reduce spurious recreation of fragments. - Corrected pluginsync documentation. - Ensure concat::setup always runs before fragments. ##2013-08-09 - 1.0.0 ###Summary Many new features and bugfixes in this release, and if you're a heavy concat user you should test carefully before upgrading. The features should all be backwards compatible but only light testing has been done from our side before this release. ####Features - New parameters in concat: - `replace`: specify if concat should replace existing files. - `ensure_newline`: controls if fragments should contain a newline at the end. - Improved README documentation. - Add rspec:system tests (rake spec:system to test concat) ####Bugfixes - Gracefully handle \n in a fragment resource name. - Adding more helpful message for 'pluginsync = true' - Allow passing `source` and `content` directly to file resource, rather than defining resource defaults. - Added -r flag to read so that filenames with \ will be read correctly. - sort always uses LANG=C. - Allow WARNMSG to contain/start with '#'. - Replace while-read pattern with for-do in order to support Solaris. ####CHANGELOG: - 2010/02/19 - initial release - 2010/03/12 - add support for 0.24.8 and newer - make the location of sort configurable - add the ability to add shell comment based warnings to top of files - add the ablity to create empty files - 2010/04/05 - fix parsing of WARN and change code style to match rest of the code - Better and safer boolean handling for warn and force - Don't use hard coded paths in the shell script, set PATH top of the script - Use file{} to copy the result and make all fragments owned by root. This means we can chnage the ownership/group of the resulting file at any time. - You can specify ensure => "/some/other/file" in concat::fragment to include the contents of a symlink into the final file. - 2010/04/16 - Add more cleaning of the fragment name - removing / from the $name - 2010/05/22 - Improve documentation and show the use of ensure => - 2010/07/14 - Add support for setting the filebucket behavior of files - 2010/10/04 - Make the warning message configurable - 2010/12/03 - Add flags to make concat work better on Solaris - thanks Jonathan Boyett - 2011/02/03 - Make the shell script more portable and add a config option for root group - 2011/06/21 - Make base dir root readable only for security - 2011/06/23 - Set base directory using a fact instead of hardcoding it - 2011/06/23 - Support operating as non privileged user - 2011/06/23 - Support dash instead of bash or sh - 2011/07/11 - Better solaris support - 2011/12/05 - Use fully qualified variables - 2011/12/13 - Improve Nexenta support - 2012/04/11 - Do not use any GNU specific extensions in the shell script - 2012/03/24 - Comply to community style guides - 2012/05/23 - Better errors when basedir isnt set - 2012/05/31 - Add spec tests - 2012/07/11 - Include concat::setup in concat improving UX - 2012/08/14 - Puppet Lint improvements - 2012/08/30 - The target path can be different from the $name - 2012/08/30 - More Puppet Lint cleanup - 2012/09/04 - RELEASE 0.2.0 - 2012/12/12 - Added (file) $replace parameter to concat diff --git a/README.md b/README.md index 2b98834..8a041ce 100644 --- a/README.md +++ b/README.md @@ -1,321 +1,321 @@ #concat ####Table of Contents 1. [Overview](#overview) 2. [Module Description - What the module does and why it is useful](#module-description) 3. [Setup - The basics of getting started with concat](#setup) * [What concat affects](#what-concat-affects) * [Beginning with concat](#beginning-with-concat) 4. [Usage - Configuration options and additional functionality](#usage) 5. [Reference - An under-the-hood peek at what the module is doing and how](#reference) * [Defines](#defines) * [Parameters](#parameters) * [Removed functionality](#removed-functionality) 6. [Limitations - OS compatibility, etc.](#limitations) 7. [Development - Guide for contributing to the module](#development) ##Overview The concat module lets you construct files from multiple ordered fragments of text. ##Module Description The concat module lets you gather `concat::fragment` resources from your other modules and order them into a coherent file through a single `concat` resource. ###Beginning with concat To start using concat you need to create: * A concat{} resource for the final file. * One or more concat::fragment{}s. A minimal example might be: ~~~ concat { '/tmp/file': ensure => present, } concat::fragment { 'tmpfile': target => '/tmp/file', content => 'test contents', order => '01' } ~~~ ##Usage ###Maintain a list of the major modules on a node To maintain an motd file that lists the modules on one of your nodes, first create a class to frame up the file: ~~~ class motd { $motd = '/etc/motd' concat { $motd: owner => 'root', group => 'root', mode => '0644' } concat::fragment{ 'motd_header': target => $motd, content => "\nPuppet modules on this server:\n\n", order => '01' } # let local users add to the motd by creating a file called # /etc/motd.local concat::fragment{ 'motd_local': target => $motd, source => '/etc/motd.local', order => '15' } } # let other modules register themselves in the motd define motd::register($content="", $order='10') { if $content == "" { $body = $name } else { $body = $content } concat::fragment{ "motd_fragment_$name": target => '/etc/motd', order => $order, content => " -- $body\n" } } ~~~ Then, in the declarations for each module on the node, add `motd::register{ 'Apache': }` to register the module in the motd. ~~~ class apache { include apache::install, apache::config, apache::service motd::register{ 'Apache': } } ~~~ These two steps populate the /etc/motd file with a list of the installed and registered modules, which stays updated even if you just remove the registered modules' `include` lines. System administrators can append text to the list by writing to /etc/motd.local. When you're finished, the motd file will look something like this: ~~~ Puppet modules on this server: -- Apache -- MySQL ~~~ ##Reference ###Defines * `concat`: Manages a file, compiled from one or more text fragments. * `concat::fragment`: Manages a fragment of text to be compiled into a file. ###Types * `concat_file`: Generates a file with content from fragments sharing a common unique tag. * `concat_fragment`: Manages the fragment. ###Parameters ####Define: `concat` All the parameters listed below are optional. #####`backup` Specifies whether (and how) to back up the destination file before overwriting it. Your value gets passed on to Puppet's [native `file` resource](https://docs.puppetlabs.com/references/latest/type.html#file-attribute-backup) for execution. Valid options: 'true', 'false', or a string representing either a target filebucket or a filename extension beginning with ".". Default value: 'puppet'. #####`ensure` Specifies whether the destination file should exist. Setting to 'absent' tells Puppet to delete the destination file if it exists, and negates the effect of any other parameters. Valid options: 'present' and 'absent'. Default value: 'present'. #####`ensure_newline` Specifies whether to add a line break at the end of each fragment that doesn't already end in one. Valid options: 'true' and 'false'. Default value: 'false'. #####`force` Deprecated as of concat v2.0.0. Has no effect. #####`group` Specifies a permissions group for the destination file. Valid options: a string containing a group name. Default value: undefined. #####`mode` Specifies the permissions mode of the destination file. Valid options: a string containing a permission mode value in octal notation. Default value: '0644'. #####`order` Specifies a method for sorting your fragments by name within the destination file. Valid options: 'alpha' (e.g., '1, 10, 2') or 'numeric' (e.g., '1, 2, 10'). Default value: 'alpha'. You can override this setting for individual fragments by adjusting the `order` parameter in their `concat::fragment` declarations. #####`owner` Specifies the owner of the destination file. Valid options: a string containing a username. Default value: undefined. #####`path` Specifies a destination file for the combined fragments. Valid options: a string containing an absolute path. Default value: the title of your declared resource. #####`replace` Specifies whether to overwrite the destination file if it already exists. Valid options: 'true' and 'false'. Default value: 'true'. #####`validate_cmd` Specifies a validation command to apply to the destination file. Requires Puppet version 3.5 or newer. Valid options: a string to be passed to a file resource. Default value: undefined. #####`warn` Specifies whether to add a header message at the top of the destination file. Valid options: the booleans 'true' and 'false', or a string to serve as the header. Default value: 'false'. If you set 'warn' to 'true', `concat` adds the following line with an `order` of `0`: ~~~ # This file is managed by Puppet. DO NOT EDIT. ~~~ Before 2.0.0, this parameter would add a newline at the end of the warn message. To improve flexibilty, this was removed. Please add it explicitely if you need it. ####Define: `concat::fragment` Except where noted, all the below parameters are optional. #####`content` Supplies the content of the fragment. **Note**: You must supply either a `content` parameter or a `source` parameter. Valid options: a string. Default value: undef. #####`ensure` Deprecated as of concat v2.0.0. Has no effect. #####`order` Reorders your fragments within the destination file. Fragments that share the same order number are ordered by name. Valid options: a string (recommended) or an integer. Default value: '10'. #####`source` Specifies a file to read into the content of the fragment. **Note**: You must supply either a `content` parameter or a `source` parameter. Valid options: a string or an array, containing one or more Puppet URLs. Default value: undefined. #####`target` -*Required.* Specifies the destination file of the fragment. Valid options: a string containing an absolute path. +*Required.* Specifies the destination file of the fragment. Valid options: a string containing the title of the parent `concat` resource. ####Type: `concat_file` #####`backup` Specifies whether (and how) to back up the destination file before overwriting it. Your value gets passed on to Puppet's [native `file` resource](https://docs.puppetlabs.com/references/latest/type.html#file-attribute-backup) for execution. Valid options: 'true', 'false', or a string representing either a target filebucket or a filename extension beginning with ".". Default value: 'puppet'. #####`ensure` Specifies whether the destination file should exist. Setting to 'absent' tells Puppet to delete the destination file if it exists, and negates the effect of any other parameters. Valid options: 'present' and 'absent'. Default value: 'present'. #####`ensure_newline` Specifies whether to add a line break at the end of each fragment that doesn't already end in one. Valid options: 'true' and 'false'. Default value: 'false'. #####`group` Specifies a permissions group for the destination file. Valid options: a string containing a group name. Default value: undefined. #####`mode` Specifies the permissions mode of the destination file. Valid options: a string containing a permission mode value in octal notation. Default value: '0644'. #####`order` Specifies a method for sorting your fragments by name within the destination file. Valid options: 'alpha' (e.g., '1, 10, 2') or 'numeric' (e.g., '1, 2, 10'). Default value: 'numeric'. You can override this setting for individual fragments by adjusting the `order` parameter in their `concat::fragment` declarations. #####`owner` Specifies the owner of the destination file. Valid options: a string containing a username. Default value: undefined. #####`path` Specifies a destination file for the combined fragments. Valid options: a string containing an absolute path. Default value: the title of your declared resource. #####`replace` Specifies whether to overwrite the destination file if it already exists. Valid options: 'true' and 'false'. Default value: 'true'. ####`tag` *Required.* Specifies a unique tag reference to collect all concat_fragments with the same tag. #####`validate_cmd` Specifies a validation command to apply to the destination file. Requires Puppet version 3.5 or newer. Valid options: a string to be passed to a file resource. Default value: undefined. ####Type: `concat_fragment` #####`content` Supplies the content of the fragment. **Note**: You must supply either a `content` parameter or a `source` parameter. Valid options: a string. Default value: undef. #####`order` Reorders your fragments within the destination file. Fragments that share the same order number are ordered by name. Valid options: a string (recommended) or an integer. Default value: '10'. #####`source` Specifies a file to read into the content of the fragment. **Note**: You must supply either a `content` parameter or a `source` parameter. Valid options: a string or an array, containing one or more Puppet URLs. Default value: undefined. #####`tag` *Required.* Specifies a unique tag to be used by concat_file to reference and collect content. #####`target` -*Required.* Specifies the destination file of the fragment. Valid options: a string containing an absolute path. +*Required.* Specifies the destination file of the fragment. Valid options: a string containing the title of the parent `concat_file` resource. ###Removed functionality The following functionality existed in previous versions of the concat module, but was removed in version 2.0.0: Parameters removed from `concat::fragment`: * `gnu` * `backup` * `group` * `mode` * `owner` The `concat::setup` class has also been removed. Prior to concat version 2.0.0, if you set the `warn` parameter to a string value of 'true', 'false', 'yes', 'no', 'on', or 'off', the module translated the string to the corresponding boolean value. In concat version 2.0.0 and newer, the `warn_header` parameter treats those values the same as other strings and uses them as the content of your header message. To avoid that, pass the 'true' and 'false' values as booleans instead of strings. ##Limitations This module has been tested on [all PE-supported platforms](https://forge.puppetlabs.com/supported#compat-matrix), and no issues have been identified. ##Development Puppet Labs modules on the Puppet Forge are open projects, and community contributions are essential for keeping them great. We can't access the huge number of platforms and myriad of hardware, software, and deployment configurations that Puppet is intended to serve. We want to keep it as easy as possible to contribute changes so that our modules work in your environment. There are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things. For more information, see our [module contribution guide.](https://docs.puppetlabs.com/forge/contributing.html) ###Contributors Richard Pijnenburg ([@Richardp82](http://twitter.com/richardp82)) Joshua Hoblitt ([@jhoblitt](http://twitter.com/jhoblitt)) [More contributors.](https://github.com/puppetlabs/puppetlabs-concat/graphs/contributors) diff --git a/lib/puppet/type/concat_file.rb b/lib/puppet/type/concat_file.rb index 6a6225e..3d503fb 100644 --- a/lib/puppet/type/concat_file.rb +++ b/lib/puppet/type/concat_file.rb @@ -1,165 +1,189 @@ require 'puppet/type/file/owner' require 'puppet/type/file/group' require 'puppet/type/file/mode' require 'puppet/util/checksums' Puppet::Type.newtype(:concat_file) do @doc = "Gets all the file fragments and puts these into the target file. This will mostly be used with exported resources. example: Concat_fragment <<| tag == 'unique_tag' |>> concat_file { '/tmp/file': tag => 'unique_tag', # Mandatory path => '/tmp/file', # Optional. If given it overrides the resource name owner => 'root', # Optional. Default to undef group => 'root', # Optional. Default to undef mode => '0644' # Optional. Default to undef order => 'numeric' # Optional, Default to 'numeric' ensure_newline => false # Optional, Defaults to false } " ensurable do defaultvalues defaultto { :present } end def exists? self[:ensure] == :present end newparam(:name, :namevar => true) do desc "Resource name" end newparam(:tag) do desc "Tag reference to collect all concat_fragment's with the same tag" end newparam(:path) do desc "The output file" defaultto do resource.value(:name) end end newparam(:owner, :parent => Puppet::Type::File::Owner) do desc "Desired file owner." end newparam(:group, :parent => Puppet::Type::File::Group) do desc "Desired file group." end newparam(:mode, :parent => Puppet::Type::File::Mode) do desc "Desired file mode." end newparam(:order) do desc "Controls the ordering of fragments. Can be set to alphabetical or numeric." defaultto 'numeric' end newparam(:backup) do desc "Controls the filebucketing behavior of the final file and see File type reference for its use." defaultto 'puppet' end newparam(:replace) do desc "Whether to replace a file that already exists on the local system." defaultto true end newparam(:validate_cmd) do desc "Validates file." end newparam(:ensure_newline) do desc "Whether to ensure there is a newline after each fragment." defaultto false end autorequire(:concat_fragment) do catalog.resources.collect do |r| if r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag] r.name end end.compact end + # Copied from puppet's file type + # Autorequire the nearest ancestor directory found in the catalog. + autorequire(:file) do + req = [] + path = Pathname.new(self[:path]) + if !path.root? + # Start at our parent, to avoid autorequiring ourself + parents = path.parent.enum_for(:ascend) + if found = parents.find { |p| catalog.resource(:file, p.to_s) } + req << found.to_s + end + end + + req + end + def should_content return @generated_content if @generated_content @generated_content = "" content_fragments = [] resources = catalog.resources.select do |r| r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag] end resources.each do |r| content_fragments << ["#{r[:order]}___#{r[:name]}", fragment_content(r)] end if self[:order] == 'numeric' sorted = content_fragments.sort do |a, b| def decompound(d) d.split('___').map { |v| v =~ /^\d+$/ ? v.to_i : v } end decompound(a[0]) <=> decompound(b[0]) end else sorted = content_fragments.sort do |a, b| def decompound(d) d.split('___').first end decompound(a[0]) <=> decompound(b[0]) end end @generated_content = sorted.map { |cf| cf[1] }.join @generated_content end def fragment_content(r) if r[:content].nil? == false fragment_content = r[:content] elsif r[:source].nil? == false @source = nil Array(r[:source]).each do |source| if Puppet::FileServing::Metadata.indirection.find(source) @source = source break end end self.fail "Could not retrieve source(s) #{r[:source].join(", ")}" unless @source tmp = Puppet::FileServing::Content.indirection.find(@source, :environment => catalog.environment) fragment_content = tmp.content unless tmp.nil? end if self[:ensure_newline] fragment_content<<"\n" unless fragment_content =~ /\n$/ end fragment_content end - def eval_generate + def generate file_opts = { :ensure => self[:ensure] == :absent ? :absent : :file, - :content => self.should_content, } [:path, :owner, :group, :mode, :replace, :backup].each do |param| unless self[param].nil? file_opts[param] = self[param] end end [Puppet::Type.type(:file).new(file_opts)] end + + def eval_generate + content = should_content + + if !content.nil? and !content.empty? + catalog.resource("File[#{self[:path]}]")[:content] = content + end + [] + end end diff --git a/lib/puppet/type/concat_fragment.rb b/lib/puppet/type/concat_fragment.rb index 3a1e722..4567f2f 100644 --- a/lib/puppet/type/concat_fragment.rb +++ b/lib/puppet/type/concat_fragment.rb @@ -1,48 +1,64 @@ Puppet::Type.newtype(:concat_fragment) do @doc = "Create a concat fragment to be used by concat. the `concat_fragment` type creates a file fragment to be collected by concat based on the tag. The example is based on exported resources. Example: @@concat_fragment { \"uniqe_name_${::fqdn}\": tag => 'unique_name', order => 10, # Optional. Default to 10 content => 'some content' # OR content => template('template.erb') # OR source => 'puppet:///path/to/file' } " newparam(:name, :namevar => true) do desc "Unique name" end + newparam(:target) do + desc "Target" + end + newparam(:content) do desc "Content" end newparam(:source) do desc "Source" end newparam(:order) do desc "Order" defaultto '10' validate do |val| fail Puppet::ParseError, '$order is not a string or integer.' if !(val.is_a? String or val.is_a? Integer) fail Puppet::ParseError, "Order cannot contain '/', ':', or '\n'." if val.to_s =~ /[:\n\/]/ end end newparam(:tag) do desc "Tag name to be used by concat to collect all concat_fragments by tag name" end + autorequire(:file) do + unless catalog.resource("Concat_file[#{self[:target]}]") + warning "Target Concat_file[#{self[:target]}] not found in the catalog" + end + end + validate do + # Check if target is set + fail Puppet::ParseError, "Target not set" if self[:target].nil? + + # Check if tag is set + fail Puppet::ParseError, "Tag not set" if self[:tag].nil? + # Check if either source or content is set. raise error if none is set fail Puppet::ParseError, "Set either 'source' or 'content'" if self[:source].nil? && self[:content].nil? # Check if both are set, if so rais error fail Puppet::ParseError, "Can't use 'source' and 'content' at the same time" if !self[:source].nil? && !self[:content].nil? end end diff --git a/manifests/fragment.pp b/manifests/fragment.pp index 9cfae66..cc33643 100644 --- a/manifests/fragment.pp +++ b/manifests/fragment.pp @@ -1,55 +1,56 @@ # == Define: concat::fragment # # Creates a concat_fragment in the catalogue # # === Options: # # [*target*] # The file that these fragments belong to # [*content*] # If present puts the content into the file # [*source*] # If content was not specified, use the source # [*order*] # By default all files gets a 10_ prefix in the directory you can set it to # anything else using this to influence the order of the content in the file # define concat::fragment( $target, $ensure = undef, $content = undef, $source = undef, $order = '10', ) { validate_string($target) if $ensure != undef { warning('The $ensure parameter to concat::fragment is deprecated and has no effect.') } validate_string($content) if !(is_string($source) or is_array($source)) { fail('$source is not a string or an Array.') } if !(is_string($order) or is_integer($order)) { fail('$order is not a string or integer.') } elsif (is_string($order) and $order =~ /[:\n\/]/) { fail("Order cannot contain '/', ':', or '\n'.") } if ! ($content or $source) { crit('No content, source or symlink specified') } elsif ($content and $source) { fail("Can't use 'source' and 'content' at the same time") } $safe_target_name = regsubst($target, '[/:\n\s]', '_', 'GM') concat_fragment { $name: + target => $target, tag => $safe_target_name, order => $order, content => $content, source => $source, } } diff --git a/manifests/init.pp b/manifests/init.pp index 46d4896..03a4df2 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,127 +1,128 @@ # == Define: concat # # Sets up so that you can use fragments to build a final config file, # # === Options: # # [*ensure*] # Present/Absent # [*path*] # The path to the final file. Use this in case you want to differentiate # between the name of a resource and the file path. Note: Use the name you # provided in the target of your fragments. # [*owner*] # Who will own the file # [*group*] # Who will own the file # [*mode*] # The mode of the final file # [*warn*] # Adds a normal shell style comment top of the file indicating that it is # built by puppet. # Before 2.0.0, this parameter would add a newline at the end of the warn # message. To improve flexibilty, this was removed. Please add it explicitely # if you need it. # [*backup*] # Controls the filebucketing behavior of the final file and see File type # reference for its use. Defaults to 'puppet' # [*replace*] # Whether to replace a file that already exists on the local system # [*order*] # Select whether to order associated fragments by 'alpha' or 'numeric'. # Defaults to 'alpha'. # [*ensure_newline*] # Specifies whether to ensure there's a new line at the end of each fragment. # Valid options: 'true' and 'false'. Default value: 'false'. # [*validate_cmd*] # Specifies a validation command to apply to the destination file. # Requires Puppet version 3.5 or newer. Valid options: a string to be passed # to a file resource. Default value: undefined. # define concat( $ensure = 'present', $path = $name, $owner = undef, $group = undef, $mode = '0644', $warn = false, $force = undef, $backup = 'puppet', $replace = true, $order = 'alpha', $ensure_newline = false, $validate_cmd = undef, ) { validate_re($ensure, '^present$|^absent$') validate_absolute_path($path) validate_string($owner) validate_string($group) validate_string($mode) if ! (is_string($warn) or $warn == true or $warn == false) { fail('$warn is not a string or boolean') } if ! is_bool($backup) and ! is_string($backup) { fail('$backup must be string or bool!') } validate_bool($replace) validate_re($order, '^alpha$|^numeric$') validate_bool($ensure_newline) if $validate_cmd and ! is_string($validate_cmd) { fail('$validate_cmd must be a string') } if $force != undef { warning('The $force parameter to concat is deprecated and has no effect.') } $safe_name = regsubst($name, '[/:\n\s]', '_', 'G') $default_warn_message = "# This file is managed by Puppet. DO NOT EDIT.\n" case $warn { true: { $warn_message = $default_warn_message $_append_header = true } false: { $warn_message = '' $_append_header = false } default: { $warn_message = $warn $_append_header = true } } if $ensure == 'present' { concat_file { $name: tag => $safe_name, path => $path, owner => $owner, group => $group, mode => $mode, replace => $replace, backup => $backup, order => $order, ensure_newline => $ensure_newline, validate_cmd => $validate_cmd, } if $_append_header { concat_fragment { "${name}_header": + target => $name, tag => $safe_name, content => $warn_message, order => '0', } } } else { concat_file { $name: ensure => $ensure, tag => $safe_name, path => $path, backup => $backup, } } } diff --git a/metadata.json b/metadata.json index 92fb8a1..31d9144 100644 --- a/metadata.json +++ b/metadata.json @@ -1,110 +1,110 @@ { "name": "puppetlabs-concat", - "version": "2.0.0", + "version": "2.0.1", "author": "Puppet Labs", "summary": "Construct files from multiple fragments.", "license": "Apache-2.0", "source": "https://github.com/puppetlabs/puppetlabs-concat", "project_page": "https://github.com/puppetlabs/puppetlabs-concat", "issues_url": "https://tickets.puppetlabs.com/browse/MODULES", - "dependencies": [ - {"name":"puppetlabs/stdlib","version_requirement":">= 4.5.0 < 5.0.0"} - ], "operatingsystem_support": [ { "operatingsystem": "RedHat", "operatingsystemrelease": [ "5", "6", "7" ] }, { "operatingsystem": "CentOS", "operatingsystemrelease": [ "5", "6", "7" ] }, { "operatingsystem": "OracleLinux", "operatingsystemrelease": [ "5", "6", "7" ] }, { "operatingsystem": "Scientific", "operatingsystemrelease": [ "5", "6", "7" ] }, { "operatingsystem": "SLES", "operatingsystemrelease": [ "10 SP4", "11 SP1", "12" ] }, { "operatingsystem": "Debian", "operatingsystemrelease": [ "6", "7" ] }, { "operatingsystem": "Ubuntu", "operatingsystemrelease": [ "10.04", "12.04", "14.04" ] }, { "operatingsystem": "Solaris", "operatingsystemrelease": [ "10", "11" ] }, { "operatingsystem": "Windows", "operatingsystemrelease": [ "Server 2003 R2", "Server 2008 R2", "Server 2012", "Server 2012 R2" ] }, { "operatingsystem": "AIX", "operatingsystemrelease": [ "5.3", "6.1", "7.1" ] }, { "operatingsystem": "OSX", "operatingsystemrelease": [ "10.9" ] } ], "requirements": [ { "name": "pe", "version_requirement": ">= 3.7.0 < 4.0.0" }, { "name": "puppet", "version_requirement": "3.x" } + ], + "dependencies": [ + {"name":"puppetlabs/stdlib","version_requirement":">= 3.2.0 < 5.0.0"} ] } diff --git a/spec/acceptance/concurrency_spec.rb b/spec/acceptance/concurrency_spec.rb new file mode 100644 index 0000000..fcffdbd --- /dev/null +++ b/spec/acceptance/concurrency_spec.rb @@ -0,0 +1,37 @@ +require 'spec_helper_acceptance' + +describe 'with file recursive purge' do + basedir = default.tmpdir('concat') + context 'should still create concat file' do + pp = <<-EOS + file { '#{basedir}/bar': + ensure => directory, + purge => true, + recurse => true, + } + + concat { "foobar": + ensure => 'present', + path => '#{basedir}/bar/foobar', + } + + concat::fragment { 'foo': + target => 'foobar', + content => 'foo', + } + EOS + + it 'applies the manifest twice with no stderr' do + apply_manifest(pp, :catch_failures => true) + apply_manifest(pp, :catch_changes => true) + end + + describe file("#{basedir}/bar/foobar") do + it { should be_file } + its(:content) { + should match 'foo' + } + end + end +end + diff --git a/spec/acceptance/fragment_source_spec.rb b/spec/acceptance/fragment_source_spec.rb index 5d66e4d..b208979 100644 --- a/spec/acceptance/fragment_source_spec.rb +++ b/spec/acceptance/fragment_source_spec.rb @@ -1,157 +1,156 @@ require 'spec_helper_acceptance' case fact('osfamily') when 'AIX' username = 'root' groupname = 'system' when 'Darwin' username = 'root' groupname = 'wheel' when 'windows' username = 'Administrator' groupname = 'Administrators' else username = 'root' groupname = 'root' end describe 'concat::fragment source' do basedir = default.tmpdir('concat') context 'should read file fragments from local system' do pp = <<-EOS file { '#{basedir}/file1': content => "file1 contents\n" } file { '#{basedir}/file2': content => "file2 contents\n" } concat { '#{basedir}/foo': } concat::fragment { '1': target => '#{basedir}/foo', source => '#{basedir}/file1', require => File['#{basedir}/file1'], } concat::fragment { '2': target => '#{basedir}/foo', content => 'string1 contents', } concat::fragment { '3': target => '#{basedir}/foo', source => '#{basedir}/file2', require => File['#{basedir}/file2'], } EOS it 'applies the manifest twice with no stderr' do apply_manifest(pp, :catch_failures => true) apply_manifest(pp, :catch_changes => true) end describe file("#{basedir}/foo") do it { should be_file } its(:content) { should match 'file1 contents' should match 'string1 contents' should match 'file2 contents' } end end # should read file fragments from local system context 'should create files containing first match only.' do pp = <<-EOS file { '#{basedir}/file1': content => "file1 contents\n" } file { '#{basedir}/file2': content => "file2 contents\n" } concat { '#{basedir}/result_file1': owner => '#{username}', group => '#{groupname}', mode => '0644', } concat { '#{basedir}/result_file2': owner => '#{username}', group => '#{groupname}', mode => '0644', } concat { '#{basedir}/result_file3': owner => '#{username}', group => '#{groupname}', mode => '0644', } concat::fragment { '1': target => '#{basedir}/result_file1', source => [ '#{basedir}/file1', '#{basedir}/file2' ], require => [ File['#{basedir}/file1'], File['#{basedir}/file2'] ], order => '01', } concat::fragment { '2': target => '#{basedir}/result_file2', source => [ '#{basedir}/file2', '#{basedir}/file1' ], require => [ File['#{basedir}/file1'], File['#{basedir}/file2'] ], order => '01', } concat::fragment { '3': target => '#{basedir}/result_file3', source => [ '#{basedir}/file1', '#{basedir}/file2' ], require => [ File['#{basedir}/file1'], File['#{basedir}/file2'] ], order => '01', } EOS it 'applies the manifest twice with no stderr' do apply_manifest(pp, :catch_failures => true) apply_manifest(pp, :catch_changes => true) end describe file("#{basedir}/result_file1") do it { should be_file } its(:content) { should match 'file1 contents' should_not match 'file2 contents' } end describe file("#{basedir}/result_file2") do it { should be_file } its(:content) { should match 'file2 contents' should_not match 'file1 contents' } end describe file("#{basedir}/result_file3") do it { should be_file } its(:content) { should match 'file1 contents' should_not match 'file2 contents' } end end context 'should fail if no match on source.' do pp = <<-EOS concat { '#{basedir}/fail_no_source': owner => '#{username}', group => '#{groupname}', mode => '0644', } concat::fragment { '1': target => '#{basedir}/fail_no_source', source => [ '#{basedir}/nofilehere', '#{basedir}/nothereeither' ], order => '01', } EOS it 'applies the manifest with resource failures' do expect(apply_manifest(pp, :catch_failures => true).stderr).to match(/Failed to generate additional resources using 'eval_generate'/) end describe file("#{basedir}/fail_no_source") do #FIXME: Serverspec::Type::File doesn't support exists? for some reason. so... hack. - it { should_not be_file } it { should_not be_directory } end end end diff --git a/spec/acceptance/deprecation_warnings_spec.rb b/spec/acceptance/warnings_spec.rb similarity index 68% rename from spec/acceptance/deprecation_warnings_spec.rb rename to spec/acceptance/warnings_spec.rb index 8a689a7..89b3c3c 100644 --- a/spec/acceptance/deprecation_warnings_spec.rb +++ b/spec/acceptance/warnings_spec.rb @@ -1,44 +1,61 @@ require 'spec_helper_acceptance' -describe 'deprecation warnings' do +describe 'warnings' do basedir = default.tmpdir('concat') shared_examples 'has_warning' do |pp, w| it 'applies the manifest twice with a stderr regex' do expect(apply_manifest(pp, :catch_failures => true).stderr).to match(/#{Regexp.escape(w)}/m) expect(apply_manifest(pp, :catch_changes => true).stderr).to match(/#{Regexp.escape(w)}/m) end end - context 'concat force parameter' do + context 'concat force parameter deprecation' do pp = <<-EOS concat { '#{basedir}/file': force => false, } concat::fragment { 'foo': target => '#{basedir}/file', content => 'bar', } EOS w = 'The $force parameter to concat is deprecated and has no effect.' it_behaves_like 'has_warning', pp, w end - context 'concat::fragment ensure parameter' do + context 'concat::fragment ensure parameter deprecation' do context 'target file exists' do pp = <<-EOS concat { '#{basedir}/file': } concat::fragment { 'foo': target => '#{basedir}/file', ensure => false, content => 'bar', } EOS w = 'The $ensure parameter to concat::fragment is deprecated and has no effect.' it_behaves_like 'has_warning', pp, w end end + + context 'concat::fragment target not found' do + context 'target not found' do + pp = <<-EOS + concat { 'file': + path => '#{basedir}/file', + } + concat::fragment { 'foo': + target => '#{basedir}/file', + content => 'bar', + } + EOS + w = 'not found in the catalog' + + it_behaves_like 'has_warning', pp, w + end + end end