Sha256: 3709aec700a03e69713070bb29c8ec7be18c2519315d6332f1ea25f05c9ef7b5

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

require 'metaractor'
require 'consul_stockpile/logger'
require 'fog/aws'
require 'fog/local' if ENV['FOG_LOCAL']

module ConsulStockpile
  class DownloadBackup
    include Metaractor

    required :bucket

    def call
      Logger.info "Downloading consul kv backup from bucket #{bucket}"

      directory = nil
      if ENV['FOG_LOCAL']
        Logger.debug 'Using fog local storage'
        storage = Fog::Storage.new provider: 'Local', local_root: '/fog'
        directory = storage.directories.create(key: bucket)
      else
        storage = Fog::Storage.new provider: 'AWS', use_iam_profile: true
        directory = storage.directories.get(bucket)
      end

      body = nil
      last_modified = nil
      directory.files.each do |file|
        next if last_modified != nil && file.last_modified < last_modified
        last_modified = file.last_modified
        body = file.body
      end

      Logger.info 'Download complete.'

      context.json_body = body
    end

    private
    def bucket
      context.bucket
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
consul_stockpile-0.1.5 lib/consul_stockpile/download_backup.rb