runner.rb 4.6 KB
Newer Older
D
Douwe Maan 已提交
1 2 3
module Ci
  class Runner < ActiveRecord::Base
    extend Ci::Model
4

5
    RUNNER_QUEUE_EXPIRY_TIME = 60.minutes
6
    LAST_CONTACT_TIME = 1.hour.ago
7
    AVAILABLE_SCOPES = %w[specific shared active paused online]
8
    FORM_EDITABLE = %i[description tag_list active run_untagged locked]
9

10 11 12
    has_many :builds
    has_many :runner_projects, dependent: :destroy
    has_many :projects, through: :runner_projects, foreign_key: :gl_project_id
D
Douwe Maan 已提交
13 14 15 16 17 18 19 20 21

    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) }
22
    scope :online, ->() { where('contacted_at > ?', LAST_CONTACT_TIME) }
K
Kamil Trzcinski 已提交
23
    scope :ordered, ->() { order(id: :desc) }
D
Douwe Maan 已提交
24

K
Kamil Trzcinski 已提交
25
    after_save :tick_runner_queue, if: :form_editable_changed?
26

27 28
    scope :owned_or_shared, ->(project_id) do
      joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
29
        .where("ci_runner_projects.gl_project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
30 31
    end

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

39
    validate :tag_constraints
40

D
Douwe Maan 已提交
41 42
    acts_as_taggable

43 44 45 46 47 48 49 50 51 52 53 54
    # 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 已提交
55
    def self.search(query)
56
      t = arel_table
57 58 59
      pattern = "%#{query}%"

      where(t[:token].matches(pattern).or(t[:description].matches(pattern)))
D
Douwe Maan 已提交
60 61 62 63 64 65 66 67 68
    end

    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
69
      project.runner_projects.create(runner_id: self.id)
D
Douwe Maan 已提交
70 71 72
    end

    def display_name
73
      return short_sha if description.blank?
D
Douwe Maan 已提交
74 75 76 77 78 79 80 81

      description
    end

    def shared?
      is_shared
    end

82 83 84 85 86 87 88 89 90 91 92 93 94 95
    def online?
      contacted_at && contacted_at > LAST_CONTACT_TIME
    end

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

D
Douwe Maan 已提交
96 97 98 99 100 101 102 103
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

104
    def can_pick?(build)
L
Lin Jen-Shin 已提交
105
      assignable_for?(build.project) && accepting_tags?(build)
106 107
    end

D
Douwe Maan 已提交
108 109 110 111 112
    def only_for?(project)
      projects == [project]
    end

    def short_sha
K
Kamil Trzcinski 已提交
113
      token[0...8] if token
D
Douwe Maan 已提交
114
    end
115 116 117 118

    def has_tags?
      tag_list.any?
    end
119

120 121 122 123 124 125 126 127
    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 已提交
128
    def tick_runner_queue
K
Kamil Trzcinski 已提交
129
      new_update = SecureRandom.hex
130
      Gitlab::Redis.with { |redis| redis.set(runner_queue_key, new_update, ex: RUNNER_QUEUE_EXPIRY_TIME) }
131 132 133
      new_update
    end

K
Kim "BKC" Carlbäcker 已提交
134 135
    def ensure_runner_queue_value
      Gitlab::Redis.with do |redis|
K
Kamil Trzcinski 已提交
136
        value = SecureRandom.hex
137
        redis.set(runner_queue_key, value, ex: RUNNER_QUEUE_EXPIRY_TIME, nx: true)
K
Kim "BKC" Carlbäcker 已提交
138
        redis.get(runner_queue_key)
K
Kim "BKC" Carlbäcker 已提交
139 140 141 142 143
      end
    end

    def is_runner_queue_value_latest?(value)
      ensure_runner_queue_value == value if value.present?
144 145
    end

146 147
    private

K
Kim "BKC" Carlbäcker 已提交
148
    def runner_queue_key
K
Kim "BKC" Carlbäcker 已提交
149
      "runner:build_queue:#{self.token}"
150 151
    end

K
Kamil Trzcinski 已提交
152 153 154 155 156 157
    def form_editable_changed?
      FORM_EDITABLE.any? do |editable|
        public_send("#{editable}_changed?")
      end
    end

158
    def tag_constraints
159 160 161 162 163
      unless has_tags? || run_untagged?
        errors.add(:tags_list,
          'can not be empty when runner is not allowed to pick untagged jobs')
      end
    end
164

L
Lin Jen-Shin 已提交
165
    def assignable_for?(project)
166 167 168
      !locked? || projects.exists?(id: project.id)
    end

169 170
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
171
    end
D
Douwe Maan 已提交
172 173
  end
end