oneops-admin/lib/shared/cookbooks/artifact/libraries/chef_artifact.rb
# Downloads a file to disk from an Amazon S3 bucket
#
# @param  node [Chef::Node] the node
# @param  source_file [String] a s3 url that represents the artifact in the form: s3://<bucket>/<object-path>
# @param  destination_file [String] a path to download the artifact to
#
# def retrieve_from_s3(node, source_file, destination_file)
# begin
# require 'aws-sdk'
# config = data_bag_config_for(node, DATA_BAG_AWS)
# protocol, bucket_name, object_name = URI.split(source_file).compact
# object_name = object_name[1..-1]
# if config.empty?
# Chef::Log.debug('No AWS Credentials provided, requires ENV variables or an IAM profile')
# s3 = AWS::S3.new()
# else
# Chef::Log.debug("Using AWS Credentials from data_bag #{DATA_BAG}")
# s3 = AWS::S3.new(
# :access_key_id     => config['access_key_id'],
# :secret_access_key => config['secret_access_key'])
# end
# 
# bucket = s3.buckets[bucket_name]
# raise S3BucketNotFoundError.new(bucket_name) unless bucket.exists?
# 
# object = bucket.objects[object_name]
# raise S3ArtifactNotFoundError.new(bucket_name, object_name) unless object.exists?
# 
# Chef::Log.debug("Downloading #{object_name} from S3 bucket #{bucket_name}")
# ::File.open(destination_file, 'w') do |file|
# object.read do |chunk|
# file.write(chunk)
# end
# Chef::Log.debug("File #{destination_file} is #{file.size} bytes on disk")
# end
# rescue URI::InvalidURIError
# Chef::Log.warn("Expected an S3 URL but found #{source_file}")
# raise
# end
# end
Downloads a file to disk from an Amazon S3 bucket
Downloads a file to disk from an Amazon S3 bucket
def retrieve_from_s3(node, source_file, destination_file)
        begin
          require 'fog'
          config = data_bag_config_for(node, DATA_BAG_AWS)
          protocol, bucket_name, object_name = URI.split(source_file).compact
          object_name = object_name[1..-1]
          if config.empty?
            Chef::Log.debug('No AWS Credentials provided')
            raise
          else
            Chef::Log.debug("Using AWS Credentials from data_bag #{DATA_BAG}")
            s3 = Fog::Storage.new({
                :provider => 'AWS',
                :aws_access_key_id        => config['access_key_id'],
                :aws_secret_access_key    => config['secret_access_key']
              })
          end
          Chef::Log.debug("Downloading #{object_name} from S3 bucket #{bucket_name}")
          object = s3.get_object(bucket_name,object_name)
          raise S3ArtifactNotFoundError.new(bucket_name, object_name) unless object
          Chef::Log.debug("Headers: #{object.headers.inspect}")
          ::File.open(destination_file, 'w') do |file|
            file.write(object.body)
          end
        rescue URI::InvalidURIError
          Chef::Log.warn("Expected an S3 URL but found #{source_file}")
          raise
        end
      end
def retrieve_from_s3 ( node , source_file , destination_file ) begin require 'fog' config = data_bag_config_for ( node , DATA_BAG_AWS ) protocol , bucket_name , object_name = URI . split ( source_file ) . compact object_name = object_name [ 1 .. - 1 ] if config . empty? Chef :: Log . debug ( 'No AWS Credentials provided' ) raise else Chef :: Log . debug ( "Using AWS Credentials from data_bag #{DATA_BAG}" ) s3 = Fog :: Storage . new ( { :provider => 'AWS' , :aws_access_key_id => config [ 'access_key_id' ] , :aws_secret_access_key => config [ 'secret_access_key' ] } ) end Chef :: Log . debug ( "Downloading #{object_name} from S3 bucket #{bucket_name}" ) object = s3 . get_object ( bucket_name , object_name ) raise S3ArtifactNotFoundError . new ( bucket_name , object_name ) unless object Chef :: Log . debug ( "Headers: #{object.headers.inspect}" ) :: File . open ( destination_file , 'w' ) do | file | file . write ( object . body ) end rescue URI :: InvalidURIError Chef :: Log . warn ( "Expected an S3 URL but found #{source_file}" ) raise end end
Downloads a file to disk from an Amazon S3 bucket
Downloads a file to disk from an Amazon S3 bucket
node
None
source_file
a s3 url that represents the artifact in the form: s3:///
a s3 url that represents the artifact in the form : s3 : ///
destination_file
a path to download the artifact to def retrieve_from_s3(node, source_file, destination_file) begin require 'aws-sdk' config = data_bag_config_for(node, DATA_BAG_AWS) protocol, bucket_name, object_name = URI.split(source_file).compact object_name = object_name[1..-1] if config.empty. Chef::Log.debug('No AWS Credentials provided, requires ENV variables or an IAM profile') s3 = AWS::S3.new() else Chef::Log.debug("Using AWS Credentials from data_bag #{DATA_BAG}") s3 = AWS::S3.new( :access_key_id => config['access_key_id'], :secret_access_key => config['secret_access_key']) end bucket = s3.buckets[bucket_name] raise S3BucketNotFoundError.new(bucket_name) unless bucket.exists. object = bucket.objects[object_name] raise S3ArtifactNotFoundError.new(bucket_name, object_name) unless object.exists. Chef::Log.debug("Downloading #{object_name} from S3 bucket #{bucket_name}") ::File.open(destination_file, 'w') do |file| object.read do |chunk| file.write(chunk) end Chef::Log.debug("File #{destination_file} is #{file.size} bytes on disk") end rescue URI::InvalidURIError Chef::Log.warn("Expected an S3 URL but found #{source_file}") raise end end
a path to download the artifact to def retrieve_from_s3 ( node source_file destination_file ) begin require ' aws - sdk ' config = data_bag_config_for ( node DATA_BAG_AWS ) protocol bucket_name object_name = URI . split ( source_file ) . compact object_name = object_name [ 1 .. - 1 ] if config . empty . Chef :: Log . debug ( ' No AWS Credentials provided requires ENV variables or an IAM profile ' ) s3 = AWS :: S3 . new () else Chef :: Log . debug ( " Using AWS Credentials from data_bag # { DATA_BAG } " ) s3 = AWS :: S3 . new ( : access_key_id = > config [ ' access_key_id ' ] : secret_access_key = > config [ ' secret_access_key ' ] ) end bucket = s3 . buckets [ bucket_name ] raise S3BucketNotFoundError . new ( bucket_name ) unless bucket . exists . object = bucket . objects [ object_name ] raise S3ArtifactNotFoundError . new ( bucket_name object_name ) unless object . exists . Chef :: Log . debug ( " Downloading # { object_name } from S3 bucket # { bucket_name } " ) :: File . open ( destination_file ' w ' ) do |file| object . read do |chunk| file . write ( chunk ) end Chef :: Log . debug ( " File # { destination_file } is # { file . size } bytes on disk " ) end rescue URI :: InvalidURIError Chef :: Log . warn ( " Expected an S3 URL but found # { source_file } " ) raise end end