runner.rb 9.5 KB
Newer Older
1 2
# frozen_string_literal: true

D
Douwe Maan 已提交
3 4
module Ci
  class Runner < ActiveRecord::Base
5
    extend Gitlab::Ci::Model
6
    include Gitlab::SQL::Pattern
7
    include IgnorableColumn
8
    include RedisCacheable
9
    include ChronicDurationAttribute
10
    include FromUnion
11

A
Alexis Reigel 已提交
12 13 14 15 16 17 18 19 20 21 22
    enum access_level: {
      not_protected: 0,
      ref_protected: 1
    }

    enum runner_type: {
      instance_type: 1,
      group_type: 2,
      project_type: 3
    }

23
    RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
A
Alessio Caiazza 已提交
24
    ONLINE_CONTACT_TIMEOUT = 1.hour
25
    UPDATE_DB_RUNNER_INFO_EVERY = 40.minutes
A
Alexis Reigel 已提交
26 27
    AVAILABLE_TYPES_LEGACY = %w[specific shared].freeze
    AVAILABLE_TYPES = runner_types.keys.freeze
28
    AVAILABLE_STATUSES = %w[active paused online offline].freeze
A
Alexis Reigel 已提交
29
    AVAILABLE_SCOPES = (AVAILABLE_TYPES_LEGACY + AVAILABLE_TYPES + AVAILABLE_STATUSES).freeze
30
    FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze
31

32 33
    ignore_column :is_shared

34
    has_many :builds
K
Kamil Trzciński 已提交
35
    has_many :runner_projects, inverse_of: :runner, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
36
    has_many :projects, through: :runner_projects
K
Kamil Trzciński 已提交
37
    has_many :runner_namespaces, inverse_of: :runner
38
    has_many :groups, through: :runner_namespaces
D
Douwe Maan 已提交
39 40 41 42 43

    has_one :last_build, ->() { order('id DESC') }, class_name: 'Ci::Build'

    before_validation :set_default_values

44 45 46
    scope :active, -> { where(active: true) }
    scope :paused, -> { where(active: false) }
    scope :online, -> { where('contacted_at > ?', contact_time_deadline) }
47 48 49 50 51 52
    # The following query using negation is cheaper than using `contacted_at <= ?`
    # because there are less runners online than have been created. The
    # resulting query is quickly finding online ones and then uses the regular
    # indexed search and rejects the ones that are in the previous set. If we
    # did `contacted_at <= ?` the query would effectively have to do a seq
    # scan.
A
Alexis Reigel 已提交
53
    scope :offline, -> { where.not(id: online) }
54
    scope :ordered, -> { order(id: :desc) }
55

56
    # BACKWARD COMPATIBILITY: There are needed to maintain compatibility with `AVAILABLE_SCOPES` used by `lib/api/runners.rb`
57
    scope :deprecated_shared, -> { instance_type }
58
    # this should get replaced with `project_type.or(group_type)` once using Rails5
59
    scope :deprecated_specific, -> { where(runner_type: [runner_types[:project_type], runner_types[:group_type]]) }
60

61 62 63
    scope :belonging_to_project, -> (project_id) {
      joins(:runner_projects).where(ci_runner_projects: { project_id: project_id })
    }
64

65
    scope :belonging_to_parent_group_of_project, -> (project_id) {
66 67 68 69
      project_groups = ::Group.joins(:projects).where(projects: { id: project_id })
      hierarchy_groups = Gitlab::GroupHierarchy.new(project_groups).base_and_ancestors

      joins(:groups).where(namespaces: { id: hierarchy_groups })
70
    }
71

72
    scope :owned_or_instance_wide, -> (project_id) do
73 74 75 76 77 78
      from_union(
        [
          belonging_to_project(project_id),
          belonging_to_parent_group_of_project(project_id),
          instance_type
        ],
79 80
        remove_duplicates: false
      )
81 82
    end

