class Chef::Knife::S3Source

Attributes

config[RW]
url[RW]

Public Class Methods

fetch(url, config) click to toggle source
# File lib/chef/knife/helpers/s3_source.rb, line 22
def self.fetch(url, config)
  source = Chef::Knife::S3Source.new
  source.url = url
  source.config = config
  source.body
end

Public Instance Methods

body() click to toggle source
# File lib/chef/knife/helpers/s3_source.rb, line 29
def body
  bucket_obj.body.string
end

Private Instance Methods

bucket() click to toggle source

@return [URI]

# File lib/chef/knife/helpers/s3_source.rb, line 43
def bucket
  uri = URI(@url)
  if uri.scheme == "s3"
    URI(@url).host
  else
    URI(@url).path.split("/")[1]
  end
end
bucket_obj() click to toggle source
# File lib/chef/knife/helpers/s3_source.rb, line 35
def bucket_obj
  s3_connection.get_object({
    bucket: bucket,
    key: path,
  })
end
connection_string() click to toggle source
# File lib/chef/knife/helpers/s3_source.rb, line 62
def connection_string
  conn = {}
  conn[:region] = config[:region]
  conn[:credentials] =
    if config[:use_iam_profile]
      Aws::InstanceProfileCredentials.new
    else
      Aws::Credentials.new(
        config[:aws_access_key_id],
        config[:aws_secret_access_key],
        config[:aws_session_token]
      )
    end
  conn
end
path() click to toggle source

@return [URI]

# File lib/chef/knife/helpers/s3_source.rb, line 53
def path
  uri = URI(@url)
  if uri.scheme == "s3"
    URI(@url).path.sub(%r{^/}, "")
  else
    URI(@url).path.split(bucket).last.sub(%r{^/}, "")
  end
end
s3_connection() click to toggle source

@return [Aws::S3::Client]

# File lib/chef/knife/helpers/s3_source.rb, line 79
def s3_connection
  @s3_connection ||= begin
    require "aws-sdk-s3" # lazy load the aws sdk to speed up the knife run
    Aws::S3::Client.new(connection_string)
  end
end