diff options
author | Shinya Maeda <shinya@gitlab.com> | 2018-02-07 14:24:01 +0300 |
---|---|---|
committer | Shinya Maeda <shinya@gitlab.com> | 2018-02-07 22:19:18 +0300 |
commit | 3cd7430b29c6f1d056b4a013f1e7ab3bd84dde03 (patch) | |
tree | ca24c387974b8e2e8f3583c02d4e39136a7f0106 | |
parent | 4457cf9d178dc9912fd9c16427ad81b389179d00 (diff) |
Migrate legacy trace files to artifacts
-rw-r--r-- | lib/gitlab/ci/trace/migrater.rb | 86 | ||||
-rw-r--r-- | lib/tasks/gitlab/traces.rake | 18 |
2 files changed, 104 insertions, 0 deletions
diff --git a/lib/gitlab/ci/trace/migrater.rb b/lib/gitlab/ci/trace/migrater.rb new file mode 100644 index 00000000000..c54b516ad9f --- /dev/null +++ b/lib/gitlab/ci/trace/migrater.rb @@ -0,0 +1,86 @@ +module Gitlab + module Ci + class Trace + class Migrater + include Gitlab::Utils::StrongMemoize + + attr_reader :path + + def initialize(path) + raise "File not found: #{path}" unless File.exists?(path) + + @path = path + end + + def perform + backup! + migrate! + end + + private + + def status + strong_memoize(:status) do + if !job + :not_found + elsif !job.complete? + :not_completed + elsif job.job_artifacts_trace + :duplicate + else + :migratable + end + end + end + + def job + @job ||= ::Ci::Build.find_by(id: job_id) + end + + def job_id + @job_id ||= File.basename(path, '.log')&.to_i + end + + def backup_path + strong_memoize(:backup_path) do + case status + when :not_found + path.gsub(/(\d{4}_\d{2})/, '\1_not_found') + when :not_completed + path.gsub(/(\d{4}_\d{2})/, '\1_not_completed') + when :duplicate + path.gsub(/(\d{4}_\d{2})/, '\1_duplicate') + when :migratable + path.gsub(/(\d{4}_\d{2})/, '\1_migrated') + end + end + end + + def backup_dir + @backup_dir ||= File.dirname(backup_path) + end + + def backup! + FileUtils.mkdir_p(backup_dir) + + if status == :migratable + FileUtils.cp(path, backup_path) + else + FileUtils.mv(path, backup_path) + end + end + + def migrate! + return unless status == :migratable + + File.open(path) do |stream| + job.create_job_artifacts_trace!( + project: job.project, + file_type: :trace, + file: stream) + end + end + end + end + end +end diff --git a/lib/tasks/gitlab/traces.rake b/lib/tasks/gitlab/traces.rake new file mode 100644 index 00000000000..de7a7046c71 --- /dev/null +++ b/lib/tasks/gitlab/traces.rake @@ -0,0 +1,18 @@ +require 'logger' +require 'resolv-replace' + +desc "GitLab | Migrate trace files to trace artifacts" +namespace :gitlab do + namespace :traces do + task :migrate, [:relative_path] => :environment do |t, args| + logger = Logger.new(STDOUT) + logger.info('Starting migration for trace files') + + trace_path = File.join(Settings.gitlab_ci.builds_path, args.relative_path) + + logger.info("trace_path: #{trace_path}") + + Gitlab::Ci::Trace::Migrater.new(trace_path).perform + end + end +end |