unpack_bundle

in lib/instance_agent/plugins/codedeploy/command_executor.rb [399:446]


        def unpack_bundle(cmd, bundle_file, deployment_spec)
          dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive')

          if "tar".eql? deployment_spec.bundle_type
            InstanceAgent::Platform.util.extract_tar(bundle_file, dst)
          elsif "tgz".eql? deployment_spec.bundle_type
            InstanceAgent::Platform.util.extract_tgz(bundle_file, dst)
          elsif "zip".eql? deployment_spec.bundle_type
            begin
              InstanceAgent::Platform.util.extract_zip(bundle_file, dst)
            rescue
              log(:warn, "Encountered non-zero exit code with default system unzip util. Hence falling back to ruby unzip to mitigate any partially unzipped or skipped zip files.")
              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) { true }
                end
              end
            end
          else
            InstanceAgent::Platform.util.extract_tar(bundle_file, dst)
          end

          archive_root_files = Dir.entries(dst)
          archive_root_files.delete_if { |name| name == '.' || name == '..' }

          
          
          if ((archive_root_files.size == 1) &&
              File.directory?(File.join(dst, archive_root_files[0])) &&
              Dir.entries(File.join(dst, archive_root_files[0])).grep(/appspec/i).any?)
            log(:info, "Stripping leading directory from archive bundle contents.")
            
            tmp_dst = File.join(deployment_root_dir(deployment_spec), 'deployment-archive-temp')
            FileUtils.rm_rf(tmp_dst)
            FileUtils.mv(dst, tmp_dst)

            
            nested_archive_root = File.join(tmp_dst, archive_root_files[0])
            log(:debug, "Actual archive root at #{nested_archive_root}. Moving to #{dst}")
            FileUtils.mv(nested_archive_root, dst)
            FileUtils.rmdir(tmp_dst)

            log(:debug, Dir.entries(dst).join("; "))
          end
        end