Commit f64294cc authored by Lin Jen-Shin's avatar Lin Jen-Shin

Merge branch...

Merge branch '329870-get-rid-of-the-scripts-sync-reports-file-and-associated-env-variables' into 'master'

Resolve "Get rid of the `scripts/sync-reports` file and associated env variables"

See merge request gitlab-org/gitlab!60929
parents ef8ea4da a93a9ca6
#!/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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment