diff options
Diffstat (limited to 'lib/gitlab/pages')
-rw-r--r-- | lib/gitlab/pages/migration_helper.rb | 53 | ||||
-rw-r--r-- | lib/gitlab/pages/settings.rb | 22 | ||||
-rw-r--r-- | lib/gitlab/pages/stores/local_store.rb | 15 |
3 files changed, 87 insertions, 3 deletions
diff --git a/lib/gitlab/pages/migration_helper.rb b/lib/gitlab/pages/migration_helper.rb new file mode 100644 index 00000000000..8f8667fafd9 --- /dev/null +++ b/lib/gitlab/pages/migration_helper.rb @@ -0,0 +1,53 @@ +# frozen_string_literal: true + +module Gitlab + module Pages + class MigrationHelper + def initialize(logger = nil) + @logger = logger + end + + def migrate_to_remote_storage + deployments = ::PagesDeployment.with_files_stored_locally + migrate(deployments, ObjectStorage::Store::REMOTE) + end + + def migrate_to_local_storage + deployments = ::PagesDeployment.with_files_stored_remotely + migrate(deployments, ObjectStorage::Store::LOCAL) + end + + private + + def batch_size + ENV.fetch('MIGRATION_BATCH_SIZE', 10).to_i + end + + def migrate(deployments, store) + deployments.find_each(batch_size: batch_size) do |deployment| # rubocop:disable CodeReuse/ActiveRecord + deployment.file.migrate!(store) + + log_success(deployment, store) + rescue => e + log_error(e, deployment) + end + end + + def log_success(deployment, store) + logger.info("Transferred deployment ID #{deployment.id} of type #{deployment.file_type} with size #{deployment.size} to #{storage_label(store)} storage") + end + + def log_error(err, deployment) + logger.warn("Failed to transfer deployment of type #{deployment.file_type} and ID #{deployment.id} with error: #{err.message}") + end + + def storage_label(store) + if store == ObjectStorage::Store::LOCAL + 'local' + else + 'object' + end + end + end + end +end diff --git a/lib/gitlab/pages/settings.rb b/lib/gitlab/pages/settings.rb index 8650a80a85e..be71018e851 100644 --- a/lib/gitlab/pages/settings.rb +++ b/lib/gitlab/pages/settings.rb @@ -6,12 +6,28 @@ module Gitlab DiskAccessDenied = Class.new(StandardError) def path - if ::Gitlab::Runtime.web_server? && !::Gitlab::Runtime.test_suite? - raise DiskAccessDenied - end + report_denied_disk_access super end + + def local_store + @local_store ||= ::Gitlab::Pages::Stores::LocalStore.new(super) + end + + private + + def disk_access_denied? + return true unless ::Settings.pages.local_store&.enabled + + ::Gitlab::Runtime.web_server? && !::Gitlab::Runtime.test_suite? + end + + def report_denied_disk_access + raise DiskAccessDenied if disk_access_denied? + rescue => e + ::Gitlab::ErrorTracking.track_exception(e) + end end end end diff --git a/lib/gitlab/pages/stores/local_store.rb b/lib/gitlab/pages/stores/local_store.rb new file mode 100644 index 00000000000..68a7ebaceff --- /dev/null +++ b/lib/gitlab/pages/stores/local_store.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Gitlab + module Pages + module Stores + class LocalStore < ::SimpleDelegator + def enabled + return false unless Feature.enabled?(:pages_update_legacy_storage, default_enabled: true) + + super + end + end + end + end +end |