BigW Consortium Gitlab

runner.rb 4.7 KB
Newer Older
1 2 3
module Ci
  class Runner < ActiveRecord::Base
    extend Ci::Model
4

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

10
    has_many :builds
11
    has_many :runner_projects, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
12
    has_many :projects, through: :runner_projects
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 > ?', contact_time_deadline) }
23
    scope :ordered, ->() { order(id: :desc) }
24

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

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

37
    validate :tag_constraints
38

39 40
    acts_as_taggable

41 42
    after_destroy :cleanup_runner_queue

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.
55
    def self.search(query)
56
      t = arel_table
57 58 59
      pattern = "%#{query}%"

      where(t[:token].matches(pattern).or(t[:description].matches(pattern)))
60 61
    end

62 63 64 65
    def self.contact_time_deadline
      ONLINE_CONTACT_TIMEOUT.ago
    end

66 67 68 69 70 71 72
    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
73
      project.runner_projects.create(runner_id: self.id)
74 75 76
    end

    def display_name
77
      return short_sha if description.blank?
78 79 80 81 82 83 84 85

      description
    end

    def shared?
      is_shared
    end

86
    def online?
87
      contacted_at && contacted_at > self.class.contact_time_deadline
88 89 90 91 92 93 94 95 96 97 98 99
    end

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

100 101 102 103 104 105 106 107
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

108
    def can_pick?(build)
Lin Jen-Shin committed
109
      assignable_for?(build.project) && accepting_tags?(build)
110 111
    end

112 113 114 115 116
    def only_for?(project)
      projects == [project]
    end

    def short_sha
Kamil Trzcinski committed
117
      token[0...8] if token
118
    end
119 120 121 122

    def has_tags?
      tag_list.any?
    end
123

124 125 126 127 128 129 130 131
    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

Kim "BKC" Carlbäcker committed
132
    def tick_runner_queue
133
      SecureRandom.hex.tap do |new_update|
134
        ::Gitlab::Workhorse.set_key_and_notify(runner_queue_key, new_update,
135
          expire: RUNNER_QUEUE_EXPIRY_TIME, overwrite: true)
136
      end
137 138
    end

Kim "BKC" Carlbäcker committed
139
    def ensure_runner_queue_value
140 141 142
      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 committed
143 144
    end

145
    def runner_queue_value_latest?(value)
Kim "BKC" Carlbäcker committed
146
      ensure_runner_queue_value == value if value.present?
147 148
    end

149 150
    private

151
    def cleanup_runner_queue
152
      Gitlab::Redis::Queues.with do |redis|
153 154 155 156
        redis.del(runner_queue_key)
      end
    end

Kim "BKC" Carlbäcker committed
157
    def runner_queue_key
158
      "runner:build_queue:#{self.token}"
159 160
    end

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

Lin Jen-Shin committed
168
    def assignable_for?(project)
169 170 171
      !locked? || projects.exists?(id: project.id)
    end

172 173
    def accepting_tags?(build)
      (run_untagged? || build.has_tags?) && (build.tag_list - tag_list).empty?
174
    end
175 176
  end
end