summaryrefslogtreecommitdiff
path: root/lib/chef/cookbook_uploader.rb
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2014-07-29 19:30:32 -0700
committerdanielsdeleo <dan@getchef.com>2014-07-30 14:07:07 -0700
commitd48a9b9af71844da7c0b517db29512be6d3505b7 (patch)
tree2c06c49674a03ef6a5fd6774acfb0da06fc2dfef /lib/chef/cookbook_uploader.rb
parent263f62774641bf32be6fd79d1d69022531fb3285 (diff)
downloadchef-d48a9b9af71844da7c0b517db29512be6d3505b7.tar.gz
Update CookbookUploader contract with HTTP client object
* Use the same HTTP client object for cookbook file upload as for object upload. Now that JSONInput disables json-encoding of request body based on content type header, there is no need to work around that limitation by using a different HTTP client class/object. * Use method names that don't have `_rest`. These are not supported by custom Chef::HTTP subclasses (also they are ugly and redundant). * Add high-ish level unit tests. The code must be refactored in order to write better unit tests, but tests for a variety of "happy paths" are needed before that can occur.
Diffstat (limited to 'lib/chef/cookbook_uploader.rb')
-rw-r--r--lib/chef/cookbook_uploader.rb25
1 files changed, 6 insertions, 19 deletions
diff --git a/lib/chef/cookbook_uploader.rb b/lib/chef/cookbook_uploader.rb
index adad55b4ca..ae02dd68c4 100644
--- a/lib/chef/cookbook_uploader.rb
+++ b/lib/chef/cookbook_uploader.rb
@@ -53,7 +53,7 @@ class Chef
end
checksums = checksum_files.inject({}){|memo,elt| memo[elt.first]=nil ; memo}
- new_sandbox = rest.post_rest("sandboxes", { :checksums => checksums })
+ new_sandbox = rest.post("sandboxes", { :checksums => checksums })
Chef::Log.info("Uploading files")
@@ -80,7 +80,7 @@ class Chef
# in eventual consistency)
retries = 0
begin
- rest.put_rest(sandbox_url, {:is_completed => true})
+ rest.put(sandbox_url, {:is_completed => true})
rescue Net::HTTPServerException => e
if e.message =~ /^400/ && (retries += 1) <= 5
sleep 2
@@ -94,7 +94,7 @@ class Chef
cookbooks.each do |cb|
save_url = opts[:force] ? cb.force_save_url : cb.save_url
begin
- rest.put_rest(save_url, cb)
+ rest.put(save_url, cb)
rescue Net::HTTPServerException => e
case e.response.code
when "409"
@@ -108,32 +108,19 @@ class Chef
Chef::Log.info("Upload complete!")
end
- def worker_thread(work_queue)
- end
-
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
- timestamp = Time.now.utc.iso8601
file_contents = File.open(file, "rb") {|f| f.read}
- # TODO - 5/28/2010, cw: make signing and sending the request streaming
+
+ # Custom headers. 'content-type' disables JSON serialization of the request body.
headers = { 'content-type' => 'application/x-binary', 'content-md5' => checksum64, "accept" => 'application/json' }
- if rest.signing_key
- sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(
- :http_method => :put,
- :path => URI.parse(url).path,
- :body => file_contents,
- :timestamp => timestamp,
- :user_id => rest.client_name
- )
- headers.merge!(sign_obj.sign(OpenSSL::PKey::RSA.new(rest.signing_key)))
- end
begin
- Chef::HTTP::Simple.new(url, :headers=>headers).put(url, file_contents)
+ rest.put(url, file_contents, headers)
checksums_to_upload.delete(checksum)
rescue Net::HTTPServerException, Net::HTTPFatalError, Errno::ECONNREFUSED, Timeout::Error, Errno::ETIMEDOUT, SocketError => e
error_message = "Failed to upload #{file} (#{checksum}) to #{url} : #{e.message}"