Sha256: e81e838eab794ef42c935dd86ff6ee94b1a5fbf3bd99843d1c6dddb1496b3f2a

Contents?: true

Size: 1.72 KB

Versions: 1

Compression:

Stored size: 1.72 KB

Contents

module Heirloom

  class ArtifactDownloader

    def initialize(args)
      @config = args[:config]
      @logger = args[:logger]
    end

    def download(args)
      @id = args[:id]
      @name = args[:name]
      @output = args[:output]
      @region = args[:region]

      s3_downloader = Downloader::S3.new :config => @config,
                                         :logger => @logger,
                                         :region => @region

      bucket = artifact_reader.get_bucket :region => @region,
                                          :name   => @name,
                                          :id     => @id

      key = artifact_reader.get_key :region => @region,
                                    :name   => @name,
                                    :id     => @id

      @logger.info "Downloading s3://#{bucket}/#{key} from #{@region}."

      file = s3_downloader.download_file :bucket => bucket,
                                         :key    => key

      @logger.info "Writing file to #{@output}."

      File.open(@output, 'w') do |local_file|
        local_file.write file
      end
    end

    private

    def get_bucket
      artifact = artifact_reader.show :name => @name,
                                      :id   => @id

      url = artifact["#{@region}-s3-url"].first

      bucket = url.gsub('s3://', '').split('/').first
    end

    def get_key
      artifact = artifact_reader.show :name => @name,
                                      :id   => @id

      url = artifact["#{@region}-s3-url"].first

      bucket = url.gsub('s3://', '').gsub(get_bucket, '')
      bucket.slice!(0)
      bucket
    end

    def artifact_reader
      @artifact_reader ||= ArtifactReader.new :config => @config
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
heirloom-0.1.3 lib/heirloom/artifact/artifact_downloader.rb