debian-mirror-gitlab/lib/gitlab/import_export/shared.rb

127 lines
3.7 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2019-12-21 20:55:43 +05:30
#
# This class encapsulates the directories used by project import/export:
#
# 1. The project export job first generates the project metadata tree
# (e.g. `project.json) and repository bundle (e.g. `project.bundle`)
# inside a temporary `export_path`
# (e.g. /path/to/shared/tmp/project_exports/namespace/project/:randomA/:randomB).
#
# 2. The job then creates a tarball (e.g. `project.tar.gz`) in
# `archive_path` (e.g. /path/to/shared/tmp/project_exports/namespace/project/:randomA).
# CarrierWave moves this tarball files into its permanent location.
#
# 3. Lock files are used to indicate whether a project is in the
# `after_export` state. These are stored in a directory
# (e.g. /path/to/shared/tmp/project_exports/namespace/project/locks. The
# number of lock files present signifies how many concurrent project
# exports are running. Note that this assumes the temporary directory
# is a shared mount:
# https://gitlab.com/gitlab-org/gitlab/issues/32203
#
# NOTE: Stale files should be cleaned up via ImportExportCleanupService.
2016-06-22 15:30:34 +05:30
module Gitlab
module ImportExport
class Shared
2019-12-26 22:10:19 +05:30
attr_reader :errors, :exportable, :logger
2016-06-22 15:30:34 +05:30
2019-12-21 20:55:43 +05:30
LOCKS_DIRECTORY = 'locks'
2019-12-26 22:10:19 +05:30
def initialize(exportable)
@exportable = exportable
@errors = []
@logger = Gitlab::Import::Logger.build
2016-06-22 15:30:34 +05:30
end
2018-03-27 19:54:05 +05:30
def active_export_count
2019-12-21 20:55:43 +05:30
Dir[File.join(base_path, '*')].count { |name| File.basename(name) != LOCKS_DIRECTORY && File.directory?(name) }
2018-03-27 19:54:05 +05:30
end
2019-12-26 22:10:19 +05:30
# The path where the exportable metadata and repository bundle (in case of project) is saved
2016-06-22 15:30:34 +05:30
def export_path
2018-03-17 18:26:18 +05:30
@export_path ||= Gitlab::ImportExport.export_path(relative_path: relative_path)
end
2019-12-21 20:55:43 +05:30
# The path where the tarball is saved
2018-03-17 18:26:18 +05:30
def archive_path
@archive_path ||= Gitlab::ImportExport.export_path(relative_path: relative_archive_path)
2016-06-22 15:30:34 +05:30
end
2019-12-21 20:55:43 +05:30
def base_path
@base_path ||= Gitlab::ImportExport.export_path(relative_path: relative_base_path)
end
def lock_files_path
@locks_files_path ||= File.join(base_path, LOCKS_DIRECTORY)
end
2016-06-22 15:30:34 +05:30
def error(error)
2020-01-01 13:55:28 +05:30
Gitlab::ErrorTracking.track_exception(error, log_base_data)
2019-02-15 15:39:39 +05:30
2019-02-02 18:00:53 +05:30
add_error_message(error.message)
2016-06-22 15:30:34 +05:30
end
2019-02-02 18:00:53 +05:30
def add_error_message(message)
@errors << filtered_error_message(message)
2018-05-09 12:01:36 +05:30
end
def after_export_in_progress?
2019-12-21 20:55:43 +05:30
locks_present?
end
def locks_present?
Dir.exist?(lock_files_path) && !Dir.empty?(lock_files_path)
2018-05-09 12:01:36 +05:30
end
2016-06-22 15:30:34 +05:30
private
2018-03-17 18:26:18 +05:30
def relative_path
2019-12-21 20:55:43 +05:30
@relative_path ||= File.join(relative_archive_path, SecureRandom.hex)
2018-03-17 18:26:18 +05:30
end
def relative_archive_path
2019-12-26 22:10:19 +05:30
@relative_archive_path ||= File.join(relative_base_path, SecureRandom.hex)
2019-12-21 20:55:43 +05:30
end
def relative_base_path
2019-12-26 22:10:19 +05:30
case exportable_type
when 'Project'
@exportable.disk_path
when 'Group'
@exportable.full_path
else
raise Gitlab::ImportExport::Error.new("Unsupported Exportable Type #{@exportable&.class}")
end
2018-03-17 18:26:18 +05:30
end
2019-02-02 18:00:53 +05:30
def log_error(details)
@logger.error(log_base_data.merge(details))
end
def log_debug(details)
@logger.debug(log_base_data.merge(details))
end
def log_base_data
2019-12-26 22:10:19 +05:30
log = {
importer: 'Import/Export',
exportable_id: @exportable&.id,
exportable_path: @exportable&.full_path
2019-02-02 18:00:53 +05:30
}
2019-12-26 22:10:19 +05:30
log[:import_jid] = @exportable&.import_state&.jid if exportable_type == 'Project'
log
2019-02-02 18:00:53 +05:30
end
def filtered_error_message(message)
Projects::ImportErrorFilter.filter_message(message)
2016-06-22 15:30:34 +05:30
end
2019-12-26 22:10:19 +05:30
def exportable_type
@exportable.class.name
end
2016-06-22 15:30:34 +05:30
end
end
end