2020-12-21 18:10:26 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Pages
|
|
|
|
class MigrateLegacyStorageToDeploymentService
|
2020-12-23 06:10:22 +00:00
|
|
|
ExclusiveLeaseTakenError = Class.new(StandardError)
|
2020-12-21 18:10:26 +00:00
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
include BaseServiceUtility
|
2020-12-21 18:10:26 +00:00
|
|
|
include ::Pages::LegacyStorageLease
|
|
|
|
|
|
|
|
attr_reader :project
|
|
|
|
|
2021-02-11 21:09:00 +00:00
|
|
|
def initialize(project, ignore_invalid_entries: false)
|
2020-12-21 18:10:26 +00:00
|
|
|
@project = project
|
2021-02-11 21:09:00 +00:00
|
|
|
@ignore_invalid_entries = ignore_invalid_entries
|
2020-12-21 18:10:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2021-01-12 18:11:03 +00:00
|
|
|
result = try_obtain_lease do
|
2020-12-21 18:10:26 +00:00
|
|
|
execute_unsafe
|
|
|
|
end
|
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
raise ExclusiveLeaseTakenError, "Can't migrate pages for project #{project.id}: exclusive lease taken" if result.nil?
|
|
|
|
|
|
|
|
result
|
2020-12-21 18:10:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_unsafe
|
2021-02-11 21:09:00 +00:00
|
|
|
zip_result = ::Pages::ZipDirectoryService.new(project.pages_path, ignore_invalid_entries: @ignore_invalid_entries).execute
|
2021-01-12 18:11:03 +00:00
|
|
|
|
|
|
|
if zip_result[:status] == :error
|
|
|
|
if !project.pages_metadatum&.reload&.pages_deployment &&
|
|
|
|
Feature.enabled?(:pages_migration_mark_as_not_deployed, project)
|
|
|
|
project.mark_pages_as_not_deployed
|
|
|
|
end
|
|
|
|
|
|
|
|
return error("Can't create zip archive: #{zip_result[:message]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
archive_path = zip_result[:archive_path]
|
2020-12-21 18:10:26 +00:00
|
|
|
|
|
|
|
deployment = nil
|
|
|
|
File.open(archive_path) do |file|
|
|
|
|
deployment = project.pages_deployments.create!(
|
|
|
|
file: file,
|
2021-01-12 18:11:03 +00:00
|
|
|
file_count: zip_result[:entries_count],
|
2020-12-21 18:10:26 +00:00
|
|
|
file_sha256: Digest::SHA256.file(archive_path).hexdigest
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
project.set_first_pages_deployment!(deployment)
|
|
|
|
|
2021-01-12 18:11:03 +00:00
|
|
|
success
|
2020-12-21 18:10:26 +00:00
|
|
|
ensure
|
|
|
|
FileUtils.rm_f(archive_path) if archive_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|