Skip to content

Instantly share code, notes, and snippets.

@fujin
Created October 30, 2010 02:09
Show Gist options
  • Save fujin/654808 to your computer and use it in GitHub Desktop.
Save fujin/654808 to your computer and use it in GitHub Desktop.
this one gets monkeypatched into a library. I called mine cookbooks/libsonian/libraries/signed_url.rb & chef.rb
class Chef
class Provider
class RemoteS3File < Chef::Provider::RemoteFile
def load_current_resource
super
%w{bucket object_name aws_access_key_id aws_secret_access_key}.map do |attribute|
Chef::Application.fatal! "remote_s3_file: required attr: #{attribute} is nil", -92 if
@new_resource.send(attribute.to_sym).nil?
end
@new_resource.source( LibSonian.signed_url(@new_resource.bucket,
@new_resource.object_name,
@new_resource.aws_access_key_id,
@new_resource.aws_secret_access_key)
)
end
end
end
class Resource
class RemoteS3File < Chef::Resource::RemoteFile
def provider
Chef::Provider::RemoteS3File
end
def initialize(name, run_context=nil)
super
@resource_name = :remote_s3_file
@cookbook = nil
Chef::Log.info "remote_s3_file: initializing #{name}"
end
def bucket(args=nil)
set_or_return(
:bucket,
args,
:kind_of => String
)
end
def object_name(args=nil)
set_or_return(
:object_name,
args,
:kind_of => String
)
end
def aws_access_key_id(args=nil)
set_or_return(
:aws_access_key_id,
args,
:kind_of => String
)
end
def aws_secret_access_key(args=nil)
set_or_return(
:aws_secret_access_key,
args,
:kind_of => String
)
end
end
end
end
Chef::Platform.platforms[:default].merge! :remote_s3_file => Chef::Provider::RemoteS3File
include_recipe "aws"
include_recipe "libsonian" # you'll need both of these as deps
aws = data_bag_item("aws", "some credz for s3 yo")
remote_s3_file "/usr/src/remote_s3_file.tbz" do
bucket "foo"
object_name "bar/baz/bong.tbz"
aws_access_key_id aws["aws_access_key_id"]
aws_secret_access_key aws["aws_secret_access_key"]
mode "00644"
end
module LibSonian
def self.signed_url(bucket,
object_name,
aws_access_key_id,
aws_secret_access_key)
begin
Gem.clear_paths
require 'fog'
rescue LoadError
Chef::Log.warn("Missing gem 'fog'")
end
@@s3 ||= ::Fog::AWS::Storage.new(:aws_access_key_id => aws_access_key_id,
:aws_secret_access_key => aws_secret_access_key)
url = @@s3.directories.get(bucket).files.get_url(object_name, Time.now + (60 * 10))
Chef::Log.info "signed_url[#{bucket}/#{object_name}]: generated signed 10 minute S3 url #{url}"
url
end
end
@tnine
Copy link

tnine commented Nov 11, 2010

I needed this exact functionality. I created an actual cookbook that installs fog and it's dependencies. I've only tested it on Ubuntu 10.0.4

http://cookbooks.opscode.com/cookbooks/awsclient

@fujin
Copy link
Author

fujin commented Nov 11, 2010

dope

@tnine
Copy link

tnine commented Nov 11, 2010

Thanks for posting this man. I would have been lost without it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment