2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-14 17:52:02 -05:00
|
|
|
module API
|
2020-04-27 23:09:53 -04:00
|
|
|
class Runner < Grape::API
|
2017-02-15 19:30:46 -05:00
|
|
|
helpers ::API::Helpers::Runner
|
2017-02-14 17:52:02 -05:00
|
|
|
|
|
|
|
resource :runners do
|
|
|
|
desc 'Registers a new Runner' do
|
|
|
|
success Entities::RunnerRegistrationDetails
|
|
|
|
http_codes [[201, 'Runner was created'], [403, 'Forbidden']]
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: 'Registration token'
|
|
|
|
optional :description, type: String, desc: %q(Runner's description)
|
|
|
|
optional :info, type: Hash, desc: %q(Runner's metadata)
|
2018-05-09 08:13:54 -04:00
|
|
|
optional :active, type: Boolean, desc: 'Should Runner be active'
|
2017-02-14 17:52:02 -05:00
|
|
|
optional :locked, type: Boolean, desc: 'Should Runner be locked for current project'
|
2019-04-18 09:06:53 -04:00
|
|
|
optional :access_level, type: String, values: Ci::Runner.access_levels.keys,
|
|
|
|
desc: 'The access_level of the runner'
|
2017-02-14 17:52:02 -05:00
|
|
|
optional :run_untagged, type: Boolean, desc: 'Should Runner handle untagged jobs'
|
2020-04-27 23:09:53 -04:00
|
|
|
optional :tag_list, type: Array[String], desc: %q(List of Runner's tags)
|
2018-03-20 21:02:38 -04:00
|
|
|
optional :maximum_timeout, type: Integer, desc: 'Maximum timeout set when this Runner will handle the job'
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
|
|
|
post '/' do
|
2019-04-18 09:06:53 -04:00
|
|
|
attributes = attributes_for_keys([:description, :active, :locked, :run_untagged, :tag_list, :access_level, :maximum_timeout])
|
2018-02-22 01:03:00 -05:00
|
|
|
.merge(get_runner_details_from_request)
|
2017-02-14 17:52:02 -05:00
|
|
|
|
2018-05-28 07:09:31 -04:00
|
|
|
attributes =
|
2017-02-14 17:52:02 -05:00
|
|
|
if runner_registration_token_valid?
|
|
|
|
# Create shared runner. Requires admin access
|
2018-06-26 05:36:54 -04:00
|
|
|
attributes.merge(runner_type: :instance_type)
|
2018-11-20 09:25:24 -05:00
|
|
|
elsif project = Project.find_by_runners_token(params[:token])
|
2017-09-12 05:34:34 -04:00
|
|
|
# Create a specific runner for the project
|
2018-06-26 05:36:54 -04:00
|
|
|
attributes.merge(runner_type: :project_type, projects: [project])
|
2018-11-20 09:25:24 -05:00
|
|
|
elsif group = Group.find_by_runners_token(params[:token])
|
2017-09-12 05:34:34 -04:00
|
|
|
# Create a specific runner for the group
|
2018-06-26 05:36:54 -04:00
|
|
|
attributes.merge(runner_type: :group_type, groups: [group])
|
2018-05-28 07:09:31 -04:00
|
|
|
else
|
|
|
|
forbidden!
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
|
|
|
|
2018-05-28 07:09:31 -04:00
|
|
|
runner = Ci::Runner.create(attributes)
|
2017-02-14 17:52:02 -05:00
|
|
|
|
2018-05-28 07:09:31 -04:00
|
|
|
if runner.persisted?
|
2017-02-14 17:52:02 -05:00
|
|
|
present runner, with: Entities::RunnerRegistrationDetails
|
|
|
|
else
|
2018-05-28 07:09:31 -04:00
|
|
|
render_validation_error!(runner)
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Deletes a registered Runner' do
|
2017-02-27 11:02:25 -05:00
|
|
|
http_codes [[204, 'Runner was deleted'], [403, 'Forbidden']]
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: %q(Runner's authentication token)
|
|
|
|
end
|
|
|
|
delete '/' do
|
|
|
|
authenticate_runner!
|
2017-03-02 07:14:13 -05:00
|
|
|
|
|
|
|
runner = Ci::Runner.find_by_token(params[:token])
|
|
|
|
|
|
|
|
destroy_conditionally!(runner)
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
2017-03-14 19:27:10 -04:00
|
|
|
|
|
|
|
desc 'Validates authentication credentials' do
|
|
|
|
http_codes [[200, 'Credentials are valid'], [403, 'Forbidden']]
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: %q(Runner's authentication token)
|
|
|
|
end
|
|
|
|
post '/verify' do
|
|
|
|
authenticate_runner!
|
|
|
|
status 200
|
|
|
|
end
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
2017-02-15 12:08:29 -05:00
|
|
|
|
|
|
|
resource :jobs do
|
2020-02-05 16:09:02 -05:00
|
|
|
before do
|
|
|
|
Gitlab::ApplicationContext.push(
|
|
|
|
user: -> { current_job&.user },
|
|
|
|
project: -> { current_job&.project }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-02-15 12:08:29 -05:00
|
|
|
desc 'Request a job' do
|
2017-02-15 19:05:44 -05:00
|
|
|
success Entities::JobRequest::Response
|
2017-03-15 22:31:09 -04:00
|
|
|
http_codes [[201, 'Job was scheduled'],
|
|
|
|
[204, 'No job for Runner'],
|
|
|
|
[403, 'Forbidden']]
|
2017-02-15 12:08:29 -05:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: %q(Runner's authentication token)
|
2017-02-27 08:21:06 -05:00
|
|
|
optional :last_update, type: String, desc: %q(Runner's queue last_update token)
|
2018-07-17 06:58:57 -04:00
|
|
|
optional :info, type: Hash, desc: %q(Runner's metadata) do
|
|
|
|
optional :name, type: String, desc: %q(Runner's name)
|
|
|
|
optional :version, type: String, desc: %q(Runner's version)
|
|
|
|
optional :revision, type: String, desc: %q(Runner's revision)
|
|
|
|
optional :platform, type: String, desc: %q(Runner's platform)
|
|
|
|
optional :architecture, type: String, desc: %q(Runner's architecture)
|
|
|
|
optional :executor, type: String, desc: %q(Runner's executor)
|
|
|
|
optional :features, type: Hash, desc: %q(Runner's features)
|
|
|
|
end
|
2018-07-05 09:55:10 -04:00
|
|
|
optional :session, type: Hash, desc: %q(Runner's session data) do
|
|
|
|
optional :url, type: String, desc: %q(Session's url)
|
|
|
|
optional :certificate, type: String, desc: %q(Session's certificate)
|
|
|
|
optional :authorization, type: String, desc: %q(Session's authorization)
|
|
|
|
end
|
2019-05-20 11:03:34 -04:00
|
|
|
optional :job_age, type: Integer, desc: %q(Job should be older than passed age in seconds to be ran on runner)
|
2017-02-15 12:08:29 -05:00
|
|
|
end
|
|
|
|
post '/request' do
|
|
|
|
authenticate_runner!
|
2018-06-17 10:47:25 -04:00
|
|
|
|
|
|
|
unless current_runner.active?
|
|
|
|
header 'X-GitLab-Last-Update', current_runner.ensure_runner_queue_value
|
|
|
|
break no_content!
|
|
|
|
end
|
2017-02-15 12:08:29 -05:00
|
|
|
|
2018-07-05 09:55:10 -04:00
|
|
|
runner_params = declared_params(include_missing: false)
|
|
|
|
|
|
|
|
if current_runner.runner_queue_value_latest?(runner_params[:last_update])
|
|
|
|
header 'X-GitLab-Last-Update', runner_params[:last_update]
|
2017-02-15 12:08:29 -05:00
|
|
|
Gitlab::Metrics.add_event(:build_not_found_cached)
|
2018-04-18 05:19:40 -04:00
|
|
|
break no_content!
|
2017-02-15 12:08:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
new_update = current_runner.ensure_runner_queue_value
|
2018-07-05 09:55:10 -04:00
|
|
|
result = ::Ci::RegisterJobService.new(current_runner).execute(runner_params)
|
2017-02-15 12:08:29 -05:00
|
|
|
|
|
|
|
if result.valid?
|
|
|
|
if result.build
|
2018-06-19 13:03:25 -04:00
|
|
|
Gitlab::Metrics.add_event(:build_found)
|
2018-07-27 01:04:35 -04:00
|
|
|
present Ci::BuildRunnerPresenter.new(result.build), with: Entities::JobRequest::Response
|
2017-02-15 12:08:29 -05:00
|
|
|
else
|
|
|
|
Gitlab::Metrics.add_event(:build_not_found)
|
|
|
|
header 'X-GitLab-Last-Update', new_update
|
2017-03-15 22:31:09 -04:00
|
|
|
no_content!
|
2017-02-15 12:08:29 -05:00
|
|
|
end
|
|
|
|
else
|
|
|
|
# We received build that is invalid due to concurrency conflict
|
|
|
|
Gitlab::Metrics.add_event(:build_invalid)
|
|
|
|
conflict!
|
|
|
|
end
|
|
|
|
end
|
2017-02-28 06:52:08 -05:00
|
|
|
|
|
|
|
desc 'Updates a job' do
|
|
|
|
http_codes [[200, 'Job was updated'], [403, 'Forbidden']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 07:29:52 -05:00
|
|
|
requires :token, type: String, desc: %q(Runners's authentication token)
|
2017-02-28 14:25:58 -05:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 06:52:08 -05:00
|
|
|
optional :trace, type: String, desc: %q(Job's full trace)
|
|
|
|
optional :state, type: String, desc: %q(Job's status: success, failed)
|
2018-10-26 04:01:28 -04:00
|
|
|
optional :failure_reason, type: String, desc: %q(Job's failure_reason)
|
2017-02-28 06:52:08 -05:00
|
|
|
end
|
|
|
|
put '/:id' do
|
2017-03-31 07:56:27 -04:00
|
|
|
job = authenticate_job!
|
2018-06-07 11:27:26 -04:00
|
|
|
job_forbidden!(job, 'Job is not running') unless job.running?
|
2017-02-28 06:52:08 -05:00
|
|
|
|
2017-04-06 12:20:27 -04:00
|
|
|
job.trace.set(params[:trace]) if params[:trace]
|
2017-02-28 06:52:08 -05:00
|
|
|
|
2018-06-19 13:03:25 -04:00
|
|
|
Gitlab::Metrics.add_event(:update_build)
|
2017-02-28 06:52:08 -05:00
|
|
|
|
|
|
|
case params[:state].to_s
|
2018-06-07 11:27:26 -04:00
|
|
|
when 'running'
|
|
|
|
job.touch if job.needs_touch?
|
2017-02-28 06:52:08 -05:00
|
|
|
when 'success'
|
2018-05-27 08:23:37 -04:00
|
|
|
job.success!
|
2017-02-28 06:52:08 -05:00
|
|
|
when 'failed'
|
2018-05-27 08:23:37 -04:00
|
|
|
job.drop!(params[:failure_reason] || :unknown_failure)
|
2017-02-28 06:52:08 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-28 07:29:52 -05:00
|
|
|
|
2017-03-07 06:30:34 -05:00
|
|
|
desc 'Appends a patch to the job trace' do
|
2017-02-28 07:29:52 -05:00
|
|
|
http_codes [[202, 'Trace was patched'],
|
|
|
|
[400, 'Missing Content-Range header'],
|
|
|
|
[403, 'Forbidden'],
|
|
|
|
[416, 'Range not satisfiable']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 14:25:58 -05:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 07:29:52 -05:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
|
|
|
end
|
|
|
|
patch '/:id/trace' do
|
2017-03-31 07:56:27 -04:00
|
|
|
job = authenticate_job!
|
2018-06-07 11:27:26 -04:00
|
|
|
job_forbidden!(job, 'Job is not running') unless job.running?
|
2017-02-28 07:29:52 -05:00
|
|
|
|
2017-06-02 13:11:26 -04:00
|
|
|
error!('400 Missing header Content-Range', 400) unless request.headers.key?('Content-Range')
|
2017-02-28 07:29:52 -05:00
|
|
|
content_range = request.headers['Content-Range']
|
|
|
|
content_range = content_range.split('-')
|
|
|
|
|
2018-05-06 13:54:41 -04:00
|
|
|
# TODO:
|
|
|
|
# it seems that `Content-Range` as formatted by runner is wrong,
|
|
|
|
# the `byte_end` should point to final byte, but it points byte+1
|
|
|
|
# that means that we have to calculate end of body,
|
|
|
|
# as we cannot use `content_length[1]`
|
|
|
|
# Issue: https://gitlab.com/gitlab-org/gitlab-runner/issues/3275
|
|
|
|
|
|
|
|
body_data = request.body.read
|
|
|
|
body_start = content_range[0].to_i
|
|
|
|
body_end = body_start + body_data.bytesize
|
|
|
|
|
2018-05-18 03:05:11 -04:00
|
|
|
stream_size = job.trace.append(body_data, body_start)
|
2018-05-06 13:54:41 -04:00
|
|
|
unless stream_size == body_end
|
|
|
|
break error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{stream_size}" })
|
2017-02-28 07:29:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
status 202
|
|
|
|
header 'Job-Status', job.status
|
2017-04-06 12:20:27 -04:00
|
|
|
header 'Range', "0-#{stream_size}"
|
2020-04-01 08:08:00 -04:00
|
|
|
header 'X-GitLab-Trace-Update-Interval', job.trace.update_interval.to_s
|
2017-02-28 07:29:52 -05:00
|
|
|
end
|
2017-02-28 08:27:25 -05:00
|
|
|
|
|
|
|
desc 'Authorize artifacts uploading for job' do
|
|
|
|
http_codes [[200, 'Upload allowed'],
|
|
|
|
[403, 'Forbidden'],
|
|
|
|
[405, 'Artifacts support not enabled'],
|
|
|
|
[413, 'File too large']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 14:25:58 -05:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 08:27:25 -05:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
2017-02-28 14:25:58 -05:00
|
|
|
optional :filesize, type: Integer, desc: %q(Artifacts filesize)
|
2017-02-28 08:27:25 -05:00
|
|
|
end
|
|
|
|
post '/:id/artifacts/authorize' do
|
|
|
|
not_allowed! unless Gitlab.config.artifacts.enabled
|
|
|
|
require_gitlab_workhorse!
|
|
|
|
Gitlab::Workhorse.verify_api_request!(headers)
|
|
|
|
|
2017-03-31 07:56:27 -04:00
|
|
|
job = authenticate_job!
|
2017-02-28 08:27:25 -05:00
|
|
|
forbidden!('Job is not running') unless job.running?
|
|
|
|
|
2019-10-08 08:06:01 -04:00
|
|
|
max_size = max_artifacts_size(job)
|
|
|
|
|
2017-02-28 08:27:25 -05:00
|
|
|
if params[:filesize]
|
|
|
|
file_size = params[:filesize].to_i
|
2019-10-08 08:06:01 -04:00
|
|
|
file_too_large! unless file_size < max_size
|
2017-02-28 08:27:25 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
status 200
|
|
|
|
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
|
2019-10-08 08:06:01 -04:00
|
|
|
JobArtifactUploader.workhorse_authorize(has_length: false, maximum_size: max_size)
|
2017-02-28 08:27:25 -05:00
|
|
|
end
|
2017-02-28 11:36:17 -05:00
|
|
|
|
|
|
|
desc 'Upload artifacts for job' do
|
2017-03-02 11:44:15 -05:00
|
|
|
success Entities::JobRequest::Response
|
2017-02-28 11:36:17 -05:00
|
|
|
http_codes [[201, 'Artifact uploaded'],
|
|
|
|
[400, 'Bad request'],
|
|
|
|
[403, 'Forbidden'],
|
|
|
|
[405, 'Artifacts support not enabled'],
|
|
|
|
[413, 'File too large']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 14:25:58 -05:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2020-04-14 23:09:11 -04:00
|
|
|
requires :file, type: ::API::Validations::Types::WorkhorseFile, desc: %(The artifact file to store (generated by Multipart middleware))
|
2017-02-28 11:36:17 -05:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
|
|
|
optional :expire_in, type: String, desc: %q(Specify when artifacts should expire)
|
2018-07-27 01:04:35 -04:00
|
|
|
optional :artifact_type, type: String, desc: %q(The type of artifact),
|
|
|
|
default: 'archive', values: Ci::JobArtifact.file_types.keys
|
|
|
|
optional :artifact_format, type: String, desc: %q(The format of artifact),
|
|
|
|
default: 'zip', values: Ci::JobArtifact.file_formats.keys
|
2020-04-14 23:09:11 -04:00
|
|
|
optional :metadata, type: ::API::Validations::Types::WorkhorseFile, desc: %(The artifact metadata to store (generated by Multipart middleware))
|
2017-02-28 11:36:17 -05:00
|
|
|
end
|
|
|
|
post '/:id/artifacts' do
|
|
|
|
not_allowed! unless Gitlab.config.artifacts.enabled
|
|
|
|
require_gitlab_workhorse!
|
|
|
|
|
2017-03-31 07:56:27 -04:00
|
|
|
job = authenticate_job!
|
2017-02-28 11:36:17 -05:00
|
|
|
forbidden!('Job is not running!') unless job.running?
|
|
|
|
|
2020-04-14 23:09:11 -04:00
|
|
|
artifacts = params[:file]
|
|
|
|
metadata = params[:metadata]
|
2017-02-28 11:36:17 -05:00
|
|
|
|
2019-10-08 08:06:01 -04:00
|
|
|
file_too_large! unless artifacts.size < max_artifacts_size(job)
|
2017-02-28 11:36:17 -05:00
|
|
|
|
2020-02-26 10:08:56 -05:00
|
|
|
result = Ci::CreateJobArtifactsService.new(job.project).execute(job, artifacts, params, metadata_file: metadata)
|
|
|
|
|
|
|
|
if result[:status] == :success
|
2020-02-05 07:09:15 -05:00
|
|
|
status :created
|
2017-02-28 11:36:17 -05:00
|
|
|
else
|
2020-02-26 10:08:56 -05:00
|
|
|
render_api_error!(result[:message], result[:http_status])
|
2017-02-28 11:36:17 -05:00
|
|
|
end
|
|
|
|
end
|
2017-02-28 11:55:56 -05:00
|
|
|
|
|
|
|
desc 'Download the artifacts file for job' do
|
|
|
|
http_codes [[200, 'Upload allowed'],
|
|
|
|
[403, 'Forbidden'],
|
|
|
|
[404, 'Artifact not found']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 14:25:58 -05:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 11:55:56 -05:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
2018-02-06 10:39:20 -05:00
|
|
|
optional :direct_download, default: false, type: Boolean, desc: %q(Perform direct download from remote storage instead of proxying artifacts)
|
2017-02-28 11:55:56 -05:00
|
|
|
end
|
|
|
|
get '/:id/artifacts' do
|
2017-03-31 07:56:27 -04:00
|
|
|
job = authenticate_job!
|
2017-02-28 11:55:56 -05:00
|
|
|
|
2018-03-09 09:16:06 -05:00
|
|
|
present_carrierwave_file!(job.artifacts_file, supports_direct_download: params[:direct_download])
|
2017-02-28 11:55:56 -05:00
|
|
|
end
|
2017-02-15 12:08:29 -05:00
|
|
|
end
|
2017-02-14 17:52:02 -05:00
|
|
|
end
|
|
|
|
end
|