2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
module API
|
2020-04-27 14:09:41 -04:00
|
|
|
class Runners < Grape::API::Instance
|
2016-12-04 12:11:19 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :runners do
|
2016-11-09 10:29:07 -05:00
|
|
|
desc 'Get runners available for user' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
2018-06-07 11:43:23 -04:00
|
|
|
optional :scope, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
|
2016-11-09 10:29:07 -05:00
|
|
|
desc: 'The scope of specific runners to show'
|
2018-06-11 07:37:47 -04:00
|
|
|
optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES,
|
|
|
|
desc: 'The type of the runners to show'
|
2018-06-11 09:17:46 -04:00
|
|
|
optional :status, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
|
|
|
|
desc: 'The status of the runners to show'
|
2020-04-27 14:09:41 -04:00
|
|
|
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce, desc: 'The tags of the runners to show'
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-11-09 10:29:07 -05:00
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
get do
|
2018-06-11 07:37:47 -04:00
|
|
|
runners = current_user.ci_owned_runners
|
|
|
|
runners = filter_runners(runners, params[:scope], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES)
|
|
|
|
runners = filter_runners(runners, params[:type], allowed_scopes: Ci::Runner::AVAILABLE_TYPES)
|
2018-06-11 09:17:46 -04:00
|
|
|
runners = filter_runners(runners, params[:status], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES)
|
2018-06-11 12:04:46 -04:00
|
|
|
runners = runners.tagged_with(params[:tag_list]) if params[:tag_list]
|
2018-06-11 07:37:47 -04:00
|
|
|
|
2016-02-02 09:52:02 -05:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc 'Get all runners - shared and specific' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
2018-06-07 11:43:23 -04:00
|
|
|
optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES,
|
2016-11-09 10:29:07 -05:00
|
|
|
desc: 'The scope of specific runners to show'
|
2018-06-11 07:37:47 -04:00
|
|
|
optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES,
|
|
|
|
desc: 'The type of the runners to show'
|
2018-06-11 09:17:46 -04:00
|
|
|
optional :status, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
|
|
|
|
desc: 'The status of the runners to show'
|
2020-04-27 14:09:41 -04:00
|
|
|
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce, desc: 'The tags of the runners to show'
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-11-09 10:29:07 -05:00
|
|
|
end
|
2016-02-02 09:52:02 -05:00
|
|
|
get 'all' do
|
|
|
|
authenticated_as_admin!
|
2018-06-11 07:37:47 -04:00
|
|
|
|
|
|
|
runners = Ci::Runner.all
|
|
|
|
runners = filter_runners(runners, params[:scope])
|
|
|
|
runners = filter_runners(runners, params[:type], allowed_scopes: Ci::Runner::AVAILABLE_TYPES)
|
2018-06-11 09:17:46 -04:00
|
|
|
runners = filter_runners(runners, params[:status], allowed_scopes: Ci::Runner::AVAILABLE_STATUSES)
|
2018-06-11 12:04:46 -04:00
|
|
|
runners = runners.tagged_with(params[:tag_list]) if params[:tag_list]
|
2018-06-11 07:37:47 -04:00
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc "Get runner's details" do
|
|
|
|
success Entities::RunnerDetails
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the runner'
|
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
get ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_show_runner!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2016-02-10 06:26:56 -05:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc "Update runner's details" do
|
|
|
|
success Entities::RunnerDetails
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the runner'
|
|
|
|
optional :description, type: String, desc: 'The description of the runner'
|
|
|
|
optional :active, type: Boolean, desc: 'The state of a runner'
|
2020-04-27 14:09:41 -04:00
|
|
|
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce, desc: 'The list of tags for a runner'
|
2016-11-09 10:29:07 -05:00
|
|
|
optional :run_untagged, type: Boolean, desc: 'Flag indicating the runner can execute untagged jobs'
|
|
|
|
optional :locked, type: Boolean, desc: 'Flag indicating the runner is locked'
|
2017-08-29 03:09:30 -04:00
|
|
|
optional :access_level, type: String, values: Ci::Runner.access_levels.keys,
|
|
|
|
desc: 'The access_level of the runner'
|
2018-03-06 10:14:23 -05:00
|
|
|
optional :maximum_timeout, type: Integer, desc: 'Maximum timeout set when this Runner will handle the job'
|
2018-07-11 05:13:28 -04:00
|
|
|
at_least_one_of :description, :active, :tag_list, :run_untagged, :locked, :access_level, :maximum_timeout
|
2016-11-09 10:29:07 -05:00
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
put ':id' do
|
2016-11-09 10:29:07 -05:00
|
|
|
runner = get_runner(params.delete(:id))
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_update_runner!(runner)
|
2017-02-08 09:29:44 -05:00
|
|
|
update_service = Ci::UpdateRunnerService.new(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2017-02-08 09:29:44 -05:00
|
|
|
if update_service.update(declared_params(include_missing: false))
|
2016-02-10 08:20:51 -05:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 11:03:38 -05:00
|
|
|
else
|
|
|
|
render_validation_error!(runner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc 'Remove a runner' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the runner'
|
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
delete ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2017-03-01 08:35:48 -05:00
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_delete_runner!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
|
2017-03-02 07:14:13 -05:00
|
|
|
destroy_conditionally!(runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
2017-11-16 11:12:33 -05:00
|
|
|
|
2017-11-16 13:44:14 -05:00
|
|
|
desc 'List jobs running on a runner' do
|
2017-11-21 06:37:18 -05:00
|
|
|
success Entities::JobBasicWithProject
|
2017-11-16 13:44:14 -05:00
|
|
|
end
|
2017-11-16 11:12:33 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: Integer, desc: 'The ID of the runner'
|
2017-11-27 16:59:01 -05:00
|
|
|
optional :status, type: String, desc: 'Status of the job', values: Ci::Build::AVAILABLE_STATUSES
|
2020-03-13 20:09:30 -04:00
|
|
|
optional :order_by, type: String, desc: 'Order by `id` or not', values: Ci::RunnerJobsFinder::ALLOWED_INDEXED_COLUMNS
|
2019-06-12 16:03:21 -04:00
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc', desc: 'Sort by asc (ascending) or desc (descending)'
|
2017-11-16 11:12:33 -05:00
|
|
|
use :pagination
|
|
|
|
end
|
2018-09-20 12:47:34 -04:00
|
|
|
get ':id/jobs' do
|
2017-11-16 11:12:33 -05:00
|
|
|
runner = get_runner(params[:id])
|
|
|
|
authenticate_list_runners_jobs!(runner)
|
|
|
|
|
2020-03-13 20:09:30 -04:00
|
|
|
jobs = Ci::RunnerJobsFinder.new(runner, params).execute
|
2017-11-21 06:37:07 -05:00
|
|
|
|
2017-11-21 06:37:18 -05:00
|
|
|
present paginate(jobs), with: Entities::JobBasicWithProject
|
2017-11-16 11:12:33 -05:00
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-01-26 11:03:38 -05:00
|
|
|
before { authorize_admin_project }
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc 'Get runners available for project' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
2018-06-07 11:43:23 -04:00
|
|
|
optional :scope, type: String, values: Ci::Runner::AVAILABLE_SCOPES,
|
2016-11-09 10:29:07 -05:00
|
|
|
desc: 'The scope of specific runners to show'
|
2018-06-11 07:37:47 -04:00
|
|
|
optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES,
|
|
|
|
desc: 'The type of the runners to show'
|
2018-06-11 09:17:46 -04:00
|
|
|
optional :status, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
|
|
|
|
desc: 'The status of the runners to show'
|
2020-04-27 14:09:41 -04:00
|
|
|
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce, desc: 'The tags of the runners to show'
|
2016-12-04 12:11:19 -05:00
|
|
|
use :pagination
|
2016-11-09 10:29:07 -05:00
|
|
|
end
|
2016-01-26 11:03:38 -05:00
|
|
|
get ':id/runners' do
|
2018-06-11 07:37:47 -04:00
|
|
|
runners = Ci::Runner.owned_or_instance_wide(user_project.id)
|
2020-03-30 14:08:07 -04:00
|
|
|
# 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
|
2018-06-11 07:37:47 -04:00
|
|
|
runners = filter_runners(runners, params[:scope])
|
2020-03-30 14:08:07 -04:00
|
|
|
runners = apply_filter(runners, params)
|
2018-06-11 07:37:47 -04:00
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-29 09:40:25 -05:00
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc 'Enable a runner for a project' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :runner_id, type: Integer, desc: 'The ID of the runner'
|
|
|
|
end
|
2016-02-16 06:05:48 -05:00
|
|
|
post ':id/runners' do
|
2016-01-29 09:40:25 -05:00
|
|
|
runner = get_runner(params[:runner_id])
|
2016-02-04 05:01:16 -05:00
|
|
|
authenticate_enable_runner!(runner)
|
2016-01-29 09:40:25 -05:00
|
|
|
|
2018-05-28 06:49:08 -04:00
|
|
|
if runner.assign_to(user_project)
|
2016-06-14 10:17:01 -04:00
|
|
|
present runner, with: Entities::Runner
|
|
|
|
else
|
2018-05-28 07:41:04 -04:00
|
|
|
render_validation_error!(runner)
|
2016-06-14 10:17:01 -04:00
|
|
|
end
|
2016-01-29 09:40:25 -05:00
|
|
|
end
|
|
|
|
|
2016-11-09 10:29:07 -05:00
|
|
|
desc "Disable project's runner" do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :runner_id, type: Integer, desc: 'The ID of the runner'
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-01-29 09:40:25 -05:00
|
|
|
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
|
2016-02-04 05:01:16 -05:00
|
|
|
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
|
2016-01-29 09:40:25 -05:00
|
|
|
|
2017-03-02 07:14:13 -05:00
|
|
|
destroy_conditionally!(runner_project)
|
2016-01-29 09:40:25 -05:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2020-03-30 14:08:07 -04:00
|
|
|
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
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :type, type: String, values: Ci::Runner::AVAILABLE_TYPES,
|
|
|
|
desc: 'The type of the runners to show'
|
|
|
|
optional :status, type: String, values: Ci::Runner::AVAILABLE_STATUSES,
|
|
|
|
desc: 'The status of the runners to show'
|
2020-04-27 14:09:41 -04:00
|
|
|
optional :tag_list, type: Array[String], coerce_with: ::API::Validations::Types::CommaSeparatedToArray.coerce, desc: 'The tags of the runners to show'
|
2020-03-30 14:08:07 -04:00
|
|
|
use :pagination
|
|
|
|
end
|
|
|
|
get ':id/runners' do
|
|
|
|
runners = Ci::Runner.belonging_to_group(user_group.id, include_ancestors: true)
|
|
|
|
runners = apply_filter(runners, params)
|
|
|
|
|
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
helpers do
|
2018-09-12 14:14:49 -04:00
|
|
|
def filter_runners(runners, scope, allowed_scopes: ::Ci::Runner::AVAILABLE_SCOPES)
|
2016-01-26 11:03:38 -05:00
|
|
|
return runners unless scope.present?
|
|
|
|
|
2018-09-12 14:14:49 -04:00
|
|
|
unless allowed_scopes.include?(scope)
|
2016-01-26 11:03:38 -05:00
|
|
|
render_api_error!('Scope contains invalid value', 400)
|
|
|
|
end
|
2016-01-29 10:44:29 -05:00
|
|
|
|
2018-07-04 05:02:45 -04:00
|
|
|
# Support deprecated scopes
|
|
|
|
if runners.respond_to?("deprecated_#{scope}")
|
|
|
|
scope = "deprecated_#{scope}"
|
|
|
|
end
|
|
|
|
|
2017-08-10 12:39:26 -04:00
|
|
|
runners.public_send(scope) # rubocop:disable GitlabSecurity/PublicSend
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2020-03-30 14:08:07 -04:00
|
|
|
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 = runners.tagged_with(params[:tag_list]) if params[:tag_list]
|
|
|
|
|
|
|
|
runners
|
|
|
|
end
|
|
|
|
|
2016-01-26 11:03:38 -05:00
|
|
|
def get_runner(id)
|
|
|
|
runner = Ci::Runner.find(id)
|
|
|
|
not_found!('Runner') unless runner
|
|
|
|
runner
|
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_show_runner!(runner)
|
2018-06-26 05:36:54 -04:00
|
|
|
return if runner.instance_type? || current_user.admin?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-05-09 10:41:15 -04:00
|
|
|
forbidden!("No access granted") unless can?(current_user, :read_runner, runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_update_runner!(runner)
|
2017-04-08 22:20:57 -04:00
|
|
|
return if current_user.admin?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-05-09 10:41:15 -04:00
|
|
|
forbidden!("No access granted") unless can?(current_user, :update_runner, runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_delete_runner!(runner)
|
2017-04-08 22:20:57 -04:00
|
|
|
return if current_user.admin?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
forbidden!("Runner associated with more than one project") if runner.projects.count > 1
|
2018-05-09 10:41:15 -04:00
|
|
|
forbidden!("No access granted") unless can?(current_user, :delete_runner, runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
|
2016-02-04 05:01:16 -05:00
|
|
|
def authenticate_enable_runner!(runner)
|
2018-05-09 08:46:34 -04:00
|
|
|
forbidden!("Runner is a group runner") if runner.group_type?
|
2018-05-11 04:10:22 -04:00
|
|
|
|
2017-04-08 22:20:57 -04:00
|
|
|
return if current_user.admin?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-05-11 04:10:22 -04:00
|
|
|
forbidden!("Runner is locked") if runner.locked?
|
2018-05-09 10:41:15 -04:00
|
|
|
forbidden!("No access granted") unless can?(current_user, :assign_runner, runner)
|
2016-01-29 09:40:25 -05:00
|
|
|
end
|
|
|
|
|
2017-11-16 11:12:33 -05:00
|
|
|
def authenticate_list_runners_jobs!(runner)
|
|
|
|
return if current_user.admin?
|
|
|
|
|
2018-05-10 08:53:24 -04:00
|
|
|
forbidden!("No access granted") unless can?(current_user, :read_runner, runner)
|
2016-01-26 11:03:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|