2016-01-26 16:03:38 +00:00
|
|
|
module API
|
|
|
|
class Runners < Grape::API
|
2016-12-04 17:11:19 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2016-01-26 16:03:38 +00:00
|
|
|
before { authenticate! }
|
|
|
|
|
|
|
|
resource :runners do
|
2016-11-09 15:29:07 +00:00
|
|
|
desc 'Get runners available for user' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :scope, type: String, values: %w[active paused online],
|
|
|
|
desc: 'The scope of specific runners to show'
|
2016-12-04 17:11:19 +00:00
|
|
|
use :pagination
|
2016-11-09 15:29:07 +00:00
|
|
|
end
|
2016-01-26 16:03:38 +00:00
|
|
|
get do
|
2017-02-22 17:46:57 +00:00
|
|
|
runners = filter_runners(current_user.ci_authorized_runners, params[:scope], without: %w(specific shared))
|
2016-02-02 14:52:02 +00:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-26 16:03:38 +00:00
|
|
|
|
2016-11-09 15:29:07 +00:00
|
|
|
desc 'Get all runners - shared and specific' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :scope, type: String, values: %w[active paused online specific shared],
|
|
|
|
desc: 'The scope of specific runners to show'
|
2016-12-04 17:11:19 +00:00
|
|
|
use :pagination
|
2016-11-09 15:29:07 +00:00
|
|
|
end
|
2016-02-02 14:52:02 +00:00
|
|
|
get 'all' do
|
|
|
|
authenticated_as_admin!
|
|
|
|
runners = filter_runners(Ci::Runner.all, params[:scope])
|
2016-01-26 16:03:38 +00:00
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
|
|
|
|
2016-11-09 15:29:07 +00: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 16:03:38 +00:00
|
|
|
get ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2016-02-04 10:01:16 +00:00
|
|
|
authenticate_show_runner!(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
|
2016-02-10 11:26:56 +00:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
2016-11-09 15:29:07 +00: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'
|
|
|
|
optional :tag_list, type: Array[String], desc: 'The list of tags for a runner'
|
|
|
|
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 07:09:30 +00:00
|
|
|
optional :access_level, type: String, values: Ci::Runner.access_levels.keys,
|
|
|
|
desc: 'The access_level of the runner'
|
2017-08-23 16:28:57 +00:00
|
|
|
at_least_one_of :description, :active, :tag_list, :run_untagged, :locked, :access_level
|
2016-11-09 15:29:07 +00:00
|
|
|
end
|
2016-01-26 16:03:38 +00:00
|
|
|
put ':id' do
|
2016-11-09 15:29:07 +00:00
|
|
|
runner = get_runner(params.delete(:id))
|
2016-02-04 10:01:16 +00:00
|
|
|
authenticate_update_runner!(runner)
|
2017-02-08 14:29:44 +00:00
|
|
|
update_service = Ci::UpdateRunnerService.new(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
|
2017-02-08 14:29:44 +00:00
|
|
|
if update_service.update(declared_params(include_missing: false))
|
2016-02-10 13:20:51 +00:00
|
|
|
present runner, with: Entities::RunnerDetails, current_user: current_user
|
2016-01-26 16:03:38 +00:00
|
|
|
else
|
|
|
|
render_validation_error!(runner)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 15:29:07 +00: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 16:03:38 +00:00
|
|
|
delete ':id' do
|
|
|
|
runner = get_runner(params[:id])
|
2017-03-01 13:35:48 +00:00
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
authenticate_delete_runner!(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
|
2017-03-02 12:14:13 +00:00
|
|
|
destroy_conditionally!(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-09 15:29:07 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-08-31 11:44:49 +00:00
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2016-01-26 16:03:38 +00:00
|
|
|
before { authorize_admin_project }
|
|
|
|
|
2016-11-09 15:29:07 +00:00
|
|
|
desc 'Get runners available for project' do
|
|
|
|
success Entities::Runner
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
optional :scope, type: String, values: %w[active paused online specific shared],
|
|
|
|
desc: 'The scope of specific runners to show'
|
2016-12-04 17:11:19 +00:00
|
|
|
use :pagination
|
2016-11-09 15:29:07 +00:00
|
|
|
end
|
2016-01-26 16:03:38 +00:00
|
|
|
get ':id/runners' do
|
|
|
|
runners = filter_runners(Ci::Runner.owned_or_shared(user_project.id), params[:scope])
|
|
|
|
present paginate(runners), with: Entities::Runner
|
|
|
|
end
|
2016-01-29 14:40:25 +00:00
|
|
|
|
2016-11-09 15:29:07 +00: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 11:05:48 +00:00
|
|
|
post ':id/runners' do
|
2016-01-29 14:40:25 +00:00
|
|
|
runner = get_runner(params[:runner_id])
|
2016-02-04 10:01:16 +00:00
|
|
|
authenticate_enable_runner!(runner)
|
2016-01-29 14:40:25 +00:00
|
|
|
|
2016-06-14 15:11:43 +00:00
|
|
|
runner_project = runner.assign_to(user_project)
|
|
|
|
|
|
|
|
if runner_project.persisted?
|
2016-06-14 14:17:01 +00:00
|
|
|
present runner, with: Entities::Runner
|
|
|
|
else
|
|
|
|
conflict!("Runner was already enabled for this project")
|
|
|
|
end
|
2016-01-29 14:40:25 +00:00
|
|
|
end
|
|
|
|
|
2016-11-09 15:29:07 +00: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
|
2016-01-29 14:40:25 +00: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 10:01:16 +00:00
|
|
|
forbidden!("Only one project associated with the runner. Please remove the runner instead") if runner.projects.count == 1
|
2016-01-29 14:40:25 +00:00
|
|
|
|
2017-03-02 12:14:13 +00:00
|
|
|
destroy_conditionally!(runner_project)
|
2016-01-29 14:40:25 +00:00
|
|
|
end
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
helpers do
|
2016-02-05 14:35:21 +00:00
|
|
|
def filter_runners(runners, scope, options = {})
|
2016-01-26 16:03:38 +00:00
|
|
|
return runners unless scope.present?
|
|
|
|
|
|
|
|
available_scopes = ::Ci::Runner::AVAILABLE_SCOPES
|
2016-02-05 14:35:21 +00:00
|
|
|
if options[:without]
|
|
|
|
available_scopes = available_scopes - options[:without]
|
|
|
|
end
|
|
|
|
|
2016-01-29 15:44:29 +00:00
|
|
|
if (available_scopes & [scope]).empty?
|
2016-01-26 16:03:38 +00:00
|
|
|
render_api_error!('Scope contains invalid value', 400)
|
|
|
|
end
|
2016-01-29 15:44:29 +00:00
|
|
|
|
2017-08-10 16:39:26 +00:00
|
|
|
runners.public_send(scope) # rubocop:disable GitlabSecurity/PublicSend
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_runner(id)
|
|
|
|
runner = Ci::Runner.find(id)
|
|
|
|
not_found!('Runner') unless runner
|
|
|
|
runner
|
|
|
|
end
|
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
def authenticate_show_runner!(runner)
|
2017-04-09 02:20:57 +00:00
|
|
|
return if runner.is_shared || current_user.admin?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
def authenticate_update_runner!(runner)
|
2017-04-09 02:20:57 +00:00
|
|
|
return if current_user.admin?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
def authenticate_delete_runner!(runner)
|
2017-04-09 02:20:57 +00:00
|
|
|
return if current_user.admin?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
|
|
|
forbidden!("Runner associated with more than one project") if runner.projects.count > 1
|
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
def authenticate_enable_runner!(runner)
|
|
|
|
forbidden!("Runner is shared") if runner.is_shared?
|
2016-06-14 14:58:38 +00:00
|
|
|
forbidden!("Runner is locked") if runner.locked?
|
2017-04-09 02:20:57 +00:00
|
|
|
return if current_user.admin?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-04 10:01:16 +00:00
|
|
|
forbidden!("No access granted") unless user_can_access_runner?(runner)
|
2016-01-29 14:40:25 +00:00
|
|
|
end
|
|
|
|
|
2016-01-26 16:03:38 +00:00
|
|
|
def user_can_access_runner?(runner)
|
2016-02-04 10:01:16 +00:00
|
|
|
current_user.ci_authorized_runners.exists?(runner.id)
|
2016-01-26 16:03:38 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|