diff --git a/lib/instance_agent/platform/linux_util.rb b/lib/instance_agent/platform/linux_util.rb index a4512774..464f0b12 100644 --- a/lib/instance_agent/platform/linux_util.rb +++ b/lib/instance_agent/platform/linux_util.rb @@ -39,6 +39,7 @@ def self.script_executable?(path) end def self.extract_tar(bundle_file, dst) + log(:debug, "extract_tar - dst : #{dst}") FileUtils.mkdir_p(dst) working_dir = FileUtils.pwd() absolute_bundle_path = File.expand_path(bundle_file) @@ -47,7 +48,18 @@ def self.extract_tar(bundle_file, dst) FileUtils.cd(working_dir) end + def self.extract_zip(bundle_file, dst) + log(:debug, "extract_zip - dst : #{dst}") + FileUtils.mkdir_p(dst) + working_dir = FileUtils.pwd() + absolute_bundle_path = File.expand_path(bundle_file) + FileUtils.cd(dst) + execute_zip_command("unzip -qo #{absolute_bundle_path}") + FileUtils.cd(working_dir) + end + def self.extract_tgz(bundle_file, dst) + log(:debug, "extract_tgz - dst : #{dst}") FileUtils.mkdir_p(dst) working_dir = FileUtils.pwd() absolute_bundle_path = File.expand_path(bundle_file) @@ -110,6 +122,23 @@ def self.execute_tar_command(cmd) raise msg end end + + private + def self.execute_zip_command(cmd) + log(:debug, "Executing #{cmd}") + + output = `#{cmd} 2>&1` + exit_status = $?.exitstatus + + log(:debug, "Command status: #{$?}") + log(:debug, "Command output: #{output}") + + if exit_status != 0 + msg = "Error extracting zip archive: #{exit_status}" + log(:error, msg) + raise msg + end + end private def self.log(severity, message) diff --git a/lib/instance_agent/platform/windows_util.rb b/lib/instance_agent/platform/windows_util.rb index 2980a155..baa1a899 100644 --- a/lib/instance_agent/platform/windows_util.rb +++ b/lib/instance_agent/platform/windows_util.rb @@ -42,6 +42,14 @@ def self.extract_tgz(bundle_file, dst) Minitar.unpack(compressed, dst) end + def self.extract_zip(bundle_file, dst) + log(:debug, "extract_zip - dst : #{dst}") + FileUtils.mkdir_p(dst) + working_dir = FileUtils.pwd() + absolute_bundle_path = File.expand_path(bundle_file) + execute_zip_command("powershell [System.Reflection.Assembly]::LoadWithPartialName(‘System.IO.Compression.FileSystem’); [System.IO.Compression.ZipFile]::ExtractToDirectory(‘#{absolute_bundle_path}’, ‘#{dst}’)") + end + def self.supports_process_groups?() false end @@ -80,6 +88,23 @@ def self.delete_folder (dir) end end + private + def self.execute_zip_command(cmd) + log(:debug, "Executing #{cmd}") + + output = `#{cmd} 2>&1` + exit_status = $?.exitstatus + + log(:debug, "Command status: #{$?}") + log(:debug, "Command output: #{output}") + + if exit_status != 0 + msg = "Error extracting zip archive: #{exit_status}" + log(:debug, msg) + raise msg + end + end + private def self.log(severity, message) raise ArgumentError, "Unknown severity #{severity.inspect}" unless InstanceAgent::Log::SEVERITIES.include?(severity.to_s) diff --git a/lib/instance_agent/plugins/codedeploy/command_executor.rb b/lib/instance_agent/plugins/codedeploy/command_executor.rb index 3d1a1def..4a110238 100644 --- a/lib/instance_agent/plugins/codedeploy/command_executor.rb +++ b/lib/instance_agent/plugins/codedeploy/command_executor.rb @@ -381,11 +381,15 @@ def unpack_bundle(cmd, bundle_file, deployment_spec) elsif "tgz".eql? deployment_spec.bundle_type InstanceAgent::Platform.util.extract_tgz(bundle_file, dst) elsif "zip".eql? deployment_spec.bundle_type - Zip::File.open(bundle_file) do |zipfile| - zipfile.each do |f| - file_dst = File.join(dst, f.name) - FileUtils.mkdir_p(File.dirname(file_dst)) - zipfile.extract(f, file_dst) + begin + InstanceAgent::Platform.util.extract_zip(bundle_file, dst) + rescue + Zip::File.open(bundle_file) do |zipfile| + zipfile.each do |f| + file_dst = File.join(dst, f.name) + FileUtils.mkdir_p(File.dirname(file_dst)) + zipfile.extract(f, file_dst) + end end end else