2015-11-03 15:28:07 -05:00
|
|
|
class PagesWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include Gitlab::CurrentSettings
|
|
|
|
|
|
|
|
BLOCK_SIZE = 32.kilobytes
|
|
|
|
MAX_SIZE = 1.terabyte
|
|
|
|
|
2015-12-16 06:53:35 -05:00
|
|
|
sidekiq_options queue: :pages, retry: false
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2015-12-18 07:07:53 -05:00
|
|
|
def perform(action, *arg)
|
|
|
|
send(action, *arg)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deploy(build_id)
|
2015-11-03 15:28:07 -05:00
|
|
|
@build_id = build_id
|
|
|
|
return unless valid?
|
|
|
|
|
|
|
|
# Create status notifying the deployment of pages
|
2015-12-16 10:29:53 -05:00
|
|
|
@status = create_status
|
2015-11-03 15:28:07 -05:00
|
|
|
@status.run!
|
2015-12-16 11:09:11 -05:00
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
raise 'pages are outdated' unless latest?
|
2015-11-03 15:28:07 -05:00
|
|
|
|
|
|
|
# Create temporary directory in which we will extract the artifacts
|
2015-12-16 11:09:11 -05:00
|
|
|
FileUtils.mkdir_p(tmp_path)
|
2015-12-16 10:29:53 -05:00
|
|
|
Dir.mktmpdir(nil, tmp_path) do |archive_path|
|
2015-12-16 11:09:11 -05:00
|
|
|
extract_archive!(archive_path)
|
2015-11-03 15:28:07 -05:00
|
|
|
|
|
|
|
# Check if we did extract public directory
|
2015-12-16 10:29:53 -05:00
|
|
|
archive_public_path = File.join(archive_path, 'public')
|
|
|
|
raise 'pages miss the public folder' unless Dir.exists?(archive_public_path)
|
|
|
|
raise 'pages are outdated' unless latest?
|
2015-12-16 11:09:11 -05:00
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
deploy_page!(archive_public_path)
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
@status.success
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
fail(e.message, !latest?)
|
2015-12-16 11:09:11 -05:00
|
|
|
return false
|
2015-12-16 10:29:53 -05:00
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2015-12-18 07:07:53 -05:00
|
|
|
def remove(namespace_path, project_path)
|
|
|
|
full_path = File.join(Settings.pages.path, namespace_path, project_path)
|
|
|
|
FileUtils.rm_r(full_path, force: true)
|
|
|
|
end
|
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
private
|
2015-12-16 06:53:35 -05:00
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
def create_status
|
|
|
|
GenericCommitStatus.new(
|
|
|
|
project: project,
|
|
|
|
commit: build.commit,
|
|
|
|
user: build.user,
|
|
|
|
ref: build.ref,
|
|
|
|
stage: 'deploy',
|
|
|
|
name: 'pages:deploy'
|
|
|
|
)
|
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
|
2015-12-16 11:09:11 -05:00
|
|
|
def extract_archive!(temp_path)
|
2015-12-16 10:29:53 -05:00
|
|
|
results = Open3.pipeline(%W(gunzip -c #{artifacts}),
|
|
|
|
%W(dd bs=#{BLOCK_SIZE} count=#{blocks}),
|
|
|
|
%W(tar -x -C #{temp_path} public/),
|
|
|
|
err: '/dev/null')
|
2015-12-16 11:09:11 -05:00
|
|
|
raise 'pages failed to extract' unless results.compact.all?(&:success?)
|
2015-12-16 10:29:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
2015-12-16 11:09:11 -05:00
|
|
|
begin
|
|
|
|
FileUtils.move(public_path, previous_public_path)
|
|
|
|
rescue
|
|
|
|
end
|
2015-12-16 10:29:53 -05:00
|
|
|
FileUtils.move(archive_public_path, public_path)
|
2015-11-03 15:28:07 -05:00
|
|
|
ensure
|
2015-12-16 10:29:53 -05:00
|
|
|
FileUtils.rm_r(previous_public_path, force: true)
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
|
|
|
|
2015-12-16 10:29:53 -05:00
|
|
|
def fail(message, allow_failure = true)
|
|
|
|
@status.allow_failure = allow_failure
|
|
|
|
@status.description = message
|
|
|
|
@status.drop
|
|
|
|
end
|
2015-11-03 15:28:07 -05:00
|
|
|
|
|
|
|
def valid?
|
2015-12-16 10:29:53 -05:00
|
|
|
build && build.artifacts_file?
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest?
|
2015-11-03 15:28:07 -05:00
|
|
|
# check if sha for the ref is still the most recent one
|
|
|
|
# this helps in case when multiple deployments happens
|
2015-12-16 10:29:53 -05:00
|
|
|
sha == latest_sha
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocks
|
|
|
|
# Calculate dd parameters: we limit the size of pages
|
|
|
|
max_size = current_application_settings.max_pages_size.megabytes
|
|
|
|
max_size ||= MAX_SIZE
|
|
|
|
blocks = 1 + max_size / BLOCK_SIZE
|
|
|
|
blocks
|
2015-11-03 15:28:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build
|
|
|
|
@build ||= Ci::Build.find_by(id: @build_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def project
|
|
|
|
@project ||= build.project
|
|
|
|
end
|
|
|
|
|
|
|
|
def tmp_path
|
|
|
|
@tmp_path ||= File.join(Settings.pages.path, 'tmp')
|
|
|
|
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
|
|
|
|
|
|
|
|
def artifacts
|
|
|
|
build.artifacts_file.path
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_sha
|
|
|
|
project.commit(build.ref).try(:sha).to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha
|
|
|
|
build.sha
|
|
|
|
end
|
|
|
|
end
|