runner.rb 4.9 KB
Newer Older
D
Douwe Maan 已提交
1 2
module Ci
  class Runner < ActiveRecord::Base
3
    extend Gitlab::Ci::Model
4
    include Gitlab::SQL::Pattern
5

6
    RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
A
Alessio Caiazza 已提交
7
    ONLINE_CONTACT_TIMEOUT = 1.hour
D
Douwe Maan 已提交
8
    AVAILABLE_SCOPES = %w[specific shared active paused online].freeze
9
    FORM_EDITABLE = %i[description tag_list active run_untagged locked access_level].freeze
10

11
    has_many :builds
12
    has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
K
Kamil Trzciński 已提交
13
    has_many :projects, through: :runner_projects
D
Douwe Maan 已提交
14 15 16 17 18 19 20 21 22

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

    before_validation :set_default_values

    scope :specific, ->() { where(is_shared: false) }
    scope :shared, ->() { where(is_shared: true) }
    scope :active, ->() { where(active: true) }
    scope :paused, ->() { where(active: false) }
A
Alessio Caiazza 已提交
23
    scope :online, ->() { where('contacted_at > ?', contact_time_deadline) }
K
Kamil Trzcinski 已提交
24
    scope :ordered, ->() { order(id: :desc) }
D
Douwe Maan 已提交
25

26
    scope :owned_or_shared, ->(project_id) do
D
Douwe Maan 已提交
27
      joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
K
Kamil Trzciński 已提交
28
        .where("ci_runner_projects.project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
29 30
    end

L
Lin Jen-Shin 已提交
31
    scope :assignable_for, ->(project) do
L
Lin Jen-Shin 已提交
32 33
      # FIXME: That `to_sql` is needed to workaround a weird Rails bug.
      #        Without that, placeholders would miss one and couldn't match.
34 35
      where(locked: false)
        .where.not("id IN (#{project.runners.select(:id).to_sql})").specific
36 37
    end

38
    validate :tag_constraints
39
    validates :access_level, presence: true
40

D
Douwe Maan 已提交
41 42
    acts_as_taggable

43 44
    after_destroy :cleanup_runner_queue

45
    enum access_level: {
46 47
      not_protected: 0,
      ref_protected: 1
48 49
    }

50 51 52 53 54 55 56 57 58 59 60 61
    # 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 已提交
62
    def self.search(query)
63
      t = arel_table
64
      pattern = to_pattern(query)
65 66

      where(t[:token].matches(pattern).or(t[:description].matches(pattern)))
D
Douwe Maan 已提交
67 68
    end

A
Alessio Caiazza 已提交
69 70 71 72
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

D
Douwe Maan 已提交
73 74 75 76 77 78 79
    def set_default_values
      self.token = SecureRandom.hex(15) if self.token.blank?
    end

    def assign_to(project, current_user = nil)
      self.is_shared = false if shared?
      self.save
80
      project.runner_projects.create(runner_id: self.id)
D
Douwe Maan 已提交
81 82 83
    end

    def display_name
84
      return short_sha if description.blank?
D
Douwe Maan 已提交
85 86 87 88 89 90 91 92

      description
    end

    def shared?
      is_shared
    end

93
    def online?
A
Alessio Caiazza 已提交
94
      contacted_at && contacted_at > self.class.contact_time_deadline
95 96 97 98 99 100 101 102 103 104 105 106
    end

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

D
Douwe Maan 已提交
107 108 109 110 111 112 113 114
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

115
    def can_pick?(build)
116
      return false if self.ref_protected? && !build.protected?
S
Shinya Maeda 已提交
117

L
Lin Jen-Shin 已提交
118
      assignable_for?(build.project) && accepting_tags?(build)
119 120
    end

D
Douwe Maan 已提交
121 122 123 124 125
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
126
      token[0...8] if token
D
Douwe Maan 已提交
127
    end
128 129 130 131

    def has_tags?
      tag_list.any?
    end
132

133 134 135 136 137 138 139 140
    def predefined_variables
      [
        { key: 'CI_RUNNER_ID', value: id.to_s, public: true },
        { key: 'CI_RUNNER_DESCRIPTION', value: description, public: true },
        { key: 'CI_RUNNER_TAGS', value: tag_list.to_s, public: true }
      ]
    end

K
Kim "BKC" Carlbäcker 已提交
141
    def tick_runner_queue
142
      SecureRandom.hex.tap do |new_update|
K
Kamil Trzcinski 已提交
143
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
144
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
145
      end
146 147
    end

K
Kim "BKC" Carlbäcker 已提交
148
    def ensure_runner_queue_value
K
Kamil Trzcinski 已提交
149 150 151
      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 已提交
152 153
    end

154
    def runner_queue_value_latest?(value)
K
Kim "BKC" Carlbäcker 已提交
155
      ensure_runner_queue_value == value if value.present?
156 157
    end

158 159
    private

160
    def cleanup_runner_queue
161
      Gitlab::Redis::Queues.with do |redis|
162 163 164 165
        redis.del(runner_queue_key)
      end
    end

K
Kim "BKC" Carlbäcker 已提交
166
    def runner_queue_key
K
Kim "BKC" Carlbäcker 已提交
167
      "runner:build_queue:#{self.token}"
168 169
    end

170
    def tag_constraints
171 172 173 174 175
      unless has_tags? || run_untagged?
        errors.add(:tags_list,
          'can not be empty when runner is not allowed to pick untagged jobs')
      end
    end
176

L
Lin Jen-Shin 已提交
177
    def assignable_for?(project)
178
      is_shared? || projects.exists?(id: project.id)
179 180
    end

181 182
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
183
    end
D
Douwe Maan 已提交
184 185
  end
end