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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/chef/cookbook_uploader.rb', line 13
def upload_cookbook(cookbook)
Chef::Log.info("Saving #{cookbook.name}")
rest = Chef::REST.new(Chef::Config[:chef_server_url])
validate_cookbook(cookbook)
build_metadata(cookbook)
checksum_files = cookbook.checksums
checksums = checksum_files.inject({}){|memo,elt| memo[elt.first]=nil ; memo}
new_sandbox = rest.post_rest("sandboxes", { :checksums => checksums })
Chef::Log.info("Uploading files")
new_sandbox['checksums'].each do |checksum, info|
if info['needs_upload'] == true
Chef::Log.info("Uploading #{checksum_files[checksum]} (checksum hex = #{checksum}) to #{info['url']}")
checksum64 = Base64.encode64([checksum].pack("H*")).strip
timestamp = Time.now.utc.iso8601
file_contents = File.read(checksum_files[checksum])
sign_obj = Mixlib::Authentication::SignedHeaderAuth.signing_object(
:http_method => :put,
:path => URI.parse(info['url']).path,
:body => file_contents,
:timestamp => timestamp,
:user_id => rest.client_name
)
= { 'content-type' => 'application/x-binary', 'content-md5' => checksum64, :accept => 'application/json' }
.merge!(sign_obj.sign(OpenSSL::PKey::RSA.new(rest.signing_key)))
begin
RestClient::Resource.new(info['url'], :headers=>, :timeout=>1800, :open_timeout=>1800).put(file_contents)
rescue RestClient::Exception => e
Chef::Log.error("Upload failed: #{e.message}\n#{e.response.body}")
raise
end
else
Chef::Log.debug("#{checksum_files[checksum]} has not changed")
end
end
sandbox_url = new_sandbox['uri']
Chef::Log.debug("Committing sandbox")
retries = 0
begin
rest.put_rest(sandbox_url, {:is_completed => true})
rescue Net::HTTPServerException => e
if e.message =~ /^400/ && (retries += 1) <= 5
sleep 2
retry
else
raise
end
end
cookbook.save
Chef::Log.info("Upload complete!")
end
|