L
Lin Jen-Shin 已提交
83
    scope :assignable_for, ->(project) do
L
Lin Jen-Shin 已提交
84 85
      # FIXME: That `to_sql` is needed to workaround a weird Rails bug.
      #        Without that, placeholders would miss one and couldn't match.
86 87 88 89 90
      #
      # We use "unscoped" here so that any current Ci::Runner filters don't
      # apply to the inner query, which is not necessary.
      exclude_runners = unscoped { project.runners.select(:id) }.to_sql

91
      where(locked: false)
92
        .where.not("ci_runners.id IN (#{exclude_runners})")
93
        .project_type
94 95
    end

96 97 98
    scope :order_contacted_at_asc, -> { order(contacted_at: :asc) }
    scope :order_created_at_desc, -> { order(created_at: :desc) }

99
    validate :tag_constraints
100
    validates :access_level, presence: true
101
    validates :runner_type, presence: true
102

103 104 105 106 107
    validate :no_projects, unless: :project_type?
    validate :no_groups, unless: :group_type?
    validate :any_project, if: :project_type?
    validate :exactly_one_group, if: :group_type?

D
Douwe Maan 已提交
108 109
    acts_as_taggable

110 111
    after_destroy :cleanup_runner_queue

112
    cached_attr_reader :version, :revision, :platform, :architecture, :ip_address, :contacted_at
113

114
    chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout
115

116 117 118 119
    validates :maximum_timeout, allow_nil: true,
                                numericality: { greater_than_or_equal_to: 600,
                                                message: 'needs to be at least 10 minutes' }

120 121 122 123 124 125 126 127 128 129 130 131
    # Searches for runners matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # This method performs a *partial* match on tokens, thus a query for "a"
    # will match any runner where the token contains the letter "a". As a result
    # you should *not* use this method for non-admin purposes as otherwise users
    # might be able to query a list of all runners.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation.
D
Douwe Maan 已提交
132
    def self.search(query)
133
      fuzzy_search(query, [:token, :description])
D
Douwe Maan 已提交
134 135
    end

A
Alessio Caiazza 已提交
136 137 138 139
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

140 141 142 143 144 145 146 147
    def self.order_by(order)
      if order == 'contacted_asc'
        order_contacted_at_asc
      else
        order_created_at_desc
      end
    end

D
Douwe Maan 已提交
148 149 150 151 152
    def set_default_values
      self.token = SecureRandom.hex(15) if self.token.blank?
    end

    def assign_to(project, current_user = nil)
153
      if instance_type?
154
        self.runner_type = :project_type
155 156
      elsif group_type?
        raise ArgumentError, 'Transitioning a group runner to a project runner is not supported'
157 158
      end

159 160 161 162 163 164 165 166 167
      begin
        transaction do
          self.projects << project
          self.save!
        end
      rescue ActiveRecord::RecordInvalid => e
        self.errors.add(:assign_to, e.message)
        false
      end
D
Douwe Maan 已提交
168 169 170
    end

    def display_name
171
      return short_sha if description.blank?
D
Douwe Maan 已提交
172 173 174 175

      description
    end

176
    def online?
177
      contacted_at && contacted_at > self.class.contact_time_deadline
178 179 180 181 182 183 184 185 186 187 188 189
    end

    def status
      if contacted_at.nil?
        :not_connected
      elsif active?
        online? ? :online : :offline
      else
        :paused
      end
    end

D
Douwe Maan 已提交
190 191 192 193
    def belongs_to_one_project?
      runner_projects.count == 1
    end

194
    def assigned_to_group?
195
      runner_namespaces.any?
A
Alexis Reigel 已提交
196 197
    end

198
    def assigned_to_project?
A
Alexis Reigel 已提交
199 200 201
      runner_projects.any?
    end

202
    def can_pick?(build)
203
      return false if self.ref_protected? && !build.protected?
