]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/concat/lib/puppet/type/concat_file.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / concat / lib / puppet / type / concat_file.rb
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         ensure_newline => false         # Optional, Defaults to false
21       }
22   "
23   ensurable do
24     defaultvalues
25
26     defaultto { :present }
27   end
28
29   def exists?
30     self[:ensure] == :present
31   end
32
33   newparam(:name, :namevar => true) do
34     desc "Resource name"
35   end
36
37   newparam(:tag) do
38     desc "Tag reference to collect all concat_fragment's with the same tag"
39   end
40
41   newparam(:path) do
42     desc "The output file"
43     defaultto do
44       resource.value(:name)
45     end
46   end
47
48   newparam(:owner, :parent => Puppet::Type::File::Owner) do
49     desc "Desired file owner."
50   end
51
52   newparam(:group, :parent => Puppet::Type::File::Group) do
53     desc "Desired file group."
54   end
55
56   newparam(:mode, :parent => Puppet::Type::File::Mode) do
57     desc "Desired file mode."
58   end
59
60   newparam(:order) do
61     desc "Controls the ordering of fragments. Can be set to alphabetical or numeric."
62     defaultto 'numeric'
63   end
64
65   newparam(:backup) do
66     desc "Controls the filebucketing behavior of the final file and see File type reference for its use."
67     defaultto 'puppet'
68   end
69
70   newparam(:replace) do
71     desc "Whether to replace a file that already exists on the local system."
72     defaultto true
73   end
74
75   newparam(:validate_cmd) do
76     desc "Validates file."
77   end
78
79   newparam(:ensure_newline) do
80     desc "Whether to ensure there is a newline after each fragment."
81     defaultto false
82   end
83
84   autorequire(:concat_fragment) do
85     catalog.resources.collect do |r|
86       if r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag]
87         r.name
88       end
89     end.compact
90   end
91
92   def should_content
93     return @generated_content if @generated_content
94     @generated_content = ""
95     content_fragments = []
96
97     resources = catalog.resources.select do |r|
98       r.is_a?(Puppet::Type.type(:concat_fragment)) && r[:tag] == self[:tag]
99     end
100
101     resources.each do |r|
102       content_fragments << ["#{r[:order]}___#{r[:name]}", fragment_content(r)]
103     end
104
105     if self[:order] == 'numeric'
106       sorted = content_fragments.sort do |a, b|
107         def decompound(d)
108           d.split('___').map { |v| v =~ /^\d+$/ ? v.to_i : v }
109         end
110
111         decompound(a[0]) <=> decompound(b[0])
112       end
113     else
114       sorted = content_fragments.sort do |a, b|
115         def decompound(d)
116           d.split('___').first
117         end
118
119         decompound(a[0]) <=> decompound(b[0])
120       end
121     end
122
123     @generated_content = sorted.map { |cf| cf[1] }.join
124
125     @generated_content
126   end
127
128   def fragment_content(r)
129     if r[:content].nil? == false
130       fragment_content = r[:content]
131     elsif r[:source].nil? == false
132       @source = nil
133       Array(r[:source]).each do |source|
134         if Puppet::FileServing::Metadata.indirection.find(source)
135           @source = source 
136           break
137         end
138       end
139       self.fail "Could not retrieve source(s) #{r[:source].join(", ")}" unless @source
140       tmp = Puppet::FileServing::Content.indirection.find(@source, :environment => catalog.environment)
141       fragment_content = tmp.content unless tmp.nil?
142     end
143
144     if self[:ensure_newline]
145       fragment_content<<"\n" unless fragment_content =~ /\n$/
146     end
147
148     fragment_content
149   end
150
151   def eval_generate
152     file_opts = {
153       :ensure => self[:ensure] == :absent ? :absent : :file,
154       :content => self.should_content,
155     }
156
157     [:path, :owner, :group, :mode, :replace, :backup].each do |param|
158       unless self[param].nil?
159         file_opts[param] = self[param]
160       end
161     end
162
163     [Puppet::Type.type(:file).new(file_opts)]
164   end
165 end