2020-10-24 23:57:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Clusters
|
|
|
|
class Agent < ApplicationRecord
|
|
|
|
self.table_name = 'cluster_agents'
|
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
INACTIVE_AFTER = 1.hour.freeze
|
2022-03-02 08:16:31 +05:30
|
|
|
ACTIVITY_EVENT_LIMIT = 200
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
belongs_to :created_by_user, class_name: 'User', optional: true
|
2020-10-24 23:57:45 +05:30
|
|
|
belongs_to :project, class_name: '::Project' # Otherwise, it will load ::Clusters::Project
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
has_many :agent_tokens, -> { order_last_used_at_desc }, class_name: 'Clusters::AgentToken', inverse_of: :agent
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
has_many :ci_access_group_authorizations, class_name: 'Clusters::Agents::Authorizations::CiAccess::GroupAuthorization'
|
|
|
|
has_many :ci_access_authorized_groups, class_name: '::Group', through: :ci_access_group_authorizations, source: :group
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2023-06-20 00:43:36 +05:30
|
|
|
has_many :ci_access_project_authorizations, class_name: 'Clusters::Agents::Authorizations::CiAccess::ProjectAuthorization'
|
|
|
|
has_many :ci_access_authorized_projects, class_name: '::Project', through: :ci_access_project_authorizations, source: :project
|
|
|
|
|
|
|
|
has_many :user_access_group_authorizations, class_name: 'Clusters::Agents::Authorizations::UserAccess::GroupAuthorization'
|
|
|
|
has_many :user_access_authorized_groups, class_name: '::Group', through: :user_access_group_authorizations, source: :group
|
|
|
|
|
|
|
|
has_many :user_access_project_authorizations, class_name: 'Clusters::Agents::Authorizations::UserAccess::ProjectAuthorization'
|
|
|
|
has_many :user_access_authorized_projects, class_name: '::Project', through: :user_access_project_authorizations, source: :project
|
2021-11-11 11:23:49 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
has_many :activity_events, -> { in_timeline_order }, class_name: 'Clusters::Agents::ActivityEvent', inverse_of: :agent
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
scope :ordered_by_name, -> { order(:name) }
|
2020-11-24 15:15:51 +05:30
|
|
|
scope :with_name, -> (name) { where(name: name) }
|
2022-07-23 23:45:48 +05:30
|
|
|
scope :has_vulnerabilities, -> (value = true) { where(has_vulnerabilities: value) }
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
|
|
|
}
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
def has_access_to?(requested_project)
|
|
|
|
requested_project == project
|
|
|
|
end
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-03-02 08:16:31 +05:30
|
|
|
def connected?
|
|
|
|
agent_tokens.active.where("last_used_at > ?", INACTIVE_AFTER.ago).exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def activity_event_deletion_cutoff
|
|
|
|
# Order is defined by the association
|
|
|
|
activity_events
|
|
|
|
.offset(ACTIVITY_EVENT_LIMIT - 1)
|
|
|
|
.pick(:recorded_at)
|
2022-01-26 12:08:38 +05:30
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
|
|
|
def to_ability_name
|
|
|
|
:cluster
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
2022-08-13 15:12:31 +05:30
|
|
|
|
|
|
|
Clusters::Agent.prepend_mod_with('Clusters::Agent')
|