From 0d30b00de807df550bec947751c098317c5bb79f Mon Sep 17 00:00:00 2001 From: Dylan Griffith Date: Mon, 30 Apr 2018 17:00:28 +0400 Subject: [PATCH] Start persisting runner_type when creating runners --- app/models/ci/runner.rb | 6 ++++++ ...430101916_add_runner_type_to_ci_runners.rb | 9 +++++++++ ...runner_type_for_ci_runners_post_migrate.rb | 20 +++++++++++++++++++ db/schema.rb | 3 ++- lib/api/runner.rb | 6 +++--- 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20180430101916_add_runner_type_to_ci_runners.rb create mode 100644 db/post_migrate/20180430143705_backfill_runner_type_for_ci_runners_post_migrate.rb diff --git a/app/models/ci/runner.rb b/app/models/ci/runner.rb index da1107951bf..cdd28407172 100644 --- a/app/models/ci/runner.rb +++ b/app/models/ci/runner.rb @@ -67,6 +67,12 @@ module Ci ref_protected: 1 } + enum runner_type: { + instance_type: 1, + group_type: 2, + project_type: 3 + } + cached_attr_reader :version, :revision, :platform, :architecture, :contacted_at, :ip_address chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout diff --git a/db/migrate/20180430101916_add_runner_type_to_ci_runners.rb b/db/migrate/20180430101916_add_runner_type_to_ci_runners.rb new file mode 100644 index 00000000000..8c8009f28fb --- /dev/null +++ b/db/migrate/20180430101916_add_runner_type_to_ci_runners.rb @@ -0,0 +1,9 @@ +class AddRunnerTypeToCiRunners < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + def change + add_column :ci_runners, :runner_type, :integer + end +end diff --git a/db/post_migrate/20180430143705_backfill_runner_type_for_ci_runners_post_migrate.rb b/db/post_migrate/20180430143705_backfill_runner_type_for_ci_runners_post_migrate.rb new file mode 100644 index 00000000000..8509222edc2 --- /dev/null +++ b/db/post_migrate/20180430143705_backfill_runner_type_for_ci_runners_post_migrate.rb @@ -0,0 +1,20 @@ +class BackfillRunnerTypeForCiRunnersPostMigrate < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + DOWNTIME = false + + disable_ddl_transaction! + + def up + update_column_in_batches(:ci_runners, :runner_type, 1) do |table, query| + query.where(table[:is_shared].eq(true)).where(table[:runner_type].eq(nil)) + end + + update_column_in_batches(:ci_runners, :runner_type, 3) do |table, query| + query.where(table[:is_shared].eq(false)).where(table[:runner_type].eq(nil)) + end + end + + def down + end +end diff --git a/db/schema.rb b/db/schema.rb index db1e41e00b3..4a541b3ac81 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180425131009) do +ActiveRecord::Schema.define(version: 20180430143705) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -479,6 +479,7 @@ ActiveRecord::Schema.define(version: 20180425131009) do t.integer "access_level", default: 0, null: false t.string "ip_address" t.integer "maximum_timeout" + t.integer "runner_type" end add_index "ci_runners", ["contacted_at"], name: "index_ci_runners_on_contacted_at", using: :btree diff --git a/lib/api/runner.rb b/lib/api/runner.rb index 49d9b0b1b4f..67896ae1fc5 100644 --- a/lib/api/runner.rb +++ b/lib/api/runner.rb @@ -23,13 +23,13 @@ module API runner = if runner_registration_token_valid? # Create shared runner. Requires admin access - Ci::Runner.create(attributes.merge(is_shared: true)) + Ci::Runner.create(attributes.merge(is_shared: true, runner_type: :instance_type)) elsif project = Project.find_by(runners_token: params[:token]) # Create a specific runner for the project - project.runners.create(attributes) + project.runners.create(attributes.merge(runner_type: :project_type)) elsif group = Group.find_by(runners_token: params[:token]) # Create a specific runner for the group - group.runners.create(attributes) + group.runners.create(attributes.merge(runner_type: :group_type)) end break forbidden! unless runner