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
|
|
|
|
|
2022-05-24 20:09:46 -04:00
|
|
|
has_many :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) }
|
2022-06-16 05:09:15 -04:00
|
|
|
scope :has_vulnerabilities, -> (value = true) { where(has_vulnerabilities: value) }
|
2020-09-11 08:08:50 -04:00
|
|
|
|
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
|
2022-06-07 08:08:05 -04:00
|
|
|
|
|
|
|
def to_ability_name
|
|
|
|
:cluster
|
|
|
|
end
|
2020-07-20 05:09:22 -04:00
|
|
|
end
|
|
|
|
end
|
2022-06-23 17:09:13 -04:00
|
|
|
|
|
|
|
Clusters::Agent.prepend_mod_with('Clusters::Agent')
|