debian-mirror-gitlab/lib/gitlab/import_export/after_export_strategies/base_after_export_strategy.rb

115 lines
2.6 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2018-05-09 12:01:36 +05:30
module Gitlab
module ImportExport
module AfterExportStrategies
class BaseAfterExportStrategy
2018-11-08 19:23:39 +05:30
extend Gitlab::ImportExport::CommandLineUtil
2018-05-09 12:01:36 +05:30
include ActiveModel::Validations
extend Forwardable
StrategyError = Class.new(StandardError)
private
2019-12-21 20:55:43 +05:30
attr_reader :project, :current_user, :lock_file
2018-05-09 12:01:36 +05:30
public
def initialize(attributes = {})
@options = OpenStruct.new(attributes)
self.class.instance_eval do
def_delegators :@options, *attributes.keys
end
end
def execute(current_user, project)
@project = project
2018-11-08 19:23:39 +05:30
2019-12-21 20:55:43 +05:30
ensure_export_ready!
ensure_lock_files_path!
@lock_file = File.join(lock_files_path, SecureRandom.hex)
2018-05-09 12:01:36 +05:30
@current_user = current_user
if invalid?
log_validation_errors
return
end
create_or_update_after_export_lock
strategy_execute
true
2021-06-08 01:23:25 +05:30
rescue StandardError => e
2018-05-09 12:01:36 +05:30
project.import_export_shared.error(e)
false
ensure
delete_after_export_lock
2019-12-21 20:55:43 +05:30
delete_export_file
delete_archive_path
2018-05-09 12:01:36 +05:30
end
def to_json(options = {})
@options.to_h.merge!(klass: self.class.name).to_json
end
2019-12-21 20:55:43 +05:30
def ensure_export_ready!
raise StrategyError unless project.export_file_exists?
end
def ensure_lock_files_path!
FileUtils.mkdir_p(lock_files_path) unless Dir.exist?(lock_files_path)
end
def lock_files_path
project.import_export_shared.lock_files_path
end
2018-05-09 12:01:36 +05:30
2019-12-21 20:55:43 +05:30
def archive_path
project.import_export_shared.archive_path
end
2018-11-08 19:23:39 +05:30
2019-12-21 20:55:43 +05:30
def locks_present?
project.import_export_shared.locks_present?
2018-05-09 12:01:36 +05:30
end
protected
def strategy_execute
raise NotImplementedError
end
2019-12-21 20:55:43 +05:30
def delete_export?
true
end
2018-05-09 12:01:36 +05:30
private
2019-12-21 20:55:43 +05:30
def delete_export_file
return if locks_present? || !delete_export?
project.remove_exports
end
def delete_archive_path
FileUtils.rm_rf(archive_path) if File.directory?(archive_path)
end
2018-05-09 12:01:36 +05:30
def create_or_update_after_export_lock
2019-12-21 20:55:43 +05:30
FileUtils.touch(lock_file)
2018-05-09 12:01:36 +05:30
end
def delete_after_export_lock
FileUtils.rm(lock_file) if lock_file.present? && File.exist?(lock_file)
end
def log_validation_errors
errors.full_messages.each { |msg| project.import_export_shared.add_error_message(msg) }
end
end
end
end
end