runners_controller.rb 2.54 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Admin::RunnersController < Admin::ApplicationController
4 5 6
  include RunnerSetupScripts

  before_action :runner, except: [:index, :tag_list, :runner_setup_scripts]
7

8
  feature_category :runner
9

10
  def index
11
    @active_runners_count = Ci::Runner.online.count
12 13 14
  end

  def show
15
    assign_builds_and_projects
16 17 18
  end

  def update
19
    if Ci::UpdateRunnerService.new(@runner).update(runner_params)
20 21 22 23 24 25
      respond_to do |format|
        format.html { redirect_to admin_runner_path(@runner) }
      end
    else
      assign_builds_and_projects
      render 'show'
26 27 28 29 30 31
    end
  end

  def destroy
    @runner.destroy

32
    redirect_to admin_runners_path, status: :found
33 34 35
  end

  def resume
36
    if Ci::UpdateRunnerService.new(@runner).update(active: true)
37
      redirect_to admin_runners_path, notice: _('Runner was successfully updated.')
38
    else
39
      redirect_to admin_runners_path, alert: _('Runner was not updated.')
40 41 42 43
    end
  end

  def pause
44
    if Ci::UpdateRunnerService.new(@runner).update(active: false)
45
      redirect_to admin_runners_path, notice: _('Runner was successfully updated.')
46
    else
47
      redirect_to admin_runners_path, alert: _('Runner was not updated.')
48 49 50
    end
  end

51 52 53 54 55 56
  def tag_list
    tags = Autocomplete::ActsAsTaggableOn::TagsFinder.new(params: params).execute

    render json: ActsAsTaggableOn::TagSerializer.new.represent(tags)
  end

57 58 59 60
  def runner_setup_scripts
    private_runner_setup_scripts
  end

61 62 63 64 65 66 67
  private

  def runner
    @runner ||= Ci::Runner.find(params[:id])
  end

  def runner_params
68 69 70 71 72 73 74 75 76
    params.require(:runner).permit(permitted_attrs)
  end

  def permitted_attrs
    if Gitlab.com?
      Ci::Runner::FORM_EDITABLE + Ci::Runner::MINUTES_COST_FACTOR_FIELDS
    else
      Ci::Runner::FORM_EDITABLE
    end
77
  end
78

79
  # rubocop: disable CodeReuse/ActiveRecord
80
  def assign_builds_and_projects
81
    @builds = runner.builds.order('id DESC').preload_project_and_pipeline_project.first(30)
82 83 84 85 86 87
    @projects =
      if params[:search].present?
        ::Project.search(params[:search])
      else
        Project.all
      end
88

89 90
    ::Gitlab::Database.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/338659') do
      @projects = @projects.where.not(id: runner.projects.select(:id)) if runner.projects.any?
91
      @projects = @projects.allow_cross_joins_across_databases(url: 'https://gitlab.com/gitlab-org/gitlab/-/issues/338659')
92 93 94
      @projects = @projects.inc_routes
      @projects = @projects.page(params[:page]).per(30).without_count
    end
95
  end
96
  # rubocop: enable CodeReuse/ActiveRecord
97
end