diff options
author | Tomasz Maczukin <tomasz@maczukin.pl> | 2017-02-14 23:52:02 +0100 |
---|---|---|
committer | Tomasz Maczukin <tomasz@maczukin.pl> | 2017-02-16 01:16:41 +0100 |
commit | 618ce941647177b560fb3f5b677325bb964edae3 (patch) | |
tree | 580ef62f72af092ef8fec9ce6a216d4afe11a5a0 /lib | |
parent | b05e75b8faccc50749adc63419074c91802a8f50 (diff) | |
download | gitlab-ce-618ce941647177b560fb3f5b677325bb964edae3.tar.gz |
Add Runner registration/deletion API
Diffstat (limited to 'lib')
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/ci.rb | 52 | ||||
-rw-r--r-- | lib/api/entities.rb | 4 | ||||
-rw-r--r-- | lib/api/helpers/ci.rb | 24 |
4 files changed, 81 insertions, 0 deletions
diff --git a/lib/api/api.rb b/lib/api/api.rb index 06346ae822a..6d7eb3eb84f 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -52,6 +52,7 @@ module API mount ::API::Branches mount ::API::BroadcastMessages mount ::API::Builds + mount ::API::Ci mount ::API::Commits mount ::API::CommitStatuses mount ::API::DeployKeys diff --git a/lib/api/ci.rb b/lib/api/ci.rb new file mode 100644 index 00000000000..635116cc88d --- /dev/null +++ b/lib/api/ci.rb @@ -0,0 +1,52 @@ +module API + class Ci < Grape::API + helpers ::API::Helpers::Ci + + 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 + attributes = attributes_for_keys [:description, :locked, :run_untagged, :tag_list] + + runner = + if runner_registration_token_valid? + # Create shared runner. Requires admin access + ::Ci::Runner.create(attributes.merge(is_shared: true)) + 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 + runner.update(get_runner_version_from_params) + present runner, with: Entities::RunnerRegistrationDetails + else + not_found! + end + end + + desc 'Deletes a registered Runner' do + http_codes [[200, 'Runner was deleted'], [403, 'Forbidden']] + end + params do + requires :token, type: String, desc: %q(Runner's authentication token) + end + delete '/' do + authenticate_runner! + ::Ci::Runner.find_by_token(params[:token]).destroy + end + end + end +end diff --git a/lib/api/entities.rb b/lib/api/entities.rb index 232f231ddd2..8229e67eeac 100644 --- a/lib/api/entities.rb +++ b/lib/api/entities.rb @@ -620,6 +620,10 @@ module API end end + class RunnerRegistrationDetails < Grape::Entity + expose :id, :token + end + class BuildArtifactFile < Grape::Entity expose :filename, :size end diff --git a/lib/api/helpers/ci.rb b/lib/api/helpers/ci.rb new file mode 100644 index 00000000000..24669eba4bb --- /dev/null +++ b/lib/api/helpers/ci.rb @@ -0,0 +1,24 @@ +module API + module Helpers + module Ci + def runner_registration_token_valid? + ActiveSupport::SecurityUtils.variable_size_secure_compare( + params[:token], + current_application_settings.runners_registration_token) + end + + def get_runner_version_from_params + return unless params['info'].present? + attributes_for_keys(%w(name version revision platform architecture), params['info']) + end + + def authenticate_runner! + forbidden! unless current_runner + end + + def current_runner + @runner ||= ::Ci::Runner.find_by_token(params[:token].to_s) + end + end + end +end
\ No newline at end of file |