Read about our upcoming Code of Conduct on this issue

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

Douwe Maan's avatar
Douwe Maan committed
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

12
    RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
Alessio Caiazza's avatar
Alessio Caiazza committed
13
    ONLINE_CONTACT_TIMEOUT = 1.hour
14
    UPDATE_DB_RUNNER_INFO_EVERY = 40.minutes
15
16
17
    AVAILABLE_TYPES = %w[specific shared].freeze
    AVAILABLE_STATUSES = %w[active paused online offline].freeze
    AVAILABLE_SCOPES = (AVAILABLE_TYPES + AVAILABLE_STATUSES).freeze
18
    FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level maximum_timeout_human_readable].freeze
19

20
21
    ignore_column :is_shared

22
    has_many :builds
23
    has_many :runner_projects, inverse_of: :runner, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
24
    has_many :projects, through: :runner_projects
25
    has_many :runner_namespaces, inverse_of: :runner
26
    has_many :groups, through: :runner_namespaces
Douwe Maan's avatar
Douwe Maan committed
27
28
29
30
31

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

    before_validation :set_default_values

32
33
34
    scope :active, -> { where(active: true) }
    scope :paused, -> { where(active: false) }
    scope :online, -> { where('contacted_at > ?', contact_time_deadline) }
35
36
37
38
39
40
    # 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.
41
    scope :offline, -> { where.not(id: online) }
42
    scope :ordered, -> { order(id: :desc) }
43

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

49
50
51
    scope :belonging_to_project, -> (project_id) {
      joins(:runner_projects).where(ci_runner_projects: { project_id: project_id })
    }
52

53
    scope :belonging_to_parent_group_of_project, -> (project_id) {
54
55
56
57
      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 })
58
    }
59

60
    scope :owned_or_instance_wide, -> (project_id) do
61
62
63
64
65
66
      from_union(
        [
          belonging_to_project(project_id),
          belonging_to_parent_group_of_project(project_id),
          instance_type
        ],
67
68
        remove_duplicates: false
      )
69
70
    end

Lin Jen-Shin's avatar
Lin Jen-Shin committed
71
    scope :assignable_for, ->(project) do
72
73
      # FIXME: That `to_sql` is needed to workaround a weird Rails bug.
      #        Without that, placeholders would miss one and couldn't match.
74
75
76
77
78
      #
      # 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

79
      where(locked: false)
80
        .where.not("ci_runners.id IN (#{exclude_runners})")
81
        .project_type
82
83
    end

84
85
86
    scope :order_contacted_at_asc, -> { order(contacted_at: :asc) }
    scope :order_created_at_desc, -> { order(created_at: :desc) }

87
    validate :tag_constraints
88
    validates :access_level, presence: true
89
    validates :runner_type, presence: true
90

91
92
93
94
95
    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?

Douwe Maan's avatar
Douwe Maan committed
96
97
    acts_as_taggable

98
99
    after_destroy :cleanup_runner_queue

100
    enum access_level: {
101
102
      not_protected: 0,
      ref_protected: 1
103
104
    }

105
106
107
108
109
110
    enum runner_type: {
      instance_type: 1,
      group_type: 2,
      project_type: 3
    }

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

113
    chronic_duration_attr :maximum_timeout_human_readable, :maximum_timeout
114

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

119
120
121
122
123
124
125
126
127
128
129
130
    # 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.
Douwe Maan's avatar
Douwe Maan committed
131
    def self.search(query)
132
      fuzzy_search(query, [:token, :description])
Douwe Maan's avatar
Douwe Maan committed
133
134
    end

Alessio Caiazza's avatar
Alessio Caiazza committed
135
136
137
138
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

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

Douwe Maan's avatar
Douwe Maan committed
147
148
149
150
151
    def set_default_values
      self.token = SecureRandom.hex(15) if self.token.blank?
    end

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

158
159
160
161
162
163
164
165
166
      begin
        transaction do
          self.projects << project
          self.save!
        end
      rescue ActiveRecord::RecordInvalid => e
        self.errors.add(:assign_to, e.message)
        false
      end
Douwe Maan's avatar
Douwe Maan committed
167
168
169
    end

    def display_name
170
      return short_sha if description.blank?
Douwe Maan's avatar
Douwe Maan committed
171
172
173
174

      description
    end

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

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

Douwe Maan's avatar
Douwe Maan committed
189
190
191
192
    def belongs_to_one_project?
      runner_projects.count == 1
    end

193
    def assigned_to_group?
194
      runner_namespaces.any?
Alexis Reigel's avatar
Alexis Reigel committed
195
196
    end

197
    def assigned_to_project?
Alexis Reigel's avatar
Alexis Reigel committed
198
199
200
      runner_projects.any?
    end

201
    def can_pick?(build)
202
      return false if self.ref_protected? && !build.protected?
Shinya Maeda's avatar
Shinya Maeda committed
203

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

Douwe Maan's avatar
Douwe Maan committed
207
208
209
210
211
    def only_for?(project)
      projects == [project]
    end

    def short_sha
Kamil Trzcinski's avatar
Kamil Trzcinski committed
212
      token[0...8] if token
Douwe Maan's avatar
Douwe Maan committed
213
    end
214
215
216
217

    def has_tags?
      tag_list.any?
    end
218

219
    def predefined_variables
220
221
222
223
      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)
224
225
    end

Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
226
    def tick_runner_queue
227
      SecureRandom.hex.tap do |new_update|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
228
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
229
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
230
      end
231
232
    end

Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
233
    def ensure_runner_queue_value
Kamil Trzcinski's avatar
Kamil Trzcinski committed
234
235
236
      new_value = SecureRandom.hex
      ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_value,
        expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: false)
Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
237
238
    end

239
    def runner_queue_value_latest?(value)
Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
240
      ensure_runner_queue_value == value if value.present?
241
242
    end

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

247
      cache_attributes(values)
248

Kamil Trzciński's avatar
Kamil Trzciński committed
249
250
      # We save data without validation, it will always change due to `contacted_at`
      self.update_columns(values) if persist_cached_data?
251
252
    end

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

259
260
    private

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

Kim "BKC" Carlbäcker's avatar
Kim "BKC" Carlbäcker committed
267
    def runner_queue_key
268
      "runner:build_queue:#{self.token}"
269
270
    end

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

275
276
277
278
279
280
281
      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

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

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

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

    def no_groups
      if groups.any?
301
302
303
304
305
306
307
308
309
310
311
312
313
        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')
314
      end
315
    end
316

317
318
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
319
    end
Douwe Maan's avatar
Douwe Maan committed
320
321
  end
end