From 7e46db0f5a5b6aa84ac653fa4826b70bf50d6909 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Tue, 28 Feb 2017 13:29:52 +0100 Subject: Add job patch trace API --- lib/api/helpers/runner.rb | 13 +++++++++++++ lib/api/runner.rb | 32 +++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/api/helpers/runner.rb b/lib/api/helpers/runner.rb index 15eb6b932ed..e71895c091e 100644 --- a/lib/api/helpers/runner.rb +++ b/lib/api/helpers/runner.rb @@ -1,6 +1,8 @@ module API module Helpers module Runner + JOB_TOKEN_HEADER = 'HTTP_JOB_TOKEN' + JOB_TOKEN_PARAM = :token UPDATE_RUNNER_EVERY = 10 * 60 def runner_registration_token_valid? @@ -55,6 +57,17 @@ module API forbidden!('Project has been deleted!') unless job.project forbidden!('Job has been erased!') if job.erased? end + + def authenticate_job!(job) + validate_job!(job) do + forbidden! unless job_token_valid?(job) + end + end + + def job_token_valid?(job) + token = (params[JOB_TOKEN_PARAM] || env[JOB_TOKEN_HEADER]).to_s + token && job.valid_token?(token) + end end end end diff --git a/lib/api/runner.rb b/lib/api/runner.rb index b57fbdabab9..2b636847dba 100644 --- a/lib/api/runner.rb +++ b/lib/api/runner.rb @@ -93,7 +93,7 @@ module API http_codes [[200, 'Job was updated'], [403, 'Forbidden']] end params do - requires :token, type: String, desc: %q(Job's authentication token) + requires :token, type: String, desc: %q(Runners's authentication token) requires :id, type: Fixnum, desc: %q(Job's ID) optional :trace, type: String, desc: %q(Job's full trace) optional :state, type: String, desc: %q(Job's status: success, failed) @@ -114,6 +114,36 @@ module API job.drop end end + + desc 'Appends a patch to the job.trace' do + http_codes [[202, 'Trace was patched'], + [400, 'Missing Content-Range header'], + [403, 'Forbidden'], + [416, 'Range not satisfiable']] + end + params do + requires :id, type: Fixnum, desc: %q(Job's ID) + optional :token, type: String, desc: %q(Job's authentication token) + end + patch '/:id/trace' do + job = Ci::Build.find_by_id(params[:id]) + authenticate_job!(job) + + error!('400 Missing header Content-Range', 400) unless request.headers.has_key?('Content-Range') + content_range = request.headers['Content-Range'] + content_range = content_range.split('-') + + current_length = job.trace_length + unless current_length == content_range[0].to_i + return error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{current_length}" }) + end + + job.append_trace(request.body.read, content_range[0].to_i) + + status 202 + header 'Job-Status', job.status + header 'Range', "0-#{job.trace_length}" + end end end end -- cgit v1.2.1