2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Saver
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def self.save(*args, **kwargs)
|
|
|
|
new(*args, **kwargs).save
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
2019-12-26 22:10:19 +05:30
|
|
|
def initialize(exportable:, shared:)
|
|
|
|
@exportable = exportable
|
2020-06-23 00:09:42 +05:30
|
|
|
@shared = shared
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if compress_and_save
|
2020-06-23 00:09:42 +05:30
|
|
|
Gitlab::Export::Logger.info(
|
|
|
|
message: 'Export archive saved',
|
|
|
|
exportable_class: @exportable.class.to_s,
|
|
|
|
archive_file: archive_file
|
|
|
|
)
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
save_upload
|
2016-06-22 15:30:34 +05:30
|
|
|
else
|
2018-11-08 19:23:39 +05:30
|
|
|
@shared.error(Gitlab::ImportExport::Error.new(error_message))
|
2016-06-22 15:30:34 +05:30
|
|
|
false
|
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2016-06-22 15:30:34 +05:30
|
|
|
@shared.error(e)
|
|
|
|
false
|
2018-11-08 19:23:39 +05:30
|
|
|
ensure
|
2021-03-11 19:13:27 +05:30
|
|
|
remove_archive_tmp_dir
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def compress_and_save
|
|
|
|
tar_czf(archive: archive_file, dir: @shared.export_path)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def remove_archive_tmp_dir
|
|
|
|
FileUtils.rm_rf(@shared.archive_path)
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
2016-06-22 15:30:34 +05:30
|
|
|
def archive_file
|
2019-12-26 22:10:19 +05:30
|
|
|
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(exportable: @exportable))
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
def save_upload
|
2019-12-26 22:10:19 +05:30
|
|
|
upload = initialize_upload
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
File.open(archive_file) { |file| upload.export_file = file }
|
|
|
|
|
|
|
|
upload.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
2018-11-20 20:47:30 +05:30
|
|
|
"Unable to save #{archive_file} into #{@shared.export_path}."
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def initialize_upload
|
|
|
|
exportable_kind = @exportable.class.name.downcase
|
|
|
|
|
|
|
|
ImportExportUpload.find_or_initialize_by(Hash[exportable_kind, @exportable])
|
|
|
|
end
|
2016-06-22 15:30:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|