blob: 73afd276e6c7ec0e1c76e809224ca87f0f75e69c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'rubygems'
require 'fog/aws'
class SyncReports
ACTIONS = %w[get put].freeze
attr_reader :options
def initialize(options)
@options = options
perform_sync!
end
private
def perform_sync!
case options[:action]
when 'get'
get_reports!
when 'put'
put_reports!
end
end
def get_reports!
options[:report_paths].each { |report_path| get_report!(report_path) }
end
def put_reports!
options[:report_paths].each { |report_path| put_report!(report_path) }
end
def get_report!(report_path)
file = bucket.files.get(report_path)
if file.respond_to?(:body)
File.write(report_path, file.body)
puts "#{report_path} was retrieved from S3."
else
puts "#{report_path} does not seem to exist on S3."
end
end
def put_report!(report_path)
bucket.files.create(
key: report_path,
body: File.open(report_path),
public: true
)
puts "#{report_path} was uploaded to S3."
end
def bucket
@bucket ||= storage.directories.get(options[:bucket])
end
def storage
@storage ||=
Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY']
)
end
end
def usage!(error: 'action')
print "\n[ERROR]: "
case error
when 'action'
puts "Please specify an action as first argument: #{SyncReports::ACTIONS.join(', ')}\n\n"
when 'bucket'
puts "Please specify a bucket as second argument!\n\n"
when 'files'
puts "Please specify one or more file paths as third argument!\n\n"
end
puts "Usage: #{__FILE__} [get|put] bucket report_path ...\n\n"
puts "Note: the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment "\
"variables need to be set\n\n"
exit 1
end
if $0 == __FILE__
action = ARGV.shift
usage!(error: 'action') unless SyncReports::ACTIONS.include?(action)
bucket = ARGV.shift
usage!(error: 'bucket') unless bucket
usage!(error: 'files') unless ARGV.any?
SyncReports.new(action: action, bucket: bucket, report_paths: ARGV)
end
|