2018-11-16 19:37:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-14 10:57:25 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Saver
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
2020-10-15 14:08:43 -04:00
|
|
|
def self.save(*args, **kwargs)
|
|
|
|
new(*args, **kwargs).save
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
2019-11-11 16:06:20 -05:00
|
|
|
def initialize(exportable:, shared:)
|
|
|
|
@exportable = exportable
|
2020-05-25 11:07:58 -04:00
|
|
|
@shared = shared
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if compress_and_save
|
2022-01-25 13:11:55 -05:00
|
|
|
log_export_results('Export archive saved')
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2018-09-07 07:42:49 -04:00
|
|
|
save_upload
|
2022-01-25 13:11:55 -05:00
|
|
|
|
|
|
|
log_export_results('Export archive uploaded')
|
2016-04-14 10:57:25 -04:00
|
|
|
else
|
2018-06-25 09:10:26 -04:00
|
|
|
@shared.error(Gitlab::ImportExport::Error.new(error_message))
|
2022-01-25 13:11:55 -05:00
|
|
|
|
2016-04-14 10:57:25 -04:00
|
|
|
false
|
|
|
|
end
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2016-05-13 06:33:13 -04:00
|
|
|
@shared.error(e)
|
2022-01-25 13:11:55 -05:00
|
|
|
log_export_results('Export archive saver failed')
|
|
|
|
|
2016-05-10 11:15:20 -04:00
|
|
|
false
|
2018-06-25 09:10:26 -04:00
|
|
|
ensure
|
2021-01-26 07:09:27 -05:00
|
|
|
remove_archive_tmp_dir
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-01-25 13:11:55 -05:00
|
|
|
attr_accessor :compress_duration_s, :assign_duration_s, :upload_duration_s, :upload_bytes
|
|
|
|
|
2016-04-14 10:57:25 -04:00
|
|
|
def compress_and_save
|
2022-01-25 13:11:55 -05:00
|
|
|
result = nil
|
|
|
|
|
|
|
|
@compress_duration_s = Benchmark.realtime do
|
|
|
|
result = tar_czf(archive: archive_file, dir: @shared.export_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
2021-01-26 07:09:27 -05:00
|
|
|
def remove_archive_tmp_dir
|
|
|
|
FileUtils.rm_rf(@shared.archive_path)
|
2018-06-25 09:10:26 -04:00
|
|
|
end
|
|
|
|
|
2016-04-14 10:57:25 -04:00
|
|
|
def archive_file
|
2019-11-11 16:06:20 -05:00
|
|
|
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @exportable))
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2018-09-07 07:42:49 -04:00
|
|
|
def save_upload
|
2019-11-11 16:06:20 -05:00
|
|
|
upload = initialize_upload
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2022-01-25 13:11:55 -05:00
|
|
|
@upload_bytes = File.size(archive_file)
|
|
|
|
@assign_duration_s = Benchmark.realtime do
|
|
|
|
File.open(archive_file) { |file| upload.export_file = file }
|
|
|
|
end
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2022-01-25 13:11:55 -05:00
|
|
|
@upload_duration_s = Benchmark.realtime { upload.save! }
|
2022-01-24 10:11:33 -05:00
|
|
|
|
|
|
|
true
|
2018-06-25 09:10:26 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2018-08-29 09:41:56 -04:00
|
|
|
"Unable to save #{archive_file} into #{@shared.export_path}."
|
2018-06-25 09:10:26 -04:00
|
|
|
end
|
2019-11-11 16:06:20 -05:00
|
|
|
|
|
|
|
def initialize_upload
|
|
|
|
exportable_kind = @exportable.class.name.downcase
|
|
|
|
|
|
|
|
ImportExportUpload.find_or_initialize_by(Hash[exportable_kind, @exportable])
|
|
|
|
end
|
2022-01-25 13:11:55 -05:00
|
|
|
|
|
|
|
def log_export_results(message)
|
|
|
|
Gitlab::Export::Logger.info(message: message, **log_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_data
|
|
|
|
ApplicationContext.current.merge(
|
|
|
|
{
|
|
|
|
exportable_class: @exportable.class.to_s,
|
|
|
|
archive_file: archive_file,
|
|
|
|
compress_duration_s: compress_duration_s&.round(6),
|
|
|
|
assign_duration_s: assign_duration_s&.round(6),
|
|
|
|
upload_duration_s: upload_duration_s&.round(6),
|
|
|
|
upload_bytes: upload_bytes
|
|
|
|
}
|
|
|
|
).compact
|
|
|
|
end
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|