2017-02-14 22:52:02 +00:00
|
|
|
module API
|
2017-02-16 00:30:46 +00:00
|
|
|
class Runner < Grape::API
|
|
|
|
helpers ::API::Helpers::Runner
|
2017-02-14 22:52:02 +00: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)
|
|
|
|
optional :locked, type: Boolean, desc: 'Should Runner be locked for current project'
|
|
|
|
optional :run_untagged, type: Boolean, desc: 'Should Runner handle untagged jobs'
|
|
|
|
optional :tag_list, type: Array[String], desc: %q(List of Runner's tags)
|
|
|
|
end
|
|
|
|
post '/' do
|
|
|
|
attributes = attributes_for_keys [:description, :locked, :run_untagged, :tag_list]
|
|
|
|
|
|
|
|
runner =
|
|
|
|
if runner_registration_token_valid?
|
|
|
|
# Create shared runner. Requires admin access
|
2017-02-16 00:30:46 +00:00
|
|
|
Ci::Runner.create(attributes.merge(is_shared: true))
|
2017-02-14 22:52:02 +00:00
|
|
|
elsif project = Project.find_by(runners_token: params[:token])
|
|
|
|
# Create a specific runner for project.
|
|
|
|
project.runners.create(attributes)
|
|
|
|
end
|
|
|
|
|
|
|
|
return forbidden! unless runner
|
|
|
|
|
|
|
|
if runner.id
|
|
|
|
runner.update(get_runner_version_from_params)
|
|
|
|
present runner, with: Entities::RunnerRegistrationDetails
|
|
|
|
else
|
|
|
|
not_found!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
desc 'Deletes a registered Runner' do
|
2017-02-27 16:02:25 +00:00
|
|
|
http_codes [[204, 'Runner was deleted'], [403, 'Forbidden']]
|
2017-02-14 22:52:02 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: %q(Runner's authentication token)
|
|
|
|
end
|
|
|
|
delete '/' do
|
|
|
|
authenticate_runner!
|
2017-07-20 13:33:18 +00:00
|
|
|
status 204
|
2017-02-16 00:30:46 +00:00
|
|
|
Ci::Runner.find_by_token(params[:token]).destroy
|
2017-02-14 22:52:02 +00:00
|
|
|
end
|
2017-03-14 23:27:10 +00: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 22:52:02 +00:00
|
|
|
end
|
2017-02-15 17:08:29 +00:00
|
|
|
|
|
|
|
resource :jobs do
|
|
|
|
desc 'Request a job' do
|
2017-02-16 00:05:44 +00:00
|
|
|
success Entities::JobRequest::Response
|
2017-03-16 02:31:09 +00:00
|
|
|
http_codes [[201, 'Job was scheduled'],
|
|
|
|
[204, 'No job for Runner'],
|
|
|
|
[403, 'Forbidden']]
|
2017-02-15 17:08:29 +00:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :token, type: String, desc: %q(Runner's authentication token)
|
2017-02-27 13:21:06 +00:00
|
|
|
optional :last_update, type: String, desc: %q(Runner's queue last_update token)
|
|
|
|
optional :info, type: Hash, desc: %q(Runner's metadata)
|
2017-02-15 17:08:29 +00:00
|
|
|
end
|
|
|
|
post '/request' do
|
|
|
|
authenticate_runner!
|
2017-03-16 02:31:09 +00:00
|
|
|
no_content! unless current_runner.active?
|
2017-02-15 17:08:29 +00:00
|
|
|
update_runner_info
|
|
|
|
|
|
|
|
if current_runner.is_runner_queue_value_latest?(params[:last_update])
|
|
|
|
header 'X-GitLab-Last-Update', params[:last_update]
|
|
|
|
Gitlab::Metrics.add_event(:build_not_found_cached)
|
2017-03-16 02:31:09 +00:00
|
|
|
return no_content!
|
2017-02-15 17:08:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
new_update = current_runner.ensure_runner_queue_value
|
2017-02-16 00:05:44 +00:00
|
|
|
result = ::Ci::RegisterJobService.new(current_runner).execute
|
2017-02-15 17:08:29 +00:00
|
|
|
|
|
|
|
if result.valid?
|
|
|
|
if result.build
|
|
|
|
Gitlab::Metrics.add_event(:build_found,
|
|
|
|
project: result.build.project.path_with_namespace)
|
2017-02-16 00:05:44 +00:00
|
|
|
present result.build, with: Entities::JobRequest::Response
|
2017-02-15 17:08:29 +00:00
|
|
|
else
|
|
|
|
Gitlab::Metrics.add_event(:build_not_found)
|
|
|
|
header 'X-GitLab-Last-Update', new_update
|
2017-03-16 02:31:09 +00:00
|
|
|
no_content!
|
2017-02-15 17:08:29 +00: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 11:52:08 +00:00
|
|
|
|
|
|
|
desc 'Updates a job' do
|
|
|
|
http_codes [[200, 'Job was updated'], [403, 'Forbidden']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 12:29:52 +00:00
|
|
|
requires :token, type: String, desc: %q(Runners's authentication token)
|
2017-02-28 19:25:58 +00:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 11:52:08 +00:00
|
|
|
optional :trace, type: String, desc: %q(Job's full trace)
|
|
|
|
optional :state, type: String, desc: %q(Job's status: success, failed)
|
|
|
|
end
|
|
|
|
put '/:id' do
|
2017-03-31 11:56:27 +00:00
|
|
|
job = authenticate_job!
|
2017-02-28 11:52:08 +00:00
|
|
|
|
2017-04-06 16:20:27 +00:00
|
|
|
job.trace.set(params[:trace]) if params[:trace]
|
2017-02-28 11:52:08 +00:00
|
|
|
|
|
|
|
Gitlab::Metrics.add_event(:update_build,
|
|
|
|
project: job.project.path_with_namespace)
|
|
|
|
|
|
|
|
case params[:state].to_s
|
|
|
|
when 'success'
|
|
|
|
job.success
|
|
|
|
when 'failed'
|
|
|
|
job.drop
|
|
|
|
end
|
|
|
|
end
|
2017-02-28 12:29:52 +00:00
|
|
|
|
2017-03-07 11:30:34 +00:00
|
|
|
desc 'Appends a patch to the job trace' do
|
2017-02-28 12:29:52 +00:00
|
|
|
http_codes [[202, 'Trace was patched'],
|
|
|
|
[400, 'Missing Content-Range header'],
|
|
|
|
[403, 'Forbidden'],
|
|
|
|
[416, 'Range not satisfiable']]
|
|
|
|
end
|
|
|
|
params do
|
2017-02-28 19:25:58 +00:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 12:29:52 +00:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
|
|
|
end
|
|
|
|
patch '/:id/trace' do
|
2017-03-31 11:56:27 +00:00
|
|
|
job = authenticate_job!
|
2017-02-28 12:29:52 +00:00
|
|
|
|
2017-06-02 17:11:26 +00:00
|
|
|
error!('400 Missing header Content-Range', 400) unless request.headers.key?('Content-Range')
|
2017-02-28 12:29:52 +00:00
|
|
|
content_range = request.headers['Content-Range']
|
|
|
|
content_range = content_range.split('-')
|
|
|
|
|
2017-04-06 16:20:27 +00:00
|
|
|
stream_size = job.trace.append(request.body.read, content_range[0].to_i)
|
|
|
|
if stream_size < 0
|
|
|
|
return error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{-stream_size}" })
|
2017-02-28 12:29:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
status 202
|
|
|
|
header 'Job-Status', job.status
|
2017-04-06 16:20:27 +00:00
|
|
|
header 'Range', "0-#{stream_size}"
|
2017-02-28 12:29:52 +00:00
|
|
|
end
|
2017-02-28 13:27:25 +00: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 19:25:58 +00:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 13:27:25 +00:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
2017-02-28 19:25:58 +00:00
|
|
|
optional :filesize, type: Integer, desc: %q(Artifacts filesize)
|
2017-02-28 13:27:25 +00: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 11:56:27 +00:00
|
|
|
job = authenticate_job!
|
2017-02-28 13:27:25 +00:00
|
|
|
forbidden!('Job is not running') unless job.running?
|
|
|
|
|
|
|
|
if params[:filesize]
|
|
|
|
file_size = params[:filesize].to_i
|
|
|
|
file_to_large! unless file_size < max_artifacts_size
|
|
|
|
end
|
|
|
|
|
|
|
|
status 200
|
|
|
|
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
|
|
|
|
Gitlab::Workhorse.artifact_upload_ok
|
|
|
|
end
|
2017-02-28 16:36:17 +00:00
|
|
|
|
|
|
|
desc 'Upload artifacts for job' do
|
2017-03-02 16:44:15 +00:00
|
|
|
success Entities::JobRequest::Response
|
2017-02-28 16:36:17 +00: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 19:25:58 +00:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 16:36:17 +00:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
|
|
|
optional :expire_in, type: String, desc: %q(Specify when artifacts should expire)
|
2017-03-02 16:44:15 +00:00
|
|
|
optional :file, type: File, desc: %q(Artifact's file)
|
2017-02-28 16:36:17 +00:00
|
|
|
optional 'file.path', type: String, desc: %q(path to locally stored body (generated by Workhorse))
|
|
|
|
optional 'file.name', type: String, desc: %q(real filename as send in Content-Disposition (generated by Workhorse))
|
|
|
|
optional 'file.type', type: String, desc: %q(real content type as send in Content-Type (generated by Workhorse))
|
|
|
|
optional 'metadata.path', type: String, desc: %q(path to locally stored body (generated by Workhorse))
|
|
|
|
optional 'metadata.name', type: String, desc: %q(filename (generated by Workhorse))
|
|
|
|
end
|
|
|
|
post '/:id/artifacts' do
|
|
|
|
not_allowed! unless Gitlab.config.artifacts.enabled
|
|
|
|
require_gitlab_workhorse!
|
|
|
|
|
2017-03-31 11:56:27 +00:00
|
|
|
job = authenticate_job!
|
2017-02-28 16:36:17 +00:00
|
|
|
forbidden!('Job is not running!') unless job.running?
|
|
|
|
|
|
|
|
artifacts_upload_path = ArtifactUploader.artifacts_upload_path
|
|
|
|
artifacts = uploaded_file(:file, artifacts_upload_path)
|
|
|
|
metadata = uploaded_file(:metadata, artifacts_upload_path)
|
|
|
|
|
|
|
|
bad_request!('Missing artifacts file!') unless artifacts
|
|
|
|
file_to_large! unless artifacts.size < max_artifacts_size
|
|
|
|
|
|
|
|
job.artifacts_file = artifacts
|
|
|
|
job.artifacts_metadata = metadata
|
|
|
|
job.artifacts_expire_in = params['expire_in'] ||
|
2017-02-28 19:25:58 +00:00
|
|
|
Gitlab::CurrentSettings.current_application_settings.default_artifacts_expire_in
|
2017-02-28 16:36:17 +00:00
|
|
|
|
|
|
|
if job.save
|
|
|
|
present job, with: Entities::JobRequest::Response
|
|
|
|
else
|
|
|
|
render_validation_error!(job)
|
|
|
|
end
|
|
|
|
end
|
2017-02-28 16:55:56 +00: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 19:25:58 +00:00
|
|
|
requires :id, type: Integer, desc: %q(Job's ID)
|
2017-02-28 16:55:56 +00:00
|
|
|
optional :token, type: String, desc: %q(Job's authentication token)
|
|
|
|
end
|
|
|
|
get '/:id/artifacts' do
|
2017-03-31 11:56:27 +00:00
|
|
|
job = authenticate_job!
|
2017-02-28 16:55:56 +00:00
|
|
|
|
2017-06-01 07:52:19 +00:00
|
|
|
present_artifacts!(job.artifacts_file)
|
2017-02-28 16:55:56 +00:00
|
|
|
end
|
2017-02-15 17:08:29 +00:00
|
|
|
end
|
2017-02-14 22:52:02 +00:00
|
|
|
end
|
|
|
|
end
|