2016-01-15 11:21:52 +00:00
|
|
|
module Projects
|
2016-02-15 14:01:42 +00:00
|
|
|
class UpdatePagesService < BaseService
|
2018-03-05 12:57:48 +00:00
|
|
|
InvaildStateError = Class.new(StandardError)
|
2018-03-05 19:37:09 +00:00
|
|
|
FailedToExtractError = Class.new(StandardError)
|
2018-03-05 18:15:42 +00:00
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
BLOCK_SIZE = 32.kilobytes
|
|
|
|
MAX_SIZE = 1.terabyte
|
2017-02-21 23:32:18 +00:00
|
|
|
SITE_PATH = 'public/'.freeze
|
2016-01-15 11:21:52 +00:00
|
|
|
|
|
|
|
attr_reader :build
|
|
|
|
|
|
|
|
def initialize(project, build)
|
|
|
|
@project, @build = project, build
|
|
|
|
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
|
2016-08-13 09:15:31 +00:00
|
|
|
@status.enqueue!
|
2016-01-15 11:21:52 +00:00
|
|
|
@status.run!
|
|
|
|
|
2018-03-05 12:57:48 +00:00
|
|
|
raise InvaildStateError, 'missing pages artifacts' unless build.artifacts?
|
|
|
|
raise InvaildStateError, 'pages are outdated' unless latest?
|
2016-01-15 11:21:52 +00:00
|
|
|
|
|
|
|
# Create temporary directory in which we will extract the artifacts
|
|
|
|
FileUtils.mkdir_p(tmp_path)
|
|
|
|
Dir.mktmpdir(nil, tmp_path) do |archive_path|
|
|
|
|
extract_archive!(archive_path)
|
|
|
|
|
|
|
|
# Check if we did extract public directory
|
|
|
|
archive_public_path = File.join(archive_path, 'public')
|
2018-03-30 08:20:47 +00:00
|
|
|
raise InvaildStateError, 'pages miss the public folder' unless Dir.exist?(archive_public_path)
|
2018-03-05 12:57:48 +00:00
|
|
|
raise InvaildStateError, 'pages are outdated' unless latest?
|
2016-01-15 11:21:52 +00:00
|
|
|
|
|
|
|
deploy_page!(archive_public_path)
|
|
|
|
success
|
|
|
|
end
|
2018-03-30 07:50:23 +00:00
|
|
|
rescue InvaildStateError => e
|
2016-01-15 11:21:52 +00:00
|
|
|
error(e.message)
|
2018-03-30 07:50:23 +00:00
|
|
|
rescue => 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
|
|
|
|
@status.success
|
|
|
|
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
|
2017-09-05 06:10:34 +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
|
|
|
|
|
|
|
|
def extract_archive!(temp_path)
|
2018-03-29 17:37:00 +00:00
|
|
|
if artifacts.ends_with?('.zip')
|
2016-01-20 20:49:26 +00:00
|
|
|
extract_zip_archive!(temp_path)
|
|
|
|
else
|
2018-03-30 07:50:23 +00:00
|
|
|
raise InvaildStateError, 'unsupported artifacts format'
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_zip_archive!(temp_path)
|
2018-03-30 07:50:23 +00:00
|
|
|
raise InvaildStateError, 'missing artifacts metadata' unless build.artifacts_metadata?
|
2016-01-20 20:49:26 +00:00
|
|
|
|
|
|
|
# Calculate page size after extract
|
|
|
|
public_entry = build.artifacts_metadata_entry(SITE_PATH, recursive: true)
|
|
|
|
|
|
|
|
if public_entry.total_size > max_size
|
2018-03-30 07:50:23 +00:00
|
|
|
raise InvaildStateError, "artifacts for pages are too large: #{public_entry.total_size}"
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Requires UnZip at least 6.00 Info-ZIP.
|
2017-06-28 21:52:58 +00:00
|
|
|
# -qq be (very) quiet
|
2016-01-20 20:49:26 +00:00
|
|
|
# -n never overwrite existing files
|
|
|
|
# We add * to end of SITE_PATH, because we want to extract SITE_PATH and all subdirectories
|
|
|
|
site_path = File.join(SITE_PATH, '*')
|
2017-06-08 05:29:35 +00:00
|
|
|
build.artifacts_file.use_file do |artifacts_path|
|
|
|
|
unless system(*%W(unzip -n #{artifacts_path} #{site_path} -d #{temp_path}))
|
2018-03-09 15:31:31 +00:00
|
|
|
raise FailedToExtractError, 'pages failed to extract'
|
2017-06-08 05:29:35 +00:00
|
|
|
end
|
2016-01-20 20:49:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-15 11:21:52 +00:00
|
|
|
def deploy_page!(archive_public_path)
|
|
|
|
# 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)
|
|
|
|
rescue
|
|
|
|
end
|
|
|
|
FileUtils.move(archive_public_path, public_path)
|
|
|
|
ensure
|
|
|
|
FileUtils.rm_r(previous_public_path, force: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def max_size
|
2018-02-02 18:39:55 +00:00
|
|
|
max_pages_size = Gitlab::CurrentSettings.max_pages_size.megabytes
|
2017-07-26 09:19:57 +00:00
|
|
|
|
|
|
|
return MAX_SIZE if max_pages_size.zero?
|
|
|
|
|
|
|
|
[max_pages_size, MAX_SIZE].min
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_path
|
2017-01-10 15:46:47 +00:00
|
|
|
@tmp_path ||= File.join(::Settings.pages.path, 'tmp')
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def pages_path
|
|
|
|
@pages_path ||= project.pages_path
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_path
|
|
|
|
@public_path ||= File.join(pages_path, 'public')
|
|
|
|
end
|
|
|
|
|
|
|
|
def previous_public_path
|
|
|
|
@previous_public_path ||= File.join(pages_path, "public.#{SecureRandom.hex}")
|
|
|
|
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
|
2016-01-15 11:21:52 +00:00
|
|
|
end
|
|
|
|
end
|