2018-02-27 14:15:25 -05:00
|
|
|
module Gitlab
|
|
|
|
module Verify
|
|
|
|
class BatchVerifier
|
|
|
|
attr_reader :batch_size, :start, :finish
|
|
|
|
|
|
|
|
def initialize(batch_size:, start: nil, finish: nil)
|
|
|
|
@batch_size = batch_size
|
|
|
|
@start = start
|
|
|
|
@finish = finish
|
2018-06-13 13:11:43 -04:00
|
|
|
|
|
|
|
fix_google_api_logger
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Yields a Range of IDs and a Hash of failed verifications (object => error)
|
|
|
|
def run_batches(&blk)
|
2018-06-13 13:11:43 -04:00
|
|
|
all_relation.in_batches(of: batch_size, start: start, finish: finish) do |batch| # rubocop: disable Cop/InBatches
|
|
|
|
range = batch.first.id..batch.last.id
|
|
|
|
failures = run_batch_for(batch)
|
2018-02-27 14:15:25 -05:00
|
|
|
|
|
|
|
yield(range, failures)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def name
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def describe(_object)
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
def run_batch_for(batch)
|
|
|
|
batch.map { |upload| verify(upload) }.compact.to_h
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def verify(object)
|
2018-06-13 13:11:43 -04:00
|
|
|
local?(object) ? verify_local(object) : verify_remote(object)
|
|
|
|
rescue => err
|
|
|
|
failure(object, err.inspect)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_local(object)
|
2018-02-27 14:15:25 -05:00
|
|
|
expected = expected_checksum(object)
|
|
|
|
actual = actual_checksum(object)
|
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
return failure(object, 'Checksum missing') unless expected.present?
|
|
|
|
return failure(object, 'Checksum mismatch') unless expected == actual
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
2018-02-27 14:15:25 -05:00
|
|
|
|
2018-06-13 13:11:43 -04:00
|
|
|
# We don't calculate checksum for remote objects, so just check existence
|
|
|
|
def verify_remote(object)
|
|
|
|
return failure(object, 'Remote object does not exist') unless remote_object_exists?(object)
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
def success
|
2018-02-27 14:15:25 -05:00
|
|
|
nil
|
2018-06-13 13:11:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def failure(object, message)
|
|
|
|
[object, message]
|
|
|
|
end
|
|
|
|
|
|
|
|
# It's already set to Logger::INFO, but acts as if it is set to
|
|
|
|
# Logger::DEBUG, and this fixes it...
|
|
|
|
def fix_google_api_logger
|
|
|
|
if Object.const_defined?('Google::Apis')
|
|
|
|
Google::Apis.logger.level = Logger::INFO
|
|
|
|
end
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This should return an ActiveRecord::Relation suitable for calling #in_batches on
|
2018-06-13 13:11:43 -04:00
|
|
|
def all_relation
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Should return true if the object is stored locally
|
|
|
|
def local?(_object)
|
2018-02-27 14:15:25 -05:00
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# The checksum we expect the object to have
|
|
|
|
def expected_checksum(_object)
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# The freshly-recalculated checksum of the object
|
|
|
|
def actual_checksum(_object)
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
2018-06-13 13:11:43 -04:00
|
|
|
|
|
|
|
# Be sure to perform a hard check of the remote object (don't just check DB value)
|
|
|
|
def remote_object_exists?(object)
|
|
|
|
raise NotImplementedError.new
|
|
|
|
end
|
2018-02-27 14:15:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|