4 require 'puppet/parameter/boolean'
6 Puppet::Type.newtype(:archive) do
7 @doc = 'Manage archive file download, extraction, and cleanup.'
10 desc 'whether archive file should be present/absent (default: present)'
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
28 def should_to_s(value)
29 return "(#{resource[:checksum_type]})#{resource[:checksum]}" if provider.archive_checksum
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'
38 if provider.archive_checksum
39 "replace archive: #{provider.archive_filepath} from #{is_to_s(currentvalue)} to #{should_to_s(newvalue)}"
41 "download archive from #{resource[:source]} to #{provider.archive_filepath} #{extract} #{cleanup}"
43 elsif newvalue == :absent
44 "remove archive: #{provider.archive_filepath} "
53 newparam(:path, namevar: true) do
54 desc 'namevar, archive file fully qualified file path.'
56 unless Puppet::Util.absolute_path? value
57 raise ArgumentError, "archive path must be absolute: #{value}"
62 newparam(:filename) do
63 desc 'archive file name (derived from path).'
67 desc 'whether archive will be extracted after download (true|false).'
68 newvalues(:true, :false)
72 newparam(:extract_path) do
73 desc 'target folder path to extract archive.'
75 unless Puppet::Util.absolute_path? value
76 raise ArgumentError, "archive extract_path must be absolute: #{value}"
81 desc 'target folder path to extract archive. (this parameter is for camptocamp/archive compatibility)'
83 unless Puppet::Util.absolute_path? value
84 raise ArgumentError, "archive extract_path must be absolute: #{value}"
88 resource[:extract_path] = val
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)"
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.'
99 unless Puppet::Util.absolute_path?(value)
100 raise ArgumentError, "Invalid temp_dir #{value}"
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'}."
110 newproperty(:creates) do
111 desc 'if file/directory exists, will not download/extract archive.'
113 def should_to_s(value)
114 "extracting in #{resource[:extract_path]} to create #{value}"
118 newparam(:cleanup) do
119 desc 'whether archive file will be removed after extraction (true|false).'
120 newvalues(:true, :false)
125 desc 'archive file source, supports puppet|http|https|ftp|file|s3 uri.'
127 unless value =~ URI.regexp(%w[puppet http https ftp file s3]) || Puppet::Util.absolute_path?(value)
128 raise ArgumentError, "invalid source url: #{value}"
134 desc 'archive file source, supports http|https|ftp|file uri.
135 (for camptocamp/archive compatibility)'
137 unless value =~ URI.regexp(%w[http https file ftp])
138 raise ArgumentError, "invalid source url: #{value}"
142 resource[:source] = val
147 desc 'archive file download cookie.'
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, '')
154 if val.nil? || val.empty? || val == :undef
156 elsif [:true, :false].include? val
157 resource[:checksum_verify] = val
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})
168 if !val.nil? && !val.empty?
169 resource[:checksum] = val
176 newparam(:checksum_url) do
177 desc 'archive file checksum source (instead of specifying checksum)'
179 newparam(:digest_url) do
180 desc 'archive file checksum source (instead of specifying checksum)
181 (this parameter is for camptocamp/archive compatibility)'
183 resource[:checksum_url] = val
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)
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)
197 resource[:checksum_type] = val
201 newparam(:checksum_verify) do
202 desc 'whether checksum wil be verified (true|false).'
203 newvalues(:true, :false)
207 newparam(:username) do
208 desc 'username to download source file.'
211 newparam(:password) do
212 desc 'password to download source file.'
216 desc 'extract command user (using this option will configure the archive file permission to 0644 so the user can read the file).'
220 desc 'extract command group (using this option will configure the archive file permisison to 0644 so the user can read the file).'
223 newparam(:proxy_type) do
224 desc 'proxy type (none|ftp|http|https)'
225 newvalues(:none, :ftp, :http, :https)
228 newparam(:proxy_server) do
229 desc 'proxy address to use when accessing source'
232 newparam(:allow_insecure, boolean: true, parent: Puppet::Parameter::Boolean) do
233 desc 'ignore HTTPS certificate errors'
237 newparam(:download_options) do
238 desc 'provider download options (affects curl, wget, and only s3 downloads for ruby provider)'
241 unless val.is_a?(::String) || val.is_a?(::Array)
242 raise ArgumentError, "download_options should be String or Array: #{val}"
256 autorequire(:file) do
258 Pathname.new(self[:path]).parent.to_s,
261 '/root/.aws/credentials'
265 autorequire(:exec) 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."
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."
278 if self[:proxy_server]
279 self[:proxy_type] ||= URI(self[:proxy_server]).scheme.to_sym
281 self[:proxy_type] = :none