runner.rb 9.56 KB
Newer Older
1
module API
2 3
  class Runner < Grape::API
    helpers ::API::Helpers::Runner
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

    resource :runners do
      desc 'Registers a new Runner' do
        success Entities::RunnerRegistrationDetails
        http_codes [[201, 'Runner was created'], [403, 'Forbidden']]
      end
      params do
        requires :token, type: String, desc: 'Registration token'
        optional :description, type: String, desc: %q(Runner's description)
        optional :info, type: Hash, desc: %q(Runner's metadata)
        optional :locked, type: Boolean, desc: 'Should Runner be locked for current project'
        optional :run_untagged, type: Boolean, desc: 'Should Runner handle untagged jobs'
        optional :tag_list, type: Array[String], desc: %q(List of Runner's tags)
      end
      post '/' do
19 20
        attributes = attributes_for_keys([:description, :locked, :run_untagged, :tag_list])
          .merge(get_runner_details_from_request)
21 22 23 24

        runner =
          if runner_registration_token_valid?
            # Create shared runner. Requires admin access
25
            Ci::Runner.create(attributes.merge(is_shared: true))
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
          elsif project = Project.find_by(runners_token: params[:token])
            # Create a specific runner for project.
            project.runners.create(attributes)
          end

        return forbidden! unless runner

        if runner.id
          present runner, with: Entities::RunnerRegistrationDetails
        else
          not_found!
        end
      end

      desc 'Deletes a registered Runner' do
41
        http_codes [[204, 'Runner was deleted'], [403, 'Forbidden']]
42 43 44 45 46 47
      end
      params do
        requires :token, type: String, desc: %q(Runner's authentication token)
      end
      delete '/' do
        authenticate_runner!
48 49 50 51

        runner = Ci::Runner.find_by_token(params[:token])

        destroy_conditionally!(runner)
52
      end
53 54 55 56 57 58 59 60 61 62 63

      desc 'Validates authentication credentials' do
        http_codes [[200, 'Credentials are valid'], [403, 'Forbidden']]
      end
      params do
        requires :token, type: String, desc: %q(Runner's authentication token)
      end
      post '/verify' do
        authenticate_runner!
        status 200
      end
64
    end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
65 66 67

    resource :jobs do
      desc 'Request a job' do
68
        success Entities::JobRequest::Response
69 70 71
        http_codes [[201, 'Job was scheduled'],
                    [204, 'No job for Runner'],
                    [403, 'Forbidden']]
Tomasz Maczukin's avatar
Tomasz Maczukin committed
72 73 74
      end
      params do
        requires :token, type: String, desc: %q(Runner's authentication token)
75 76
        optional :last_update, type: String, desc: %q(Runner's queue last_update token)
        optional :info, type: Hash, desc: %q(Runner's metadata)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
77 78 79
      end
      post '/request' do
        authenticate_runner!
80
        no_content! unless current_runner.active?
Tomasz Maczukin's avatar
Tomasz Maczukin committed
81

82
        if current_runner.runner_queue_value_latest?(params[:last_update])
Tomasz Maczukin's avatar
Tomasz Maczukin committed
83 84
          header 'X-GitLab-Last-Update', params[:last_update]
          Gitlab::Metrics.add_event(:build_not_found_cached)
85
          return no_content!
Tomasz Maczukin's avatar
Tomasz Maczukin committed
86 87 88
        end

        new_update = current_runner.ensure_runner_queue_value
89
        result = ::Ci::RegisterJobService.new(current_runner).execute
Tomasz Maczukin's avatar
Tomasz Maczukin committed
90 91 92 93

        if result.valid?
          if result.build
            Gitlab::Metrics.add_event(:build_found,
94
                                      project: result.build.project.full_path)
95
            present result.build, with: Entities::JobRequest::Response
Tomasz Maczukin's avatar
Tomasz Maczukin committed
96 97 98
          else
            Gitlab::Metrics.add_event(:build_not_found)
            header 'X-GitLab-Last-Update', new_update
99
            no_content!
Tomasz Maczukin's avatar
Tomasz Maczukin committed
100 101 102 103 104 105 106
          end
        else
          # We received build that is invalid due to concurrency conflict
          Gitlab::Metrics.add_event(:build_invalid)
          conflict!
        end
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
107 108 109 110 111

      desc 'Updates a job' do
        http_codes [[200, 'Job was updated'], [403, 'Forbidden']]
      end
      params do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
112
        requires :token, type: String, desc: %q(Runners's authentication token)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
113
        requires :id, type: Integer, desc: %q(Job's ID)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
114 115
        optional :trace, type: String, desc: %q(Job's full trace)
        optional :state, type: String, desc: %q(Job's status: success, failed)
116 117
        optional :failure_reason, type: String, values: CommitStatus.failure_reasons.keys,
                                  desc: %q(Job's failure_reason)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
118 119
      end
      put '/:id' do
120
        job = authenticate_job!
Tomasz Maczukin's avatar
Tomasz Maczukin committed
121

122
        job.trace.set(params[:trace]) if params[:trace]
Tomasz Maczukin's avatar
Tomasz Maczukin committed
123 124

        Gitlab::Metrics.add_event(:update_build,
125
                                  project: job.project.full_path)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
126 127 128 129 130

        case params[:state].to_s
        when 'success'
          job.success
        when 'failed'
131
          job.drop(params[:failure_reason] || :unknown_failure)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
132 133
        end
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
134

Tomasz Maczukin's avatar
Tomasz Maczukin committed
135
      desc 'Appends a patch to the job trace' do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
136 137 138 139 140 141
        http_codes [[202, 'Trace was patched'],
                    [400, 'Missing Content-Range header'],
                    [403, 'Forbidden'],
                    [416, 'Range not satisfiable']]
      end
      params do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
142
        requires :id, type: Integer, desc: %q(Job's ID)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
143 144 145
        optional :token, type: String, desc: %q(Job's authentication token)
      end
      patch '/:id/trace' do
146
        job = authenticate_job!
Tomasz Maczukin's avatar
Tomasz Maczukin committed
147

148
        error!('400 Missing header Content-Range', 400) unless request.headers.key?('Content-Range')
Tomasz Maczukin's avatar
Tomasz Maczukin committed
149 150 151
        content_range = request.headers['Content-Range']
        content_range = content_range.split('-')

152 153 154
        stream_size = job.trace.append(request.body.read, content_range[0].to_i)
        if stream_size < 0
          return error!('416 Range Not Satisfiable', 416, { 'Range' => "0-#{-stream_size}" })
Tomasz Maczukin's avatar
Tomasz Maczukin committed
155 156 157 158
        end

        status 202
        header 'Job-Status', job.status
159
        header 'Range', "0-#{stream_size}"
Tomasz Maczukin's avatar
Tomasz Maczukin committed
160
      end
161 162 163 164 165 166 167 168

      desc 'Authorize artifacts uploading for job' do
        http_codes [[200, 'Upload allowed'],
                    [403, 'Forbidden'],
                    [405, 'Artifacts support not enabled'],
                    [413, 'File too large']]
      end
      params do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
169
        requires :id, type: Integer, desc: %q(Job's ID)
170
        optional :token, type: String, desc: %q(Job's authentication token)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
171
        optional :filesize, type: Integer, desc: %q(Artifacts filesize)
172 173 174 175 176 177
      end
      post '/:id/artifacts/authorize' do
        not_allowed! unless Gitlab.config.artifacts.enabled
        require_gitlab_workhorse!
        Gitlab::Workhorse.verify_api_request!(headers)

178
        job = authenticate_job!
179 180 181 182 183 184 185 186 187 188 189
        forbidden!('Job is not running') unless job.running?

        if params[:filesize]
          file_size = params[:filesize].to_i
          file_to_large! unless file_size < max_artifacts_size
        end

        status 200
        content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
        Gitlab::Workhorse.artifact_upload_ok
      end
190 191

      desc 'Upload artifacts for job' do
192
        success Entities::JobRequest::Response
193 194 195 196 197 198 199
        http_codes [[201, 'Artifact uploaded'],
                    [400, 'Bad request'],
                    [403, 'Forbidden'],
                    [405, 'Artifacts support not enabled'],
                    [413, 'File too large']]
      end
      params do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
200
        requires :id, type: Integer, desc: %q(Job's ID)
201 202
        optional :token, type: String, desc: %q(Job's authentication token)
        optional :expire_in, type: String, desc: %q(Specify when artifacts should expire)
203
        optional :file, type: File, desc: %q(Artifact's file)
204 205 206
        optional 'file.path', type: String, desc: %q(path to locally stored body (generated by Workhorse))
        optional 'file.name', type: String, desc: %q(real filename as send in Content-Disposition (generated by Workhorse))
        optional 'file.type', type: String, desc: %q(real content type as send in Content-Type (generated by Workhorse))
207
        optional 'file.sha256', type: String, desc: %q(checksum of the file)
208 209 210 211 212 213 214
        optional 'metadata.path', type: String, desc: %q(path to locally stored body (generated by Workhorse))
        optional 'metadata.name', type: String, desc: %q(filename (generated by Workhorse))
      end
      post '/:id/artifacts' do
        not_allowed! unless Gitlab.config.artifacts.enabled
        require_gitlab_workhorse!

215
        job = authenticate_job!
216 217
        forbidden!('Job is not running!') unless job.running?

218 219 220
        workhorse_upload_path = JobArtifactUploader.workhorse_upload_path
        artifacts = uploaded_file(:file, workhorse_upload_path)
        metadata = uploaded_file(:metadata, workhorse_upload_path)
221 222 223 224

        bad_request!('Missing artifacts file!') unless artifacts
        file_to_large! unless artifacts.size < max_artifacts_size

225
        expire_in = params['expire_in'] ||
Tomasz Maczukin's avatar
Tomasz Maczukin committed
226
          Gitlab::CurrentSettings.current_application_settings.default_artifacts_expire_in
227

228
        job.build_job_artifacts_archive(project: job.project, file_type: :archive, file: artifacts, checksum: params['file.sha256'], expire_in: expire_in)
229 230 231
        job.build_job_artifacts_metadata(project: job.project, file_type: :metadata, file: metadata, expire_in: expire_in) if metadata
        job.artifacts_expire_in = expire_in

232 233 234 235 236 237
        if job.save
          present job, with: Entities::JobRequest::Response
        else
          render_validation_error!(job)
        end
      end
238 239 240 241 242 243 244

      desc 'Download the artifacts file for job' do
        http_codes [[200, 'Upload allowed'],
                    [403, 'Forbidden'],
                    [404, 'Artifact not found']]
      end
      params do
Tomasz Maczukin's avatar
Tomasz Maczukin committed
245
        requires :id, type: Integer, desc: %q(Job's ID)
246 247 248
        optional :token, type: String, desc: %q(Job's authentication token)
      end
      get '/:id/artifacts' do
249
        job = authenticate_job!
250

Kamil Trzcinski's avatar
Kamil Trzcinski committed
251
        present_artifacts!(job.artifacts_file)
252
      end
Tomasz Maczukin's avatar
Tomasz Maczukin committed
253
    end
254 255
  end
end