Add puppet/archive module, required for newer puppet/rabbitmq
[mirror/dsa-puppet.git] / 3rdparty / modules / archive / lib / puppet / type / archive.rb
1 require 'pathname'
2 require 'uri'
3 require 'puppet/util'
4 require 'puppet/parameter/boolean'
5
6 Puppet::Type.newtype(:archive) do
7   @doc = 'Manage archive file download, extraction, and cleanup.'
8
9   ensurable do
10     desc 'whether archive file should be present/absent (default: present)'
11
12     newvalue(:present) do
13       provider.create
14     end
15
16     newvalue(:absent) do
17       provider.destroy
18     end
19
20     defaultto(:present)
21
22     # The following changes allows us to notify if the resource is being replaced
23     def is_to_s(value) # rubocop:disable Style/PredicateName
24       return "(#{resource[:checksum_type]})#{provider.archive_checksum}" if provider.archive_checksum
25       super
26     end
27
28     def should_to_s(value)
29       return "(#{resource[:checksum_type]})#{resource[:checksum]}" if provider.archive_checksum
30       super
31     end
32
33     def change_to_s(currentvalue, newvalue)
34       if currentvalue == :absent || currentvalue.nil?
35         extract = resource[:extract] == :true ? "and extracted in #{resource[:extract_path]}" : ''
36         cleanup = resource[:cleanup] == :true ? 'with cleanup' : 'without cleanup'
37
38         if provider.archive_checksum
39           "replace archive: #{provider.archive_filepath} from #{is_to_s(currentvalue)} to #{should_to_s(newvalue)}"
40         else
41           "download archive from #{resource[:source]} to #{provider.archive_filepath} #{extract} #{cleanup}"
42         end
43       elsif newvalue == :absent
44         "remove archive: #{provider.archive_filepath} "
45       else
46         super
47       end
48     rescue StandardError
49       super
50     end
51   end
52
53   newparam(:path, namevar: true) do
54     desc 'namevar, archive file fully qualified file path.'
55     validate do |value|
56       unless Puppet::Util.absolute_path? value
57         raise ArgumentError, "archive path must be absolute: #{value}"
58       end
59     end
60   end
61
62   newparam(:filename) do
63     desc 'archive file name (derived from path).'
64   end
65
66   newparam(:extract) do
67     desc 'whether archive will be extracted after download (true|false).'
68     newvalues(:true, :false)
69     defaultto(:false)
70   end
71
72   newparam(:extract_path) do
73     desc 'target folder path to extract archive.'
74     validate do |value|
75       unless Puppet::Util.absolute_path? value
76         raise ArgumentError, "archive extract_path must be absolute: #{value}"
77       end
78     end
79   end
80   newparam(:target) do
81     desc 'target folder path to extract archive. (this parameter is for camptocamp/archive compatibility)'
82     validate do |value|
83       unless Puppet::Util.absolute_path? value
84         raise ArgumentError, "archive extract_path must be absolute: #{value}"
85       end
86     end
87     munge do |val|
88       resource[:extract_path] = val
89     end
90   end
91
92   newparam(:extract_command) do
93     desc "custom extraction command ('tar xvf example.tar.gz'), also support sprintf format ('tar xvf %s') which will be processed with the filename: sprintf('tar xvf %s', filename)"
94   end
95
96   newparam(:temp_dir) do
97     desc 'Specify an alternative temporary directory to use for copying files, if unset then the operating system default will be used.'
98     validate do |value|
99       unless Puppet::Util.absolute_path?(value)
100         raise ArgumentError, "Invalid temp_dir #{value}"
101       end
102     end
103   end
104
105   newparam(:extract_flags) do
106     desc "custom extraction options, this replaces the default flags. A string such as 'xvf' for a tar file would replace the default xf flag. A hash is useful when custom flags are needed for different platforms. {'tar' => 'xzf', '7z' => 'x -aot'}."
107     defaultto(:undef)
108   end
109
110   newproperty(:creates) do
111     desc 'if file/directory exists, will not download/extract archive.'
112
113     def should_to_s(value)
114       "extracting in #{resource[:extract_path]} to create #{value}"
115     end
116   end
117
118   newparam(:cleanup) do
119     desc 'whether archive file will be removed after extraction (true|false).'
120     newvalues(:true, :false)
121     defaultto(:true)
122   end
123
124   newparam(:source) do
125     desc 'archive file source, supports puppet|http|https|ftp|file|s3 uri.'
126     validate do |value|
127       unless value =~ URI.regexp(%w[puppet http https ftp file s3]) || Puppet::Util.absolute_path?(value)
128         raise ArgumentError, "invalid source url: #{value}"
129       end
130     end
131   end
132
133   newparam(:url) do
134     desc 'archive file source, supports http|https|ftp|file uri.
135     (for camptocamp/archive compatibility)'
136     validate do |value|
137       unless value =~ URI.regexp(%w[http https file ftp])
138         raise ArgumentError, "invalid source url: #{value}"
139       end
140     end
141     munge do |val|
142       resource[:source] = val
143     end
144   end
145
146   newparam(:cookie) do
147     desc 'archive file download cookie.'
148   end
149
150   newparam(:checksum) do
151     desc 'archive file checksum (match checksum_type).'
152     newvalues(%r{\b[0-9a-f]{5,128}\b}, :true, :false, :undef, nil, '')
153     munge do |val|
154       if val.nil? || val.empty? || val == :undef
155         :false
156       elsif [:true, :false].include? val
157         resource[:checksum_verify] = val
158       else
159         val
160       end
161     end
162   end
163   newparam(:digest_string) do
164     desc 'archive file checksum (match checksum_type)
165     (this parameter is for camptocamp/archive compatibility).'
166     newvalues(%r{\b[0-9a-f]{5,128}\b})
167     munge do |val|
168       if !val.nil? && !val.empty?
169         resource[:checksum] = val
170       else
171         val
172       end
173     end
174   end
175
176   newparam(:checksum_url) do
177     desc 'archive file checksum source (instead of specifying checksum)'
178   end
179   newparam(:digest_url) do
180     desc 'archive file checksum source (instead of specifying checksum)
181     (this parameter is for camptocamp/archive compatibility)'
182     munge do |val|
183       resource[:checksum_url] = val
184     end
185   end
186
187   newparam(:checksum_type) do
188     desc 'archive file checksum type (none|md5|sha1|sha2|sha256|sha384|sha512).'
189     newvalues(:none, :md5, :sha1, :sha2, :sha256, :sha384, :sha512)
190     defaultto(:none)
191   end
192   newparam(:digest_type) do
193     desc 'archive file checksum type (none|md5|sha1|sha2|sha256|sha384|sha512)
194     (this parameter is camptocamp/archive compatibility).'
195     newvalues(:none, :md5, :sha1, :sha2, :sha256, :sha384, :sha512)
196     munge do |val|
197       resource[:checksum_type] = val
198     end
199   end
200
201   newparam(:checksum_verify) do
202     desc 'whether checksum wil be verified (true|false).'
203     newvalues(:true, :false)
204     defaultto(:true)
205   end
206
207   newparam(:username) do
208     desc 'username to download source file.'
209   end
210
211   newparam(:password) do
212     desc 'password to download source file.'
213   end
214
215   newparam(:user) do
216     desc 'extract command user (using this option will configure the archive file permission to 0644 so the user can read the file).'
217   end
218
219   newparam(:group) do
220     desc 'extract command group (using this option will configure the archive file permisison to 0644 so the user can read the file).'
221   end
222
223   newparam(:proxy_type) do
224     desc 'proxy type (none|ftp|http|https)'
225     newvalues(:none, :ftp, :http, :https)
226   end
227
228   newparam(:proxy_server) do
229     desc 'proxy address to use when accessing source'
230   end
231
232   newparam(:allow_insecure, boolean: true, parent: Puppet::Parameter::Boolean) do
233     desc 'ignore HTTPS certificate errors'
234     defaultto :false
235   end
236
237   newparam(:download_options) do
238     desc 'provider download options (affects curl, wget, and only s3 downloads for ruby provider)'
239
240     validate do |val|
241       unless val.is_a?(::String) || val.is_a?(::Array)
242         raise ArgumentError, "download_options should be String or Array: #{val}"
243       end
244     end
245
246     munge do |val|
247       case val
248       when ::String
249         [val]
250       else
251         val
252       end
253     end
254   end
255
256   autorequire(:file) do
257     [
258       Pathname.new(self[:path]).parent.to_s,
259       self[:extract_path],
260       '/root/.aws/config',
261       '/root/.aws/credentials'
262     ].compact
263   end
264
265   autorequire(:exec) do
266     ['install_aws_cli']
267   end
268
269   validate do
270     filepath = Pathname.new(self[:path])
271     self[:filename] = filepath.basename.to_s
272     if !self[:source].nil? && !self[:url].nil? && self[:source] != self[:url]
273       raise ArgumentError, "invalid parameter: url (#{self[:url]}) and source (#{self[:source]}) are mutually exclusive."
274     end
275     if !self[:checksum_url].nil? && !self[:digest_url].nil? && self[:checksum_url] != self[:digest_url]
276       raise ArgumentError, "invalid parameter: checksum_url (#{self[:checksum_url]}) and digest_url (#{self[:digest_url]}) are mutually exclusive."
277     end
278     if self[:proxy_server]
279       self[:proxy_type] ||= URI(self[:proxy_server]).scheme.to_sym
280     else
281       self[:proxy_type] = :none
282     end
283   end
284 end