Module: Bosh::Director::DownloadHelper

Included in:
Jobs::UpdateRelease, Jobs::UpdateStemcell
Defined in:
lib/bosh/director/download_helper.rb

Instance Method Summary collapse

Instance Method Details

#download_remote_file(resource, remote_file, local_file, num_redirects = 0) ⇒ Object

Downloads a remote file

Parameters:

  • resource (String)

    Resource name to be logged

  • remote_file (String)

    Remote file to download

  • local_file (String)

    Local file to store the downloaded file

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/bosh/director/download_helper.rb', line 13

def download_remote_file(resource, remote_file, local_file, num_redirects = 0)
  @logger.info("Downloading remote #{resource} from #{remote_file}") if @logger
  uri = URI.parse(remote_file)
  Net::HTTP.start(uri.host, uri.port,
                  :use_ssl => uri.scheme == 'https',
                  :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |http|
    http.request_get(uri.request_uri) do |response|
      case response
        when Net::HTTPSuccess
          File.open(local_file, 'wb') do |file|
            response.read_body do |chunk|
              file.write(chunk)
            end
          end

        when Net::HTTPFound
          raise ResourceError, "Too many redirects at '#{remote_file}'." if num_redirects >= 9
          location = response.header['location']
          raise ResourceError, "No location header for redirect found at '#{remote_file}'." if location.nil?

          location = URI.join(uri, location).to_s
          download_remote_file(resource, location, local_file, num_redirects + 1)

        when Net::HTTPNotFound
          @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger
          raise ResourceNotFound, "No #{resource} found at '#{remote_file}'."

        else
          @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger
          raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details."
      end
    end
  end
rescue URI::Error, SocketError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError,
       Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{e.inspect}") if @logger
  raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details."
end