Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamically install rest-client #36

Merged
merged 2 commits into from
Dec 9, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions libraries/s3_file.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'rest-client'

require 'time'
require 'openssl'
require 'base64'

module S3FileLib
BLOCKSIZE_TO_READ = 1024 * 1000 unless const_defined?(:BLOCKSIZE_TO_READ)
RestClient.proxy = ENV['http_proxy']

def self.build_headers(date, authorization, token)
headers = {
Expand All @@ -24,13 +23,14 @@ def self.get_md5_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key
end

def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
client = self.client
now, auth_string = get_s3_auth("HEAD", bucket,path,aws_access_key_id,aws_secret_access_key, token)

headers = build_headers(now, auth_string, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

response = RestClient.head("#{url}#{path}", headers)
response = client.head("#{url}#{path}", headers)

etag = response.headers[:etag].gsub('"','')
digest = response.headers[:x_amz_meta_digest]
Expand All @@ -39,14 +39,14 @@ def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access
return {"md5" => etag}.merge(digests)
end

def self.get_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
def self.get_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
client = self.client
now, auth_string = get_s3_auth("GET", bucket,path,aws_access_key_id,aws_secret_access_key, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

headers = build_headers(now, auth_string, token)
# response = RestClient.get('https://%s.s3.amazonaws.com%s' % [bucket,path], headers)
response = RestClient::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)
response = client::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)

return response
end
Expand Down Expand Up @@ -122,4 +122,10 @@ def self.verify_md5_checksum(checksum, file)

local_md5.hexdigest == s3_md5
end

def self.client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

require 'rest-client'
RestClient.proxy = ENV['http_proxy']
RestClient
end
end
10 changes: 6 additions & 4 deletions providers/default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'digest/md5'
require 'rest-client'
require 'json'

use_inline_resources

action :create do
rest_client_gem = Chef::Resource::ChefGem.new('rest-client', @run_context)
rest_client_gem.run_action :install
client = S3FileLib::client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

download = true

# handle key specified without leading slash
Expand All @@ -20,12 +22,12 @@
if aws_access_key_id.nil? && aws_secret_access_key.nil? && token.nil?
instance_profile_base_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
begin
instance_profiles = RestClient.get(instance_profile_base_url)
rescue RestClient::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
instance_profiles = client.get(instance_profile_base_url)
rescue client::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
raise ArgumentError.new 'No credentials provided and no instance profile on this machine.'
end
instance_profile_name = instance_profiles.split.first
instance_profile = JSON.load(RestClient.get(instance_profile_base_url + instance_profile_name))
instance_profile = JSON.load(client.get(instance_profile_base_url + instance_profile_name))

aws_access_key_id = instance_profile['AccessKeyId']
aws_secret_access_key = instance_profile['SecretAccessKey']
Expand Down