2 require 'puppet/util/execution'
10 @file_path = if Facter.value(:osfamily) == 'windows'
13 Shellwords.shellescape file
18 return nil if type == :none
20 digest = Digest.const_get(type.to_s.upcase)
21 digest.file(@file).hexdigest
23 raise $ERROR_INFO, "invalid checksum type #{type}. #{$ERROR_INFO}", $ERROR_INFO.backtrace
27 if Facter.value(:osfamily) == 'windows'
34 def extract(path = root_dir, opts = {})
42 custom_command = opts.fetch(:custom_command, nil)
43 options = opts.fetch(:options)
45 cmd = if custom_command && custom_command =~ %r{%s}
46 custom_command % @file_path
48 "#{custom_command} #{options} #{@file_path}"
53 Puppet.debug("Archive extracting #{@file} in #{path}: #{cmd}")
54 File.chmod(0o644, @file) if opts[:uid] || opts[:gid]
55 Puppet::Util::Execution.execute(cmd, uid: opts[:uid], gid: opts[:gid], failonfail: true, squelch: false, combine: true)
62 if ENV['path'].include?('7-Zip')
64 elsif File.directory?('C:\\Program Files\\7-Zip')
65 'C:\\Program Files\\7-Zip\\7z.exe'
66 elsif File.directory?('C:\\Program Files (x86)\\7-zip')
67 'C:\\Program Files (x86)\\7-Zip\\7z.exe'
68 elsif @file_path =~ %r{.zip"$}
69 # Fallback to powershell for zipfiles - this works with windows
70 # 2012+ if your powershell/.net is too old the script will fail
71 # on execution and ask user to install 7zip.
72 # We have to manually extract each entry in the zip file
73 # to ensure we extract fresh copy because `ExtractToDirectory`
74 # method does not support overwriting
77 Add-Type -AssemblyName System.IO.Compression.FileSystem -erroraction "silentlycontinue"
78 $zipFile = [System.IO.Compression.ZipFile]::openread(#{@file_path})
79 foreach ($zipFileEntry in $zipFile.Entries) {
80 $pwd = (Get-Item -Path ".\" -Verbose).FullName
81 $outputFile = [io.path]::combine($pwd, $zipFileEntry.FullName)
82 $dir = ([io.fileinfo]$outputFile).DirectoryName
84 if (-not(Test-Path -type Container -path $dir)) {
87 if ($zipFileEntry.Name -ne "") {
88 write-host "[extract] $zipFileEntry.Name"
89 [System.IO.Compression.ZipFileExtensions]::ExtractToFile($zipFileEntry, $outputFile, $true)
92 } catch [System.invalidOperationException] {
93 write-error "Your OS does not support System.IO.Compression.FileSystem - please install 7zip"
97 "powershell -command #{ps.gsub(%r{"}, '\\"').gsub(%r{\n}, '; ')}"
99 raise Exception, '7z.exe not available'
104 if Facter.value(:osfamily) == 'windows'
105 opt = parse_flags('x -aoa', options, '7z')
107 cmd =~ %r{7z.exe} ? "#{cmd} #{opt} #{@file_path}" : cmd
111 opt = parse_flags('xf', options, 'tar')
112 "tar #{opt} #{@file_path}"
113 when %r{(\.tgz|\.tar\.gz)$}
114 case Facter.value(:osfamily)
115 when 'Solaris', 'AIX'
116 gunzip_opt = parse_flags('-dc', options, 'gunzip')
117 tar_opt = parse_flags('xf', options, 'tar')
118 "gunzip #{gunzip_opt} #{@file_path} | tar #{tar_opt} -"
120 opt = parse_flags('xzf', options, 'tar')
121 "tar #{opt} #{@file_path}"
123 when %r{(\.tbz|\.tar\.bz2)$}
124 case Facter.value(:osfamily)
125 when 'Solaris', 'AIX'
126 bunzip_opt = parse_flags('-dc', options, 'bunzip')
127 tar_opt = parse_flags('xf', options, 'tar')
128 "bunzip2 #{bunzip_opt} #{@file_path} | tar #{tar_opt} -"
130 opt = parse_flags('xjf', options, 'tar')
131 "tar #{opt} #{@file_path}"
133 when %r{(\.txz|\.tar\.xz)$}
134 unxz_opt = parse_flags('-dc', options, 'unxz')
135 tar_opt = parse_flags('xf', options, 'tar')
136 "unxz #{unxz_opt} #{@file_path} | tar #{tar_opt} -"
138 opt = parse_flags('-d', options, 'gunzip')
139 "gunzip #{opt} #{@file_path}"
140 when %r{(\.zip|\.war|\.jar)$}
141 opt = parse_flags('-o', options, 'zip')
142 "unzip #{opt} #{@file_path}"
144 raise NotImplementedError, "Unknown filetype: #{@file}"
149 def parse_flags(default, options, command = nil)
158 raise ArgumentError, "Invalid options for command #{command}: #{options.inspect}"