Skip to content

Commit

Permalink
re-refactor of concat to not depend on file_concat
Browse files Browse the repository at this point in the history
  • Loading branch information
bmjen committed May 1, 2015
1 parent bcf3af5 commit 6876399
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 37 deletions.
3 changes: 0 additions & 3 deletions .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,5 @@ fixtures:
'stdlib':
repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
ref: '4.5.1'
'file_concat':
repo: 'git://github.com/electrical/puppet-lib-file_concat.git'
ref: '1.0.1'
symlinks:
'concat': '#{source_dir}'
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ The concat module lets you construct files from multiple ordered fragments of te

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.

##Setup

###What concat affects

The concat module requires the [file_concat module](https://forge.puppetlabs.com/electrical/file_concat). If you don't have file_concat installed, concat installs it for you.

###Beginning with concat

To start using concat you need to create:
Expand Down Expand Up @@ -240,4 +234,8 @@ For more information, see our [module contribution guide.](https://docs.puppetla

###Contributors

To see who's already involved, see the [list of contributors.](https://github.com/puppetlabs/puppetlabs-concat/graphs/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)
154 changes: 154 additions & 0 deletions lib/puppet/type/concat_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
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'
}
"
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

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
fragment_content
end

def eval_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
end
48 changes: 48 additions & 0 deletions lib/puppet/type/concat_fragment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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(: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

validate do
# 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
4 changes: 2 additions & 2 deletions manifests/fragment.pp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# == Define: concat::fragment
#
# Creates a file_fragment in the catalogue
# Creates a concat_fragment in the catalogue
#
# === Options:
#
Expand Down Expand Up @@ -46,7 +46,7 @@

$safe_target_name = regsubst($target, '[/:\n\s]', '_', 'GM')

file_fragment { $name:
concat_fragment { $name:
tag => $safe_target_name,
order => $order,
content => $content,
Expand Down
17 changes: 3 additions & 14 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@
# Select whether to order associated fragments by 'alpha' or 'numeric'.
# Defaults to 'alpha'.
#
# === Actions:
# * Creates a file_concat resource from the electrical/puppet-lib-file_concat library.
# * Creates file_fragment resources from electrical/puppet-lib-file_concat
#
# === Aliases:
#
# * The exec can notified using Exec["concat_/path/to/file"] or
# Exec["concat_/path/to/directory"]
# * The final file can be referenced as File["/path/to/file"] or
# File["concat_/path/to/file"]
#

define concat(
$ensure = 'present',
Expand Down Expand Up @@ -93,7 +82,7 @@
}

if $ensure == 'present' {
file_concat { $name:
concat_file { $name:
tag => $safe_name,
path => $path,
owner => $owner,
Expand All @@ -106,14 +95,14 @@
}

if $_append_header {
file_fragment { "#{$name}_header":
concat_fragment { "#{$name}_header":
tag => $safe_name,
content => $warn_message,
order => '0',
}
}
} else {
file_concat { $name:
concat_file { $name:
ensure => $ensure,
tag => $safe_name,
path => $path,
Expand Down
1 change: 0 additions & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,5 @@
],
"dependencies": [
{"name":"puppetlabs/stdlib","version_requirement":">= 4.5.0 < 5.0.0"},
{"name":"electrical/file_concat","version_requirement":"1.0.1"}
]
}
8 changes: 0 additions & 8 deletions spec/spec_helper_acceptance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@
command => "wget -P /root/ https://forgeapi.puppetlabs.com/v3/files/puppetlabs-stdlib-4.5.1.tar.gz --no-check-certificate",
path => ['/opt/csw/bin/','/usr/bin/']
}
exec{'download-file_concat':
command => "wget -P /root/ https://forgeapi.puppetlabs.com/v3/files/electrical-file_concat-1.0.1.tar.gz --no-check-certificate",
path => ['/opt/csw/bin/','/usr/bin/']
}
EOS
apply_manifest_on(host, get_deps)
# have to use force otherwise it checks ssl cert even though it is a local file
on host, puppet('module install /root/puppetlabs-stdlib-4.5.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
on host, puppet('module install /root/electrical-file_concat-1.0.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
elsif host['platform'] =~ /windows/i
on host, shell('curl -k -o c:/puppetlabs-stdlib-4.5.1.tar.gz https://forgeapi.puppetlabs.com/v3/files/puppetlabs-stdlib-4.5.1.tar.gz')
on host, puppet('module install c:/puppetlabs-stdlib-4.5.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
on host, shell('curl -k -o c:/electrical-file_concat-1.0.1.tar.gz https://forgeapi.puppetlabs.com/v3/files/electrical-file_concat-1.0.1.tar.gz')
on host, puppet('module install c:/electrical-file_concat-1.0.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
else
on host, puppet('module install puppetlabs-stdlib'), {:acceptable_exit_codes => [0, 1]}
on host, puppet('module install electrical-file_concat'), {:acceptable_exit_codes => [0, 1]}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/defines/concat_fragment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

it do
should contain_concat(p[:target])
should contain_file_concat(p[:target])
should contain_file_fragment(title)
should contain_concat_file(p[:target])
should contain_concat_fragment(title)
end
end

Expand Down

0 comments on commit 6876399

Please sign in to comment.