Method: Chef::CookbookUploader#uploader_function_for

Defined in:
lib/chef/cookbook_uploader.rb

#uploader_function_for(file, checksum, url, checksums_to_upload) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/cookbook_uploader.rb', line 116

def uploader_function_for(file, checksum, url, checksums_to_upload)
  lambda do
    # Checksum is the hexadecimal representation of the md5,
    # but we need the base64 encoding for the content-md5
    # header
    checksum64 = Base64.encode64([checksum].pack("H*")).strip
    file_contents = File.binread(file)

    # Custom headers. 'content-type' disables JSON serialization of the request body.
    headers = { "content-type" => "application/x-binary", "content-md5" => checksum64, "accept" => "application/json" }

    begin
      rest.put(url, file_contents, headers)
      checksums_to_upload.delete(checksum)
    rescue Net::HTTPClientException, Net::HTTPFatalError, Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError => e
      error_message = "Failed to upload #{file} (#{checksum}) to #{url} : #{e.message}"
      error_message << "\n#{e.response.body}" if e.respond_to?(:response)
      Chef::Knife.ui.error(error_message)
      raise
    end
  end
end