diff options
Diffstat (limited to 'app/services/ci/runners')
-rw-r--r-- | app/services/ci/runners/assign_runner_service.rb | 28 | ||||
-rw-r--r-- | app/services/ci/runners/register_runner_service.rb | 60 | ||||
-rw-r--r-- | app/services/ci/runners/reset_registration_token_service.rb | 31 | ||||
-rw-r--r-- | app/services/ci/runners/unassign_runner_service.rb | 28 | ||||
-rw-r--r-- | app/services/ci/runners/unregister_runner_service.rb | 22 | ||||
-rw-r--r-- | app/services/ci/runners/update_runner_service.rb | 21 |
6 files changed, 190 insertions, 0 deletions
diff --git a/app/services/ci/runners/assign_runner_service.rb b/app/services/ci/runners/assign_runner_service.rb new file mode 100644 index 00000000000..886cd3a4e44 --- /dev/null +++ b/app/services/ci/runners/assign_runner_service.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Ci + module Runners + class AssignRunnerService + # @param [Ci::Runner] runner: the runner to assign to a project + # @param [Project] project: the new project to assign the runner to + # @param [User] user: the user performing the operation + def initialize(runner, project, user) + @runner = runner + @project = project + @user = user + end + + def execute + return false unless @user.present? && @user.can?(:assign_runner, @runner) + + @runner.assign_to(@project, @user) + end + + private + + attr_reader :runner, :project, :user + end + end +end + +Ci::Runners::AssignRunnerService.prepend_mod diff --git a/app/services/ci/runners/register_runner_service.rb b/app/services/ci/runners/register_runner_service.rb new file mode 100644 index 00000000000..7978d094d9b --- /dev/null +++ b/app/services/ci/runners/register_runner_service.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Ci + module Runners + class RegisterRunnerService + def execute(registration_token, attributes) + runner_type_attrs = extract_runner_type_attrs(registration_token) + + return unless runner_type_attrs + + ::Ci::Runner.create(attributes.merge(runner_type_attrs)) + end + + private + + def extract_runner_type_attrs(registration_token) + @attrs_from_token ||= check_token(registration_token) + + return unless @attrs_from_token + + attrs = @attrs_from_token.clone + case attrs[:runner_type] + when :project_type + attrs[:projects] = [attrs.delete(:scope)] + when :group_type + attrs[:groups] = [attrs.delete(:scope)] + end + + attrs + end + + def check_token(registration_token) + if runner_registration_token_valid?(registration_token) + # Create shared runner. Requires admin access + { runner_type: :instance_type } + elsif runner_registrar_valid?('project') && project = ::Project.find_by_runners_token(registration_token) + # Create a specific runner for the project + { runner_type: :project_type, scope: project } + elsif runner_registrar_valid?('group') && group = ::Group.find_by_runners_token(registration_token) + # Create a specific runner for the group + { runner_type: :group_type, scope: group } + end + end + + def runner_registration_token_valid?(registration_token) + ActiveSupport::SecurityUtils.secure_compare(registration_token, Gitlab::CurrentSettings.runners_registration_token) + end + + def runner_registrar_valid?(type) + Feature.disabled?(:runner_registration_control, default_enabled: :yaml) || Gitlab::CurrentSettings.valid_runner_registrars.include?(type) + end + + def token_scope + @attrs_from_token[:scope] + end + end + end +end + +Ci::Runners::RegisterRunnerService.prepend_mod diff --git a/app/services/ci/runners/reset_registration_token_service.rb b/app/services/ci/runners/reset_registration_token_service.rb new file mode 100644 index 00000000000..bbe49c04644 --- /dev/null +++ b/app/services/ci/runners/reset_registration_token_service.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module Ci + module Runners + class ResetRegistrationTokenService + # @param [ApplicationSetting, Project, Group] scope: the scope of the reset operation + # @param [User] user: the user performing the operation + def initialize(scope, user) + @scope = scope + @user = user + end + + def execute + return unless @user.present? && @user.can?(:update_runners_registration_token, scope) + + case scope + when ::ApplicationSetting + scope.reset_runners_registration_token! + ApplicationSetting.current_without_cache.runners_registration_token + when ::Group, ::Project + scope.reset_runners_token! + scope.runners_token + end + end + + private + + attr_reader :scope, :user + end + end +end diff --git a/app/services/ci/runners/unassign_runner_service.rb b/app/services/ci/runners/unassign_runner_service.rb new file mode 100644 index 00000000000..1e46cf6add8 --- /dev/null +++ b/app/services/ci/runners/unassign_runner_service.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +module Ci + module Runners + class UnassignRunnerService + # @param [Ci::RunnerProject] runner_project the runner/project association to destroy + # @param [User] user the user performing the operation + def initialize(runner_project, user) + @runner_project = runner_project + @runner = runner_project.runner + @project = runner_project.project + @user = user + end + + def execute + return false unless @user.present? && @user.can?(:assign_runner, @runner) + + @runner_project.destroy + end + + private + + attr_reader :runner, :project, :user + end + end +end + +Ci::Runners::UnassignRunnerService.prepend_mod diff --git a/app/services/ci/runners/unregister_runner_service.rb b/app/services/ci/runners/unregister_runner_service.rb new file mode 100644 index 00000000000..4ee1e73c458 --- /dev/null +++ b/app/services/ci/runners/unregister_runner_service.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module Ci + module Runners + class UnregisterRunnerService + attr_reader :runner, :author + + # @param [Ci::Runner] runner the runner to unregister/destroy + # @param [User, authentication token String] author the user or the authentication token that authorizes the removal + def initialize(runner, author) + @runner = runner + @author = author + end + + def execute + @runner&.destroy + end + end + end +end + +Ci::Runners::UnregisterRunnerService.prepend_mod diff --git a/app/services/ci/runners/update_runner_service.rb b/app/services/ci/runners/update_runner_service.rb new file mode 100644 index 00000000000..6cc080f81c2 --- /dev/null +++ b/app/services/ci/runners/update_runner_service.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Ci + module Runners + class UpdateRunnerService + attr_reader :runner + + def initialize(runner) + @runner = runner + end + + def update(params) + params[:active] = !params.delete(:paused) if params.include?(:paused) + + runner.update(params).tap do |updated| + runner.tick_runner_queue if updated + end + end + end + end +end |