2016-04-14 10:57:25 -04:00
|
|
|
module Gitlab
|
|
|
|
module ImportExport
|
|
|
|
class Saver
|
|
|
|
include Gitlab::ImportExport::CommandLineUtil
|
|
|
|
|
|
|
|
def self.save(*args)
|
|
|
|
new(*args).save
|
|
|
|
end
|
|
|
|
|
2016-07-15 06:47:06 -04:00
|
|
|
def initialize(project:, shared:)
|
|
|
|
@project = project
|
2016-05-10 11:15:20 -04:00
|
|
|
@shared = shared
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
if compress_and_save
|
2016-05-11 08:51:25 -04:00
|
|
|
remove_export_path
|
2018-06-25 09:10:26 -04:00
|
|
|
|
2016-05-03 05:13:10 -04:00
|
|
|
Rails.logger.info("Saved project export #{archive_file}")
|
2018-06-25 09:10:26 -04:00
|
|
|
|
|
|
|
save_on_object_storage if use_object_storage?
|
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))
|
2016-04-14 10:57:25 -04:00
|
|
|
false
|
|
|
|
end
|
2016-05-10 11:15:20 -04:00
|
|
|
rescue => e
|
2016-05-13 06:33:13 -04:00
|
|
|
@shared.error(e)
|
2016-05-10 11:15:20 -04:00
|
|
|
false
|
2018-06-25 09:10:26 -04:00
|
|
|
ensure
|
|
|
|
if use_object_storage?
|
|
|
|
remove_archive
|
|
|
|
remove_export_path
|
|
|
|
end
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def compress_and_save
|
2016-05-11 08:51:25 -04:00
|
|
|
tar_czf(archive: archive_file, dir: @shared.export_path)
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
2016-05-11 08:51:25 -04:00
|
|
|
def remove_export_path
|
|
|
|
FileUtils.rm_rf(@shared.export_path)
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
|
2018-06-25 09:10:26 -04:00
|
|
|
def remove_archive
|
|
|
|
FileUtils.rm_rf(@shared.archive_path)
|
|
|
|
end
|
|
|
|
|
2016-04-14 10:57:25 -04:00
|
|
|
def archive_file
|
2018-01-08 10:42:41 -05:00
|
|
|
@archive_file ||= File.join(@shared.archive_path, Gitlab::ImportExport.export_filename(project: @project))
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
2018-06-25 09:10:26 -04:00
|
|
|
|
|
|
|
def save_on_object_storage
|
|
|
|
upload = ImportExportUpload.find_or_initialize_by(project: @project)
|
|
|
|
|
|
|
|
File.open(archive_file) { |file| upload.export_file = file }
|
|
|
|
|
|
|
|
upload.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def use_object_storage?
|
|
|
|
Gitlab::ImportExport.object_storage?
|
|
|
|
end
|
|
|
|
|
|
|
|
def error_message
|
|
|
|
"Unable to save #{archive_file} into #{@shared.export_path}. Object storage enabled: #{use_object_storage?}"
|
|
|
|
end
|
2016-04-14 10:57:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|