2018-07-17 16:50:37 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
module Projects
|
2016-02-15 14:01:42 +00:00
|
|
|
class UpdatePagesService < BaseService
|
2018-04-27 17:40:31 +00:00
|
|
|
InvalidStateError = Class.new(StandardError)
|
2018-03-05 19:37:09 +00:00
|
|
|
FailedToExtractError = Class.new(StandardError)
|
2020-12-02 12:09:46 +00:00
|
|
|
ExclusiveLeaseTaken = Class.new(StandardError)
|
|
|
|
|
|
|
|
include ::Pages::LegacyStorageLease
|
2018-03-05 18:15:42 +00:00
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
BLOCK_SIZE = 32.kilobytes
|
2019-08-31 19:22:19 +00:00
|
|
|
PUBLIC_DIR = 'public'
|
2019-01-02 19:01:11 +00:00
|
|
|
|
|
|
|
# this has to be invalid group name,
|
|
|
|
# as it shares the namespace with groups
|
2019-08-31 19:22:19 +00:00
|
|
|
TMP_EXTRACT_PATH = '@pages.tmp'
|
2016-01-15 11:21:52 +00:00
|
|
|
|
2020-10-29 15:09:12 +00:00
|
|
|
# old deployment can be cached by pages daemon
|
|
|
|
# so we need to give pages daemon some time update cache
|
|
|
|
# 10 minutes is enough, but 30 feels safer
|
|
|
|
OLD_DEPLOYMENTS_DESTRUCTION_DELAY = 30.minutes.freeze
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
attr_reader :build
|
|
|
|
|
|
|
|
def initialize(project, build)
|
2021-04-19 21:09:27 +00:00
|
|
|
@project = project
|
|
|
|
@build = build
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2018-03-05 12:57:48 +00:00
|
|
|
register_attempt
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
# Create status notifying the deployment of pages
|
|
|
|
@status = create_status
|
2021-04-13 18:11:28 +00:00
|
|
|
@status.update_older_statuses_retried! if Feature.enabled?(:ci_fix_commit_status_retried, project, default_enabled: :yaml)
|
2020-05-27 15:08:11 +00:00
|
|
|
@status.enqueue!
|
|
|
|
@status.run!
|
2016-01-15 11:21:52 +00:00
|
|
|
|
2018-04-27 17:40:31 +00:00
|
|
|
raise InvalidStateError, 'missing pages artifacts' unless build.artifacts?
|
2020-02-21 21:08:57 +00:00
|
|
|
raise InvalidStateError, 'build SHA is outdated for this ref' unless latest?
|
2016-01-15 11:21:52 +00:00
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
build.artifacts_file.use_file do |artifacts_path|
|
|
|
|
deploy_to_legacy_storage(artifacts_path)
|
2016-01-15 11:21:52 +00:00
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
create_pages_deployment(artifacts_path, build)
|
2016-01-15 11:21:52 +00:00
|
|
|
|
|
|
|
success
|
|
|
|
end
|
2018-04-27 17:40:31 +00:00
|
|
|
rescue InvalidStateError => e
|
2016-01-15 11:21:52 +00:00
|
|
|
error(e.message)
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError => e
|
2018-04-27 17:30:52 +00:00
|
|
|
error(e.message)
|
2018-03-30 08:20:47 +00:00
|
|
|
raise e
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def success
|
2020-05-27 15:08:11 +00:00
|
|
|
@status.success
|
2020-08-31 18:10:43 +00:00
|
|
|
@project.mark_pages_as_deployed(artifacts_archive: build.job_artifacts_archive)
|
2016-01-15 11:21:52 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2018-04-27 17:30:52 +00:00
|
|
|
def error(message)
|
2018-03-30 07:50:23 +00:00
|
|
|
register_failure
|
2017-03-30 12:39:17 +00:00
|
|
|
log_error("Projects::UpdatePagesService: #{message}")
|
2016-01-15 11:21:52 +00:00
|
|
|
@status.allow_failure = !latest?
|
|
|
|
@status.description = message
|
2020-05-27 15:08:11 +00:00
|
|
|
@status.drop(:script_failure)
|
2016-01-15 11:21:52 +00:00
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_status
|
|
|
|
GenericCommitStatus.new(
|
|
|
|
project: project,
|
2016-06-07 14:40:15 +00:00
|
|
|
pipeline: build.pipeline,
|
2016-01-15 11:21:52 +00:00
|
|
|
user: build.user,
|
|
|
|
ref: build.ref,
|
|
|
|
stage: 'deploy',
|
|
|
|
name: 'pages:deploy'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
def deploy_to_legacy_storage(artifacts_path)
|
2021-02-02 15:09:06 +00:00
|
|
|
# path today used by one project can later be used by another
|
|
|
|
# so we can't really scope this feature flag by project or group
|
2021-04-06 12:09:21 +00:00
|
|
|
return unless ::Settings.pages.local_store.enabled
|
2021-02-02 15:09:06 +00:00
|
|
|
|
2021-04-16 03:09:00 +00:00
|
|
|
return if Feature.enabled?(:skip_pages_deploy_to_legacy_storage, project, default_enabled: :yaml)
|
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
# Create temporary directory in which we will extract the artifacts
|
|
|
|
make_secure_tmp_dir(tmp_path) do |tmp_path|
|
|
|
|
extract_archive!(artifacts_path, tmp_path)
|
|
|
|
|
|
|
|
# Check if we did extract public directory
|
|
|
|
archive_public_path = File.join(tmp_path, PUBLIC_DIR)
|
|
|
|
raise InvalidStateError, 'pages miss the public folder' unless Dir.exist?(archive_public_path)
|
|
|
|
raise InvalidStateError, 'build SHA is outdated for this ref' unless latest?
|
|
|
|
|
|
|
|
deploy_page!(archive_public_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_archive!(artifacts_path, temp_path)
|
2018-03-29 17:37:00 +00:00
|
|
|
if artifacts.ends_with?('.zip')
|
2021-01-18 15:10:42 +00:00
|
|
|
extract_zip_archive!(artifacts_path, temp_path)
|
2016-01-20 20:49:26 +00:00
|
|
|
else
|
2018-04-27 17:40:31 +00:00
|
|
|
raise InvalidStateError, 'unsupported artifacts format'
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
def extract_zip_archive!(artifacts_path, temp_path)
|
2018-04-27 17:40:31 +00:00
|
|
|
raise InvalidStateError, 'missing artifacts metadata' unless build.artifacts_metadata?
|
2016-01-20 20:49:26 +00:00
|
|
|
|
|
|
|
# Calculate page size after extract
|
2019-01-02 19:01:11 +00:00
|
|
|
public_entry = build.artifacts_metadata_entry(PUBLIC_DIR + '/', recursive: true)
|
2016-01-20 20:49:26 +00:00
|
|
|
|
|
|
|
if public_entry.total_size > max_size
|
2018-04-27 17:40:31 +00:00
|
|
|
raise InvalidStateError, "artifacts for pages are too large: #{public_entry.total_size}"
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
|
2021-01-18 15:10:42 +00:00
|
|
|
SafeZip::Extract.new(artifacts_path)
|
|
|
|
.extract(directories: [PUBLIC_DIR], to: temp_path)
|
2019-01-02 19:01:11 +00:00
|
|
|
rescue SafeZip::Extract::Error => e
|
|
|
|
raise FailedToExtractError, e.message
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
def deploy_page!(archive_public_path)
|
2020-12-02 12:09:46 +00:00
|
|
|
deployed = try_obtain_lease do
|
|
|
|
deploy_page_unsafe!(archive_public_path)
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
unless deployed
|
|
|
|
raise ExclusiveLeaseTaken, "Failed to deploy pages - other deployment is in progress"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy_page_unsafe!(archive_public_path)
|
2016-01-15 11:21:52 +00:00
|
|
|
# Do atomic move of pages
|
|
|
|
# Move and removal may not be atomic, but they are significantly faster then extracting and removal
|
|
|
|
# 1. We move deployed public to previous public path (file removal is slow)
|
|
|
|
# 2. We move temporary public to be deployed public
|
|
|
|
# 3. We remove previous public path
|
|
|
|
FileUtils.mkdir_p(pages_path)
|
|
|
|
begin
|
|
|
|
FileUtils.move(public_path, previous_public_path)
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
FileUtils.move(archive_public_path, public_path)
|
|
|
|
ensure
|
|
|
|
FileUtils.rm_r(previous_public_path, force: true)
|
|
|
|
end
|
|
|
|
|
2020-10-22 15:08:25 +00:00
|
|
|
def create_pages_deployment(artifacts_path, build)
|
|
|
|
# we're using the full archive and pages daemon needs to read it
|
|
|
|
# so we want the total count from entries, not only "public/" directory
|
|
|
|
# because it better approximates work we need to do before we can serve the site
|
|
|
|
entries_count = build.artifacts_metadata_entry("", recursive: true).entries.count
|
|
|
|
sha256 = build.job_artifacts_archive.file_sha256
|
|
|
|
|
2020-10-29 15:09:12 +00:00
|
|
|
deployment = nil
|
2020-10-12 18:08:31 +00:00
|
|
|
File.open(artifacts_path) do |file|
|
2020-10-22 15:08:25 +00:00
|
|
|
deployment = project.pages_deployments.create!(file: file,
|
|
|
|
file_count: entries_count,
|
|
|
|
file_sha256: sha256)
|
2021-01-18 15:10:42 +00:00
|
|
|
|
|
|
|
raise InvalidStateError, 'build SHA is outdated for this ref' unless latest?
|
|
|
|
|
2020-11-05 15:09:04 +00:00
|
|
|
project.update_pages_deployment!(deployment)
|
2020-10-12 18:08:31 +00:00
|
|
|
end
|
|
|
|
|
2020-10-29 15:09:12 +00:00
|
|
|
DestroyPagesDeploymentsWorker.perform_in(
|
|
|
|
OLD_DEPLOYMENTS_DESTRUCTION_DELAY,
|
|
|
|
project.id,
|
|
|
|
deployment.id
|
|
|
|
)
|
2020-10-12 18:08:31 +00:00
|
|
|
end
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
def latest?
|
|
|
|
# check if sha for the ref is still the most recent one
|
|
|
|
# this helps in case when multiple deployments happens
|
|
|
|
sha == latest_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocks
|
|
|
|
# Calculate dd parameters: we limit the size of pages
|
2016-01-20 20:49:26 +00:00
|
|
|
1 + max_size / BLOCK_SIZE
|
|
|
|
end
|
|
|
|
|
2019-12-20 09:24:38 +00:00
|
|
|
def max_size_from_settings
|
|
|
|
Gitlab::CurrentSettings.max_pages_size.megabytes
|
|
|
|
end
|
|
|
|
|
2016-01-20 20:49:26 +00:00
|
|
|
def max_size
|
2019-12-20 09:24:38 +00:00
|
|
|
max_pages_size = max_size_from_settings
|
2017-07-26 09:19:57 +00:00
|
|
|
|
2020-08-12 06:09:53 +00:00
|
|
|
return ::Gitlab::Pages::MAX_SIZE if max_pages_size == 0
|
2017-07-26 09:19:57 +00:00
|
|
|
|
2019-12-20 09:24:38 +00:00
|
|
|
max_pages_size
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_path
|
2019-01-02 19:01:11 +00:00
|
|
|
@tmp_path ||= File.join(::Settings.pages.path, TMP_EXTRACT_PATH)
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages_path
|
|
|
|
@pages_path ||= project.pages_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_path
|
2019-01-02 19:01:11 +00:00
|
|
|
@public_path ||= File.join(pages_path, PUBLIC_DIR)
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def previous_public_path
|
2019-01-02 19:01:11 +00:00
|
|
|
@previous_public_path ||= File.join(pages_path, "#{PUBLIC_DIR}.#{SecureRandom.hex}")
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def ref
|
|
|
|
build.ref
|
|
|
|
end
|
|
|
|
|
2018-03-09 15:31:31 +00:00
|
|
|
def artifacts
|
|
|
|
build.artifacts_file.path
|
|
|
|
end
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
def latest_sha
|
|
|
|
project.commit(build.ref).try(:sha).to_s
|
2018-03-30 08:20:32 +00:00
|
|
|
ensure
|
|
|
|
# Close any file descriptors that were opened and free libgit2 buffers
|
|
|
|
project.cleanup
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
build.sha
|
|
|
|
end
|
2017-07-24 10:44:33 +00:00
|
|
|
|
|
|
|
def register_attempt
|
2017-08-10 18:05:44 +00:00
|
|
|
pages_deployments_total_counter.increment
|
2017-07-24 10:44:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def register_failure
|
2017-08-10 18:05:44 +00:00
|
|
|
pages_deployments_failed_total_counter.increment
|
2017-07-24 10:44:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages_deployments_total_counter
|
|
|
|
@pages_deployments_total_counter ||= Gitlab::Metrics.counter(:pages_deployments_total, "Counter of GitLab Pages deployments triggered")
|
|
|
|
end
|
|
|
|
|
|
|
|
def pages_deployments_failed_total_counter
|
|
|
|
@pages_deployments_failed_total_counter ||= Gitlab::Metrics.counter(:pages_deployments_failed_total, "Counter of GitLab Pages deployments which failed")
|
|
|
|
end
|
2019-01-02 19:01:11 +00:00
|
|
|
|
|
|
|
def make_secure_tmp_dir(tmp_path)
|
|
|
|
FileUtils.mkdir_p(tmp_path)
|
2021-04-12 12:09:15 +00:00
|
|
|
path = Dir.mktmpdir(tmp_dir_prefix, tmp_path)
|
2019-01-02 19:01:11 +00:00
|
|
|
begin
|
|
|
|
yield(path)
|
|
|
|
ensure
|
|
|
|
FileUtils.remove_entry_secure(path)
|
|
|
|
end
|
|
|
|
end
|
2021-04-12 12:09:15 +00:00
|
|
|
|
|
|
|
def tmp_dir_prefix
|
|
|
|
"project-#{project.id}-build-#{build.id}-"
|
|
|
|
end
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
end
|
2019-12-20 09:24:38 +00:00
|
|
|
|
|
|
|
Projects::UpdatePagesService.prepend_if_ee('EE::Projects::UpdatePagesService')
|