Class: Redis_from_S3::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/redis_from_s3/cli.rb

Defined Under Namespace

Classes: Settings

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/redis_from_s3/cli.rb', line 12

def initialize(argv)
  @opts = Trollop::options do
    opt :config, "config file path", :type => :string, :default => '/etc/redis_from_s3/config.yml'
  end

  unless ::File.exists?(::File.expand_path(@opts[:config]))
    puts "Config file #{@opts[:config]} doesn't exist!"
    exit 1
  end

  @settings = Settings.new(@opts[:config])
  Aws.config.update({
                      region: 'us-east-1',
                      credentials: Aws::Credentials.new(@settings.aws.aws_access_key_id, @settings.aws.aws_secret_access_key)
                    })
  @s3 = Aws::S3::Client.new(region: 'us-east-1')

  @file_to_download = ""
end

Instance Method Details

#downloadObject



57
58
59
60
61
62
63
64
# File 'lib/redis_from_s3/cli.rb', line 57

def download
  output = "#{@settings.dirs.temp}/#{@settings.files.output}"
  puts "Download latest Chronicle Redis keys dump from #{@settings.aws.s3_bucket}/#{@file_to_download} to #{output}"

  File.open(output, 'wb') do |file|
    reap = @s3.get_object({ bucket: @settings.aws.s3_bucket, key: @file_to_download }, target: file)
  end
end

#get_file_nameObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/redis_from_s3/cli.rb', line 37

def get_file_name
  begin
    @s3.head_bucket(bucket: @settings.aws.s3_bucket)
  rescue Aws::S3::Errors::NoSuchBucket => e
    msg = "No #{@settings.aws.s3_bucket} Bucket"
  rescue Aws::S3::Errors::Forbidden => e
    msg = "Access Denied to #{@settings.aws.s3_bucket} Bucket"
  rescue Aws::S3::Errors::NotFound => e
    msg = "#{@settings.aws.s3_bucket} Bucket Not Found"
  end
  if msg
    puts msg
    exit 1
  end

  a = []
  @s3.list_objects(bucket: @settings.aws.s3_bucket).contents.each { |c| a << c.key }
  @file_to_download = a.sort.reverse[1]
end

#runObject



32
33
34
35
# File 'lib/redis_from_s3/cli.rb', line 32

def run
  get_file_name
  download
end