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) ⇒ 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:



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
# File 'lib/bosh/director/download_helper.rb', line 15

def download_remote_file(resource, remote_file, local_file)
  @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|
      unless response.kind_of? Net::HTTPSuccess
        @logger.error("Downloading remote #{resource} from #{remote_file} failed: #{response.message}") if @logger
        if response.kind_of? Net::HTTPNotFound 
          raise ResourceNotFound, "No #{resource} found at `#{remote_file}'."
        else
          raise ResourceError, "Downloading remote #{resource} failed. Check task debug log for details."
        end
      end
        
      File.open(local_file, 'wb') do |file|
        response.read_body do |chunk|
          file.write(chunk)
        end
      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