summaryrefslogtreecommitdiff
path: root/lib/ci/api/runners.rb
blob: 2a611a67eafac7297ea81cacf8a9d1940d03ca44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Ci
  module API
    class Runners < Grape::API
      resource :runners do
        desc 'Delete a runner'
        params do
          requires :token, type: String, desc: 'The unique token of the runner'
        end
        delete "delete" do
          authenticate_runner!
          Ci::Runner.find_by_token(params[:token]).destroy
        end

        desc 'Register a new runner' do
          success Entities::Runner
        end
        params do
          requires :token, type: String, desc: 'The unique token of the runner'
          optional :description, type: String, desc: 'The description of the runner'
          optional :tag_list, type: Array[String], desc: 'A list of tags the runner should run for'
          optional :run_untagged, type: Boolean, desc: 'Flag if the runner should execute untagged jobs'
          optional :locked, type: Boolean, desc: 'Lock this runner for this specific project'
        end
        post "register" do
          runner_params = declared(params, include_missing: false)

          runner =
            if runner_registration_token_valid?
              # Create shared runner. Requires admin access
              Ci::Runner.create(runner_params.merge(is_shared: true))
            elsif project = Project.find_by(runners_token: runner_params[:token])
              # Create a specific runner for project.
              project.runners.create(runner_params)
            end

          return forbidden! unless runner

          if runner.id
            runner.update(get_runner_version_from_params)
            present runner, with: Entities::Runner
          else
            not_found!
          end
        end
      end
    end
  end
end