Commit 51f1a875 authored by Robert Speicher's avatar Robert Speicher

Merge branch 'gitlab-pages-zip' into 'master'

Add support for ZIP build artifacts for Pages

This is based on https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/116

It adds support for creating pages from zip archives.

Omnibus change: https://gitlab.com/gitlab-org/omnibus-gitlab/issues/1057

See merge request !117
parents fa75027e 1c5c8f99
...@@ -347,17 +347,17 @@ module Ci ...@@ -347,17 +347,17 @@ module Ci
end end
def artifacts_browse_url def artifacts_browse_url
if artifacts_browser_supported? if artifacts_metadata?
browse_namespace_project_build_artifacts_path(project.namespace, project, self) browse_namespace_project_build_artifacts_path(project.namespace, project, self)
end end
end end
def artifacts_browser_supported? def artifacts_metadata?
artifacts? && artifacts_metadata.exists? artifacts? && artifacts_metadata.exists?
end end
def artifacts_metadata_entry(path) def artifacts_metadata_entry(path, **options)
Gitlab::Ci::Build::Artifacts::Metadata.new(artifacts_metadata.path, path).to_entry Gitlab::Ci::Build::Artifacts::Metadata.new(artifacts_metadata.path, path, **options).to_entry
end end
private private
......
...@@ -2,6 +2,7 @@ module Projects ...@@ -2,6 +2,7 @@ module Projects
class UpdatePagesService < BaseService class UpdatePagesService < BaseService
BLOCK_SIZE = 32.kilobytes BLOCK_SIZE = 32.kilobytes
MAX_SIZE = 1.terabyte MAX_SIZE = 1.terabyte
SITE_PATH = 'public/'
attr_reader :build attr_reader :build
...@@ -60,13 +61,42 @@ module Projects ...@@ -60,13 +61,42 @@ module Projects
end end
def extract_archive!(temp_path) def extract_archive!(temp_path)
if artifacts.ends_with?('.tar.gz') || artifacts.ends_with?('.tgz')
extract_tar_archive!(temp_path)
elsif artifacts.ends_with?('.zip')
extract_zip_archive!(temp_path)
else
raise 'unsupported artifacts format'
end
end
def extract_tar_archive!(temp_path)
results = Open3.pipeline(%W(gunzip -c #{artifacts}), results = Open3.pipeline(%W(gunzip -c #{artifacts}),
%W(dd bs=#{BLOCK_SIZE} count=#{blocks}), %W(dd bs=#{BLOCK_SIZE} count=#{blocks}),
%W(tar -x -C #{temp_path} public/), %W(tar -x -C #{temp_path} #{SITE_PATH}),
err: '/dev/null') err: '/dev/null')
raise 'pages failed to extract' unless results.compact.all?(&:success?) raise 'pages failed to extract' unless results.compact.all?(&:success?)
end end
def extract_zip_archive!(temp_path)
raise 'missing artifacts metadata' unless build.artifacts_metadata?
# Calculate page size after extract
public_entry = build.artifacts_metadata_entry(SITE_PATH, recursive: true)
if public_entry.total_size > max_size
raise "artifacts for pages are too large: #{total_size}"
end
# Requires UnZip at least 6.00 Info-ZIP.
# -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, '*')
unless system(*%W(unzip -n #{artifacts} #{site_path} -d #{temp_path}))
raise 'pages failed to extract'
end
end
def deploy_page!(archive_public_path) def deploy_page!(archive_public_path)
# Do atomic move of pages # Do atomic move of pages
# Move and removal may not be atomic, but they are significantly faster then extracting and removal # Move and removal may not be atomic, but they are significantly faster then extracting and removal
...@@ -91,10 +121,11 @@ module Projects ...@@ -91,10 +121,11 @@ module Projects
def blocks def blocks
# Calculate dd parameters: we limit the size of pages # Calculate dd parameters: we limit the size of pages
max_size = current_application_settings.max_pages_size.megabytes 1 + max_size / BLOCK_SIZE
max_size ||= MAX_SIZE end
blocks = 1 + max_size / BLOCK_SIZE
blocks def max_size
current_application_settings.max_pages_size.megabytes || MAX_SIZE
end end
def tmp_path def tmp_path
......
...@@ -96,7 +96,7 @@ ...@@ -96,7 +96,7 @@
.center .center
.btn-group{ role: :group } .btn-group{ role: :group }
= link_to "Download", @build.artifacts_download_url, class: 'btn btn-sm btn-primary' = link_to "Download", @build.artifacts_download_url, class: 'btn btn-sm btn-primary'
- if @build.artifacts_browser_supported? - if @build.artifacts_metadata?
= link_to "Browse", @build.artifacts_browse_url, class: 'btn btn-sm btn-primary' = link_to "Browse", @build.artifacts_browse_url, class: 'btn btn-sm btn-primary'
.build-widget .build-widget
......
...@@ -13,8 +13,8 @@ module Gitlab ...@@ -13,8 +13,8 @@ module Gitlab
attr_reader :file, :path, :full_version attr_reader :file, :path, :full_version
def initialize(file, path) def initialize(file, path, **opts)
@file, @path = file, path @file, @path, @opts = file, path, opts
@full_version = read_version @full_version = read_version
end end
...@@ -52,7 +52,9 @@ module Gitlab ...@@ -52,7 +52,9 @@ module Gitlab
def match_entries(gz) def match_entries(gz)
entries = {} entries = {}
match_pattern = %r{^#{Regexp.escape(@path)}[^/]*/?$}
child_pattern = '[^/]*/?$' unless @opts[:recursive]
match_pattern = /^#{Regexp.escape(@path)}#{child_pattern}/
until gz.eof? do until gz.eof? do
begin begin
......
...@@ -95,6 +95,13 @@ module Gitlab ...@@ -95,6 +95,13 @@ module Gitlab
children.empty? children.empty?
end end
def total_size
descendant_pattern = %r{^#{Regexp.escape(@path)}}
entries.sum do |path, entry|
(entry[:size] if path =~ descendant_pattern).to_i
end
end
def to_s def to_s
@path @path
end end
......
...@@ -4,13 +4,13 @@ describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do ...@@ -4,13 +4,13 @@ describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do
let(:entries) do let(:entries) do
{ 'path/' => {}, { 'path/' => {},
'path/dir_1/' => {}, 'path/dir_1/' => {},
'path/dir_1/file_1' => {}, 'path/dir_1/file_1' => { size: 10 },
'path/dir_1/file_b' => {}, 'path/dir_1/file_b' => { size: 10 },
'path/dir_1/subdir/' => {}, 'path/dir_1/subdir/' => {},
'path/dir_1/subdir/subfile' => {}, 'path/dir_1/subdir/subfile' => { size: 10 },
'path/second_dir' => {}, 'path/second_dir' => {},
'path/second_dir/dir_3/file_2' => {}, 'path/second_dir/dir_3/file_2' => { size: 10 },
'path/second_dir/dir_3/file_3'=> {}, 'path/second_dir/dir_3/file_3'=> { size: 10 },
'another_directory/'=> {}, 'another_directory/'=> {},
'another_file' => {}, 'another_file' => {},
'/file/with/absolute_path' => {} } '/file/with/absolute_path' => {} }
...@@ -112,6 +112,11 @@ describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do ...@@ -112,6 +112,11 @@ describe Gitlab::Ci::Build::Artifacts::Metadata::Entry do
subject { |example| path(example).empty? } subject { |example| path(example).empty? }
it { is_expected.to be false } it { is_expected.to be false }
end end
describe '#total_size' do
subject { |example| path(example).total_size }
it { is_expected.to eq(30) }
end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Ci::Build::Artifacts::Metadata do describe Gitlab::Ci::Build::Artifacts::Metadata do
def metadata(path = '') def metadata(path = '', **opts)
described_class.new(metadata_file_path, path) described_class.new(metadata_file_path, path, **opts)
end end
let(:metadata_file_path) do let(:metadata_file_path) do
...@@ -51,6 +51,19 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do ...@@ -51,6 +51,19 @@ describe Gitlab::Ci::Build::Artifacts::Metadata do
end end
end end
describe '#find_entries! recursively for other_artifacts_0.1.2/' do
subject { metadata('other_artifacts_0.1.2/', recursive: true).find_entries! }
it 'matches correct paths' do
expect(subject.keys).
to contain_exactly 'other_artifacts_0.1.2/',
'other_artifacts_0.1.2/doc_sample.txt',
'other_artifacts_0.1.2/another-subdirectory/',
'other_artifacts_0.1.2/another-subdirectory/empty_directory/',
'other_artifacts_0.1.2/another-subdirectory/banana_sample.gif'
end
end
describe '#to_entry' do describe '#to_entry' do
subject { metadata('').to_entry } subject { metadata('').to_entry }
it { is_expected.to be_an_instance_of(Gitlab::Ci::Build::Artifacts::Metadata::Entry) } it { is_expected.to be_an_instance_of(Gitlab::Ci::Build::Artifacts::Metadata::Entry) }
......
...@@ -362,12 +362,12 @@ describe Ci::Build, models: true do ...@@ -362,12 +362,12 @@ describe Ci::Build, models: true do
subject { build.artifacts_browse_url } subject { build.artifacts_browse_url }
it "should be nil if artifacts browser is unsupported" do it "should be nil if artifacts browser is unsupported" do
allow(build).to receive(:artifacts_browser_supported?).and_return(false) allow(build).to receive(:artifacts_metadata?).and_return(false)
is_expected.to be_nil is_expected.to be_nil
end end
it 'should not be nil if artifacts browser is supported' do it 'should not be nil if artifacts browser is supported' do
allow(build).to receive(:artifacts_browser_supported?).and_return(true) allow(build).to receive(:artifacts_metadata?).and_return(true)
is_expected.to_not be_nil is_expected.to_not be_nil
end end
end end
...@@ -391,8 +391,8 @@ describe Ci::Build, models: true do ...@@ -391,8 +391,8 @@ describe Ci::Build, models: true do
end end
describe :artifacts_browser_supported? do describe :artifacts_metadata? do
subject { build.artifacts_browser_supported? } subject { build.artifacts_metadata? }
context 'artifacts metadata does not exist' do context 'artifacts metadata does not exist' do
it { is_expected.to be_falsy } it { is_expected.to be_falsy }
end end
......
...@@ -4,9 +4,7 @@ describe Projects::UpdatePagesService do ...@@ -4,9 +4,7 @@ describe Projects::UpdatePagesService do
let(:project) { create :project } let(:project) { create :project }
let(:commit) { create :ci_commit, project: project, sha: project.commit('HEAD').sha } let(:commit) { create :ci_commit, project: project, sha: project.commit('HEAD').sha }
let(:build) { create :ci_build, commit: commit, ref: 'HEAD' } let(:build) { create :ci_build, commit: commit, ref: 'HEAD' }
let(:file) { fixture_file_upload(Rails.root + 'spec/fixtures/pages.tar.gz', 'application/octet-stream') } let(:invalid_file) { fixture_file_upload(Rails.root + 'spec/fixtures/dk.png') }
let(:empty_file) { fixture_file_upload(Rails.root + 'spec/fixtures/pages_empty.tar.gz', 'application/octet-stream') }
let(:invalid_file) { fixture_file_upload(Rails.root + 'spec/fixtures/dk.png', 'application/octet-stream') }
subject { described_class.new(project, build) } subject { described_class.new(project, build) }
...@@ -14,8 +12,19 @@ describe Projects::UpdatePagesService do ...@@ -14,8 +12,19 @@ describe Projects::UpdatePagesService do
project.remove_pages project.remove_pages
end end
context 'for valid file' do %w(tar.gz zip).each do |format|
before { build.update_attributes(artifacts_file: file) } context "for valid #{format}" do
let(:file) { fixture_file_upload(Rails.root + "spec/fixtures/pages.#{format}") }
let(:empty_file) { fixture_file_upload(Rails.root + "spec/fixtures/pages_empty.#{format}") }
let(:metadata) do
filename = Rails.root + "spec/fixtures/pages.#{format}.meta"
fixture_file_upload(filename) if File.exists?(filename)
end
before do
build.update_attributes(artifacts_file: file)
build.update_attributes(artifacts_metadata: metadata)
end
it 'succeeds' do it 'succeeds' do
expect(project.pages_url).to be_nil expect(project.pages_url).to be_nil
...@@ -36,6 +45,18 @@ describe Projects::UpdatePagesService do ...@@ -36,6 +45,18 @@ describe Projects::UpdatePagesService do
project.destroy project.destroy
expect(Dir.exist?(project.public_pages_path)).to be_falsey expect(Dir.exist?(project.public_pages_path)).to be_falsey
end end
it 'fails if sha on branch is not latest' do
commit.update_attributes(sha: 'old_sha')
build.update_attributes(artifacts_file: file)
expect(execute).to_not eq(:success)
end
it 'fails for empty file fails' do
build.update_attributes(artifacts_file: empty_file)
expect(execute).to_not eq(:success)
end
end
end end
it 'fails to remove project pages when no pages is deployed' do it 'fails to remove project pages when no pages is deployed' do
...@@ -48,22 +69,11 @@ describe Projects::UpdatePagesService do ...@@ -48,22 +69,11 @@ describe Projects::UpdatePagesService do
expect(execute).to_not eq(:success) expect(execute).to_not eq(:success)
end end
it 'fails for empty file fails' do
build.update_attributes(artifacts_file: empty_file)
expect(execute).to_not eq(:success)
end
it 'fails for invalid archive' do it 'fails for invalid archive' do
build.update_attributes(artifacts_file: invalid_file) build.update_attributes(artifacts_file: invalid_file)
expect(execute).to_not eq(:success) expect(execute).to_not eq(:success)
end end
it 'fails if sha on branch is not latest' do
commit.update_attributes(sha: 'old_sha')
build.update_attributes(artifacts_file: file)
expect(execute).to_not eq(:success)
end
def execute def execute
subject.execute[:status] subject.execute[:status]
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment