5959#
6060
6161Puppet ::Type . type ( :archive ) . provide ( :ruby ) do
62+ desc 'Archive resource type for Puppet.'
6263 optional_commands aws : 'aws'
6364 optional_commands gsutil : 'gsutil'
6465 defaultfor feature : :microsoft_windows
@@ -112,15 +113,12 @@ def checksum
112113 end
113114
114115 def remote_checksum
115- PuppetX ::Bodeco ::Util . content (
116- resource [ :checksum_url ] ,
117- username : resource [ :username ] ,
118- password : resource [ :password ] ,
119- cookie : resource [ :cookie ] ,
120- proxy_server : resource [ :proxy_server ] ,
121- proxy_type : resource [ :proxy_type ] ,
122- insecure : resource [ :allow_insecure ]
123- ) [ %r{\b [\d a-f]{32,128}\b }i ]
116+ temp_path = download_file ( resource [ :checksum_url ] )
117+ raise ( Puppet ::Error , 'Unable to create temporary checksum file.' ) if temp_path . nil?
118+
119+ File . read ( temp_path ) [ %r{\b [\d a-f]{32,128}\b }i ]
120+ ensure
121+ FileUtils . rm_f ( temp_path ) if temp_path && File . exist? ( temp_path )
124122 end
125123
126124 # Private: See if local archive checksum matches.
@@ -160,46 +158,48 @@ def extracted?
160158 end
161159
162160 def transfer_download ( archive_filepath )
163- raise Puppet ::Error , "Temporary directory #{ resource [ :temp_dir ] } doesn't exist" if resource [ :temp_dir ] && ! File . directory? ( resource [ :temp_dir ] )
161+ raise ( Puppet ::Error , 'Unable to fetch archive, the source parameter is nil.' ) if resource [ :source ] . nil?
164162
165- tempfile = Tempfile . new ( tempfile_name , resource [ :temp_dir ] )
166-
167- temppath = tempfile . path
168- tempfile . close!
169-
170- case resource [ :source ]
171- when %r{^(puppet)}
172- puppet_download ( temppath )
173- when %r{^(http|ftp)}
174- download ( temppath )
175- when %r{^file}
176- uri = URI ( resource [ :source ] )
177- FileUtils . copy ( Puppet ::Util . uri_to_path ( uri ) , temppath )
178- when %r{^s3}
179- s3_download ( temppath )
180- when %r{^gs}
181- gs_download ( temppath )
182- when nil
183- raise ( Puppet ::Error , 'Unable to fetch archive, the source parameter is nil.' )
184- else
185- raise ( Puppet ::Error , "Source file: #{ resource [ :source ] } does not exists." ) unless File . exist? ( resource [ :source ] )
186-
187- FileUtils . copy ( resource [ :source ] , temppath )
188- end
163+ temp_path = download_file ( resource [ :source ] )
164+ raise ( Puppet ::Error , 'Unable to create temporary file.' ) if temp_path . nil?
189165
190166 # conditionally verify checksum:
191167 if resource [ :checksum_verify ] == :true && resource [ :checksum_type ] != :none
192- archive = PuppetX ::Bodeco ::Archive . new ( temppath )
168+ archive = PuppetX ::Bodeco ::Archive . new ( temp_path )
193169 actual_checksum = archive . checksum ( resource [ :checksum_type ] )
194170 if actual_checksum != checksum
195171 destroy
196172 raise ( Puppet ::Error , "Download file checksum mismatch (expected: #{ checksum } actual: #{ actual_checksum } )" )
197173 end
198174 end
199175
200- move_file_in_place ( temppath , archive_filepath )
176+ move_file_in_place ( temp_path , archive_filepath )
201177 ensure
202- FileUtils . rm_f ( temppath ) if File . exist? ( temppath )
178+ FileUtils . rm_f ( temp_path ) if temp_path && File . exist? ( temp_path )
179+ end
180+
181+ def download_file ( location )
182+ raise Puppet ::Error , "Temporary directory #{ resource [ :temp_dir ] } doesn't exist" if resource [ :temp_dir ] && !File . directory? ( resource [ :temp_dir ] )
183+
184+ Dir ::Tmpname . create ( tempfile_name , resource [ :temp_dir ] ) do |temp_path |
185+ case location
186+ when %r{^(puppet)}
187+ puppet_download ( location , temp_path )
188+ when %r{^(http|ftp)}
189+ download ( location , temp_path )
190+ when %r{^file}
191+ uri = URI ( location )
192+ FileUtils . copy ( Puppet ::Util . uri_to_path ( uri ) , temp_path )
193+ when %r{^s3}
194+ s3_download ( location , temp_path )
195+ when %r{^gs}
196+ gs_download ( location , temp_path )
197+ else
198+ raise ( Puppet ::Error , "Source file: #{ location } does not exists." ) unless File . exist? ( location )
199+
200+ FileUtils . copy ( location , temp_path )
201+ end
202+ end
203203 end
204204
205205 def move_file_in_place ( from , to )
@@ -208,9 +208,9 @@ def move_file_in_place(from, to)
208208 FileUtils . mv ( from , to )
209209 end
210210
211- def download ( filepath )
211+ def download ( location , filepath )
212212 PuppetX ::Bodeco ::Util . download (
213- resource [ :source ] ,
213+ location ,
214214 filepath ,
215215 username : resource [ :username ] ,
216216 password : resource [ :password ] ,
@@ -221,29 +221,29 @@ def download(filepath)
221221 )
222222 end
223223
224- def puppet_download ( filepath )
224+ def puppet_download ( location , filepath )
225225 PuppetX ::Bodeco ::Util . puppet_download (
226- resource [ :source ] ,
226+ location ,
227227 filepath
228228 )
229229 end
230230
231- def s3_download ( path )
231+ def s3_download ( location , path )
232232 params = [
233233 's3' ,
234234 'cp' ,
235- resource [ :source ] ,
235+ location ,
236236 path
237237 ]
238238 params += resource [ :download_options ] if resource [ :download_options ]
239239
240240 aws ( params )
241241 end
242242
243- def gs_download ( path )
243+ def gs_download ( location , path )
244244 params = [
245245 'cp' ,
246- resource [ :source ] ,
246+ location ,
247247 path
248248 ]
249249 params += resource [ :download_options ] if resource [ :download_options ]
0 commit comments