diff --git a/lib/puppet/type/concat_file.rb b/lib/puppet/type/concat_file.rb index 6a6225e..9b3fde4 100644 --- a/lib/puppet/type/concat_file.rb +++ b/lib/puppet/type/concat_file.rb @@ -1,165 +1,169 @@ 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 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 + catalog.resource("File[#{self[:path]}]")[:content] = should_content + [] + end end 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