Skip to content

Commit b880a99

Browse files
author
Morgan Haskel
committed
Merge pull request #306 from bmjen/re-refactor-2.0
re-refactor of concat to not depend on file_concat
2 parents b2c4145 + 6876399 commit b880a99

File tree

9 files changed

+214
-37
lines changed

9 files changed

+214
-37
lines changed

.fixtures.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@ fixtures:
33
'stdlib':
44
repo: 'git://github.com/puppetlabs/puppetlabs-stdlib.git'
55
ref: '4.5.1'
6-
'file_concat':
7-
repo: 'git://github.com/electrical/puppet-lib-file_concat.git'
8-
ref: '1.0.1'
96
symlinks:
107
'concat': '#{source_dir}'

README.md

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ The concat module lets you construct files from multiple ordered fragments of te
2323

2424
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.
2525

26-
##Setup
27-
28-
###What concat affects
29-
30-
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.
31-
3226
###Beginning with concat
3327

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

241235
###Contributors
242236

243-
To see who's already involved, see the [list of contributors.](https://github.com/puppetlabs/puppetlabs-concat/graphs/contributors)
237+
Richard Pijnenburg ([@Richardp82](http://twitter.com/richardp82))
238+
239+
Joshua Hoblitt ([@jhoblitt](http://twitter.com/jhoblitt))
240+
241+
[More contributors.](https://github.com/puppetlabs/puppetlabs-concat/graphs/contributors)

lib/puppet/type/concat_file.rb

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
require 'puppet/type/file/owner'
2+
require 'puppet/type/file/group'
3+
require 'puppet/type/file/mode'
4+
require 'puppet/util/checksums'
5+
6+
Puppet::Type.newtype(:concat_file) do
7+
@doc = "Gets all the file fragments and puts these into the target file.
8+
This will mostly be used with exported resources.
9+
10+
example:
11+
Concat_fragment <<| tag == 'unique_tag' |>>
12+
13+
concat_file { '/tmp/file:
14+
tag => 'unique_tag', # Mandatory
15+
path => '/tmp/file', # Optional. If given it overrides the resource name
16+
owner => 'root', # Optional. Default to undef
17+
group => 'root', # Optional. Default to undef
18+
mode => '0644' # Optional. Default to undef
19+
order => 'numeric' # Optional, Default to 'numeric'
20+
}
21+
"
22+
ensurable do
23+
defaultvalues
24+
25+
defaultto { :present }
26+
end
27+
28+
def exists?
29+
self[:ensure] == :present
30+
end
31+
32+
newparam(:name, :namevar => true) do
33+
desc "Resource name"
34+
end
35+
36+
newparam(:tag) do
37+
desc "Tag reference to collect all concat_fragment's with the same tag"
38+
end
39+
40+
newparam(:path) do
41+
desc "The output file"
42+
defaultto do
43+
resource.value(:name)
44+
end
45+
end
46+
47+
newparam(:owner, :parent => Puppet::Type::File::Owner) do
48+
desc "Desired file owner."
49+
end
50+
51+
newparam(:group, :parent => Puppet::Type::File::Group) do
52+
desc "Desired file group."
53+
end
54+
55+
newparam(:mode, :parent => Puppet::Type::File::Mode) do
56+
desc "Desired file mode."
57+
end
58+
59+
newparam(:order) do
60+
desc "Controls the ordering of fragments. Can be set to alphabetical or numeric."
61+
defaultto 'numeric'
62+
end
63+
64+
newparam(:backup) do
65+
desc "Controls the filebucketing behavior of the final file and see File type reference for its use."
66+
defaultto 'puppet'
67+
end
68+
69+
newparam(:replace) do
70+
desc "Whether to replace a file that already exists on the local system."
71+
defaultto true
72+
end
73+
74+
newparam(:validate_cmd) do
75+
desc "Validates file."
76+
end
77+
78+
autorequire(:concat_fragment) do
79+
catalog.resources.collect do |r|
80+
if r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag]
81+
r.name
82+
end
83+
end.compact
84+
end
85+
86+
def should_content
87+
return @generated_content if @generated_content
88+
@generated_content = ""
89+
content_fragments = []
90+
91+
resources = catalog.resources.select do |r|
92+
r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag]
93+
end
94+
95+
resources.each do |r|
96+
content_fragments << ["#{r[:order]}___#{r[:name]}", fragment_content(r)]
97+
end
98+
99+
if self[:order] == 'numeric'
100+
sorted = content_fragments.sort do |a, b|
101+
def decompound(d)
102+
d.split('___').map { |v| v =~ /^\d+$/ ? v.to_i : v }
103+
end
104+
105+
decompound(a[0]) <=> decompound(b[0])
106+
end
107+
else
108+
sorted = content_fragments.sort do |a, b|
109+
def decompound(d)
110+
d.split('___').first
111+
end
112+
113+
decompound(a[0]) <=> decompound(b[0])
114+
end
115+
end
116+
117+
@generated_content = sorted.map { |cf| cf[1] }.join
118+
119+
@generated_content
120+
end
121+
122+
def fragment_content(r)
123+
if r[:content].nil? == false
124+
fragment_content = r[:content]
125+
elsif r[:source].nil? == false
126+
@source = nil
127+
Array(r[:source]).each do |source|
128+
if Puppet::FileServing::Metadata.indirection.find(source)
129+
@source = source
130+
break
131+
end
132+
end
133+
self.fail "Could not retrieve source(s) #{r[:source].join(", ")}" unless @source
134+
tmp = Puppet::FileServing::Content.indirection.find(@source, :environment => catalog.environment)
135+
fragment_content = tmp.content unless tmp.nil?
136+
end
137+
fragment_content
138+
end
139+
140+
def eval_generate
141+
file_opts = {
142+
:ensure => self[:ensure] == :absent ? :absent : :file,
143+
:content => self.should_content,
144+
}
145+
146+
[:path, :owner, :group, :mode, :replace, :backup].each do |param|
147+
unless self[param].nil?
148+
file_opts[param] = self[param]
149+
end
150+
end
151+
152+
[Puppet::Type.type(:file).new(file_opts)]
153+
end
154+
end

lib/puppet/type/concat_fragment.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Puppet::Type.newtype(:concat_fragment) do
2+
@doc = "Create a concat fragment to be used by concat.
3+
the `concat_fragment` type creates a file fragment to be collected by concat based on the tag.
4+
The example is based on exported resources.
5+
6+
Example:
7+
@@concat_fragment { \"uniqe_name_${::fqdn}\":
8+
tag => 'unique_name',
9+
order => 10, # Optional. Default to 10
10+
content => 'some content' # OR
11+
content => template('template.erb') # OR
12+
source => 'puppet:///path/to/file'
13+
}
14+
"
15+
16+
newparam(:name, :namevar => true) do
17+
desc "Unique name"
18+
end
19+
20+
newparam(:content) do
21+
desc "Content"
22+
end
23+
24+
newparam(:source) do
25+
desc "Source"
26+
end
27+
28+
newparam(:order) do
29+
desc "Order"
30+
defaultto '10'
31+
validate do |val|
32+
fail Puppet::ParseError, '$order is not a string or integer.' if !(val.is_a? String or val.is_a? Integer)
33+
fail Puppet::ParseError, "Order cannot contain '/', ':', or '\n'." if val.to_s =~ /[:\n\/]/
34+
end
35+
end
36+
37+
newparam(:tag) do
38+
desc "Tag name to be used by concat to collect all concat_fragments by tag name"
39+
end
40+
41+
validate do
42+
# Check if either source or content is set. raise error if none is set
43+
fail Puppet::ParseError, "Set either 'source' or 'content'" if self[:source].nil? && self[:content].nil?
44+
45+
# Check if both are set, if so rais error
46+
fail Puppet::ParseError, "Can't use 'source' and 'content' at the same time" if !self[:source].nil? && !self[:content].nil?
47+
end
48+
end

manifests/fragment.pp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# == Define: concat::fragment
22
#
3-
# Creates a file_fragment in the catalogue
3+
# Creates a concat_fragment in the catalogue
44
#
55
# === Options:
66
#
@@ -46,7 +46,7 @@
4646

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

49-
file_fragment { $name:
49+
concat_fragment { $name:
5050
tag => $safe_target_name,
5151
order => $order,
5252
content => $content,

manifests/init.pp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@
2828
# Select whether to order associated fragments by 'alpha' or 'numeric'.
2929
# Defaults to 'alpha'.
3030
#
31-
# === Actions:
32-
# * Creates a file_concat resource from the electrical/puppet-lib-file_concat library.
33-
# * Creates file_fragment resources from electrical/puppet-lib-file_concat
34-
#
35-
# === Aliases:
36-
#
37-
# * The exec can notified using Exec["concat_/path/to/file"] or
38-
# Exec["concat_/path/to/directory"]
39-
# * The final file can be referenced as File["/path/to/file"] or
40-
# File["concat_/path/to/file"]
41-
#
4231

4332
define concat(
4433
$ensure = 'present',
@@ -93,7 +82,7 @@
9382
}
9483

9584
if $ensure == 'present' {
96-
file_concat { $name:
85+
concat_file { $name:
9786
tag => $safe_name,
9887
path => $path,
9988
owner => $owner,
@@ -106,14 +95,14 @@
10695
}
10796

10897
if $_append_header {
109-
file_fragment { "#{$name}_header":
98+
concat_fragment { "#{$name}_header":
11099
tag => $safe_name,
111100
content => $warn_message,
112101
order => '0',
113102
}
114103
}
115104
} else {
116-
file_concat { $name:
105+
concat_file { $name:
117106
ensure => $ensure,
118107
tag => $safe_name,
119108
path => $path,

metadata.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,5 @@
106106
],
107107
"dependencies": [
108108
{"name":"puppetlabs/stdlib","version_requirement":">= 4.5.0 < 5.0.0"},
109-
{"name":"electrical/file_concat","version_requirement":"1.0.1"}
110109
]
111110
}

spec/spec_helper_acceptance.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,15 @@
2222
command => "wget -P /root/ https://forgeapi.puppetlabs.com/v3/files/puppetlabs-stdlib-4.5.1.tar.gz --no-check-certificate",
2323
path => ['/opt/csw/bin/','/usr/bin/']
2424
}
25-
exec{'download-file_concat':
26-
command => "wget -P /root/ https://forgeapi.puppetlabs.com/v3/files/electrical-file_concat-1.0.1.tar.gz --no-check-certificate",
27-
path => ['/opt/csw/bin/','/usr/bin/']
28-
}
2925
EOS
3026
apply_manifest_on(host, get_deps)
3127
# have to use force otherwise it checks ssl cert even though it is a local file
3228
on host, puppet('module install /root/puppetlabs-stdlib-4.5.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
33-
on host, puppet('module install /root/electrical-file_concat-1.0.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
3429
elsif host['platform'] =~ /windows/i
3530
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')
3631
on host, puppet('module install c:/puppetlabs-stdlib-4.5.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
37-
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')
38-
on host, puppet('module install c:/electrical-file_concat-1.0.1.tar.gz --force --ignore-dependencies'), {:acceptable_exit_codes => [0, 1]}
3932
else
4033
on host, puppet('module install puppetlabs-stdlib'), {:acceptable_exit_codes => [0, 1]}
41-
on host, puppet('module install electrical-file_concat'), {:acceptable_exit_codes => [0, 1]}
4234
end
4335
end
4436
end

spec/unit/defines/concat_fragment_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
it do
2424
should contain_concat(p[:target])
25-
should contain_file_concat(p[:target])
26-
should contain_file_fragment(title)
25+
should contain_concat_file(p[:target])
26+
should contain_concat_fragment(title)
2727
end
2828
end
2929

0 commit comments

Comments
 (0)