2021-03-08 18:12:59 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Pages
|
|
|
|
class MigrateLegacyStorageToDeploymentService
|
|
|
|
ExclusiveLeaseTakenError = Class.new(StandardError)
|
|
|
|
|
|
|
|
include BaseServiceUtility
|
|
|
|
include ::Pages::LegacyStorageLease
|
|
|
|
|
|
|
|
attr_reader :project
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
def initialize(project, ignore_invalid_entries: false, mark_projects_as_not_deployed: false)
|
2021-03-08 18:12:59 +05:30
|
|
|
@project = project
|
2021-03-11 19:13:27 +05:30
|
|
|
@ignore_invalid_entries = ignore_invalid_entries
|
2021-04-29 21:17:54 +05:30
|
|
|
@mark_projects_as_not_deployed = mark_projects_as_not_deployed
|
2021-03-08 18:12:59 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
result = try_obtain_lease do
|
|
|
|
execute_unsafe
|
|
|
|
end
|
|
|
|
|
|
|
|
raise ExclusiveLeaseTakenError, "Can't migrate pages for project #{project.id}: exclusive lease taken" if result.nil?
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def execute_unsafe
|
2021-03-11 19:13:27 +05:30
|
|
|
zip_result = ::Pages::ZipDirectoryService.new(project.pages_path, ignore_invalid_entries: @ignore_invalid_entries).execute
|
2021-03-08 18:12:59 +05:30
|
|
|
|
|
|
|
if zip_result[:status] == :error
|
|
|
|
return error("Can't create zip archive: #{zip_result[:message]}")
|
|
|
|
end
|
|
|
|
|
|
|
|
archive_path = zip_result[:archive_path]
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
unless archive_path
|
|
|
|
return error("Archive not created. Missing public directory in #{@project.pages_path}") unless @mark_projects_as_not_deployed
|
|
|
|
|
|
|
|
project.set_first_pages_deployment!(nil)
|
|
|
|
|
|
|
|
return success(
|
|
|
|
message: "Archive not created. Missing public directory in #{project.pages_path}? Marked project as not deployed")
|
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
deployment = nil
|
|
|
|
File.open(archive_path) do |file|
|
|
|
|
deployment = project.pages_deployments.create!(
|
|
|
|
file: file,
|
|
|
|
file_count: zip_result[:entries_count],
|
|
|
|
file_sha256: Digest::SHA256.file(archive_path).hexdigest
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
project.set_first_pages_deployment!(deployment)
|
|
|
|
|
|
|
|
success
|
|
|
|
ensure
|
|
|
|
FileUtils.rm_f(archive_path) if archive_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|