def retrieve
      raise RetrieverError.new("download retriever is unavailable") unless available?
      ::FileUtils.remove_entry_secure @repo_dir if File.exists?(@repo_dir)
      ::FileUtils.remove_entry_secure workdir if File.exists?(workdir)
      ::FileUtils.mkdir_p @repo_dir
      ::FileUtils.mkdir_p workdir
      file = ::File.join(workdir, "package")

      # TEAL FIX: we have to always-download the tarball before we can
      # determine if contents have changed, but afterward we can compare the
      # previous download against the latest downloaded and short-circuit the
      # remaining flow for the no-difference case.
      @logger.operation(:downloading) do
        credential_command = if @repository.first_credential && @repository.second_credential
          ['-u', "#{@repository.first_credential}:#{@repository.second_credential}"]
        else
          []
        end
        @output = ::RightScale::RightPopen::SafeOutputBuffer.new
        @cmd = [
          'curl',
          '--silent', '--show-error', '--location', '--fail',
          '--location-trusted', '-o', file, credential_command,
          @repository.url
        ].flatten
        begin
          ::RightScale::RightPopen.popen3_sync(
            @cmd,
            :target             => self,
            :pid_handler        => :pid_download,
            :timeout_handler    => :timeout_download,
            :size_limit_handler => :size_limit_download,
            :exit_handler       => :exit_download,
            :stderr_handler     => :output_download,
            :stdout_handler     => :output_download,
            :inherit_io         => true,  # avoid killing any rails connection
            :watch_directory    => workdir,
            :size_limit_bytes   => @max_bytes,
            :timeout_seconds    => @max_seconds)
        rescue Exception => e
          @logger.note_phase(:abort, :running_command, 'curl', e)
          raise
        end
      end

      note_tag(file)

      @logger.operation(:unpacking) do
        path = @repository.to_url.path
        if path =~ /\.gz$/
          extraction = "xzf"
        elsif path =~ /\.bz2$/
          extraction = "xjf"
        else
          extraction = "xf"
        end
        Dir.chdir(@repo_dir) do
          @output = ::RightScale::RightPopen::SafeOutputBuffer.new
          @cmd = ['tar', extraction, file]
          begin
            ::RightScale::RightPopen.popen3_sync(
              @cmd,
              :target             => self,
              :pid_handler        => :pid_download,
              :timeout_handler    => :timeout_download,
              :size_limit_handler => :size_limit_download,
              :exit_handler       => :exit_download,
              :stderr_handler     => :output_download,
              :stdout_handler     => :output_download,
              :inherit_io         => true,  # avoid killing any rails connection
              :watch_directory    => @repo_dir,
              :size_limit_bytes   => @max_bytes,
              :timeout_seconds    => @max_seconds)
          rescue Exception => e
            @logger.note_phase(:abort, :running_command, @cmd.first, e)
            raise
          end
        end
      end
      true
    end
def retrieve
      raise RetrieverError.new("download retriever is unavailable") unless available?
      ::FileUtils.remove_entry_secure @repo_dir if File.exists?(@repo_dir)
      ::FileUtils.remove_entry_secure workdir if File.exists?(workdir)
      ::FileUtils.mkdir_p @repo_dir
      ::FileUtils.mkdir_p workdir
      file = ::File.join(workdir, "package")

      # TEAL FIX: we have to always-download the tarball before we can
      # determine if contents have changed, but afterward we can compare the
      # previous download against the latest downloaded and short-circuit the
      # remaining flow for the no-difference case.
      @logger.operation(:downloading) do
        credential_command = if @repository.first_credential && @repository.second_credential
          ['-u', "#{@repository.first_credential}:#{@repository.second_credential}"]
        else
          []
        end
        @output = ::RightScale::RightPopen::SafeOutputBuffer.new
        @cmd = [
          'curl',
          '--silent', '--show-error', '--location', '--fail',
          '--location-trusted', '-o', file, credential_command,
          @repository.url
        ].flatten
        begin
          ::RightScale::RightPopen.popen3_sync(
            @cmd,
            :target             => self,
            :pid_handler        => :pid_download,
            :timeout_handler    => :timeout_download,
            :size_limit_handler => :size_limit_download,
            :exit_handler       => :exit_download,
            :stderr_handler     => :output_download,
            :stdout_handler     => :output_download,
            :inherit_io         => true,  # avoid killing any rails connection
            :watch_directory    => workdir,
            :size_limit_bytes   => @max_bytes,
            :timeout_seconds    => @max_seconds)
        rescue Exception => e
          @logger.note_phase(:abort, :running_command, 'curl', e)
          raise
        end
      end

      note_tag(file)

      @logger.operation(:unpacking) do
        path = @repository.to_url.path
        if path =~ /\.gz$/
          extraction = "xzf"
        elsif path =~ /\.bz2$/
          extraction = "xjf"
        else
          extraction = "xf"
        end
        Dir.chdir(@repo_dir) do
          @output = ::RightScale::RightPopen::SafeOutputBuffer.new
          @cmd = ['tar', extraction, file]
          begin
            ::RightScale::RightPopen.popen3_sync(
              @cmd,
              :target             => self,
              :pid_handler        => :pid_download,
              :timeout_handler    => :timeout_download,
              :size_limit_handler => :size_limit_download,
              :exit_handler       => :exit_download,
              :stderr_handler     => :output_download,
              :stdout_handler     => :output_download,
              :inherit_io         => true,  # avoid killing any rails connection
              :watch_directory    => @repo_dir,
              :size_limit_bytes   => @max_bytes,
              :timeout_seconds    => @max_seconds)
          rescue Exception => e
            @logger.note_phase(:abort, :running_command, @cmd.first, e)
            raise
          end
        end
      end
      true
    end
Download tarball and unpack it