2018-11-17 00:37:17 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-14 14:57:25 +00:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Saver
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
|
|
|
def self.save(*args)
|
|
|
|
new(*args).save
|
|
|
|
end
|
|
|
|
|
2019-11-11 21:06:20 +00:00
|
|
|
def initialize(exportable:, shared:)
|
|
|
|
@exportable = exportable
|
2020-05-25 15:07:58 +00:00
|
|
|
@shared = shared
|
2016-04-14 14:57:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if compress_and_save
|
2020-05-25 15:07:58 +00:00
|
|
|
Gitlab::Export::Logger.info(
|
|
|
|
message: 'Export archive saved',
|
|
|
|
exportable_class: @exportable.class.to_s,
|
|
|
|
archive_file: archive_file
|
|
|
|
)
|
2018-06-25 13:10:26 +00:00
|
|
|
|
2018-09-07 11:42:49 +00:00
|
|
|
save_upload
|
2016-04-14 14:57:25 +00:00
|
|
|
else
|
2018-06-25 13:10:26 +00:00
|
|
|
@shared.error(Gitlab::ImportExport::Error.new(error_message))
|
2016-04-14 14:57:25 +00:00
|
|
|
false
|
|
|
|
end
|
2016-05-10 15:15:20 +00:00
|
|
|
rescue => e
|
2016-05-13 10:33:13 +00:00
|
|
|
@shared.error(e)
|
2016-05-10 15:15:20 +00:00
|
|
|
false
|
2018-06-25 13:10:26 +00:00
|
|
|
ensure
|
2020-05-26 15:08:17 +00:00
|
|
|
remove_base_tmp_dir
|
2016-04-14 14:57:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def compress_and_save
|
2016-05-11 12:51:25 +00:00
|
|
|
tar_czf(archive: archive_file, dir: @shared.export_path)
|
2016-04-14 14:57:25 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 15:08:17 +00:00
|
|
|
def remove_base_tmp_dir
|
|
|
|
FileUtils.rm_rf(@shared.base_path)
|
2018-06-25 13:10:26 +00:00
|
|
|
end
|
|
|
|
|
2016-04-14 14:57:25 +00:00
|
|
|
def archive_file
|
2019-11-11 21:06:20 +00:00
|
|
|
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @exportable))
|
2016-04-14 14:57:25 +00:00
|
|
|
end
|
2018-06-25 13:10:26 +00:00
|
|
|
|
2018-09-07 11:42:49 +00:00
|
|
|
def save_upload
|
2019-11-11 21:06:20 +00:00
|
|
|
upload = initialize_upload
|
2018-06-25 13:10:26 +00:00
|
|
|
|
|
|
|
File.open(archive_file) { |file| upload.export_file = file }
|
|
|
|
|
|
|
|
upload.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2018-08-29 13:41:56 +00:00
|
|
|
"Unable to save #{archive_file} into #{@shared.export_path}."
|
2018-06-25 13:10:26 +00:00
|
|
|
end
|
2019-11-11 21:06:20 +00:00
|
|
|
|
|
|
|
def initialize_upload
|
|
|
|
exportable_kind = @exportable.class.name.downcase
|
|
|
|
|
|
|
|
ImportExportUpload.find_or_initialize_by(Hash[exportable_kind, @exportable])
|
|
|
|
end
|
2016-04-14 14:57:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|