Sha256: 430151cf0dec5e7eb861eed60ae36dee3f43fa9f3cc651aafdcb3a2c4b5362c3

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

begin
  require "google/cloud/storage"
rescue LoadError => e
  $stderr.puts "#{e.class}: #{e.message}".color(:red)
  $stderr.puts <<~EOL

    Unable to: require "google/cloud/storage"

    In order to use gcs as a terraspace bundler source,
    please add the google-cloud-storage gem to
    your Terraspace project's Gemfile and run bundle.

    To add the gem to your Gemfile, you can run:

        bundle add google-cloud-storage

    Then download modules in defined your Terrafile with:

        terraspace bundle
  EOL
  exit 1
end

class TerraspaceBundler::Mod::Fetcher
  class Gcs < Base
    extend Memoist

    def run
      bucket, key, path = gcs_info
      download(bucket, key, path)
    end

    def download(bucket_name, key, path)
      # Download to cache area
      bucket = storage.bucket(bucket_name)
      unless bucket
        logger.error "ERROR: bucket #{bucket_name} does not exist".color(:red)
        exit 1
      end
      file = bucket.file(key)
      unless file
        logger.error "ERROR: Unable to find gs://#{bucket_name}/#{key}".color(:red)
        exit 1
      end

      archive = cache_path(path) # temporary path
      logger.debug "Gcs save archive to #{archive}"
      FileUtils.mkdir_p(File.dirname(archive))
      file.download(archive)

      # Save to stage
      dest = stage_path(rel_dest_dir)
      extract(archive, dest)
    end

  private
    def gcs_info
      path = type_path
      path.sub!(%r{storage/v\d+/},'')
      bucket, key = get_bucket_key(path)
      [bucket, key, path]
    end

    def storage
      Google::Cloud::Storage.new
    end
    memoize :storage
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
terraspace-bundler-0.5.0 lib/terraspace_bundler/mod/fetcher/gcs.rb
terraspace-bundler-0.4.4 lib/terraspace_bundler/mod/fetcher/gcs.rb