summaryrefslogtreecommitdiff
path: root/lib/gitlab
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2018-03-06 16:20:33 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2018-03-06 16:20:33 +0000
commit12bc2d8df0890fad3296b47395fb7279a11e43c1 (patch)
treefa1905eb722c4b11237fcfc52f95baecab0dc05c /lib/gitlab
parente5d03612552d1aedb24dcb1347bc507835a97757 (diff)
parent0ac1322045bd6d069aa74da04df7fb2d2797a3f0 (diff)
downloadgitlab-ce-12bc2d8df0890fad3296b47395fb7279a11e43c1.tar.gz
Merge branch 'proper-fix-for-artifacts-service' into 'master'
Add archive feature to trace Closes #43022 and gitlab-ee#4170 See merge request gitlab-org/gitlab-ce!17314
Diffstat (limited to 'lib/gitlab')
-rw-r--r--lib/gitlab/ci/trace.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/gitlab/ci/trace.rb b/lib/gitlab/ci/trace.rb
index f2e5124c8a8..c5356af2f02 100644
--- a/lib/gitlab/ci/trace.rb
+++ b/lib/gitlab/ci/trace.rb
@@ -1,6 +1,8 @@
module Gitlab
module Ci
class Trace
+ ArchiveError = Class.new(StandardError)
+
attr_reader :job
delegate :old_trace, to: :job
@@ -93,8 +95,52 @@ module Gitlab
job.erase_old_trace!
end
+ def archive!
+ raise ArchiveError, 'Already archived' if trace_artifact
+ raise ArchiveError, 'Job is not finished yet' unless job.complete?
+
+ if current_path
+ File.open(current_path) do |stream|
+ archive_stream!(stream)
+ FileUtils.rm(current_path)
+ end
+ elsif old_trace
+ StringIO.new(old_trace, 'rb').tap do |stream|
+ archive_stream!(stream)
+ job.erase_old_trace!
+ end
+ end
+ end
+
private
+ def archive_stream!(stream)
+ clone_file!(stream, JobArtifactUploader.workhorse_upload_path) do |clone_path|
+ create_job_trace!(job, clone_path)
+ end
+ end
+
+ def clone_file!(src_stream, temp_dir)
+ FileUtils.mkdir_p(temp_dir)
+ Dir.mktmpdir('tmp-trace', temp_dir) do |dir_path|
+ temp_path = File.join(dir_path, "job.log")
+ FileUtils.touch(temp_path)
+ size = IO.copy_stream(src_stream, temp_path)
+ raise ArchiveError, 'Failed to copy stream' unless size == src_stream.size
+
+ yield(temp_path)
+ end
+ end
+
+ def create_job_trace!(job, path)
+ File.open(path) do |stream|
+ job.create_job_artifacts_trace!(
+ project: job.project,
+ file_type: :trace,
+ file: stream)
+ end
+ end
+
def ensure_path
return current_path if current_path