runner.rb 3.12 KB
Newer Older
1 2
# == Schema Information
#
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3
# Table name: ci_runners
4 5
#
#  id           :integer          not null, primary key
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
6
#  token        :string
7 8
#  created_at   :datetime
#  updated_at   :datetime
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
9
#  description  :string
10 11 12
#  contacted_at :datetime
#  active       :boolean          default(TRUE), not null
#  is_shared    :boolean          default(FALSE)
Zeger-Jan van de Weg's avatar
Zeger-Jan van de Weg committed
13 14 15 16 17
#  name         :string
#  version      :string
#  revision     :string
#  platform     :string
#  architecture :string
18 19 20 21 22
#

module Ci
  class Runner < ActiveRecord::Base
    extend Ci::Model
23 24

    LAST_CONTACT_TIME = 5.minutes.ago
25
    AVAILABLE_SCOPES = ['specific', 'shared', 'active', 'paused', 'online']
26

27 28
    has_many :builds, class_name: 'Ci::Build'
    has_many :runner_projects, dependent: :destroy, class_name: 'Ci::RunnerProject'
29
    has_many :projects, through: :runner_projects, class_name: '::Project', foreign_key: :gl_project_id
30 31 32 33 34 35 36 37 38

    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) }
39
    scope :online, ->() { where('contacted_at > ?', LAST_CONTACT_TIME) }
40
    scope :ordered, ->() { order(id: :desc) }
41

42 43
    scope :owned_or_shared, ->(project_id) do
      joins('LEFT JOIN ci_runner_projects ON ci_runner_projects.runner_id = ci_runners.id')
44
        .where("ci_runner_projects.gl_project_id = :project_id OR ci_runners.is_shared = true", project_id: project_id)
45 46
    end

47 48
    acts_as_taggable

49 50 51 52 53 54 55 56 57 58 59 60
    # 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.
61
    def self.search(query)
62
      t = arel_table
63 64 65
      pattern = "%#{query}%"

      where(t[:token].matches(pattern).or(t[:description].matches(pattern)))
66 67 68 69 70 71 72 73 74
    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
75
      project.runner_projects.create!(runner_id: self.id)
76 77 78
    end

    def display_name
Kamil Trzcinski's avatar
Kamil Trzcinski committed
79
      return short_sha unless !description.blank?
80 81 82 83 84 85 86 87

      description
    end

    def shared?
      is_shared
    end

88 89 90 91 92 93 94 95 96 97 98 99 100 101
    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

102 103 104 105 106 107 108 109 110 111 112 113 114
    def belongs_to_one_project?
      runner_projects.count == 1
    end

    def specific?
      !shared?
    end

    def only_for?(project)
      projects == [project]
    end

    def short_sha
Kamil Trzcinski's avatar
Kamil Trzcinski committed
115
      token[0...8] if token
116 117 118
    end
  end
end