2020-07-20 09:09:22 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class Agent < ApplicationRecord
|
|
|
|
self.table_name = 'cluster_agents'
|
|
|
|
|
2021-12-08 12:13:04 +00:00
|
|
|
INACTIVE_AFTER = 1.hour.freeze
|
|
|
|
|
2021-02-17 00:09:19 +00:00
|
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
2020-07-20 09:09:22 +00:00
|
|
|
belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project
|
|
|
|
|
|
|
|
has_many :agent_tokens, class_name: 'Clusters::AgentToken'
|
2021-04-21 09:09:15 +00:00
|
|
|
has_many :last_used_agent_tokens, -> { order_last_used_at_desc }, class_name: 'Clusters::AgentToken', inverse_of: :agent
|
2020-07-20 09:09:22 +00:00
|
|
|
|
2021-08-23 09:10:23 +00:00
|
|
|
has_many :group_authorizations, class_name: 'Clusters::Agents::GroupAuthorization'
|
|
|
|
has_many :authorized_groups, class_name: '::Group', through: :group_authorizations, source: :group
|
|
|
|
|
2021-09-16 12:09:35 +00:00
|
|
|
has_many :project_authorizations, class_name: 'Clusters::Agents::ProjectAuthorization'
|
|
|
|
has_many :authorized_projects, class_name: '::Project', through: :project_authorizations, source: :project
|
|
|
|
|
2021-11-23 21:10:02 +00:00
|
|
|
has_many :activity_events, -> { in_timeline_order }, class_name: 'Clusters::Agents::ActivityEvent', inverse_of: :agent
|
|
|
|
|
2020-10-19 15:08:58 +00:00
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
2020-09-11 12:08:50 +00:00
|
|
|
scope :with_name, -> (name) { where(name: name) }
|
|
|
|
|
2020-07-23 06:09:19 +00:00
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: 63 },
|
|
|
|
uniqueness: { scope: :project_id },
|
|
|
|
format: {
|
|
|
|
with: Gitlab::Regex.cluster_agent_name_regex,
|
|
|
|
message: Gitlab::Regex.cluster_agent_name_regex_message
|
|
|
|
}
|
2020-11-27 12:09:14 +00:00
|
|
|
|
|
|
|
def has_access_to?(requested_project)
|
|
|
|
requested_project == project
|
|
|
|
end
|
2021-12-08 12:13:04 +00:00
|
|
|
|
2021-12-22 03:15:20 +00:00
|
|
|
def connected?
|
|
|
|
agent_tokens.active.where("last_used_at > ?", INACTIVE_AFTER.ago).exists?
|
2021-12-08 12:13:04 +00:00
|
|
|
end
|
2020-07-20 09:09:22 +00:00
|
|
|
end
|
|
|
|
end
|