S
Shinya Maeda 已提交
204

205
      assignable_for?(build.project_id) && accepting_tags?(build)
206 207
    end

D
Douwe Maan 已提交
208 209 210 211 212
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
213
      token[0...8] if token
D
Douwe Maan 已提交
214
    end
215 216 217 218

    def has_tags?
      tag_list.any?
    end
219

220
    def predefined_variables
221 222 223 224
      Gitlab::Ci::Variables::Collection.new
        .append(key: 'CI_RUNNER_ID', value: id.to_s)
        .append(key: 'CI_RUNNER_DESCRIPTION', value: description)
        .append(key: 'CI_RUNNER_TAGS', value: tag_list.to_s)
225 226
    end

K
Kim "BKC" Carlbäcker 已提交
227
    def tick_runner_queue
228
      SecureRandom.hex.tap do |new_update|
K
Kamil Trzcinski 已提交
229
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
230
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
231
      end
232 233
    end

K
Kim "BKC" Carlbäcker 已提交
234
    def ensure_runner_queue_value
K
Kamil Trzcinski 已提交
235 236 237
      new_value = SecureRandom.hex
      ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_value,
        expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false)
K
Kim "BKC" Carlbäcker 已提交
238 239
    end

240
    def runner_queue_value_latest?(value)
K
Kim "BKC" Carlbäcker 已提交
241
      ensure_runner_queue_value == value if value.present?
242 243
    end

244
    def update_cached_info(values)
245
      values = values&.slice(:version, :revision, :platform, :architecture, :ip_address) || {}
246
      values[:contacted_at] = Time.now
247

248
      cache_attributes(values)
249

K
Kamil Trzciński 已提交
250 251
      # We save data without validation, it will always change due to `contacted_at`
      self.update_columns(values) if persist_cached_data?
252 253
    end

254
    def pick_build!(build)
255 256 257 258 259
      if can_pick?(build)
        tick_runner_queue
      end
    end

260 261
    private

262
    def cleanup_runner_queue
263
      Gitlab::Redis::Queues.with do |redis|
264 265 266 267
        redis.del(runner_queue_key)
      end
    end

K
Kim "BKC" Carlbäcker 已提交
268
    def runner_queue_key
K
Kim "BKC" Carlbäcker 已提交
269
      "runner:build_queue:#{self.token}"
270 271
    end

272 273 274
    def persist_cached_data?
      # Use a random threshold to prevent beating DB updates.
      # It generates a distribution between [40m, 80m].
275

276 277 278 279 280 281 282
      contacted_at_max_age = UPDATE_DB_RUNNER_INFO_EVERY + Random.rand(UPDATE_DB_RUNNER_INFO_EVERY)

      real_contacted_at = read_attribute(:contacted_at)
      real_contacted_at.nil? ||
        (Time.now - real_contacted_at) >= contacted_at_max_age
    end

283
    def tag_constraints
284 285 286 287 288
      unless has_tags? || run_untagged?
        errors.add(:tags_list,
          'can not be empty when runner is not allowed to pick untagged jobs')
      end
    end
289

290
    def assignable_for?(project_id)
291
      self.class.owned_or_instance_wide(project_id).where(id: self.id).any?
292 293
    end

294 295
    def no_projects
      if projects.any?
296
        errors.add(:runner, 'cannot have projects assigned')
297 298 299 300 301
      end
    end

    def no_groups
      if groups.any?
302 303 304 305 306 307 308 309 310 311 312 313 314
        errors.add(:runner, 'cannot have groups assigned')
      end
    end

    def any_project
      unless projects.any?
        errors.add(:runner, 'needs to be assigned to at least one project')
      end
    end

    def exactly_one_group
      unless groups.one?
        errors.add(:runner, 'needs to be assigned to exactly one group')
315
      end
316
    end
317

318 319
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
320
    end
D
Douwe Maan 已提交
321 322
  end
end