debian-mirror-gitlab/lib/api/ci/runners.rb

422 lines
18 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module API
module Ci
2021-01-03 14:25:43 +05:30
class Runners < ::API::Base
2020-07-28 23:09:34 +05:30
include PaginationParams
before { authenticate! }
2021-09-30 23:02:18 +05:30
feature_category :runner
2022-07-16 23:28:13 +05:30
urgency :low
2021-01-29 00:20:46 +05:30
2023-01-13 00:05:48 +05:30
helpers do
params :deprecated_filter_params do
optional :scope, type: String, values: ::Ci::Runner::AVAILABLE_SCOPES,
2023-04-23 21:23:45 +05:30
desc: 'Deprecated: Use `type` or `status` instead. The scope of runners to return'
2023-01-13 00:05:48 +05:30
end
params :filter_params do
optional :type, type: String, values: ::Ci::Runner::AVAILABLE_TYPES, desc: 'The type of runners to return'
optional :paused, type: Boolean,
desc: 'Whether to include only runners that are accepting or ignoring new jobs'
optional :status, type: String, values: ::Ci::Runner::AVAILABLE_STATUSES,
desc: 'The status of runners to return'
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce,
desc: 'A list of runner tags', documentation: { example: "['macos', 'shell']" }
use :pagination
end
def filter_runners(runners, scope, allowed_scopes: ::Ci::Runner::AVAILABLE_SCOPES)
return runners unless scope.present?
unless allowed_scopes.include?(scope)
render_api_error!('Scope contains invalid value', 400)
end
# Support deprecated scopes
if runners.respond_to?("deprecated_#{scope}")
scope = "deprecated_#{scope}"
end
runners.public_send(scope) # rubocop:disable GitlabSecurity/PublicSend
end
def apply_filter(runners, params)
runners = filter_runners(runners, params[:type], allowed_scopes: ::Ci::Runner::AVAILABLE_TYPES)
runners = filter_runners(runners, params[:status], allowed_scopes: ::Ci::Runner::AVAILABLE_STATUSES)
runners = filter_runners(runners, params[:paused] ? 'paused' : 'active', allowed_scopes: %w[paused active]) if params.include?(:paused)
runners = runners.tagged_with(params[:tag_list]) if params[:tag_list]
runners
end
def get_runner(id)
runner = ::Ci::Runner.find(id)
not_found!('Runner') unless runner
runner
end
def authenticate_show_runner!(runner)
2023-03-04 22:38:38 +05:30
return if runner.instance_type? || current_user.can_read_all_resources?
2023-01-13 00:05:48 +05:30
forbidden!("No access granted") unless can?(current_user, :read_runner, runner)
end
def authenticate_update_runner!(runner)
2023-03-04 22:38:38 +05:30
return if current_user.can_admin_all_resources?
2023-01-13 00:05:48 +05:30
forbidden!("No access granted") unless can?(current_user, :update_runner, runner)
end
def authenticate_delete_runner!(runner)
2023-03-04 22:38:38 +05:30
return if current_user.can_admin_all_resources?
2023-01-13 00:05:48 +05:30
forbidden!("Runner associated with more than one project") if runner.runner_projects.count > 1
forbidden!("No access granted") unless can?(current_user, :delete_runner, runner)
end
def authenticate_enable_runner!(runner)
forbidden!("Runner is a group runner") if runner.group_type?
2023-03-04 22:38:38 +05:30
return if current_user.can_admin_all_resources?
2023-01-13 00:05:48 +05:30
forbidden!("Runner is locked") if runner.locked?
forbidden!("No access granted") unless can?(current_user, :assign_runner, runner)
end
def authenticate_list_runners_jobs!(runner)
2023-03-04 22:38:38 +05:30
return if current_user.can_read_all_resources?
2023-01-13 00:05:48 +05:30
forbidden!("No access granted") unless can?(current_user, :read_builds, runner)
end
end
2020-07-28 23:09:34 +05:30
resource :runners do
desc 'Get runners available for user' do
2023-01-13 00:05:48 +05:30
summary 'List owned runners'
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Scope contains invalid value'], [401, 'Unauthorized']]
tags %w[runners]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
use :deprecated_filter_params
use :filter_params
2020-07-28 23:09:34 +05:30
end
get do
runners = current_user.ci_owned_runners
runners = filter_runners(runners, params[:scope], allowed_scopes: ::Ci::Runner::AVAILABLE_STATUSES)
2022-04-04 11:22:00 +05:30
runners = apply_filter(runners, params)
2020-07-28 23:09:34 +05:30
2021-09-30 23:02:18 +05:30
present paginate(runners), with: Entities::Ci::Runner
2020-07-28 23:09:34 +05:30
end
2023-04-23 21:23:45 +05:30
desc 'Get all runners - shared and project' do
2023-01-13 00:05:48 +05:30
summary 'List all runners'
2023-04-23 21:23:45 +05:30
detail 'Get a list of all runners in the GitLab instance (shared and project). ' \
2023-01-13 00:05:48 +05:30
'Access is restricted to users with administrator access.'
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Scope contains invalid value'], [401, 'Unauthorized']]
tags %w[runners]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
use :deprecated_filter_params
use :filter_params
2020-07-28 23:09:34 +05:30
end
get 'all' do
authenticated_as_admin!
runners = ::Ci::Runner.all
runners = filter_runners(runners, params[:scope])
2022-04-04 11:22:00 +05:30
runners = apply_filter(runners, params)
2020-07-28 23:09:34 +05:30
2021-09-30 23:02:18 +05:30
present paginate(runners), with: Entities::Ci::Runner
2020-07-28 23:09:34 +05:30
end
desc "Get runner's details" do
2023-01-13 00:05:48 +05:30
detail 'At least the Maintainer role is required to get runner details at the project and group level. ' \
'Instance-level runner details via this endpoint are available to all signed in users.'
2021-09-30 23:02:18 +05:30
success Entities::Ci::RunnerDetails
2023-01-13 00:05:48 +05:30
failure [[401, 'Unauthorized'], [403, 'No access granted'], [404, 'Runner not found']]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
end
get ':id' do
runner = get_runner(params[:id])
authenticate_show_runner!(runner)
2021-09-30 23:02:18 +05:30
present runner, with: Entities::Ci::RunnerDetails, current_user: current_user
2020-07-28 23:09:34 +05:30
end
desc "Update runner's details" do
2023-01-13 00:05:48 +05:30
summary "Update details of a runner"
2021-09-30 23:02:18 +05:30
success Entities::Ci::RunnerDetails
2023-01-13 00:05:48 +05:30
failure [[400, 'Bad Request'], [401, 'Unauthorized'], [403, 'No access granted'], [404, 'Runner not found']]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
optional :description, type: String, desc: 'The description of the runner'
2023-01-13 00:05:48 +05:30
optional :active, type: Boolean, desc: 'Deprecated: Use `paused` instead. Flag indicating whether the runner is allowed to receive jobs'
2023-06-20 00:43:36 +05:30
optional :paused, type: Boolean, desc: 'Specifies if the runner should ignore new jobs'
2023-01-13 00:05:48 +05:30
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce,
desc: 'The list of tags for a runner', documentation: { example: "['macos', 'shell']" }
2023-06-20 00:43:36 +05:30
optional :run_untagged, type: Boolean, desc: 'Specifies if the runner can execute untagged jobs'
optional :locked, type: Boolean, desc: 'Specifies if the runner is locked'
2020-07-28 23:09:34 +05:30
optional :access_level, type: String, values: ::Ci::Runner.access_levels.keys,
2023-01-13 00:05:48 +05:30
desc: 'The access level of the runner'
optional :maximum_timeout, type: Integer,
desc: 'Maximum timeout that limits the amount of time (in seconds) ' \
'that runners can run jobs'
2022-04-04 11:22:00 +05:30
at_least_one_of :description, :active, :paused, :tag_list, :run_untagged, :locked, :access_level, :maximum_timeout
mutually_exclusive :active, :paused
2020-07-28 23:09:34 +05:30
end
put ':id' do
runner = get_runner(params.delete(:id))
authenticate_update_runner!(runner)
2022-04-04 11:22:00 +05:30
params[:active] = !params.delete(:paused) if params.include?(:paused)
2022-05-07 20:08:51 +05:30
update_service = ::Ci::Runners::UpdateRunnerService.new(runner)
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
if update_service.execute(declared_params(include_missing: false)).success?
2021-09-30 23:02:18 +05:30
present runner, with: Entities::Ci::RunnerDetails, current_user: current_user
2020-07-28 23:09:34 +05:30
else
render_validation_error!(runner)
end
end
desc 'Remove a runner' do
2023-01-13 00:05:48 +05:30
summary 'Delete a runner'
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[401, 'Unauthorized'], [403, 'No access granted'],
[403, 'Runner associated with more than one project'], [404, 'Runner not found'],
[412, 'Precondition Failed']]
tags %w[runners]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
end
delete ':id' do
runner = get_runner(params[:id])
authenticate_delete_runner!(runner)
2022-05-07 20:08:51 +05:30
destroy_conditionally!(runner) { ::Ci::Runners::UnregisterRunnerService.new(runner, current_user).execute }
2020-07-28 23:09:34 +05:30
end
desc 'List jobs running on a runner' do
2023-01-13 00:05:48 +05:30
summary "List runner's jobs"
detail 'List jobs that are being processed or were processed by the specified runner. ' \
'The list of jobs is limited to projects where the user has at least the Reporter role.'
2020-10-24 23:57:45 +05:30
success Entities::Ci::JobBasicWithProject
2023-01-13 00:05:48 +05:30
failure [[401, 'Unauthorized'], [403, 'No access granted'], [404, 'Runner not found']]
tags %w[runners jobs]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
optional :status, type: String, desc: 'Status of the job', values: ::Ci::Build::AVAILABLE_STATUSES
2023-01-13 00:05:48 +05:30
optional :order_by, type: String, desc: 'Order by `id`', values: ::Ci::RunnerJobsFinder::ALLOWED_INDEXED_COLUMNS
optional :sort, type: String, values: %w[asc desc], default: 'desc', desc: 'Sort by `asc` or `desc` order. ' \
'Specify `order_by` as well, including for `id`'
2020-07-28 23:09:34 +05:30
use :pagination
end
get ':id/jobs' do
runner = get_runner(params[:id])
authenticate_list_runners_jobs!(runner)
2022-07-01 11:34:44 +05:30
jobs = ::Ci::RunnerJobsFinder.new(runner, current_user, params).execute
2022-10-11 01:57:18 +05:30
jobs = jobs.preload( # rubocop: disable CodeReuse/ActiveRecord
[
:user,
{ pipeline: { project: [:route, { namespace: :route }] } },
{ project: [:route, { namespace: :route }] }
]
)
jobs = paginate(jobs)
jobs.each(&:commit) # batch loads all commits in the page
2020-07-28 23:09:34 +05:30
2022-10-11 01:57:18 +05:30
present jobs, with: Entities::Ci::JobBasicWithProject
2020-07-28 23:09:34 +05:30
end
2021-11-18 22:05:49 +05:30
desc 'Reset runner authentication token' do
2023-01-13 00:05:48 +05:30
summary "Reset runner's authentication token"
2021-11-18 22:05:49 +05:30
success Entities::Ci::ResetTokenResult
2023-01-13 00:05:48 +05:30
failure [[403, 'No access granted'], [404, 'Runner not found']]
tags %w[runners]
2021-11-18 22:05:49 +05:30
end
params do
requires :id, type: Integer, desc: 'The ID of the runner'
end
post ':id/reset_authentication_token' do
runner = get_runner(params[:id])
authenticate_update_runner!(runner)
runner.reset_token!
2022-04-04 11:22:00 +05:30
present runner.token_with_expiration, with: Entities::Ci::ResetTokenResult
2021-11-18 22:05:49 +05:30
end
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :id,
types: [String, Integer],
desc: 'The ID or URL-encoded path of the project owned by the authenticated user'
2020-07-28 23:09:34 +05:30
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
before { authorize_admin_project }
desc 'Get runners available for project' do
2023-01-13 00:05:48 +05:30
summary "List project's runners"
detail 'List all runners available in the project, including from ancestor groups ' \
'and any allowed shared runners.'
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Scope contains invalid value'], [403, 'No access granted']]
tags %w[runners projects]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
use :deprecated_filter_params
use :filter_params
2020-07-28 23:09:34 +05:30
end
get ':id/runners' do
runners = ::Ci::Runner.owned_or_instance_wide(user_project.id)
# scope is deprecated (for project runners), however api documentation still supports it.
# Not including them in `apply_filter` method as it's not supported for group runners
runners = filter_runners(runners, params[:scope])
runners = apply_filter(runners, params)
2021-09-30 23:02:18 +05:30
present paginate(runners), with: Entities::Ci::Runner
2020-07-28 23:09:34 +05:30
end
2023-01-13 00:05:48 +05:30
desc 'Enable a runner in project' do
2023-04-23 21:23:45 +05:30
detail "Enable an available project runner in the project."
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Bad Request'],
[403, 'No access granted'], [403, 'Runner is a group runner'], [403, 'Runner is locked'],
[404, 'Runner not found']]
tags %w[runners projects]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :runner_id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
end
post ':id/runners' do
runner = get_runner(params[:runner_id])
authenticate_enable_runner!(runner)
2022-08-27 11:52:29 +05:30
if ::Ci::Runners::AssignRunnerService.new(runner, user_project, current_user).execute.success?
2021-09-30 23:02:18 +05:30
present runner, with: Entities::Ci::Runner
2020-07-28 23:09:34 +05:30
else
render_validation_error!(runner)
end
end
desc "Disable project's runner" do
2023-04-23 21:23:45 +05:30
summary "Disable a project runner from the project"
2023-01-13 00:05:48 +05:30
detail "It works only if the project isn't the only project associated with the specified runner. " \
"If so, an error is returned. Use the call to delete a runner instead."
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Bad Request'],
[403, 'Only one project associated with the runner. Please remove the runner instead'],
[404, 'Runner not found'], [412, 'Precondition Failed']]
tags %w[runners projects]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
requires :runner_id, type: Integer, desc: 'The ID of a runner'
2020-07-28 23:09:34 +05:30
end
# rubocop: disable CodeReuse/ActiveRecord
delete ':id/runners/:runner_id' do
runner_project = user_project.runner_projects.find_by(runner_id: params[:runner_id])
not_found!('Runner') unless runner_project
runner = runner_project.runner
2021-11-18 22:05:49 +05:30
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.runner_projects.count == 1
2020-07-28 23:09:34 +05:30
destroy_conditionally!(runner_project)
end
# rubocop: enable CodeReuse/ActiveRecord
end
params do
requires :id, type: String, desc: 'The ID of a group'
end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
before { authorize_admin_group }
desc 'Get runners available for group' do
2023-01-13 00:05:48 +05:30
summary "List group's runners"
detail 'List all runners available in the group as well as its ancestor groups, ' \
'including any allowed shared runners.'
2021-09-30 23:02:18 +05:30
success Entities::Ci::Runner
2023-01-13 00:05:48 +05:30
failure [[400, 'Scope contains invalid value'], [403, 'Forbidden']]
tags %w[runners groups]
2020-07-28 23:09:34 +05:30
end
params do
2023-01-13 00:05:48 +05:30
use :filter_params
2020-07-28 23:09:34 +05:30
end
get ':id/runners' do
2022-04-04 11:22:00 +05:30
runners = ::Ci::Runner.group_or_instance_wide(user_group)
2020-07-28 23:09:34 +05:30
runners = apply_filter(runners, params)
2021-09-30 23:02:18 +05:30
present paginate(runners), with: Entities::Ci::Runner
2020-07-28 23:09:34 +05:30
end
end
2021-11-11 11:23:49 +05:30
resource :runners do
before { authenticate_non_get! }
2023-01-13 00:05:48 +05:30
desc 'Reset runner registration token' do
summary "Reset instance's runner registration token"
2021-11-18 22:05:49 +05:30
success Entities::Ci::ResetTokenResult
2023-01-13 00:05:48 +05:30
failure [[403, 'Forbidden']]
tags %w[runners groups]
2021-11-11 11:23:49 +05:30
end
post 'reset_registration_token' do
2022-05-07 20:08:51 +05:30
authorize! :update_runners_registration_token, ApplicationSetting.current
2021-11-11 11:23:49 +05:30
2022-05-07 20:08:51 +05:30
::Ci::Runners::ResetRegistrationTokenService.new(ApplicationSetting.current, current_user).execute
2022-04-04 11:22:00 +05:30
present ApplicationSetting.current_without_cache.runners_registration_token_with_expiration, with: Entities::Ci::ResetTokenResult
2021-11-11 11:23:49 +05:30
end
end
params do
requires :id, type: String, desc: 'The ID of a project'
end
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
before { authenticate_non_get! }
2023-01-13 00:05:48 +05:30
desc 'Reset runner registration token' do
summary "Reset the runner registration token for a project"
2021-11-18 22:05:49 +05:30
success Entities::Ci::ResetTokenResult
2023-01-13 00:05:48 +05:30
failure [[401, 'Unauthorized'], [403, 'Forbidden'], [404, 'Project Not Found']]
tags %w[runners projects]
2021-11-11 11:23:49 +05:30
end
post ':id/runners/reset_registration_token' do
project = find_project! user_project.id
authorize! :update_runners_registration_token, project
project.reset_runners_token!
2022-04-04 11:22:00 +05:30
present project.runners_token_with_expiration, with: Entities::Ci::ResetTokenResult
2021-11-11 11:23:49 +05:30
end
end
params do
requires :id, type: String, desc: 'The ID of a group'
end
resource :groups, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
before { authenticate_non_get! }
2023-01-13 00:05:48 +05:30
desc 'Reset runner registration token' do
summary "Reset the runner registration token for a group"
2021-11-18 22:05:49 +05:30
success Entities::Ci::ResetTokenResult
2023-01-13 00:05:48 +05:30
failure [[401, 'Unauthorized'], [403, 'Forbidden'], [404, 'Group Not Found']]
tags %w[runners groups]
2021-11-11 11:23:49 +05:30
end
post ':id/runners/reset_registration_token' do
group = find_group! user_group.id
authorize! :update_runners_registration_token, group
group.reset_runners_token!
2022-04-04 11:22:00 +05:30
present group.runners_token_with_expiration, with: Entities::Ci::ResetTokenResult
2021-11-11 11:23:49 +05:30
end
end
2020-07-28 23:09:34 +05:30
end
end
end