2020-07-20 05:09:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class Agent < ApplicationRecord
|
|
|
|
self.table_name = 'cluster_agents'
|
|
|
|
|
2021-12-08 07:13:04 -05:00
|
|
|
INACTIVE_AFTER = 1.hour.freeze
|
2022-01-14 04:14:36 -05:00
|
|
|
ACTIVITY_EVENT_LIMIT = 200
|
2021-12-08 07:13:04 -05:00
|
|
|
|
2021-02-16 19:09:19 -05:00
|
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
2020-07-20 05:09:22 -04:00
|
|
|
belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project
|
|
|
|
|
|
|
|
has_many :agent_tokens, class_name: 'Clusters::AgentToken'
|
2021-04-21 05:09:15 -04:00
|
|
|
has_many :last_used_agent_tokens, -> { order_last_used_at_desc }, class_name: 'Clusters::AgentToken', inverse_of: :agent
|
2020-07-20 05:09:22 -04:00
|
|
|
|
2021-08-23 05:10:23 -04: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 08:09:35 -04: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 16:10:02 -05:00
|
|
|
has_many :activity_events, -> { in_timeline_order }, class_name: 'Clusters::Agents::ActivityEvent', inverse_of: :agent
|
|
|
|
|
2020-10-19 11:08:58 -04:00
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
2020-09-11 08:08:50 -04:00
|
|
|
scope :with_name, -> (name) { where(name: name) }
|
|
|
|
|
2020-07-23 02:09:19 -04: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 07:09:14 -05:00
|
|
|
|
|
|
|
def has_access_to?(requested_project)
|
|
|
|
requested_project == project
|
|
|
|
end
|
2021-12-08 07:13:04 -05:00
|
|
|
|
2021-12-21 22:15:20 -05:00
|
|
|
def connected?
|
|
|
|
agent_tokens.active.where("last_used_at > ?", INACTIVE_AFTER.ago).exists?
|
2021-12-08 07:13:04 -05:00
|
|
|
end
|
2022-01-14 04:14:36 -05:00
|
|
|
|
|
|
|
def activity_event_deletion_cutoff
|
|
|
|
# Order is defined by the association
|
|
|
|
activity_events
|
|
|
|
.offset(ACTIVITY_EVENT_LIMIT - 1)
|
|
|
|
.pick(:recorded_at)
|
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|